Took part in a coding competition and got destroyed by this challenge. Had to solve this in 45 mins and even after kind of solving this here and wrapped my head around the logic I still believe 45 mins is too short for a person new to this question. My solution might be wrong as i still want to do a lot of testing with different samples of test data.
Weapon of choice - Python
Ping me if you have a better solution or if my solution is not right in anyway.
Connect and Ping
Thanks in advance.
There are n blocks placed in a row. Each block must be covered with one of the three colors available, but no two adjacent blocks can be the same color. The cost of coloring each block varies and is given in an array. Given the cost of using each color on each block, determine the minimum cost to color all the blocks.
There are three blocks to color and the cost to use each color for each block is given as
cost = [[1,2,3],
[1,2,3],
[3,3,1]].
For the first block, the cheapest color is the first color which costs 1. For the second block, colors cost the same but color 1 cannot be used because it matches the first block. instead, choose color 2. for the third block, it can be color 1 or 3. the cheaper is color 3 at 1 unit. The total cost to color the blocks is 1+2+1 = 4.
Complete the function minPrice in the code. the Function must return an integer that denotes the minimum cost to color all the blocks.
minPrice( ) has the following parameter: cost[cost[ 0 ],.....cost[ n-1 ]]: an array of integers where cost[ i ][ j ] denotes the cost of using the jth color on the ith block.
- 1 <= n <= 100
- 0 <= cost[ i ][ j ] <=100
- The number of rows will always be 3.
- Input data ie: cost should be hardcoded.
Any version of Python.
Execute the following command to run the program in the \Box-Colouring-Puzzle directory. Input desired data before running the code.
python Col-Puzzle.py
- Saurav Mohan V - LinkedIn
- HackerRank - Question Courtesy - Sign-Up!! Its Great Not Sponsored by HackerRank!!!
This project is licensed under the MIT License