🤓 1N73LL1G3NC3 15 7H3 4B1L1TY 70 4D4P7 70 CH4NG3.

Day 1 : Coin Flip

Election Stress? Connect with a Therapist Before It Overwhelms You

With Election Season upon us, it’s easy to feel overwhelmed by stress and uncertainty. BetterHelp, trusted by 4 million subscribers, connects you with one of 32,000 licensed therapists specializing in helping people manage stress and anxiety during turbulent times. Whether it’s through phone, video, or text, their therapists are here to support you whenever and wherever you need it. Take their free 5-minute assessment and get matched with a therapist who understands your unique concerns. Plus, save $250 on your first three months of therapy and enjoy a risk-free experience with their money-back guarantee. Start the journey to a more peaceful mind today!

TLDR

  1. Welcome!

  2. 322. Coin Change

  3. Recommendations I live by!

  4. Self-Gaslight of the week

  5. JOBS & SCHOLARSHIPS

Hello and welcome back brave human being!

7H3 M057 B34U71FUL TH1NG W3 C4N 3XP3R13NC3 15 7H3 MYST3RY.
 17 15 7H3 50URC3 0F 4LL 7RU3 4R7 4ND 51ENC3

This cryptic message is a preparation for today’s problem!!!

I am a 100% sure you will be able to solve it if you just give me a minute!

LFG!!

LeetCode problem

Given an array of coins representing different denominations, and a target amount of money, return the minimum number of coins needed to reach that amount. 

If it’s not possible to make up the amount, return -1.

Before we dive in,

Breathe

Just know it took me a full day to understand this problem, so if I don’t explain it correctly, it’s on me. Not on you.

Now, let’s think about the problem before diving into it.

We are asked to find the minimum number of coins when summed it equals the amount.

Let’s think about the problem together before we jump into coding. We’re asked to find the minimum number of coins that sum up to a specific amount.

Given this example:

coins = [1, 2, 5]
amount = 11

We could try using 11 coins of 1 (like 1 + 1 + 1 + ...) or maybe 1 + 5 + 2 + 1 + 1 + 1. Both add up to 11, but we want to find the solution that uses the fewest coins.

A brute-force approach would involve finding all possible combinations and then identifying the one with the least coins. However, this would take a massive amount of time.

Instead, let’s use a dynamic programming (DP) approach to solve this by breaking the problem into smaller subproblems.

Here’s the plan:

  1. Define an array dp where dp[i] represents the minimum number of coins needed to reach the amount i.

  2. Set dp[0] = 0 because no coins are needed to make an amount of 0.

  3. Initialize all other entries in dp to a large number, like 100005, to signify that we haven’t yet found a way to reach those amounts.

For each coin value, we’ll update our dp array, keeping track of the minimum coins needed to reach each amount.

For example, with coin = 1:

  • dp[0] = 0 (no coins needed for 0)

  • dp[1] = 1 (we need 1 coin of 1 to make an amount of 1)

  • dp[2] = 2 (we need 2 coins of 1 to make an amount of 2)

  • … and so on until we reach dp[11] = 11.

dp = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

Now let’s introduce a second coin, coin = 2, which gives us more efficient ways to reach certain amounts. For example:

  • dp[2] could now use 1 coin of 2 instead of 2 coins of 1.

  • dp[3] could use 1 coin of 2 and 1 coin of 1, making it more efficient.

The general update rule for any position i in dp is:

dp[i] = min(dp[i], dp[i-coin] + 1)
        ↑         ↑         ↑
    Current     Previous    New coin
    Best        Best       we're adding

This rule allows us to find the optimal number of coins needed to reach each amount by checking if adding a coin of a certain denomination gives a better solution.

Now, where would our answer be?

Remember dp[i] ……

Which variable holds the final answer?

Minimum number of coins needed to make up the target amount

Login or Subscribe to participate in polls.

Complete Solution :

class Solution {
public:
    int coinChange(vector<int>& coins, int amount) {
        vector<int>dp (amount+1, 100005);
        dp[0] =0; 
        for(auto coin: coins){
            for(int x=coin; x<=amount; x++){
                if(!(dp[x-coin]==100005)){
                    dp[x] = min(dp[x], dp[x-coin]+1);
                }
            }
        }
        if(dp[amount]==100005){
            return -1;
        }
        return dp[amount];
       
    }
};

Recommendations

Level up your inbox by subscribing to what I am subscribed to!

Hacks of the week

Self-gaslight of the week

I truly believe you can gaslight yourself to success.

  1. Make space for the emotions you crave

  2. USE QWOTED to find questions you can answer to become a thought leader

Cheers to you hacking your week,

Denisse

Reply

or to participate.