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...

Week of Code 36 - Hackerrank

Q-1 Acid Naming


Solution:-

public class Solution {

   public static void main(String[] args) throws Exception{
       
       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       
       int n = Integer.parseInt(br.readLine());
       
       for(;n-->0;){
           String acid = br.readLine().trim();
           acid =acid.toLowerCase();
           
           if(acid.startsWith("hydro") && acid.endsWith("ic")){
               System.out.println("non-metal acid");
           } else if(acid.endsWith("ic")){
               System.out.println("polyatomic acid");
           } else {
               System.out.println("not an acid");
           }
       }
   }
}



Q-2 Revised Russian Roulette


Solution :-

public class Solution {

   static int[] revisedRussianRoulette(int[] d) {
      
       int result[] = new int[2];
       for(int i = 0 ; i < d.length; i++){
           if(d[i]==1){
               result[1]++;
           }
       }
       
       for(int i = 0 ; i < d.length; i++){
           if(d[i]==1){
               result[0]++;
               d[i] = 0;
               
               if(i<d.length-1){
                   d[i+1] = 0;
               }
           }
       }
       
       return result;
   }

   public static void main(String[] args)throws IOException {

       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       
       int n = Integer.parseInt(br.readLine());
       
       int[] doors = new int[n];
       String doorstring[] = br.readLine().split(" ");
       
       for(int i = 0; i < n; i++){
           doors[i] = Integer.parseInt(doorstring[i]);
       }
       
       int[] result = revisedRussianRoulette(doors);

       System.out.println(result[0] + " " + result[1]);
   }
}

Q-4 A Race Against Time


Solution :-

Coming soon

For Solution Video

Comments

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