Skip to content

feat: add solutions to lc problems: No.2012,2598 #4111

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
Feb 26, 2025
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
27 changes: 27 additions & 0 deletions solution/2000-2099/2012.Sum of Beauty in the Array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,33 @@ function sumOfBeauties(nums: number[]): number {
}
```

#### Rust

```rust
impl Solution {
pub fn sum_of_beauties(nums: Vec<i32>) -> i32 {
let n = nums.len();
let mut right: Vec<i32> = vec![0; n];
right[n - 1] = nums[n - 1];
for i in (1..n - 1).rev() {
right[i] = right[i + 1].min(nums[i]);
}
let mut ans = 0;
let mut l = nums[0];
for i in 1..n - 1 {
let r = right[i + 1];
if l < nums[i] && nums[i] < r {
ans += 2;
} else if nums[i - 1] < nums[i] && nums[i] < nums[i + 1] {
ans += 1;
}
l = l.max(nums[i]);
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
27 changes: 27 additions & 0 deletions solution/2000-2099/2012.Sum of Beauty in the Array/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,33 @@ function sumOfBeauties(nums: number[]): number {
}
```

#### Rust

```rust
impl Solution {
pub fn sum_of_beauties(nums: Vec<i32>) -> i32 {
let n = nums.len();
let mut right: Vec<i32> = vec![0; n];
right[n - 1] = nums[n - 1];
for i in (1..n - 1).rev() {
right[i] = right[i + 1].min(nums[i]);
}
let mut ans = 0;
let mut l = nums[0];
for i in 1..n - 1 {
let r = right[i + 1];
if l < nums[i] && nums[i] < r {
ans += 2;
} else if nums[i - 1] < nums[i] && nums[i] < nums[i + 1] {
ans += 1;
}
l = l.max(nums[i]);
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
22 changes: 22 additions & 0 deletions solution/2000-2099/2012.Sum of Beauty in the Array/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
impl Solution {
pub fn sum_of_beauties(nums: Vec<i32>) -> i32 {
let n = nums.len();
let mut right: Vec<i32> = vec![0; n];
right[n - 1] = nums[n - 1];
for i in (1..n - 1).rev() {
right[i] = right[i + 1].min(nums[i]);
}
let mut ans = 0;
let mut l = nums[0];
for i in 1..n - 1 {
let r = right[i + 1];
if l < nums[i] && nums[i] < r {
ans += 2;
} else if nums[i - 1] < nums[i] && nums[i] < nums[i + 1] {
ans += 1;
}
l = l.max(nums[i]);
}
ans
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ class Solution {
int mask = 0;
for (int x : nums) {
mask ^= x;
ans += cnt.getOrDefault(mask, 0);
cnt.merge(mask, 1, Integer::sum);
ans += cnt.merge(mask, 1, Integer::sum) - 1;
}
return ans;
}
Expand All @@ -133,8 +132,7 @@ public:
int mask = 0;
for (int x : nums) {
mask ^= x;
ans += cnt[mask];
++cnt[mask];
ans += cnt[mask]++;
}
return ans;
}
Expand Down Expand Up @@ -173,6 +171,27 @@ function beautifulSubarrays(nums: number[]): number {
}
```

#### Rust

```rust
use std::collections::HashMap;

impl Solution {
pub fn beautiful_subarrays(nums: Vec<i32>) -> i64 {
let mut cnt = HashMap::new();
cnt.insert(0, 1);
let mut ans = 0;
let mut mask = 0;
for &x in nums.iter() {
mask ^= x;
ans += *cnt.get(&mask).unwrap_or(&0);
*cnt.entry(mask).or_insert(0) += 1;
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ class Solution {
int mask = 0;
for (int x : nums) {
mask ^= x;
ans += cnt.getOrDefault(mask, 0);
cnt.merge(mask, 1, Integer::sum);
ans += cnt.merge(mask, 1, Integer::sum) - 1;
}
return ans;
}
Expand All @@ -131,8 +130,7 @@ public:
int mask = 0;
for (int x : nums) {
mask ^= x;
ans += cnt[mask];
++cnt[mask];
ans += cnt[mask]++;
}
return ans;
}
Expand Down Expand Up @@ -171,6 +169,27 @@ function beautifulSubarrays(nums: number[]): number {
}
```

#### Rust

```rust
use std::collections::HashMap;

impl Solution {
pub fn beautiful_subarrays(nums: Vec<i32>) -> i64 {
let mut cnt = HashMap::new();
cnt.insert(0, 1);
let mut ans = 0;
let mut mask = 0;
for &x in nums.iter() {
mask ^= x;
ans += *cnt.get(&mask).unwrap_or(&0);
*cnt.entry(mask).or_insert(0) += 1;
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ class Solution {
int mask = 0;
for (int x : nums) {
mask ^= x;
ans += cnt[mask];
++cnt[mask];
ans += cnt[mask]++;
}
return ans;
}
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ public long beautifulSubarrays(int[] nums) {
int mask = 0;
for (int x : nums) {
mask ^= x;
ans += cnt.getOrDefault(mask, 0);
cnt.merge(mask, 1, Integer::sum);
ans += cnt.merge(mask, 1, Integer::sum) - 1;
}
return ans;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use std::collections::HashMap;

impl Solution {
pub fn beautiful_subarrays(nums: Vec<i32>) -> i64 {
let mut cnt = HashMap::new();
cnt.insert(0, 1);
let mut ans = 0;
let mut mask = 0;
for &x in nums.iter() {
mask ^= x;
ans += *cnt.get(&mask).unwrap_or(&0);
*cnt.entry(mask).or_insert(0) += 1;
}
ans
}
}