## LeetCode – First Missing Positive (Java)

Given an unsorted integer array, find the first missing positive integer. For example, given [1,2,0] return 3 and [3,4,-1,1] return 2. Your algorithm should run in O(n) time and uses constant space. Analysis This problem can solve by using a bucket-sort like algorithm. Let’s consider finding first missing positive and 0 first. The key fact … Read more

## LeetCode – Contains Duplicate II (Java)

Given an array of integers and an integer k, return true if and only if there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k. Java Solution 1 – HashMap public boolean containsNearbyDuplicate(int[] nums, int k) { HashMap<Integer, … Read more

## LeetCode – Combination Sum III (Java)

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascending order. Example 1: Input: k = 3, n = 7 … Read more

## LeetCode – House Robber II (Java)

After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, the security system … Read more

## LeetCode – Kth Largest Element in an Array (Java)

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example, given [3,2,1,5,6,4] and k = 2, return 5. Note: You may assume k is always valid, 1 â‰¤ k â‰¤ array’s length. Java Solution 1 – Sorting … Read more

## LeetCode – Contains Duplicate (Java)

Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. Java Solution public boolean containsDuplicate(int[] nums) { if(nums==null || nums.length==0) return false;   HashSet<Integer> set = new HashSet<Integer>(); … Read more

## LeetCode – Maximal Rectangle (Java)

Given a 2D binary matrix filled with 0’s and 1’s, find the largest rectangle containing all ones and return its area. Analysis This problem can be converted to the “Largest Rectangle in Histogram” problem. Java Solution public int maximalRectangle(char[][] matrix) { int m = matrix.length; int n = m == 0 ? 0 : matrix[0].length; … Read more

## LeetCode – Largest Rectangle in Histogram (Java)

Given n non-negative integers representing the histogram’s bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]. The largest rectangle is shown in the shaded area, which has area = 10 … Read more

## LeetCode – Meeting Rooms II (Java)

Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],…] find the minimum number of conference rooms required. Java Solution When a room is taken, the room can not be used for anther meeting until the current meeting is over. As soon as the current meeting is finished, the room can … Read more

## LeetCode – Unique Paths II (Java)

Follow up for “Unique Paths“: Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, there is one obstacle in the middle of a 3×3 grid as illustrated below, [ [0,0,0], … Read more

## LeetCode – Spiral Matrix II (Java)

Given an integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order. For example, given n = 4, [ [1, 2, 3, 4], [12, 13, 14, 5], [11, 16, 15, 6], [10, 9, 8, 7] ] Java Solution 1 public int[][] generateMatrix(int n) { int total = n*n; int[][] … Read more

## LeetCode – Multiply Strings (Java)

Given two numbers represented as strings, return multiplication of the numbers as a string. Analysis The key to solve this problem is multiplying each digit of the numbers at the corresponding positions and get the sum values at each position. That is how we do multiplication manually. Java Solution public String multiply(String num1, String num2) … Read more

## LeetCode – Sliding Window Maximum (Java)

Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. Java Solution public int[] maxSlidingWindow(int[] nums, int k) { … Read more

## LeetCode – Intersection of Two Arrays II (Java)

Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2]. Java Solution 1 public int[] intersect(int[] nums1, int[] nums2) { HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); for(int i: nums1){ if(map.containsKey(i)){ map.put(i, map.get(i)+1); }else{ map.put(i, 1); } }   ArrayList<Integer> … Read more

## LeetCode – Minimum Window Substring (Java)

Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). For example, S = “ADOBECODEBANC”, T = “ABC”, Minimum window is “BANC”. Java Solution public String minWindow(String s, String t) { HashMap<Character, Integer> goal = new HashMap<>(); int goalSize = … Read more