LeetCode Solutions | 901 - 1000
904. Fruit Into Baskets
sliding window (max subarray with at most 2 distinct elements
can be optimized: instead of doing i += 1
repeatedly, remember where to jump
932. Beautiful Array
漂亮数组有以下的性质:
- A 是一个漂亮数组,如果对 A 中所有元素添加一个常数,那么A还是一个漂亮数组。
- A 是一个漂亮数组,如果对 A 中所有元素乘以一个常数,那么 A 还是一个漂亮数组。
- A 是一个漂亮数组,如果删除一些 A 中一些元素,那么 A 还是一个漂亮数组。
- A 是一个奇数构成的漂亮数组,B 是一个偶数构成的漂亮数组,那么 A+B 也是一个漂亮数组
- 比如:
{1, 5, 3, 7} + {2, 6, 4, 8} = {1, 5, 3, 7, 2, 6, 4, 8}
也是一个漂亮数组。
- 比如:
所以我们假设一个 1..=m
的数组是漂亮数组,可以通过下面的方式构造漂亮数组 1..=2 * m
:
- 对
1..=m
中所有的数乘以 2 - 1,构成一个奇数漂亮数组 A。如{1, 3, 2, 4}
, 可以得到{1, 5, 3, 7}
- 对
1..=m
中所有的数乘以 2, 构成一个偶数漂亮数组 B, 如{1, 3, 2, 4}
, 可以得到{2, 6, 4, 8}
- A+B 构成了
1..=2 * m
的漂亮数组。{1, 5, 3, 7} + {2, 6, 4, 8} = {1, 5, 3, 7, 2, 6, 4, 8}
- 从中删除不要的数字即可。
DC TODO
951. Flip Equivalent Binary Trees
just compare
973. K Closest Points to Origin
use quicksort to find the kth
element, all elements to the left of kth
is the result
heap
986. Interval List Intersections
like a merge sort