Leetcode Unique Binary Search Trees Java Solution

Given n, how many structurally unique BST’s (binary search trees) that store values 1 … n? Example: Input: 3 Output: 5 Explanation: Given n = 3, there are a total of 5 unique BST’s: 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 2 3…… Continue reading Leetcode Unique Binary Search Trees Java Solution

Leetcode 200 Number of Islands (Java) With Video Explanation

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

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