Divide and Conquer
1
2 218 The Skyline Problem 最大堆 遍历节点
public List<int[]> getSkyline(int[][] buildings) {
List<int[]> res = new ArrayList<>();
List<int[]> point = new ArrayList<>();
for (int i = 0; i < buildings.length; i++)
{
point.add(new int[]{buildings[i][0], buildings[i][2]});
point.add(new int[]{buildings[i][1], -buildings[i][2]});
}
Collections.sort(point, (a,b)-> {
if (a[0] != b[0]) return a[0] - b[0];
return b[1] - a[1];});
PriorityQueue<Integer> maxheap = new PriorityQueue<>((a,b)->(b - a));
int cur = 0, pre = 0;
for (int i = 0; i < point.size(); i++)
{
int[] b = point.get(i);
if (b[1] > 0)
{
maxheap.add(b[1]);
cur = maxheap.peek();
}
else
{
maxheap.remove(-b[1]);
cur = maxheap.peek() == null ? 0 : maxheap.peek();
}
if (pre != cur)
{
res.add(new int[]{b[0], cur});
pre = cur;
}
}
return res;
}
3 241 Different Ways to Add Parentheses 找符号,分开
public List<Integer> diffWaysToCompute(String input) {
List<Integer> res = new LinkedList<>();
for (int i = 0; i < input.length(); i++)
{
char c = input.charAt(i);
if (c == '+' || c == '-' || c == '*')
{
String a = input.substring(0, i);
String b = input.substring(i+1);
List<Integer> a1 = diffWaysToCompute(a);
List<Integer> b1 = diffWaysToCompute(b);
for (int x : a1)
{
for (int y : b1)
{
if (c == '+')
{
res.add(x + y);
}
else if (c == '-')
{
res.add(x - y);
}
else if (c == '*')
{
res.add(x * y);
}
}
}
}
}
if (res.size() == 0) res.add(Integer.valueOf(input));
return res;
}
Divide and Conquer的更多相关文章
- [LeetCode] 236. Lowest Common Ancestor of a Binary Tree_ Medium tag: DFS, Divide and conquer
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- [LeetCode] 系统刷题4_Binary Tree & Divide and Conquer
参考[LeetCode] questions conlusion_InOrder, PreOrder, PostOrder traversal 可以对binary tree进行遍历. 此处说明Divi ...
- [LeetCode] 124. Binary Tree Maximum Path Sum_ Hard tag: DFS recursive, Divide and conquer
Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...
- 算法与数据结构基础 - 分治法(Divide and Conquer)
分治法基础 分治法(Divide and Conquer)顾名思义,思想核心是将问题拆分为子问题,对子问题求解.最终合并结果,分治法用伪代码表示如下: function f(input x size ...
- 算法上机题目mergesort,priority queue,Quicksort,divide and conquer
1.Implement exercise 2.3-7. 2. Implement priority queue. 3. Implement Quicksort and answer the follo ...
- 【LeetCode】分治法 divide and conquer (共17题)
链接:https://leetcode.com/tag/divide-and-conquer/ [4]Median of Two Sorted Arrays [23]Merge k Sorted Li ...
- The Divide and Conquer Approach - 归并排序
The divide and conquer approach - 归并排序 归并排序所应用的理论思想叫做分治法. 分治法的思想是: 将问题分解为若干个规模较小,并且类似于原问题的子问题, 然后递归( ...
- [算法]分治算法(Divide and Conquer)
转载请注明:http://www.cnblogs.com/StartoverX/p/4575744.html 分治算法 在计算机科学中,分治法是建基于多项分支递归的一种很重要的算法范式.字面上的解释是 ...
- Divide and Conquer.(Merge Sort) by sixleaves
algo-C1-Introductionhtml, body {overflow-x: initial !important;}html { font-size: 14px; }body { marg ...
- 分治法 - Divide and Conquer
在计算机科学中,分治法是一种很重要的算法.分治法即『分而治之』,把一个复杂的问题分成两个或更多的相同或相似的子问题,再把子问题分成更小的子问题……直到最后子问题可以简单的直接求解,原问题的解即子问题的 ...
随机推荐
- HTML行内元素、块级元素、行内块级元素的特点与区别
元素 HTML 元素指的是从开始标签(start tag)到结束标签(end tag)的所有代码. 元素分类方式 HTML 可以将元素分类方式分为行内元素.块状元素和行内块状元素三种,这三者可以通过设 ...
- composer-laravel-China源和官方源
composer config -g repo.packagist composer https://repo.packagist.org composer config -g repo.packag ...
- 安装mysql apache php smb
1 SMB LinuX下SMB的配置 使用Smb进行连接的命令: smbclient //192.168.128.1/Share 今天要在LINUX之间以及LINUX与WINDOWS之间互相传送文件, ...
- C#使用 SSL Socket 建立 Client 与 Server 连接
当 Client 与 Server 需要建立一个沟通的管道时可以使用 Socket 的方式建立一个信道,但是使用单纯的 Socket 联机信道可能会担心传输数据的过程中可能被截取修改因而不够安全,为了 ...
- Programming In Lua 第八章
1, 也就是说,lua虽然会把代码预编译成中间码,以提高运行速度.但其会在程序运行过程中需要编译器,所以其仍然是解释型语言.loadfile会加载一个文件并将其编译成中间码,并返回一个函数. 2, 3 ...
- 关于C51 keil使用中.c文件的链接心得
这个问题一直烦了我很久,使用C51 keil进行.c文件链接的时候,老是报错诸如下面的信息: *** ERROR L104: MULTIPLE PUBLIC DEFINITIONS SYMBOL: a ...
- Ural 2070:Interesting Numbers(思维)
http://acm.timus.ru/problem.aspx?space=1&num=2070 题意:A认为如果某个数为质数的话,该数字是有趣的.B认为如果某个数它分解得到的因子数目是素数 ...
- HDU 1394:Minimum Inversion Number(线段树区间求和单点更新)
http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number Problem Description The in ...
- Flutter学习笔记(5)--Dart运算符
如需转载,请注明出处:Flutter学习笔记(5)--Dart运算符 先给出一个Dart运算符表,接下来在逐个解释和使用.如下: 描述 ...
- 小白开学Asp.Net Core 《七》
小白开学Asp.Net Core <七> — — 探究中间件(MiddleWare) 1.何为中间件? 中间件是组装到应用程序管道中以处理请求和响应的家伙,管道中的每个组件都要满足以下两个 ...