From 551eed2d1015008d809d77b84ee410087e634163 Mon Sep 17 00:00:00 2001 From: Ming91 Date: Wed, 13 Sep 2023 05:32:33 +0000 Subject: [PATCH 1/8] feat: update Java solution to lc problem: No.135 --- package-lock.json | 2 +- solution/0100-0199/0135.Candy/README.md | 37 +++++++++++---------- solution/0100-0199/0135.Candy/README_EN.md | 37 +++++++++++---------- solution/0100-0199/0135.Candy/Solution.java | 37 +++++++++++---------- 4 files changed, 61 insertions(+), 52 deletions(-) diff --git a/package-lock.json b/package-lock.json index b7a71c1fcb60c..9ba412afea23e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "leetcode", + "name": "leetcode-1", "lockfileVersion": 2, "requires": true, "packages": { diff --git a/solution/0100-0199/0135.Candy/README.md b/solution/0100-0199/0135.Candy/README.md index fff5914d6a0c6..0cf7ab36ff47b 100644 --- a/solution/0100-0199/0135.Candy/README.md +++ b/solution/0100-0199/0135.Candy/README.md @@ -88,25 +88,28 @@ class Solution: class Solution { public int candy(int[] ratings) { int n = ratings.length; - int[] left = new int[n]; - int[] right = new int[n]; - Arrays.fill(left, 1); - Arrays.fill(right, 1); - for (int i = 1; i < n; ++i) { - if (ratings[i] > ratings[i - 1]) { - left[i] = left[i - 1] + 1; - } - } - for (int i = n - 2; i >= 0; --i) { - if (ratings[i] > ratings[i + 1]) { - right[i] = right[i + 1] + 1; + int up = 0; + int down = 0; + int peak = 0; + int candies = 1; + for (int i = 1; i < n; i++) { + if (ratings[i - 1] < ratings[i]) { + up++; + peak = up + 1; + down = 0; + candies += peak; + } else if (ratings[i] == ratings[i - 1]) { + peak = 0; + up = 0; + down = 0; + candies++; + } else { + down++; + up = 0; + candies += down + (peak > down ? 0 : 1); } } - int ans = 0; - for (int i = 0; i < n; ++i) { - ans += Math.max(left[i], right[i]); - } - return ans; + return candies; } } ``` diff --git a/solution/0100-0199/0135.Candy/README_EN.md b/solution/0100-0199/0135.Candy/README_EN.md index 03198acfef1c1..0d2e8a95fbf03 100644 --- a/solution/0100-0199/0135.Candy/README_EN.md +++ b/solution/0100-0199/0135.Candy/README_EN.md @@ -79,25 +79,28 @@ class Solution: class Solution { public int candy(int[] ratings) { int n = ratings.length; - int[] left = new int[n]; - int[] right = new int[n]; - Arrays.fill(left, 1); - Arrays.fill(right, 1); - for (int i = 1; i < n; ++i) { - if (ratings[i] > ratings[i - 1]) { - left[i] = left[i - 1] + 1; - } - } - for (int i = n - 2; i >= 0; --i) { - if (ratings[i] > ratings[i + 1]) { - right[i] = right[i + 1] + 1; + int up = 0; + int down = 0; + int peak = 0; + int candies = 1; + for (int i = 1; i < n; i++) { + if (ratings[i - 1] < ratings[i]) { + up++; + peak = up + 1; + down = 0; + candies += peak; + } else if (ratings[i] == ratings[i - 1]) { + peak = 0; + up = 0; + down = 0; + candies++; + } else { + down++; + up = 0; + candies += down + (peak > down ? 0 : 1); } } - int ans = 0; - for (int i = 0; i < n; ++i) { - ans += Math.max(left[i], right[i]); - } - return ans; + return candies; } } ``` diff --git a/solution/0100-0199/0135.Candy/Solution.java b/solution/0100-0199/0135.Candy/Solution.java index 0fcbd2d60ca2a..12695f93146a2 100644 --- a/solution/0100-0199/0135.Candy/Solution.java +++ b/solution/0100-0199/0135.Candy/Solution.java @@ -1,24 +1,27 @@ class Solution { public int candy(int[] ratings) { int n = ratings.length; - int[] left = new int[n]; - int[] right = new int[n]; - Arrays.fill(left, 1); - Arrays.fill(right, 1); - for (int i = 1; i < n; ++i) { - if (ratings[i] > ratings[i - 1]) { - left[i] = left[i - 1] + 1; + int up = 0; + int down = 0; + int peak = 0; + int candies = 1; + for (int i = 1; i < n; i++) { + if (ratings[i - 1] < ratings[i]) { + up++; + peak = up + 1; + down = 0; + candies += peak; + } else if (ratings[i] == ratings[i - 1]) { + peak = 0; + up = 0; + down = 0; + candies++; + } else { + down++; + up = 0; + candies += down + (peak > down ? 0 : 1); } } - for (int i = n - 2; i >= 0; --i) { - if (ratings[i] > ratings[i + 1]) { - right[i] = right[i + 1] + 1; - } - } - int ans = 0; - for (int i = 0; i < n; ++i) { - ans += Math.max(left[i], right[i]); - } - return ans; + return candies; } } \ No newline at end of file From b4779eaa3775e0153e524b57df386590d5808d0d Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 13 Sep 2023 16:46:37 +0800 Subject: [PATCH 2/8] Update README_EN.md --- solution/0100-0199/0135.Candy/README_EN.md | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/solution/0100-0199/0135.Candy/README_EN.md b/solution/0100-0199/0135.Candy/README_EN.md index 0d2e8a95fbf03..221a6949c1c7f 100644 --- a/solution/0100-0199/0135.Candy/README_EN.md +++ b/solution/0100-0199/0135.Candy/README_EN.md @@ -75,6 +75,33 @@ class Solution: ### **Java** +```java +class Solution { + public int candy(int[] ratings) { + int n = ratings.length; + int[] left = new int[n]; + int[] right = new int[n]; + Arrays.fill(left, 1); + Arrays.fill(right, 1); + for (int i = 1; i < n; ++i) { + if (ratings[i] > ratings[i - 1]) { + left[i] = left[i - 1] + 1; + } + } + for (int i = n - 2; i >= 0; --i) { + if (ratings[i] > ratings[i + 1]) { + right[i] = right[i + 1] + 1; + } + } + int ans = 0; + for (int i = 0; i < n; ++i) { + ans += Math.max(left[i], right[i]); + } + return ans; + } +} +``` + ```java class Solution { public int candy(int[] ratings) { From 43d3b2eabec867db542f51fdd6c00ad58f50133a Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 13 Sep 2023 16:47:27 +0800 Subject: [PATCH 3/8] Update README.md --- solution/0100-0199/0135.Candy/README.md | 27 +++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/solution/0100-0199/0135.Candy/README.md b/solution/0100-0199/0135.Candy/README.md index 0cf7ab36ff47b..ccd2a788a985a 100644 --- a/solution/0100-0199/0135.Candy/README.md +++ b/solution/0100-0199/0135.Candy/README.md @@ -84,6 +84,33 @@ class Solution: +```java +class Solution { + public int candy(int[] ratings) { + int n = ratings.length; + int[] left = new int[n]; + int[] right = new int[n]; + Arrays.fill(left, 1); + Arrays.fill(right, 1); + for (int i = 1; i < n; ++i) { + if (ratings[i] > ratings[i - 1]) { + left[i] = left[i - 1] + 1; + } + } + for (int i = n - 2; i >= 0; --i) { + if (ratings[i] > ratings[i + 1]) { + right[i] = right[i + 1] + 1; + } + } + int ans = 0; + for (int i = 0; i < n; ++i) { + ans += Math.max(left[i], right[i]); + } + return ans; + } +} +``` + ```java class Solution { public int candy(int[] ratings) { From 05047ccddc9dc2afde8ee39527ade57db7a3490d Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 13 Sep 2023 16:47:56 +0800 Subject: [PATCH 4/8] Update package-lock.json --- package-lock.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index 9ba412afea23e..b7a71c1fcb60c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "leetcode-1", + "name": "leetcode", "lockfileVersion": 2, "requires": true, "packages": { From 70cac4462ef048a5eb7493acf432f26459cbc05e Mon Sep 17 00:00:00 2001 From: Ming91 Date: Thu, 14 Sep 2023 04:48:19 +0000 Subject: [PATCH 5/8] feat: add Java solution to lc problem: No.332 --- .../0332.Reconstruct Itinerary/README.md | 35 ++++++++++++++++++- .../0332.Reconstruct Itinerary/README_EN.md | 35 ++++++++++++++++++- .../0332.Reconstruct Itinerary/Solution.java | 34 ++++++++++++++++++ 3 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 solution/0300-0399/0332.Reconstruct Itinerary/Solution.java diff --git a/solution/0300-0399/0332.Reconstruct Itinerary/README.md b/solution/0300-0399/0332.Reconstruct Itinerary/README.md index bd47fa1c4dca8..1c34d9f3208bc 100644 --- a/solution/0300-0399/0332.Reconstruct Itinerary/README.md +++ b/solution/0300-0399/0332.Reconstruct Itinerary/README.md @@ -65,7 +65,40 @@ ```java - +class Solution { + int n; + boolean found; + void dfs(Map> adjLists, + List ans, String curr) { + Queue neighbors = adjLists.get(curr); + if (neighbors == null) { + ans.add(curr); + return ; + } + while (!neighbors.isEmpty()) { + String neighbor = neighbors.poll(); + dfs(adjLists, ans, neighbor); + } + ans.add(curr); + return ; + } + public List findItinerary(List> tickets) { + n = tickets.size(); + Map> adjLists = new HashMap<>(); + for (List ticket : tickets) { + String from = ticket.get(0); + String to = ticket.get(1); + if (!adjLists.containsKey(from)) { + adjLists.put(from, new PriorityQueue<>()); + } + adjLists.get(from).add(to); + } + List ans = new ArrayList<>(); + dfs(adjLists, ans, "JFK"); + Collections.reverse(ans); + return ans; + } +} ``` ### **...** diff --git a/solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md b/solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md index ad2e9866078d9..b29aa73b370c0 100644 --- a/solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md +++ b/solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md @@ -55,7 +55,40 @@ ### **Java** ```java - +class Solution { + int n; + boolean found; + void dfs(Map> adjLists, + List ans, String curr) { + Queue neighbors = adjLists.get(curr); + if (neighbors == null) { + ans.add(curr); + return ; + } + while (!neighbors.isEmpty()) { + String neighbor = neighbors.poll(); + dfs(adjLists, ans, neighbor); + } + ans.add(curr); + return ; + } + public List findItinerary(List> tickets) { + n = tickets.size(); + Map> adjLists = new HashMap<>(); + for (List ticket : tickets) { + String from = ticket.get(0); + String to = ticket.get(1); + if (!adjLists.containsKey(from)) { + adjLists.put(from, new PriorityQueue<>()); + } + adjLists.get(from).add(to); + } + List ans = new ArrayList<>(); + dfs(adjLists, ans, "JFK"); + Collections.reverse(ans); + return ans; + } +} ``` ### **...** diff --git a/solution/0300-0399/0332.Reconstruct Itinerary/Solution.java b/solution/0300-0399/0332.Reconstruct Itinerary/Solution.java new file mode 100644 index 0000000000000..a5744bd9c724d --- /dev/null +++ b/solution/0300-0399/0332.Reconstruct Itinerary/Solution.java @@ -0,0 +1,34 @@ +class Solution { + int n; + boolean found; + void dfs(Map> adjLists, + List ans, String curr) { + Queue neighbors = adjLists.get(curr); + if (neighbors == null) { + ans.add(curr); + return ; + } + while (!neighbors.isEmpty()) { + String neighbor = neighbors.poll(); + dfs(adjLists, ans, neighbor); + } + ans.add(curr); + return ; + } + public List findItinerary(List> tickets) { + n = tickets.size(); + Map> adjLists = new HashMap<>(); + for (List ticket : tickets) { + String from = ticket.get(0); + String to = ticket.get(1); + if (!adjLists.containsKey(from)) { + adjLists.put(from, new PriorityQueue<>()); + } + adjLists.get(from).add(to); + } + List ans = new ArrayList<>(); + dfs(adjLists, ans, "JFK"); + Collections.reverse(ans); + return ans; + } +} \ No newline at end of file From 17929a7452c4bc09a08d20efcaa54feec31dd026 Mon Sep 17 00:00:00 2001 From: Ming91 Date: Thu, 14 Sep 2023 04:50:21 +0000 Subject: [PATCH 6/8] feat: update Java solution to lc problem: No.332 --- solution/0300-0399/0332.Reconstruct Itinerary/README.md | 3 --- solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md | 3 --- solution/0300-0399/0332.Reconstruct Itinerary/Solution.java | 5 +---- 3 files changed, 1 insertion(+), 10 deletions(-) diff --git a/solution/0300-0399/0332.Reconstruct Itinerary/README.md b/solution/0300-0399/0332.Reconstruct Itinerary/README.md index 1c34d9f3208bc..715b84655e47d 100644 --- a/solution/0300-0399/0332.Reconstruct Itinerary/README.md +++ b/solution/0300-0399/0332.Reconstruct Itinerary/README.md @@ -66,8 +66,6 @@ ```java class Solution { - int n; - boolean found; void dfs(Map> adjLists, List ans, String curr) { Queue neighbors = adjLists.get(curr); @@ -83,7 +81,6 @@ class Solution { return ; } public List findItinerary(List> tickets) { - n = tickets.size(); Map> adjLists = new HashMap<>(); for (List ticket : tickets) { String from = ticket.get(0); diff --git a/solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md b/solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md index b29aa73b370c0..87a15337b8ed8 100644 --- a/solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md +++ b/solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md @@ -56,8 +56,6 @@ ```java class Solution { - int n; - boolean found; void dfs(Map> adjLists, List ans, String curr) { Queue neighbors = adjLists.get(curr); @@ -73,7 +71,6 @@ class Solution { return ; } public List findItinerary(List> tickets) { - n = tickets.size(); Map> adjLists = new HashMap<>(); for (List ticket : tickets) { String from = ticket.get(0); diff --git a/solution/0300-0399/0332.Reconstruct Itinerary/Solution.java b/solution/0300-0399/0332.Reconstruct Itinerary/Solution.java index a5744bd9c724d..298018229b34f 100644 --- a/solution/0300-0399/0332.Reconstruct Itinerary/Solution.java +++ b/solution/0300-0399/0332.Reconstruct Itinerary/Solution.java @@ -1,7 +1,5 @@ class Solution { - int n; - boolean found; - void dfs(Map> adjLists, + void dfs(Map> adjLists, List ans, String curr) { Queue neighbors = adjLists.get(curr); if (neighbors == null) { @@ -16,7 +14,6 @@ void dfs(Map> adjLists, return ; } public List findItinerary(List> tickets) { - n = tickets.size(); Map> adjLists = new HashMap<>(); for (List ticket : tickets) { String from = ticket.get(0); From 8d5f2a2e96f9d1df7f861fe9bb0a9ae116e66edb Mon Sep 17 00:00:00 2001 From: Ming91 Date: Thu, 14 Sep 2023 06:14:34 +0000 Subject: [PATCH 7/8] feat: update solution to lc problem: No.332 --- solution/0300-0399/0332.Reconstruct Itinerary/README.md | 7 ++++--- solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md | 7 ++++--- .../0300-0399/0332.Reconstruct Itinerary/Solution.java | 7 ++++--- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/solution/0300-0399/0332.Reconstruct Itinerary/README.md b/solution/0300-0399/0332.Reconstruct Itinerary/README.md index 715b84655e47d..543609ce23b9e 100644 --- a/solution/0300-0399/0332.Reconstruct Itinerary/README.md +++ b/solution/0300-0399/0332.Reconstruct Itinerary/README.md @@ -67,19 +67,20 @@ ```java class Solution { void dfs(Map> adjLists, - List ans, String curr) { + List ans, String curr) { Queue neighbors = adjLists.get(curr); if (neighbors == null) { ans.add(curr); - return ; + return; } while (!neighbors.isEmpty()) { String neighbor = neighbors.poll(); dfs(adjLists, ans, neighbor); } ans.add(curr); - return ; + return; } + public List findItinerary(List> tickets) { Map> adjLists = new HashMap<>(); for (List ticket : tickets) { diff --git a/solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md b/solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md index 87a15337b8ed8..2237927e1dd16 100644 --- a/solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md +++ b/solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md @@ -57,19 +57,20 @@ ```java class Solution { void dfs(Map> adjLists, - List ans, String curr) { + List ans, String curr) { Queue neighbors = adjLists.get(curr); if (neighbors == null) { ans.add(curr); - return ; + return; } while (!neighbors.isEmpty()) { String neighbor = neighbors.poll(); dfs(adjLists, ans, neighbor); } ans.add(curr); - return ; + return; } + public List findItinerary(List> tickets) { Map> adjLists = new HashMap<>(); for (List ticket : tickets) { diff --git a/solution/0300-0399/0332.Reconstruct Itinerary/Solution.java b/solution/0300-0399/0332.Reconstruct Itinerary/Solution.java index 298018229b34f..eef31263b5ec6 100644 --- a/solution/0300-0399/0332.Reconstruct Itinerary/Solution.java +++ b/solution/0300-0399/0332.Reconstruct Itinerary/Solution.java @@ -1,18 +1,19 @@ class Solution { void dfs(Map> adjLists, - List ans, String curr) { + List ans, String curr) { Queue neighbors = adjLists.get(curr); if (neighbors == null) { ans.add(curr); - return ; + return; } while (!neighbors.isEmpty()) { String neighbor = neighbors.poll(); dfs(adjLists, ans, neighbor); } ans.add(curr); - return ; + return; } + public List findItinerary(List> tickets) { Map> adjLists = new HashMap<>(); for (List ticket : tickets) { From 2eb8e45ceff4b7857c3197f18c71110418df247c Mon Sep 17 00:00:00 2001 From: Ming91 Date: Thu, 14 Sep 2023 06:36:37 +0000 Subject: [PATCH 8/8] feat: add Java solution to lc problem: No.332 --- solution/0300-0399/0332.Reconstruct Itinerary/README.md | 3 +-- solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md | 3 +-- solution/0300-0399/0332.Reconstruct Itinerary/Solution.java | 3 +-- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/solution/0300-0399/0332.Reconstruct Itinerary/README.md b/solution/0300-0399/0332.Reconstruct Itinerary/README.md index 543609ce23b9e..caca416c94500 100644 --- a/solution/0300-0399/0332.Reconstruct Itinerary/README.md +++ b/solution/0300-0399/0332.Reconstruct Itinerary/README.md @@ -66,8 +66,7 @@ ```java class Solution { - void dfs(Map> adjLists, - List ans, String curr) { + void dfs(Map> adjLists, List ans, String curr) { Queue neighbors = adjLists.get(curr); if (neighbors == null) { ans.add(curr); diff --git a/solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md b/solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md index 2237927e1dd16..711fa5d3d6131 100644 --- a/solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md +++ b/solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md @@ -56,8 +56,7 @@ ```java class Solution { - void dfs(Map> adjLists, - List ans, String curr) { + void dfs(Map> adjLists, List ans, String curr) { Queue neighbors = adjLists.get(curr); if (neighbors == null) { ans.add(curr); diff --git a/solution/0300-0399/0332.Reconstruct Itinerary/Solution.java b/solution/0300-0399/0332.Reconstruct Itinerary/Solution.java index eef31263b5ec6..970cd30092f14 100644 --- a/solution/0300-0399/0332.Reconstruct Itinerary/Solution.java +++ b/solution/0300-0399/0332.Reconstruct Itinerary/Solution.java @@ -1,6 +1,5 @@ class Solution { - void dfs(Map> adjLists, - List ans, String curr) { + void dfs(Map> adjLists, List ans, String curr) { Queue neighbors = adjLists.get(curr); if (neighbors == null) { ans.add(curr);