//
// 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的更多相关文章

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

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

  3. 函数式Android编程(II):Kotlin语言的集合操作

    原文标题:Functional Android (II): Collection operations in Kotlin 原文链接:http://antonioleiva.com/collectio ...

  4. 统计分析中Type I Error与Type II Error的区别

    统计分析中Type I Error与Type II Error的区别 在统计分析中,经常提到Type I Error和Type II Error.他们的基本概念是什么?有什么区别? 下面的表格显示 b ...

  5. hdu1032 Train Problem II (卡特兰数)

    题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能.    (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...

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

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

  8. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  9. [LeetCode] Permutations II 全排列之二

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

随机推荐

  1. node二进制安装

    你可能因为重装系统node的npm不管用, 但是node管用, 我不知道为什么, 但是 二进制安装就好了 管他那么多 下面这些网址你就可以了 https://blog.csdn.net/wocaoni ...

  2. 安装 webstorm--->vue

    一.先去官网下载webstorm     https://www.jetbrains.com/ 不论是Mac的还是win得都有相应的版本, 二.再去官网下载git     https://git-sc ...

  3. Java初步学习——2021.10.10每日总结,第五周周日

    (1)今天做了什么: (2)明天准备做什么? (3)遇到的问题,如何解决? 今天继续学习菜鸟教程java字符串实例 5.字符串反转--reverse方法 public class Main { pub ...

  4. Ajax样例

    $.ajax({ url : "newsservlet",//请求地址 dataType : "json",//数据格式 type : "post&q ...

  5. Java(21)内部类

    作者:季沐测试笔记 原文地址:https://www.cnblogs.com/testero/p/15228411.html 博客主页:https://www.cnblogs.com/testero ...

  6. Servlet学习一(Servlet的使用流程)

    一.servlet运行流程 运行流程:浏览器发送请求到服务器,服务器根据url地址在webapps中寻找对应的项目文件夹然后再web.xml中检索对应的servlet,并进行调用二.servlet类写 ...

  7. [Beta]the Agiles Scrum Meeting 2

    会议时间:2020.5.11 20:00 1.每个人的工作 今天已完成的工作 成员 已完成的工作 yjy 修复bug将自动评测改为异步HTTP请求 tq 实现查看.删除测试点功能的后端将自动评测改为异 ...

  8. springboot多配置环境

    在我们的开发过程中,经常会有多套配置环境,比如开发环境(dev),测试环境(test),生产环境(prod)等,在各个环境中我们需要使用到不同的配置,那么在springboot中是如何做到的呢? 1. ...

  9. 深入理解和运用Pandas的GroupBy机制——理解篇

    GroupBy是Pandas提供的强大的数据聚合处理机制,可以对大量级的多维数据进行透视,同时GroupBy还提供强大的apply函数,使得在多维数据中应用复杂函数得到复杂结果成为可能(这也是个人认为 ...

  10. 从零开始的DIY智能浇水应用

    前言 作为一个新世纪打工人,平常也会去养一些花草,来给我的房间增加点绿色和活力,但是常常因为工作忙而忘记一些事情.,毕竟我大部分的时间都是陪伴着电脑的(严正声明:我不是个单身狗!!! (¬◡¬)✧), ...