新农上路
- 积分
- 99
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2018-3-7
- 最后登录
- 1970-1-1
|
双指针的话应该是要loop 两遍, 但是还是要考虑去重的问题
比如 第一遍 A 指针从0 开始, B 指针从末尾开始
第二遍 A 指针从 末尾开始, B 指针从0开始。 这个解法我也没有非常确定,但想不出反例。
最好的解法还是用map,可以参考这个老哥的代码,这是在刷题网discuss下面的答案,coding style不好,但是思路肯定是对的
- public static void main(String[] args) {
- int arr1[][] = { {1, 2000}, {2, 3000}, {3, 4000}, {4, 2000} };
- int arr2[][] = { { 1, 5000 }, {2, 3000} };
- List<int[]> re = getClosestPairs(arr1, arr2, 7500);
- for(int[] r : re)
- {
- System.out.println(r[0] + " " + r[1]);
- }
- }
- private static List<int[]> getClosestPairs(int[][] A, int[][] B, int target)
- {
- TreeMap<Integer, List<Integer>> valueWithIds = new TreeMap<>();
- List<int[]> result = new ArrayList<>();
- int closestTarget = 0;
- /*
- Loop through the first Array and insert the values into the map with 'value' (A[1]) as key and id (A[0]) add to the list as value.
- */
- for(int[] a : A)
- {
- int currentTarget = a[1], id = a[0];
- valueWithIds.putIfAbsent(currentTarget, new ArrayList<>());
- valueWithIds.get(currentTarget).add(id);
- }
- for(int[] b : B)
- {
- int remainingTarget = target - b[1]; // We will find the remaining target by the value from the value of B.
- // For example if target = 5000, b[1] = 3000, then remainingTarget = 2000
- if(remainingTarget < 0) continue;
- if(valueWithIds.containsKey(remainingTarget)) // That means we find the closest target == currentTarget
- {
- if(closestTarget != target)
- {
- closestTarget = target;
- result = new ArrayList<>();
- }
- for(int id : valueWithIds.get(remainingTarget) )
- {
- result.add(new int[] {id, b[0]});
- }
- }
- else
- {
- Integer floor = valueWithIds.floorKey(remainingTarget); // Check if we have any least value which is less than remainingTarget.
- if(floor == null) continue;
- int currentTarget = b[1] + floor;
- if( currentTarget >= closestTarget )
- {
- if(currentTarget != closestTarget)
- {
- closestTarget = currentTarget;
- result = new ArrayList<>();
- }
- for(int id : valueWithIds.get(floor) )
- {
- result.add(new int[] {id, b[0]});
- }
- }
- }
- }
- return result;
- }}
复制代码 |
|