Skip to content

Commit

Permalink
update 121 java, progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed May 11, 2024
1 parent 95c2d03 commit 7536f40
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 2 deletions.
1 change: 1 addition & 0 deletions data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
20240511: 121,252
20240510: 125
20240314: 647
20240313: 338,347,371,417(again),424(again),435,572(again)
Expand Down
11 changes: 9 additions & 2 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
2024-07-05 -> ['121,252']
2024-07-04 -> ['125']
2024-06-14 -> ['121,252']
2024-06-13 -> ['125']
2024-06-01 -> ['121,252']
2024-05-31 -> ['125']
2024-05-24 -> ['121,252']
2024-05-23 -> ['125']
2024-05-19 -> ['121,252']
2024-05-18 -> ['125']
2024-05-16 -> ['121,252']
2024-05-15 -> ['125']
2024-05-13 -> ['125']
2024-05-12 -> ['125']
2024-05-14 -> ['121,252']
2024-05-13 -> ['121,252', '125']
2024-05-12 -> ['121,252', '125']
2024-05-11 -> ['125']
2024-05-07 -> ['338,347,371,417(again),424(again),435,572(again)']
2024-05-06 -> ['297(again),300,322(again),323']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,44 @@ public class BestTimeToBuyAndSellStock {
// V0
public int maxProfit(int[] prices) {

if (prices.length == 0){
return 0;
}

int res = 0;
int min = -1;
int max = -1;

for (int i : prices){
int cur = i;
//System.out.println("cur = " + cur);
if (min == -1){
min = cur;
continue;
}
if (min > cur){
min = cur;
continue;
}
if (max == -1){
max = cur;
}
if (cur > max){
max = cur;
}
int tmp = max - min;
//System.out.println("max = " + max + " min = " + min + " tmp = " + tmp);
/** NOTE : need to reset max val after get "revenue", so we don't reuse previous max val */
max = -1;
res = Math.max(tmp, res);
}

return res;
}

// V0'
public int maxProfit_0(int[] prices) {

int minVal = (int) Math.pow(10, 4);
int maxVal = 0;
int maxProfit = 0;
Expand Down
39 changes: 39 additions & 0 deletions leetcode_java/src/main/java/dev/workspace3.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,43 @@ public boolean isPalindrome(String s) {
return sb.toString() == sb.reverse().toString();
}

// LC 121
public int maxProfit(int[] prices) {

if (prices.length == 0){
return 0;
}

int res = 0;
int min = -1;
int max = -1;

for (int i : prices){
int cur = i; //prices[i];
System.out.println("cur = " + cur);
if (min == -1){
min = cur;
continue;
}
if (min > cur){
min = cur;
continue;
}
if (max == -1){
max = cur;
max = cur;
}
if (cur > max){
max = cur;
}
int tmp = max - min;
System.out.println("max = " + max + " min = " + min + " tmp = " + tmp);
max = -1;
res = Math.max(tmp, res);
}

return res;
}


}

0 comments on commit 7536f40

Please sign in to comment.