Skip to content

Merge from master #77

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 40 commits into from
Jun 23, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
5199224
test: 813 solution
QuBenhao Jun 17, 2025
b36683f
test: [20250618] Add (2966)
actions-user Jun 17, 2025
9d10421
test: 2966 solution
QuBenhao Jun 18, 2025
2f38636
test: 1 solution
QuBenhao Jun 18, 2025
dd8c79f
test: 791 solution
QuBenhao Jun 18, 2025
cc41073
test: 21 cpp memory
QuBenhao Jun 18, 2025
fffdd6d
test: 24, 25 cpp memory
QuBenhao Jun 18, 2025
6637ea5
test: [20250619] Add (2294)
actions-user Jun 18, 2025
5075d66
test: 2294 solution
QuBenhao Jun 18, 2025
103d3e4
test: 2294 solution
QuBenhao Jun 18, 2025
a247bba
test: reformat
QuBenhao Jun 19, 2025
b47be1d
test: cpp
QuBenhao Jun 19, 2025
6a12133
test: 775 solution
QuBenhao Jun 19, 2025
0347a36
test: 775
QuBenhao Jun 19, 2025
b2df641
test: [20250620] Add (3443)
actions-user Jun 19, 2025
53e7921
test: 3443 solution
QuBenhao Jun 19, 2025
bc40a9b
test: 879 solution
QuBenhao Jun 20, 2025
09b76ab
test: 864 solution
QuBenhao Jun 20, 2025
8e8c28a
test: [20250621] Add (3085)
actions-user Jun 20, 2025
20d5fc6
test: 3085 solution
QuBenhao Jun 21, 2025
c339a4a
test: 2385 solution
QuBenhao Jun 21, 2025
dfd89d7
test: 978 solution
QuBenhao Jun 21, 2025
fd36ad8
test: 1662 solution
QuBenhao Jun 21, 2025
a8b28d0
test: 1662 template
QuBenhao Jun 21, 2025
ce94dbc
test: [20250622] Add (2138)
actions-user Jun 21, 2025
274ebc6
test: doc
QuBenhao Jun 21, 2025
2a1d5fc
test: 2138 solution
QuBenhao Jun 21, 2025
74d3141
test: add bi-weekly contest #159
QuBenhao Jun 21, 2025
d607b58
test: 3587, 3588, 3589, 3590 solution
QuBenhao Jun 21, 2025
7125804
test: 3587, 3589 solution
QuBenhao Jun 22, 2025
964b7cf
test: add weekly contest #455
QuBenhao Jun 22, 2025
b12649f
test: 3591, 3592, 3593, 3594 solution
QuBenhao Jun 22, 2025
77fcc45
test: 3591, 3592, 3593, 3594 solution (unfinished)
QuBenhao Jun 22, 2025
f8282e3
test: 3593 solution
QuBenhao Jun 22, 2025
49dbafc
test: 3593 solution
QuBenhao Jun 22, 2025
a72c260
test: [20250623] Add (2081)
actions-user Jun 22, 2025
000884e
test: 2081 solution
QuBenhao Jun 23, 2025
8cd1be0
test: 3594 solution
QuBenhao Jun 23, 2025
9ae741e
test: 1704 solution
QuBenhao Jun 23, 2025
c809158
test: 816 solution
QuBenhao Jun 23, 2025
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
Prev Previous commit
Next Next commit
test: 978 solution
py, c++, go, java
  • Loading branch information
QuBenhao committed Jun 21, 2025
commit dfd89d77bd9bec6a8122ebb4c9f71b452fd017fc
2 changes: 1 addition & 1 deletion daily-problems.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"daily": "2385",
"daily": "978",
"plans": ["3582", "problems", "3583", "problems", "3584", "problems", "3585", "problems"]
}
4 changes: 2 additions & 2 deletions golang/solution_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package golang

import (
problem "leetCode/problems/problems_2385"
problem "leetCode/problems/problems_978"
"testing"
)

func TestSolution(t *testing.T) {
TestEach(t, "2385", "problems", problem.Solve)
TestEach(t, "978", "problems", problem.Solve)
}
42 changes: 42 additions & 0 deletions problems/problems_978/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//go:build ignore
#include "cpp/common/Solution.h"

using namespace std;
using json = nlohmann::json;

class Solution {
public:
int maxTurbulenceSize(vector<int> &arr) {
int n = arr.size(), ans = 1;
int cur0 = 1, cur1 = 1;
for (int i = 1; i < n; ++i) {
if (arr[i] > arr[i - 1]) {
cur1 = cur0 + 1;
cur0 = 1;
} else if (arr[i] < arr[i - 1]) {
cur0 = cur1 + 1;
cur1 = 1;
} else {
cur0 = 1;
cur1 = 1;
}
ans = max(ans, max(cur0, cur1));
}
return ans;
}
};

json leetcode::qubh::Solve(string input_json_values) {
vector<string> inputArray;
size_t pos = input_json_values.find('\n');
while (pos != string::npos) {
inputArray.push_back(input_json_values.substr(0, pos));
input_json_values = input_json_values.substr(pos + 1);
pos = input_json_values.find('\n');
}
inputArray.push_back(input_json_values);

Solution solution;
vector<int> arr = json::parse(inputArray.at(0));
return solution.maxTurbulenceSize(arr);
}
34 changes: 34 additions & 0 deletions problems/problems_978/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package problems.problems_978;

import com.alibaba.fastjson.JSON;
import java.util.*;
import qubhjava.BaseSolution;


public class Solution extends BaseSolution {
public int maxTurbulenceSize(int[] arr) {
int n = arr.length;
int ans = 1;
int cur0 = 1, cur1 = 1;
for (int i = 1; i < n; i++) {
if (arr[i] > arr[i - 1]) {
cur1 = cur0 + 1;
cur0 = 1;
} else if (arr[i] < arr[i - 1]) {
cur0 = cur1 + 1;
cur1 = 1;
} else {
cur0 = 1;
cur1 = 1;
}
ans = Math.max(ans, Math.max(cur0, cur1));
}
return ans;
}

@Override
public Object solve(String[] inputJsonValues) {
int[] arr = jsonArrayToIntArray(inputJsonValues[0]);
return JSON.toJSON(maxTurbulenceSize(arr));
}
}
53 changes: 53 additions & 0 deletions problems/problems_978/problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# 978. Longest Turbulent Subarray [Rating: 1393.41]

<p>Given an integer array <code>arr</code>, return <em>the length of a maximum size turbulent subarray of</em> <code>arr</code>.</p>

<p>A subarray is <strong>turbulent</strong> if the comparison sign flips between each adjacent pair of elements in the subarray.</p>

<p>More formally, a subarray <code>[arr[i], arr[i + 1], ..., arr[j]]</code> of <code>arr</code> is said to be turbulent if and only if:</p>

<ul>
<li>For <code>i &lt;= k &lt; j</code>:
<ul>
<li><code>arr[k] &gt; arr[k + 1]</code> when <code>k</code> is odd, and</li>
<li><code>arr[k] &lt; arr[k + 1]</code> when <code>k</code> is even.</li>
</ul>
</li>
<li>Or, for <code>i &lt;= k &lt; j</code>:
<ul>
<li><code>arr[k] &gt; arr[k + 1]</code> when <code>k</code> is even, and</li>
<li><code>arr[k] &lt; arr[k + 1]</code> when <code>k</code> is odd.</li>
</ul>
</li>
</ul>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> arr = [9,4,2,10,7,8,8,1,9]
<strong>Output:</strong> 5
<strong>Explanation:</strong> arr[1] &gt; arr[2] &lt; arr[3] &gt; arr[4] &lt; arr[5]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> arr = [4,8,12,16]
<strong>Output:</strong> 2
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> arr = [100]
<strong>Output:</strong> 1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= arr.length &lt;= 4 * 10<sup>4</sup></code></li>
<li><code>0 &lt;= arr[i] &lt;= 10<sup>9</sup></code></li>
</ul>
54 changes: 54 additions & 0 deletions problems/problems_978/problem_zh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# 978. 最长湍流子数组 [难度分: 1393.41]

<p>给定一个整数数组 <code>arr</code>&nbsp;,返回 <code>arr</code>&nbsp;的&nbsp;<em>最大湍流子数组的<strong>长度</strong></em><strong>&nbsp;</strong>。</p>

<p>如果比较符号在子数组中的每个相邻元素对之间翻转,则该子数组是&nbsp;<strong>湍流子数组</strong>&nbsp;。</p>

<p>更正式地来说,当 <code>arr</code>&nbsp;的子数组&nbsp;<code>A[i], A[i+1], ..., A[j]</code>&nbsp;满足仅满足下列条件时,我们称其为<em>湍流子数组</em>:</p>

<ul>
<li>若&nbsp;<code>i &lt;= k &lt; j</code>&nbsp;:
<ul>
<li>当 <code>k</code>&nbsp;为奇数时,&nbsp;<code>A[k] &gt; A[k+1]</code>,且</li>
<li>当 <code>k</code> 为偶数时,<code>A[k] &lt; A[k+1]</code>;</li>
</ul>
</li>
<li><strong>或 </strong>若&nbsp;<code>i &lt;= k &lt; j</code>&nbsp;:
<ul>
<li>当 <code>k</code> 为偶数时,<code>A[k] &gt; A[k+1]</code>&nbsp;,且</li>
<li>当 <code>k</code>&nbsp;为奇数时,&nbsp;<code>A[k] &lt; A[k+1]</code>。</li>
</ul>
</li>
</ul>

<p>&nbsp;</p>

<p><strong>示例 1:</strong></p>

<pre>
<strong>输入:</strong>arr = [9,4,2,10,7,8,8,1,9]
<strong>输出:</strong>5
<strong>解释:</strong>arr[1] &gt; arr[2] &lt; arr[3] &gt; arr[4] &lt; arr[5]</pre>

<p><strong>示例 2:</strong></p>

<pre>
<strong>输入:</strong>arr = [4,8,12,16]
<strong>输出:</strong>2
</pre>

<p><strong>示例 3:</strong></p>

<pre>
<strong>输入:</strong>arr = [100]
<strong>输出:</strong>1
</pre>

<p>&nbsp;</p>

<p><strong>提示:</strong></p>

<ul>
<li><code>1 &lt;= arr.length &lt;= 4 * 10<sup>4</sup></code></li>
<li><code>0 &lt;= arr[i] &lt;= 10<sup>9</sup></code></li>
</ul>
37 changes: 37 additions & 0 deletions problems/problems_978/solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package problem978

import (
"encoding/json"
"log"
"strings"
)

func maxTurbulenceSize(arr []int) int {
n := len(arr)
ans := 1
cur0, cur1 := 1, 1
for i := 1; i < n; i++ {
if arr[i] > arr[i-1] {
cur1 = cur0 + 1
cur0 = 1
} else if arr[i] < arr[i-1] {
cur0 = cur1 + 1
cur1 = 1
} else {
cur0, cur1 = 1, 1
}
ans = max(ans, max(cur0, cur1))
}
return ans
}

func Solve(inputJsonValues string) any {
inputValues := strings.Split(inputJsonValues, "\n")
var arr []int

if err := json.Unmarshal([]byte(inputValues[0]), &arr); err != nil {
log.Fatal(err)
}

return maxTurbulenceSize(arr)
}
23 changes: 23 additions & 0 deletions problems/problems_978/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from itertools import pairwise

import solution
from typing import *


class Solution(solution.Solution):
def solve(self, test_input=None):
return self.maxTurbulenceSize(test_input)

def maxTurbulenceSize(self, arr: List[int]) -> int:
n = len(arr)
ans = 1
last = [1, 1] # 第一位表示下降,第二位表示上升
for i in range(1, n):
cur_0 = cur_1 = 1
if arr[i] > arr[i-1]:
cur_1 += last[0]
elif arr[i] < arr[i-1]:
cur_0 += last[1]
ans = max(ans, cur_0, cur_1)
last[0], last[1] = cur_0, cur_1
return ans
2 changes: 2 additions & 0 deletions problems/problems_978/testcase

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions problems/problems_978/testcase.py

Large diffs are not rendered by default.