Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Logic:
- Logic is same as paint house 1 but we need minimum from previous row but we can get the first and second minimum
- because if the minimum cost is of different color then cost to color is current_cost+previous min
- else current_cost+second_previous_min;
- */
- #include <iostream>
- #include <bits/stdc++.h>
- using namespace std ;
- int main()
- {
- int n ;
- cin >> n ;
- int k ;
- cin >> k ;
- vector<vector<int>> arr(n, vector<int>(k, 0));
- for (int i = 0 ; i < arr.size() ; i++)
- {
- for (int j = 0 ; j < arr[0].size() ; j++)
- {
- cin >> arr[i][j] ;
- }
- }
- // min values ;
- int min = INT_MAX;
- int smin = INT_MAX;
- //get min and second min from first column
- for (int j = 0; j < arr[0].size(); j++) {
- if (arr[0][j] <= min) {
- smin = min;
- min = arr[0][j];
- } else if (arr[0][j] <= smin) {
- smin = arr[0][j];
- }
- }
- // get answer
- for (int i = 1; i < arr.size(); i++) {
- int cmin = INT_MAX;
- int csmin = INT_MAX;
- for (int j = 0; j < arr[i].size(); j++) {
- if (arr[i - 1][j] != min) {
- arr[i][j] += min;
- } else {
- arr[i][j] += smin;
- }
- if (arr[i][j] <= cmin) {
- csmin = cmin;
- cmin = arr[i][j];
- } else if (arr[i][j] <= csmin) {
- csmin = arr[i][j];
- }
- }
- min = cmin;
- smin = csmin;
- }
- c
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement