PaintHouse II
//
// Created by Administrator on 2021/7/27.
//
#ifndef C__TEST01_PAINTHOUSE_HPP
#define C__TEST01_PAINTHOUSE_HPP
#include <vector>
class PaintHouse {
/*有一排n栋房子,每一栋要漆成K种颜色中的一种
* 任何两栋相邻的房子不能漆成同一种颜色
* 房子i染成第j种颜色的花费是cost[i][j]
* 问最少花费多少钱油漆这些房子
* 输入
* -N = 3, K = 3
* -cost = {
* {14, 2, 11},
* {11, 14, 15},
* {14, 3, 10}
* }
* */
public:
PaintHouse(vector<vector<int>> costN);
int PaintHouseDP(vector<vector<int>> &cost);
private:
vector<vector<int>> cost;
};
PaintHouse::PaintHouse( vector<vector<int>> costN) :
cost(costN){
cost.resize(costN.size());
for(int i; i<costN.size(); ++i){
cost[i].resize(costN[i].size());
}
}
int PaintHouse::PaintHouseDP( vector<vector<int>> &cost) {
if(cost.size() == 0 || cost[0].size() == 0){
return 0;
}
int N = cost.size();
int K = cost[0].size();
vector<vector<int>> f(N); //f[i][j]表示第i个房子漆成第j种颜色的最小花费
for(int i = 0;i < f.size();i++){
f[i].resize(K);
}
f[0][0] = 0;
int min1 = INT_MAX;
int j1, j2;
int min2 = INT_MAX;
for(int j = 0; j < f[0].size(); ++j){
f[0][j] = cost[0][j];
}
for(int i = 1; i < f.size(); ++i){
min1 = INT_MAX;
min2 = INT_MAX;
j1 = j2 = 0;
for(int j = 0; j < f[i].size() ;++j){
if(f[i-1][j] < min1){
min2 = min1;
j2 = j1;
min1 = f[i-1][j];
j1 = j;
}else{
if(f[i-1][j]<min2){
min2 = f[i-1][j];
j2 = j;
}
}
}
for (int j = 0; j < f[i].size(); ++j) {
f[i][j] = INT_MAX;
if(i>0){
if(j == j1){
f[i][j] = min2 + cost[i][j];
}else{
f[i][j] = min1 + cost[i][j];
}
}
}
}
int res = INT_MAX;
for(int j = 0; j<f[N-1].size(); ++j){
if(f[N-1][j] < res) res = f[N-1][j];
}
return res;
}
#endif //C__TEST01_PAINTHOUSE_HPP
PaintHouse II的更多相关文章
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II
题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...
- 函数式Android编程(II):Kotlin语言的集合操作
原文标题:Functional Android (II): Collection operations in Kotlin 原文链接:http://antonioleiva.com/collectio ...
- 统计分析中Type I Error与Type II Error的区别
统计分析中Type I Error与Type II Error的区别 在统计分析中,经常提到Type I Error和Type II Error.他们的基本概念是什么?有什么区别? 下面的表格显示 b ...
- hdu1032 Train Problem II (卡特兰数)
题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能. (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...
- [LeetCode] Guess Number Higher or Lower II 猜数字大小之二
We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...
- [LeetCode] Number of Islands II 岛屿的数量之二
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
随机推荐
- vue3 专用 indexedDB 封装库,基于Promise告别回调地狱
IndexedDB 的官网 https://developer.mozilla.org/zh-CN/docs/Web/API/IndexedDB_API 这个大概是官网吧,原始是英文的,现在陆续是出中 ...
- css3新增文本属性
css3新增属性 边框属性 背景属性 文本属性 颜色属性 文本属性 属性 说明 text-shadow 为文字添加阴影 box-shadow 在元素的框架上添加阴影效果 text-overflow 确 ...
- ORM框架查询数据库时返回指定的字段
django model.objects.filter() 查询指定字段 1.model.objects.filter().values('field_name'),单个字段 2.model.obje ...
- python中函数里面冒号和函数后面的箭头是什么含义
函数里参数后的冒号其实是参数的类型建议,但是只是建议,就算你不按约定传也不会报错.而后面的箭头,则是函数返回值的类型建议.
- Fikker 管理平台弱口令
官网:www.fikker.com 应用介绍:Fikker 是一款面向 CDN/站长 的专业级网站缓存(Webcache)和反向代理服务器软件(Reverse Proxy Server). 发现过程: ...
- NX开发 刀路生成
此段是可以生成程序的完整代码,只有从坐标(10,10,10)到(500,500,500)一根刀轨.motion_ptr->feed_value 的值为0时生成G00,非0时生成G01.此代码只有 ...
- 《基于SIRS模型的行人过街违章传播研究》
My Focus: 行人违章过街 这一行为的传播与控制 Behavior definition in this paper: 人在生活中表现出来的生活态度及具体的生活方式 Title: Researc ...
- Python课程笔记 (五)
今天主要学习图形用户界面,更多的还是要我们自己去实际操作,课仿佛上了一半就完了,分享一下课程(这里在SixthClass)的源码: https://gitee.com/wang_ming_er/pyt ...
- Codeforces 1009E Intercity Travelling | 概率与期望
题目链接 题目大意: 一个人要从$A$地前往$B$地,两地相距$N$千米,$A$地在第$0$千米处,$B$地在第$N$千米处. 从$A$地开始,每隔$1$千米都有$\dfrac{1}{2}$的概率拥有 ...
- oracle 修改表空间名
1.登录使用sys用户登录 sqlplus sys/ as sysdba 2.修改表空间名字 SQL> alter tablespace 旧表空间名 rename to 新表空间名; 表空间已更 ...