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 ...
随机推荐
- Unity——资源文件夹介绍
Unity资源文件夹介绍 1.编辑时 在Asset文件下存在Resources和SteamingAsset文件夹: Resources 只读不可修改,打包时直接写死,没有办法通过热更新替换资源: 可以 ...
- SpringCloud-初见
目录 前言 微服务概述 微服务与微服务架构 微服务优缺点 微服务技术栈 为什么选择SpringCloud作为微服务架构 SpringCloud入门 SpringCloud和SpringBoot的关系 ...
- Python语法1
变量 命名规则 变量名必须是大小写英文字母.数字或下划线 _ 的组合,不能用数字开头,并且对大小写敏感 变量赋值 同一变量可以反复赋值,而且可以是不同类型的变量 i=2; i="name&q ...
- Golang通脉之错误处理
在实际工程项目中,总是通过程序的错误信息快速定位问题,但是又不希望错误处理代码写的冗余而又啰嗦.Go语言没有提供像Java.C#语言中的try...catch异常处理方式,而是通过函数返回值逐层往上抛 ...
- djago后台管理页面
from django.contrib import admin from blogtest.models import * #修改网页title和站点header.+ admin.site.site ...
- the Agiles Scrum Meeting 6
会议时间:2020.4.14 20:00 1.每个人的工作 今天已完成的工作 增量组:开发广播正文展开收起功能 issues:增量组:广播正文展开收起功能实现 完善组:修复冲刺部分的bug issue ...
- BUAA 软工 个人博客作业(一)
项目 内容 这个作业属于哪个课程 2020春季计算机学院软件工程(罗杰 任健) 这个作业的要求在哪里 个人博客作业 我在这个课程的目标是 通过阅读<构建之法>大致了解软件工程 这个作业在哪 ...
- springboot整合rabbitmq实现生产者消息确认、死信交换器、未路由到队列的消息
在上篇文章 springboot 整合 rabbitmq 中,我们实现了springboot 和rabbitmq的简单整合,这篇文章主要是对上篇文章功能的增强,主要完成如下功能. 需求: 生产者在启 ...
- 2021.9.25考试总结[NOIP模拟61]
终于有点阳间题了然而挂了60pts 哈哈 T1 交通 类似简单题,限制看似很复杂,但不难发现当确定一条边是否被删后会产生裙带关系,很多边会跟着自动被确定是否被删. 仔细观察可以得出这种关系会构成偶环结 ...
- Spring Security:Authentication 认证(一)
1. Spring Security 简介 在 Spring 生态系统中,为他的项目增加安全性,你可以借助 Spring Security 库来做到这一点. 那什么是 Spring Security? ...