PaintHouse I
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的更多相关文章
- PaintHouse II
// // Created by Administrator on 2021/7/27. // #ifndef C__TEST01_PAINTHOUSE_HPP #define C__TEST01_P ...
- LintCode刷题笔记-- PaintHouse 1&2
标签: 动态规划 题目描述: There are a row of n houses, each house can be painted with one of the k colors. The ...
- [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 ...
- LeetCode Paint House
原题链接在这里:https://leetcode.com/problems/paint-house/ 题目: There are a row of n houses, each house can b ...
- 【Todo】所有Locked的题目的分析解答
下面这个链接有比较全的leetcode题目包括锁的 http://www.cnblogs.com/grandyang/p/4606334.html https://leetcode.com/probl ...
- 256. Paint House
题目: There are a row of n houses, each house can be painted with one of the three colors: red, blue o ...
- 【LeetCode】1165. Single-Row Keyboard 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...
- 【LeetCode】256. Paint House 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...
随机推荐
- vue3 专用 indexedDB 封装库,基于Promise告别回调地狱
IndexedDB 的官网 https://developer.mozilla.org/zh-CN/docs/Web/API/IndexedDB_API 这个大概是官网吧,原始是英文的,现在陆续是出中 ...
- bzoj1503 郁闷的出纳员(平衡树,思维)
题目大意: 现在有n个操作和一个最低限度m \(I\)命令\(I\ k\)新建一个工资档案,初始工资为k. \(A\)命令$A\ k $把每位员工的工资加上k \(S\)命令$S\ k $把每位员工的 ...
- Vulnhub实战-Dockhole_2靶机👻
Vulnhub实战-Dockhole_2靶机 靶机地址:https://www.vulnhub.com/entry/darkhole-2,740/ 1.描述 hint:让我们不要浪费时间在蛮力上面! ...
- vue2和vue3比较
一.vue3新特性: 1.数据响应重新实现(ES6的proxy代替Es5的Object.defineProperty) 2.源码使用ts重写,更好的类型推导 3.虚拟DOM新算法(更快,更小) 4.提 ...
- px,dp sp是像素、尺寸、尺寸
px:即像素,1px代表屏幕上一个物理的像素点:px单位不被建议使用,因为同样100px的图片,在不同手机上显示的实际大小可能不同,如下图所示(图片来自android developer guide, ...
- Scrum Meeting 15
第15次例会报告 日期:2021年06月09日 会议主要内容概述: 开发工作接近尾声,接下来两天重点放在单元测试.调CSS和增加数据集数量上. 一.进度情况 我们采用日报的形式记录每个人的具体进度,链 ...
- UltraSoft - Alpha - 发布声明
DDL_Killer Alpha版本发布声明 1. Alpha 阶段功能描述与版本实现 功能描述 设计原型 Alpha实现 登陆界面 注册界面 首页 日历视图 事项详情页 新建事项 列表视图 课程视图 ...
- 震惊,hzoi的分差竟然折磨大,活到爆!
众所周知,hzoi的分差非常"大",那么究竟有多大呢?最近,一位外国小哥开发出了hzoi的分差竟然折磨大,活到爆!的方法,这究竟是怎么一回事呢?快和小编一起来看看吧- 竟然1分就可 ...
- ESD
Reverse standoff voltage是保护二极管的反向工作电压, 在这个电压, 二极管是不工作的. Breakdown voltage 是二极管的击穿电压, 超过这个电压后, 二极管迅速反 ...
- 单片机stm32串口分析
stm32作为现在嵌入式物联网单片机行业中经常要用多的技术,相信大家都有所接触,今天这篇就给大家详细的分析下有关于stm32的出口,还不是很清楚的朋友要注意看看了哦,在最后还会为大家分享有些关于stm ...