20200920 第207场周赛
第一题
题目描述
给你一个字符串 text ,该字符串由若干被空格包围的单词组成。每个单词由一个或者多个小写英文字母组成,并且两个单词之间至少存在一个空格。题目测试用例保证 text 至少包含一个单词 。
请你重新排列空格,使每对相邻单词之间的空格数目都 相等 ,并尽可能 最大化 该数目。如果不能重新平均分配所有空格,请 将多余的空格放置在字符串末尾 ,这也意味着返回的字符串应当与原 text 字符串的长度相等。
返回 重新排列空格后的字符串 。
示例 1:
输入:text = ” this is a sentence “
输出:“this is a sentence”
解释:总共有 9 个空格和 4 个单词。可以将 9 个空格平均分配到相邻单词之间,相邻单词间空格数为:9 / (4-1) = 3 个。
示例 2:
输入:text = ” practice makes perfect”
输出:”practice makes perfect “
解释:总共有 7 个空格和 3 个单词。7 / (3-1) = 3 个空格加上 1 个多余的空格。多余的空格需要放在字符串的末尾。
示例 3:
输入:text = “hello world”
输出:“hello world”
示例 4:
输入:text = ” walks udp package into bar a”
输出:”walks udp package into bar a “
示例 5:
输入:text = “a”
输出:“a”
标签
字符串
python解法
class Solution:
def reorderSpaces(self, text: str) -> str:
words = text.split()
count = 0
for i in text:
if i == ' ':
count += 1
s = ''
length = len(words)
count1 = count
for i in range(length):
s += words[i]
if i!=length-1:
for j in range(count // (length - 1)):
s += ' '
count1 -= 1
for i in range(count1):
s += ' '
return s
第二题
题目描述
给你一个字符串 s ,请你拆分该字符串,并返回拆分后唯一子字符串的最大数目。
字符串 s 拆分后可以得到若干 非空子字符串 ,这些子字符串连接后应当能够还原为原字符串。但是拆分出来的每个子字符串都必须是 唯一的 。
注意:子字符串 是字符串中的一个连续字符序列。
示例 1:
输入:s = “ababccc”
输出:5
解释:一种最大拆分方法为 [‘a’, ‘b’, ‘ab’, ‘c’, ‘cc’] 。像 [‘a’, ‘b’, ‘a’, ‘b’, ‘c’, ‘cc’] 这样拆分不满足题目要求,因为其中的 ‘a’ 和 ‘b’ 都出现了不止一次。
示例 2:
输入:s = “aba”
输出:2
解释:一种最大拆分方法为 [‘a’, ‘ba’] 。
示例 3:
输入:s = “aa”
输出:1
解释:无法进一步拆分字符串。
解析
回溯,判断每个位置是否需要切割,
python解法
class Solution:
def maxUniqueSplit(self, s: str) -> int:
strings = []
def splitString(s, i):
if s in strings:
return 0
if i == len(s):
return 1
num1 = 1
if s[:i] not in strings:
strings.append(s[:i])
num1 = 1 + splitString(s[i:], 1)
strings.pop(-1)
num2 = splitString(s, i + 1)
return max(num1, num2)
return splitString(s, 1)
第三题
题目描述
给你一个大小为 rows x cols 的矩阵 grid 。最初,你位于左上角 (0, 0) ,每一步,你可以在矩阵中 向右 或 向下 移动。
在从左上角 (0, 0) 开始到右下角 (rows – 1, cols – 1) 结束的所有路径中,找出具有 最大非负积 的路径。路径的积是沿路径访问的单元格中所有整数的乘积。
返回 最大非负积 对 109 + 7 取余 的结果。如果最大积为负数,则返回 -1 。
注意,取余是在得到最大积之后执行的。
示例 1:
输入:grid = [[-1,-2,-3],
[-2,-3,-3],
[-3,-3,-2]]
输出:-1
解释:从 (0, 0) 到 (2, 2) 的路径中无法得到非负积,所以返回 -1
示例 2:
输入:grid = [[1,-2,1],
[1,-2,1],
[3,-4,1]]
输出:8
解释:最大非负积对应的路径已经用粗体标出 (1 * 1 * -2 * -4 * 1 = 8)
示例 3:
输入:grid = [[1, 3],
[0,-4]]
输出:0
解释:最大非负积对应的路径已经用粗体标出 (1 * 0 * -4 = 0)
示例 4:
输入:grid = [[ 1, 4,4,0],
[-2, 0,0,1],
[ 1,-1,1,1]]
输出:2
解释:最大非负积对应的路径已经用粗体标出 (1 * -2 * 1 * -1 * 1 * 1 = 2)
提示:
- 1 <= rows, cols <= 15
- -4 <= grid[i][j] <= 4
标签
动态规划
解析
动态规划题,注意状态转移方程是遍历整个图形的就行。
python解法
class Solution:
def maxProductPath(self, grid) -> int:
rows, cols = len(grid), len(grid[0])
maxNums = [[0] * cols for i in range(rows)]
minNums = [[0] * cols for i in range(rows)]
maxNums[0][0] = minNums[0][0] = grid[0][0]
for i in range(1, rows):
maxNums[i][0] = minNums[i][0] = maxNums[i - 1][0] * grid[i][0]
for i in range(1, cols):
maxNums[0][i] = minNums[0][i] = maxNums[0][i - 1] * grid[0][i]
for i in range(1, rows):
for j in range(1, cols):
if grid[i][j] == 0:
maxNums[i][j] = minNums[i][j] = 0
elif grid[i][j] > 0:
maxNums[i][j] = grid[i][j] * max(maxNums[i - 1][j],
maxNums[i][j - 1])
minNums[i][j] = grid[i][j] * min(minNums[i - 1][j],
minNums[i][j - 1])
elif grid[i][j] < 0:
minNums[i][j] = grid[i][j] * max(maxNums[i - 1][j],
maxNums[i][j - 1])
maxNums[i][j] = grid[i][j] * min(minNums[i - 1][j],
minNums[i][j - 1])
return maxNums[rows - 1][cols - 1] % (10**9 +7) if maxNums[rows-1][cols-1] > -1 else -1
第四题
不会