Skip to content

Commit 7f4d6c3

Browse files
add 121
1 parent 4bcb469 commit 7f4d6c3

File tree

4 files changed

+28
-25
lines changed

4 files changed

+28
-25
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ Your ideas/fixes/algorithms are more than welcome!
242242
|243|[Shortest Word Distance](https://leetcode.com/problems/shortest-word-distance/)|[Solution](../master/src/main/java/com/stevesun/solutions/ShortestWordDistance.java) | O(n) | O(1) |
243243
|240|[Search a 2D Matrix II](https://leetcode.com/problems/search-a-2d-matrix-ii/)|[Solution](../master/src/main/java/com/stevesun/solutions/Searcha2DMatrixII.java)| O(log(m*n))|O(1) | Medium| Binary Search
244244
|239|[Sliding Window Maximum](https://leetcode.com/problems/sliding-window-maximum/)|[Solution](../master/src/main/java/com/stevesun/solutions/SlidingWindowMaximum.java)| O(nlogn)|O(k) | Hard| Heap
245-
|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
246246
|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
247247
|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
248248
|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!
321321
|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
322322
|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 |
323323
|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
325325
|120|[Triangle](https://leetcode.com/problems/triangle/)|[Solution](../master/src/main/java/com/stevesun/solutions/Triangle.java)| O(m*n)|O(n) | Medium| DP
326326
|119|[Pascal's Triangle II](https://leetcode.com/problems/pascals-triangle-ii/)|[Solution](../master/src/main/java/com/stevesun/solutions/PascalsTriangleII.java)| O(n^2)|O(1) | Easy|
327327
|118|[Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/)|[Solution](../master/src/main/java/com/stevesun/solutions/PascalsTriangle.java)| O(n^2)|O(1) | Easy|

src/main/java/com/stevesun/solutions/BestTimeToBuyAndSellStock.java renamed to src/main/java/com/stevesun/solutions/_121.java

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,36 @@
44
*
55
Say you have an array for which the ith element is the price of a given stock on day i.
66
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.
89
910
Example 1:
1011
Input: [7, 1, 5, 3, 6, 4]
1112
Output: 5
1213
1314
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:
1518
Input: [7, 6, 4, 3, 1]
1619
Output: 0
1720
1821
In this case, no transaction is done, i.e. max profit = 0.*/
19-
public class BestTimeToBuyAndSellStock {
22+
23+
public class _121 {
24+
/**Pretty straightforward, sell before you buy, keep a global maxProfit variable, update it along the way if necessary.*/
25+
public int maxProfit_20160924(int[] prices) {
26+
if(prices == null || prices.length < 2) return 0;
27+
int minBuy = prices[0];
28+
int maxSell = prices[1];
29+
int maxProfit = (maxSell - minBuy) > 0 ? (maxSell - minBuy) : 0;
30+
for(int i = 1; i < prices.length; i++){
31+
minBuy = Math.min(minBuy, prices[i]);
32+
maxProfit = Math.max(maxProfit, prices[i] - minBuy);
33+
}
34+
return maxProfit;
35+
}
36+
2037
/**The key here is that you'll have to buy first, before you can sell.
2138
* That means, if the lower price comes after a higher price, their combination won't work! Since you cannot sell first
2239
* before you buy it.*/
@@ -37,21 +54,7 @@ public static void main(String...strings){
3754
// int[] prices = new int[]{7,6,4,3,1};
3855
// int[] prices = new int[]{2,4,1};
3956
int[] prices = new int[]{1,2};
40-
BestTimeToBuyAndSellStock test = new BestTimeToBuyAndSellStock();
57+
_121 test = new _121();
4158
System.out.println(test.maxProfit(prices));
4259
}
43-
44-
45-
/**Pretty straightforward, sell before you buy, keep a global maxProfit variable, update it along the way if necessary.*/
46-
public int maxProfit_20160924(int[] prices) {
47-
if(prices == null || prices.length == 0 || prices.length < 2) return 0;
48-
int minBuy = prices[0];
49-
int maxSell = prices[1];
50-
int maxProfit = (maxSell - minBuy) > 0 ? (maxSell - minBuy) : 0;
51-
for(int i = 1; i < prices.length; i++){
52-
minBuy = Math.min(minBuy, prices[i]);
53-
maxProfit = Math.max(maxProfit, prices[i] - minBuy);
54-
}
55-
return maxProfit;
56-
}
5760
}

src/main/java/com/stevesun/solutions/ProductofArrayExceptSelf.java renamed to src/main/java/com/stevesun/solutions/_238.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Solve it without division and in O(n).
1010
Follow up:
1111
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.)
1212
*/
13-
public class ProductofArrayExceptSelf {
13+
public class _238 {
1414

1515
public int[] productExceptSelf(int[] nums) {
1616
int n = nums.length;

src/test/java/com/stevesun/ProductofArrayExceptSelfTest.java renamed to src/test/java/com/stevesun/_238Test.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
package com.stevesun;
22

3-
import com.stevesun.solutions.ProductofArrayExceptSelf;
3+
import com.stevesun.solutions._238;
44
import org.junit.Before;
55
import org.junit.BeforeClass;
66
import org.junit.Test;
77

88
import static org.junit.Assert.assertArrayEquals;
99

10-
public class ProductofArrayExceptSelfTest {
11-
private static ProductofArrayExceptSelf test;
10+
public class _238Test {
11+
private static _238 test;
1212
private static int[] expected;
1313
private static int[] actual;
1414
private static int[] nums;
1515

1616
@BeforeClass
1717
public static void setup(){
18-
test = new ProductofArrayExceptSelf();
18+
test = new _238();
1919
}
2020

2121
@Before

0 commit comments

Comments
 (0)