LeetCode 五月挑战专题每日随缘更新，点击类别 2020 May LeetCoding Challenge 查看更多。

## May 22nd: Sort Characters By Frequency

Given a string, sort it in decreasing order based on the frequency of characters.

Example 1:

Example 2:

Example 3:

## May 23rd: Interval List Intersections

Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order.

Return the intersection of these two interval lists.

(Formally, a closed interval `[a, b]` (with `a <= b`) denotes the set of real numbers `x` with `a <= x <= b`. The intersection of two closed intervals is a set of real numbers that is either empty, or can be represented as a closed interval. For example, the intersection of [1, 3] and [2, 4] is [2, 3].)

Example 1:

Note:

• `0 <= A.length < 1000`

• `0 <= B.length < 1000`

• `0 <= A[i].start, A[i].end, B[i].start, B[i].end < 10^9`

### 题目解析

• 如果 `a` 的右界小于 `b` 的左界，说明两者不相交，而且因为 `B` 有序，所以 `b` 之后的区间也不可能与 `a` 相交，跳出小循环。

• 如果 `a` 的左界大于 `b` 的右界，说明两者不相交，进入下一次小循环。

## May 24th: Construct Binary Search Tree from Preorder Traversal

Return the root node of a binary search tree that matches the given `preorder` traversal.

(Recall that a binary search tree is a binary tree where for every node, any descendant of `node.left` has a value `< node.val`, and any descendant of `node.right` has a value `> node.val`. Also recall that a preorder traversal displays the value of the `node` first, then traverses `node.left`, then traverses `node.right`.)

It’s guaranteed that for the given test cases there is always possible to find a binary search tree with the given requirements.

Example 1:

Constraints:

• `1 <= preorder.length <= 100`

• `1 <= preorder[i] <= 10^8`

• The values of `preorder` are distinct.

### 题目解析

• 对于根节点，因为 `preorder` 是前序遍历，所以根节点必然是 `preorder[beg]`

• 对于左子树和右子树，在 `preorder``[beg+1, end]` 区间内遍历寻找第一个大于 `preorder[beg]` 的数字，其下标为 `idx`，那么左子树必然是 `preorder[beg+1, idx-1]`，右子树必然是 `preorder[idx, end]`

## May 25th: Uncrossed Lines

We write the integers of `A` and `B` (in the order they are given) on two separate horizontal lines.

Now, we may draw connecting lines: a straight line connecting two numbers `A[i]` and `B[j]` such that:

• `A[i] == B[j]`;

• The line we draw does not intersect any other connecting (non-horizontal) line.

Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line.

Return the maximum number of connecting lines we can draw in this way.

Example 1:

Example 2:

Example 3:

Note:

• `1 <= A.length <= 500`

• `1 <= B.length <= 500`

• `1 <= A[i], B[i] <= 2000`

### 题目解析

P.S. 嗨，看着动态规划的代码感觉还挺好理解的，自己写就写不出来。

## May 26th: Contiguous Array

Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1.

Example 1:

Example 2:

Note: The length of the given binary array will not exceed 50,000.

## May 27th: Possible Bipartition

Given a set of `N` people (numbered `1, 2, ..., N`), we would like to split everyone into two groups of any size.

Each person may dislike some other people, and they should not go into the same group.

Formally, if `dislikes[i] = [a, b]`, it means it is not allowed to put the people numbered `a` and `b` into the same group.

Return `true` if and only if it is possible to split everyone into two groups in this way.

Example 1:

Example 2:

Example 3:

Note:

• `1 <= N <= 2000`

• `0 <= dislikes.length <= 10000`

• `1 <= dislikes[i][j] <= N`

• `dislikes[i] < dislikes[i]`

• There does not exist `i != j` for which `dislikes[i] == dislikes[j]`.

## May 28th: Counting Bits

Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1’s in their binary representation and return them as an array.

Example 1:

Example 2:

### 题目解析

#### C++ 实现 微信 支付宝