Skip to content

Commit bedaaa1

Browse files
committed
solutions
1 parent 5f24233 commit bedaaa1

File tree

7 files changed

+153
-101
lines changed

7 files changed

+153
-101
lines changed

solutions/3577/01.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def countPermutations(self, complexity: List[int]) -> int:
6+
MOD = 10**9 + 7
7+
8+
n = len(complexity)
9+
10+
# Check if complexity[0] is the unique minimum
11+
min_complexity = complexity[0]
12+
min_count = sum(1 for c in complexity if c == min_complexity)
13+
14+
if min_count > 1:
15+
# If there are other elements with the same complexity as index 0, no valid permutations
16+
return 0
17+
18+
# Index 0 must be first. The remaining n-1 indices can be arranged in any order
19+
# So the answer is (n-1)!
20+
res = 1
21+
for i in range(1, n):
22+
res = (res * i) % MOD
23+
24+
return res
25+

solutions/3724/01.py

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,26 @@
1+
from typing import List
2+
3+
14
class Solution:
25
def minOperations(self, nums1: List[int], nums2: List[int]) -> int:
36
n = len(nums1)
4-
res = float('inf')
5-
6-
# nums2 has one extra element, which must come from appending
7+
res = float("inf")
8+
9+
# nums2 has n+1 elements, so one element must come from an append operation
710
# Try each index j in nums1 as the one to append
811
for j in range(n):
912
# Cost to make nums1[j] match both nums2[j] and nums2[-1]
10-
# We need to adjust nums1[j] to one value, then append it
11-
# The cost is: |nums1[j] - nums2[j]| + |nums1[j] - nums2[-1]|
12-
# But we can optimize: make nums1[j] equal to one, then adjust the appended copy
13-
# Actually, we can make one copy nums2[j] and one copy nums2[-1]
14-
# Cost = |nums1[j] - nums2[j]| + |nums1[j] - nums2[-1]|
15-
# But wait, we append first, then adjust. So:
16-
# 1. Append nums1[j] (cost 0 for append operation itself)
17-
# 2. Adjust original to nums2[j]: |nums1[j] - nums2[j]|
18-
# 3. Adjust appended to nums2[-1]: |nums1[j] - nums2[-1]|
19-
20-
cost = abs(nums1[j] - nums2[j]) + abs(nums1[j] - nums2[-1])
21-
13+
# We need: one copy becomes nums2[j], appended copy becomes nums2[-1]
14+
# The cost is |nums1[j] - nums2[j]| + |nums1[j] - nums2[-1]|
15+
append_cost = abs(nums1[j] - nums2[j]) + abs(nums1[j] - nums2[-1])
16+
2217
# Cost to transform all other positions
18+
other_cost = 0
2319
for i in range(n):
2420
if i != j:
25-
cost += abs(nums1[i] - nums2[i])
26-
27-
res = min(res, cost)
28-
29-
return res
21+
other_cost += abs(nums1[i] - nums2[i])
22+
23+
total_cost = append_cost + other_cost
24+
res = min(res, total_cost)
3025

26+
return res

solutions/3728/01.py

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,24 @@
1+
from typing import List
2+
3+
14
class Solution:
25
def countStableSubarrays(self, capacity: List[int]) -> int:
36
n = len(capacity)
4-
5-
# Compute prefix sums
7+
res = 0
8+
9+
# Build prefix sum
610
prefix = [0] * (n + 1)
711
for i in range(n):
812
prefix[i + 1] = prefix[i] + capacity[i]
9-
10-
res = 0
11-
12-
# For each right endpoint r, find valid left endpoints l
13-
# Condition: capacity[l] == capacity[r] and
14-
# capacity[l] == sum(capacity[l+1:r])
15-
# Which means: capacity[l] == prefix[r] - prefix[l+1]
16-
# Rearranging: prefix[l+1] == prefix[r] - capacity[r]
17-
18-
# Use a map: (value, prefix_sum) -> count
19-
from collections import defaultdict
20-
freq_map = defaultdict(int)
21-
22-
# Process from left to right
23-
for r in range(n):
24-
# For each r, we want to find l such that:
25-
# capacity[l] == capacity[r] and prefix[l+1] == prefix[r] - capacity[r]
26-
target_prefix = prefix[r] - capacity[r]
27-
key = (capacity[r], target_prefix)
28-
res += freq_map[key]
29-
30-
# Update map: add (capacity[r], prefix[r]) for future matches
31-
# Note: we use prefix[r] (not prefix[r+1]) because when we check at position r,
32-
# we're looking for l where prefix[l+1] matches
33-
update_key = (capacity[r], prefix[r])
34-
freq_map[update_key] += 1
35-
36-
return res
3713

14+
# For each subarray [l, r] where length >= 3
15+
# Check if capacity[l] == capacity[r] == sum of elements between them
16+
for l in range(n):
17+
for r in range(l + 2, n): # length >= 3
18+
if capacity[l] == capacity[r]:
19+
# Sum of elements strictly between l and r
20+
interior_sum = prefix[r] - prefix[l + 1]
21+
if capacity[l] == interior_sum:
22+
res += 1
23+
24+
return res

solutions/3733/01.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from typing import List
2+
import math
3+
4+
5+
class Solution:
6+
def minimumTime(self, d: List[int], r: List[int]) -> int:
7+
d1, d2 = d[0], d[1]
8+
r1, r2 = r[0], r[1]
9+
10+
# Binary search on total time T
11+
def can_complete(T):
12+
# Calculate available hours for each drone
13+
# Drone 1: T - floor(T/r1) hours available (subtract recharge hours)
14+
avail1 = T - (T // r1)
15+
avail2 = T - (T // r2)
16+
17+
# Hours when both are recharging (unavailable)
18+
lcm_r = math.lcm(r1, r2)
19+
both_recharging = T // lcm_r
20+
21+
# Shared available hours (when at least one is available)
22+
shared = T - both_recharging
23+
24+
# Check if we can complete all deliveries
25+
# Each drone needs its deliveries, and they can't overlap in time
26+
# We need: avail1 >= d1 and avail2 >= d2, and total available >= d1 + d2
27+
return avail1 >= d1 and avail2 >= d2 and shared >= d1 + d2
28+
29+
# Binary search
30+
left, right = 1, 10**18
31+
while left < right:
32+
mid = (left + right) // 2
33+
if can_complete(mid):
34+
right = mid
35+
else:
36+
left = mid + 1
37+
38+
return left
39+

solutions/3747/01.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
class Solution:
2-
def beautifulSubstrings(self, s: str, k: int) -> int:
3-
vowels = {'a', 'e', 'i', 'o', 'u'}
4-
n = len(s)
5-
res = 0
6-
7-
# Try all possible substrings
8-
for i in range(n):
9-
vowel_count = 0
10-
consonant_count = 0
11-
12-
for j in range(i, n):
13-
if s[j] in vowels:
14-
vowel_count += 1
15-
else:
16-
consonant_count += 1
17-
18-
# Check if beautiful
19-
if vowel_count == consonant_count and (vowel_count * consonant_count) % k == 0:
20-
res += 1
21-
22-
return res
2+
def countDistinct(self, n: int) -> int:
3+
# For each integer x from 1 to n, remove zeros and count distinct results
4+
distinct_set = set()
5+
6+
for x in range(1, n + 1):
7+
# Remove zeros from decimal representation
8+
s = str(x)
9+
s_no_zeros = s.replace("0", "")
10+
if s_no_zeros: # If not empty after removing zeros
11+
distinct_set.add(int(s_no_zeros))
12+
else:
13+
# If all zeros, the result is empty, but we can represent it as 0 or empty
14+
# Actually, removing zeros from a number with only zeros gives empty string
15+
# But we need to handle this - let's check the examples
16+
# For n=10: 10 -> "10" -> "1" -> 1
17+
# So we should add something for numbers that become empty
18+
# Actually, looking at example: n=10 gives 9 distinct (1-9), and 10 becomes 1
19+
# So empty strings might not be counted, or they become 0
20+
pass
21+
22+
return len(distinct_set)

solutions/3753/01.py

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,22 @@
11
class Solution:
2-
def numberOfPaths(self, grid: List[List[int]], k: int) -> int:
3-
MOD = 10**9 + 7
4-
m, n = len(grid), len(grid[0])
5-
6-
# dp[i][j][r] = number of paths to (i,j) with sum % k == r
7-
# Use 2D DP with k states for remainder
8-
dp = [[[0] * k for _ in range(n)] for _ in range(m)]
9-
10-
# Initialize starting position
11-
initial_remainder = grid[0][0] % k
12-
dp[0][0][initial_remainder] = 1
13-
14-
for i in range(m):
15-
for j in range(n):
16-
current_val = grid[i][j]
17-
18-
# Update from top
19-
if i > 0:
20-
for r in range(k):
21-
new_remainder = (r + current_val) % k
22-
dp[i][j][new_remainder] = (dp[i][j][new_remainder] + dp[i-1][j][r]) % MOD
23-
24-
# Update from left
25-
if j > 0:
26-
for r in range(k):
27-
new_remainder = (r + current_val) % k
28-
dp[i][j][new_remainder] = (dp[i][j][new_remainder] + dp[i][j-1][r]) % MOD
29-
30-
# Return paths to (m-1, n-1) with remainder 0
31-
return dp[m-1][n-1][0]
2+
def totalWaviness(self, num1: int, num2: int) -> int:
3+
def get_waviness(n):
4+
s = str(n)
5+
if len(s) < 3:
6+
return 0
327

8+
waviness = 0
9+
for i in range(1, len(s) - 1):
10+
# Check if digit at i is a peak or valley
11+
if s[i] > s[i - 1] and s[i] > s[i + 1]:
12+
waviness += 1 # Peak
13+
elif s[i] < s[i - 1] and s[i] < s[i + 1]:
14+
waviness += 1 # Valley
15+
16+
return waviness
17+
18+
res = 0
19+
for num in range(num1, num2 + 1):
20+
res += get_waviness(num)
21+
22+
return res

solutions/3769/01.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def sortByReflection(self, nums: List[int]) -> List[int]:
6+
def binary_reflection(n):
7+
# Convert to binary, remove '0b' prefix, reverse, convert back to int
8+
binary = bin(n)[2:]
9+
reversed_binary = binary[::-1]
10+
return int(reversed_binary, 2)
11+
12+
# Sort by binary reflection, then by original value if reflection is equal
13+
res = sorted(nums, key=lambda x: (binary_reflection(x), x))
14+
return res
15+

0 commit comments

Comments
 (0)