Skip to content
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

feat: add solutions to lc problem: No.2586,2587 #1911

Merged
merged 1 commit into from
Nov 1, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ function vowelStrings(words: string[], left: number, right: number): number {
let ans = 0;
const check: string[] = ['a', 'e', 'i', 'o', 'u'];
for (let i = left; i <= right; ++i) {
var w = words[i];
if (check.includes(w[0]) && check.includes(w[w.length - 1])) {
const w = words[i];
if (check.includes(w[0]) && check.includes(w.at(-1))) {
++ans;
}
}
Expand All @@ -161,10 +161,8 @@ impl Solution {

let mut ans = 0;
for i in left..=right {
let words_bytes = words[i as usize].as_bytes();
let first_char = words_bytes[0];
let last_char = words_bytes[words_bytes.len() - 1];
if check(first_char) && check(last_char) {
let w = words[i as usize].as_bytes();
if check(w[0]) && check(w[w.len() - 1]) {
ans += 1;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ function vowelStrings(words: string[], left: number, right: number): number {
let ans = 0;
const check: string[] = ['a', 'e', 'i', 'o', 'u'];
for (let i = left; i <= right; ++i) {
var w = words[i];
if (check.includes(w[0]) && check.includes(w[w.length - 1])) {
const w = words[i];
if (check.includes(w[0]) && check.includes(w.at(-1))) {
++ans;
}
}
Expand All @@ -151,10 +151,8 @@ impl Solution {

let mut ans = 0;
for i in left..=right {
let words_bytes = words[i as usize].as_bytes();
let first_char = words_bytes[0];
let last_char = words_bytes[words_bytes.len() - 1];
if check(first_char) && check(last_char) {
let w = words[i as usize].as_bytes();
if check(w[0]) && check(w[w.len() - 1]) {
ans += 1;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ impl Solution {

let mut ans = 0;
for i in left..=right {
let words_bytes = words[i as usize].as_bytes();
let first_char = words_bytes[0];
let last_char = words_bytes[words_bytes.len() - 1];
if check(first_char) && check(last_char) {
let w = words[i as usize].as_bytes();
if check(w[0]) && check(w[w.len() - 1]) {
ans += 1;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ function vowelStrings(words: string[], left: number, right: number): number {
let ans = 0;
const check: string[] = ['a', 'e', 'i', 'o', 'u'];
for (let i = left; i <= right; ++i) {
var w = words[i];
if (check.includes(w[0]) && check.includes(w[w.length - 1])) {
const w = words[i];
if (check.includes(w[0]) && check.includes(w.at(-1))) {
++ans;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ prefix = [2,5,6,5,2,2,-1] ,分数为 6 。

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

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

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

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

<!-- tabs:start -->

Expand Down Expand Up @@ -127,6 +127,24 @@ func maxScore(nums []int) int {
}
```

### **Rust**

```rust
impl Solution {
pub fn max_score(mut nums: Vec<i32>) -> i32 {
nums.sort_by(|a, b| b.cmp(a));
let mut s: i64 = 0;
for (i, &x) in nums.iter().enumerate() {
s += x as i64;
if s <= 0 {
return i as i32;
}
}
nums.len() as i32
}
}
```

### **TypeScript**

```ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ It can be shown that 6 is the maximum score we can obtain.

## Solutions

**Solution 1: Greedy + Sorting**

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.

Otherwise, after the traversal, we return the length of the array.

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

<!-- tabs:start -->

### **Python3**
Expand Down Expand Up @@ -111,6 +119,24 @@ func maxScore(nums []int) int {
}
```

### **Rust**

```rust
impl Solution {
pub fn max_score(mut nums: Vec<i32>) -> i32 {
nums.sort_by(|a, b| b.cmp(a));
let mut s: i64 = 0;
for (i, &x) in nums.iter().enumerate() {
s += x as i64;
if s <= 0 {
return i as i32;
}
}
nums.len() as i32
}
}
```

### **TypeScript**

```ts
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
impl Solution {
pub fn max_score(mut nums: Vec<i32>) -> i32 {
nums.sort_by(|a, b| b.cmp(a));
let mut s: i64 = 0;
for (i, &x) in nums.iter().enumerate() {
s += x as i64;
if s <= 0 {
return i as i32;
}
}
nums.len() as i32
}
}