笔记仓库

正常人的正常笔记集

LeetCode Weekly Contest 135

https://leetcode.com/contest/weekly-contest-135

Valid Boomerang

原题地址 https://leetcode.com/contest/weekly-contest-135/problems/valid-boomerang/

A boomerang is a set of 3 points that are all distinct and not in a straight line.

Given a list of three points in the plane, return whether these points are a boomerang.

Example 1:

1
2
Input: [[1,1],[2,3],[3,2]]
Output: true

Example 2:

1
2
Input: [[1,1],[2,2],[3,3]]
Output: false

Note:

  1. points.length == 3
  2. points[i].length == 2
  3. 0 <= points[i][j] <= 100

对于[(x1,y1),(x2,y2),(x3,y3)]检验(x1-x2)/(y1-y2)!=(x1-x3)/(y1-y3),同分一下比较两边乘式顺便就把包含重复点的情况也考虑在内了。

1
2
3
4
5
6
7
8
9
class Solution(object):
def isBoomerang(self, points):
"""
:type points: List[List[int]]
:rtype: bool
"""
(x1, y1), (x2, y2), (x3, y3) = tuple(points)
left, right = (x1 - x2) * (y1 - y3), (x1 - x3) * (y1 - y2)
return left != right

Binary Search Tree to Greater Sum Tree

原题地址 https://leetcode.com/contest/weekly-contest-135/problems/binary-search-tree-to-greater-sum-tree/

Given the root of a binary search tree with distinct values, modify it so that every node has a new value equal to the sum of the values of the original tree that are greater than or equal to node.val.

As a reminder, a binary search tree is a tree that satisfies these constraints:

  • The left subtree of a node contains only nodes with keys less than the node’s key.
  • The right subtree of a node contains only nodes with keys greater than the node’s key.
  • Both the left and right subtrees must also be binary search trees.

Example 1:

1
2
Input: [4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
Output: [30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]

Note:

  1. The number of nodes in the tree is between 1 and 100.
  2. Each node will have value between 0 and 100.
  3. The given tree is a binary search tree.

调整一下dfs遍历顺序,从右往左遍历,从右向左把遍历到的结点放在self.temp,接下来被遍历到的结点的值只需要再加上self.temp,然后再被记录在self.temp开始遍历下一个结点。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None


class Solution(object):
def bstToGst(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
"""
self.temp = 0

def right_bfs(r):
if not r:
return
right_bfs(r.right)
r.val += self.temp
self.temp = r.val
right_bfs(r.left)

right_bfs(root)
return root

Minimum Score Triangulation of Polygon

原题地址 https://leetcode.com/contest/weekly-contest-135/problems/minimum-score-triangulation-of-polygon/

Given N, consider a convex N-sided polygon with vertices labelled A[0], A[i], ..., A[N-1] in clockwise order.

Suppose you triangulate the polygon into N-2 triangles. For each triangle, the value of that triangle is the product of the labels of the vertices, and the total score of the triangulation is the sum of these values over all N-2 triangles in the triangulation.

Return the smallest possible total score that you can achieve with some triangulation of the polygon.

Example 1:

1
2
3
Input: [1,2,3]
Output: 6
Explanation: The polygon is already triangulated, and the score of the only triangle is 6.

Example 2:

1
2
3
Input: [3,7,4,5]
Output: 144
Explanation: There are two triangulations, with possible scores: 3*7*5 + 4*5*7 = 245, or 3*4*5 + 3*4*7 = 144. The minimum score is 144.

Example 3:

1
2
3
Input: [1,3,1,4,1,5]
Output: 13
Explanation: The minimum score triangulation has score 1*1*3 + 1*1*4 + 1*1*5 + 1*1*1 = 13.

Note:

  1. 3 <= A.length <= 50
  2. 1 <= A[i] <= 100

dp[i][j](i<j)用来表示从A[i]A[j]顺序构造的多边形划分为三角形后最高的得分,可以找一个i<k<jA[k]先构造以A[i],A[j],A[k]为顶点的三角形,那么有dp[i][j]可以是所有i<k<jk中使得dp[i][k]+dp[k][j]+A[i]*A[j]*A[k]最大的。从底向上进行动态规划时,逐步增大多边形的顶点数,即j-i+1,当顶点数为n时找到整个问题的解。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution(object):
def minScoreTriangulation(self, A):
"""
:type A: List[int]
:rtype: int
"""
n = len(A)
dp = [[0] * n for _ in range(n)]

for d in range(2, n):
for i in range(n - d):
j = i + d
dp[i][j] = min(dp[i][k] + dp[k][j] + A[i] * A[j] * A[k]
for k in range(i + 1, j))
return dp[0][-1]

Moving Stones Until Consecutive II

On an infinite number line, the position of the i-th stone is given by stones[i]. Call a stone an endpoint stone if it has the smallest or largest position.

Each turn, you pick up an endpoint stone and move it to an unoccupied position so that it is no longer an endpoint stone.

In particular, if the stones are at say, stones = [1,2,5], you cannot move the endpoint stone at position 5, since moving it to any position (such as 0, or 3) will still keep that stone as an endpoint stone.

The game ends when you cannot make any more moves, ie. the stones are in consecutive positions.

When the game ends, what is the minimum and maximum number of moves that you could have made? Return the answer as an length 2 array: answer = [minimum_moves, maximum_moves]

Example 1:

1
2
3
4
5
Input: [7,4,9]
Output: [1,2]
Explanation:
We can move 4 -> 8 for one move to finish the game.
Or, we can move 9 -> 5, 4 -> 6 for two moves to finish the game.

Example 2:

1
2
3
4
5
Input: [6,5,4,3,10]
Output: [2,3]
We can move 3 -> 8 then 10 -> 7 to finish the game.
Or, we can move 3 -> 7, 4 -> 8, 5 -> 9 to finish the game.
Notice we cannot move 10 -> 2 to finish the game, because that would be an illegal move.

Example 3:

1
2
Input: [100,101,104,102,103]
Output: [0,0]

Note:

  1. 3 <= stones.length <= 10^4
  2. 1 <= stones[i] <= 10^9
  3. stones[i] have distinct values.

解法见这个讨论,下界需要考虑两种方向的的移动,把A[n-1]移动到A[n-2]-1的位置,最后排成以A[0]开始的n个连续石头,也是一种可能。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution(object):
def numMovesStonesII(self, A):
"""
:type stones: List[int]
:rtype: List[int]
"""
A.sort()
i, n, low = 0, len(A), len(A)
high = max(A[-1] - n + 2 - A[1], A[-2] - A[0] - n + 2)
for j in range(n):
while A[j] - A[i] >= n: i += 1
if j - i + 1 == n - 1 and A[j] - A[i] == n - 2:
low = min(low, 2)
else:
low = min(low, n - (j - i + 1))
return [low, high]