//
// 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. 霍尔效应实验 execl数据处理

    execl 函数 =POWER() /*幂次*/ =ROUND() /*保留小数点后几位*/ =SQRT() /*开平方根*/ =POWER( x, 1/3 ) /*开立方根*/ =COUNTA() ...

  2. Sentinel-Go 源码系列(一)|开篇

    大家好呀,打算写一个 Go 语言组件源码分析系列,一是为了能学习下 Go 语言,看下别人是怎么写 Go 的,二是也掌握一个组件. 本次选择了 Sentinel-Go,一是对 Java 版本的 Sent ...

  3. 自定义ConditionalOnXX注解

    一.Conditional注解介绍 对SpringBoot有足够了解的小伙伴应该都用过Conditional系列注解,该注解可用在类或者方法上用于控制Bean的初始化. 常用的Conditional注 ...

  4. 耗时一个月,整理出这份Hadoop吐血宝典

    本文目录: 一.HDFS 二.MapReduce 三.Yarn 四.Hadoop3.x 新特性 五.Hadoop 大厂面试真题解析 Hadoop 涉及的知识点如下图所示,本文将逐一讲解: 本文档参考了 ...

  5. SharkCTF2021 pwn“初见”1

    (无内鬼 今日不想学了 水一篇) nc nc nc easyoverflow Intoverflow

  6. 数据结构与算法-基础(十一)AVL 树

    AVL 树 是最早时期发明的自平衡二叉搜索树之一.是依据它的两位发明者的名称命名. AVL 树有一个重要的属性,即平衡因子(Balance Factor),平衡因子 == 某个节点的左右子树高度差. ...

  7. [no_code][Beta] 中期组内总结

    $( "#cnblogs_post_body" ).catalog() 目前scrum meeting beta阶段目前共7次.在alpha阶段我们博客发布时间比较匆忙,是扣分项, ...

  8. WebGL着色器渲染小游戏实战

    项目起因 经过对 GLSL 的了解,以及 shadertoy 上各种项目的洗礼,现在开发简单交互图形应该不是一个怎么困难的问题了.下面开始来对一些已有业务逻辑的项目做GLSL渲染器替换开发. 起因是看 ...

  9. 零基础入门该如何实现C 语言面向对象编程(很有帮助)

    零基础如果更快更好的入门C语言,如何在枯燥的学习中找到属于自己的兴趣,如果把学习当成一种事务性的那以后的学习将会很难有更深入的进步,如果带着乐趣来完成学习那将越学越有意思这样才会让你有想要更深入学习的 ...

  10. Spring Security:Authorization 授权(二)

    Authorization 授权 在更简单的应用程序中,身份验证可能就足够了:用户进行身份验证后,便可以访问应用程序的每个部分. 但是大多数应用程序都有权限(或角色)的概念.想象一下:有权访问你的面向 ...