Created
May 3, 2025 00:28
-
-
Save joshuabenson/bddda41d00186bde241c5e6501b0cf73 to your computer and use it in GitHub Desktop.
Neetcode best time to sell stock
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
https://neetcode.io/problems/buy-and-sell-crypto | |
**/ | |
class Solution { | |
/** | |
* @param {number[]} prices | |
* @return {number} | |
*/ | |
maxProfit(prices) { | |
// storing indicies in a object | |
let leftPointer = 0, | |
rightPointer = 1, | |
maxProfit = 0; | |
while (rightPointer < prices.length) { | |
if (prices[rightPointer] > prices[leftPointer]) { | |
let profit = prices[rightPointer] - prices[leftPointer]; | |
maxProfit = Math.max(profit, maxProfit); | |
} else { | |
leftPointer = rightPointer; | |
} | |
rightPointer += 1; | |
} | |
return maxProfit; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment