Skip to content

Commit e73b0cd

Browse files
committed
Merge branch 'master' of https://github.com/CharryWu/leetcode
2 parents 773f73e + 921cf7c commit e73b0cd

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

2024/meta/selfmock.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
2+
# You are given two lists of closed intervals, firstList and secondList, where firstList[i] = [starti, endi] and secondList[j] = [startj, endj]. Each list of intervals is pairwise disjoint and in sorted order.
3+
4+
# Return the intersection of these two interval lists.
5+
6+
# A closed interval [a, b] (with a <= b) denotes the set of real numbers x with a <= x <= b.
7+
8+
# The intersection of two closed intervals is a set of real numbers that are either empty or represented as a closed interval. For example, the intersection of [1, 3] and [2, 4] is [2, 3].
9+
10+
# Input: firstList = [[0,2],[5,10],[13,23],[24,25]], secondList = [[1,5],[8,12],[15,24],[25,26]]
11+
# Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
12+
def solution(firstList, secondList):
13+
result = []
14+
15+
i, j = 0, 0
16+
17+
while i < len(firstList) and j < len(secondList):
18+
newstart = max(firstList[i][0], secondList[j][0])
19+
newend = min(firstList[i][1], secondList[j][1])
20+
21+
if newstart <= newend:
22+
result.append((newstart, newend))
23+
24+
if firstList[i][1] < secondList[j][1]:
25+
i += 1
26+
else:
27+
j += 1
28+
29+
return result
30+
31+
firstList = [[0,2],[5,10],[13,23],[24,25]]
32+
secondList = [[1,5],[8,12],[15,24],[25,26]]
33+
solution(firstList, secondList)
34+
35+
i = 0, j = 0, (newstart, newend) = (1, 2), result = [(1,2)]. i += 1 = 1
36+
i = 1, j = 0, (newstart, newend) = (5, 5), result = [(1,2), (5, 5)]. j += 1 = 1
37+
i = 1, j = 1, (newstart, newend) = (8, 10), result = [(1,2), (5, 5), (8,10)]. i += 1 = 2
38+
i = 2, j = 1, (newstart, newend) = (13, 12), result = [(1,2), (5, 5), (8,10)]. j += 1 = 2
39+
//
40+
i = 2, j = 2, (newstart, newend) = (13, 12), result = [(1,2), (5, 5), (8,10)]. j += 1 = 2
41+
42+
43+
# Given an n x n binary matrix grid, return the length of the shortest clear path in the matrix. If there is no clear path, return -1.
44+
45+
# A clear path in a binary matrix is a path from the top-left cell (i.e., (0, 0)) to the bottom-right cell (i.e., (n - 1, n - 1)) such that:
46+
47+
# All the visited cells of the path are 0.
48+
# All the adjacent cells of the path are 8-directionally connected (i.e., they are different and they share an edge or a corner).
49+
# The length of a clear path is the number of visited cells of this path.
50+
51+
# Input: grid = [[0,1],
52+
# [1,0]
53+
# ]
54+
# Output: 2
55+
56+
57+
DIR = {(0,1),(1,0),(-1,0),(0,-1),(1,1),(1,-1),(-1,-1),(-1,1)}
58+
from collections import deque
59+
def solution(grid):
60+
n = len(grid)
61+
if n == 0:
62+
return -1
63+
64+
if grid[0][0] == 1 or grid[n-1][n-1] == 1:
65+
return -1
66+
67+
queue = deque((0, 0, 1)) # (x, y, dist)
68+
visited = set([(0,0)]) # (x, y) visited
69+
70+
while queue:
71+
x, y, dist = queue.popleft()
72+
if x == n-1 and y == n-1:
73+
return dist
74+
75+
for dx, dy in DIR:
76+
nx, ny = x+dx, y+dy
77+
if 0 <= nx < n and 0 <= ny < n and grid[nx][ny] == 0 and (nx,ny) not in visited:
78+
queue.append((nx,ny,dist+1))
79+
visited.add(nx, ny)
80+
6
81+
return -1
82+
83+
solution( [[0,1],
84+
[1,0]
85+
]) # 2
86+
87+
n = 2
88+
queue = [(0,0,1)]
89+
90+
x = 0, y = 0, dist = 1
91+
- queue = ((1,1,2)), visited = {(1,1)}
92+
93+
x = 1, y = 1, dist = 2
94+
- return 2
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def minSwaps(self, data: List[int]) -> int:
3+
need_count = sum(data)
4+
i = 0
5+
max_window_ones = 0
6+
window_count = 0
7+
8+
for j, char in enumerate(data):
9+
if char == 1:
10+
window_count += 1
11+
if j-i+1 > need_count:
12+
if data[i] == 1:
13+
window_count -= 1
14+
i += 1
15+
16+
max_window_ones = max(max_window_ones, window_count)
17+
18+
return need_count - max_window_ones

0 commit comments

Comments
 (0)