Best Time to Buy and Sell Stock II Java LeetCode coding solution. One of Facebook’s most commonly asked interview questions according to LeetCode.
Coding Interviews Best Time to Buy and Sell Stock II (LeetCode) question and explanation.
This question is a commonly asked by the following companies: Facebook, Amazon, Microsoft, Adobe, Alibaba, and Bloomberg.
Link to problem: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/
Problem description: Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).
Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).
Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4. Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are engaging multiple transactions at the same time. You must sell before buying again.
Explanation: In this case, no transaction is done, i.e. max profit = 0.
Intuition behind solution: Find each place where we can make a profit. Therefore we are simply comparing the ith price with the i + 1 price at every index and buy and selling if the ith price is less than the i + 1 price. If this is the case, we should buy on the current day and a sell the next day which yields a profit of prices[i + 1] – prices[i].
My Desk Setup
Desk – https://bit.ly/3jfY195
Chair – https://amzn.to/2O9TM3r
Monitor – https://amzn.to/3rcSHGa
Webcam – https://amzn.to/2NUmwgi
Desktop – https://amzn.to/3tiySPL
Laptops – https://amzn.to/3aRoN3Z
iPad – https://amzn.to/2LlJzzJ
Keyboard – https://amzn.to/3jfbxdd
Mouse – https://amzn.to/36ElWtT
Wrist Rest – https://amzn.to/3trrHF4 (pls don’t buy this)
Mouse Pad – https://amzn.to/2Myz2lt
Microphone – https://amzn.to/3atNyTA
Lamp – https://amzn.to/3jjfZYp
Headphones – https://amzn.to/3tvr0KU (new model)
Headphone Hook – https://amzn.to/3tr8uTC
Blue Light Glasses – https://amzn.to/3cDVUdK
Wireless Charger – https://amzn.to/39LY1uu
Keyboard cable – https://amzn.to/2O5p2R5
Mic arm – https://amzn.to/3cECZj8
Audio interface – https://amzn.to/36HdWIi
Cloudlifter – https://amzn.to/36VO6kf
Laptop dock – https://amzn.to/2O2DsBw
Motherboard – https://amzn.to/3rkiWuA
Solid state – https://amzn.to/3rk5vuo
CPU cooler – https://amzn.to/3tnwwPA
CableMod – https://amzn.to/3tqbtM8
CPU – https://amzn.to/3auG1ns
Power supply – https://amzn.to/3trsAxo
RAM – https://amzn.to/39JZcuf
Designing Data-Intensive Applications – https://amzn.to/2YK4ek1
Clean Code – https://amzn.to/3txqfB5
Meditations – https://amzn.to/3cDa4fi
Support me on Patreon: https://www.patreon.com/KevinNaughtonJr
Follow me on GitHub: https://github.com/kdn251
Follow me on Instagram: https://www.instagram.com/programeme