Skip to content

Commit 0498712

Browse files
committed
Solution of 1037, 1038, 1039
1 parent cbcdc21 commit 0498712

File tree

4 files changed

+141
-0
lines changed

4 files changed

+141
-0
lines changed

1000-1100q/1037.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'''
2+
A boomerang is a set of 3 points that are all distinct and not in a straight line.
3+
4+
Given a list of three points in the plane, return whether these points are a boomerang.
5+
6+
7+
8+
Example 1:
9+
10+
Input: [[1,1],[2,3],[3,2]]
11+
Output: true
12+
Example 2:
13+
14+
Input: [[1,1],[2,2],[3,3]]
15+
Output: false
16+
17+
18+
Note:
19+
20+
points.length == 3
21+
points[i].length == 2
22+
0 <= points[i][j] <= 100
23+
'''
24+
25+
class Solution(object):
26+
def isBoomerang(self, points):
27+
"""
28+
:type points: List[List[int]]
29+
:rtype: bool
30+
"""
31+
x1, x2, x3, y1, y2, y3 = points[0][0], points[1][0], points[2][0], points[0][1], points[1][1] ,points[2][1]
32+
if ((y3 - y2)*(x2 - x1) == (y2 - y1)*(x3 - x2)):
33+
return False
34+
return True

1000-1100q/1038.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'''
2+
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.
3+
4+
As a reminder, a binary search tree is a tree that satisfies these constraints:
5+
6+
The left subtree of a node contains only nodes with keys less than the node's key.
7+
The right subtree of a node contains only nodes with keys greater than the node's key.
8+
Both the left and right subtrees must also be binary search trees.
9+
10+
11+
Example 1:
12+
Input: [4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
13+
Output: [30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]
14+
15+
16+
Note:
17+
18+
The number of nodes in the tree is between 1 and 100.
19+
Each node will have value between 0 and 100.
20+
The given tree is a binary search tree.
21+
'''
22+
23+
24+
# Definition for a binary tree node.
25+
# class TreeNode(object):
26+
# def __init__(self, x):
27+
# self.val = x
28+
# self.left = None
29+
# self.right = None
30+
31+
class Solution(object):
32+
def bstToGst(self, root):
33+
"""
34+
:type root: TreeNode
35+
:rtype: TreeNode
36+
"""
37+
self.curr_sum = 0
38+
def greaterSum(root):
39+
if not root:
40+
return
41+
greaterSum(root.right)
42+
self.curr_sum += root.val
43+
root.val = self.curr_sum
44+
greaterSum(root.left)
45+
46+
greaterSum(root)
47+
return root

1000-1100q/1039.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'''
2+
Given N, consider a convex N-sided polygon with vertices labelled A[0], A[i], ..., A[N-1] in clockwise order.
3+
4+
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.
5+
6+
Return the smallest possible total score that you can achieve with some triangulation of the polygon.
7+
8+
9+
10+
Example 1:
11+
12+
Input: [1,2,3]
13+
Output: 6
14+
Explanation: The polygon is already triangulated, and the score of the only triangle is 6.
15+
Example 2:
16+
17+
3 - 7 3 - 7
18+
| / | | \ |
19+
5 - 4 5 - 4
20+
21+
Input: [3,7,4,5]
22+
Output: 144
23+
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.
24+
Example 3:
25+
26+
Input: [1,3,1,4,1,5]
27+
Output: 13
28+
Explanation: The minimum score triangulation has score 1*1*3 + 1*1*4 + 1*1*5 + 1*1*1 = 13.
29+
30+
31+
Note:
32+
33+
3 <= A.length <= 50
34+
1 <= A[i] <= 100
35+
'''
36+
37+
class Solution(object):
38+
def minScoreTriangulation(self, A):
39+
"""
40+
:type A: List[int]
41+
:rtype: int
42+
"""
43+
44+
n = len(A)
45+
dp = [[0]*n for _ in range(n)]
46+
for length in range(n):
47+
index_i = 0
48+
for index_j in range(length, n):
49+
if index_j < index_i+2:
50+
dp[index_i][index_j] = 0
51+
else:
52+
dp[index_i][index_j] = float('inf')
53+
for index_k in range(index_i+1, index_j):
54+
val = dp[index_i][index_k] + dp[index_k][index_j] + (A[index_i]*A[index_k]*A[index_j])
55+
dp[index_i][index_j] = min(dp[index_i][index_j], val)
56+
index_i += 1
57+
return dp[0][n-1]

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
77
##### [Problems 1000-1100](./1000-1100q/)
88
| # | Title | Solution | Difficulty |
99
|---| ----- | -------- | ---------- |
10+
|1039|[Minimum Score Triangulation of Polygon](https://leetcode.com/problems/minimum-score-triangulation-of-polygon)|[Python](./1000-1100q/1039.py)|Medium|
11+
|1038|[Binary Search Tree to Greater Sum Tree](https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/)|[Python](./1000-1100q/1038.py)|Medium|
12+
|1037|[Valid Boomerang](https://leetcode.com/problems/valid-boomerang/)|[Python](./1000-1100q/1037.py)|Easy|
1013
|1035|[Uncrossed Lines](https://leetcode.com/problems/uncrossed-lines/)|[Python](./1000-1100q/1035.py)|Medium|
1114
|1034|[Coloring A Border](https://leetcode.com/problems/coloring-a-border/)|[Python](./1000-1100q/1034.py)|Medium|
1215
|1033|[Moving Stones Until Consecutive](https://leetcode.com/problems/moving-stones-until-consecutive)|[Python](./1000-1100q/1033.py)|Easy

0 commit comments

Comments
 (0)