LintCode "Post Office Problem" !!!
* Non-intuitive state design
class Solution
{
public:
/**
* @param A an integer array
* @param k an integer
* @return an integer
*/
int postOffice(vector<int>& A, int k)
{
int n = A.size();
sort(A.begin(), A.end()); // Cost btw. 2 houses i-j with 1 post-office - built in the mid
vector<vector<int>> w(n + , vector<int>(n + ));
for(int i = ; i <= n; i ++)
{
w[i][i] = ;
for(int j = i + ; j <= n; j ++)
{
// Check both odd\even. It holds.
w[i][j] = w[i][j-] + A[j - ] - A[(i + j) / - ];
}
} // main DP
vector<vector<int>> dp(n + , vector<int>(k + ));
for(int i = ; i <= n; i++)
{
dp[i][] = w[][i];
}
for(int i = ; i <= k; i ++) // post-offices
{
for(int j = n; j > i; j --) // houses. Low j sets high j
{
dp[j][i] = INT_MAX;
for(int x = i - ; x < j; x ++)
{
dp[j][i] = min(dp[j][i], dp[x][i-] + w[x + ][j]);
}
}
} return dp[n][k];
}
};
TODO: DP optimized to O(n^2)
LintCode "Post Office Problem" !!!的更多相关文章
- LintCode A + B Problem
原题链接在这里:http://www.lintcode.com/en/problem/a-b-problem/ 不让用 数学运算符,就用位运算符. a的对应位 ^ b的对应位 ^ carry 就是re ...
- Post Office Problem
Description There are n houses on a line. Given an array A and A[i] represents the position of i-th ...
- [LintCode] Nuts & Bolts Problem 螺栓螺母问题
Given a set of n nuts of different sizes and n bolts of different sizes. There is a one-one mapping ...
- Lintcode: Nuts & Bolts Problem
Given a set of n nuts of different sizes and n bolts of different sizes. There is a one-one mapping ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- Lintcode 85. 在二叉查找树中插入节点
-------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...
- Lintcode 166. 主元素
----------------------------------- Moore's voting algorithm算法:从一个集合中找出出现次数半数以上的元素,每次从集合中去掉一对不同的数,当剩 ...
- Lintcode 166. 链表倒数第n个节点
----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * De ...
- Lintcode 157. 判断字符串是否没有重复字符
------------------------ 因为字符究竟是什么样的无法确定(比如编码之类的),恐怕是没办法假设使用多大空间(位.数组)来标记出现次数的,集合应该可以但感觉会严重拖慢速度... 还 ...
随机推荐
- URL结尾反斜杠对SEO的影响(转)
开始纠结网站URL加不加反斜杠对SEO的影响,还有些人把这个反斜杠说的神乎其神,我擦,本人手贱百度了一下,果然“博大精深”,敬请参考! 从百度站长平台的外链分析里,我们可以看到,一些目录结构的URL, ...
- android内嵌入webview导致闪退
这里碰到的是各种闪退情况之一,webview退出后,程序里立马需要申请内存空间做别的事情,这时内存不够就会闪退,做法就是延时个几百毫秒,在这段时间内让java把该回收的内存都回收,然后延时到了再做接下 ...
- How to adjust OOM score for a process?
转载自http://www.dbasquare.com/kb/how-to-adjust-oom-score-for-a-process/ How to adjust OOM score for a ...
- 计算机中如何表示数字-07IEEE754浮点数标准
由于不同机器所选用的基数.尾数位长度和阶码位长度不同,因此对浮点数的表示有较大差别,这不利于软件在不同计算机之间的移植.为此,美国IEEE(电器及电子工程师协会)提出了一个从系统角度支持浮点数的表示方 ...
- poj 3159 dijkstra 最短路
Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the he ...
- vimrc配置文件_version1.0_+pathogen, taglist, wordcomplete插件说明
为了表示对Ruchee的感谢,首先这是Ruchee的个人网站:http://www.ruchee.com/index.html,他的以前很多的代码都放到Git里面了,里面有链接. 看了整整一天,刚开始 ...
- oracle锁机制
1 前言 数据库大并发操作要考虑死锁和锁的性能问题.看到网上大多语焉不详(尤其更新锁),所以这里做个简明解释,为下面描述方便,这里用T1代表一个数据 库执行请求,T2代表另一个请求,也可以理解为T1为 ...
- leetcode 130. Surrounded Regions----- java
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
- Discuz论坛搭建过程
1.系统环境 操作系统版本:CentOS Linux 5.7 内核版本:2.6.18-274.el5 arch:x86_64 apache版本:Apache/2.4.6 (Unix) mysql版本 ...
- android loginDemo +WebService用户登录验证
android loginDemo +WebService用户登录验证 本文是基于android4.0下的loginActivity Demo和android下的Webservice实现的.l ...