ColorCostDP.hpp

//
// Created by Administrator on 2021/7/21.
// #ifndef C__TEST01_COLORCOSTDP_HPP
#define C__TEST01_COLORCOSTDP_HPP #include <vector> class ColorCostDP {
public:
ColorCostDP(vector<vector<int>> costN, int N);
void printData();
int algorithmDP(vector<vector<int>> &cost, int &N);
private:
int N; // array of bulidings
vector<vector<int>> cost;
};
ColorCostDP::ColorCostDP(vector<vector<int>> costN, int N):
cost(costN), N(N)
{
cost.resize(costN.size());
for (int i = 0; i < costN.size(); ++i) {
cost[i].resize(costN.size());
}
} void ColorCostDP::printData() {
for(int i; i<cost.size();++i){
for (int j = 0; j < cost[i].size(); ++j) {
cout<<cost[i][j]<<" "; }
}
} int ColorCostDP::algorithmDP(vector<vector<int>> &cost, int &N){
vector<vector<int>> f;
f.resize(N+1); //f[1] is the first buliding
for(int i = 0; i<f.size(); ++i)
f[i].resize(3); //color of every buliding for(int i = 0; i<f.size(); ++i){
if(i == 0) {
f[0][0] = f[0][1] = f[0][2] = 0; //Initialization
continue;
}
if(i == 1) {
f[i][0] = cost[i-1][0];
f[i][1] = cost[i-1][1];
f[i][2] = cost[i-1][2]; //Initialization
continue;
}
for(int j = 0; j < 3; ++j){
f[i][j] = INT_MAX;
for(int k = 0; k < 3; ++k){
if(j == k) continue;
if(f[i-1][k] + cost[i-1][j]<f[i][j])
f[i][j] = f[i-1][k] + cost[i-1][j];
}
}
}
int result = INT_MAX;
for(int i = 0; i<3; i++){
if(f[f.size()-1][i] < result)
result = f[f.size()-1][i];
}
return result;
} #endif //C__TEST01_COLORCOSTDP_HPP

main.cpp

#include <iostream>
using namespace std; /*
* 有一排N栋房子,每栋房子要漆成3种颜色中的一种:红蓝绿
任何相邻的两栋房子不能漆成同样的颜色
第i栋房子要染成红色、蓝色和绿色的花费分别为cost[i][0] cost[i][1] cost[i][2]
问最少花多少钱
例子:
输入:
- N = 3
- Cost = [[14, 2, 11], [11, 14, 5], [14, 3, 10]]
-输出:10
* */
#include "ColorCostDP.hpp" int main() {
vector<vector<int>> cost = {
{14, 2, 11},
{11, 14, 5},
{14, 3, 10},
};
int N = 4;
ColorCostDP ccdp(cost, N); //ccdp.printData();
int result;
result = ccdp.algorithmDP(cost, N); cout << result << endl; return 0;
}

PaintHouse I的更多相关文章

  1. PaintHouse II

    // // Created by Administrator on 2021/7/27. // #ifndef C__TEST01_PAINTHOUSE_HPP #define C__TEST01_P ...

  2. LintCode刷题笔记-- PaintHouse 1&2

    标签: 动态规划 题目描述: There are a row of n houses, each house can be painted with one of the k colors. The ...

  3. [LeetCode] Paint House 粉刷房子

    There are a row of n houses, each house can be painted with one of the three colors: red, blue or gr ...

  4. LeetCode Paint House

    原题链接在这里:https://leetcode.com/problems/paint-house/ 题目: There are a row of n houses, each house can b ...

  5. 【Todo】所有Locked的题目的分析解答

    下面这个链接有比较全的leetcode题目包括锁的 http://www.cnblogs.com/grandyang/p/4606334.html https://leetcode.com/probl ...

  6. 256. Paint House

    题目: There are a row of n houses, each house can be painted with one of the three colors: red, blue o ...

  7. 【LeetCode】1165. Single-Row Keyboard 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...

  8. 【LeetCode】256. Paint House 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...

随机推荐

  1. The type name or alias SqlServer could not be resolved.Please check your configuration

    The type name or alias SqlServer could not be resolved.Please check your configuration file.... 检查一下 ...

  2. IP包头结构

    版本号(Version): 长度4比特.标识目前采用的IP协议的版本号.一般的值为0100(IPv4),0110(IPv6) IP包头长度(Header Length): 长度4比特.这个字段的作用是 ...

  3. netty系列之:让TLS支持http2

    目录 简介 TLS的扩展协议NPN和ALPN SslProvider ApplicationProtocolConfig 构建SslContext ProtocolNegotiationHandler ...

  4. 以太坊web3开发初步学习

    以太坊web3开发初步学习 此文是对https://learnblockchain.cn/2018/04/15/web3-html/的学习再理解. 以太坊智能合约通过使用web3.js前端和智能合约交 ...

  5. 三分钟极速体验:Java版人脸检测

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  6. 微信小程序添加外部地图服务数据

    先上效果: 缘起 使用微信小程序做地图相关功能的时候,有个需求是需要接入自己发布的地图服务.查看微信小程序地图组件文档,发现它对地图相关的支持很少,只有一些基础功能,比如添加点.线.面.气泡和一些常规 ...

  7. UltraSoft Scrum Meeting 博客汇总

    一.Alpha阶段 UltraSoft - Alpha - Scrum Meeting 1 UltraSoft - Alpha - Scrum Meeting 2 UltraSoft - Alpha ...

  8. RSA加密——前端JSEncrypt

    RSA加密--前端JSEncrypt 介绍 ​ JSEncrypt是一个RSA加密库,在没有SSL加密传输通道支持https协议的情况下,该库可以在http传输重要信息如时,保证数据的安全性.我们小组 ...

  9. 嵌入式STM32的GPIO口工作模式的介绍

    一.输入模式 1. 浮空输入 浮空输入模式下,上拉和下拉两个开关断开,高或低电平通过施密特触发器到达输入数据寄存器,CPU可以通过读取输入数据寄存器从而读取到外部输入的高低电平值. 2. 输入上拉模式 ...

  10. (转)linux下错误的捕获:errno和strerror的使用,以及perror和strerror的区别

    经常在调用linux 系统api 的时候会出现一些错误,比方说使用open() write() creat()之类的函数有些时候会返回-1,也就是调用失败,这个时候往往需要知道失败的原因.这个时候使用 ...