Created
May 17, 2023 09:48
-
-
Save ritik-agrawal/a317854ae970c6df1f87c54d7e1aa6d2 to your computer and use it in GitHub Desktop.
This file contains 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
class Solution { | |
public int maxProfit(int[] prices) { | |
var ret = 0; | |
var bp = Integer.MAX_VALUE; | |
for (int sp : prices){ | |
if (bp < sp){ | |
var profit = sp - bp; | |
ret = Math.max(profit, ret); | |
} | |
bp = Math.min(bp, sp); | |
} | |
return ret; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Leet Code
Question: Given with an integer array with the ith index having prices of a particular stock on the day (i+1). We need to find the best time to buy and sell to maximize the profit.
Interview Question
The same question was asked in my interview with the company Name BeepKart.
Achievement
The above code was created by me and the code runtime is 2ms which beats 93% of the total submissions and beats 23% of the total submissions in terms of space complexity.