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 ...
随机推荐
- Server Tools(服务器工具)
服务器工具 1.发布 # Process: MXD 转 Web 地图 arcpy.MXDToWebMap_server("", "", "" ...
- PAT (Basic Level) Practice (中文)1009 说反话 (20分)
给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出. 输入格式: 测试输入包含一个测试用例,在一行内给出总长度不超过 80 的字符串.字符串由若干单词和若干空格组成,其中单词是由英文字母(大小 ...
- 异构智联Wi-Fi+蓝牙模组,连接快、准、稳!
下班回家打开门,电灯.电视.空调.音响.电动窗帘.扫地机器人--一呼百应,有序开工,原本冰冷的房子立刻变成了温暖港湾.可以说,舒适便捷的智能设备已经完全融入了我们的生活中. 从单一场景.单一设备,到现 ...
- 初学python-day11 函数3
函数 1. global关键字 修改全局变量,声明函数内外使用同一个变量 示例: 1 name = 'xiaoming' 2 3 def test(): 4 global name 5 name = ...
- jq问题
<div id="box"> <p> <span>A</span> <span>B</span> </ ...
- Python在Linux下编译安装报错:Makefile:1141:install
正常情况下执行:./configuremake && make install可以直接安装python,但是在在更新了乌版图后居然报错了!!!检查了一圈,发现乌版图安装了python3 ...
- [敏捷软工团队博客]Beta阶段项目展示
团队成员简介和个人博客地址 头像 姓名 博客园名称 自我介绍 PM 测试 前端 后端 dzx 秃头院的大闸蟹 大闸蟹是1706菜市场里无菜可卖的底层水货.大闸蟹喜欢音乐(但可惜不会),喜欢lol(可惜 ...
- Beta阶段第一次会议
Beta阶段第一次例会 时间:2020.5.16 完成工作 姓名 完成任务 难度 完成度 lm 1.修订网页端信息编辑bug2.修订网页端登录bug(提前完成,相关issue已关闭) 中 100% x ...
- MySQL 8.0安装 + 配置环境变量 + 连接 cmd
MySQL 安装教程 搜索 MySQL,进入官网,找到 download 点击适用于 window community 版本,点击图中第二个 450.7 M 的安装包进行下载 这里有五个选项,选择第二 ...
- 将manjaro作为主力开发系统,我遇到了哪些坑。
首先遇到的问题就是企业微信. 最开始几天,我直接去安装企业微信和微信,安装全都报错了. 无奈之下,只好安装了virtual box,装了一个win7,可以正常使用微信,企业微信,最开始蛋疼的地方是,企 ...