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. 判断字符串是否没有重复字符
------------------------ 因为字符究竟是什么样的无法确定(比如编码之类的),恐怕是没办法假设使用多大空间(位.数组)来标记出现次数的,集合应该可以但感觉会严重拖慢速度... 还 ...
随机推荐
- 【转】使用cocos2d-console工具转换脚本为字节码
从Cocos2D-X v2.1.4版本开始,增加了Cocos2D-console命令行工具,该工具的其中一个功能是:把.js文件转换为.jsc文件,该文件是字节码格式,可以提高代码的安全性. 使用这个 ...
- 循环列表的Java实现,解决约瑟夫环问题
import java.util.Scanner; /** * 循环列表的Java实现,解决约瑟夫环问题 * * @author LIU * */ public class LinkedList { ...
- Linux下常用压缩格式的压缩与解压方法
.tar 解包: tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) --------------------- ...
- Spring MVC 使用拦截器优雅地实现权限验证功能
在上一篇 SpringAOP 实现功能权限校验功能 中虽然用AOP通过抛异常,请求转发等勉强地实现了权限验证功能,但感觉不是那么完美,应该用拦截器来实现才是最佳的,因为拦截器就是用来拦截请求的,在请求 ...
- 《C标准库》——之<stddef.h>
<stddef.h>,顾名思义,就是标准定义.C语言里这个标准库里定义了一些类型,和宏定义. <stddef.h>的内容: 类型: ptrdiff_t : 是两个指针相减的结果 ...
- c#部分--- 一维数组放到集合中,在从集合中提取输出
输入班级人数,统计人名,性别,年龄: 建立一维数组,并放到集合中: //Console.Write("请输入班级人数:"); //int a = int.Parse(Console ...
- hihoCoder#1015 : KMP算法 (KMP模板)
代码如下: # include<iostream> # include<cstdio> # include<cstring> # include<vector ...
- Learn python the hard way. python test program 2016.04.27
# this will not be printed in python ! print "I could have code like this." # and the comm ...
- 转-OpenJDK源码阅读导航跟编译
OpenJDK源码阅读导航 OpenJDK源码阅读导航 博客分类: Virtual Machine HotSpot VM Java OpenJDK openjdk 这是链接帖.主体内容都在各链接中. ...
- Linux-Nginx之sendfile与上下文切换
今天在看nginx thread pool的时候,频繁的看到sendfile,其实以前也经常看到sendfile,只是我平时选择性的忽视而已... 先说下sendfile,明天在好好聊下nginx 线 ...