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 ea80f0e50b142..f9f02e987e9bb 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 @@ -58,6 +58,14 @@ +**方法一:模拟** + +我们直接遍历每个下标 $i$,判断其二进制表示中 $1$ 的个数是否等于 $k$,如果等于则将其对应的元素累加到答案 $ans$ 中。 + +遍历结束后,返回答案即可。 + +时间复杂度 $O(n \times \log n)$,其中 $n$ 是数组 $nums$ 的长度。空间复杂度 $O(1)$。 + ### **Python3** @@ -65,7 +73,9 @@ ```python - +class Solution: + def sumIndicesWithKSetBits(self, nums: List[int], k: int) -> int: + return sum(x for i, x in enumerate(nums) if i.bit_count() == k) ``` ### **Java** @@ -89,13 +99,54 @@ class Solution { ### **C++** ```cpp - +class Solution { +public: + int sumIndicesWithKSetBits(vector& nums, int k) { + int ans = 0; + for (int i = 0; i < nums.size(); ++i) { + if (__builtin_popcount(i) == k) { + ans += nums[i]; + } + } + return ans; + } +}; ``` ### **Go** ```go +func sumIndicesWithKSetBits(nums []int, k int) (ans int) { + for i, x := range nums { + if bits.OnesCount(uint(i)) == k { + ans += x + } + } + return +} +``` + +### **TypeScript** + +```ts +function sumIndicesWithKSetBits(nums: number[], k: number): number { + let ans = 0; + for (let i = 0; i < nums.length; ++i) { + if (bitCount(i) === k) { + ans += nums[i]; + } + } + return ans; +} +function bitCount(n: number): number { + let count = 0; + while (n) { + n &= n - 1; + count++; + } + return count; +} ``` ### **...** 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 a28318283419d..3076e252b4e80 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 @@ -59,7 +59,9 @@ Hence, the answer is nums[3] = 1. ### **Python3** ```python - +class Solution: + def sumIndicesWithKSetBits(self, nums: List[int], k: int) -> int: + return sum(x for i, x in enumerate(nums) if i.bit_count() == k) ``` ### **Java** @@ -81,13 +83,54 @@ class Solution { ### **C++** ```cpp - +class Solution { +public: + int sumIndicesWithKSetBits(vector& nums, int k) { + int ans = 0; + for (int i = 0; i < nums.size(); ++i) { + if (__builtin_popcount(i) == k) { + ans += nums[i]; + } + } + return ans; + } +}; ``` ### **Go** ```go +func sumIndicesWithKSetBits(nums []int, k int) (ans int) { + for i, x := range nums { + if bits.OnesCount(uint(i)) == k { + ans += x + } + } + return +} +``` + +### **TypeScript** + +```ts +function sumIndicesWithKSetBits(nums: number[], k: number): number { + let ans = 0; + for (let i = 0; i < nums.length; ++i) { + if (bitCount(i) === k) { + ans += nums[i]; + } + } + return ans; +} +function bitCount(n: number): number { + let count = 0; + while (n) { + n &= n - 1; + count++; + } + return count; +} ``` ### **...** diff --git a/solution/2800-2899/2859.Sum of Values at Indices With K Set Bits/Solution.cpp b/solution/2800-2899/2859.Sum of Values at Indices With K Set Bits/Solution.cpp new file mode 100644 index 0000000000000..bdc07d66a91e6 --- /dev/null +++ b/solution/2800-2899/2859.Sum of Values at Indices With K Set Bits/Solution.cpp @@ -0,0 +1,12 @@ +class Solution { +public: + int sumIndicesWithKSetBits(vector& nums, int k) { + int ans = 0; + for (int i = 0; i < nums.size(); ++i) { + if (__builtin_popcount(i) == k) { + ans += nums[i]; + } + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/2800-2899/2859.Sum of Values at Indices With K Set Bits/Solution.go b/solution/2800-2899/2859.Sum of Values at Indices With K Set Bits/Solution.go new file mode 100644 index 0000000000000..149bbc17d2e80 --- /dev/null +++ b/solution/2800-2899/2859.Sum of Values at Indices With K Set Bits/Solution.go @@ -0,0 +1,8 @@ +func sumIndicesWithKSetBits(nums []int, k int) (ans int) { + for i, x := range nums { + if bits.OnesCount(uint(i)) == k { + ans += x + } + } + return +} \ No newline at end of file diff --git a/solution/2800-2899/2859.Sum of Values at Indices With K Set Bits/Solution.py b/solution/2800-2899/2859.Sum of Values at Indices With K Set Bits/Solution.py new file mode 100644 index 0000000000000..3de0c9fd75b7e --- /dev/null +++ b/solution/2800-2899/2859.Sum of Values at Indices With K Set Bits/Solution.py @@ -0,0 +1,3 @@ +class Solution: + def sumIndicesWithKSetBits(self, nums: List[int], k: int) -> int: + return sum(x for i, x in enumerate(nums) if i.bit_count() == k) diff --git a/solution/2800-2899/2859.Sum of Values at Indices With K Set Bits/Solution.ts b/solution/2800-2899/2859.Sum of Values at Indices With K Set Bits/Solution.ts new file mode 100644 index 0000000000000..f971c2bc78889 --- /dev/null +++ b/solution/2800-2899/2859.Sum of Values at Indices With K Set Bits/Solution.ts @@ -0,0 +1,18 @@ +function sumIndicesWithKSetBits(nums: number[], k: number): number { + let ans = 0; + for (let i = 0; i < nums.length; ++i) { + if (bitCount(i) === k) { + ans += nums[i]; + } + } + return ans; +} + +function bitCount(n: number): number { + let count = 0; + while (n) { + n &= n - 1; + count++; + } + return count; +} diff --git a/solution/2800-2899/2860.Happy Students/README.md b/solution/2800-2899/2860.Happy Students/README.md index 3016e884b4016..ea8c2c072ae0a 100644 --- a/solution/2800-2899/2860.Happy Students/README.md +++ b/solution/2800-2899/2860.Happy Students/README.md @@ -56,6 +56,22 @@ +**方法一:排序 + 枚举** + +假设选出了 $k$ 个学生,那么以下情况成立: + +- 如果 $nums[i] = k$,那么不存在分组方法; +- 如果 $nums[i] \gt k$,那么学生 $i$ 不被选中; +- 如果 $nums[i] \lt k$,那么学生 $i$ 被选中。 + +因此,被选中的学生一定是排序后的 $nums$ 数组中的前 $k$ 个元素。 + +我们在 $[0,..n]$ 范围内枚举 $k$,对于当前选出的学生人数 $i$,我们可以得到组内最大的学生编号 $i-1$,数字为 $nums[i-1]$。如果 $i \gt 0$ 并且 $nums[i-1] \ge i$,那么不存在分组方法;如果 $i \lt n$ 并且 $nums[i] \le i$,那么不存在分组方法。否则,存在分组方法,答案加一。 + +枚举结束后,返回答案即可。 + +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组长度。 + ### **Python3** @@ -63,7 +79,17 @@ ```python - +class Solution: + def countWays(self, nums: List[int]) -> int: + nums.sort() + n = len(nums) + ans = 0 + for i in range(n + 1): + if i and nums[i - 1] >= i: + continue + if i < n and nums[i] <= i: + continue + return ans ``` ### **Java** @@ -89,13 +115,54 @@ class Solution { ### **C++** ```cpp - +class Solution { +public: + int countWays(vector& nums) { + sort(nums.begin(), nums.end()); + int ans = 0; + int n = nums.size(); + for (int i = 0; i <= n; ++i) { + if ((i && nums[i - 1] >= i) || (i < n && nums[i] <= i)) { + continue; + } + ++ans; + } + return ans; + } +}; ``` ### **Go** ```go +func countWays(nums []int) (ans int) { + sort.Ints(nums) + n := len(nums) + for i := 0; i <= n; i++ { + if (i > 0 && nums[i-1] >= i) || (i < n && nums[i] <= i) { + continue + } + ans++ + } + return +} +``` + +### **TypeScript** +```ts +function countWays(nums: number[]): number { + nums.sort((a, b) => a - b); + let ans = 0; + const n = nums.length; + for (let i = 0; i <= n; ++i) { + if ((i && nums[i - 1] >= i) || (i < n && nums[i] <= i)) { + continue; + } + ++ans; + } + return ans; +} ``` ### **...** diff --git a/solution/2800-2899/2860.Happy Students/README_EN.md b/solution/2800-2899/2860.Happy Students/README_EN.md index 75796a6cfc4b2..b83a52336018b 100644 --- a/solution/2800-2899/2860.Happy Students/README_EN.md +++ b/solution/2800-2899/2860.Happy Students/README_EN.md @@ -55,7 +55,17 @@ The class teacher selects all the students to form the group. ### **Python3** ```python - +class Solution: + def countWays(self, nums: List[int]) -> int: + nums.sort() + n = len(nums) + ans = 0 + for i in range(n + 1): + if i and nums[i - 1] >= i: + continue + if i < n and nums[i] <= i: + continue + return ans ``` ### **Java** @@ -79,13 +89,54 @@ class Solution { ### **C++** ```cpp - +class Solution { +public: + int countWays(vector& nums) { + sort(nums.begin(), nums.end()); + int ans = 0; + int n = nums.size(); + for (int i = 0; i <= n; ++i) { + if ((i && nums[i - 1] >= i) || (i < n && nums[i] <= i)) { + continue; + } + ++ans; + } + return ans; + } +}; ``` ### **Go** ```go +func countWays(nums []int) (ans int) { + sort.Ints(nums) + n := len(nums) + for i := 0; i <= n; i++ { + if (i > 0 && nums[i-1] >= i) || (i < n && nums[i] <= i) { + continue + } + ans++ + } + return +} +``` + +### **TypeScript** +```ts +function countWays(nums: number[]): number { + nums.sort((a, b) => a - b); + let ans = 0; + const n = nums.length; + for (let i = 0; i <= n; ++i) { + if ((i && nums[i - 1] >= i) || (i < n && nums[i] <= i)) { + continue; + } + ++ans; + } + return ans; +} ``` ### **...** diff --git a/solution/2800-2899/2860.Happy Students/Solution.cpp b/solution/2800-2899/2860.Happy Students/Solution.cpp new file mode 100644 index 0000000000000..bcb4f8ec97572 --- /dev/null +++ b/solution/2800-2899/2860.Happy Students/Solution.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + int countWays(vector& nums) { + sort(nums.begin(), nums.end()); + int ans = 0; + int n = nums.size(); + for (int i = 0; i <= n; ++i) { + if ((i && nums[i - 1] >= i) || (i < n && nums[i] <= i)) { + continue; + } + ++ans; + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/2800-2899/2860.Happy Students/Solution.go b/solution/2800-2899/2860.Happy Students/Solution.go new file mode 100644 index 0000000000000..40bdd8d427c50 --- /dev/null +++ b/solution/2800-2899/2860.Happy Students/Solution.go @@ -0,0 +1,11 @@ +func countWays(nums []int) (ans int) { + sort.Ints(nums) + n := len(nums) + for i := 0; i <= n; i++ { + if (i > 0 && nums[i-1] >= i) || (i < n && nums[i] <= i) { + continue + } + ans++ + } + return +} \ No newline at end of file diff --git a/solution/2800-2899/2860.Happy Students/Solution.py b/solution/2800-2899/2860.Happy Students/Solution.py new file mode 100644 index 0000000000000..75a5e9623d8d0 --- /dev/null +++ b/solution/2800-2899/2860.Happy Students/Solution.py @@ -0,0 +1,11 @@ +class Solution: + def countWays(self, nums: List[int]) -> int: + nums.sort() + n = len(nums) + ans = 0 + for i in range(n + 1): + if i and nums[i - 1] >= i: + continue + if i < n and nums[i] <= i: + continue + return ans diff --git a/solution/2800-2899/2860.Happy Students/Solution.ts b/solution/2800-2899/2860.Happy Students/Solution.ts new file mode 100644 index 0000000000000..0e90c85018905 --- /dev/null +++ b/solution/2800-2899/2860.Happy Students/Solution.ts @@ -0,0 +1,12 @@ +function countWays(nums: number[]): number { + nums.sort((a, b) => a - b); + let ans = 0; + const n = nums.length; + for (let i = 0; i <= n; ++i) { + if ((i && nums[i - 1] >= i) || (i < n && nums[i] <= i)) { + continue; + } + ++ans; + } + return ans; +} 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 62045e9d8574e..2d8c7603edbd5 100644 --- a/solution/2800-2899/2861.Maximum Number of Alloys/README.md +++ b/solution/2800-2899/2861.Maximum Number of Alloys/README.md @@ -79,6 +79,14 @@ +**方法一:二分查找** + +我们注意到,所有合金都需要由同一台机器制造,因此我们可以枚举使用哪一台机器来制造合金。 + +对于每一台机器,我们可以使用二分查找的方法找出最大的整数 $x$,使得我们可以使用这台机器制造 $x$ 份合金。找出所有 $x$ 中的最大值即为答案。 + +时间复杂度 $O(n \times k \times \log M)$,其中 $M$ 是二分查找的上界,本题中 $M \leq 2 \times 10^8$。空间复杂度 $O(1)$。 + ### **Python3** @@ -86,7 +94,28 @@ ```python - +class Solution: + def maxNumberOfAlloys( + self, + n: int, + k: int, + budget: int, + composition: List[List[int]], + stock: List[int], + cost: List[int], + ) -> int: + ans = 0 + for c in composition: + l, r = 0, budget + stock[0] + while l < r: + mid = (l + r + 1) >> 1 + s = sum(max(0, mid * x - y) * z for x, y, z in zip(c, stock, cost)) + if s <= budget: + l = mid + else: + r = mid - 1 + ans = max(ans, l) + return ans ``` ### **Java** @@ -96,16 +125,14 @@ ```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++) { + for (List currMachine : composition) { 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); @@ -120,7 +147,6 @@ class Solution { 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; @@ -143,13 +169,111 @@ class Solution { ### **C++** ```cpp - +class Solution { +public: + int maxNumberOfAlloys(int n, int k, int budget, vector>& composition, vector& stock, vector& cost) { + auto isValid = [&](long long target) { + for (int i = 0; i < k; i++) { + long long remain = budget; + auto currMachine = composition[i]; + for (int j = 0; j < n && remain >= 0; j++) { + long long need = max(0LL, target * currMachine[j] - stock[j]); + remain -= need * cost[j]; + } + if (remain >= 0) { + return true; + } + } + return false; + }; + long long l = 0, r = budget + stock[0]; + while (l < r) { + long long mid = (l + r + 1) >> 1; + if (isValid(mid)) { + l = mid; + } else { + r = mid - 1; + } + } + return l; + } +}; ``` ### **Go** ```go +func maxNumberOfAlloys(n int, k int, budget int, composition [][]int, stock []int, cost []int) int { + isValid := func(target int) bool { + for _, currMachine := range composition { + remain := budget + for i, x := range currMachine { + need := max(0, x*target-stock[i]) + remain -= need * cost[i] + } + if remain >= 0 { + return true + } + } + return false + } + + l, r := 0, budget+stock[0] + for l < r { + mid := (l + r + 1) >> 1 + if isValid(mid) { + l = mid + } else { + r = mid - 1 + } + } + return l +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} +``` +### **TypeScript** + +```ts +function maxNumberOfAlloys( + n: number, + k: number, + budget: number, + composition: number[][], + stock: number[], + cost: number[], +): number { + let l = 0; + let r = budget + stock[0]; + const isValid = (target: number): boolean => { + for (const currMachine of composition) { + let remain = budget; + for (let i = 0; i < n; ++i) { + let need = Math.max(0, target * currMachine[i] - stock[i]); + remain -= need * cost[i]; + } + if (remain >= 0) { + return true; + } + } + return false; + }; + while (l < r) { + const mid = (l + r + 1) >> 1; + if (isValid(mid)) { + l = mid; + } else { + r = mid - 1; + } + } + return l; +} ``` ### **...** 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 cafe9053406cb..19d2beb14bdb1 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 @@ -78,7 +78,28 @@ It can be proven that we can create at most 2 alloys. ### **Python3** ```python - +class Solution: + def maxNumberOfAlloys( + self, + n: int, + k: int, + budget: int, + composition: List[List[int]], + stock: List[int], + cost: List[int], + ) -> int: + ans = 0 + for c in composition: + l, r = 0, budget + stock[0] + while l < r: + mid = (l + r + 1) >> 1 + s = sum(max(0, mid * x - y) * z for x, y, z in zip(c, stock, cost)) + if s <= budget: + l = mid + else: + r = mid - 1 + ans = max(ans, l) + return ans ``` ### **Java** @@ -86,16 +107,14 @@ It can be proven that we can create at most 2 alloys. ```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++) { + for (List currMachine : composition) { 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); @@ -110,7 +129,6 @@ class Solution { 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; @@ -133,13 +151,111 @@ class Solution { ### **C++** ```cpp - +class Solution { +public: + int maxNumberOfAlloys(int n, int k, int budget, vector>& composition, vector& stock, vector& cost) { + auto isValid = [&](long long target) { + for (int i = 0; i < k; i++) { + long long remain = budget; + auto currMachine = composition[i]; + for (int j = 0; j < n && remain >= 0; j++) { + long long need = max(0LL, target * currMachine[j] - stock[j]); + remain -= need * cost[j]; + } + if (remain >= 0) { + return true; + } + } + return false; + }; + long long l = 0, r = budget + stock[0]; + while (l < r) { + long long mid = (l + r + 1) >> 1; + if (isValid(mid)) { + l = mid; + } else { + r = mid - 1; + } + } + return l; + } +}; ``` ### **Go** ```go +func maxNumberOfAlloys(n int, k int, budget int, composition [][]int, stock []int, cost []int) int { + isValid := func(target int) bool { + for _, currMachine := range composition { + remain := budget + for i, x := range currMachine { + need := max(0, x*target-stock[i]) + remain -= need * cost[i] + } + if remain >= 0 { + return true + } + } + return false + } + + l, r := 0, budget+stock[0] + for l < r { + mid := (l + r + 1) >> 1 + if isValid(mid) { + l = mid + } else { + r = mid - 1 + } + } + return l +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} +``` +### **TypeScript** + +```ts +function maxNumberOfAlloys( + n: number, + k: number, + budget: number, + composition: number[][], + stock: number[], + cost: number[], +): number { + let l = 0; + let r = budget + stock[0]; + const isValid = (target: number): boolean => { + for (const currMachine of composition) { + let remain = budget; + for (let i = 0; i < n; ++i) { + let need = Math.max(0, target * currMachine[i] - stock[i]); + remain -= need * cost[i]; + } + if (remain >= 0) { + return true; + } + } + return false; + }; + while (l < r) { + const mid = (l + r + 1) >> 1; + if (isValid(mid)) { + l = mid; + } else { + r = mid - 1; + } + } + return l; +} ``` ### **...** diff --git a/solution/2800-2899/2861.Maximum Number of Alloys/Solution.cpp b/solution/2800-2899/2861.Maximum Number of Alloys/Solution.cpp new file mode 100644 index 0000000000000..1fa88708517cc --- /dev/null +++ b/solution/2800-2899/2861.Maximum Number of Alloys/Solution.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + int maxNumberOfAlloys(int n, int k, int budget, vector>& composition, vector& stock, vector& cost) { + auto isValid = [&](long long target) { + for (int i = 0; i < k; i++) { + long long remain = budget; + auto currMachine = composition[i]; + for (int j = 0; j < n && remain >= 0; j++) { + long long need = max(0LL, target * currMachine[j] - stock[j]); + remain -= need * cost[j]; + } + if (remain >= 0) { + return true; + } + } + return false; + }; + long long l = 0, r = budget + stock[0]; + while (l < r) { + long long 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/2861.Maximum Number of Alloys/Solution.go b/solution/2800-2899/2861.Maximum Number of Alloys/Solution.go new file mode 100644 index 0000000000000..db628dc3e0398 --- /dev/null +++ b/solution/2800-2899/2861.Maximum Number of Alloys/Solution.go @@ -0,0 +1,33 @@ +func maxNumberOfAlloys(n int, k int, budget int, composition [][]int, stock []int, cost []int) int { + isValid := func(target int) bool { + for _, currMachine := range composition { + remain := budget + for i, x := range currMachine { + need := max(0, x*target-stock[i]) + remain -= need * cost[i] + } + if remain >= 0 { + return true + } + } + return false + } + + l, r := 0, budget+stock[0] + for l < r { + mid := (l + r + 1) >> 1 + if isValid(mid) { + l = mid + } else { + r = mid - 1 + } + } + return l +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} \ No newline at end of file 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 90d9f0274c424..07c6107dd7d97 100644 --- a/solution/2800-2899/2861.Maximum Number of Alloys/Solution.java +++ b/solution/2800-2899/2861.Maximum Number of Alloys/Solution.java @@ -1,15 +1,13 @@ 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++) { + for (List currMachine : composition) { 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); @@ -24,7 +22,6 @@ boolean isValid(long target) { 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; diff --git a/solution/2800-2899/2861.Maximum Number of Alloys/Solution.py b/solution/2800-2899/2861.Maximum Number of Alloys/Solution.py new file mode 100644 index 0000000000000..40ce831f74b15 --- /dev/null +++ b/solution/2800-2899/2861.Maximum Number of Alloys/Solution.py @@ -0,0 +1,22 @@ +class Solution: + def maxNumberOfAlloys( + self, + n: int, + k: int, + budget: int, + composition: List[List[int]], + stock: List[int], + cost: List[int], + ) -> int: + ans = 0 + for c in composition: + l, r = 0, budget + stock[0] + while l < r: + mid = (l + r + 1) >> 1 + s = sum(max(0, mid * x - y) * z for x, y, z in zip(c, stock, cost)) + if s <= budget: + l = mid + else: + r = mid - 1 + ans = max(ans, l) + return ans diff --git a/solution/2800-2899/2861.Maximum Number of Alloys/Solution.ts b/solution/2800-2899/2861.Maximum Number of Alloys/Solution.ts new file mode 100644 index 0000000000000..1e4532a56704d --- /dev/null +++ b/solution/2800-2899/2861.Maximum Number of Alloys/Solution.ts @@ -0,0 +1,33 @@ +function maxNumberOfAlloys( + n: number, + k: number, + budget: number, + composition: number[][], + stock: number[], + cost: number[], +): number { + let l = 0; + let r = budget + stock[0]; + const isValid = (target: number): boolean => { + for (const currMachine of composition) { + let remain = budget; + for (let i = 0; i < n; ++i) { + let need = Math.max(0, target * currMachine[i] - stock[i]); + remain -= need * cost[i]; + } + if (remain >= 0) { + return true; + } + } + return false; + }; + while (l < r) { + const mid = (l + r + 1) >> 1; + if (isValid(mid)) { + l = mid; + } else { + r = mid - 1; + } + } + return l; +}