# Array

## Range sum(subarray/matrix)

Essentially, this kind of problem can be solved with prefix sum.\
But search prefix sum takes O(N), how can we accelerate? Use Binary Index Tree(BIT) which guarantee O(logN) for searching an accumulated sum.

So to find the sum of a range, these are typical solutions:

* Prefix sum. Works for array: Sum(i, j) = prefixSum\[j] - prefixSum\[i-1], i is inclusive.
  * [0325. Maximum Size Subarray Sum Equals k](../solutions/0325-maximum-size-subarray-sum-equals-k)
    * Trick: `map.put(0, -1);`, this means Sum(0, i), i.e. prefixSum\[i], is what we want.
* Binary Index Tree(BIT). When prefixSum1 is known, we need to find another prefixSum2 that meets some constraint, e.g. prefixSum1 - prefixSum = k.
  * We can also build BST to achieve similar O(logN) search, but worst case is still O(N).
  * [0315. Count of Smaller Numbers After Self](../solutions/0315-count-of-smaller-numbers-after-self)
  * [0327. Count of Range Sum](../solutions/0327-count-of-range-sum)
* Segment Tree. Mostly used in range sum lookup.(Not used in search cases)
  * [0307. Range Sum Query - Mutable](../solutions/0307-range-sum-query-mutable)

Typicall questions:

* [0209. Minimum Size Subarray Sum](../solutions/0209-minimum-size-subarray-sum)
* [0315. Count of Smaller Numbers After Self](../solutions/0315-count-of-smaller-numbers-after-self)
  * [0327. Count of Range Sum](../solutions/0327-count-of-range-sum)
  * [0493. Reverse Pairs](../solutions/0493-reverse-pairs)
* [0307. Range Sum Query - Mutable](../solutions/0307-range-sum-query-mutable)
  * [0303. Range Sum Query - Immutable](../solutions/0303-range-sum-query-immutable)
  * [0308. Range Sum Query 2D - Mutable](../solutions/0308-range-sum-query-2d-mutable)
  * [0304. Range Sum Query 2D - Immutable](../solutions/0304-range-sum-query-2d-immutable)
* [0325. Maximum Size Subarray Sum Equals k](../solutions/0325-maximum-size-subarray-sum-equals-k)
  * [0523. Continuous Subarray Sum](../solutions/0523-continuous-subarray-sum): more of a math trick.
  * [0525. Contiguous Array](../solutions/0525-contiguous-array)
* [0437. Path Sum III](../solutions/0437-path-sum-iii)

## Interval problems

Interval problem typically cope with a range: a start point, a end point, and optionally some attributes of this interval, like [0218. The Skyline Problem](../solutions/0218-the-skyline-problem). It almost always requires sorting. Since it's O(NlogN) anyway, always try TreeMap.

Typicall ideas:

* Just sort it and iterate
  * [0056. Merge Intervals](../solutions/0056-merge-intervals)
* PriorityQueue. When we need to know how many accumulative intervals at some point.
  * [0253. Meeting Rooms II](../solutions/0253-meeting-rooms-ii)
* 2 sorted arrays: 1 for start points, 1 for end points
* 1 sorted array to store all points.
  * You can store them as array of classes, like [0218. The Skyline Problem](../solutions/0218-the-skyline-problem).
  * Alternatively, why not just use [TreeMap](https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html). Built-in APIs are amazing!
    * [0715. Range Module](../solutions/0715-range-module)
    * [0732. My Calendar III](../solutions/0732-my-calendar-iii)

Typical questions:

* [0056. Merge Intervals](../solutions/0056-merge-intervals)
  * [0057. Insert Interval](../solutions/0057-insert-interval)
  * [0352. Data Stream as Disjoint Intervals](../solutions/0352-data-stream-as-disjoint-intervals)
  * [0715. Range Module](../solutions/0715-range-module)
  * [0759. Employee Free Time](../solutions/0759-employee-free-time)
  * [0986. Interval List Intersections](../solutions/0986-interval-list-intersections)
* [0253. Meeting Rooms II](../solutions/0253-meeting-rooms-ii)
  * [0729. My Calendar I](../solutions/0729-my-calendar-i)
    * TreeMap is your friend. Calendar problem optimal is O(N).
  * [0731. My Calendar II](../solutions/0731-my-calendar-ii)
  * [0732. My Calendar III](../solutions/0732-my-calendar-iii)
* [0218. The Skyline Problem](../solutions/0218-the-skyline-problem): Key is how to order multiple points, high->low start points, then low->high end points.
* [0436. Find Right Interval](../solutions/0436-find-right-interval)
* Greedy:
  * [0452. Minimum Number of Arrows to Burst Balloons](../solutions/0452-minimum-number-of-arrows-to-burst-balloons)
  * [0435. Non-overlapping Intervals](../solutions/0435-non-overlapping-intervals)
* [0699. Falling Squares](../solutions/0699-falling-squares): Key is how to update TreeMap.

## make use of index

match nums\[i] with i

[https://leetcode.com/problems/first-missing-positive/description/](https://leetcode.com/problems/first-missing-positive/description/) [https://leetcode.com/problems/missing-number/description/](https://leetcode.com/problems/missing-number/description/) [https://leetcode.com/problems/find-the-duplicate-number/description/](https://leetcode.com/problems/find-the-duplicate-number/description/) [https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/) [https://leetcode.com/problems/find-all-duplicates-in-an-array/description/](https://leetcode.com/problems/find-all-duplicates-in-an-array/description/) [https://leetcode.com/problems/couples-holding-hands/description/](https://leetcode.com/problems/couples-holding-hands/description/)

## Peak and Valley

[https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/)

sliding to find peak or valley

```java
for (int i = 0; i < prices.length; i++) {
    while (i < prices.length - 1 && prices[i] >= prices[i+1]) i++;
    buy = prices[i];

    while (i < prices.length - 1 && prices[i] <= prices[i+1]) i++;
    profit += prices[i] - buy;
}
```

## K sum problem

2 pointers: [https://leetcode.com/problems/two-sum](https://leetcode.com/problems/two-sum) [https://leetcode.com/problems/two-sum-ii-input-array-is-sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted) [https://leetcode.com/problems/two-sum-iii-data-structure-design](https://leetcode.com/problems/two-sum-iii-data-structure-design) [https://leetcode.com/problems/two-sum-iv-input-is-a-bst](https://leetcode.com/problems/two-sum-iv-input-is-a-bst) [https://leetcode.com/problems/3sum](https://leetcode.com/problems/3sum) [https://leetcode.com/problems/3sum-closest](https://leetcode.com/problems/3sum-closest) [https://leetcode.com/problems/3sum-smaller](https://leetcode.com/problems/3sum-smaller) [https://leetcode.com/problems/valid-triangle-number/description/](https://leetcode.com/problems/valid-triangle-number/description/) [https://leetcode.com/problems/4sum](https://leetcode.com/problems/4sum) [https://leetcode.com/problems/4sum-ii](https://leetcode.com/problems/4sum-ii)

hash map:

[https://leetcode.com/problems/two-sum](https://leetcode.com/problems/two-sum) [https://leetcode.com/problems/subarray-sum-equals-k/description/](https://leetcode.com/problems/subarray-sum-equals-k/description/) [https://leetcode.com/problems/two-sum-iv-input-is-a-bst](https://leetcode.com/problems/two-sum-iv-input-is-a-bst) [https://leetcode.com/problems/4sum-ii](https://leetcode.com/problems/4sum-ii)

## Avoid duplicate

1\) sort and skip same value, if (nums\[i] == nums\[i-1]) continue; [https://leetcode.com/problems/combination-sum-ii/description/](https://leetcode.com/problems/combination-sum-ii/description/) 2) when it can't be sort, use array or set at this level; [https://leetcode.com/problems/increasing-subsequences/description/](https://leetcode.com/problems/increasing-subsequences/description/)

## Trap water

[https://leetcode.com/problems/container-with-most-water/description/](https://leetcode.com/problems/container-with-most-water/description/) [https://leetcode.com/problems/trapping-rain-water/description/](https://leetcode.com/problems/trapping-rain-water/description/) [https://leetcode.com/problems/trapping-rain-water-ii](https://leetcode.com/problems/trapping-rain-water-ii)

## Two pass

1 from left and 1 from right:

* [0238. Product of Array Except Self](../solutions/0238-product-of-array-except-self)
* [0581. Shortest Unsorted Continuous Subarray](../solutions/0581-shortest-unsorted-continuous-subarray)
