Heap —— 20181120
Posted lizzyluvcoding
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Heap —— 20181120相关的知识,希望对你有一定的参考价值。
363. Trapping Rain Water
1 public class Solution { 2 /** 3 * @param heights: a list of integers 4 * @return: a integer 5 */ 6 public int trapRainWater(int[] heights) { 7 // write your code here 8 if (heights == null || heights.length == 0) { 9 return 0; 10 } 11 12 int start = 0; 13 int end = heights.length - 1; 14 int maxLeft = Integer.MIN_VALUE; 15 int maxRight = Integer.MIN_VALUE; 16 int sum = 0; 17 while (start < end) { 18 maxLeft = heights[start] > maxLeft ? heights[start] : maxLeft; 19 maxRight = heights[end] > maxRight ? heights[end] : maxRight; 20 if (maxLeft < maxRight) { 21 sum += maxLeft - heights[start]; 22 start++; 23 } else { 24 sum += maxRight - heights[end]; 25 end--; 26 } 27 } 28 return sum; 29 } 30 }
364. Trapping Rain Water II
1 class Point { 2 int x; 3 int y; 4 int height; 5 6 public Point(int x, int y, int height) { 7 this.x = x; 8 this.y = y; 9 this.height = height; 10 } 11 } 12 13 14 public class Solution { 15 /** 16 * @param heights: a matrix of integers 17 * @return: an integer 18 */ 19 20 int[] dx = {0, 1, 0, -1}; 21 int[] dy = {1, 0, -1, 0}; 22 23 public int trapRainWater(int[][] heights) { 24 // write your code here 25 if (heights == null || heights.length == 0 || heights[0].length == 0) { 26 return 0; 27 } 28 int r = heights.length; 29 int c = heights[0].length; 30 boolean[][] visited = new boolean[r][c]; 31 32 Comparator<Point> minComparator = new Comparator<Point>() { 33 @Override 34 public int compare(Point o1, Point o2) { 35 return o1.height - o2.height; 36 } 37 }; 38 39 PriorityQueue<Point> minHeap = new PriorityQueue<>(minComparator); 40 41 for (int i = 0; i < r; i++) { 42 minHeap.add(new Point(i, 0, heights[i][0])); 43 visited[i][0] = true; 44 minHeap.add(new Point(i, c - 1, heights[i][c - 1])); 45 visited[i][c - 1] = true; 46 } 47 48 for (int j = 0; j < c; j++) { 49 minHeap.add(new Point(0, j, heights[0][j])); 50 visited[0][j] = true; 51 minHeap.add(new Point(r - 1, j, heights[r - 1][j])); 52 visited[r - 1][j] = true; 53 } 54 55 int sum = 0; 56 while (!minHeap.isEmpty()) { 57 Point point = minHeap.poll(); 58 59 for (int i = 0; i < 4; i++) { 60 int nx = point.x + dx[i]; 61 int ny = point.y + dy[i]; 62 if (!isValid(nx, ny, heights, visited)) { 63 continue; 64 } 65 66 visited[nx][ny] = true; 67 minHeap.add(new Point(nx, ny, Math.max(heights[nx][ny], point.height))); 68 sum += Math.max(heights[nx][ny], point.height) - heights[nx][ny]; 69 } 70 } 71 return sum; 72 73 } 74 75 public boolean isValid(int x, int y, int[][] heights, boolean[][] visited) { 76 if (x < 0 || x > heights.length - 1 || y < 0 || y > heights[0].length - 1) { 77 return false; 78 } 79 if (visited[x][y]) { 80 return false; 81 } 82 return true; 83 } 84 }
360. Sliding Window Median 具体思路见 81. Find Median from Data Stream
1 public class Solution { 2 /** 3 * @param nums: A list of integers 4 * @param k: An integer 5 * @return: The median of the element inside the window at each moving 6 */ 7 private PriorityQueue<Integer> minHeap; 8 private PriorityQueue<Integer> maxHeap; 9 private int maxSize = 0; 10 private int minSize = 0; 11 12 public List<Integer> medianSlidingWindow(int[] nums, int k) { 13 // write your code here 14 if (nums == null || nums.length < k || k <= 0) { 15 return new ArrayList<>(); 16 } 17 18 minHeap = new PriorityQueue<>(); 19 maxHeap = new PriorityQueue<>(new Comparator<Integer>() { 20 @Override 21 public int compare(Integer o1, Integer o2) { 22 return o2.compareTo(o1); 23 } 24 }); 25 List<Integer> res = new ArrayList<>(); 26 27 for (int i = 0; i < k; i++) { 28 addNumber(nums[i]); 29 } 30 res.add(getMedian()); 31 for (int j = k; j < nums.length; j++) { 32 slideByWindow(nums[j], nums[j - k]); 33 res.add(getMedian()); 34 } 35 return res; 36 } 37 38 public void addNumber(int num) { 39 maxHeap.add(num); 40 maxSize++; 41 if (maxSize - minSize <= 1) { 42 if (minHeap.isEmpty()) { 43 return; 44 } 45 46 if (maxHeap.peek() > minHeap.peek()) { 47 int maxTemp = maxHeap.poll(); 48 int minTemp = minHeap.poll(); 49 minHeap.add(maxTemp); 50 maxHeap.add(minTemp); 51 } 52 return; 53 } 54 55 minHeap.add(maxHeap.poll()); 56 minSize++; 57 maxSize--; 58 } 59 60 public int getMedian() { 61 return maxHeap.peek(); 62 } 63 64 public void slideByWindow(int toAdd, int toRemove) { 65 if (toRemove <= getMedian()) { 66 maxHeap.remove(toRemove); 67 maxSize--; 68 } else { 69 minHeap.remove(toRemove); 70 minSize--; 71 } 72 addNumber(toAdd); 73 } 74 }
以上是关于Heap —— 20181120的主要内容,如果未能解决你的问题,请参考以下文章