Leetcode-Array
By: Yunrui Li
Tags: Leetcode, Data Structure Algorithm, Python3, Arrays

Array Problem is given an array, to ask you to meet a certain requirements. Usually, it can be solved with below techniques:

Sorting : Interval series problem. 除了有關找到number of non-overlapping interval sorted by ending time. 其他系列都是sorted by starting time.

: Interval series problem. 除了有關找到number of interval sorted by ending time. 其他系列都是sorted by starting time. HashMap or HashSet : find duplicate, find intersections.

: find duplicate, find intersections. DP : Usually, we think of DP when we found current result relies on previous result.

: Usually, we think of DP when we found current result relies on previous result. Two pointers : Usually, we use this technique when required to do operation in-place on array. O(1) space complexity.

: Usually, we use this technique when required to do operation in-place on array. O(1) space complexity. Binary Search : Usually, we think of this technique when mentioned “sorted” in question or do it in log(n) time complexity.

: Usually, we think of this technique when mentioned “sorted” in question or do it in log(n) time complexity. Prefix Sum+HashMap: leverage ps[i]-ps[j] = sum of subarray ranging from index j to index i. Using prefix sum as a key and index or count as a corresponding value.

Note:

Find duplicate problems: HashMap is leveraged as helper data structure. In order to find duplicates in O(1) space complexity, we might need slow and fast pointer but assumption is every element in the array must between 1 and n-1 and there must be duplicate element in the given array.

pattern search problems: sliding window is adopted usually.

Interval problems: sorting and priority queue

sample problems: search a random target on an array.

Rotate array k 次 等效於作出以下動作:

— reverse前n-k個element

— reverse後k個element

— 最後在reverse 整個array

— reverse前n-k個element — reverse後k個element — 最後在reverse 整個array Rotated Array 當我們從mid切一刀下去不管從那邊切, 一定有一半邊subarray是sorted array, 一半邊subarray不是(minimum element/pivot element在這一半邊).

→Edge case of rotated Array is array is sorted.

目前唯一需要用union-find這個data structure才可以最佳解的題目(LC 128 Longest Consecutive Sequence)

→ step1:去掉重複的node

→ step2:把前後值差一的node並在一起, 他們會有相同的祖先

→ step3:掃過每一個node, 找出他們的祖先, 然後找出到每個group最多有多少個node, 最多node的group就是答案. 這組group的祖先都相同.

Rotate Matrix: transpose matrix + reverse each row of matrix(math solution)

The below LC problems are related to Array:

128. Longest Consecutive Sequence

153. Find Minimum in Rotated Sorted Array

349. Intersection of Two Arrays

528. Random Pick with Weight

240. Search a 2D Matrix II

277. Find the Celebrity

118. Pascal’s Triangle

119. Pascal’s Triangle II

26. Remove Duplicates from Sorted Array

27. Remove Element

80. Remove Duplicates from Sorted Array II

4. Median of Two Sorted Arrays

1. Two Sum

167. Two Sum II — Input array is sorted

1099. Two Sum Less Than K

15. 3Sum

259. 3Sum Smaller

16. 3Sum Closest

18. 4Sum

454. 4Sum II

1679. Max Number of K-Sum Pairs

49. Group Anagrams

242. Valid Anagram

438. Find All Anagrams in a String

189. Rotate Array

287. Find the Duplicate Number

163. Missing Ranges

228. Summary Ranges

31. Next Permutation

73. Set Matrix Zeroes

54. Spiral Matrix

59. Spiral Matrix II

48. Rotate Image

169. Majority Element

229. Majority Element II

311. Sparse Matrix Multiplication

977. Squares of a Sorted Array

42. Trapping Rain Water

56. Merge Intervals

57. Insert Interval

252. Meeting Rooms

981. Time Based Key-Value Store

139. Word Break

140. Word Break II

560. Subarray Sum Equals K