You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Segment_Tree/3721.Longest-Balanced-Subarray-II/Readme.md
+8-2Lines changed: 8 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,12 @@
2
2
3
3
既然是求区间,那么不妨考虑“前缀之差”。根据经验,我们需要记录每个前缀里“unique even numbers与unique odd numbers的个数之差”。任何两个差相同的前缀,意味着一个符合条件的区间,即“unique even numbers与unique odd numbers的个数相等”。
4
4
5
-
对于一个nums[0:i]的前缀,如何表达“unique even numbers与unique odd numbers的个数之差”呢?我们只需要关心每个unique number最后一次出现的位置。记作lastPos[x]。比如偶数4最后一次出现在了a这个位置,那么我们就记录array[a]=1;假设奇数7最后一次出现在了b这个位置,那么我们就记录array[b]=-1.可见,只要计算这个01数组array[0:i]的前缀和,就可以知道nums[0:i]里“unique even numbers与unique odd numbers的个数之差”。
5
+
对于一个nums[0:i]的前缀,如何表达“unique even numbers与unique odd numbers的个数之差”呢?因为每个unique number可能出现在很多位置,为了便于分析,我们约定只关心每个unique number最后一次出现的位置。记作lastPos[x]。比如偶数4最后一次出现在了a这个位置,那么我们就记录array[a]=1;假设奇数7最后一次出现在了b这个位置,那么我们就记录array[b]=-1.可见,只要计算这个01数组array[0:i]的前缀和,就可以知道nums[0:i]里“unique even numbers与unique odd numbers的个数之差”。
0 commit comments