From 012bdd166281594c3e36b506df262e9b872a0f89 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 1 Nov 2023 14:53:42 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.2586,2587 * No.2586.Count the Number of Vowel Strings in Range * No.2587.Rearrange Array to Maximize Prefix Score --- .../README.md | 10 +++---- .../README_EN.md | 10 +++---- .../Solution.rs | 6 ++--- .../Solution.ts | 4 +-- .../README.md | 22 ++++++++++++++-- .../README_EN.md | 26 +++++++++++++++++++ .../Solution.rs | 13 ++++++++++ 7 files changed, 71 insertions(+), 20 deletions(-) create mode 100644 solution/2500-2599/2587.Rearrange Array to Maximize Prefix Score/Solution.rs diff --git a/solution/2500-2599/2586.Count the Number of Vowel Strings in Range/README.md b/solution/2500-2599/2586.Count the Number of Vowel Strings in Range/README.md index 4c4e0275fa815..eee5fbf10ea47 100644 --- a/solution/2500-2599/2586.Count the Number of Vowel Strings in Range/README.md +++ b/solution/2500-2599/2586.Count the Number of Vowel Strings in Range/README.md @@ -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; } } @@ -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; } } diff --git a/solution/2500-2599/2586.Count the Number of Vowel Strings in Range/README_EN.md b/solution/2500-2599/2586.Count the Number of Vowel Strings in Range/README_EN.md index e1424394396f2..bae786e9c1c1f 100644 --- a/solution/2500-2599/2586.Count the Number of Vowel Strings in Range/README_EN.md +++ b/solution/2500-2599/2586.Count the Number of Vowel Strings in Range/README_EN.md @@ -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; } } @@ -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; } } diff --git a/solution/2500-2599/2586.Count the Number of Vowel Strings in Range/Solution.rs b/solution/2500-2599/2586.Count the Number of Vowel Strings in Range/Solution.rs index f8d6735e9b4ef..84801564c6704 100644 --- a/solution/2500-2599/2586.Count the Number of Vowel Strings in Range/Solution.rs +++ b/solution/2500-2599/2586.Count the Number of Vowel Strings in Range/Solution.rs @@ -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; } } diff --git a/solution/2500-2599/2586.Count the Number of Vowel Strings in Range/Solution.ts b/solution/2500-2599/2586.Count the Number of Vowel Strings in Range/Solution.ts index f13bed0ffaeb5..fa28db89017a2 100644 --- a/solution/2500-2599/2586.Count the Number of Vowel Strings in Range/Solution.ts +++ b/solution/2500-2599/2586.Count the Number of Vowel Strings in Range/Solution.ts @@ -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; } } diff --git a/solution/2500-2599/2587.Rearrange Array to Maximize Prefix Score/README.md b/solution/2500-2599/2587.Rearrange Array to Maximize Prefix Score/README.md index b6a6dff47817c..7f25bc0a5680f 100644 --- a/solution/2500-2599/2587.Rearrange Array to Maximize Prefix Score/README.md +++ b/solution/2500-2599/2587.Rearrange Array to Maximize Prefix Score/README.md @@ -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$ 的长度。 @@ -127,6 +127,24 @@ func maxScore(nums []int) int { } ``` +### **Rust** + +```rust +impl Solution { + pub fn max_score(mut nums: Vec) -> 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 diff --git a/solution/2500-2599/2587.Rearrange Array to Maximize Prefix Score/README_EN.md b/solution/2500-2599/2587.Rearrange Array to Maximize Prefix Score/README_EN.md index c95da2f03d431..d672fe1c56784 100644 --- a/solution/2500-2599/2587.Rearrange Array to Maximize Prefix Score/README_EN.md +++ b/solution/2500-2599/2587.Rearrange Array to Maximize Prefix Score/README_EN.md @@ -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$. + ### **Python3** @@ -111,6 +119,24 @@ func maxScore(nums []int) int { } ``` +### **Rust** + +```rust +impl Solution { + pub fn max_score(mut nums: Vec) -> 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 diff --git a/solution/2500-2599/2587.Rearrange Array to Maximize Prefix Score/Solution.rs b/solution/2500-2599/2587.Rearrange Array to Maximize Prefix Score/Solution.rs new file mode 100644 index 0000000000000..02a714069ab73 --- /dev/null +++ b/solution/2500-2599/2587.Rearrange Array to Maximize Prefix Score/Solution.rs @@ -0,0 +1,13 @@ +impl Solution { + pub fn max_score(mut nums: Vec) -> 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 + } +} \ No newline at end of file