Advertisement
sweet1cris

Untitled

Jan 9th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.66 KB | None | 0 0
  1. // 方法一
  2. class Pair {
  3.     public int x, y, sum;
  4.     public Pair(int x, int y, int val) {
  5.         this.x = x;
  6.         this.y = y;
  7.         this.sum = val;
  8.     }
  9. }
  10. class PairComparator implements Comparator<Pair> {
  11.     public int compare(Pair a, Pair b) {
  12.         return a.sum - b.sum;
  13.     }
  14. }
  15.  
  16. public class Solution {
  17.     /**
  18.      * @param A an integer arrays sorted in ascending order
  19.      * @param B an integer arrays sorted in ascending order
  20.      * @param k an integer
  21.      * @return an integer
  22.      */
  23.     public int kthSmallestSum(int[] A, int[] B, int k) {
  24.         int[] dx = new int[]{0, 1};
  25.         int[] dy = new int[]{1, 0};
  26.         boolean[][] hash = new boolean[A.length][B.length];
  27.         PriorityQueue<Pair> minHeap = new PriorityQueue<Pair>(k, new PairComparator());
  28.         minHeap.add(new Pair(0, 0, A[0] + B[0]));
  29.  
  30.         for(int i = 0; i < k - 1; i ++){
  31.             Pair cur = minHeap.poll();
  32.             for(int j = 0; j < 2; j ++){
  33.                 int next_x = cur.x + dx[j];
  34.                 int next_y = cur.y + dy[j];
  35.                 Pair next_Pair = new Pair(next_x, next_y, 0);
  36.                 if(next_x < A.length && next_y < B.length &&  !hash[next_x][next_y]){
  37.                     hash[next_x][next_y] = true;
  38.                     next_Pair.sum = A[next_x] + B[next_y];
  39.                     minHeap.add(next_Pair);
  40.                 }
  41.             }
  42.         }
  43.         return minHeap.peek().sum;
  44.     }
  45. }
  46.  
  47. //方法二:
  48. class Pair implements Comparable<Pair> {
  49.     int x;
  50.     int y;
  51.     int sum;
  52.     Pair(int x, int y, int sum) {
  53.         this.x = x;
  54.         this.y = y;
  55.         this.sum = sum;
  56.     }
  57.     @Override
  58.     public int compareTo(Pair another) {
  59.         if (this.sum == another.sum) {
  60.             return 0;
  61.         }
  62.         return this.sum < another.sum ? -1 : 1;
  63.     }
  64.     @Override
  65.     public boolean equals(Object obj) {
  66.         if (obj == this) {
  67.             return true;
  68.         } else if (!(obj instanceof Pair)) {
  69.             return false;
  70.         }
  71.         Pair another = (Pair) obj;
  72.         return this.x == another.x && this.y == another.y;
  73.     }
  74.     @Override
  75.     public int hashCode() {
  76.         return x * 101 + y;
  77.     }
  78. }
  79. public class Solution {
  80.     /**
  81.      * @param A an integer arrays sorted in ascending order
  82.      * @param B an integer arrays sorted in ascending order
  83.      * @param k an integer
  84.      * @return an integer
  85.      */
  86.     int[] dx = {1, 0};
  87.     int[] dy = {0, 1};
  88.     public int kthSmallestSum(int[] A, int[] B, int k) {
  89.         if (A.length == 0 && B.length == 0) {
  90.             return 0;
  91.         } else if (A.length == 0) {
  92.             return B[k];
  93.         } else if (B.length == 0) {
  94.             return A[k];
  95.         }
  96.         HashSet<Pair> isVisited = new HashSet<Pair>();
  97.         PriorityQueue<Pair> minHeap = new PriorityQueue<Pair>();
  98.         Pair p;
  99.         Pair nextP;
  100.         p = new Pair(0, 0, A[0] + B[0]);
  101.         minHeap.offer(p);
  102.         isVisited.add(p);
  103.         int nextX;
  104.         int nextY;
  105.         int nextSum;
  106.         for (int count = 0; count < k - 1; count++) {
  107.             p = minHeap.poll();
  108.             for (int i = 0; i < 2; i++) {
  109.                 nextX = p.x + dx[i];
  110.                 nextY = p.y + dy[i];
  111.                 nextP = new Pair(nextX, nextY, 0);
  112.                 if (nextX >= 0 && nextX < A.length && nextY >= 0 && nextY < B.length && !isVisited.contains(nextP)) {
  113.                     nextSum = A[nextX] + B[nextY];
  114.                     nextP.sum = nextSum;
  115.                     minHeap.offer(nextP);
  116.                     isVisited.add(nextP);
  117.                 }
  118.             }
  119.         }
  120.         return minHeap.peek().sum;
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement