Given a 2d grid map of ‘1’s (land) and ‘0’s (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water. Example : Input: 11000 11000 00100 00011 Output: 3 Solution: We…… Continue reading Leetcode 200 Number of Islands (Java) With Video Explanation

# Category: Leetcode

## Leetcode 56 Merge Intervals (Java)

Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8,10],[15,18]] Output: [[1,6],[8,10],[15,18]] Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6]. Solution: We can solve this by comparing the start of the next interval with the end of the current interval and merged if the start of the next interval…… Continue reading Leetcode 56 Merge Intervals (Java)

## Leetcode 23: Merge K Sorted Lists (Java)

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. Example: Input: [ 1->4->5, 1->3->4, 2->6 ] Output: 1->1->2->3->4->4->5->6 Solution: For this problem, the brute force solution would be passing all the lists and perform Merge Sort, but for the optimal solution we will use minHeap using…… Continue reading Leetcode 23: Merge K Sorted Lists (Java)

## Leetcode Problem Product of Array Except Self (Java) With Video

Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. Example: Input: [1,5,6,4] Output: [120,24,20,30] The below solution takes O(N) time complexity and O(N) space complexity class Solution { public int[] productExceptSelf(int[] nums) { int[] left = new int[nums.length]; int[] right = new int[nums.length]; int[] result= new…… Continue reading Leetcode Problem Product of Array Except Self (Java) With Video

## Trapping Rain Water Problem LeetCode Java Solution

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. Example: Input: [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6 class Solution { public int trap(int[] height) { int arrLength = height.length; int total = 0; if(arrLength<=2) return total; int[] leftMaxArr = new int[arrLength]; int[]…… Continue reading Trapping Rain Water Problem LeetCode Java Solution