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. Data Interoperability Tools

    这里的工具貌似没有对应函数~~~

  2. 远程设备管理opendx平台搭建-appium和adb的安装

    多年不见了,说起来也有3年了我又开始写博客了,这几年我还是没啥长进,还是干测试,但是测试行业的话,我已经成了一个测开了,也在搭建自己的测试网站 本系列文章讲述的是一个系列的第一部分,最终可以搭建一整套 ...

  3. 【UE4 设计模式】策略模式 Strategy Pattern

    概述 描述 策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换.策略模式让算法的变化不会影响到使用算法的客户. 套路 Context(环境类) 负责使用算法策略,其中维持了一 ...

  4. Codeforces Round #748 (Div. 3)

    Codeforces Round #748 (Div. 3) A. Elections 思路分析: 令当前值比最大值大即可,如果最大值是它自己,就输出\(0\) 代码 #include <bit ...

  5. Kotlin/Native 用KMM写Flutter插件

    一.用KMM写Flutter插件 Google官方有一个写Flutter例子How to write a Flutter plugin,这里把Google plugin_codelab 例子改成用KM ...

  6. GEOS使用记录

    由于需要计算GIS障碍物的缓冲区,所以研究了 一下GEOS库的使用,将使用的一些细节内容记录一下: 1.vs2010IDE无法编译较高版本的GEOS库,较高版本的库使用了更加高级的C++语法,如果想使 ...

  7. USB_ID OTG

    谁知道USB_ID pin 脚的功能意义?是干什么用的?USB 中不就有 VDD,GND,USB+,USB- 并没有USB_ID 的信息呀?检测ID脚状态高低,从而判断为主设备或从设备,otg的时候用 ...

  8. 一文带你掌握【TCP拥塞窗口】原理

    ❝ 关注公众号:高性能架构探索.后台回复[资料],可以免费领取 ❞ 学过网络相关课程的,都知道TCP中,有两个窗口: 滑动窗口(在我们的上一篇文章中有讲),接收方通过通告发送方自己的可以接受缓冲区大小 ...

  9. 前端面试手写代码——JS数组去重

    目录 1 测试用例 2 JS 数组去重4大类型 2.1 元素比较型 2.1.1 双层 for 循环逐一比较(es5常用) 2.1.2 排序相邻比较 2.2 查找元素位置型 2.2.1 indexOf ...

  10. Django 前端BootCSS 实现分页

    通过使用bootstrap框架,并配合Django自带的Paginator分页组件即可实现简单的分页效果. 1.创建MyWeb项目 python manage.py startapp MyWeb 2. ...