Skip to content

Commit 91dd214

Browse files
committed
feat: best-time-to-buy-and-sell-stock
1 parent abf9a39 commit 91dd214

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/best-time-to-buy-and-sell-stock/">week05-1.best-time-to-buy-and-sell-stock</a>
3+
* <li>Description: Return the maximum profit you can achieve from this transaction</li>
4+
* <li>Topics: Array, Dynamic Programming </li>
5+
* <li>Time Complexity: O(N), Runtime 2ms </li>
6+
* <li>Space Complexity: O(1), Memory 61.62MB </li>
7+
*/
8+
class Solution {
9+
public int maxProfit(int[] prices) {
10+
int min = prices[0];
11+
int profit = 0;
12+
for(int i=1; i<prices.length; i++) {
13+
profit = Math.max(profit, prices[i]-min);
14+
min = Math.min(min, prices[i]);
15+
}
16+
return profit;
17+
}
18+
}

0 commit comments

Comments
 (0)