leetcode contest 52 solution

本次题解包括:

  • 686. Repeated String Match
  • 687. Longest Univalue Path
  • 688. Knight Probability in Chessboard
  • 689. 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.

题目地址: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(A*q),只需要查看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

见: 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:

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.

题目地址:leetcode Longest Univalue Path

题意:给定一颗二叉树,求相同元素组成的最大路径长度。注意最大路径长度为路径上边的个数。

思路:

写个递归,返回左or右的最大路径即可

 

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.

题目地址:leetcode Knight Probability in Chessboard

题意:给定一个N*N的国际象棋棋盘,初始的时候在r,c这个位置。骑士每步可以如上图那样走动。问经过k步之后,你的骑士还有多大的概率在棋盘上。

思路:

记忆化搜索

 

写成迭代貌似速度还慢了。。

 

 

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).

题目地址:leetcode Maximum Sum of 3 Non-Overlapping Subarrays

题意:给定一个数组nums和数字k,求数组中三个长度为k的不重叠子数组,它们的和最大。返回这三个数组的起始坐标即可。

思路:

方法一

朴素的方法:

枚举a~b、b~i、i之后的三段O(n^3),不用看也知道TLE

如何加速?

为了方便描述,下面将a~b、b~i、i之后三段称为a、b、i

如果我们枚举i,对于当前的这个i,那么我们要是知道a、b两段的和的最大值就好了。

换句话说,假设已知a、b两段的和的最大值,那么我们只需要枚举i即可。

那么如何只枚举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

 

方法二

分为左、中右三段,预处理左右两段k个数的最大值。

 

本次是 leetcode 如下的题解

  • 686. Repeated String Match
  • 687. Longest Univalue Path
  • 688. Knight Probability in Chessboard
  • 689. Maximum Sum of 3 Non-Overlapping Subarrays

更多题解可以查看:https://www.hrwhisper.me/leetcode-algorithm-solution/

本博客若无特殊说明则由 hrwhisper 原创发布
转载请点名出处:细语呢喃 > leetcode contest 52 solution
本文地址:https://www.hrwhisper.me/leetcode-contest-52-solution/

打赏一杯咖啡钱呗

Leetcode , . permalink.

3 thoughts on “leetcode contest 52 solution

Leave a Reply

Your email address will not be published. Required fields are marked *