0%

1. Repeated String Match
1. Longest Univalue Path
1. Knight Probability in Chessboard
1. Maximum Sum of 3 Non-Overlapping Subarrays

### 686. Repeated String Match

Given two strings A and B, find the minimum number of times A has to be repeated such that B is a substring of it. If no such solution, return -1.

For example, with A = "abcd" and B = "cdabcdab".

Return 3, because by repeating A three times (“abcdabcdabcd”), B is a substring of it; and B is not a substring of A repeated two times ("abcdabcd").

Note: The length of A and B will be between 1 and 10000.

Imagine we wrote S = A+A+A+.... If B is to be a substring of S, we only need to check whether some S[0:], S[1:], ..., S[len(A) - 1:] starts with B, as S is long enough to contain B, and S has period at most len(A).

Now, suppose q is the least number for which len(B) <= len(A * q). We only need to check whether B is a substring of A * q or A * (q+1). If we try k < q, then B has larger length than A * q and therefore can't be a substring. When k = q+1, A * k is already big enough to try all positions for B; namely, A[i:i+len(B)] == B for i = 0, 1, ..., len(A) - 1.

B如果是S=AAAA的字串，那么必然有B的长度不大于S。

len(B) <= len(Aq)，只需要查看B是否为 A q 或者A * (q+1)的字串。因为如果尝试的k<q，那么不够长。当k=q+1的时候，A*k已经足够长来包括所有的B了， A[i:i+len(B)] == B for i = 0, 1, ..., len(A) - 1.

KMP，对B求失配函数，然后匹配A。如果每次A遍历完一次而出现了重复的B中的下标，说明无解。

Rolling Hash

### 687. Longest Univalue Path

Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the root.

Note: The length of path between two nodes is represented by the number of edges between them.

Example 1:

Input:

Output:

Example 2:

Input:

Output:

Note: The given binary tree has not more than 10000 nodes. The height of the tree is not more than 1000.

### 688. Knight Probability in Chessboard

On an NxN chessboard, a knight starts at the r-th row and c-th column and attempts to make exactly Kmoves. The rows and columns are 0 indexed, so the top-left square is (0, 0), and the bottom-right square is (N-1, N-1).

A chess knight has 8 possible moves it can make, as illustrated below. Each move is two squares in a cardinal direction, then one square in an orthogonal direction. Each time the knight is to move, it chooses one of eight possible moves uniformly at random (even if the piece would go off the chessboard) and moves there.

The knight continues moving until it has made exactly K moves or has moved off the chessboard. Return the probability that the knight remains on the board after it has stopped moving.

Example:

Note:

• N will be between 1 and 25.
• K will be between 0 and 100.
• The knight always initially starts on the board.

### 689. Maximum Sum of 3 Non-Overlapping Subarrays

In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum.

Each subarray will be of size k, and we want to maximize the sum of all 3*k entries.

Return the result as a list of indices representing the starting position of each interval (0-indexed). If there are multiple answers, return the lexicographically smallest one.

Example:

Note:

• nums.length will be between 1 and 20000.
• nums[i] will be between 1 and 65535.
• k will be between 1 and floor(nums.length / 3).

b段不重复的就可以多了一个元素，即[i-k-1,i-1]，而这个元素要能在最大的a、b段中，必然加上之前的最大值。

• three = pre_sum[i + k - 1] - pre_sum[i - 1]
• two = pre_sum[i - 1] - pre_sum[i - 1 - k]
• one = pre_sum[i - 1 - k] - pre_sum[i - 1 - (k << 1)]

Python 72ms

1. Repeated String Match
1. Longest Univalue Path
1. Knight Probability in Chessboard
1. Maximum Sum of 3 Non-Overlapping Subarrays