General
Replace and remove
Given a char array, replace 'a' to 2 'd', Remove 'b', size is the size of character array
Forward and backward iteration
public static int replaceAndRemove(int size, char[] s) {
int countA = 0, writeIndex = 0;
// Forward iteration: remove 'b', count the number of 'a'
for (int i = 0; i < size; i++){
if (s[i] != 'b'){
s[writeIndex++] = s[i];
}
if (s[i] == 'a'){
countA++;
}
}
writeIndex -= 1;
int currentIndex = writeIndex;
writeIndex += countA;
int finalSize = writeIndex + 1;
// Backward: repalce 'a' with 2 'd's
while(currentIndex >= 0){
if (s[currentIndex] == 'a'){
s[writeIndex--] = 'd';
s[writeIndex--] = 'd';
}else{
s[writeIndex--] = s[currentIndex];
}
currentIndex--;
}
return finalSize;
}Rotated Array
Increasing Sorted Array with break point
O(1) space, O(n) time
三步翻轉

4. Median of 2 Sorted Arrays
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays.
Example Given A=[1,2,3,4,5,6] and B=[2,3,4,5], the median is 3.5.
Given A=[1,2,3] and B=[4,5], the median is 3

k = (m+n)/2, 想法:找到合併後從小到大第k個element
遞回比較兩個arrays中第k/2個ele, 其中一個array前進k/2 idx
k=1時就是答案
54 Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
Layer by layer

Sort By Matrix Layer
Sort the matrix from out layer to inner layer, and put the array in clockwise
31. Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
The replacement must be in-place and use only constant extra memory.
From rhs, find the pivot smaller than previous number
The rhs array is in decreasing order
Swap pivot with first greater number among reversed number
Reverse array after pivot
41. First Missing Positive
Including zero and negative number
First missing number in consecutive array
Binary search, use array index, O(logN)
1060. Missing Element in Sorted Array
Given a sorted array A of unique numbers, find the K-th missing number starting from the leftmost number of the array.
128 Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence
Hash First ensuring the number immediately preceding the current number is not present, after that lookup subsequent number in set and count the sequence length.
Time Looks like O(n^2), but actually O(n)
Space O(n)
565. Array Nesting
A zero-indexed array A of length N contains all integers from 0 to N-1. Find and return the longest length of set S, where S[i] = {A[i], A[A[i]], A[A[A[i]]], ... }
300 Longest Increasing Subsequence
The longest increasing subsequence problem is to find a subsequence of a given sequence in which the subsequence's elements are in sorted order, lowest to highest, and in which the subsequence is as long as possible. This subsequence is not necessarily contiguous, or unique.
Example
For [5, 4, 1, 2, 3], the LIS is [1, 2, 3], return 3
For [4, 2, 4, 5, 3, 7], the LIS is [2, 4, 5, 7], return 4
State: f[i] = 第i+1根木樁的LIS
Init: f[0...i] = 1
function: f[i] = max{f[j]+1}, j<i && num[j]<num[i]
Ans: max{f[0...i]}
O(n^2)
Greedy + DP: O(nlogn)
建立一個空list, 這個list裡面的數字都要越小越好(increase 的機會越大) 遍歷nums, 只要 n 要append 到 list 最後,update length, 其它則更新到相對應位置上。Array 裡的答案不是正確的排序答案,但長度是對的
[4, 2, 4, 5, 3, 7] => [2, 3, 5, 7]
611. Triangle Count
Given an array of integers, how many three numbers can be found in the array, so that we can build a triangle whose three edges length is the three numbers that we find?
Example Given [4, 6, 3, 7] Ans [[3,4,6], [4,6,7], [3,6,7]]
Last updated
Was this helpful?