Skip to content

Commit cbcdc21

Browse files
committed
Solution for 1033, 1034, 1035
1 parent 82b2ab7 commit cbcdc21

File tree

4 files changed

+182
-0
lines changed

4 files changed

+182
-0
lines changed

1000-1100q/1033.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'''
2+
Three stones are on a number line at positions a, b, and c.
3+
4+
Each turn, let's say the stones are currently at positions x, y, z with x < y < z. You pick up the stone at either position x or position z, and move that stone to an integer position k, with x < k < z and k != y.
5+
6+
The game ends when you cannot make any more moves, ie. the stones are in consecutive positions.
7+
8+
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]
9+
10+
11+
12+
Example 1:
13+
14+
Input: a = 1, b = 2, c = 5
15+
Output: [1, 2]
16+
Explanation: Move stone from 5 to 4 then to 3, or we can move it directly to 3.
17+
Example 2:
18+
19+
Input: a = 4, b = 3, c = 2
20+
Output: [0, 0]
21+
Explanation: We cannot make any moves.
22+
23+
24+
Note:
25+
26+
1 <= a <= 100
27+
1 <= b <= 100
28+
1 <= c <= 100
29+
a != b, b != c, c != a
30+
'''
31+
32+
class Solution(object):
33+
def numMovesStones(self, a, b, c):
34+
"""
35+
:type a: int
36+
:type b: int
37+
:type c: int
38+
:rtype: List[int]
39+
"""
40+
lista = [a, b, c]
41+
lista.sort()
42+
a, b, c = lista[0], lista[1], lista[2]
43+
minsteps = 0
44+
if b == a+1 and c == a+2:
45+
return [0, 0]
46+
elif b == a+1 or c == b+1 or c == b+2 or b == a+2:
47+
minsteps = 1
48+
else:
49+
minsteps = 2
50+
return [minsteps, b-a-1+c-b-1]

1000-1100q/1034.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'''
2+
Given a 2-dimensional grid of integers, each value in the grid represents the color of the grid square at that location.
3+
4+
Two squares belong to the same connected component if and only if they have the same color and are next to each other in any of the 4 directions.
5+
6+
The border of a connected component is all the squares in the connected component that are either 4-directionally adjacent to a square not in the component, or on the boundary of the grid (the first or last row or column).
7+
8+
Given a square at location (r0, c0) in the grid and a color, color the border of the connected component of that square with the given color, and return the final grid.
9+
10+
Example 1:
11+
12+
Input: grid = [[1,1],[1,2]], r0 = 0, c0 = 0, color = 3
13+
Output: [[3, 3], [3, 2]]
14+
Example 2:
15+
16+
Input: grid = [[1,2,2],[2,3,2]], r0 = 0, c0 = 1, color = 3
17+
Output: [[1, 3, 3], [2, 3, 3]]
18+
Example 3:
19+
20+
Input: grid = [[1,1,1],[1,1,1],[1,1,1]], r0 = 1, c0 = 1, color = 2
21+
Output: [[2, 2, 2], [2, 1, 2], [2, 2, 2]]
22+
23+
24+
Note:
25+
26+
1 <= grid.length <= 50
27+
1 <= grid[0].length <= 50
28+
1 <= grid[i][j] <= 1000
29+
0 <= r0 < grid.length
30+
0 <= c0 < grid[0].length
31+
1 <= color <= 1000
32+
'''
33+
34+
class Solution(object):
35+
def colorBorder(self, grid, r0, c0, color):
36+
"""
37+
:type grid: List[List[int]]
38+
:type r0: int
39+
:type c0: int
40+
:type color: int
41+
:rtype: List[List[int]]
42+
"""
43+
if not grid:
44+
return grid
45+
visited, border = [], []
46+
m, n = len(grid), len(grid[0])
47+
48+
def dfs(r, c):
49+
if r < 0 or c < 0 or r >= m or c >= n or grid[r][c] != grid[r0][c0] or (r,c) in visited:
50+
return
51+
visited.append((r,c))
52+
53+
# check if the current row, col index is edge of the matrix
54+
# if not then check adjacent cells doesnt have same value as grid[r0][c0] then add in border
55+
if (r == 0 or c == 0 or r == m-1 or c == n-1 or
56+
(r+1 < m and grid[r+1][c] != grid[r0][c0]) or
57+
(r-1 >= 0 and grid[r-1][c] != grid[r0][c0]) or
58+
(c+1 < n and grid[r][c+1] != grid[r0][c0]) or
59+
(c-1 >= 0 and grid[r][c-1] != grid[r0][c0])):
60+
border.append((r,c))
61+
dfs(r-1, c)
62+
dfs(r+1, c)
63+
dfs(r, c-1)
64+
dfs(r, c+1)
65+
66+
dfs(r0, c0)
67+
for (x, y) in border:
68+
grid[x][y] = color
69+
return grid

1000-1100q/1035.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
'''
2+
We write the integers of A and B (in the order they are given) on two separate horizontal lines.
3+
4+
Now, we may draw a straight line connecting two numbers A[i] and B[j] as long as A[i] == B[j], and the line we draw does not intersect any other connecting (non-horizontal) line.
5+
6+
Return the maximum number of connecting lines we can draw in this way.
7+
8+
9+
10+
Example 1:
11+
12+
13+
Input: A = [1,4,2], B = [1,2,4]
14+
Output: 2
15+
Explanation: We can draw 2 uncrossed lines as in the diagram.
16+
We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2.
17+
Example 2:
18+
19+
Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2]
20+
Output: 3
21+
Example 3:
22+
23+
Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1]
24+
Output: 2
25+
26+
27+
Note:
28+
29+
1 <= A.length <= 500
30+
1 <= B.length <= 500
31+
1 <= A[i], B[i] <= 2000
32+
'''
33+
34+
class Solution(object):
35+
def maxUncrossedLines(self, A, B):
36+
"""
37+
:type A: List[int]
38+
:type B: List[int]
39+
:rtype: int
40+
"""
41+
dp = [[0]*len(A) for _ in range(len(B))]
42+
43+
dp[0][0] = 1 if A[0] == B[0] else 0
44+
for index_i in range(1, len(dp)):
45+
dp[index_i][0] = dp[index_i-1][0]
46+
if A[0] == B[index_i]:
47+
dp[index_i][0] = 1
48+
49+
for index_j in range(1, len(dp[0])):
50+
dp[0][index_j] = dp[0][index_j-1]
51+
if B[0] == A[index_j]:
52+
dp[0][index_j] = 1
53+
54+
for index_i in range(1, len(dp)):
55+
for index_j in range(1, len(dp[0])):
56+
if A[index_j] == B[index_i]:
57+
dp[index_i][index_j] = max(dp[index_i-1][index_j-1] + 1, max(dp[index_i-1][index_j], dp[index_i][index_j-1]))
58+
else:
59+
dp[index_i][index_j] = max(dp[index_i-1][index_j-1], max(dp[index_i-1][index_j], dp[index_i][index_j-1]))
60+
return dp[len(B)-1][len(A)-1]

README.md

Lines changed: 3 additions & 0 deletions
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+
|1035|[Uncrossed Lines](https://leetcode.com/problems/uncrossed-lines/)|[Python](./1000-1100q/1035.py)|Medium|
11+
|1034|[Coloring A Border](https://leetcode.com/problems/coloring-a-border/)|[Python](./1000-1100q/1034.py)|Medium|
12+
|1033|[Moving Stones Until Consecutive](https://leetcode.com/problems/moving-stones-until-consecutive)|[Python](./1000-1100q/1033.py)|Easy
1013
|1032|[Stream of Characters](https://leetcode.com/problems/stream-of-characters)|[Python](./1000-1100q/1032.py)|Hard|
1114
|1031|[Maximum Sum of Two Non-Overlapping Subarrays](https://leetcode.com/problems/maximum-sum-of-two-non-overlapping-subarrays)|[Python](./1000-1100q/1031.py)|Medium|
1215
|1030|[Matrix Cells in Distance Order](https://leetcode.com/problems/matrix-cells-in-distance-order)|[Python](./1000-1100/1030.py)|Easy|

0 commit comments

Comments
 (0)