File tree Expand file tree Collapse file tree 3 files changed +104
-1
lines changed
3105-Longest Strictly Increasing or Strictly Decreasing Subarray Expand file tree Collapse file tree 3 files changed +104
-1
lines changed Original file line number Diff line number Diff line change 30
30
runs-on : ubuntu-latest
31
31
steps :
32
32
- name : Checkout
33
- uses : actions/checkout@v3
33
+ uses : actions/checkout@v4
34
34
- name : Setup Pages
35
35
uses : actions/configure-pages@v3
36
36
- name : Upload artifact
Original file line number Diff line number Diff line change
1
+ # 3105. Longest Strictly Increasing or Strictly Decreasing Subarray
2
+
3
+ You are given an array of integers ` nums ` . Return the length of the longest subarray of ` nums `
4
+ which is either strictly increasing or strictly decreasing.
5
+
6
+ ## 基礎思路
7
+ 我們可以觀察兩個元素之間差異,來決定趨勢,趨勢共有三種可能:
8
+ - 變換趨勢: 重置長度為 2 (因為檢測到趨勢變換時已經有兩個元素)
9
+ - 維持趨勢: 此時將當前趨勢長度加一即可
10
+ - 保持不變: 當數字相同時,我們需要紀錄當前長度為 1 (因為題目要求嚴格遞增或遞減)
11
+
12
+ ## 解題步驟
13
+
14
+ ### Step 1: 初始化變數
15
+
16
+ ``` typescript
17
+ const n = nums .length ;
18
+
19
+ let maxLength = 1 ;
20
+ let currentLength = 1 ;
21
+ // 當前 subarray 的趨勢
22
+ // 1 代表遞增
23
+ // -1 代表遞減
24
+ // 0 代表無趨勢 (或重置)
25
+ let currentType = 0 ;
26
+ ```
27
+
28
+ ### Step 2: 迭代數組
29
+
30
+ ``` typescript
31
+ for (let i = 1 ; i < n ; i ++ ) {
32
+ const different = nums [i ] - nums [i - 1 ];
33
+ // 檢查趨勢類型
34
+ const newType = different > 0 ? 1 : different < 0 ? - 1 : 0 ;
35
+
36
+ if (newType === 0 ) {
37
+ // 當前元素與前一個元素相同,重置 subarray 長度為 1
38
+ currentLength = 1 ;
39
+ currentType = 0 ;
40
+ } else if (newType === currentType ) {
41
+ // 當前元素與前一個元素趨勢相同,增加 subarray 長度
42
+ currentLength ++ ;
43
+ } else {
44
+ // 當前元素與前一個元素趨勢不同,重置 subarray 長度為 2,開始新的 subarray 計算
45
+ currentLength = 2 ;
46
+ currentType = newType ;
47
+ }
48
+
49
+ // 更新最大長度
50
+ if (currentLength > maxLength ) {
51
+ maxLength = currentLength ;
52
+ }
53
+ }
54
+ ```
55
+
56
+ ## 時間複雜度
57
+ - 計算趨勢的時間複雜度為 $O(n)$
58
+ - 總體時間複雜度為 $O(n)$
59
+
60
+ > $O(n)$
61
+
62
+ ## 空間複雜度
63
+ - 僅使用常數空間
64
+ - 總空間複雜度為 $O(1)$
65
+
66
+ > $O(1)$
Original file line number Diff line number Diff line change
1
+ function longestMonotonicSubarray ( nums : number [ ] ) : number {
2
+ const n = nums . length ;
3
+
4
+ let maxLength = 1 ;
5
+ let currentLength = 1 ;
6
+ // The type of the current subarray:
7
+ // 1 for increasing,
8
+ // -1 for decreasing,
9
+ // 0 for none (or reset)
10
+ let currentType = 0 ;
11
+
12
+ for ( let i = 1 ; i < n ; i ++ ) {
13
+ const different = nums [ i ] - nums [ i - 1 ] ;
14
+ // Determine the new trend
15
+ const newType = different > 0 ? 1 : different < 0 ? - 1 : 0 ;
16
+
17
+ if ( newType === 0 ) {
18
+ // Reset when equal.
19
+ currentLength = 1 ;
20
+ currentType = 0 ;
21
+ } else if ( newType === currentType ) {
22
+ // Continue in the same direction.
23
+ currentLength ++ ;
24
+ } else {
25
+ // New trend: start a new subarray that includes the previous element.
26
+ currentLength = 2 ;
27
+ currentType = newType ;
28
+ }
29
+
30
+ // Update the maximum length.
31
+ if ( currentLength > maxLength ) {
32
+ maxLength = currentLength ;
33
+ }
34
+ }
35
+
36
+ return maxLength ;
37
+ }
You can’t perform that action at this time.
0 commit comments