diff --git a/solution/2200-2299/2278.Percentage of Letter in String/README.md b/solution/2200-2299/2278.Percentage of Letter in String/README.md index 8c1d7fc62de18..f07378e546082 100644 --- a/solution/2200-2299/2278.Percentage of Letter in String/README.md +++ b/solution/2200-2299/2278.Percentage of Letter in String/README.md @@ -55,7 +55,11 @@ tags: -### 方法一 +### 方法一:计数 + +我们可以遍历字符串 $\textit{s}$,统计其中等于 $\textit{letter}$ 的字符的个数,然后根据公式 $\textit{count} \times 100 \, / \, \textit{len}(\textit{s})$ 计算百分比。 + +时间复杂度 $O(n)$,其中 $n$ 为字符串 $\textit{s}$ 的长度。空间复杂度 $O(1)$。 @@ -89,9 +93,7 @@ class Solution { class Solution { public: int percentageLetter(string s, char letter) { - int cnt = 0; - for (char& c : s) cnt += c == letter; - return cnt * 100 / s.size(); + return 100 * ranges::count(s, letter) / s.size(); } }; ``` @@ -100,13 +102,7 @@ public: ```go func percentageLetter(s string, letter byte) int { - cnt := 0 - for i := range s { - if s[i] == letter { - cnt++ - } - } - return cnt * 100 / len(s) + return strings.Count(s, string(letter)) * 100 / len(s) } ``` @@ -114,12 +110,8 @@ func percentageLetter(s string, letter byte) int { ```ts function percentageLetter(s: string, letter: string): number { - let count = 0; - let total = s.length; - for (let i of s) { - if (i === letter) count++; - } - return Math.floor((count / total) * 100); + const count = s.split('').filter(c => c === letter).length; + return Math.floor((100 * count) / s.length); } ``` @@ -128,13 +120,8 @@ function percentageLetter(s: string, letter: string): number { ```rust impl Solution { pub fn percentage_letter(s: String, letter: char) -> i32 { - let mut count = 0; - for c in s.chars() { - if c == letter { - count += 1; - } - } - ((count * 100) / s.len()) as i32 + let count = s.chars().filter(|&c| c == letter).count(); + (100 * count as i32 / s.len() as i32) as i32 } } ``` diff --git a/solution/2200-2299/2278.Percentage of Letter in String/README_EN.md b/solution/2200-2299/2278.Percentage of Letter in String/README_EN.md index 2152a2dc0ff70..60c71ac9de8e5 100644 --- a/solution/2200-2299/2278.Percentage of Letter in String/README_EN.md +++ b/solution/2200-2299/2278.Percentage of Letter in String/README_EN.md @@ -53,7 +53,11 @@ The percentage of characters in s that equal the letter 'k' is 0%, so we -### Solution 1 +### Solution 1: Counting + +We can traverse the string $\textit{s}$ and count the number of characters that are equal to $\textit{letter}$. Then, we calculate the percentage using the formula $\textit{count} \times 100 \, / \, \textit{len}(\textit{s})$. + +Time complexity is $O(n)$, where $n$ is the length of the string $\textit{s}$. Space complexity is $O(1)$. @@ -87,9 +91,7 @@ class Solution { class Solution { public: int percentageLetter(string s, char letter) { - int cnt = 0; - for (char& c : s) cnt += c == letter; - return cnt * 100 / s.size(); + return 100 * ranges::count(s, letter) / s.size(); } }; ``` @@ -98,13 +100,7 @@ public: ```go func percentageLetter(s string, letter byte) int { - cnt := 0 - for i := range s { - if s[i] == letter { - cnt++ - } - } - return cnt * 100 / len(s) + return strings.Count(s, string(letter)) * 100 / len(s) } ``` @@ -112,12 +108,8 @@ func percentageLetter(s string, letter byte) int { ```ts function percentageLetter(s: string, letter: string): number { - let count = 0; - let total = s.length; - for (let i of s) { - if (i === letter) count++; - } - return Math.floor((count / total) * 100); + const count = s.split('').filter(c => c === letter).length; + return Math.floor((100 * count) / s.length); } ``` @@ -126,13 +118,8 @@ function percentageLetter(s: string, letter: string): number { ```rust impl Solution { pub fn percentage_letter(s: String, letter: char) -> i32 { - let mut count = 0; - for c in s.chars() { - if c == letter { - count += 1; - } - } - ((count * 100) / s.len()) as i32 + let count = s.chars().filter(|&c| c == letter).count(); + (100 * count as i32 / s.len() as i32) as i32 } } ``` diff --git a/solution/2200-2299/2278.Percentage of Letter in String/Solution.cpp b/solution/2200-2299/2278.Percentage of Letter in String/Solution.cpp index 055559d6c2cd1..a8eea05b52f67 100644 --- a/solution/2200-2299/2278.Percentage of Letter in String/Solution.cpp +++ b/solution/2200-2299/2278.Percentage of Letter in String/Solution.cpp @@ -1,8 +1,6 @@ class Solution { public: int percentageLetter(string s, char letter) { - int cnt = 0; - for (char& c : s) cnt += c == letter; - return cnt * 100 / s.size(); + return 100 * ranges::count(s, letter) / s.size(); } -}; \ No newline at end of file +}; diff --git a/solution/2200-2299/2278.Percentage of Letter in String/Solution.go b/solution/2200-2299/2278.Percentage of Letter in String/Solution.go index 2bb7eacd1817e..0ae12142cc523 100644 --- a/solution/2200-2299/2278.Percentage of Letter in String/Solution.go +++ b/solution/2200-2299/2278.Percentage of Letter in String/Solution.go @@ -1,9 +1,3 @@ func percentageLetter(s string, letter byte) int { - cnt := 0 - for i := range s { - if s[i] == letter { - cnt++ - } - } - return cnt * 100 / len(s) -} \ No newline at end of file + return strings.Count(s, string(letter)) * 100 / len(s) +} diff --git a/solution/2200-2299/2278.Percentage of Letter in String/Solution.rs b/solution/2200-2299/2278.Percentage of Letter in String/Solution.rs index 5d3cee347cba6..616221e7c6851 100644 --- a/solution/2200-2299/2278.Percentage of Letter in String/Solution.rs +++ b/solution/2200-2299/2278.Percentage of Letter in String/Solution.rs @@ -1,11 +1,6 @@ impl Solution { pub fn percentage_letter(s: String, letter: char) -> i32 { - let mut count = 0; - for c in s.chars() { - if c == letter { - count += 1; - } - } - ((count * 100) / s.len()) as i32 + let count = s.chars().filter(|&c| c == letter).count(); + (100 * count as i32 / s.len() as i32) as i32 } } diff --git a/solution/2200-2299/2278.Percentage of Letter in String/Solution.ts b/solution/2200-2299/2278.Percentage of Letter in String/Solution.ts index fdd2298b5231e..d6d43008df7fe 100644 --- a/solution/2200-2299/2278.Percentage of Letter in String/Solution.ts +++ b/solution/2200-2299/2278.Percentage of Letter in String/Solution.ts @@ -1,8 +1,4 @@ function percentageLetter(s: string, letter: string): number { - let count = 0; - let total = s.length; - for (let i of s) { - if (i === letter) count++; - } - return Math.floor((count / total) * 100); + const count = s.split('').filter(c => c === letter).length; + return Math.floor((100 * count) / s.length); } diff --git a/solution/2200-2299/2279.Maximum Bags With Full Capacity of Rocks/README.md b/solution/2200-2299/2279.Maximum Bags With Full Capacity of Rocks/README.md index 9383d4f33594b..c83b11fb6bca9 100644 --- a/solution/2200-2299/2279.Maximum Bags With Full Capacity of Rocks/README.md +++ b/solution/2200-2299/2279.Maximum Bags With Full Capacity of Rocks/README.md @@ -74,6 +74,10 @@ tags: ### 方法一:排序 + 贪心 +我们首先将每个背包的剩余容量计算出来,然后对剩余容量进行排序,接着我们从小到大遍历剩余容量,将额外的石头放入背包中,直到额外的石头用完或者背包的剩余容量用完为止,返回此时的背包数量即可。 + +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为背包的数量。 + #### Python3 @@ -83,14 +87,14 @@ class Solution: def maximumBags( self, capacity: List[int], rocks: List[int], additionalRocks: int ) -> int: - d = [a - b for a, b in zip(capacity, rocks)] - d.sort() - ans = 0 - for v in d: - if v <= additionalRocks: - ans += 1 - additionalRocks -= v - return ans + for i, x in enumerate(rocks): + capacity[i] -= x + capacity.sort() + for i, x in enumerate(capacity): + additionalRocks -= x + if additionalRocks < 0: + return i + return len(capacity) ``` #### Java @@ -98,22 +102,18 @@ class Solution: ```java class Solution { public int maximumBags(int[] capacity, int[] rocks, int additionalRocks) { - int n = capacity.length; - int[] d = new int[n]; + int n = rocks.length; for (int i = 0; i < n; ++i) { - d[i] = capacity[i] - rocks[i]; + capacity[i] -= rocks[i]; } - Arrays.sort(d); - int ans = 0; - for (int v : d) { - if (v <= additionalRocks) { - ++ans; - additionalRocks -= v; - } else { - break; + Arrays.sort(capacity); + for (int i = 0; i < n; ++i) { + additionalRocks -= capacity[i]; + if (additionalRocks < 0) { + return i; } } - return ans; + return n; } } ``` @@ -124,17 +124,18 @@ class Solution { class Solution { public: int maximumBags(vector& capacity, vector& rocks, int additionalRocks) { - int n = capacity.size(); - vector d(n); - for (int i = 0; i < n; ++i) d[i] = capacity[i] - rocks[i]; - sort(d.begin(), d.end()); - int ans = 0; - for (int& v : d) { - if (v > additionalRocks) break; - ++ans; - additionalRocks -= v; + int n = rocks.size(); + for (int i = 0; i < n; ++i) { + capacity[i] -= rocks[i]; } - return ans; + ranges::sort(capacity); + for (int i = 0; i < n; ++i) { + additionalRocks -= capacity[i]; + if (additionalRocks < 0) { + return i; + } + } + return n; } }; ``` @@ -143,21 +144,17 @@ public: ```go func maximumBags(capacity []int, rocks []int, additionalRocks int) int { - n := len(capacity) - d := make([]int, n) - for i, v := range capacity { - d[i] = v - rocks[i] + for i, x := range rocks { + capacity[i] -= x } - sort.Ints(d) - ans := 0 - for _, v := range d { - if v > additionalRocks { - break + sort.Ints(capacity) + for i, x := range capacity { + additionalRocks -= x + if additionalRocks < 0 { + return i } - ans++ - additionalRocks -= v } - return ans + return len(capacity) } ``` @@ -165,15 +162,18 @@ func maximumBags(capacity []int, rocks []int, additionalRocks int) int { ```ts function maximumBags(capacity: number[], rocks: number[], additionalRocks: number): number { - const n = capacity.length; - const diffs = capacity.map((c, i) => c - rocks[i]); - diffs.sort((a, b) => a - b); - let ans = 0; - for (let i = 0; i < n && (diffs[i] === 0 || diffs[i] <= additionalRocks); i++) { - ans++; - additionalRocks -= diffs[i]; + const n = rocks.length; + for (let i = 0; i < n; ++i) { + capacity[i] -= rocks[i]; + } + capacity.sort((a, b) => a - b); + for (let i = 0; i < n; ++i) { + additionalRocks -= capacity[i]; + if (additionalRocks < 0) { + return i; + } } - return ans; + return n; } ``` @@ -181,20 +181,18 @@ function maximumBags(capacity: number[], rocks: number[], additionalRocks: numbe ```rust impl Solution { - pub fn maximum_bags(capacity: Vec, rocks: Vec, mut additional_rocks: i32) -> i32 { - let n = capacity.len(); - let mut diffs = vec![0; n]; - for i in 0..n { - diffs[i] = capacity[i] - rocks[i]; + pub fn maximum_bags(mut capacity: Vec, rocks: Vec, mut additional_rocks: i32) -> i32 { + for i in 0..rocks.len() { + capacity[i] -= rocks[i]; } - diffs.sort(); - for i in 0..n { - if diffs[i] > additional_rocks { + capacity.sort(); + for i in 0..capacity.len() { + additional_rocks -= capacity[i]; + if additional_rocks < 0 { return i as i32; } - additional_rocks -= diffs[i]; } - n as i32 + capacity.len() as i32 } } ``` diff --git a/solution/2200-2299/2279.Maximum Bags With Full Capacity of Rocks/README_EN.md b/solution/2200-2299/2279.Maximum Bags With Full Capacity of Rocks/README_EN.md index 8b53a9d2dcb78..5b35dca0954be 100644 --- a/solution/2200-2299/2279.Maximum Bags With Full Capacity of Rocks/README_EN.md +++ b/solution/2200-2299/2279.Maximum Bags With Full Capacity of Rocks/README_EN.md @@ -70,7 +70,11 @@ Note that we did not use all of the additional rocks. -### Solution 1 +### Solution 1: Sorting + Greedy + +First, we calculate the remaining capacity of each bag, then sort the remaining capacities. Next, we traverse the remaining capacities from smallest to largest, putting the extra stones into the bags until the extra stones are used up or the remaining capacities of the bags are exhausted. Finally, we return the number of bags at this point. + +Time complexity is $O(n \times \log n)$, and space complexity is $O(\log n)$. Here, $n$ is the number of bags. @@ -81,14 +85,14 @@ class Solution: def maximumBags( self, capacity: List[int], rocks: List[int], additionalRocks: int ) -> int: - d = [a - b for a, b in zip(capacity, rocks)] - d.sort() - ans = 0 - for v in d: - if v <= additionalRocks: - ans += 1 - additionalRocks -= v - return ans + for i, x in enumerate(rocks): + capacity[i] -= x + capacity.sort() + for i, x in enumerate(capacity): + additionalRocks -= x + if additionalRocks < 0: + return i + return len(capacity) ``` #### Java @@ -96,22 +100,18 @@ class Solution: ```java class Solution { public int maximumBags(int[] capacity, int[] rocks, int additionalRocks) { - int n = capacity.length; - int[] d = new int[n]; + int n = rocks.length; for (int i = 0; i < n; ++i) { - d[i] = capacity[i] - rocks[i]; + capacity[i] -= rocks[i]; } - Arrays.sort(d); - int ans = 0; - for (int v : d) { - if (v <= additionalRocks) { - ++ans; - additionalRocks -= v; - } else { - break; + Arrays.sort(capacity); + for (int i = 0; i < n; ++i) { + additionalRocks -= capacity[i]; + if (additionalRocks < 0) { + return i; } } - return ans; + return n; } } ``` @@ -122,17 +122,18 @@ class Solution { class Solution { public: int maximumBags(vector& capacity, vector& rocks, int additionalRocks) { - int n = capacity.size(); - vector d(n); - for (int i = 0; i < n; ++i) d[i] = capacity[i] - rocks[i]; - sort(d.begin(), d.end()); - int ans = 0; - for (int& v : d) { - if (v > additionalRocks) break; - ++ans; - additionalRocks -= v; + int n = rocks.size(); + for (int i = 0; i < n; ++i) { + capacity[i] -= rocks[i]; } - return ans; + ranges::sort(capacity); + for (int i = 0; i < n; ++i) { + additionalRocks -= capacity[i]; + if (additionalRocks < 0) { + return i; + } + } + return n; } }; ``` @@ -141,21 +142,17 @@ public: ```go func maximumBags(capacity []int, rocks []int, additionalRocks int) int { - n := len(capacity) - d := make([]int, n) - for i, v := range capacity { - d[i] = v - rocks[i] + for i, x := range rocks { + capacity[i] -= x } - sort.Ints(d) - ans := 0 - for _, v := range d { - if v > additionalRocks { - break + sort.Ints(capacity) + for i, x := range capacity { + additionalRocks -= x + if additionalRocks < 0 { + return i } - ans++ - additionalRocks -= v } - return ans + return len(capacity) } ``` @@ -163,15 +160,18 @@ func maximumBags(capacity []int, rocks []int, additionalRocks int) int { ```ts function maximumBags(capacity: number[], rocks: number[], additionalRocks: number): number { - const n = capacity.length; - const diffs = capacity.map((c, i) => c - rocks[i]); - diffs.sort((a, b) => a - b); - let ans = 0; - for (let i = 0; i < n && (diffs[i] === 0 || diffs[i] <= additionalRocks); i++) { - ans++; - additionalRocks -= diffs[i]; + const n = rocks.length; + for (let i = 0; i < n; ++i) { + capacity[i] -= rocks[i]; + } + capacity.sort((a, b) => a - b); + for (let i = 0; i < n; ++i) { + additionalRocks -= capacity[i]; + if (additionalRocks < 0) { + return i; + } } - return ans; + return n; } ``` @@ -179,20 +179,18 @@ function maximumBags(capacity: number[], rocks: number[], additionalRocks: numbe ```rust impl Solution { - pub fn maximum_bags(capacity: Vec, rocks: Vec, mut additional_rocks: i32) -> i32 { - let n = capacity.len(); - let mut diffs = vec![0; n]; - for i in 0..n { - diffs[i] = capacity[i] - rocks[i]; + pub fn maximum_bags(mut capacity: Vec, rocks: Vec, mut additional_rocks: i32) -> i32 { + for i in 0..rocks.len() { + capacity[i] -= rocks[i]; } - diffs.sort(); - for i in 0..n { - if diffs[i] > additional_rocks { + capacity.sort(); + for i in 0..capacity.len() { + additional_rocks -= capacity[i]; + if additional_rocks < 0 { return i as i32; } - additional_rocks -= diffs[i]; } - n as i32 + capacity.len() as i32 } } ``` diff --git a/solution/2200-2299/2279.Maximum Bags With Full Capacity of Rocks/Solution.cpp b/solution/2200-2299/2279.Maximum Bags With Full Capacity of Rocks/Solution.cpp index 0cc800926f796..1c829c8bc6db7 100644 --- a/solution/2200-2299/2279.Maximum Bags With Full Capacity of Rocks/Solution.cpp +++ b/solution/2200-2299/2279.Maximum Bags With Full Capacity of Rocks/Solution.cpp @@ -1,16 +1,17 @@ class Solution { public: int maximumBags(vector& capacity, vector& rocks, int additionalRocks) { - int n = capacity.size(); - vector d(n); - for (int i = 0; i < n; ++i) d[i] = capacity[i] - rocks[i]; - sort(d.begin(), d.end()); - int ans = 0; - for (int& v : d) { - if (v > additionalRocks) break; - ++ans; - additionalRocks -= v; + int n = rocks.size(); + for (int i = 0; i < n; ++i) { + capacity[i] -= rocks[i]; } - return ans; + ranges::sort(capacity); + for (int i = 0; i < n; ++i) { + additionalRocks -= capacity[i]; + if (additionalRocks < 0) { + return i; + } + } + return n; } -}; \ No newline at end of file +}; diff --git a/solution/2200-2299/2279.Maximum Bags With Full Capacity of Rocks/Solution.go b/solution/2200-2299/2279.Maximum Bags With Full Capacity of Rocks/Solution.go index b4b1f56386b3f..71750d79224cc 100644 --- a/solution/2200-2299/2279.Maximum Bags With Full Capacity of Rocks/Solution.go +++ b/solution/2200-2299/2279.Maximum Bags With Full Capacity of Rocks/Solution.go @@ -1,17 +1,13 @@ func maximumBags(capacity []int, rocks []int, additionalRocks int) int { - n := len(capacity) - d := make([]int, n) - for i, v := range capacity { - d[i] = v - rocks[i] + for i, x := range rocks { + capacity[i] -= x } - sort.Ints(d) - ans := 0 - for _, v := range d { - if v > additionalRocks { - break + sort.Ints(capacity) + for i, x := range capacity { + additionalRocks -= x + if additionalRocks < 0 { + return i } - ans++ - additionalRocks -= v } - return ans -} \ No newline at end of file + return len(capacity) +} diff --git a/solution/2200-2299/2279.Maximum Bags With Full Capacity of Rocks/Solution.java b/solution/2200-2299/2279.Maximum Bags With Full Capacity of Rocks/Solution.java index a05c54f6caeff..3c4f5cdadbde5 100644 --- a/solution/2200-2299/2279.Maximum Bags With Full Capacity of Rocks/Solution.java +++ b/solution/2200-2299/2279.Maximum Bags With Full Capacity of Rocks/Solution.java @@ -1,20 +1,16 @@ class Solution { public int maximumBags(int[] capacity, int[] rocks, int additionalRocks) { - int n = capacity.length; - int[] d = new int[n]; + int n = rocks.length; for (int i = 0; i < n; ++i) { - d[i] = capacity[i] - rocks[i]; + capacity[i] -= rocks[i]; } - Arrays.sort(d); - int ans = 0; - for (int v : d) { - if (v <= additionalRocks) { - ++ans; - additionalRocks -= v; - } else { - break; + Arrays.sort(capacity); + for (int i = 0; i < n; ++i) { + additionalRocks -= capacity[i]; + if (additionalRocks < 0) { + return i; } } - return ans; + return n; } -} \ No newline at end of file +} diff --git a/solution/2200-2299/2279.Maximum Bags With Full Capacity of Rocks/Solution.py b/solution/2200-2299/2279.Maximum Bags With Full Capacity of Rocks/Solution.py index 009adf577a2f3..c6aba5308427b 100644 --- a/solution/2200-2299/2279.Maximum Bags With Full Capacity of Rocks/Solution.py +++ b/solution/2200-2299/2279.Maximum Bags With Full Capacity of Rocks/Solution.py @@ -2,11 +2,11 @@ class Solution: def maximumBags( self, capacity: List[int], rocks: List[int], additionalRocks: int ) -> int: - d = [a - b for a, b in zip(capacity, rocks)] - d.sort() - ans = 0 - for v in d: - if v <= additionalRocks: - ans += 1 - additionalRocks -= v - return ans + for i, x in enumerate(rocks): + capacity[i] -= x + capacity.sort() + for i, x in enumerate(capacity): + additionalRocks -= x + if additionalRocks < 0: + return i + return len(capacity) diff --git a/solution/2200-2299/2279.Maximum Bags With Full Capacity of Rocks/Solution.rs b/solution/2200-2299/2279.Maximum Bags With Full Capacity of Rocks/Solution.rs index 77645fee37285..5339d5e7cf371 100644 --- a/solution/2200-2299/2279.Maximum Bags With Full Capacity of Rocks/Solution.rs +++ b/solution/2200-2299/2279.Maximum Bags With Full Capacity of Rocks/Solution.rs @@ -1,17 +1,15 @@ impl Solution { - pub fn maximum_bags(capacity: Vec, rocks: Vec, mut additional_rocks: i32) -> i32 { - let n = capacity.len(); - let mut diffs = vec![0; n]; - for i in 0..n { - diffs[i] = capacity[i] - rocks[i]; + pub fn maximum_bags(mut capacity: Vec, rocks: Vec, mut additional_rocks: i32) -> i32 { + for i in 0..rocks.len() { + capacity[i] -= rocks[i]; } - diffs.sort(); - for i in 0..n { - if diffs[i] > additional_rocks { + capacity.sort(); + for i in 0..capacity.len() { + additional_rocks -= capacity[i]; + if additional_rocks < 0 { return i as i32; } - additional_rocks -= diffs[i]; } - n as i32 + capacity.len() as i32 } } diff --git a/solution/2200-2299/2279.Maximum Bags With Full Capacity of Rocks/Solution.ts b/solution/2200-2299/2279.Maximum Bags With Full Capacity of Rocks/Solution.ts index 863e3a13e9b88..7cd56e4e530da 100644 --- a/solution/2200-2299/2279.Maximum Bags With Full Capacity of Rocks/Solution.ts +++ b/solution/2200-2299/2279.Maximum Bags With Full Capacity of Rocks/Solution.ts @@ -1,11 +1,14 @@ function maximumBags(capacity: number[], rocks: number[], additionalRocks: number): number { - const n = capacity.length; - const diffs = capacity.map((c, i) => c - rocks[i]); - diffs.sort((a, b) => a - b); - let ans = 0; - for (let i = 0; i < n && (diffs[i] === 0 || diffs[i] <= additionalRocks); i++) { - ans++; - additionalRocks -= diffs[i]; + const n = rocks.length; + for (let i = 0; i < n; ++i) { + capacity[i] -= rocks[i]; } - return ans; + capacity.sort((a, b) => a - b); + for (let i = 0; i < n; ++i) { + additionalRocks -= capacity[i]; + if (additionalRocks < 0) { + return i; + } + } + return n; }