Skip to content

feat: update solutions to lc problems: No.2278,2279 #3377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 11 additions & 24 deletions solution/2200-2299/2278.Percentage of Letter in String/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:计数

我们可以遍历字符串 $\textit{s}$,统计其中等于 $\textit{letter}$ 的字符的个数,然后根据公式 $\textit{count} \times 100 \, / \, \textit{len}(\textit{s})$ 计算百分比。

时间复杂度 $O(n)$,其中 $n$ 为字符串 $\textit{s}$ 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand Down Expand Up @@ -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();
}
};
```
Expand All @@ -100,26 +102,16 @@ 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)
}
```

#### TypeScript

```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);
}
```

Expand All @@ -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
}
}
```
Expand Down
35 changes: 11 additions & 24 deletions solution/2200-2299/2278.Percentage of Letter in String/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ The percentage of characters in s that equal the letter &#39;k&#39; is 0%, so we

<!-- solution:start -->

### 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)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -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();
}
};
```
Expand All @@ -98,26 +100,16 @@ 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)
}
```

#### TypeScript

```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);
}
```

Expand All @@ -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
}
}
```
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
};
};
Original file line number Diff line number Diff line change
@@ -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)
}
return strings.Count(s, string(letter)) * 100 / len(s)
}
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ tags:

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

我们首先将每个背包的剩余容量计算出来,然后对剩余容量进行排序,接着我们从小到大遍历剩余容量,将额外的石头放入背包中,直到额外的石头用完或者背包的剩余容量用完为止,返回此时的背包数量即可。

时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为背包的数量。

<!-- tabs:start -->

#### Python3
Expand All @@ -83,37 +87,33 @@ 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

```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;
}
}
```
Expand All @@ -124,17 +124,18 @@ class Solution {
class Solution {
public:
int maximumBags(vector<int>& capacity, vector<int>& rocks, int additionalRocks) {
int n = capacity.size();
vector<int> 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;
}
};
```
Expand All @@ -143,58 +144,55 @@ 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)
}
```

#### TypeScript

```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;
}
```

#### Rust

```rust
impl Solution {
pub fn maximum_bags(capacity: Vec<i32>, rocks: Vec<i32>, 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<i32>, rocks: Vec<i32>, 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
}
}
```
Expand Down
Loading
Loading