We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 8246eb1 commit 73a6d43Copy full SHA for 73a6d43
best-time-to-buy-and-sell-stock/TonyKim9401.java
@@ -0,0 +1,16 @@
1
+// TC: O(n)
2
+// SC: O(1)
3
+class Solution {
4
+ public int maxProfit(int[] prices) {
5
+ int bestProfit = 0;
6
+ int buyPrice = prices[0];
7
+ for (int i = 1; i < prices.length; i++) {
8
+ if (buyPrice > prices[i]) {
9
+ buyPrice = prices[i];
10
+ continue;
11
+ }
12
+ bestProfit = Math.max(bestProfit, prices[i] - buyPrice);
13
14
+ return bestProfit;
15
16
+}
0 commit comments