Skip to content

Instantly share code, notes, and snippets.

@ritik-agrawal
Created May 17, 2023 09:48
Show Gist options
  • Save ritik-agrawal/a317854ae970c6df1f87c54d7e1aa6d2 to your computer and use it in GitHub Desktop.
Save ritik-agrawal/a317854ae970c6df1f87c54d7e1aa6d2 to your computer and use it in GitHub Desktop.
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;
}
}
@ritik-agrawal
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment