Posts

Showing posts from June, 2022

Rat Maze With Multiple Jumps Geeks For Geeks

Problem: https://practice.geeksforgeeks.org/problems/rat-maze-with-multiple-jumps3852/1# (It is also the problem of the day for 02-06-2022) Solution: class Solution {     public int[][] ShortestDistance(int[][] matrix)     {        int[][] t=new int[matrix.length][matrix[0].length];                for(int i=0;i<matrix.length;i++){            for(int j=0;j<matrix[0].length;j++){                t[i][j]=0;            }        }                if(check(matrix,t,0,0)==true){            return t;        }                else{        int a[][]=new int[1][1];        for(int i=0;i<1;i++){            for(int j=0;j<1;j++){                a[i][j]=-1;            }        }        return a;        }             }     public static boolean boundary(int i, int j, int[][] matrix){         if(i>=matrix.length || j>=matrix[0].length || matrix[i][j]==0){             return false;         }         return true;     }          public static boolean check(int[][] matrix, int[][] t, int i, i

Transpose Matrix LeetCode

 Problem Link: https://leetcode.com/problems/sum-of-beauty-in-the-array/ (It is also problem of day for 02-06-2022) Solution: class Solution {     public int[][] transpose(int[][] matrix) {         /*         int t[][]=new int[matrix[0].length][matrix.length];         for(int i=0;i<matrix.length;i++){          for(int j=0;j<matrix[0].length;j++){                 t[j][i]=matrix[i][j];             }         }         return t;         */         int a=matrix.length;         int b=matrix[0].length;         int t[][]=new int[b][a];         for(int i=0;i<a;i++){          for(int j=0;j<b;j++){                 t[j][i]=matrix[i][j];             }         }         return t;              } } Time Complexity:O(Matrix.length*Matrix[0].length)[LeetCode Time:1ms faster than 52.16%] Space   Complexity: O(Matrix.length*Matrix[0].length) Auxiliary Space: O(Matrix[0].length*Matrix.length)[LeetCode Memory:48.4Mb less than 57.35Mb] Approach Used: Traverse the arrray and store the value one by

Solutions Of Practice Questions Dated 01-06-2022

Question 1: Transmit the Message: Solution: import java.util.*; class Solution{     public static void main(String arg[]){         Scanner sc=new Scanner(System.in);         String s=sc.next();                          ArrayList<Integer>aa=new ArrayList<Integer>();         ArrayList<Character>ab=new ArrayList<Character>();                  char flag=s.charAt(0);        int index=0;        int count=1;         for(int i=1;i<s.length();i++){             if(flag==s.charAt(i)){                 count++;             }             else{                 aa.add(index,count);                 ab.add(index,flag);                 count=1;                 flag=s.charAt(i);                 index++;             }         }         aa.add(index,count);         ab.add(index,flag);                  int max=Collections.max(aa);                  for(int i=aa.size()-1;i>0;i--){             if(aa.get(i)==max){                 flag=ab.get(i);                 break;             }

Practice Questions Dated 1 June 2022

Problems: Question 1: Transmit a Message Problem statement Professor X has developed a new machine to transmit messages. The machine accepts a message in the form of a string of characters and transmits it to another machine of the same type at some other location. However, the machine has a certain bug. It can transmit only one type of character. As soon as it encounters a different type of character, the message recorded till that point is erased and the machine starts recording a new message beginning from that character. Professor X wants to send the longest message possible to his friend. To do this, he can stop the machine at any point and transmit the message by pressing the send button. However, he can do this only when the machine is in the recording mode and not the erasing mode. He wants your help. Given the input string, your task is to find the longest possible message that can be delivered by the machine. If there is more than one longest possible message, find the messag

Running Sum Of 1d Array LeetCode

 Problem Link: https://leetcode.com/problems/running-sum-of-1d-array/ (It is also the problem of the day for 01-06-2022) Solution: class Solution {     public int[] runningSum(int[] nums) {         int sum=0;         for(int i=0;i<nums.length;i++){             sum=sum+nums[i];             nums[i]=sum;         }         return nums;     } } Time Complexity: O(N)[LeetCode Time:0ms faster than 100%] Space Complexity: O(N) Auxiliary Space: O(1)[LeetCode memory usage of 39MB less than 100%] Total Test Cases:53 Approach Used: Here we will traverse the array and simultaneously keep on incrementing the value of the sum variable by arr elements value. Later we will modify the existing array element by that sum value. Here sum value at index i means the sum of all elements from 0 to i. "Thanks For Reading.😇" "Share Further To Increase Knowledge Treasure.😊"