Skip to content

Merge from master #1

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

Open
wants to merge 2,098 commits into
base: longsq
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
2098 commits
Select commit Hold shift + click to select a range
90ec84c
test: 2566 solution
QuBenhao Jun 13, 2025
2afcabf
test: 1642 solution
QuBenhao Jun 13, 2025
c3ed36d
test: 1483 solution
QuBenhao Jun 13, 2025
75d2246
test: 3534 solution
QuBenhao Jun 14, 2025
477175f
test: 3553 solution
QuBenhao Jun 14, 2025
1da56e7
test: 2103 solution
QuBenhao Jun 14, 2025
3c4ad5a
test: [20250615] Add (1432)
actions-user Jun 14, 2025
79bfa94
test: 1432 solution
QuBenhao Jun 14, 2025
3e980a6
test: 1504 solution
QuBenhao Jun 15, 2025
6a5e57b
test: 1504 solution
QuBenhao Jun 15, 2025
c7d112d
test: 316 solution
QuBenhao Jun 15, 2025
9774a03
test: 3582, 3583, 3584, 3585 solution
QuBenhao Jun 15, 2025
aca2ac6
test: 3582, 3583, 3584, 3585 solution
QuBenhao Jun 15, 2025
604013c
test: 3582, 3583, 3584, 3585 solution
QuBenhao Jun 15, 2025
7b202d8
test: 3582, 3583, 3584, 3585 solution
QuBenhao Jun 15, 2025
22c7f95
test: 1732 solution
QuBenhao Jun 15, 2025
a553d8f
test: [20250616] Add (2016)
actions-user Jun 15, 2025
96c0083
test: 2016 solution
QuBenhao Jun 15, 2025
bc2ce0e
test: add 321
QuBenhao Jun 15, 2025
ae709b8
test: 321 solution
QuBenhao Jun 16, 2025
5d0057a
test: 321
QuBenhao Jun 16, 2025
bb5ad88
test: 402 solution
QuBenhao Jun 16, 2025
2400f65
test: go build ignore
QuBenhao Jun 16, 2025
70b8af3
test: 1206 solution
QuBenhao Jun 16, 2025
404023d
test: 1206 solution
QuBenhao Jun 16, 2025
d88730c
fix: java groovy path on Windows
QuBenhao Jun 16, 2025
748fa1c
10.1.1
Jun 16, 2025
1c381e6
test: [20250617] Add (3405)
actions-user Jun 16, 2025
15f6fe5
test: 3405 solution
QuBenhao Jun 16, 2025
9ceb2ea
fix: use path.join for file paths in tests
QuBenhao Jun 17, 2025
d85887a
test: add 808, 813
QuBenhao Jun 17, 2025
2d593ea
fix: handle old python
QuBenhao Jun 17, 2025
ad89ad7
test: 808 solution
QuBenhao Jun 17, 2025
dffb5c8
test: 813 solution
QuBenhao Jun 17, 2025
a28bd3a
fix: maven problem folder and bazel plans
QuBenhao Jun 17, 2025
4f4e305
10.1.2
Jun 17, 2025
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
c728105
test: [20250624] Add (2200)
actions-user Jun 23, 2025
90e28e4
test: 2200 solution
QuBenhao Jun 23, 2025
4f89b85
test: 891 solution
QuBenhao Jun 24, 2025
6b5fd95
test: [20250625] Add (2040)
actions-user Jun 24, 2025
df162ad
test: 2040 solution
QuBenhao Jun 25, 2025
cf88b50
feat: CMake support (#161)
QuBenhao Jun 25, 2025
84d7de9
10.2.0
Jun 25, 2025
d21a63d
test: 1802, 2040 solution
QuBenhao Jun 25, 2025
60e6314
test: 1802, 2040 solution
QuBenhao Jun 25, 2025
a2507d9
test: 1802, 2040 solution
QuBenhao Jun 25, 2025
3a04222
test: [20250626] Add (2311)
actions-user Jun 25, 2025
ebbd5c6
test: 2311 solution
QuBenhao Jun 25, 2025
dc04664
test: 799 add
QuBenhao Jun 26, 2025
526d455
test: [20250627] Add (2014)
actions-user Jun 26, 2025
b4829e3
test: 799 solution
QuBenhao Jun 27, 2025
eca2cb2
test: 2014 solution
QuBenhao Jun 27, 2025
aee0e1e
test: 2240 solution
QuBenhao Jun 27, 2025
30a9307
test: 1106 solution
QuBenhao Jun 27, 2025
6a7d490
test: 228 solution
QuBenhao Jun 27, 2025
4af853a
test: 1124 solution
QuBenhao Jun 27, 2025
8a82d97
test: 1130 solution
QuBenhao Jun 27, 2025
3ae14a1
test: 496 solution
QuBenhao Jun 27, 2025
04135fe
test: [20250628] Add (2099)
actions-user Jun 27, 2025
a709549
test: 2099 solution
QuBenhao Jun 28, 2025
8d1cf5b
test: 2030 solution
QuBenhao Jun 28, 2025
360fda4
test: 213 solution
QuBenhao Jun 28, 2025
a4667d5
test: ignore
QuBenhao Jun 28, 2025
e636d0a
test: 1678 solution
QuBenhao Jun 28, 2025
ab9ce26
test: 1662 ignore
QuBenhao Jun 28, 2025
4a09821
test: [20250629] Add (1498)
actions-user Jun 28, 2025
d03ea3d
test: 1498 solution
QuBenhao Jun 28, 2025
7a9e189
test: 2818 solution
QuBenhao Jun 29, 2025
03bbc88
docs: update templates
QuBenhao Jun 29, 2025
e49d5ad
test: 3597, 3598, 3599, 3600 solution
QuBenhao Jun 29, 2025
b2578c0
test: 3597, 3598, 3599, 3600 solution
QuBenhao Jun 29, 2025
aff6604
test: 3597, 3598, 3599, 3600 solution
QuBenhao Jun 29, 2025
bfbc790
test: 3597, 3598, 3599, 3600 solution
QuBenhao Jun 29, 2025
24fc678
test: 1790 solution
QuBenhao Jun 29, 2025
7cf4c92
test: [20250630] Add (594)
actions-user Jun 29, 2025
c747291
test: 594 solution
QuBenhao Jun 29, 2025
4f24962
test: 862 solution
QuBenhao Jun 29, 2025
a3241c5
test: 209 solution
QuBenhao Jun 29, 2025
aafed6a
test: 450 solution
QuBenhao Jun 30, 2025
50d51c4
test: 784 solution
QuBenhao Jun 30, 2025
105818a
test: [20250701] Add (3330)
actions-user Jun 30, 2025
faf8be0
test: 3330 solution
QuBenhao Jun 30, 2025
4ae77c0
test: 3331 solution
QuBenhao Jul 1, 2025
4f9ce72
test: 3332 solution
QuBenhao Jul 1, 2025
b0ae265
test: 3333 solution
QuBenhao Jul 1, 2025
44b2ea5
test: 779 solution
QuBenhao Jul 1, 2025
ecfff6f
fix: daily_submission script
QuBenhao Jul 1, 2025
aa39d47
fix: daily_submission script
QuBenhao Jul 1, 2025
231211f
test: remove extra rust
QuBenhao Jul 1, 2025
faaa5f8
test: 779 solution
QuBenhao Jul 1, 2025
93a646a
test: [20250702] Add (3333)
actions-user Jul 1, 2025
2976209
test: 904 solution
QuBenhao Jul 2, 2025
5a1d53e
test: [20250703] Add (3304)
actions-user Jul 2, 2025
edd2dd7
test: 3304 solution
QuBenhao Jul 2, 2025
c5ec7f8
test: add function test
QuBenhao Jul 2, 2025
c7408e8
test: 1620 solution
QuBenhao Jul 3, 2025
0193cf2
test: [20250704] Add (3307)
actions-user Jul 3, 2025
b1c4057
test: 3307 solution
QuBenhao Jul 3, 2025
8dcf9b7
feat: 162 windows support (#163)
QuBenhao Jul 4, 2025
f765f6a
10.3.0
Jul 4, 2025
1da9d8b
test: [20250705] Add (1394)
actions-user Jul 4, 2025
61480f1
test: 1394 solution
QuBenhao Jul 5, 2025
865217c
test: 1784 solution
QuBenhao Jul 5, 2025
99913cc
test: 1784 solution
QuBenhao Jul 5, 2025
4476ea9
test: [20250706] Add (1865)
actions-user Jul 5, 2025
003b383
test: codeforces contest/475/D
QuBenhao Jul 5, 2025
78029f5
test: 1865 solution
QuBenhao Jul 5, 2025
942ff37
test: 3602, 3603, 3604, 3605 solution
QuBenhao Jul 6, 2025
432492d
fix: problemsetQuestionListV2
QuBenhao Jul 6, 2025
fde36c6
10.3.1
Jul 6, 2025
ba6a172
test: 3602, 3603, 3604, 3605 solution
QuBenhao Jul 6, 2025
f327325
docs: templates.md
QuBenhao Jul 6, 2025
1d1800b
test: 3602, 3603, 3604, 3605 solution
QuBenhao Jul 6, 2025
597415f
test: 3606, 3607, 3608, 3609
QuBenhao Jul 6, 2025
f531bd6
test: 3606, 3607, 3608, 3609
QuBenhao Jul 6, 2025
0e719bf
test: 3606, 3607, 3608, 3609 solution
QuBenhao Jul 6, 2025
63dfe2e
test: 3606, 3607, 3608, 3609 solution
QuBenhao Jul 6, 2025
bd7878b
test: 3602, 3603, 3604, 3605, 3606, 3607, 3608, 3609 solution
QuBenhao Jul 6, 2025
ecd0b02
fix: JavaWriter boolean[]
QuBenhao Jul 6, 2025
3694b4b
test: 3602, 3603, 3604, 3605, 3606, 3607, 3608, 3609 solution
QuBenhao Jul 6, 2025
24f2c7e
fix: java plans output
QuBenhao Jul 6, 2025
65d8f8b
test: 1640 solution
QuBenhao Jul 6, 2025
d7f50b2
docs: templates.md
QuBenhao Jul 6, 2025
e97afd4
test: codeforces contest/2119/A
QuBenhao Jul 6, 2025
4d66d31
test: [20250707] Add (1353)
actions-user Jul 6, 2025
8e2d582
test: 1353 solution
QuBenhao Jul 7, 2025
e3d8696
test: [20250708] Add (1751)
actions-user Jul 7, 2025
6034e41
test: 1751 solution
QuBenhao Jul 7, 2025
5f8bb77
test: 1235 solution
QuBenhao Jul 7, 2025
bfc06d2
test: 788 solution
QuBenhao Jul 7, 2025
4580c6b
test: 777 solution
QuBenhao Jul 8, 2025
9af072d
test: [20250709] Add (3439)
actions-user Jul 8, 2025
c32b579
test: 3439 solution
QuBenhao Jul 9, 2025
9949c58
test: 670 solution
QuBenhao Jul 9, 2025
83405d1
test: [20250710] Add (3440)
actions-user Jul 9, 2025
b9f4acd
test: 3440 solution
QuBenhao Jul 9, 2025
0d5c0fc
test: 672 solution
QuBenhao Jul 10, 2025
83f336f
test: [20250711] Add (3169)
actions-user Jul 10, 2025
5fffed5
test: 3169 solution
QuBenhao Jul 10, 2025
4560d6c
test: [20250712] Add (1900)
actions-user Jul 11, 2025
c215365
test: 667 solution
QuBenhao Jul 11, 2025
ec20135
test: 1900 solution
QuBenhao Jul 12, 2025
1dbd44d
test: 1592 solution
QuBenhao Jul 12, 2025
72e7b07
test: [20250713] Add (2410)
actions-user Jul 12, 2025
726cc52
test: 2410 solution
QuBenhao Jul 13, 2025
67e1590
feat: get problems by contest
QuBenhao Jul 13, 2025
1484369
test: 3612, 3613, 3614, 3615 solution
QuBenhao Jul 13, 2025
0e5b165
10.4.0
Jul 13, 2025
e853a05
test: [20250714] Add (1290)
actions-user Jul 13, 2025
fce22f4
test: 1290 solution
QuBenhao Jul 13, 2025
57eccfc
test: 662 solution
QuBenhao Jul 14, 2025
67ea07b
test: [20250715] Add (3136)
actions-user Jul 14, 2025
395d91f
test: 3136 solution
QuBenhao Jul 14, 2025
ce39a40
test: 1092 solution
QuBenhao Jul 15, 2025
367a219
test: [20250716] Add (3201)
actions-user Jul 15, 2025
79e6e1d
test: 3201 solution
QuBenhao Jul 15, 2025
600dc93
test: 658 solution
QuBenhao Jul 16, 2025
0cba050
test: [20250717] Add (3202)
actions-user Jul 16, 2025
403e196
test: 3202 solution
QuBenhao Jul 17, 2025
355a1e0
test: [20250718] Add (2163)
actions-user Jul 17, 2025
35e212d
test: 2163 solution
QuBenhao Jul 18, 2025
559ed9c
test: [20250719] Add (1233)
actions-user Jul 18, 2025
5298b19
test: 1233 solution
QuBenhao Jul 19, 2025
2e9f2ae
test: 1464 solution
QuBenhao Jul 19, 2025
e7e511f
test: [20250720] Add (1948)
actions-user Jul 19, 2025
d9c2e52
test: 1948 solution
QuBenhao Jul 19, 2025
d86a395
test: 3618, 3619, 3620, 3621 add
QuBenhao Jul 19, 2025
c33ca40
test: 3618, 3619, 3620, 3621 solution
QuBenhao Jul 19, 2025
4ebf868
test: 3618, 3619, 3620, 3621 solution
QuBenhao Jul 19, 2025
e62be22
test: 3618, 3619, 3620, 3621 solution
QuBenhao Jul 20, 2025
b4c9dce
test: 3618, 3619, 3620, 3621 solution
QuBenhao Jul 20, 2025
012e8a0
test: 3618, 3619, 3620, 3621 solution
QuBenhao Jul 20, 2025
5ccaeda
test: 3622, 3623, 3624, 3625 add
QuBenhao Jul 20, 2025
331c028
test: 3622, 3623, 3624, 3625 add
QuBenhao Jul 20, 2025
a879a1d
test: 3622, 3623, 3624, 3625 solution
QuBenhao Jul 20, 2025
dec03af
test: 3622, 3623, 3624, 3625 solution
QuBenhao Jul 20, 2025
478a98a
test: 3622, 3623, 3624, 3625 solution
QuBenhao Jul 20, 2025
ce23967
test: 3622, 3623, 3624, 3625 solution
QuBenhao Jul 20, 2025
a12ccc7
test: [20250721] Add (1957)
actions-user Jul 20, 2025
0e3eb70
test: 1957 solution
QuBenhao Jul 20, 2025
7fe8230
docs: template
QuBenhao Jul 20, 2025
41338f6
test: 1656 solution
QuBenhao Jul 20, 2025
e4e0252
test: 1282 solution
QuBenhao Jul 21, 2025
3436db3
test: [20250722] Add (1695)
actions-user Jul 21, 2025
c5ca206
test: 1695 solution
QuBenhao Jul 22, 2025
9e21e01
test: 1695 solution
QuBenhao Jul 22, 2025
1666cc8
test: 768 solution
QuBenhao Jul 22, 2025
af2e0de
test: [20250723] Add (1717)
actions-user Jul 22, 2025
ee2db84
test: 1717 solution
QuBenhao Jul 22, 2025
6e4575c
test: 640 solution
QuBenhao Jul 23, 2025
af102b4
test: [20250724] Add (2322)
actions-user Jul 23, 2025
31af44d
test: 2322 solution
QuBenhao Jul 23, 2025
dff47fa
test: 636 solution
QuBenhao Jul 24, 2025
41541c6
test: [20250725] Add (3487)
actions-user Jul 24, 2025
62e05dc
test: 3620 solution
QuBenhao Jul 24, 2025
f9a475f
test: 3487 solution
QuBenhao Jul 24, 2025
349fc52
test: [20250726] Add (3480)
actions-user Jul 25, 2025
00636f9
test: 3480 solution
QuBenhao Jul 26, 2025
b8c1409
test: 622 solution
QuBenhao Jul 26, 2025
6a7d255
test: 623 solution
QuBenhao Jul 26, 2025
b208011
test: [20250727] Add (2210)
actions-user Jul 26, 2025
6a11dd5
test: 2210 solution
QuBenhao Jul 26, 2025
fd36896
docs: add 质因数分解
QuBenhao Jul 27, 2025
c404113
test: 1260 solution
QuBenhao Jul 27, 2025
3e10dad
test: [20250728] Add (2044)
actions-user Jul 27, 2025
e270e7b
test: 2044 solution
QuBenhao Jul 27, 2025
d3fa321
test: 3628, 3629 solution
QuBenhao Jul 27, 2025
4fe3be5
test: 3628, 3629 solution
QuBenhao Jul 27, 2025
5e88ca6
test: 3628, 3629 solution
QuBenhao Jul 28, 2025
3af8eec
test: 3628, 3629 solution
QuBenhao Jul 28, 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.