Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6e8cd0d

Browse files
feat: update solutions to lc problems: No.2278,2279 (#3377)
* No.2278.Percentage of Letter in String * No.2279.Maximum Bags With Full Capacity of Rocks
1 parent 51b11dc commit 6e8cd0d

File tree

14 files changed

+204
-257
lines changed

14 files changed

+204
-257
lines changed

‎solution/2200-2299/2278.Percentage of Letter in String/README.md‎

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ tags:
5555

5656
<!-- solution:start -->
5757

58-
### 方法一
58+
### 方法一:计数
59+
60+
我们可以遍历字符串 $\textit{s},ドル统计其中等于 $\textit{letter}$ 的字符的个数,然后根据公式 $\textit{count} \times 100 ,円 / ,円 \textit{len}(\textit{s})$ 计算百分比。
61+
62+
时间复杂度 $O(n),ドル其中 $n$ 为字符串 $\textit{s}$ 的长度。空间复杂度 $O(1)$。
5963

6064
<!-- tabs:start -->
6165

@@ -89,9 +93,7 @@ class Solution {
8993
class Solution {
9094
public:
9195
int percentageLetter(string s, char letter) {
92-
int cnt = 0;
93-
for (char& c : s) cnt += c == letter;
94-
return cnt * 100 / s.size();
96+
return 100 * ranges::count(s, letter) / s.size();
9597
}
9698
};
9799
```
@@ -100,26 +102,16 @@ public:
100102
101103
```go
102104
func percentageLetter(s string, letter byte) int {
103-
cnt := 0
104-
for i := range s {
105-
if s[i] == letter {
106-
cnt++
107-
}
108-
}
109-
return cnt * 100 / len(s)
105+
return strings.Count(s, string(letter)) * 100 / len(s)
110106
}
111107
```
112108

113109
#### TypeScript
114110

115111
```ts
116112
function percentageLetter(s: string, letter: string): number {
117-
let count = 0;
118-
let total = s.length;
119-
for (let i of s) {
120-
if (i === letter) count++;
121-
}
122-
return Math.floor((count / total) * 100);
113+
const count = s.split('').filter(c => c === letter).length;
114+
return Math.floor((100 * count) / s.length);
123115
}
124116
```
125117

@@ -128,13 +120,8 @@ function percentageLetter(s: string, letter: string): number {
128120
```rust
129121
impl Solution {
130122
pub fn percentage_letter(s: String, letter: char) -> i32 {
131-
let mut count = 0;
132-
for c in s.chars() {
133-
if c == letter {
134-
count += 1;
135-
}
136-
}
137-
((count * 100) / s.len()) as i32
123+
let count = s.chars().filter(|&c| c == letter).count();
124+
(100 * count as i32 / s.len() as i32) as i32
138125
}
139126
}
140127
```

‎solution/2200-2299/2278.Percentage of Letter in String/README_EN.md‎

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ The percentage of characters in s that equal the letter &#39;k&#39; is 0%, so we
5353

5454
<!-- solution:start -->
5555

56-
### Solution 1
56+
### Solution 1: Counting
57+
58+
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})$.
59+
60+
Time complexity is $O(n),ドル where $n$ is the length of the string $\textit{s}$. Space complexity is $O(1)$.
5761

5862
<!-- tabs:start -->
5963

@@ -87,9 +91,7 @@ class Solution {
8791
class Solution {
8892
public:
8993
int percentageLetter(string s, char letter) {
90-
int cnt = 0;
91-
for (char& c : s) cnt += c == letter;
92-
return cnt * 100 / s.size();
94+
return 100 * ranges::count(s, letter) / s.size();
9395
}
9496
};
9597
```
@@ -98,26 +100,16 @@ public:
98100
99101
```go
100102
func percentageLetter(s string, letter byte) int {
101-
cnt := 0
102-
for i := range s {
103-
if s[i] == letter {
104-
cnt++
105-
}
106-
}
107-
return cnt * 100 / len(s)
103+
return strings.Count(s, string(letter)) * 100 / len(s)
108104
}
109105
```
110106

111107
#### TypeScript
112108

113109
```ts
114110
function percentageLetter(s: string, letter: string): number {
115-
let count = 0;
116-
let total = s.length;
117-
for (let i of s) {
118-
if (i === letter) count++;
119-
}
120-
return Math.floor((count / total) * 100);
111+
const count = s.split('').filter(c => c === letter).length;
112+
return Math.floor((100 * count) / s.length);
121113
}
122114
```
123115

@@ -126,13 +118,8 @@ function percentageLetter(s: string, letter: string): number {
126118
```rust
127119
impl Solution {
128120
pub fn percentage_letter(s: String, letter: char) -> i32 {
129-
let mut count = 0;
130-
for c in s.chars() {
131-
if c == letter {
132-
count += 1;
133-
}
134-
}
135-
((count * 100) / s.len()) as i32
121+
let count = s.chars().filter(|&c| c == letter).count();
122+
(100 * count as i32 / s.len() as i32) as i32
136123
}
137124
}
138125
```
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
class Solution {
22
public:
33
int percentageLetter(string s, char letter) {
4-
int cnt = 0;
5-
for (char& c : s) cnt += c == letter;
6-
return cnt * 100 / s.size();
4+
return 100 * ranges::count(s, letter) / s.size();
75
}
8-
};
6+
};
Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
11
func percentageLetter(s string, letter byte) int {
2-
cnt := 0
3-
for i := range s {
4-
if s[i] == letter {
5-
cnt++
6-
}
7-
}
8-
return cnt * 100 / len(s)
9-
}
2+
return strings.Count(s, string(letter)) * 100 / len(s)
3+
}
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
impl Solution {
22
pub fn percentage_letter(s: String, letter: char) -> i32 {
3-
let mut count = 0;
4-
for c in s.chars() {
5-
if c == letter {
6-
count += 1;
7-
}
8-
}
9-
((count * 100) / s.len()) as i32
3+
let count = s.chars().filter(|&c| c == letter).count();
4+
(100 * count as i32 / s.len() as i32) as i32
105
}
116
}
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
function percentageLetter(s: string, letter: string): number {
2-
let count = 0;
3-
let total = s.length;
4-
for (let i of s) {
5-
if (i === letter) count++;
6-
}
7-
return Math.floor((count / total) * 100);
2+
const count = s.split('').filter(c => c === letter).length;
3+
return Math.floor((100 * count) / s.length);
84
}

‎solution/2200-2299/2279.Maximum Bags With Full Capacity of Rocks/README.md‎

Lines changed: 58 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ tags:
7474

7575
### 方法一:排序 + 贪心
7676

77+
我们首先将每个背包的剩余容量计算出来,然后对剩余容量进行排序,接着我们从小到大遍历剩余容量,将额外的石头放入背包中,直到额外的石头用完或者背包的剩余容量用完为止,返回此时的背包数量即可。
78+
79+
时间复杂度 $O(n \times \log n),ドル空间复杂度 $O(\log n)$。其中 $n$ 为背包的数量。
80+
7781
<!-- tabs:start -->
7882

7983
#### Python3
@@ -83,37 +87,33 @@ class Solution:
8387
def maximumBags(
8488
self, capacity: List[int], rocks: List[int], additionalRocks: int
8589
) -> int:
86-
d = [a - b for a, b in zip(capacity, rocks)]
87-
d.sort()
88-
ans =0
89-
for v in d:
90-
if v <= additionalRocks:
91-
ans +=1
92-
additionalRocks -= v
93-
return ans
90+
for i, x in enumerate(rocks):
91+
capacity[i] -= x
92+
capacity.sort()
93+
for i, x in enumerate(capacity):
94+
additionalRocks -= x
95+
if additionalRocks <0:
96+
return i
97+
return len(capacity)
9498
```
9599

96100
#### Java
97101

98102
```java
99103
class Solution {
100104
public int maximumBags(int[] capacity, int[] rocks, int additionalRocks) {
101-
int n = capacity.length;
102-
int[] d = new int[n];
105+
int n = rocks.length;
103106
for (int i = 0; i < n; ++i) {
104-
d[i] =capacity[i] - rocks[i];
107+
capacity[i] -= rocks[i];
105108
}
106-
Arrays.sort(d);
107-
int ans = 0;
108-
for (int v : d) {
109-
if (v <= additionalRocks) {
110-
++ans;
111-
additionalRocks -= v;
112-
} else {
113-
break;
109+
Arrays.sort(capacity);
110+
for (int i = 0; i < n; ++i) {
111+
additionalRocks -= capacity[i];
112+
if (additionalRocks < 0) {
113+
return i;
114114
}
115115
}
116-
return ans;
116+
return n;
117117
}
118118
}
119119
```
@@ -124,17 +124,18 @@ class Solution {
124124
class Solution {
125125
public:
126126
int maximumBags(vector<int>& capacity, vector<int>& rocks, int additionalRocks) {
127-
int n = capacity.size();
128-
vector<int> d(n);
129-
for (int i = 0; i < n; ++i) d[i] = capacity[i] - rocks[i];
130-
sort(d.begin(), d.end());
131-
int ans = 0;
132-
for (int& v : d) {
133-
if (v > additionalRocks) break;
134-
++ans;
135-
additionalRocks -= v;
127+
int n = rocks.size();
128+
for (int i = 0; i < n; ++i) {
129+
capacity[i] -= rocks[i];
136130
}
137-
return ans;
131+
ranges::sort(capacity);
132+
for (int i = 0; i < n; ++i) {
133+
additionalRocks -= capacity[i];
134+
if (additionalRocks < 0) {
135+
return i;
136+
}
137+
}
138+
return n;
138139
}
139140
};
140141
```
@@ -143,58 +144,55 @@ public:
143144
144145
```go
145146
func maximumBags(capacity []int, rocks []int, additionalRocks int) int {
146-
n := len(capacity)
147-
d := make([]int, n)
148-
for i, v := range capacity {
149-
d[i] = v - rocks[i]
147+
for i, x := range rocks {
148+
capacity[i] -= x
150149
}
151-
sort.Ints(d)
152-
ans := 0
153-
for _, v := range d {
154-
if v > additionalRocks {
155-
break
150+
sort.Ints(capacity)
151+
for i, x := range capacity {
152+
additionalRocks -= x
153+
if additionalRocks < 0 {
154+
return i
156155
}
157-
ans++
158-
additionalRocks -= v
159156
}
160-
return ans
157+
return len(capacity)
161158
}
162159
```
163160

164161
#### TypeScript
165162

166163
```ts
167164
function maximumBags(capacity: number[], rocks: number[], additionalRocks: number): number {
168-
const n = capacity.length;
169-
const diffs = capacity.map((c, i) => c - rocks[i]);
170-
diffs.sort((a, b) => a - b);
171-
let ans = 0;
172-
for (let i = 0; i < n && (diffs[i] === 0 || diffs[i] <= additionalRocks); i++) {
173-
ans++;
174-
additionalRocks -= diffs[i];
165+
const n = rocks.length;
166+
for (let i = 0; i < n; ++i) {
167+
capacity[i] -= rocks[i];
168+
}
169+
capacity.sort((a, b) => a - b);
170+
for (let i = 0; i < n; ++i) {
171+
additionalRocks -= capacity[i];
172+
if (additionalRocks < 0) {
173+
return i;
174+
}
175175
}
176-
return ans;
176+
return n;
177177
}
178178
```
179179

180180
#### Rust
181181

182182
```rust
183183
impl Solution {
184-
pub fn maximum_bags(capacity: Vec<i32>, rocks: Vec<i32>, mut additional_rocks: i32) -> i32 {
185-
let n = capacity.len();
186-
let mut diffs = vec![0; n];
187-
for i in 0..n {
188-
diffs[i] = capacity[i] - rocks[i];
184+
pub fn maximum_bags(mut capacity: Vec<i32>, rocks: Vec<i32>, mut additional_rocks: i32) -> i32 {
185+
for i in 0..rocks.len() {
186+
capacity[i] -= rocks[i];
189187
}
190-
diffs.sort();
191-
for i in 0..n {
192-
if diffs[i] > additional_rocks {
188+
capacity.sort();
189+
for i in 0..capacity.len() {
190+
additional_rocks -= capacity[i];
191+
if additional_rocks < 0 {
193192
return i as i32;
194193
}
195-
additional_rocks -= diffs[i];
196194
}
197-
n as i32
195+
capacity.len() as i32
198196
}
199197
}
200198
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /