Leet 4000 Code Find K-th Smallest Pair Distance
2017-12-04 16:52
337 查看
Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pair (A, B) is defined as the absolute difference between A and B.
Example 1:
Input:
nums = [1,3,1]
k = 1
Output: 0
Explanation:
Here are all the pairs:
(1,3) -> 2
(1,1) -> 0
(3,1) -> 2
Then the 1st smallest distance pair is (1,1), and its distance is 0.
Note:
2 <= len(nums) <= 10000.
0 <= nums[i] < 1000000.
1 <= k <= len(nums) * (len(nums) - 1) / 2.
题意为给定一个数组,计算任意两个数之间的距离,返回所有距离中第k小的距离。最初的做法是建立一个vector存储所有距离,再将距离排序输出第k个值。运行时超时。后来改进了一下,建立大小为N的容器,运行通过了但运行时间很长:
看了提示说可以用二元搜索,又参照了其他人的做法,恍然大悟。cnt中存储所有差小于mid的个数,再将cnt与k相比较,若cnt小于k,则提高下限;否则,降低上限。用二元搜素解决该问题,确实很巧妙。
另:现在做题还没有形成一个好的思维方式,对大规模数据的处理以及排序方面接触较少,有待加强练习。
Example 1:
Input:
nums = [1,3,1]
k = 1
Output: 0
Explanation:
Here are all the pairs:
(1,3) -> 2
(1,1) -> 0
(3,1) -> 2
Then the 1st smallest distance pair is (1,1), and its distance is 0.
Note:
2 <= len(nums) <= 10000.
0 <= nums[i] < 1000000.
1 <= k <= len(nums) * (len(nums) - 1) / 2.
题意为给定一个数组,计算任意两个数之间的距离,返回所有距离中第k小的距离。最初的做法是建立一个vector存储所有距离,再将距离排序输出第k个值。运行时超时。后来改进了一下,建立大小为N的容器,运行通过了但运行时间很长:
class Solution { public: int smallestDistancePair(vector<int>& nums, int k) { int N = 1000000; vector<int> res(N, 0); for(int i=0; i<nums.size(); i++) { for(int j=i+1; j<nums.size(); j++) { res[abs(nums[i] - nums[j])]++; } } for(int i=0; i<N; i++) { if(res[i] >= k) return i; k -= res[i]; } return 0; } };
看了提示说可以用二元搜索,又参照了其他人的做法,恍然大悟。cnt中存储所有差小于mid的个数,再将cnt与k相比较,若cnt小于k,则提高下限;否则,降低上限。用二元搜素解决该问题,确实很巧妙。
class Solution { public: int smallestDistancePair(vector<int>& nums, int k) { sort(nums.begin(), nums.end()); int low = 0, high = nums[nums.size() - 1]; while(low<high) { int mid = low + (high-low)/2, cnt = 0; for(int i=0, j=0; i<nums.size(); i++) { while(j<nums.size() && (nums[j]-nums[i])<=mid) j++; cnt += j-i-1; } if(cnt < k) low = mid +1; else high = mid; } return low; } };
另:现在做题还没有形成一个好的思维方式,对大规模数据的处理以及排序方面接触较少,有待加强练习。
相关文章推荐
- leetcode 719. Find K-th Smallest Pair Distance
- LeetCode719. Find K-th Smallest Pair Distance (hard)
- Find K-th Smallest Pair Distance:查找数组元素中差值第K大的两个元素的差值
- LeetCode Weekly Contest 56 Find K-th Smallest Pair Distance
- leetcode 719. Find K-th Smallest Pair Distance 第k小的绝对距离 + 暴力计算真棒
- LEETCODE: 719 Find K-th Smallest Pair Distance
- 719. Find K-th Smallest Pair Distance
- 719. Find K-th Smallest Pair Distance
- 算法第15周Find K-th Smallest Pair Distance[hard]
- 719. Find K-th Smallest Pair Distance
- LWC 56:719. Find K-th Smallest Pair Distance
- [LeetCode]440. K-th Smallest in Lexicographical Order
- 非常非常好!写了好久 k-th-smallest-in-lexicographical-order
- [leetcode 440]K-th Smallest in Lexicographical Order
- K-th Smallest in Lexicographical Order
- 786. K-th Smallest Prime Fraction
- [Leetcode] 786. K-th Smallest Prime Fraction 解题报告
- 440. K-th Smallest in Lexicographical Order
- Find the k-th Smallest Element in the Union of Two Sorted Arrays
- leetcode 786. K-th Smallest Prime Fraction