leetcode 954. Array of Doubled Pairs
Given an integer array of even length arr
, return true
if it is possible to reorder arr
such that arr[2 * i + 1] = 2 * arr[2 * i]
for every 0 <= i < len(arr) / 2
, or false
otherwise.
Example 1:
Input: arr = [3,1,3,6]Output: false
Example 2:
Input: arr = [2,1,2,6]Output: false
Example 3:
Input: arr = [4,-2,2,-4]Output: trueExplanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Constraints:
2 <= arr.length <= 3 * 104
arr.length
is even.-105 <= arr[i] <= 105
第一次用treemap.
treemap实现了sort的接口,所以可以直接排序完成,对于小的数(>0),去判断他的2倍数是否也在map中,而且小的数的value值应该要小于等于2倍数的value值,依次loop即可;
对于负数(<0)则判断它一半大小的值是否在map中,同时value值小于等于一半大小的value值。loop 结果;
Runtime: 42 ms, faster than 90.76% of Java online submissions for Array of Doubled Pairs.
Memory Usage: 50.1 MB, less than 81.79% of Java online submissions for Array of Doubled Pairs.
Next challenges:
Find Original Array From Doubled Array