Given n non-negative integers a1, a2, …, an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. The…… Continue reading Leetcode Container With Most Water Java Solution
Tag: Leetcode Array Problems
Leetcode 787 Cheapest Flights Within K Stops Java Solution
There are n cities connected by m flights. Each flight starts from city u and arrives at v with a price w. Now given all the cities and flights, together with starting city src and the destination dst, your task is to find the cheapest price from src to dst with up to k stops. If there is no…… Continue reading Leetcode 787 Cheapest Flights Within K Stops 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 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 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