• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

Minimum Cost to Paint all Houses with No Adjacent Houses have Same Color

Aniruddha Chaudhari/1449/2
CodePython

Problem statement: There are N houses. The amount to color each house with Red, Green, Blue colors is given. Write a program to find the minimum amount to paint all houses such that no 2 adjacent houses have the same color.

This coding question was asked in the coding round of the Amazon interview for the SDE-2 role.

The difficulty level of this problem is moderate.

Example

Input:

N = 3
Red = [6, 5, 3]
Green = [3, 1, 6]
Blue =  [9, 4, 2]

Output:

9

Explanation

We get the minimum cost of painting all houses when we paint the first house with the color Red (6), the second house with the color Green (1), and the third house with the color Blue (2). So the total minimum cost to paint all houses is 9 (6+1+2).

You can use any programming langauge of your choice like C/C++, Java, Python, etc.

Python Solution

To solve this problem, we are going to use recursion and dynamic programming. The rest of the code is self-explanatory.

Prerequisite:

  • if-else statement in Python
  • Python list

Python Code:

def colorHouse(N, red, green, blue, latest_color=None, count=0):
    if count == N:
        return 0

    if latest_color==None:
        return min(
            red[count] + colorHouse(N, red, green, blue, 'r', count+1 ), 
            green[count] + colorHouse(N, red, green, blue, 'g', count+1), 
            blue[count] + colorHouse(N, red, green, blue, 'b', count+1) 
        )
    elif latest_color=='r':
        return min(
            green[count] + colorHouse(N, red, green, blue, 'g', count+1), 
            blue[count] + colorHouse(N, red, green, blue, 'b', count+1) 
        )
    elif latest_color=='g':
        return min(
            red[count] + colorHouse(N, red, green, blue, 'r', count+1 ), 
            blue[count] + colorHouse(N, red, green, blue, 'b', count+1) 
        )
    else:
        return min(
            red[count] + colorHouse(N, red, green, blue, 'r', count+1 ), 
            green[count] + colorHouse(N, red, green, blue, 'g', count+1) 
        )


N = 3
Red = [6, 5, 3]
Green = [3, 1, 6]
Blue =  [9, 4, 2]
out = colorHouse(N, Red, Green, Blue)
print("Minimum cost to paint houses: ", out)

Output:

Minimum cost to paint houses:  9

You can try executing this program with different input values.

If you have any doubt or if you have a better solution for finding minimum cost to paint all houses, let’s discuss in the comment.

Python Interview Questions eBook

coding challenge
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Comments

  • Reply
    Sreenivas
    July 31, 2022 at 5:49 am

    We can paint with blue green blue which sums up to (3 + 1 + 2) = 6.

    • Reply
      Aniruddha Chaudhari
      August 1, 2022 at 9:45 am

      No. The first blue has a value 9, not 3.

Leave a Reply Cancel reply

Why?

Why Competitive Programming Important?

Coding Challenges for Practice

  1. Count Common Factor
  2. Does it Divide
  3. Sum of Sub Arrays
  4. Pair of Desired Sum
  5. Remove Duplicate Char from String
  6. Sort String by Char Freq (Python)
  7. Sort String by Char Freq (Java)
  8. Split Array into Equal Sum Subarray
  9. Validate IP Address
  10. Validate PAN Card Number
  11. Validate Sudoku
  12. Sort Circular Rotated Array
  13. String Permutations
  14. Min Arrow to Burst Bubbles
  15. Min Cost to Paint All Houses [Amazon]
  16. HourGlass with Max Sum
  17. Max Profit by Buying/Selling Stocks
  18. Hailstone Sequence
  19. Reverse String without affecting Special Characters
  20. Secure Conversation by Encry/Decry
  21. Special Elements in Matrix
  22. Next Greater No with Same set of Digits
  23. Smallest Subarray with Sum Greater than Given Number
  24. Group Anagrams
  25. Find Duplicates in Array in O(n)
  26. Find Two Unique Numbers from Array in O(n)
  27. Number Patterns & Finding Smallest Number
  28. First Unique Element in a Stream
  29. Flip Equivalent Binary Trees [TeachMint]
  30. Minimum Cost of Merging Files [Amazon]
  31. Minimum Distance for Truck to Deliver Order [Amazon]
  32. Longest Sequence of URLs
  33. Order Task for Given Dependencies
  34. Design Music Player
  35. Multilevel Parking System Design
  36. Minimum Coins Required
  37. Max Sum Subarray
  38. Max Avg Sum of Two Subsequences
  39. Merge Overlapping Intervals
  40. Longest Balanced Substring
  41. Longest Path in a Weighted Tree
  42. Generate Balanced Parentheses
  43. PostOrder Traversal Without Recursion

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard