本次题解包括:
- Repeated String Match
- Longest Univalue Path
- Knight Probability in Chessboard
- 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
andB
will be between 1 and 10000.
题目地址:leetcode Repeated String Match
题意:
给定字符串A和字符串B,问B是A重复几次的字串。(这里的字串是连续的,比如abcd中的ad就不是字串)
思路:
方法一
引用solution的写的很清楚:
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.
1 | class Solution(object): |
方法二
KMP,对B求失配函数,然后匹配A。如果每次A遍历完一次而出现了重复的B中的下标,说明无解。
1 | class Solution(object): |
方法三
Rolling Hash
见: https://leetcode.com/articles/repeated-string-match/
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:
1
2
3
4
5 5
/ \
4 5
/ \ \
1 1 5Output:
1 2Example 2:
Input:
1
2
3
4
5 1
/ \
4 5
/ \ \
4 4 5Output:
1 2Note: The given binary tree has not more than 10000 nodes. The height of the tree is not more than 1000.
题目地址:leetcode Longest Univalue Path
题意:给定一颗二叉树,求相同元素组成的最大路径长度。注意最大路径长度为路径上边的个数。
思路:
写个递归,返回左or右的最大路径即可
1 | class Solution(object): |
688. Knight Probability in Chessboard
On an
N
xN
chessboard, a knight starts at ther
-th row andc
-th column and attempts to make exactlyK
moves. 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:
1
2
3
4
5 Input: 3, 2, 0, 0
Output: 0.0625
Explanation: There are two moves (to (1,2), (2,1)) that will keep the knight on the board.
From each of those positions, there are also two moves that will keep the knight on the board.
The total probability the knight stays on the board is 0.0625.Note:
N
will be between 1 and 25.K
will be between 0 and 100.- The knight always initially starts on the board.
题目地址:leetcode Knight Probability in Chessboard
题意:给定一个N*N的国际象棋棋盘,初始的时候在r,c这个位置。骑士每步可以如上图那样走动。问经过k步之后,你的骑士还有多大的概率在棋盘上。
思路:
记忆化搜索
1 | class Solution(object): |
写成迭代貌似速度还慢了。。
1 | class Solution(object): |
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 all3*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:
1
2
3
4 Input: [1,2,1,2,6,7,5,1], 2
Output: [0, 3, 5]
Explanation: Subarrays [1, 2], [2, 6], [7, 5] correspond to the starting indices [0, 3, 5].
We could have also taken [2, 1], but an answer of [1, 3, 5] would be lexicographically larger.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).
题目地址:leetcode Maximum Sum of 3 Non-Overlapping Subarrays
题意:给定一个数组nums和数字k,求数组中三个长度为k的不重叠子数组,它们的和最大。返回这三个数组的起始坐标即可。
思路:
方法一
朴素的方法:
1 | ---------------- |
枚举ab、bi、i之后的三段O(n^3),不用看也知道TLE
如何加速?
为了方便描述,下面将ab、bi、i之后三段称为a、b、i
如果我们枚举i,对于当前的这个i,那么我们要是知道a、b两段的和的最大值就好了。
换句话说,假设已知a、b两段的和的最大值,那么我们只需要枚举i即可。
因此我们可以用dp[k][i]来表示前i个数字分为k-1段的最大和。
则显然有dp[k][i] = max(dp[k – 1][i – k] + sum[i -k + 1…i], dp[k][i – 1])
最后回溯恢复下标即可。
1 | class Solution { |
以前写的:
那么如何只枚举i的时候更新前面两段最大值?
注意到i每次向右滑动一个(就是i+1)的时候,
b段不重复的就可以多了一个元素,即[i-k-1,i-1],而这个元素要能在最大的a、b段中,必然加上之前的最大值。
于是维护max_interval为a段、a、b两段、a、b、i三段的最大值,对应的下标为max_index,初始值max_index = [[0], [0, k], [0, k, k << 1]]
然后枚举i,范围为:[(k << 1) + 1, len(nums) - k + 1)
分别计算现在新增的三段:pre_sum为累计和
- 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)]
然后更新各段维护的最大值和其下标即可。
第一次写的时候下标max_index更新错了。因为可能第一段更新而第二段没有更新,第三段更新这种,然后max_index[0]和max_index[1]的区间是重叠的。改了就对了。
Python 72ms
1 | class Solution(object): |
方法二
分为左、中右三段,预处理左右两段k个数的最大值。
1 | class Solution(object): |
本次是 leetcode 如下的题解
- Repeated String Match
- Longest Univalue Path
- Knight Probability in Chessboard
- Maximum Sum of 3 Non-Overlapping Subarrays
更多题解可以查看:https://www.hrwhisper.me/leetcode-algorithm-solution/