Skip to content

Commit aaf73b9

Browse files
committed
relocate
1 parent 75a3c4a commit aaf73b9

File tree

129 files changed

+613
-153
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

129 files changed

+613
-153
lines changed
File renamed without changes.
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# https://leetcode.com/problems/apply-bitwise-operations-to-make-strings-equal/description/
2+
3+
4+
class Solution:
5+
def makeStringsEqual(self, s: str, target: str) -> bool:
6+
return ("1" in s) == ("1" in target)

leetcode/array/best_time_to_buy_and_sell_stock.py

-12
This file was deleted.
File renamed without changes.
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
2+
3+
class Solution:
4+
def maxProfit(self, prices: list[int]) -> int:
5+
max_profit, min_p = 0, float("inf")
6+
7+
for p in prices:
8+
max_profit = max(p - min_p, max_profit)
9+
min_p = min(p, min_p)
10+
11+
return max_profit

leetcode/bfs&dfs/network_delay_time.py

-22
This file was deleted.

leetcode/bfs&dfs/partition_to_k_equal_sum_subsets.py

-48
This file was deleted.
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# https://leetcode.com/problems/build-a-matrix-with-conditions/description/
2+
3+
from collections import defaultdict, deque
4+
5+
6+
class Solution:
7+
def buildMatrix(
8+
self, k: int, rowConditions: list[list[int]], colConditions: list[list[int]]
9+
) -> list[list[int]]:
10+
def topological_sorted(conditions: list[tuple[int, int]]) -> list[int]:
11+
graph = defaultdict(list)
12+
counter = defaultdict(int)
13+
14+
for high, low in conditions:
15+
graph[high].append(low)
16+
counter[low] += 1
17+
18+
q = deque([node for node in range(1, k + 1) if counter[node] == 0])
19+
sorted_nodes = []
20+
21+
while q:
22+
node = q.popleft()
23+
sorted_nodes.append(node)
24+
25+
for children in graph[node]:
26+
counter[children] -= 1
27+
28+
if counter[children] == 0:
29+
q.append(children)
30+
31+
if len(sorted_nodes) < k:
32+
return []
33+
34+
return sorted_nodes
35+
36+
if not (row_sorted := topological_sorted(rowConditions)):
37+
return []
38+
39+
if not (col_sorted := topological_sorted(colConditions)):
40+
return []
41+
42+
mat = [[0 for _ in range(k)] for _ in range(k)]
43+
col_sorted_indices = {v: i for i, v in enumerate(col_sorted)}
44+
45+
for r, v in enumerate(row_sorted):
46+
mat[r][col_sorted_indices[v]] = v
47+
48+
return mat
File renamed without changes.
File renamed without changes.
File renamed without changes.

leetcode/climbing_stairs.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# https://leetcode.com/problems/climbing-stairs/description/
2+
3+
class Solution:
4+
def climbStairs(self, n: int) -> int:
5+
if n < 3:
6+
return n
7+
8+
a, b = 1, 2
9+
10+
for _ in range(n - 2):
11+
b, a = a + b, b
12+
13+
return b

leetcode/corporate_flight_bookings.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# https://leetcode.com/problems/corporate-flight-bookings/description/
2+
3+
import heapq
4+
from itertools import accumulate
5+
6+
7+
class Solution:
8+
def corpFlightBookings(self, bookings: list[list[int]], n: int) -> list[int]:
9+
bookings = sorted(bookings, reverse=True)
10+
11+
lasts = []
12+
total, totals = 0, []
13+
14+
for m in range(1, n + 1):
15+
while lasts and lasts[0][0] < m:
16+
last, seats = heapq.heappop(lasts)
17+
total -= seats
18+
19+
while bookings and bookings[-1][0] <= m:
20+
first, last, seats = bookings.pop()
21+
total += seats
22+
23+
heapq.heappush(lasts, (last, seats))
24+
25+
totals.append(total)
26+
27+
return totals
28+
29+
def corpFlightBookings(self, bookings: list[list[int]], n: int) -> list[int]:
30+
diffs = [0 for _ in range(n + 1)]
31+
for first, last, seats in bookings:
32+
diffs[first - 1] += seats
33+
diffs[last] -= seats
34+
35+
return list(accumulate(diffs))[:-1]

leetcode/counting_bits.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# https://leetcode.com/problems/counting-bits
2+
3+
class Solution:
4+
def countBits(self, n: int) -> list[int]:
5+
dp = [0] * (n + 1)
6+
7+
for m in range(1, n + 1):
8+
dp[m] = dp[m >> 1] + (m & 1)
9+
10+
return dp

leetcode/dp/minimum_number_of_refueling_stops.cc

-27
This file was deleted.

leetcode/dp/minimum_number_of_refueling_stops.py

-16
This file was deleted.

leetcode/fibonacci_number.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# https://leetcode.com/problems/fibonacci-number/description/
2+
3+
class Solution:
4+
def fib(self, n: int) -> int:
5+
if n == 0:
6+
return 0
7+
if n < 3:
8+
return 1
9+
10+
a = b = 1
11+
12+
for _ in range(n - 2):
13+
a, b = a + b, a
14+
15+
return a

leetcode/four_divisors.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# https://leetcode.com/problems/four-divisors/submissions/943173970/
2+
3+
from collections import defaultdict
4+
5+
6+
class Solution:
7+
def sumFourDivisors(self, nums: list[int]) -> int:
8+
divisors = defaultdict(set)
9+
divisors_sums = defaultdict(int)
10+
divisors_sum = 0
11+
12+
for n in nums:
13+
if n in divisors and len(divisors[n]) == 4:
14+
divisors_sum += divisors_sums[n]
15+
continue
16+
17+
for m in range(1, int(n ** (1 / 2) + 1)):
18+
div, mod = divmod(n, m)
19+
if mod == 0:
20+
divisors[n] |= {div, m}
21+
22+
divisors_sums[n] = sum(divisors[n])
23+
24+
if len(divisors[n]) == 4:
25+
divisors_sum += divisors_sums[n]
26+
27+
return divisors_sum

leetcode/frog_jump_ii.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# https://leetcode.com/problems/frog-jump-ii/description/
2+
3+
4+
class Solution:
5+
def maxJump(self, stones: list[int]) -> int:
6+
if len(stones) == 2:
7+
return stones[1] - stones[0]
8+
return max(stones[i + 2] - stones[i] for i in range(len(stones) - 2))
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# https://leetcode.com/problems/get-maximum-in-generated-array/description/
2+
3+
class Solution:
4+
def getMaximumGenerated(self, n: int) -> int:
5+
if n < 2:
6+
return n
7+
8+
dp = [0] * (n + 1)
9+
dp[1] = 1
10+
11+
for m in range(2, n + 1):
12+
dp[m] = dp[m // 2]
13+
14+
if m % 2:
15+
dp[m] += + dp[m // 2 + 1]
16+
17+
return max(dp)
File renamed without changes.
File renamed without changes.
File renamed without changes.

leetcode/is_subsequence.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# https://leetcode.com/problems/is-subsequence/description/
2+
3+
class Solution:
4+
def isSubsequence(self, s: str, t: str) -> bool:
5+
if not s:
6+
return True
7+
8+
i = 0
9+
10+
for c in t:
11+
if c != s[i]:
12+
continue
13+
14+
i += 1
15+
16+
if i == len(s):
17+
return True
18+
19+
return False
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# https://leetcode.com/problems/knight-probability-in-chessboard/description/
2+
3+
4+
from functools import cache
5+
6+
7+
class Solution:
8+
def knightProbability(self, n: int, k: int, row: int, column: int) -> float:
9+
def is_valid(r: int, c: int) -> bool:
10+
return 0 <= r < n and 0 <= c < n
11+
12+
@cache
13+
def dfs(r: int, c: int, depth: int = 0) -> int:
14+
if not is_valid(r, c):
15+
return 0
16+
if depth == k:
17+
return 1
18+
19+
return sum(
20+
dfs(r + dr, c + dc, depth + 1)
21+
for dr, dc in (
22+
(-1, -2), (-2, -1), (-2, 1), (-1, 2),
23+
(1, -2), (2, -1), (2, 1), (1, 2)
24+
)
25+
)
26+
27+
return dfs(row, column) / 8**k

0 commit comments

Comments
 (0)