Skip to content

Commit 1f2eb6c

Browse files
authored
feat: add solutions to lc problem: No.2586,2587 (doocs#1911)
* No.2586.Count the Number of Vowel Strings in Range * No.2587.Rearrange Array to Maximize Prefix Score
1 parent 66b25b9 commit 1f2eb6c

File tree

7 files changed

+71
-20
lines changed

7 files changed

+71
-20
lines changed

solution/2500-2599/2586.Count the Number of Vowel Strings in Range/README.md

+4-6
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ function vowelStrings(words: string[], left: number, right: number): number {
141141
let ans = 0;
142142
const check: string[] = ['a', 'e', 'i', 'o', 'u'];
143143
for (let i = left; i <= right; ++i) {
144-
var w = words[i];
145-
if (check.includes(w[0]) && check.includes(w[w.length - 1])) {
144+
const w = words[i];
145+
if (check.includes(w[0]) && check.includes(w.at(-1))) {
146146
++ans;
147147
}
148148
}
@@ -161,10 +161,8 @@ impl Solution {
161161

162162
let mut ans = 0;
163163
for i in left..=right {
164-
let words_bytes = words[i as usize].as_bytes();
165-
let first_char = words_bytes[0];
166-
let last_char = words_bytes[words_bytes.len() - 1];
167-
if check(first_char) && check(last_char) {
164+
let w = words[i as usize].as_bytes();
165+
if check(w[0]) && check(w[w.len() - 1]) {
168166
ans += 1;
169167
}
170168
}

solution/2500-2599/2586.Count the Number of Vowel Strings in Range/README_EN.md

+4-6
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ function vowelStrings(words: string[], left: number, right: number): number {
131131
let ans = 0;
132132
const check: string[] = ['a', 'e', 'i', 'o', 'u'];
133133
for (let i = left; i <= right; ++i) {
134-
var w = words[i];
135-
if (check.includes(w[0]) && check.includes(w[w.length - 1])) {
134+
const w = words[i];
135+
if (check.includes(w[0]) && check.includes(w.at(-1))) {
136136
++ans;
137137
}
138138
}
@@ -151,10 +151,8 @@ impl Solution {
151151

152152
let mut ans = 0;
153153
for i in left..=right {
154-
let words_bytes = words[i as usize].as_bytes();
155-
let first_char = words_bytes[0];
156-
let last_char = words_bytes[words_bytes.len() - 1];
157-
if check(first_char) && check(last_char) {
154+
let w = words[i as usize].as_bytes();
155+
if check(w[0]) && check(w[w.len() - 1]) {
158156
ans += 1;
159157
}
160158
}

solution/2500-2599/2586.Count the Number of Vowel Strings in Range/Solution.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ impl Solution {
66

77
let mut ans = 0;
88
for i in left..=right {
9-
let words_bytes = words[i as usize].as_bytes();
10-
let first_char = words_bytes[0];
11-
let last_char = words_bytes[words_bytes.len() - 1];
12-
if check(first_char) && check(last_char) {
9+
let w = words[i as usize].as_bytes();
10+
if check(w[0]) && check(w[w.len() - 1]) {
1311
ans += 1;
1412
}
1513
}

solution/2500-2599/2586.Count the Number of Vowel Strings in Range/Solution.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ function vowelStrings(words: string[], left: number, right: number): number {
22
let ans = 0;
33
const check: string[] = ['a', 'e', 'i', 'o', 'u'];
44
for (let i = left; i <= right; ++i) {
5-
var w = words[i];
6-
if (check.includes(w[0]) && check.includes(w[w.length - 1])) {
5+
const w = words[i];
6+
if (check.includes(w[0]) && check.includes(w.at(-1))) {
77
++ans;
88
}
99
}

solution/2500-2599/2587.Rearrange Array to Maximize Prefix Score/README.md

+20-2
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ prefix = [2,5,6,5,2,2,-1] ,分数为 6 。
4545

4646
**方法一:贪心 + 排序**
4747

48-
要使得前缀和数组中正整数的个数最多,就要使得前缀和数组中的元素尽可能大,即尽可能多的正整数相加。因此,我们可以将数组 `nums` 降序排序,然后遍历数组,维护前缀和 $s$,如果 $s \leq 0$,则说明当前位置以及之后的位置都不可能再有正整数,因此直接返回当前位置即可。
48+
要使得前缀和数组中正整数的个数最多,就要使得前缀和数组中的元素尽可能大,即尽可能多的正整数相加。因此,我们可以将数组 $nums$ 降序排序,然后遍历数组,维护前缀和 $s$,如果 $s \leq 0$,则说明当前位置以及之后的位置都不可能再有正整数,因此直接返回当前位置即可。
4949

5050
否则,遍历结束后,返回数组长度。
5151

52-
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组 `nums` 的长度。
52+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组 $nums$ 的长度。
5353

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

@@ -127,6 +127,24 @@ func maxScore(nums []int) int {
127127
}
128128
```
129129

130+
### **Rust**
131+
132+
```rust
133+
impl Solution {
134+
pub fn max_score(mut nums: Vec<i32>) -> i32 {
135+
nums.sort_by(|a, b| b.cmp(a));
136+
let mut s: i64 = 0;
137+
for (i, &x) in nums.iter().enumerate() {
138+
s += x as i64;
139+
if s <= 0 {
140+
return i as i32;
141+
}
142+
}
143+
nums.len() as i32
144+
}
145+
}
146+
```
147+
130148
### **TypeScript**
131149

132150
```ts

solution/2500-2599/2587.Rearrange Array to Maximize Prefix Score/README_EN.md

+26
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ It can be shown that 6 is the maximum score we can obtain.
3939

4040
## Solutions
4141

42+
**Solution 1: Greedy + Sorting**
43+
44+
To maximize the number of positive integers in the prefix sum array, we need to make the elements in the prefix sum array as large as possible, that is, to add as many positive integers as possible. Therefore, we can sort the array $nums$ in descending order, then traverse the array, maintaining the prefix sum $s$. If $s \leq 0$, it means that there can be no more positive integers in the current position and the positions after it, so we can directly return the current position.
45+
46+
Otherwise, after the traversal, we return the length of the array.
47+
48+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the length of the array $nums$.
49+
4250
<!-- tabs:start -->
4351

4452
### **Python3**
@@ -111,6 +119,24 @@ func maxScore(nums []int) int {
111119
}
112120
```
113121

122+
### **Rust**
123+
124+
```rust
125+
impl Solution {
126+
pub fn max_score(mut nums: Vec<i32>) -> i32 {
127+
nums.sort_by(|a, b| b.cmp(a));
128+
let mut s: i64 = 0;
129+
for (i, &x) in nums.iter().enumerate() {
130+
s += x as i64;
131+
if s <= 0 {
132+
return i as i32;
133+
}
134+
}
135+
nums.len() as i32
136+
}
137+
}
138+
```
139+
114140
### **TypeScript**
115141

116142
```ts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
impl Solution {
2+
pub fn max_score(mut nums: Vec<i32>) -> i32 {
3+
nums.sort_by(|a, b| b.cmp(a));
4+
let mut s: i64 = 0;
5+
for (i, &x) in nums.iter().enumerate() {
6+
s += x as i64;
7+
if s <= 0 {
8+
return i as i32;
9+
}
10+
}
11+
nums.len() as i32
12+
}
13+
}

0 commit comments

Comments
 (0)