Skip to content
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

feat: add solutions to lc problem: No.2419 #3983

Merged
merged 1 commit into from
Jan 22, 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
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ X++:X 加 1 ,X = 0 + 1 = 1
<pre>
<strong>输入:</strong>operations = ["++X","++X","X++"]
<strong>输出:</strong>3
<strong>解释:</strong>操作按下述步骤执行:
<strong>解释:</strong>操作按下述步骤执行:
最初,X = 0
++X:X 加 1 ,X = 0 + 1 = 1
++X:X 加 1 ,X = 1 + 1 = 2
Expand Down Expand Up @@ -85,11 +85,11 @@ X--:X 减 1 ,X = 1 - 1 = 0

<!-- solution:start -->

### 方法一:模拟
### 方法一:计数

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

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

<!-- tabs:start -->

Expand Down Expand Up @@ -122,7 +122,9 @@ class Solution {
public:
int finalValueAfterOperations(vector<string>& operations) {
int ans = 0;
for (auto& s : operations) ans += (s[1] == '+' ? 1 : -1);
for (auto& s : operations) {
ans += s[1] == '+' ? 1 : -1;
}
return ans;
}
};
Expand All @@ -147,11 +149,7 @@ func finalValueAfterOperations(operations []string) (ans int) {

```ts
function finalValueAfterOperations(operations: string[]): number {
let ans = 0;
for (let operation of operations) {
ans += operation.includes('+') ? 1 : -1;
}
return ans;
return operations.reduce((acc, op) => acc + (op[1] === '+' ? 1 : -1), 0);
}
```

Expand All @@ -177,11 +175,7 @@ impl Solution {
* @return {number}
*/
var finalValueAfterOperations = function (operations) {
let ans = 0;
for (const s of operations) {
ans += s[1] === '+' ? 1 : -1;
}
return ans;
return operations.reduce((acc, op) => acc + (op[1] === '+' ? 1 : -1), 0);
};
```

Expand All @@ -201,22 +195,4 @@ int finalValueAfterOperations(char** operations, int operationsSize) {

<!-- solution:end -->

<!-- solution:start -->

### 方法二

<!-- tabs:start -->

#### TypeScript

```ts
function finalValueAfterOperations(operations: string[]): number {
return operations.reduce((r, v) => r + (v[1] === '+' ? 1 : -1), 0);
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ X--: X is decremented by 1, X = 1 - 1 = 0.

<!-- solution:start -->

### Solution 1: Simulation
### Solution 1: Counting

Traverse the array `operations`. For each operation $operations[i]$, if it contains `'+'`, then the answer increases by $1$, otherwise the answer decreases by $1$.
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$.

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

<!-- tabs:start -->

Expand Down Expand Up @@ -120,7 +120,9 @@ class Solution {
public:
int finalValueAfterOperations(vector<string>& operations) {
int ans = 0;
for (auto& s : operations) ans += (s[1] == '+' ? 1 : -1);
for (auto& s : operations) {
ans += s[1] == '+' ? 1 : -1;
}
return ans;
}
};
Expand All @@ -145,11 +147,7 @@ func finalValueAfterOperations(operations []string) (ans int) {

```ts
function finalValueAfterOperations(operations: string[]): number {
let ans = 0;
for (let operation of operations) {
ans += operation.includes('+') ? 1 : -1;
}
return ans;
return operations.reduce((acc, op) => acc + (op[1] === '+' ? 1 : -1), 0);
}
```

Expand All @@ -175,11 +173,7 @@ impl Solution {
* @return {number}
*/
var finalValueAfterOperations = function (operations) {
let ans = 0;
for (const s of operations) {
ans += s[1] === '+' ? 1 : -1;
}
return ans;
return operations.reduce((acc, op) => acc + (op[1] === '+' ? 1 : -1), 0);
};
```

Expand All @@ -199,22 +193,4 @@ int finalValueAfterOperations(char** operations, int operationsSize) {

<!-- solution:end -->

<!-- solution:start -->

### Solution 2

<!-- tabs:start -->

#### TypeScript

```ts
function finalValueAfterOperations(operations: string[]): number {
return operations.reduce((r, v) => r + (v[1] === '+' ? 1 : -1), 0);
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ class Solution {
public:
int finalValueAfterOperations(vector<string>& operations) {
int ans = 0;
for (auto& s : operations) ans += (s[1] == '+' ? 1 : -1);
for (auto& s : operations) {
ans += s[1] == '+' ? 1 : -1;
}
return ans;
}
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,5 @@
* @return {number}
*/
var finalValueAfterOperations = function (operations) {
let ans = 0;
for (const s of operations) {
ans += s[1] === '+' ? 1 : -1;
}
return ans;
return operations.reduce((acc, op) => acc + (op[1] === '+' ? 1 : -1), 0);
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
function finalValueAfterOperations(operations: string[]): number {
let ans = 0;
for (let operation of operations) {
ans += operation.includes('+') ? 1 : -1;
}
return ans;
return operations.reduce((acc, op) => acc + (op[1] === '+' ? 1 : -1), 0);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ tags:
<strong>输入:</strong>nums = [1,2,3,4]
<strong>输出:</strong>1
<strong>解释:</strong>
子数组按位与运算的最大值是 4 。
子数组按位与运算的最大值是 4 。
能得到此结果的最长子数组是 [4],所以返回 1 。
</pre>

Expand All @@ -77,9 +77,9 @@ tags:

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

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

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

<!-- tabs:start -->

Expand All @@ -90,8 +90,8 @@ class Solution:
def longestSubarray(self, nums: List[int]) -> int:
mx = max(nums)
ans = cnt = 0
for v in nums:
if v == mx:
for x in nums:
if x == mx:
cnt += 1
ans = max(ans, cnt)
else:
Expand All @@ -104,15 +104,11 @@ class Solution:
```java
class Solution {
public int longestSubarray(int[] nums) {
int mx = 0;
for (int v : nums) {
mx = Math.max(mx, v);
}
int mx = Arrays.stream(nums).max().getAsInt();
int ans = 0, cnt = 0;
for (int v : nums) {
if (v == mx) {
++cnt;
ans = Math.max(ans, cnt);
for (int x : nums) {
if (x == mx) {
ans = Math.max(ans, ++cnt);
} else {
cnt = 0;
}
Expand All @@ -128,12 +124,11 @@ class Solution {
class Solution {
public:
int longestSubarray(vector<int>& nums) {
int mx = *max_element(nums.begin(), nums.end());
int mx = ranges::max(nums);
int ans = 0, cnt = 0;
for (int v : nums) {
if (v == mx) {
++cnt;
ans = max(ans, cnt);
for (int x : nums) {
if (x == mx) {
ans = max(ans, ++cnt);
} else {
cnt = 0;
}
Expand All @@ -146,18 +141,18 @@ public:
#### Go

```go
func longestSubarray(nums []int) int {
func longestSubarray(nums []int) (ans int) {
mx := slices.Max(nums)
ans, cnt := 0, 0
for _, v := range nums {
if v == mx {
cnt := 0
for _, x := range nums {
if x == mx {
cnt++
ans = max(ans, cnt)
} else {
cnt = 0
}
}
return ans
return
}
```

Expand All @@ -167,38 +162,59 @@ func longestSubarray(nums []int) int {
function longestSubarray(nums: number[]): number {
const mx = Math.max(...nums);
let [ans, cnt] = [0, 0];

for (const x of nums) {
if (x === mx) {
cnt++;
ans = Math.max(ans, cnt);
ans = Math.max(ans, ++cnt);
} else {
cnt = 0;
}
}

return ans;
}
```

#### Rust

```rust
impl Solution {
pub fn longest_subarray(nums: Vec<i32>) -> i32 {
let mx = *nums.iter().max().unwrap();
let mut ans = 0;
let mut cnt = 0;

for &x in nums.iter() {
if x == mx {
cnt += 1;
ans = ans.max(cnt);
} else {
cnt = 0;
}
}

ans
}
}
```

#### JavaScript

```js
function longestSubarray(nums) {
/**
* @param {number[]} nums
* @return {number}
*/
var longestSubarray = function (nums) {
const mx = Math.max(...nums);
let [ans, cnt] = [0, 0];

for (const x of nums) {
if (x === mx) {
cnt++;
ans = Math.max(ans, cnt);
ans = Math.max(ans, ++cnt);
} else {
cnt = 0;
}
}

return ans;
}
};
```

<!-- tabs:end -->
Expand Down
Loading
Loading