Skip to content

Commit 42d4a22

Browse files
authored
feat: add solutions to lc problem: No.1493 (#4661)
No.1493.Longest Subarray of 1's After Deleting One Element
1 parent ff2df3f commit 42d4a22

File tree

8 files changed

+225
-12
lines changed

8 files changed

+225
-12
lines changed

solution/1400-1499/1493.Longest Subarray of 1's After Deleting One Element/README.md

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,37 @@ function longestSubarray(nums: number[]): number {
197197
}
198198
```
199199

200+
#### Rust
201+
202+
```rust
203+
impl Solution {
204+
pub fn longest_subarray(nums: Vec<i32>) -> i32 {
205+
let n = nums.len();
206+
let mut left = vec![0; n + 1];
207+
let mut right = vec![0; n + 1];
208+
209+
for i in 1..=n {
210+
if nums[i - 1] == 1 {
211+
left[i] = left[i - 1] + 1;
212+
}
213+
}
214+
215+
for i in (0..n).rev() {
216+
if nums[i] == 1 {
217+
right[i] = right[i + 1] + 1;
218+
}
219+
}
220+
221+
let mut ans = 0;
222+
for i in 0..n {
223+
ans = ans.max(left[i] + right[i + 1]);
224+
}
225+
226+
ans as i32
227+
}
228+
}
229+
```
230+
200231
<!-- tabs:end -->
201232

202233
<!-- solution:end -->
@@ -300,6 +331,30 @@ function longestSubarray(nums: number[]): number {
300331
}
301332
```
302333

334+
#### Rust
335+
336+
```rust
337+
impl Solution {
338+
pub fn longest_subarray(nums: Vec<i32>) -> i32 {
339+
let n = nums.len();
340+
let mut ans = 0;
341+
let mut j = 0;
342+
let mut cnt = 0;
343+
344+
for i in 0..n {
345+
cnt += nums[i] ^ 1;
346+
while cnt > 1 {
347+
cnt -= nums[j] ^ 1;
348+
j += 1;
349+
}
350+
ans = ans.max(i - j);
351+
}
352+
353+
ans as i32
354+
}
355+
}
356+
```
357+
303358
<!-- tabs:end -->
304359

305360
<!-- solution:end -->
@@ -335,7 +390,7 @@ class Solution:
335390
```java
336391
class Solution {
337392
public int longestSubarray(int[] nums) {
338-
int ans = 0, cnt = 0, l = 0;
393+
int cnt = 0, l = 0;
339394
for (int x : nums) {
340395
cnt += x ^ 1;
341396
if (cnt > 1) {
@@ -353,7 +408,7 @@ class Solution {
353408
class Solution {
354409
public:
355410
int longestSubarray(vector<int>& nums) {
356-
int ans = 0, cnt = 0, l = 0;
411+
int cnt = 0, l = 0;
357412
for (int x : nums) {
358413
cnt += x ^ 1;
359414
if (cnt > 1) {
@@ -368,7 +423,7 @@ public:
368423
#### Go
369424
370425
```go
371-
func longestSubarray(nums []int) (ans int) {
426+
func longestSubarray(nums []int) int {
372427
cnt, l := 0, 0
373428
for _, x := range nums {
374429
cnt += x ^ 1
@@ -396,6 +451,27 @@ function longestSubarray(nums: number[]): number {
396451
}
397452
```
398453

454+
#### Rust
455+
456+
```rust
457+
impl Solution {
458+
pub fn longest_subarray(nums: Vec<i32>) -> i32 {
459+
let mut cnt = 0;
460+
let mut l = 0;
461+
462+
for &x in &nums {
463+
cnt += x ^ 1;
464+
if cnt > 1 {
465+
cnt -= nums[l] ^ 1;
466+
l += 1;
467+
}
468+
}
469+
470+
(nums.len() - l - 1) as i32
471+
}
472+
}
473+
```
474+
399475
<!-- tabs:end -->
400476

401477
<!-- solution:end -->

solution/1400-1499/1493.Longest Subarray of 1's After Deleting One Element/README_EN.md

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,37 @@ function longestSubarray(nums: number[]): number {
196196
}
197197
```
198198

199+
#### Rust
200+
201+
```rust
202+
impl Solution {
203+
pub fn longest_subarray(nums: Vec<i32>) -> i32 {
204+
let n = nums.len();
205+
let mut left = vec![0; n + 1];
206+
let mut right = vec![0; n + 1];
207+
208+
for i in 1..=n {
209+
if nums[i - 1] == 1 {
210+
left[i] = left[i - 1] + 1;
211+
}
212+
}
213+
214+
for i in (0..n).rev() {
215+
if nums[i] == 1 {
216+
right[i] = right[i + 1] + 1;
217+
}
218+
}
219+
220+
let mut ans = 0;
221+
for i in 0..n {
222+
ans = ans.max(left[i] + right[i + 1]);
223+
}
224+
225+
ans as i32
226+
}
227+
}
228+
```
229+
199230
<!-- tabs:end -->
200231

201232
<!-- solution:end -->
@@ -299,6 +330,30 @@ function longestSubarray(nums: number[]): number {
299330
}
300331
```
301332

333+
#### Rust
334+
335+
```rust
336+
impl Solution {
337+
pub fn longest_subarray(nums: Vec<i32>) -> i32 {
338+
let n = nums.len();
339+
let mut ans = 0;
340+
let mut j = 0;
341+
let mut cnt = 0;
342+
343+
for i in 0..n {
344+
cnt += nums[i] ^ 1;
345+
while cnt > 1 {
346+
cnt -= nums[j] ^ 1;
347+
j += 1;
348+
}
349+
ans = ans.max(i - j);
350+
}
351+
352+
ans as i32
353+
}
354+
}
355+
```
356+
302357
<!-- tabs:end -->
303358

304359
<!-- solution:end -->
@@ -334,7 +389,7 @@ class Solution:
334389
```java
335390
class Solution {
336391
public int longestSubarray(int[] nums) {
337-
int ans = 0, cnt = 0, l = 0;
392+
int cnt = 0, l = 0;
338393
for (int x : nums) {
339394
cnt += x ^ 1;
340395
if (cnt > 1) {
@@ -352,7 +407,7 @@ class Solution {
352407
class Solution {
353408
public:
354409
int longestSubarray(vector<int>& nums) {
355-
int ans = 0, cnt = 0, l = 0;
410+
int cnt = 0, l = 0;
356411
for (int x : nums) {
357412
cnt += x ^ 1;
358413
if (cnt > 1) {
@@ -367,7 +422,7 @@ public:
367422
#### Go
368423
369424
```go
370-
func longestSubarray(nums []int) (ans int) {
425+
func longestSubarray(nums []int) int {
371426
cnt, l := 0, 0
372427
for _, x := range nums {
373428
cnt += x ^ 1
@@ -395,6 +450,27 @@ function longestSubarray(nums: number[]): number {
395450
}
396451
```
397452

453+
#### Rust
454+
455+
```rust
456+
impl Solution {
457+
pub fn longest_subarray(nums: Vec<i32>) -> i32 {
458+
let mut cnt = 0;
459+
let mut l = 0;
460+
461+
for &x in &nums {
462+
cnt += x ^ 1;
463+
if cnt > 1 {
464+
cnt -= nums[l] ^ 1;
465+
l += 1;
466+
}
467+
}
468+
469+
(nums.len() - l - 1) as i32
470+
}
471+
}
472+
```
473+
398474
<!-- tabs:end -->
399475

400476
<!-- solution:end -->
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
impl Solution {
2+
pub fn longest_subarray(nums: Vec<i32>) -> i32 {
3+
let n = nums.len();
4+
let mut left = vec![0; n + 1];
5+
let mut right = vec![0; n + 1];
6+
7+
for i in 1..=n {
8+
if nums[i - 1] == 1 {
9+
left[i] = left[i - 1] + 1;
10+
}
11+
}
12+
13+
for i in (0..n).rev() {
14+
if nums[i] == 1 {
15+
right[i] = right[i + 1] + 1;
16+
}
17+
}
18+
19+
let mut ans = 0;
20+
for i in 0..n {
21+
ans = ans.max(left[i] + right[i + 1]);
22+
}
23+
24+
ans as i32
25+
}
26+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
impl Solution {
2+
pub fn longest_subarray(nums: Vec<i32>) -> i32 {
3+
let n = nums.len();
4+
let mut ans = 0;
5+
let mut j = 0;
6+
let mut cnt = 0;
7+
8+
for i in 0..n {
9+
cnt += nums[i] ^ 1;
10+
while cnt > 1 {
11+
cnt -= nums[j] ^ 1;
12+
j += 1;
13+
}
14+
ans = ans.max(i - j);
15+
}
16+
17+
ans as i32
18+
}
19+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Solution {
22
public:
33
int longestSubarray(vector<int>& nums) {
4-
int ans = 0, cnt = 0, l = 0;
4+
int cnt = 0, l = 0;
55
for (int x : nums) {
66
cnt += x ^ 1;
77
if (cnt > 1) {
@@ -10,4 +10,4 @@ class Solution {
1010
}
1111
return nums.size() - l - 1;
1212
}
13-
};
13+
};
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
func longestSubarray(nums []int) (ans int) {
1+
func longestSubarray(nums []int) int {
22
cnt, l := 0, 0
33
for _, x := range nums {
44
cnt += x ^ 1
@@ -8,4 +8,4 @@ func longestSubarray(nums []int) (ans int) {
88
}
99
}
1010
return len(nums) - l - 1
11-
}
11+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Solution {
22
public int longestSubarray(int[] nums) {
3-
int ans = 0, cnt = 0, l = 0;
3+
int cnt = 0, l = 0;
44
for (int x : nums) {
55
cnt += x ^ 1;
66
if (cnt > 1) {
@@ -9,4 +9,4 @@ public int longestSubarray(int[] nums) {
99
}
1010
return nums.length - l - 1;
1111
}
12-
}
12+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
impl Solution {
2+
pub fn longest_subarray(nums: Vec<i32>) -> i32 {
3+
let mut cnt = 0;
4+
let mut l = 0;
5+
6+
for &x in &nums {
7+
cnt += x ^ 1;
8+
if cnt > 1 {
9+
cnt -= nums[l] ^ 1;
10+
l += 1;
11+
}
12+
}
13+
14+
(nums.len() - l - 1) as i32
15+
}
16+
}

0 commit comments

Comments
 (0)