This commit is contained in:
nik
2025-10-01 22:55:09 +03:00
parent 4d0ece634b
commit 74e98c37c4
591 changed files with 20286 additions and 0 deletions

View File

@@ -0,0 +1 @@
https://leetcode.com/problems/wildcard-matching/?envType=problem-list-v2&envId=greedy

View File

@@ -0,0 +1,26 @@
from typing import List
class Solution:
def isMatch(self, s: str, p: str) -> bool:
i = j = 0
star = -1
match = 0
n, m = len(s), len(p)
while i < n:
if j < m and (p[j] == s[i] or p[j] == "?"):
i += 1
j += 1
elif j < m and p[j] == "*":
star = j
match = i
j += 1
elif star != -1:
j = star + 1
match += 1
i = match
else:
return False
while j < m and p[j] == "*":
j += 1
return j == m

View File

@@ -0,0 +1 @@
https://leetcode.com/problems/candy/description/?envType=problem-list-v2&envId=greedy

View File

@@ -0,0 +1,14 @@
from typing import List
class Solution:
def candy(self, ratings: List[int]) -> int:
n = len(ratings)
candies = [1] * n
for i in range(1, n):
if ratings[i] > ratings[i - 1]:
candies[i] = candies[i - 1] + 1
for i in range(n - 2, -1, -1):
if ratings[i] > ratings[i + 1]:
candies[i] = max(candies[i], candies[i + 1] + 1)
return sum(candies)

View File

@@ -0,0 +1 @@
https://leetcode.com/problems/create-maximum-number/?envType=problem-list-v2&envId=greedy

View File

@@ -0,0 +1,46 @@
from typing import List
class Solution:
def maxNumber(self, nums1: List[int], nums2: List[int], k: int) -> List[int]:
def pick(arr: List[int], t: int) -> List[int]:
drop = len(arr) - t
st = []
for x in arr:
while drop and st and st[-1] < x:
st.pop()
drop -= 1
st.append(x)
return st[:t]
def greater(a: List[int], i: int, b: List[int], j: int) -> bool:
while i < len(a) and j < len(b) and a[i] == b[j]:
i += 1
j += 1
if j == len(b):
return True
if i < len(a) and a[i] > b[j]:
return True
return False
def merge(a: List[int], b: List[int]) -> List[int]:
i = j = 0
res = []
while i < len(a) or j < len(b):
if greater(a, i, b, j):
res.append(a[i])
i += 1
else:
res.append(b[j])
j += 1
return res
m, n = len(nums1), len(nums2)
ans = []
for i in range(max(0, k - n), min(k, m) + 1):
a = pick(nums1, i)
b = pick(nums2, k - i)
cand = merge(a, b)
if not ans or cand > ans:
ans = cand
return ans