You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. Example: Input: (2 ->…… Continue reading Leetcode 2 Add Two Numbers Java Solution
Category: Data Structures & Algorithms
Leetcode 1 Two Sum Java Solution
Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. Example: Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9,…… Continue reading Leetcode 1 Two Sum Java Solution
Leetcode 406 Queue Reconstruction by Height
Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers (h, k), where h is the height of the person and k is the number of people in front of this person who have a height greater than or equal to h. Write an algorithm to reconstruct the queue. Note:The…… Continue reading Leetcode 406 Queue Reconstruction by Height
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
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