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

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 72 KiB

View File

@@ -0,0 +1,14 @@
def greedy_change(n, coins):
result = []
for value, count in sorted(coins, key=lambda x: -x[0]):
while count > 0 and n >= value:
n -= value
count -= 1
result.append(value)
return result if n == 0 else None
if __name__ == "__main__":
coins = [(10, 3), (5, 2), (2, 5), (1, 10)]
N = 28
print(greedy_change(N, coins))

View File

@@ -0,0 +1,20 @@
def greedy_thief(weights, values, M, K):
items = list(zip(weights, values))
items.sort(key=lambda x: x[1] / x[0], reverse=True)
cap = M * K
total_value = 0
chosen = []
for w, v in items:
if w <= cap:
chosen.append((w, v))
cap -= w
total_value += v
return total_value, chosen
if __name__ == "__main__":
weights = [2, 3, 4, 5, 9, 7, 3]
values = [3, 4, 5, 8, 10, 7, 6]
M = 2
K = 10
print(greedy_thief(weights, values, M, K))

View File

@@ -0,0 +1,26 @@
import heapq
def dijkstra(graph, start):
dist = {v: float("inf") for v in graph}
dist[start] = 0
pq = [(0, start)]
while pq:
d, v = heapq.heappop(pq)
if d > dist[v]:
continue
for u, w in graph[v]:
if dist[v] + w < dist[u]:
dist[u] = dist[v] + w
heapq.heappush(pq, (dist[u], u))
return dist
if __name__ == "__main__":
graph = {
"Невский": [("Лиговский", 5), ("Гороховая", 3)],
"Лиговский": [("Московский", 7)],
"Гороховая": [("Московский", 4)],
"Московский": [],
}
print(dijkstra(graph, "Невский"))

Binary file not shown.

Binary file not shown.

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

Binary file not shown.

Binary file not shown.