-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3721-LongestBalancedSubarrayII.go
More file actions
157 lines (137 loc) · 4.21 KB
/
3721-LongestBalancedSubarrayII.go
File metadata and controls
157 lines (137 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
// 3721. Longest Balanced Subarray II
// You are given an integer array nums.
// A subarray is called balanced if the number of distinct even numbers in the subarray is equal to the number of distinct odd numbers.
// Return the length of the longest balanced subarray.
// A subarray is a contiguous non-empty sequence of elements within an array.
// Example 1:
// Input: nums = [2,5,4,3]
// Output: 4
// Explanation:
// The longest balanced subarray is [2, 5, 4, 3].
// It has 2 distinct even numbers [2, 4] and 2 distinct odd numbers [5, 3]. Thus, the answer is 4.
// Example 2:
// Input: nums = [3,2,2,5,4]
// Output: 5
// Explanation:
// The longest balanced subarray is [3, 2, 2, 5, 4].
// It has 2 distinct even numbers [2, 4] and 2 distinct odd numbers [3, 5]. Thus, the answer is 5.
// Example 3:
// Input: nums = [1,2,3,2]
// Output: 3
// Explanation:
// The longest balanced subarray is [2, 3, 2].
// It has 1 distinct even number [2] and 1 distinct odd number [3]. Thus, the answer is 3.
// Constraints:
// 1 <= nums.length <= 10^5
// 1 <= nums[i] <= 10^5
import "fmt"
import "math/bits"
type Pair struct{ mn, mx int }
type lazySegment []struct {
l, r int
Pair
todo int
}
func merge(l, r Pair) Pair {
return Pair{min(l.mn, r.mn), max(l.mx, r.mx)}
}
func (t lazySegment) apply(o int, f int) {
cur := &t[o]
cur.mn += f
cur.mx += f
cur.todo += f
}
func (t lazySegment) maintain(o int) {
t[o].Pair = merge(t[o<<1].Pair, t[o<<1|1].Pair)
}
func (t lazySegment) spread(o int) {
f := t[o].todo
if f == 0 { return }
t.apply(o<<1, f)
t.apply(o<<1|1, f)
t[o].todo = 0
}
func (t lazySegment) build(o, l, r int) {
t[o].l, t[o].r = l, r
if l == r {return }
m := (l + r) >> 1
t.build(o<<1, l, m)
t.build(o<<1|1, m + 1, r)
}
func (t lazySegment) update(o, l, r int, f int) {
if l <= t[o].l && t[o].r <= r {
t.apply(o, f)
return
}
t.spread(o)
m := (t[o].l + t[o].r) >> 1
if l <= m {
t.update(o<<1, l, r, f)
}
if m < r {
t.update(o<<1|1, l, r, f)
}
t.maintain(o)
}
// 查询 [l,r] 内第一个等于 target 的元素下标
func (t lazySegment) findFirst(o, l, r, target int) int {
if t[o].l > r || t[o].r < l || target < t[o].Pair.mn || target > t[o].Pair.mx {
return -1
}
if t[o].l == t[o].r { return t[o].l }
t.spread(o)
index := t.findFirst(o<<1, l, r, target)
if index < 0 {
// 去右子树找
index = t.findFirst(o<<1|1, l, r, target)
}
return index
}
func longestBalanced(nums []int) int {
res, sum, n := 0, 0, len(nums)
t := make(lazySegment, 2 << bits.Len(uint(n)))
t.build(1, 0, n)
last := map[int]int{} // nums 的元素上一次出现的位置
for i := 1; i <= n; i++ {
x := nums[i-1]
v := x % 2 * 2 - 1
if j := last[x]; j == 0 { // 首次遇到 x
sum += v
t.update(1, i, n, v) // sum[i:] 增加 v
} else { // 再次遇到 x
t.update(1, j, i-1, -v) // 撤销之前对 sum[j:i] 的增加
}
last[x] = i
j := t.findFirst(1, 0, i-1, sum)
if j >= 0 {
res = max(res, i - j)
}
}
return res
}
func main() {
// Example 1:
// Input: nums = [2,5,4,3]
// Output: 4
// Explanation:
// The longest balanced subarray is [2, 5, 4, 3].
// It has 2 distinct even numbers [2, 4] and 2 distinct odd numbers [5, 3]. Thus, the answer is 4.
fmt.Println(longestBalanced([]int{2,5,4,3})) // 4
// Example 2:
// Input: nums = [3,2,2,5,4]
// Output: 5
// Explanation:
// The longest balanced subarray is [3, 2, 2, 5, 4].
// It has 2 distinct even numbers [2, 4] and 2 distinct odd numbers [3, 5]. Thus, the answer is 5.
fmt.Println(longestBalanced([]int{3,2,2,5,4})) // 5
// Example 3:
// Input: nums = [1,2,3,2]
// Output: 3
// Explanation:
// The longest balanced subarray is [2, 3, 2].
// It has 1 distinct even number [2] and 1 distinct odd number [3]. Thus, the answer is 3.
fmt.Println(longestBalanced([]int{1,2,3,2})) // 3
fmt.Println(longestBalanced([]int{1,2,3,4,5,6,7,8,9})) // 8
fmt.Println(longestBalanced([]int{9,8,7,6,5,4,3,2,1})) // 8
}