diff --git a/solution/1400-1499/1493.Longest Subarray of 1's After Deleting One Element/README.md b/solution/1400-1499/1493.Longest Subarray of 1's After Deleting One Element/README.md index aa6ba42c903f1..452873378bfca 100644 --- a/solution/1400-1499/1493.Longest Subarray of 1's After Deleting One Element/README.md +++ b/solution/1400-1499/1493.Longest Subarray of 1's After Deleting One Element/README.md @@ -197,6 +197,37 @@ function longestSubarray(nums: number[]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn longest_subarray(nums: Vec) -> i32 { + let n = nums.len(); + let mut left = vec![0; n + 1]; + let mut right = vec![0; n + 1]; + + for i in 1..=n { + if nums[i - 1] == 1 { + left[i] = left[i - 1] + 1; + } + } + + for i in (0..n).rev() { + if nums[i] == 1 { + right[i] = right[i + 1] + 1; + } + } + + let mut ans = 0; + for i in 0..n { + ans = ans.max(left[i] + right[i + 1]); + } + + ans as i32 + } +} +``` + @@ -300,6 +331,30 @@ function longestSubarray(nums: number[]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn longest_subarray(nums: Vec) -> i32 { + let n = nums.len(); + let mut ans = 0; + let mut j = 0; + let mut cnt = 0; + + for i in 0..n { + cnt += nums[i] ^ 1; + while cnt > 1 { + cnt -= nums[j] ^ 1; + j += 1; + } + ans = ans.max(i - j); + } + + ans as i32 + } +} +``` + @@ -335,7 +390,7 @@ class Solution: ```java class Solution { public int longestSubarray(int[] nums) { - int ans = 0, cnt = 0, l = 0; + int cnt = 0, l = 0; for (int x : nums) { cnt += x ^ 1; if (cnt > 1) { @@ -353,7 +408,7 @@ class Solution { class Solution { public: int longestSubarray(vector& nums) { - int ans = 0, cnt = 0, l = 0; + int cnt = 0, l = 0; for (int x : nums) { cnt += x ^ 1; if (cnt > 1) { @@ -368,7 +423,7 @@ public: #### Go ```go -func longestSubarray(nums []int) (ans int) { +func longestSubarray(nums []int) int { cnt, l := 0, 0 for _, x := range nums { cnt += x ^ 1 @@ -396,6 +451,27 @@ function longestSubarray(nums: number[]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn longest_subarray(nums: Vec) -> i32 { + let mut cnt = 0; + let mut l = 0; + + for &x in &nums { + cnt += x ^ 1; + if cnt > 1 { + cnt -= nums[l] ^ 1; + l += 1; + } + } + + (nums.len() - l - 1) as i32 + } +} +``` + diff --git a/solution/1400-1499/1493.Longest Subarray of 1's After Deleting One Element/README_EN.md b/solution/1400-1499/1493.Longest Subarray of 1's After Deleting One Element/README_EN.md index c539187f88cc9..119cd0e2f47c1 100644 --- a/solution/1400-1499/1493.Longest Subarray of 1's After Deleting One Element/README_EN.md +++ b/solution/1400-1499/1493.Longest Subarray of 1's After Deleting One Element/README_EN.md @@ -196,6 +196,37 @@ function longestSubarray(nums: number[]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn longest_subarray(nums: Vec) -> i32 { + let n = nums.len(); + let mut left = vec![0; n + 1]; + let mut right = vec![0; n + 1]; + + for i in 1..=n { + if nums[i - 1] == 1 { + left[i] = left[i - 1] + 1; + } + } + + for i in (0..n).rev() { + if nums[i] == 1 { + right[i] = right[i + 1] + 1; + } + } + + let mut ans = 0; + for i in 0..n { + ans = ans.max(left[i] + right[i + 1]); + } + + ans as i32 + } +} +``` + @@ -299,6 +330,30 @@ function longestSubarray(nums: number[]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn longest_subarray(nums: Vec) -> i32 { + let n = nums.len(); + let mut ans = 0; + let mut j = 0; + let mut cnt = 0; + + for i in 0..n { + cnt += nums[i] ^ 1; + while cnt > 1 { + cnt -= nums[j] ^ 1; + j += 1; + } + ans = ans.max(i - j); + } + + ans as i32 + } +} +``` + @@ -334,7 +389,7 @@ class Solution: ```java class Solution { public int longestSubarray(int[] nums) { - int ans = 0, cnt = 0, l = 0; + int cnt = 0, l = 0; for (int x : nums) { cnt += x ^ 1; if (cnt > 1) { @@ -352,7 +407,7 @@ class Solution { class Solution { public: int longestSubarray(vector& nums) { - int ans = 0, cnt = 0, l = 0; + int cnt = 0, l = 0; for (int x : nums) { cnt += x ^ 1; if (cnt > 1) { @@ -367,7 +422,7 @@ public: #### Go ```go -func longestSubarray(nums []int) (ans int) { +func longestSubarray(nums []int) int { cnt, l := 0, 0 for _, x := range nums { cnt += x ^ 1 @@ -395,6 +450,27 @@ function longestSubarray(nums: number[]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn longest_subarray(nums: Vec) -> i32 { + let mut cnt = 0; + let mut l = 0; + + for &x in &nums { + cnt += x ^ 1; + if cnt > 1 { + cnt -= nums[l] ^ 1; + l += 1; + } + } + + (nums.len() - l - 1) as i32 + } +} +``` + diff --git a/solution/1400-1499/1493.Longest Subarray of 1's After Deleting One Element/Solution.rs b/solution/1400-1499/1493.Longest Subarray of 1's After Deleting One Element/Solution.rs new file mode 100644 index 0000000000000..ec6f9604321fa --- /dev/null +++ b/solution/1400-1499/1493.Longest Subarray of 1's After Deleting One Element/Solution.rs @@ -0,0 +1,26 @@ +impl Solution { + pub fn longest_subarray(nums: Vec) -> i32 { + let n = nums.len(); + let mut left = vec![0; n + 1]; + let mut right = vec![0; n + 1]; + + for i in 1..=n { + if nums[i - 1] == 1 { + left[i] = left[i - 1] + 1; + } + } + + for i in (0..n).rev() { + if nums[i] == 1 { + right[i] = right[i + 1] + 1; + } + } + + let mut ans = 0; + for i in 0..n { + ans = ans.max(left[i] + right[i + 1]); + } + + ans as i32 + } +} diff --git a/solution/1400-1499/1493.Longest Subarray of 1's After Deleting One Element/Solution2.rs b/solution/1400-1499/1493.Longest Subarray of 1's After Deleting One Element/Solution2.rs new file mode 100644 index 0000000000000..81cd18f612413 --- /dev/null +++ b/solution/1400-1499/1493.Longest Subarray of 1's After Deleting One Element/Solution2.rs @@ -0,0 +1,19 @@ +impl Solution { + pub fn longest_subarray(nums: Vec) -> i32 { + let n = nums.len(); + let mut ans = 0; + let mut j = 0; + let mut cnt = 0; + + for i in 0..n { + cnt += nums[i] ^ 1; + while cnt > 1 { + cnt -= nums[j] ^ 1; + j += 1; + } + ans = ans.max(i - j); + } + + ans as i32 + } +} diff --git a/solution/1400-1499/1493.Longest Subarray of 1's After Deleting One Element/Solution3.cpp b/solution/1400-1499/1493.Longest Subarray of 1's After Deleting One Element/Solution3.cpp index 782fa4074e10b..14c25fdbe179f 100644 --- a/solution/1400-1499/1493.Longest Subarray of 1's After Deleting One Element/Solution3.cpp +++ b/solution/1400-1499/1493.Longest Subarray of 1's After Deleting One Element/Solution3.cpp @@ -1,7 +1,7 @@ class Solution { public: int longestSubarray(vector& nums) { - int ans = 0, cnt = 0, l = 0; + int cnt = 0, l = 0; for (int x : nums) { cnt += x ^ 1; if (cnt > 1) { @@ -10,4 +10,4 @@ class Solution { } return nums.size() - l - 1; } -}; \ No newline at end of file +}; diff --git a/solution/1400-1499/1493.Longest Subarray of 1's After Deleting One Element/Solution3.go b/solution/1400-1499/1493.Longest Subarray of 1's After Deleting One Element/Solution3.go index b35f6bbe1ab57..2e0ef131a10e7 100644 --- a/solution/1400-1499/1493.Longest Subarray of 1's After Deleting One Element/Solution3.go +++ b/solution/1400-1499/1493.Longest Subarray of 1's After Deleting One Element/Solution3.go @@ -1,4 +1,4 @@ -func longestSubarray(nums []int) (ans int) { +func longestSubarray(nums []int) int { cnt, l := 0, 0 for _, x := range nums { cnt += x ^ 1 @@ -8,4 +8,4 @@ func longestSubarray(nums []int) (ans int) { } } return len(nums) - l - 1 -} \ No newline at end of file +} diff --git a/solution/1400-1499/1493.Longest Subarray of 1's After Deleting One Element/Solution3.java b/solution/1400-1499/1493.Longest Subarray of 1's After Deleting One Element/Solution3.java index 939ac9ed5ad9a..916d90e345c29 100644 --- a/solution/1400-1499/1493.Longest Subarray of 1's After Deleting One Element/Solution3.java +++ b/solution/1400-1499/1493.Longest Subarray of 1's After Deleting One Element/Solution3.java @@ -1,6 +1,6 @@ class Solution { public int longestSubarray(int[] nums) { - int ans = 0, cnt = 0, l = 0; + int cnt = 0, l = 0; for (int x : nums) { cnt += x ^ 1; if (cnt > 1) { @@ -9,4 +9,4 @@ public int longestSubarray(int[] nums) { } return nums.length - l - 1; } -} \ No newline at end of file +} diff --git a/solution/1400-1499/1493.Longest Subarray of 1's After Deleting One Element/Solution3.rs b/solution/1400-1499/1493.Longest Subarray of 1's After Deleting One Element/Solution3.rs new file mode 100644 index 0000000000000..d6fdfbc0bb33f --- /dev/null +++ b/solution/1400-1499/1493.Longest Subarray of 1's After Deleting One Element/Solution3.rs @@ -0,0 +1,16 @@ +impl Solution { + pub fn longest_subarray(nums: Vec) -> i32 { + let mut cnt = 0; + let mut l = 0; + + for &x in &nums { + cnt += x ^ 1; + if cnt > 1 { + cnt -= nums[l] ^ 1; + l += 1; + } + } + + (nums.len() - l - 1) as i32 + } +}