From 551eed2d1015008d809d77b84ee410087e634163 Mon Sep 17 00:00:00 2001 From: Ming91 Date: Wed, 13 Sep 2023 05:32:33 +0000 Subject: [PATCH 01/14] 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 02/14] 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 03/14] 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 04/14] 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 05/14] 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 06/14] 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 07/14] 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 08/14] 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); From d9bd900fd2ba5b8bdc55df2623ebe2a9ca644f07 Mon Sep 17 00:00:00 2001 From: Ming91 Date: Sun, 17 Sep 2023 05:20:28 +0000 Subject: [PATCH 09/14] feat: add Java solution to lc problem: No.2859 --- package-lock.json | 2 +- .../README.md | 12 +++++++++++- .../README_EN.md | 12 +++++++++++- .../Solultion.java | 11 +++++++++++ 4 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 solution/2800-2899/2859.Sum of Values at Indices With K Set Bits/Solultion.java 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/2800-2899/2859.Sum of Values at Indices With K Set Bits/README.md b/solution/2800-2899/2859.Sum of Values at Indices With K Set Bits/README.md index 2cf6d04435b99..ea80f0e50b142 100644 --- a/solution/2800-2899/2859.Sum of Values at Indices With K Set Bits/README.md +++ b/solution/2800-2899/2859.Sum of Values at Indices With K Set Bits/README.md @@ -73,7 +73,17 @@ ```java - +class Solution { + public int sumIndicesWithKSetBits(List nums, int k) { + int ans = 0; + for (int i = 0; i < nums.size(); i++) { + if (Integer.bitCount(i) == k) { + ans += nums.get(i); + } + } + return ans; + } +} ``` ### **C++** diff --git a/solution/2800-2899/2859.Sum of Values at Indices With K Set Bits/README_EN.md b/solution/2800-2899/2859.Sum of Values at Indices With K Set Bits/README_EN.md index ce6762e01ea38..a28318283419d 100644 --- a/solution/2800-2899/2859.Sum of Values at Indices With K Set Bits/README_EN.md +++ b/solution/2800-2899/2859.Sum of Values at Indices With K Set Bits/README_EN.md @@ -65,7 +65,17 @@ Hence, the answer is nums[3] = 1. ### **Java** ```java - +class Solution { + public int sumIndicesWithKSetBits(List nums, int k) { + int ans = 0; + for (int i = 0; i < nums.size(); i++) { + if (Integer.bitCount(i) == k) { + ans += nums.get(i); + } + } + return ans; + } +} ``` ### **C++** diff --git a/solution/2800-2899/2859.Sum of Values at Indices With K Set Bits/Solultion.java b/solution/2800-2899/2859.Sum of Values at Indices With K Set Bits/Solultion.java new file mode 100644 index 0000000000000..382705fb8c505 --- /dev/null +++ b/solution/2800-2899/2859.Sum of Values at Indices With K Set Bits/Solultion.java @@ -0,0 +1,11 @@ +class Solution { + public int sumIndicesWithKSetBits(List nums, int k) { + int ans = 0; + for (int i = 0; i < nums.size(); i++) { + if (Integer.bitCount(i) == k) { + ans += nums.get(i); + } + } + return ans; + } +} \ No newline at end of file From 200be1ba8e255cae81afd5f93310d32a15b87dfc Mon Sep 17 00:00:00 2001 From: Ming91 Date: Sun, 17 Sep 2023 05:22:10 +0000 Subject: [PATCH 10/14] feat: add Java solution to lc problem: No.2860 --- solution/2800-2899/2860.Happy Students/README.md | 14 +++++++++++++- .../2800-2899/2860.Happy Students/README_EN.md | 14 +++++++++++++- .../2800-2899/2860.Happy Students/Solution.java | 13 +++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 solution/2800-2899/2860.Happy Students/Solution.java diff --git a/solution/2800-2899/2860.Happy Students/README.md b/solution/2800-2899/2860.Happy Students/README.md index 819694e76f4e0..3016e884b4016 100644 --- a/solution/2800-2899/2860.Happy Students/README.md +++ b/solution/2800-2899/2860.Happy Students/README.md @@ -71,7 +71,19 @@ ```java - +class Solution { + public int countWays(List nums) { + Collections.sort(nums); + int n = nums.size(); + int ans = 0; + for (int i = 0; i <= n; i++) { + if ((i == 0 || nums.get(i - 1) < i) && (i == n || nums.get(i) > i)) { + ans++; + } + } + return ans; + } +} ``` ### **C++** diff --git a/solution/2800-2899/2860.Happy Students/README_EN.md b/solution/2800-2899/2860.Happy Students/README_EN.md index 88c3f3ca6e8d6..75796a6cfc4b2 100644 --- a/solution/2800-2899/2860.Happy Students/README_EN.md +++ b/solution/2800-2899/2860.Happy Students/README_EN.md @@ -61,7 +61,19 @@ The class teacher selects all the students to form the group. ### **Java** ```java - +class Solution { + public int countWays(List nums) { + Collections.sort(nums); + int n = nums.size(); + int ans = 0; + for (int i = 0; i <= n; i++) { + if ((i == 0 || nums.get(i - 1) < i) && (i == n || nums.get(i) > i)) { + ans++; + } + } + return ans; + } +} ``` ### **C++** diff --git a/solution/2800-2899/2860.Happy Students/Solution.java b/solution/2800-2899/2860.Happy Students/Solution.java new file mode 100644 index 0000000000000..9dda78748604a --- /dev/null +++ b/solution/2800-2899/2860.Happy Students/Solution.java @@ -0,0 +1,13 @@ +class Solution { + public int countWays(List nums) { + Collections.sort(nums); + int n = nums.size(); + int ans = 0; + for (int i = 0; i <= n; i++) { + if ((i == 0 || nums.get(i - 1) < i) && (i == n || nums.get(i) > i)) { + ans++; + } + } + return ans; + } +} \ No newline at end of file From 2bce655f84d1f32754d07a956e1074101934a099 Mon Sep 17 00:00:00 2001 From: Ming91 Date: Sun, 17 Sep 2023 05:24:53 +0000 Subject: [PATCH 11/14] feat: add Java solution to lc problem: No.2861 --- .../2861.Maximum Number of Alloys/README.md | 45 ++++++++++++++++++- .../README_EN.md | 45 ++++++++++++++++++- .../Solution.java | 44 ++++++++++++++++++ 3 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 solution/2800-2899/2861.Maximum Number of Alloys/Solution.java diff --git a/solution/2800-2899/2861.Maximum Number of Alloys/README.md b/solution/2800-2899/2861.Maximum Number of Alloys/README.md index aef676901c586..8398fb9fd93bb 100644 --- a/solution/2800-2899/2861.Maximum Number of Alloys/README.md +++ b/solution/2800-2899/2861.Maximum Number of Alloys/README.md @@ -94,7 +94,50 @@ ```java - +class Solution { + int n; + int k; + int budget; + List> composition; + List stock; + List cost; + + boolean isValid(long target) { + for (int i = 0; i < k; i++) { + long remain = budget; + List currMachine = composition.get(i); + for (int j = 0; j < n && remain >= 0; j++) { + long need = Math.max(0, currMachine.get(j) * target - stock.get(j)); + remain -= need * cost.get(j); + } + if (remain >= 0) { + return true; + } + } + return false; + } + + public int maxNumberOfAlloys(int n, int k, int budget, List> composition, + List stock, List cost) { + this.n = n; + this.k = k; + this.budget = budget; + this.composition = composition; + this.stock = stock; + this.cost = cost; + int l = -1; + int r = budget / cost.get(0) + stock.get(0); + while (l < r) { + int mid = (l + r + 1) >> 1; + if (isValid(mid)) { + l = mid; + } else { + r = mid - 1; + } + } + return l; + } +} ``` ### **C++** diff --git a/solution/2800-2899/2861.Maximum Number of Alloys/README_EN.md b/solution/2800-2899/2861.Maximum Number of Alloys/README_EN.md index ce9a000daff47..8215bd6413315 100644 --- a/solution/2800-2899/2861.Maximum Number of Alloys/README_EN.md +++ b/solution/2800-2899/2861.Maximum Number of Alloys/README_EN.md @@ -84,7 +84,50 @@ It can be proven that we can create at most 2 alloys. ### **Java** ```java - +class Solution { + int n; + int k; + int budget; + List> composition; + List stock; + List cost; + + boolean isValid(long target) { + for (int i = 0; i < k; i++) { + long remain = budget; + List currMachine = composition.get(i); + for (int j = 0; j < n && remain >= 0; j++) { + long need = Math.max(0, currMachine.get(j) * target - stock.get(j)); + remain -= need * cost.get(j); + } + if (remain >= 0) { + return true; + } + } + return false; + } + + public int maxNumberOfAlloys(int n, int k, int budget, List> composition, + List stock, List cost) { + this.n = n; + this.k = k; + this.budget = budget; + this.composition = composition; + this.stock = stock; + this.cost = cost; + int l = -1; + int r = budget / cost.get(0) + stock.get(0); + while (l < r) { + int mid = (l + r + 1) >> 1; + if (isValid(mid)) { + l = mid; + } else { + r = mid - 1; + } + } + return l; + } +} ``` ### **C++** diff --git a/solution/2800-2899/2861.Maximum Number of Alloys/Solution.java b/solution/2800-2899/2861.Maximum Number of Alloys/Solution.java new file mode 100644 index 0000000000000..19543ea11a830 --- /dev/null +++ b/solution/2800-2899/2861.Maximum Number of Alloys/Solution.java @@ -0,0 +1,44 @@ +class Solution { + int n; + int k; + int budget; + List> composition; + List stock; + List cost; + + boolean isValid(long target) { + for (int i = 0; i < k; i++) { + long remain = budget; + List currMachine = composition.get(i); + for (int j = 0; j < n && remain >= 0; j++) { + long need = Math.max(0, currMachine.get(j) * target - stock.get(j)); + remain -= need * cost.get(j); + } + if (remain >= 0) { + return true; + } + } + return false; + } + + public int maxNumberOfAlloys(int n, int k, int budget, List> composition, + List stock, List cost) { + this.n = n; + this.k = k; + this.budget = budget; + this.composition = composition; + this.stock = stock; + this.cost = cost; + int l = -1; + int r = budget / cost.get(0) + stock.get(0); + while (l < r) { + int mid = (l + r + 1) >> 1; + if (isValid(mid)) { + l = mid; + } else { + r = mid - 1; + } + } + return l; + } +} \ No newline at end of file From 703da9267c606236db637b85204fc7eb4f1e7ff2 Mon Sep 17 00:00:00 2001 From: Ming91 Date: Sun, 17 Sep 2023 05:26:30 +0000 Subject: [PATCH 12/14] feat: add Java solution to lc problem: No.2862 --- .../README.md | 23 +++++++++++++++++++ .../README_EN.md | 23 +++++++++++++++++++ .../Solution.java | 23 +++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 solution/2800-2899/2862.Maximum Element-Sum of a Complete Subset of Indices/Solution.java diff --git a/solution/2800-2899/2862.Maximum Element-Sum of a Complete Subset of Indices/README.md b/solution/2800-2899/2862.Maximum Element-Sum of a Complete Subset of Indices/README.md index ac58563449006..3424faa00f0e9 100644 --- a/solution/2800-2899/2862.Maximum Element-Sum of a Complete Subset of Indices/README.md +++ b/solution/2800-2899/2862.Maximum Element-Sum of a Complete Subset of Indices/README.md @@ -71,6 +71,29 @@ ```java +class Solution { + public long maximumSum(List nums) { + long ans = 0; + int n = nums.size(); + boolean[] used = new boolean[n + 1]; + int bound = (int) Math.floor(Math.sqrt(n)); + int[] squares = new int[bound + 1]; + for (int i = 1; i <= bound + 1; i++) { + squares[i - 1] = i * i; + } + for (int i = 1; i <= n; i++) { + long res = 0; + int idx = 0; + int curr = i * squares[idx]; + while (curr <= n) { + res += nums.get(curr - 1); + curr = i * squares[++idx]; + } + ans = Math.max(ans, res); + } + return ans; + } +} ``` diff --git a/solution/2800-2899/2862.Maximum Element-Sum of a Complete Subset of Indices/README_EN.md b/solution/2800-2899/2862.Maximum Element-Sum of a Complete Subset of Indices/README_EN.md index 62133de4d84dc..8a64057cbb6b3 100644 --- a/solution/2800-2899/2862.Maximum Element-Sum of a Complete Subset of Indices/README_EN.md +++ b/solution/2800-2899/2862.Maximum Element-Sum of a Complete Subset of Indices/README_EN.md @@ -61,6 +61,29 @@ Hence, the maximum element-sum of a complete subset of indices is 19. ### **Java** ```java +class Solution { + public long maximumSum(List nums) { + long ans = 0; + int n = nums.size(); + boolean[] used = new boolean[n + 1]; + int bound = (int) Math.floor(Math.sqrt(n)); + int[] squares = new int[bound + 1]; + for (int i = 1; i <= bound + 1; i++) { + squares[i - 1] = i * i; + } + for (int i = 1; i <= n; i++) { + long res = 0; + int idx = 0; + int curr = i * squares[idx]; + while (curr <= n) { + res += nums.get(curr - 1); + curr = i * squares[++idx]; + } + ans = Math.max(ans, res); + } + return ans; + } +} ``` diff --git a/solution/2800-2899/2862.Maximum Element-Sum of a Complete Subset of Indices/Solution.java b/solution/2800-2899/2862.Maximum Element-Sum of a Complete Subset of Indices/Solution.java new file mode 100644 index 0000000000000..5f793ec0cf4f8 --- /dev/null +++ b/solution/2800-2899/2862.Maximum Element-Sum of a Complete Subset of Indices/Solution.java @@ -0,0 +1,23 @@ +class Solution { + public long maximumSum(List nums) { + long ans = 0; + int n = nums.size(); + boolean[] used = new boolean[n + 1]; + int bound = (int) Math.floor(Math.sqrt(n)); + int[] squares = new int[bound + 1]; + for (int i = 1; i <= bound + 1; i++) { + squares[i - 1] = i * i; + } + for (int i = 1; i <= n; i++) { + long res = 0; + int idx = 0; + int curr = i * squares[idx]; + while (curr <= n) { + res += nums.get(curr - 1); + curr = i * squares[++idx]; + } + ans = Math.max(ans, res); + } + return ans; + } +} \ No newline at end of file From 4c3c5e087315e006b7ad6ecc9e689038ef475216 Mon Sep 17 00:00:00 2001 From: Ming91 Date: Sun, 17 Sep 2023 05:34:13 +0000 Subject: [PATCH 13/14] feat: update Java solution to lc problem: No.2861 --- solution/2800-2899/2861.Maximum Number of Alloys/README.md | 2 +- solution/2800-2899/2861.Maximum Number of Alloys/README_EN.md | 2 +- solution/2800-2899/2861.Maximum Number of Alloys/Solution.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/solution/2800-2899/2861.Maximum Number of Alloys/README.md b/solution/2800-2899/2861.Maximum Number of Alloys/README.md index 8398fb9fd93bb..62045e9d8574e 100644 --- a/solution/2800-2899/2861.Maximum Number of Alloys/README.md +++ b/solution/2800-2899/2861.Maximum Number of Alloys/README.md @@ -118,7 +118,7 @@ class Solution { } public int maxNumberOfAlloys(int n, int k, int budget, List> composition, - List stock, List cost) { + List stock, List cost) { this.n = n; this.k = k; this.budget = budget; diff --git a/solution/2800-2899/2861.Maximum Number of Alloys/README_EN.md b/solution/2800-2899/2861.Maximum Number of Alloys/README_EN.md index 8215bd6413315..cafe9053406cb 100644 --- a/solution/2800-2899/2861.Maximum Number of Alloys/README_EN.md +++ b/solution/2800-2899/2861.Maximum Number of Alloys/README_EN.md @@ -108,7 +108,7 @@ class Solution { } public int maxNumberOfAlloys(int n, int k, int budget, List> composition, - List stock, List cost) { + List stock, List cost) { this.n = n; this.k = k; this.budget = budget; diff --git a/solution/2800-2899/2861.Maximum Number of Alloys/Solution.java b/solution/2800-2899/2861.Maximum Number of Alloys/Solution.java index 19543ea11a830..90d9f0274c424 100644 --- a/solution/2800-2899/2861.Maximum Number of Alloys/Solution.java +++ b/solution/2800-2899/2861.Maximum Number of Alloys/Solution.java @@ -22,7 +22,7 @@ boolean isValid(long target) { } public int maxNumberOfAlloys(int n, int k, int budget, List> composition, - List stock, List cost) { + List stock, List cost) { this.n = n; this.k = k; this.budget = budget; From 4538c0e08aa1b07e1fde3c9696e0e6b3c1deb3b5 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sun, 17 Sep 2023 14:11:13 +0800 Subject: [PATCH 14/14] 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": {