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. public class Solution {
  14. /**
  15. * @param heights: a matrix of integers
  16. * @return: an integer
  17. */
  18.  
  19. int[] dx = {0, 1, 0, -1};
  20. int[] dy = {1, 0, -1, 0};
  21.  
  22. public int trapRainWater(int[][] heights) {
  23. // write your code here
  24. if (heights == null || heights.length == 0 || heights[0].length == 0) {
  25. return 0;
  26. }
  27. int r = heights.length;
  28. int c = heights[0].length;
  29. boolean[][] visited = new boolean[r][c];
  30.  
  31. Comparator<Point> minComparator = new Comparator<Point>() {
  32. @Override
  33. public int compare(Point o1, Point o2) {
  34. return o1.height - o2.height;
  35. }
  36. };
  37.  
  38. PriorityQueue<Point> minHeap = new PriorityQueue<>(minComparator);
  39.  
  40. for (int i = 0; i < r; i++) {
  41. minHeap.add(new Point(i, 0, heights[i][0]));
  42. visited[i][0] = true;
  43. minHeap.add(new Point(i, c - 1, heights[i][c - 1]));
  44. visited[i][c - 1] = true;
  45. }
  46.  
  47. for (int j = 0; j < c; j++) {
  48. minHeap.add(new Point(0, j, heights[0][j]));
  49. visited[0][j] = true;
  50. minHeap.add(new Point(r - 1, j, heights[r - 1][j]));
  51. visited[r - 1][j] = true;
  52. }
  53.  
  54. int sum = 0;
  55. while (!minHeap.isEmpty()) {
  56. Point point = minHeap.poll();
  57.  
  58. for (int i = 0; i < 4; i++) {
  59. int nx = point.x + dx[i];
  60. int ny = point.y + dy[i];
  61. if (!isValid(nx, ny, heights, visited)) {
  62. continue;
  63. }
  64.  
  65. visited[nx][ny] = true;
  66. minHeap.add(new Point(nx, ny, Math.max(heights[nx][ny], point.height)));
  67. sum += Math.max(heights[nx][ny], point.height) - heights[nx][ny];
  68. }
  69. }
  70. return sum;
  71.  
  72. }
  73.  
  74. public boolean isValid(int x, int y, int[][] heights, boolean[][] visited) {
  75. if (x < 0 || x > heights.length - 1 || y < 0 || y > heights[0].length - 1) {
  76. return false;
  77. }
  78. if (visited[x][y]) {
  79. return false;
  80. }
  81. return true;
  82. }
  83. }

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的更多相关文章

  1. java head space/ java.lang.OutOfMemoryError: Java heap space内存溢出

    上一篇JMX/JConsole调试本地还可以在centos6.5 服务器上进行监控有个问题端口只开放22那么设置的9998端口 你怎么都连不上怎么监控?(如果大神知道还望指点,个人见解) 线上项目出现 ...

  2. Java 堆内存与栈内存异同(Java Heap Memory vs Stack Memory Difference)

    --reference Java Heap Memory vs Stack Memory Difference 在数据结构中,堆和栈可以说是两种最基础的数据结构,而Java中的栈内存空间和堆内存空间有 ...

  3. [数据结构]——堆(Heap)、堆排序和TopK

    堆(heap),是一种特殊的数据结构.之所以特殊,因为堆的形象化是一个棵完全二叉树,并且满足任意节点始终不大于(或者不小于)左右子节点(有别于二叉搜索树Binary Search Tree).其中,前 ...

  4. Windbg Extension NetExt 使用指南 【3】 ---- 挖掘你想要的数据 Managed Heap

    摘要 : NetExt中有两个比较常用的命令可以用来分析heap上面的对象. 一个是!wheap, 另外一个是!windex. !wheap 这个命令可以用于打印出heap structure信息. ...

  5. JAVA Shallow heap & Retained heap

    最近在研究内存泄漏的问题,在使用MAT工具中发现了Shallow heap & Retained heap,不懂. 然后在网上找了一些资料. Shallow Size 对象自身占用的内存大小, ...

  6. 笔记:程序内存管理 .bss .data .rodata .text stack heap

    1.未初始化的全局变量(.bss段) bss段用来存放 没有被初始化 和 已经被初始化为0 的全局变量.如下例代码: #include<stdio.h> int bss_array[102 ...

  7. STL heap usage

    简介 heap有查找时间复杂度O(1),查找.插入.删除时间复杂度为O(logN)的特性,STL中heap相关的操作如下: make_heap() push_heap() pop_heap() sor ...

  8. Nodemanager Out of heap memory[fix bug全过程]

    问题: 自己写了一个yarn上的application,发现nodemanager过段时间,会out of memory退出,把nodemanager的heap memory从1G增大到2G也是无法避 ...

  9. Git使用出错:Couldn‘t reserve space for cygwin‘s heap, Win32

    今天使用Git在命令行下更新代码遇到了问题,起初觉得是自己安装某软件导致冲突,从网上搜索了一下找到类似问题,成功解决问题. 错误信息如下: E:\storm-sql>git pull origi ...

随机推荐

  1. HTTP请求:POST和GET的差异

    1,一般情况下应用目的不同:GET是从服务器上获取数据,POST是向服务器传送数据. 2,将数据提交到服务器的方式不同:GET是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各 ...

  2. (回溯法)ip地址的合理性

    题目: 给定一个只包含数字的字符串,通过返回所有可能有效的IP地址组合来恢复它. 例如: 给定“”, return [“255.255.11.135”,“255.255.111.35”]. (顺序无所 ...

  3. yum 安装telnet

    检测是否安装 rpm -qa |grep telnet 安装 yum install xinetd yum install telnet-server yum -y install telnet 再次 ...

  4. javax.servlet.jsp.PageContext cannot be resolved to a type

    <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifa ...

  5. JavaEE互联网轻量级框架整合开发(书籍)阅读笔记(1):Mybatis和Hibernate概念理解

    一.关键字说明: oop:面向对象 aop:面向切面 ioc:控制反转 orm:对象关系映射 pojo:数据库表映射的java实体类 二.常识说明:1.hibernate和mybatis都属于持久层. ...

  6. 编写高质量代码改善C#程序的157个建议——建议1:正确操作字符串

    最近拜读了陆敏技老师的<编写高质量代码改善C#程序的157个建议>,感觉不错,决定把笔记整理一遍. 建议1: 正确操作字符串 字符串应该是所有编程语言中使用最频繁的一种基础数据类型.如果使 ...

  7. 通过fork函数创建进程的跟踪,分析linux内核进程的创建

    作者:吴乐 山东师范大学 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.实验过程 1.打开gdb, ...

  8. POJ3020 Antenna Placement(二分图最小路径覆盖)

    The Global Aerial Research Centre has been allotted the task of building the fifth generation of mob ...

  9. timer实现Grid自动换行(连续相同的id跳到下一行)

    private { Private declarations } FRow: Integer; procedure SetRow(const Value: Integer); public { Pub ...

  10. DES加密与解密MD5加密帮助类

    public class TrialHelper { //默认密钥向量 private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xA ...