Given a set of points in the x and y axes, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
Algorithms
LeetCode – Russian Doll Envelopes (Java)
You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envelope can fit into another if and only if both the width and height of one envelope is greater than the width and height of the other envelope.
Maximum Sum of Subarray Close to K
Given an array, find the maximum sum of subarray close to k but not larger than k.
Design a Data Structure with Insert, Delete and GetMostFrequent of O(1)
Design a data structure that allows O(1) time complexity to insert, delete and get most frequent element.
LeetCode – Kth Smallest Element in a Sorted Matrix (Java)
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix.
Note that it is the kth smallest element in the sorted order, not the kth distinct element.
LeetCode – Max Chunks To Make Sorted (Java)
Given an array arr that is a permutation of [0, 1, …, arr.length – 1], we split the array into some number of “chunks” (partitions), and individually sort each chunk. After concatenating them, the result equals the sorted array.
LeetCode -Toeplitz Matrix
A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. Given a matrix, check if it is toeplitz. (Assume the matrix is not empty) Java Solution public boolean isToeplitzMatrix(int[][] matrix) { int m=matrix.length; int n=matrix[0].length; for(int i=0; i<m; i++){ for(int j=0; j<n; j++){ if(i+1<m && j+1<n && matrix[i][j]!=matrix[i+1][j+1]){ return false; … Read more
LeetCode – Number of Subarrays with Bounded Maximum (Java)
We are given an array A of positive integers, and two positive integers L and R (L <= R).
LeetCode – Rotated Digits (Java)
X is a good number if after rotating EACH digit individually by 180 degrees, we get a valid number that is different from X. A number is valid if each digit remains a digit after rotation. 0, 1, and 8 rotate to themselves; 2 and 5 rotate to each other; 6 and 9 rotate to each other, and the rest of the numbers do not rotate to any other number.
LeetCode – Count of Smaller Numbers After Self (Java)
You are given an integer array nums and you have to return a new counts array. The counts array has the property where counts[i] is the number of smaller elements to the right of nums[i]. Example: Input: [5,2,6,1] Output: [2,1,1,0] Java Solution 1 public List<Integer> countSmaller(int[] nums) { List<Integer> result = new ArrayList<Integer>(); ArrayList<Integer> sorted … Read more
LeetCode – Minimum Increment to Make Array Unique (Java)
Java Solution 1 public int minIncrementForUnique(int[] A) { Arrays.sort(A); int count = 0; for(int i=1; i<A.length; i++){ if(A[i]<=A[i-1]){ count+=A[i-1]+1; A[i]=A[i]+1; } } return count; }public int minIncrementForUnique(int[] A) { Arrays.sort(A); int count = 0; for(int i=1; i<A.length; i++){ if(A[i]<=A[i-1]){ count+=A[i-1]+1; A[i]=A[i]+1; } } return count; } Actually we do not need to increment … Read more
LeetCode – Most Stones Removed with Same Row or Column (Java)
The easiest solution for this problem is the union-find. The number of island problem can help understand how union-find works. The basic idea is that we use a disjoint set to track each component. Whenever two stones can be connected, the number of islands decrease one. The final result, the number of movement, is the … Read more
LeetCode – Is Subsequence (Java)
Given a string s and a string t, check if s is subsequence of t.
You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string (<=100).
LeetCode – Sum of Two Integers (Java)
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.
LeetCode – Find K Pairs with Smallest Sums (Java)
You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k.
Define a pair (u,v) which consists of one element from the first array and one element from the second array.
Find the k pairs (u1,v1),(u2,v2) …(uk,vk) with the smallest sums.