Skip to content

Commit 266f8b3

Browse files
committed
Latest problem solutions
1 parent d4a7298 commit 266f8b3

File tree

4 files changed

+182
-0
lines changed

4 files changed

+182
-0
lines changed

Diff for: 1000-1100q/1041.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'''
2+
On an infinite plane, a robot initially stands at (0, 0) and faces north. The robot can receive one of three instructions:
3+
4+
"G": go straight 1 unit;
5+
"L": turn 90 degrees to the left;
6+
"R": turn 90 degress to the right.
7+
The robot performs the instructions given in order, and repeats them forever.
8+
9+
Return true if and only if there exists a circle in the plane such that the robot never leaves the circle.
10+
11+
12+
13+
Example 1:
14+
15+
Input: "GGLLGG"
16+
Output: true
17+
Explanation:
18+
The robot moves from (0,0) to (0,2), turns 180 degrees, and then returns to (0,0).
19+
When repeating these instructions, the robot remains in the circle of radius 2 centered at the origin.
20+
Example 2:
21+
22+
Input: "GG"
23+
Output: false
24+
Explanation:
25+
The robot moves north indefinetely.
26+
Example 3:
27+
28+
Input: "GL"
29+
Output: true
30+
Explanation:
31+
The robot moves from (0, 0) -> (0, 1) -> (-1, 1) -> (-1, 0) -> (0, 0) -> ...
32+
33+
34+
Note:
35+
36+
1 <= instructions.length <= 100
37+
instructions[i] is in {'G', 'L', 'R'}
38+
'''
39+
40+
class Solution(object):
41+
def isRobotBounded(self, instructions):
42+
"""
43+
:type instructions: str
44+
:rtype: bool
45+
"""
46+
start_x, start_y = 0, 0
47+
left, direct = 0, 0
48+
moves = [[0, 1], [-1, 0], [0, -1], [1, 0]]
49+
instructions = instructions*4
50+
for instruction in instructions:
51+
if instruction == 'G':
52+
start_x += moves[direct][0]
53+
start_y += moves[direct][1]
54+
elif instruction == 'L':
55+
direct = (direct+1)%4
56+
elif instruction == 'R':
57+
direct = (direct+3)%4
58+
59+
if(start_x == 0 and start_y == 0):
60+
return True
61+
return False

Diff for: 1000-1100q/1042.py

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
'''
2+
You have N gardens, labelled 1 to N. In each garden, you want to plant one of 4 types of flowers.
3+
4+
paths[i] = [x, y] describes the existence of a bidirectional path from garden x to garden y.
5+
6+
Also, there is no garden that has more than 3 paths coming into or leaving it.
7+
8+
Your task is to choose a flower type for each garden such that, for any two gardens connected by a path, they have different types of flowers.
9+
10+
Return any such a choice as an array answer, where answer[i] is the type of flower planted in the (i+1)-th garden. The flower types are denoted 1, 2, 3, or 4. It is guaranteed an answer exists.
11+
12+
13+
14+
Example 1:
15+
16+
Input: N = 3, paths = [[1,2],[2,3],[3,1]]
17+
Output: [1,2,3]
18+
Example 2:
19+
20+
Input: N = 4, paths = [[1,2],[3,4]]
21+
Output: [1,2,1,2]
22+
Example 3:
23+
24+
Input: N = 4, paths = [[1,2],[2,3],[3,4],[4,1],[1,3],[2,4]]
25+
Output: [1,2,3,4]
26+
27+
28+
Note:
29+
30+
1 <= N <= 10000
31+
0 <= paths.size <= 20000
32+
No garden has 4 or more paths coming into or leaving it.
33+
It is guaranteed an answer exists.
34+
'''
35+
36+
class Solution(object):
37+
def gardenNoAdj(self, N, paths):
38+
"""
39+
:type N: int
40+
:type paths: List[List[int]]
41+
:rtype: List[int]
42+
"""
43+
plant = [1, 2, 3, 4]
44+
result = [0 for _ in range(N)]
45+
if not paths:
46+
return [plant[index%4] for index in range(N)]
47+
# print result
48+
change = {}
49+
update = []
50+
for path in paths:
51+
x, y = path[0]-1, path[1]-1
52+
53+
if x in change:
54+
change[x].append(y)
55+
else:
56+
change[x] = [y]
57+
58+
if y in change:
59+
change[y].append(x)
60+
else:
61+
change[y] = [x]
62+
63+
for garden in range(N):
64+
color_used = []
65+
if garden in change:
66+
subgarden = change[garden]
67+
for subgarden in change[garden]:
68+
if result[subgarden]:
69+
color_used.append(result[subgarden])
70+
color_rem = list(set([1, 2, 3, 4]) - set(color_used))
71+
for color in color_rem:
72+
result[garden] = color
73+
break
74+
return result
75+

Diff for: 1000-1100q/1043.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'''
2+
Given an integer array A, you partition the array into (contiguous) subarrays of length at most K. After partitioning, each subarray has their values changed to become the maximum value of that subarray.
3+
4+
Return the largest sum of the given array after partitioning.
5+
6+
7+
8+
Example 1:
9+
10+
Input: A = [1,15,7,9,2,5,10], K = 3
11+
Output: 84
12+
Explanation: A becomes [15,15,15,9,10,10,10]
13+
14+
15+
Note:
16+
17+
1 <= K <= A.length <= 500
18+
0 <= A[i] <= 10^6
19+
'''
20+
21+
class Solution(object):
22+
def maxSumAfterPartitioning(self, A, K):
23+
"""
24+
:type A: List[int]
25+
:type K: int
26+
:rtype: int
27+
"""
28+
if not A:
29+
return 0
30+
31+
N = len(A)
32+
dp = [0]*(N+1)
33+
for index_i in range(N):
34+
maxi = 0
35+
for index_j in range(index_i, index_i-K, -1):
36+
if index_j >= 0 and index_j < len(A):
37+
maxi = max(maxi, A[index_j])
38+
39+
dp[index_i+1] = max(dp[index_i+1], maxi*(index_i-index_j+1)+dp[index_j])
40+
# print index_i, maxi
41+
# print dp
42+
43+
return dp[-1]

Diff for: README.md

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
1414
| # | Title | Solution | Difficulty |
1515
|---| ----- | -------- | ---------- |
1616
|1044|[Longest Duplicate Substring](https://leetcode.com/problems/longest-duplicate-substring)|[Python](./1000-1100q/1044.py)|Hard|
17+
|1043|[Partition Array for Maximum Sum](https://leetcode.com/problems/partition-array-for-maximum-sum/)|[Python](./1000-1100q/1043.py)|Medium|
18+
|1042|[Flower Planting With No Adjacent](https://leetcode.com/problems/flower-planting-with-no-adjacent)|[Python](./1000-1100q/1042.py)|Easy|
19+
|1041|[Robot Bounded In Circle](https://leetcode.com/problems/robot-bounded-in-circle)|[Python](./1000-1100q/1041.py)|Easy|
1720
|1039|[Minimum Score Triangulation of Polygon](https://leetcode.com/problems/minimum-score-triangulation-of-polygon)|[Python](./1000-1100q/1039.py)|Medium|
1821
|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|
1922
|1037|[Valid Boomerang](https://leetcode.com/problems/valid-boomerang/)|[Python](./1000-1100q/1037.py)|Easy|

0 commit comments

Comments
 (0)