You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
|238|[Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/)|[Solution](../master/src/main/java/com/stevesun/solutions/ProductofArrayExceptSelf.java)| O(n)|O(1) | Medium| Array
245
+
|238|[Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/)|[Solution](../master/src/main/java/com/stevesun/solutions/_238.java)| O(n)|O(1) | Medium| Array
246
246
|237|[Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/)|[Solution](../master/src/main/java/com/stevesun/solutions/_237.java)| O(1)|O(1) | Easy| LinkedList
247
247
|236|[Lowest Common Ancestor of a Binary Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/)|[Solution](../master/src/main/java/com/stevesun/solutions/_236.java)| O(n)|O(h) | Medium| DFS
248
248
|235|[Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/)|[Solution](../master/src/main/java/com/stevesun/solutions/_235.java)| O(h)|O(1) | Easy| DFS
@@ -321,7 +321,7 @@ Your ideas/fixes/algorithms are more than welcome!
321
321
|124|[Binary Tree Maximum Path Sum](https://leetcode.com/problems/binary-tree-maximum-path-sum/)|[Solution](../master/src/main/java/com/stevesun/solutions/BinaryTreeMaximumPathSum.java)| O(n)|O(h) | Hard | Tree
322
322
|123|[Best Time to Buy and Sell Stock III](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/)|[Solution](../master/src/main/java/com/stevesun/solutions/BestTimeToBuyAndSellStockIII.java)| O(?)|O(?) | Hard |
323
323
|122|[Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/)|[Solution](../master/src/main/java/com/stevesun/solutions/BestTimeToBuyAndSellStockII.java)| O(n)|O(1) | Medium | Greedy
324
-
|121|[Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)|[Solution](../master/src/main/java/com/stevesun/solutions/BestTimeToBuyAndSellStock.java)| O(n)|O(1) | Easy| DP
324
+
|121|[Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)|[Solution](../master/src/main/java/com/stevesun/solutions/_121.java)| O(n)|O(1) | Easy| DP
Copy file name to clipboardExpand all lines: src/main/java/com/stevesun/solutions/_121.java
+21-18Lines changed: 21 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -4,19 +4,36 @@
4
4
*
5
5
Say you have an array for which the ith element is the price of a given stock on day i.
6
6
7
-
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
7
+
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock),
8
+
design an algorithm to find the maximum profit.
8
9
9
10
Example 1:
10
11
Input: [7, 1, 5, 3, 6, 4]
11
12
Output: 5
12
13
13
14
max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)
14
-
Example 2:
15
+
16
+
17
+
Example 2:
15
18
Input: [7, 6, 4, 3, 1]
16
19
Output: 0
17
20
18
21
In this case, no transaction is done, i.e. max profit = 0.*/
19
-
publicclassBestTimeToBuyAndSellStock {
22
+
23
+
publicclass_121 {
24
+
/**Pretty straightforward, sell before you buy, keep a global maxProfit variable, update it along the way if necessary.*/
Copy file name to clipboardExpand all lines: src/main/java/com/stevesun/solutions/_238.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ Solve it without division and in O(n).
10
10
Follow up:
11
11
Could you solve it with constant space complexity? (Note: The output array does not count as extra space for the purpose of space complexity analysis.)
0 commit comments