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

  1. LintCode A + B Problem

    原题链接在这里:http://www.lintcode.com/en/problem/a-b-problem/ 不让用 数学运算符,就用位运算符. a的对应位 ^ b的对应位 ^ carry 就是re ...

  2. Post Office Problem

    Description There are n houses on a line. Given an array A and A[i] represents the position of i-th  ...

  3. [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 ...

  4. 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 ...

  5. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  6. Lintcode 85. 在二叉查找树中插入节点

    -------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...

  7. Lintcode 166. 主元素

    ----------------------------------- Moore's voting algorithm算法:从一个集合中找出出现次数半数以上的元素,每次从集合中去掉一对不同的数,当剩 ...

  8. Lintcode 166. 链表倒数第n个节点

    ----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * De ...

  9. Lintcode 157. 判断字符串是否没有重复字符

    ------------------------ 因为字符究竟是什么样的无法确定(比如编码之类的),恐怕是没办法假设使用多大空间(位.数组)来标记出现次数的,集合应该可以但感觉会严重拖慢速度... 还 ...

随机推荐

  1. Eclipse Java 开发平台实用技巧

    前言 在使用Eclipse开发Java程序的使用,有很多实用的技巧,能大大提高开发效率. 本文将介绍一部分技巧.更多的心得还得在具体项目中慢慢掌握,熟悉. 初始设定 这些具体的设置方法这里不说,网上很 ...

  2. 十 SSH

    一 Struts 1. 定义:该框架使用 MVC 设计模式开发程序 2. 框架概览: 二 Hibernate 1. 作用:提供了利用面向对象的思想来操作关系型数据的接口 2. 框架图示: 三 Spri ...

  3. JS基础知识(作用域/垃圾管理)

    1.js没有块级作用域 if (true) { var color = “blue”; } alert(color); //”blue” for (var i=0; i < 10; i++){ ...

  4. C# Form 非法字符

    头部加上 EnableEventValidation="false" ValidateRequest="false"

  5. Java 正则表达式学习总结和一些小例子

    从Java1.4起,Java核心API就引入了java.util.regex程序包,它是一种有价值的基础工具,可以用于很多类型的文本处理, 如匹配,搜索,提取和分析结构化内容. java.util.r ...

  6. codeforces hungry sequence 水题

    题目链接:http://codeforces.com/problemset/problem/327/B 这道题目虽然超级简单,但是当初我还真的没有想出来做法,囧,看完别人的代码恍然大悟. #inclu ...

  7. 《Java程序设计》第6周学习总结

    学号20145220 <Java程序设计>第6周学习总结 教材学习内容总结 InputStream与OutputStream 10.1.1串流设计的概念 Java将输入/输出抽象化为串流, ...

  8. ..c++中用c语言的输入法

    题目: 竞选时,要求选民在n个候选人中选择,n个人的名字为 A,B,C,D--连续n个大写字母,如果选择n个人名字之外的人员,则为废票.   统计时以输入'#'为结束标记.请按候选人的得票数目从大到小 ...

  9. JSBinding+SharpKit / MonoBehaviour替换成JSComponent原理

    Unity 是基于组件式的开发,gameObject 身上可以绑定任意个脚本.每个脚本组成 gameObject 的一个部分. 脚本里通过添加预定义好的函数来执行自己的任务.比如Awake,用于初始化 ...

  10. Android Studio导入Project的方法

    Android Studio到现在已经发展到0.8+的版本了,最近也在试着使用它,原因是多方面的,一个毕竟是未来的趋势,二则是github上越来越多的大牛开源项目都是基于Android Studio的 ...