* 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. code::blocks(版本10.05) 配置opencv2.4.3

    (1)首先下载opencv2.4.3, 解压缩到D:下: (2)配置code::blocks, 具体操作如下: 第一步, 配置compiler, 操作步骤为Settings  -> Compil ...

  2. jquery保存用户名和密码到cookie里面

    http://blog.sina.com.cn/s/blog_633ad0ae0101guij.html

  3. tulterbot遥感操作使用Interactive Markers--12

    原创博客:转载请表明出处:http://www.cnblogs.com/zxouxuewei/ 1.安装ros indigo功能包: sudo apt-get install ros-indigo-t ...

  4. #梦断代码#first blood

    前几天大致瞅了一眼,哇~原来不是啃代码的书,像是本小说,读起来很舒服,翻译的相当贴近生活,“这边厢......那边厢......”这类的语言很喜欢,还没看多少,对博客园比较新奇就先写个博客签个到,每天 ...

  5. liunx之:wps for liunx的安装经验

    首先是下载正确的安装包 WPS For Linux : 社区下载:http://community.wps.cn/download/ 社区最新包下载:http://wps-community.org/ ...

  6. Linux平台下利用系统接口函数按照行读写文件

    要求:支持大文件(1M)一次性读入 源代码如下: #include<stdio.h> #include<fcntl.h> #include<stdlib.h> #i ...

  7. QueryRunner使用

    在相继学习了JDBC和数据库操作之后,我们明显感到编写JDBC代码并非一件轻松的事儿.为了帮助我们更高效的学习工作,从JDBC的繁重代码中解脱出来,老佟给我们详尽介绍了一个简化JDBC操作的组件——D ...

  8. JSBinding + SharpKit / Important Notes

    Serialization of List<T> is not supported. 1 public int v; // SUPPORTED 2 public GameObject go ...

  9. java_queue

    队列是一种特殊的线性表,先进先出(first in first out)FIFO,它只允许在表的前端(front)进行删除操作,只允许在表的后端(rear)进行插入操作. 实际应用:排队等待公交车,银 ...

  10. linux 安装phpMyAdmin

    1. 首先安装 php mysql  nginx 2. 下载phpMyadmin wget  http://sourceforge.net/projects/phpmyadmin/files/phpM ...