Table of Contents
287. Find the Duplicate Number
297. Serialize and Deserialize Binary Tree
300. Longest Increasing Subsequence
240. Search a 2D Matrix II
https://leetcode.com/problems/search-a-2d-matrix-ii/
Binary Search, Divide and Conquer
TIme: start with Top Right corner: O(m + n)
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if(matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return false;
}
int col = matrix[0].length - 1;
int row = 0;
while(row < matrix.length && col >= 0) {
if(matrix[row][col] == target) {
return true;
} else if(matrix[row][col] > target) {
col--;
} else {
row++;
}
}
return false;
}
}
279. Perfect Squares
https://leetcode.com/problems/perfect-squares/
DP
dp[n] indicates that the perfect squares count of the given n, and we have:
dp[0] = 0
dp[1] = dp[0]+1 = 1
dp[2] = dp[1]+1 = 2
dp[3] = dp[2]+1 = 3
dp[4] = Min{ dp[4-1*1]+1, dp[4-2*2]+1 }
= Min{ dp[3]+1, dp[0]+1 }
= 1
dp[5] = Min{ dp[5-1*1]+1, dp[5-2*2]+1 }
= Min{ dp[4]+1, dp[1]+1 }
= 2
.
.
.
dp[13] = Min{ dp[13-1*1]+1, dp[13-2*2]+1, dp[13-3*3]+1 }
= Min{ dp[12]+1, dp[9]+1, dp[4]+1 }
= 2
.
.
.
dp[n] = Min{ dp[n - i*i] + 1 }, n - i*i >=0 && i >= 1
class Solution {
public int numSquares(int n) {
int[] dp = new int[n + 1];
dp[0] = 0;
for(int i = 1; i <= n; i++) {
int min = Integer.MAX_VALUE;
int j = 1;
while(j*j <= i) {
min = Math.min(min, dp[i - j*j] + 1);
j++;
}
dp[i] = min;
}
return dp[n];
}
}
283. Move Zeroes
https://leetcode.com/problems/move-zeroes/
Array, Two Pointers
class Solution {
public void moveZeroes(int[] nums) {
if(nums == null || nums.length == 0) {
return;
}
int slow = 0;
for(int i = 0; i < nums.length; i++) {
if(nums[i] != 0) {
nums[slow++] = nums[i];
}
}
while(slow < nums.length) {
nums[slow++] = 0;
}
}
}
287. Find the Duplicate Number
https://leetcode.com/problems/find-the-duplicate-number/
HashSet - O(n) Time, O(n) Space
O(1) Space Solution: Similiar to LinkedList Cycle detection
class Solution {
public int findDuplicate(int[] nums) {
int slow = nums[0];
int fast = nums[nums[0]];
while (fast != slow){
slow = nums[slow];
fast = nums[nums[fast]];
}
fast = 0;
while (fast != slow){
slow = nums[slow];
fast = nums[fast];
}
return slow;
}
}
/*
1,3,4,2,2
slow: 1
fast: 3
slow: 3
fast: 4
slow: 2
fast: 4
slow: 4
fast: 4
break
slow: 4
fast: 0
slow: 2
fast: 1
slow: 4
fast: 3
slow: 2
fast: 2
return 2
*/
297. Serialize and Deserialize Binary Tree
https://leetcode.com/problems/serialize-and-deserialize-binary-tree/
Design, Binary Tree
Serialize: In-order traversal with StringBuilder
De-serialize: Queue
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Codec {
private static String NULL = "null";
private static String splitter = ",";
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
StringBuilder sb = new StringBuilder();
return buildString(root, sb).toString();
}
private StringBuilder buildString(TreeNode root, StringBuilder sb) {
if (root == null) {
return sb.append(NULL);
}
sb.append(root.val).append(splitter);
buildString(root.left, sb).append(splitter);
buildString(root.right, sb);
return sb;
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
Queue<String> q = new LinkedList<>();
q.addAll(Arrays.asList(data.split(splitter)));
return buildTree(q);
}
private TreeNode buildTree(Queue<String> q) {
String node = q.remove();
if(node.equals(NULL)) {
return null;
}
TreeNode n = new TreeNode(Integer.valueOf(node));
n.left = buildTree(q);
n.right = buildTree(q);
return n;
}
}
// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.deserialize(codec.serialize(root));
300. Longest Increasing Subsequence
https://leetcode.com/problems/longest-increasing-subsequence/
DP
Time: O(nlogn)
dp[i] represents the increasing subsequence formed by including the currently encountered element nums[i].
Use Arrays.binarySearch(int[] a, int fromIndex, int toIndex, int key) to search the insertion point:
The insertion point is defined as the point at which the key would be inserted into the array: the index of the first element in the range greater than the key, or toIndex if all elements in the range are less than the specified key.
class Solution {
public int lengthOfLIS(int[] nums) {
if(nums == null || nums.length == 0) {
return 0;
}
int[] dp = new int[nums.length];
int len = 0;
for(int num : nums) {
int index = Arrays.binarySearch(dp, 0, len, num);
if(index < 0) {
index = -index - 1;
}
dp[index] = num;
if(index == len) {
len++;
}
}
return len;
}
}
/*
[0,8,4,12,2]
dp:
[0]
[0,8]
[0,4]
[0,4,12]
[0,2,12] -> len = 3
*/