Dynamic Programming: Basics and a Practical Example

Introduction: Dynamic programming is a problem-solving technique that can seem quite intimidating at first glance. However, once you grasp its fundamentals and see it in action, you'll find it to be a powerful tool in your coding arsenal. In this blog post, we'll demystify the basics of dynamic programming and walk through a practical example. We'll provide you with clear explanations and a complete Python program, so you can understand and apply this essential concept with confidence. Understanding Dynamic Programming: At its core, dynamic programming is all about breaking down a complex problem into smaller, overlapping subproblems. It's a technique used to efficiently solve problems by storing and reusing solutions to these subproblems, rather than recomputing them. This leads to significant improvements in runtime complexity. Example: Fibonacci Sequence with Dynamic Programming Let's start by tackling a classic problem: calculating Fibonacci numbers. The Fibonac...

Women's CodeSprint 4 - Hackerrank


Q - 1   LEGOs


Sarah has four different kinds of LEGO pieces of different counts. She gives two types of LEGO to her friend and keeps the other two. She is putting two pieces together by picking one from each of the two types that she has left.
More specifically, the counts for each of the LEGO pieces are  and , respectively. The two types that Sarah gave her friend had  and  pieces, while the types that she kept have  and  pieces, respectively. Find  which would represent the number of two-piece LEGO combinations that Sarah can make.
Note that only  and  will be given as input.

Sample Input :-
2
20 10 40 30
10 30
5 5 10 10
5 10
Sample Output :-
800
50
Explanation :-

In the first case, the number of pieces of the four LEGO types are , ,  and , and the ones she brought have  and  pieces. Thus, the missing types have  and  pieces, so the answer is .
In the second case, the number of pieces of the four LEGO types are , ,  and , and the ones she brought have  and  pieces. Thus, the missing types have  and  pieces, so the answer is .
:- try to solve your self
Solution :-
static int productOfLegoTypes(int a, int b, int c, int d, int p, int q) {
        
        ArrayList<Integer> al = new ArrayList<>();
        
        al.add(a);
        al.add(b);
        al.add(c);
        al.add(d);
        
        for(int i = 0 ; i < 4 ; i++ ) {
            if(al.get(i)==p){
                al.set(i,0);
                break;
            }
        }
        
        for(int i = 0 ; i < 4 ; i++ ) {
            if(al.get(i)==q){
                al.set(i,0);
                break;
            }
        }
        
        int ans = 1;
        for(int i = 0 ; i < 4 ; i++ ) {
            if(al.get(i)!=0){
                ans *= al.get(i);
            }
        }
        
        return ans;
    }

Comments

  1. If you have any doubt write comment and for more solution follow me and subscribe on youtube :- https://www.youtube.com/channel/UC9XSeMvacrpXF3O1Y2hLeaw

    ReplyDelete

Post a Comment

Popular posts from this blog

Array Transformation | biweekly-contest-12 | leetcode | Java Solution

Dynamic Programming: Basics and a Practical Example

leetcode | Longest Substring Without Repeating Characters Solution