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 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 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..62045e9d8574e 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..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 @@ -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..90d9f0274c424 --- /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 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