Posts

Showing posts from August, 2020

Rain Water Trapped

Problem Given an array arr[]  of N non-negative integers representing height of blocks at index i as A i where the width of each block is 1. Compute how much water can be trapped in between blocks after raining. Problem Constraints 1 <=   |arr|   <= 100000 Example Input Input 1: A = [0, 1, 0, 2] Input 2:  A = [1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1] Example Output Output 1: 1 Output 2: 6  Question Link https://leetcode.com/problems/trapping-rain-water/ https://practice.geeksforgeeks.org/problems/trapping-rain-water/0   Solution We have to calculate the amount of water to be stored in between the blocks. Solution 1: Brute Force In Brute force approach, We do what it says in the question. For each element in the array, we find the maximum amount of water it can trap. we calculate the minimum of the maximum of left side and right side of the element. Algorithm Initialize res = 0 ; Iterate the array from left to right : Initialize left_max = Integer.MIN_VAL...

Difference between Static Array and Dynamic Array

Image
  An array is a collection of similar types of data. Each data item present in the array is called the element of the array. The elements of the array share the same variable name but each element has a different index number. Arrays used to organize data and provide operations such as searching and sorting of data. There are two types of arrays: Static Arrays Dynamic Arrays Static Arrays Static Arrays is a collection of similar types of data stored at contiguous memory location. Store multiple item of a same type together. In Java:  char[] arr = new char[size of Array]; To access element: 0 based indexing. arr[0] - 1st element of the array. Size of arr = arr.length; last element of the array = arr[arr.length-1]; Dynamic Arrays: In dynamic array, we store the collection of similar types of element but we cannot define the size of the array. In Java dynamic array is created using ArrayList which is the implementation of List interface. When we declare the ArrayList, it creates ...