Heap — 20181120
363. Trapping Rain Water
- public class Solution {
- /**
- * @param heights: a list of integers
- * @return: a integer
- */
- public int trapRainWater(int[] heights) {
- // write your code here
- if (heights == null || heights.length == 0) {
- return 0;
- }
- int start = 0;
- int end = heights.length - 1;
- int maxLeft = Integer.MIN_VALUE;
- int maxRight = Integer.MIN_VALUE;
- int sum = 0;
- while (start < end) {
- maxLeft = heights[start] > maxLeft ? heights[start] : maxLeft;
- maxRight = heights[end] > maxRight ? heights[end] : maxRight;
- if (maxLeft < maxRight) {
- sum += maxLeft - heights[start];
- start++;
- } else {
- sum += maxRight - heights[end];
- end--;
- }
- }
- return sum;
- }
- }
364. Trapping Rain Water II
- class Point {
- int x;
- int y;
- int height;
- public Point(int x, int y, int height) {
- this.x = x;
- this.y = y;
- this.height = height;
- }
- }
- public class Solution {
- /**
- * @param heights: a matrix of integers
- * @return: an integer
- */
- int[] dx = {0, 1, 0, -1};
- int[] dy = {1, 0, -1, 0};
- public int trapRainWater(int[][] heights) {
- // write your code here
- if (heights == null || heights.length == 0 || heights[0].length == 0) {
- return 0;
- }
- int r = heights.length;
- int c = heights[0].length;
- boolean[][] visited = new boolean[r][c];
- Comparator<Point> minComparator = new Comparator<Point>() {
- @Override
- public int compare(Point o1, Point o2) {
- return o1.height - o2.height;
- }
- };
- PriorityQueue<Point> minHeap = new PriorityQueue<>(minComparator);
- for (int i = 0; i < r; i++) {
- minHeap.add(new Point(i, 0, heights[i][0]));
- visited[i][0] = true;
- minHeap.add(new Point(i, c - 1, heights[i][c - 1]));
- visited[i][c - 1] = true;
- }
- for (int j = 0; j < c; j++) {
- minHeap.add(new Point(0, j, heights[0][j]));
- visited[0][j] = true;
- minHeap.add(new Point(r - 1, j, heights[r - 1][j]));
- visited[r - 1][j] = true;
- }
- int sum = 0;
- while (!minHeap.isEmpty()) {
- Point point = minHeap.poll();
- for (int i = 0; i < 4; i++) {
- int nx = point.x + dx[i];
- int ny = point.y + dy[i];
- if (!isValid(nx, ny, heights, visited)) {
- continue;
- }
- visited[nx][ny] = true;
- minHeap.add(new Point(nx, ny, Math.max(heights[nx][ny], point.height)));
- sum += Math.max(heights[nx][ny], point.height) - heights[nx][ny];
- }
- }
- return sum;
- }
- public boolean isValid(int x, int y, int[][] heights, boolean[][] visited) {
- if (x < 0 || x > heights.length - 1 || y < 0 || y > heights[0].length - 1) {
- return false;
- }
- if (visited[x][y]) {
- return false;
- }
- return true;
- }
- }
360. Sliding Window Median 具体思路见 81. Find Median from Data Stream
- public class Solution {
- /**
- * @param nums: A list of integers
- * @param k: An integer
- * @return: The median of the element inside the window at each moving
- */
- private PriorityQueue<Integer> minHeap;
- private PriorityQueue<Integer> maxHeap;
- private int maxSize = 0;
- private int minSize = 0;
- public List<Integer> medianSlidingWindow(int[] nums, int k) {
- // write your code here
- if (nums == null || nums.length < k || k <= 0) {
- return new ArrayList<>();
- }
- minHeap = new PriorityQueue<>();
- maxHeap = new PriorityQueue<>(new Comparator<Integer>() {
- @Override
- public int compare(Integer o1, Integer o2) {
- return o2.compareTo(o1);
- }
- });
- List<Integer> res = new ArrayList<>();
- for (int i = 0; i < k; i++) {
- addNumber(nums[i]);
- }
- res.add(getMedian());
- for (int j = k; j < nums.length; j++) {
- slideByWindow(nums[j], nums[j - k]);
- res.add(getMedian());
- }
- return res;
- }
- public void addNumber(int num) {
- maxHeap.add(num);
- maxSize++;
- if (maxSize - minSize <= 1) {
- if (minHeap.isEmpty()) {
- return;
- }
- if (maxHeap.peek() > minHeap.peek()) {
- int maxTemp = maxHeap.poll();
- int minTemp = minHeap.poll();
- minHeap.add(maxTemp);
- maxHeap.add(minTemp);
- }
- return;
- }
- minHeap.add(maxHeap.poll());
- minSize++;
- maxSize--;
- }
- public int getMedian() {
- return maxHeap.peek();
- }
- public void slideByWindow(int toAdd, int toRemove) {
- if (toRemove <= getMedian()) {
- maxHeap.remove(toRemove);
- maxSize--;
- } else {
- minHeap.remove(toRemove);
- minSize--;
- }
- addNumber(toAdd);
- }
- }
Heap — 20181120的更多相关文章
- java head space/ java.lang.OutOfMemoryError: Java heap space内存溢出
上一篇JMX/JConsole调试本地还可以在centos6.5 服务器上进行监控有个问题端口只开放22那么设置的9998端口 你怎么都连不上怎么监控?(如果大神知道还望指点,个人见解) 线上项目出现 ...
- Java 堆内存与栈内存异同(Java Heap Memory vs Stack Memory Difference)
--reference Java Heap Memory vs Stack Memory Difference 在数据结构中,堆和栈可以说是两种最基础的数据结构,而Java中的栈内存空间和堆内存空间有 ...
- [数据结构]——堆(Heap)、堆排序和TopK
堆(heap),是一种特殊的数据结构.之所以特殊,因为堆的形象化是一个棵完全二叉树,并且满足任意节点始终不大于(或者不小于)左右子节点(有别于二叉搜索树Binary Search Tree).其中,前 ...
- Windbg Extension NetExt 使用指南 【3】 ---- 挖掘你想要的数据 Managed Heap
摘要 : NetExt中有两个比较常用的命令可以用来分析heap上面的对象. 一个是!wheap, 另外一个是!windex. !wheap 这个命令可以用于打印出heap structure信息. ...
- JAVA Shallow heap & Retained heap
最近在研究内存泄漏的问题,在使用MAT工具中发现了Shallow heap & Retained heap,不懂. 然后在网上找了一些资料. Shallow Size 对象自身占用的内存大小, ...
- 笔记:程序内存管理 .bss .data .rodata .text stack heap
1.未初始化的全局变量(.bss段) bss段用来存放 没有被初始化 和 已经被初始化为0 的全局变量.如下例代码: #include<stdio.h> int bss_array[102 ...
- STL heap usage
简介 heap有查找时间复杂度O(1),查找.插入.删除时间复杂度为O(logN)的特性,STL中heap相关的操作如下: make_heap() push_heap() pop_heap() sor ...
- Nodemanager Out of heap memory[fix bug全过程]
问题: 自己写了一个yarn上的application,发现nodemanager过段时间,会out of memory退出,把nodemanager的heap memory从1G增大到2G也是无法避 ...
- Git使用出错:Couldn‘t reserve space for cygwin‘s heap, Win32
今天使用Git在命令行下更新代码遇到了问题,起初觉得是自己安装某软件导致冲突,从网上搜索了一下找到类似问题,成功解决问题. 错误信息如下: E:\storm-sql>git pull origi ...
随机推荐
- HTTP请求:POST和GET的差异
1,一般情况下应用目的不同:GET是从服务器上获取数据,POST是向服务器传送数据. 2,将数据提交到服务器的方式不同:GET是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各 ...
- (回溯法)ip地址的合理性
题目: 给定一个只包含数字的字符串,通过返回所有可能有效的IP地址组合来恢复它. 例如: 给定“”, return [“255.255.11.135”,“255.255.111.35”]. (顺序无所 ...
- yum 安装telnet
检测是否安装 rpm -qa |grep telnet 安装 yum install xinetd yum install telnet-server yum -y install telnet 再次 ...
- javax.servlet.jsp.PageContext cannot be resolved to a type
<dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifa ...
- JavaEE互联网轻量级框架整合开发(书籍)阅读笔记(1):Mybatis和Hibernate概念理解
一.关键字说明: oop:面向对象 aop:面向切面 ioc:控制反转 orm:对象关系映射 pojo:数据库表映射的java实体类 二.常识说明:1.hibernate和mybatis都属于持久层. ...
- 编写高质量代码改善C#程序的157个建议——建议1:正确操作字符串
最近拜读了陆敏技老师的<编写高质量代码改善C#程序的157个建议>,感觉不错,决定把笔记整理一遍. 建议1: 正确操作字符串 字符串应该是所有编程语言中使用最频繁的一种基础数据类型.如果使 ...
- 通过fork函数创建进程的跟踪,分析linux内核进程的创建
作者:吴乐 山东师范大学 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.实验过程 1.打开gdb, ...
- POJ3020 Antenna Placement(二分图最小路径覆盖)
The Global Aerial Research Centre has been allotted the task of building the fifth generation of mob ...
- timer实现Grid自动换行(连续相同的id跳到下一行)
private { Private declarations } FRow: Integer; procedure SetRow(const Value: Integer); public { Pub ...
- DES加密与解密MD5加密帮助类
public class TrialHelper { //默认密钥向量 private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xA ...