Skip to content

Commit e780ccd

Browse files
authored
feat: add solutions to lc problem: No.2419 (doocs#3983)
No.2419.Longest Subarray With Maximum Bitwise AND
1 parent 1fd052d commit e780ccd

File tree

15 files changed

+167
-181
lines changed

15 files changed

+167
-181
lines changed

Diff for: solution/2000-2099/2011.Final Value of Variable After Performing Operations/README.md

+9-33
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ X++:X 加 1 ,X = 0 + 1 = 1
5050
<pre>
5151
<strong>输入:</strong>operations = ["++X","++X","X++"]
5252
<strong>输出:</strong>3
53-
<strong>解释:</strong>操作按下述步骤执行:
53+
<strong>解释:</strong>操作按下述步骤执行:
5454
最初,X = 0
5555
++X:X 加 1 ,X = 0 + 1 = 1
5656
++X:X 加 1 ,X = 1 + 1 = 2
@@ -85,11 +85,11 @@ X--:X 减 1 ,X = 1 - 1 = 0
8585

8686
<!-- solution:start -->
8787

88-
### 方法一:模拟
88+
### 方法一:计数
8989

90-
遍历数组 `operations`,对于每个操作 $operations[i]$,如果包含 `'+'`,那么答案加 $1$,否则答案减 $1$。
90+
我们遍历数组 $\textit{operations}$,对于每个操作 $\textit{operations}[i]$,如果包含 `'+'`,那么答案加 $1$,否则答案减 $1$。
9191

92-
时间复杂度为 $O(n)$,其中 $n$ 为数组 `operations` 的长度。空间复杂度 $O(1)$。
92+
时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{operations}$ 的长度。空间复杂度 $O(1)$。
9393

9494
<!-- tabs:start -->
9595

@@ -122,7 +122,9 @@ class Solution {
122122
public:
123123
int finalValueAfterOperations(vector<string>& operations) {
124124
int ans = 0;
125-
for (auto& s : operations) ans += (s[1] == '+' ? 1 : -1);
125+
for (auto& s : operations) {
126+
ans += s[1] == '+' ? 1 : -1;
127+
}
126128
return ans;
127129
}
128130
};
@@ -147,11 +149,7 @@ func finalValueAfterOperations(operations []string) (ans int) {
147149

148150
```ts
149151
function finalValueAfterOperations(operations: string[]): number {
150-
let ans = 0;
151-
for (let operation of operations) {
152-
ans += operation.includes('+') ? 1 : -1;
153-
}
154-
return ans;
152+
return operations.reduce((acc, op) => acc + (op[1] === '+' ? 1 : -1), 0);
155153
}
156154
```
157155

@@ -177,11 +175,7 @@ impl Solution {
177175
* @return {number}
178176
*/
179177
var finalValueAfterOperations = function (operations) {
180-
let ans = 0;
181-
for (const s of operations) {
182-
ans += s[1] === '+' ? 1 : -1;
183-
}
184-
return ans;
178+
return operations.reduce((acc, op) => acc + (op[1] === '+' ? 1 : -1), 0);
185179
};
186180
```
187181

@@ -201,22 +195,4 @@ int finalValueAfterOperations(char** operations, int operationsSize) {
201195
202196
<!-- solution:end -->
203197
204-
<!-- solution:start -->
205-
206-
### 方法二
207-
208-
<!-- tabs:start -->
209-
210-
#### TypeScript
211-
212-
```ts
213-
function finalValueAfterOperations(operations: string[]): number {
214-
return operations.reduce((r, v) => r + (v[1] === '+' ? 1 : -1), 0);
215-
}
216-
```
217-
218-
<!-- tabs:end -->
219-
220-
<!-- solution:end -->
221-
222198
<!-- problem:end -->

Diff for: solution/2000-2099/2011.Final Value of Variable After Performing Operations/README_EN.md

+8-32
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ X--: X is decremented by 1, X = 1 - 1 = 0.
8383

8484
<!-- solution:start -->
8585

86-
### Solution 1: Simulation
86+
### Solution 1: Counting
8787

88-
Traverse the array `operations`. For each operation $operations[i]$, if it contains `'+'`, then the answer increases by $1$, otherwise the answer decreases by $1$.
88+
We traverse the array $\textit{operations}$. For each operation $\textit{operations}[i]$, if it contains `'+'`, we increment the answer by $1$, otherwise, we decrement the answer by $1$.
8989

90-
The time complexity is $O(n)$, where $n$ is the length of the array `operations`. The space complexity is $O(1)$.
90+
The time complexity is $O(n)$, where $n$ is the length of the array $\textit{operations}$. The space complexity is $O(1)$.
9191

9292
<!-- tabs:start -->
9393

@@ -120,7 +120,9 @@ class Solution {
120120
public:
121121
int finalValueAfterOperations(vector<string>& operations) {
122122
int ans = 0;
123-
for (auto& s : operations) ans += (s[1] == '+' ? 1 : -1);
123+
for (auto& s : operations) {
124+
ans += s[1] == '+' ? 1 : -1;
125+
}
124126
return ans;
125127
}
126128
};
@@ -145,11 +147,7 @@ func finalValueAfterOperations(operations []string) (ans int) {
145147

146148
```ts
147149
function finalValueAfterOperations(operations: string[]): number {
148-
let ans = 0;
149-
for (let operation of operations) {
150-
ans += operation.includes('+') ? 1 : -1;
151-
}
152-
return ans;
150+
return operations.reduce((acc, op) => acc + (op[1] === '+' ? 1 : -1), 0);
153151
}
154152
```
155153

@@ -175,11 +173,7 @@ impl Solution {
175173
* @return {number}
176174
*/
177175
var finalValueAfterOperations = function (operations) {
178-
let ans = 0;
179-
for (const s of operations) {
180-
ans += s[1] === '+' ? 1 : -1;
181-
}
182-
return ans;
176+
return operations.reduce((acc, op) => acc + (op[1] === '+' ? 1 : -1), 0);
183177
};
184178
```
185179

@@ -199,22 +193,4 @@ int finalValueAfterOperations(char** operations, int operationsSize) {
199193
200194
<!-- solution:end -->
201195
202-
<!-- solution:start -->
203-
204-
### Solution 2
205-
206-
<!-- tabs:start -->
207-
208-
#### TypeScript
209-
210-
```ts
211-
function finalValueAfterOperations(operations: string[]): number {
212-
return operations.reduce((r, v) => r + (v[1] === '+' ? 1 : -1), 0);
213-
}
214-
```
215-
216-
<!-- tabs:end -->
217-
218-
<!-- solution:end -->
219-
220196
<!-- problem:end -->

Diff for: solution/2000-2099/2011.Final Value of Variable After Performing Operations/Solution.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ class Solution {
22
public:
33
int finalValueAfterOperations(vector<string>& operations) {
44
int ans = 0;
5-
for (auto& s : operations) ans += (s[1] == '+' ? 1 : -1);
5+
for (auto& s : operations) {
6+
ans += s[1] == '+' ? 1 : -1;
7+
}
68
return ans;
79
}
8-
};
10+
};

Diff for: solution/2000-2099/2011.Final Value of Variable After Performing Operations/Solution.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,5 @@
33
* @return {number}
44
*/
55
var finalValueAfterOperations = function (operations) {
6-
let ans = 0;
7-
for (const s of operations) {
8-
ans += s[1] === '+' ? 1 : -1;
9-
}
10-
return ans;
6+
return operations.reduce((acc, op) => acc + (op[1] === '+' ? 1 : -1), 0);
117
};
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
function finalValueAfterOperations(operations: string[]): number {
2-
let ans = 0;
3-
for (let operation of operations) {
4-
ans += operation.includes('+') ? 1 : -1;
5-
}
6-
return ans;
2+
return operations.reduce((acc, op) => acc + (op[1] === '+' ? 1 : -1), 0);
73
}

Diff for: solution/2000-2099/2011.Final Value of Variable After Performing Operations/Solution2.ts

-3
This file was deleted.

Diff for: solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/README.md

+49-33
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ tags:
5252
<strong>输入:</strong>nums = [1,2,3,4]
5353
<strong>输出:</strong>1
5454
<strong>解释:</strong>
55-
子数组按位与运算的最大值是 4 。
55+
子数组按位与运算的最大值是 4 。
5656
能得到此结果的最长子数组是 [4],所以返回 1 。
5757
</pre>
5858

@@ -77,9 +77,9 @@ tags:
7777

7878
题目可以转换为求最大值在数组中最多连续出现的次数。
7979

80-
先遍历一遍数组,求出最大值,然后再遍历一遍数组,求出最大值连续出现的次数,最后返回这个次数即可。
80+
我们先遍历数组 $\textit{nums}$ 找到最大值 $\textit{mx}$,然后再遍历数组一次,找到最大值连续出现的次数,最后返回这个次数即可。
8181

82-
时间复杂度 $O(n)$其中 $n$ 为数组的长度
82+
时间复杂度 $O(n)$其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$
8383

8484
<!-- tabs:start -->
8585

@@ -90,8 +90,8 @@ class Solution:
9090
def longestSubarray(self, nums: List[int]) -> int:
9191
mx = max(nums)
9292
ans = cnt = 0
93-
for v in nums:
94-
if v == mx:
93+
for x in nums:
94+
if x == mx:
9595
cnt += 1
9696
ans = max(ans, cnt)
9797
else:
@@ -104,15 +104,11 @@ class Solution:
104104
```java
105105
class Solution {
106106
public int longestSubarray(int[] nums) {
107-
int mx = 0;
108-
for (int v : nums) {
109-
mx = Math.max(mx, v);
110-
}
107+
int mx = Arrays.stream(nums).max().getAsInt();
111108
int ans = 0, cnt = 0;
112-
for (int v : nums) {
113-
if (v == mx) {
114-
++cnt;
115-
ans = Math.max(ans, cnt);
109+
for (int x : nums) {
110+
if (x == mx) {
111+
ans = Math.max(ans, ++cnt);
116112
} else {
117113
cnt = 0;
118114
}
@@ -128,12 +124,11 @@ class Solution {
128124
class Solution {
129125
public:
130126
int longestSubarray(vector<int>& nums) {
131-
int mx = *max_element(nums.begin(), nums.end());
127+
int mx = ranges::max(nums);
132128
int ans = 0, cnt = 0;
133-
for (int v : nums) {
134-
if (v == mx) {
135-
++cnt;
136-
ans = max(ans, cnt);
129+
for (int x : nums) {
130+
if (x == mx) {
131+
ans = max(ans, ++cnt);
137132
} else {
138133
cnt = 0;
139134
}
@@ -146,18 +141,18 @@ public:
146141
#### Go
147142
148143
```go
149-
func longestSubarray(nums []int) int {
144+
func longestSubarray(nums []int) (ans int) {
150145
mx := slices.Max(nums)
151-
ans, cnt := 0, 0
152-
for _, v := range nums {
153-
if v == mx {
146+
cnt := 0
147+
for _, x := range nums {
148+
if x == mx {
154149
cnt++
155150
ans = max(ans, cnt)
156151
} else {
157152
cnt = 0
158153
}
159154
}
160-
return ans
155+
return
161156
}
162157
```
163158

@@ -167,38 +162,59 @@ func longestSubarray(nums []int) int {
167162
function longestSubarray(nums: number[]): number {
168163
const mx = Math.max(...nums);
169164
let [ans, cnt] = [0, 0];
170-
171165
for (const x of nums) {
172166
if (x === mx) {
173-
cnt++;
174-
ans = Math.max(ans, cnt);
167+
ans = Math.max(ans, ++cnt);
175168
} else {
176169
cnt = 0;
177170
}
178171
}
179-
180172
return ans;
181173
}
182174
```
183175

176+
#### Rust
177+
178+
```rust
179+
impl Solution {
180+
pub fn longest_subarray(nums: Vec<i32>) -> i32 {
181+
let mx = *nums.iter().max().unwrap();
182+
let mut ans = 0;
183+
let mut cnt = 0;
184+
185+
for &x in nums.iter() {
186+
if x == mx {
187+
cnt += 1;
188+
ans = ans.max(cnt);
189+
} else {
190+
cnt = 0;
191+
}
192+
}
193+
194+
ans
195+
}
196+
}
197+
```
198+
184199
#### JavaScript
185200

186201
```js
187-
function longestSubarray(nums) {
202+
/**
203+
* @param {number[]} nums
204+
* @return {number}
205+
*/
206+
var longestSubarray = function (nums) {
188207
const mx = Math.max(...nums);
189208
let [ans, cnt] = [0, 0];
190-
191209
for (const x of nums) {
192210
if (x === mx) {
193-
cnt++;
194-
ans = Math.max(ans, cnt);
211+
ans = Math.max(ans, ++cnt);
195212
} else {
196213
cnt = 0;
197214
}
198215
}
199-
200216
return ans;
201-
}
217+
};
202218
```
203219

204220
<!-- tabs:end -->

0 commit comments

Comments
 (0)