`{"id":"ff9a05da-26ee-4aa2-97f5-31ad64791a15","name":"Paint House - Many Colors","description":"1. You are given a number n and a number k separated by a space, representing the number of houses and number of colors.\r\n2. In the next n rows, you are given k space separated numbers representing the cost of painting nth house with one of the k colors.\r\n3. You are required to calculate and print the minimum cost of painting all houses without painting any consecutive house with same color.","inputFormat":"A number n\r\nn1-0th n1-1st n1-2nd .. n1-kth\r\nn2-0th n2-1st n2-2nd .. n2-kth\r\n.. n number of elements","outputFormat":"A number representing the minimum cost of painting all houses without painting any consecutive house with same color.","constraints":"1 &lt;= n &lt;= 1000\r\n1 &lt;= k &lt;= 10\r\n0 &lt;= n1-0th, n1-1st, .. &lt;= 1000","sampleCode":{"cpp":{"code":"#include <iostream>\n#include <bits/stdc++.h>\nusing namespace std ;\n\nint main()\n{\n int n ;\n cin>>n ;\n int k ;\n cin>>k ;\n //write your code from here\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static void main(String[] args) throws Exception {\r\n\r\n }\r\n}"},"python":{"code":"import sys\ndef minCost(arr, N , K):\n # write your code here\nif __name__ == '__main__':\n\n inp = list(map(int, input().split()))\n\n n = inp[0]\n k = inp[1]\n\n a = []\n for i in range(n) :\n b = []\n im=list(map(int,input().split()))\n for j in range(0,k) :\n temp = im[j]\n b.append(temp)\n a.append(b)\n \n print(minCost(a , n , k))"}},"points":10,"difficulty":"easy","sampleInput":"4 3\r\n1 5 7\r\n5 8 4\r\n3 2 9\r\n1 2 4","sampleOutput":"8","questionVideo":"https://www.youtube.com/embed/jGywRalvoRw?end=197","hints":[],"associated":[{"id":"21b42b0f-0dc2-4ba3-9662-090f7112e4b4","name":"What will be the time complexity of Paint House- Many Colors","slug":"what-will-be-the-time-complexity-of-paint-house-many-colors","type":4},{"id":"c4efbdfc-2e93-49e6-be96-faa5000c1fe9","name":"Which row or column will we consider to give the final minimum answer?","slug":"which-row-or-column-will-we-consider-to-give-the-final-minimum-answer","type":4},{"id":"cedbc4bd-06cb-4bdf-8b6e-7e781ad140b3","name":"What will be the direction of traversal and storage?","slug":"what-will-be-the-direction-of-traversal-and-storage","type":4},{"id":"df260f56-756d-4244-ae69-be3ce6ce1bb7","name":"What will be the size of the dp array?","slug":"what-will-be-the-size-of-the-dp-array","type":4}],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"52d62581-1313-45fb-aaf0-1d72a45f6a50","name":"Dynamic Programming And Greedy For Beginners","slug":"dynamic-programming-and-greedy-for-beginners","type":0},{"id":"00970aa1-1f35-48cd-960f-d581ca64b243","name":"Paint House - Many Colors","slug":"paint-house-many-colors","type":1}],"next":{"id":"05abd83c-e89d-4861-952f-965118bc3dbb","name":"Paint House-Many Colors","type":3,"slug":"paint-house-many-colors"},"prev":{"id":"72d7a2f3-1cc4-402e-a2ae-9abd1b18691a","name":"Paint House","type":3,"slug":"paint-house"}}}`

# Paint House - Many Colors

1. You are given a number n and a number k separated by a space, representing the number of houses and number of colors. 2. In the next n rows, you are given k space separated numbers representing the cost of painting nth house with one of the k colors. 3. You are required to calculate and print the minimum cost of painting all houses without painting any consecutive house with same color.

`{"id":"ff9a05da-26ee-4aa2-97f5-31ad64791a15","name":"Paint House - Many Colors","description":"1. You are given a number n and a number k separated by a space, representing the number of houses and number of colors.\r\n2. In the next n rows, you are given k space separated numbers representing the cost of painting nth house with one of the k colors.\r\n3. You are required to calculate and print the minimum cost of painting all houses without painting any consecutive house with same color.","inputFormat":"A number n\r\nn1-0th n1-1st n1-2nd .. n1-kth\r\nn2-0th n2-1st n2-2nd .. n2-kth\r\n.. n number of elements","outputFormat":"A number representing the minimum cost of painting all houses without painting any consecutive house with same color.","constraints":"1 &lt;= n &lt;= 1000\r\n1 &lt;= k &lt;= 10\r\n0 &lt;= n1-0th, n1-1st, .. &lt;= 1000","sampleCode":{"cpp":{"code":"#include <iostream>\n#include <bits/stdc++.h>\nusing namespace std ;\n\nint main()\n{\n int n ;\n cin>>n ;\n int k ;\n cin>>k ;\n //write your code from here\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static void main(String[] args) throws Exception {\r\n\r\n }\r\n}"},"python":{"code":"import sys\ndef minCost(arr, N , K):\n # write your code here\nif __name__ == '__main__':\n\n inp = list(map(int, input().split()))\n\n n = inp[0]\n k = inp[1]\n\n a = []\n for i in range(n) :\n b = []\n im=list(map(int,input().split()))\n for j in range(0,k) :\n temp = im[j]\n b.append(temp)\n a.append(b)\n \n print(minCost(a , n , k))"}},"points":10,"difficulty":"easy","sampleInput":"4 3\r\n1 5 7\r\n5 8 4\r\n3 2 9\r\n1 2 4","sampleOutput":"8","questionVideo":"https://www.youtube.com/embed/jGywRalvoRw?end=197","hints":[],"associated":[{"id":"21b42b0f-0dc2-4ba3-9662-090f7112e4b4","name":"What will be the time complexity of Paint House- Many Colors","slug":"what-will-be-the-time-complexity-of-paint-house-many-colors","type":4},{"id":"c4efbdfc-2e93-49e6-be96-faa5000c1fe9","name":"Which row or column will we consider to give the final minimum answer?","slug":"which-row-or-column-will-we-consider-to-give-the-final-minimum-answer","type":4},{"id":"cedbc4bd-06cb-4bdf-8b6e-7e781ad140b3","name":"What will be the direction of traversal and storage?","slug":"what-will-be-the-direction-of-traversal-and-storage","type":4},{"id":"df260f56-756d-4244-ae69-be3ce6ce1bb7","name":"What will be the size of the dp array?","slug":"what-will-be-the-size-of-the-dp-array","type":4}],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"52d62581-1313-45fb-aaf0-1d72a45f6a50","name":"Dynamic Programming And Greedy For Beginners","slug":"dynamic-programming-and-greedy-for-beginners","type":0},{"id":"00970aa1-1f35-48cd-960f-d581ca64b243","name":"Paint House - Many Colors","slug":"paint-house-many-colors","type":1}],"next":{"id":"05abd83c-e89d-4861-952f-965118bc3dbb","name":"Paint House-Many Colors","type":3,"slug":"paint-house-many-colors"},"prev":{"id":"72d7a2f3-1cc4-402e-a2ae-9abd1b18691a","name":"Paint House","type":3,"slug":"paint-house"}}}`

Editor

# Paint House - Many Colors

easy

1. You are given a number n and a number k separated by a space, representing the number of houses and number of colors. 2. In the next n rows, you are given k space separated numbers representing the cost of painting nth house with one of the k colors. 3. You are required to calculate and print the minimum cost of painting all houses without painting any consecutive house with same color.

## Constraints

1 <= n <= 1000 1 <= k <= 10 0 <= n1-0th, n1-1st, .. <= 1000

## Format

### Input

A number n n1-0th n1-1st n1-2nd .. n1-kth n2-0th n2-1st n2-2nd .. n2-kth .. n number of elements

### Output

A number representing the minimum cost of painting all houses without painting any consecutive house with same color.

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}4 3 1 5 7 5 8 4 3 2 9 1 2 4```

### Sample Output

`.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}8`

Question Video

Discussions

Show Discussion

Related Resources