File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed
Longest Strictly Increasing or Strictly Decreasing Subarray Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change
1
+ package kata
2
+
3
+ func longestMonotonicSubarray (nums []int ) int {
4
+ var lsi int
5
+ var lsd int
6
+ var currentLsi = 1
7
+ var currentLsd = 1
8
+ for i := 1 ; i < len (nums ); i ++ {
9
+ if nums [i - 1 ] < nums [i ] {
10
+ currentLsi ++
11
+ if lsd < currentLsd {
12
+ lsd = currentLsd
13
+ }
14
+ currentLsd = 1
15
+ } else if nums [i - 1 ] > nums [i ] {
16
+ currentLsd ++
17
+ if lsi < currentLsi {
18
+ lsi = currentLsi
19
+ }
20
+ currentLsi = 1
21
+ } else {
22
+ if lsi < currentLsi {
23
+ lsi = currentLsi
24
+ }
25
+ if lsd < currentLsd {
26
+ lsd = currentLsd
27
+ }
28
+ currentLsd = 1
29
+ currentLsi = 1
30
+ }
31
+ }
32
+
33
+ if lsd < currentLsd {
34
+ lsd = currentLsd
35
+ }
36
+ if lsi < currentLsi {
37
+ lsi = currentLsi
38
+ }
39
+
40
+ if lsi < lsd {
41
+ return lsd
42
+ }
43
+
44
+ return lsi
45
+ }
You can’t perform that action at this time.
0 commit comments