From 7536f40282677bf2da6b80fe0066e325027b3bb1 Mon Sep 17 00:00:00 2001 From: yennanliu Date: Sat, 11 May 2024 17:08:48 +0800 Subject: [PATCH] update 121 java, progress --- data/progress.txt | 1 + data/to_review.txt | 11 +++++- .../Array/BestTimeToBuyAndSellStock.java | 38 ++++++++++++++++++ .../src/main/java/dev/workspace3.java | 39 +++++++++++++++++++ 4 files changed, 87 insertions(+), 2 deletions(-) diff --git a/data/progress.txt b/data/progress.txt index bd237989..d74b004d 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -1,3 +1,4 @@ +20240511: 121,252 20240510: 125 20240314: 647 20240313: 338,347,371,417(again),424(again),435,572(again) diff --git a/data/to_review.txt b/data/to_review.txt index f9b82e78..b51942da 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -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'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/Array/BestTimeToBuyAndSellStock.java b/leetcode_java/src/main/java/LeetCodeJava/Array/BestTimeToBuyAndSellStock.java index 2fde3d4a..136bc0d7 100644 --- a/leetcode_java/src/main/java/LeetCodeJava/Array/BestTimeToBuyAndSellStock.java +++ b/leetcode_java/src/main/java/LeetCodeJava/Array/BestTimeToBuyAndSellStock.java @@ -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; diff --git a/leetcode_java/src/main/java/dev/workspace3.java b/leetcode_java/src/main/java/dev/workspace3.java index 15f17cff..b432cb81 100644 --- a/leetcode_java/src/main/java/dev/workspace3.java +++ b/leetcode_java/src/main/java/dev/workspace3.java @@ -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; + } + + }