upd
This commit is contained in:
BIN
algorithms/labs/lab7/assets/1.png
Normal file
BIN
algorithms/labs/lab7/assets/1.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 35 KiB |
BIN
algorithms/labs/lab7/assets/2.png
Normal file
BIN
algorithms/labs/lab7/assets/2.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 46 KiB |
BIN
algorithms/labs/lab7/assets/3.png
Normal file
BIN
algorithms/labs/lab7/assets/3.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 72 KiB |
14
algorithms/labs/lab7/code/1.py
Normal file
14
algorithms/labs/lab7/code/1.py
Normal 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))
|
||||
20
algorithms/labs/lab7/code/2.py
Normal file
20
algorithms/labs/lab7/code/2.py
Normal 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))
|
||||
26
algorithms/labs/lab7/code/3.py
Normal file
26
algorithms/labs/lab7/code/3.py
Normal 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, "Невский"))
|
||||
BIN
algorithms/labs/lab7/in/task.pdf
Normal file
BIN
algorithms/labs/lab7/in/task.pdf
Normal file
Binary file not shown.
BIN
algorithms/labs/lab7/in/theory.pdf
Normal file
BIN
algorithms/labs/lab7/in/theory.pdf
Normal file
Binary file not shown.
1
algorithms/labs/lab7/leetcode/1/link.txt
Normal file
1
algorithms/labs/lab7/leetcode/1/link.txt
Normal file
@@ -0,0 +1 @@
|
||||
https://leetcode.com/problems/wildcard-matching/?envType=problem-list-v2&envId=greedy
|
||||
26
algorithms/labs/lab7/leetcode/1/solution.py
Normal file
26
algorithms/labs/lab7/leetcode/1/solution.py
Normal 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
|
||||
1
algorithms/labs/lab7/leetcode/2/link.txt
Normal file
1
algorithms/labs/lab7/leetcode/2/link.txt
Normal file
@@ -0,0 +1 @@
|
||||
https://leetcode.com/problems/candy/description/?envType=problem-list-v2&envId=greedy
|
||||
14
algorithms/labs/lab7/leetcode/2/solution.py
Normal file
14
algorithms/labs/lab7/leetcode/2/solution.py
Normal 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)
|
||||
1
algorithms/labs/lab7/leetcode/3/link.txt
Normal file
1
algorithms/labs/lab7/leetcode/3/link.txt
Normal file
@@ -0,0 +1 @@
|
||||
https://leetcode.com/problems/create-maximum-number/?envType=problem-list-v2&envId=greedy
|
||||
46
algorithms/labs/lab7/leetcode/3/solution.py
Normal file
46
algorithms/labs/lab7/leetcode/3/solution.py
Normal 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
|
||||
BIN
algorithms/labs/lab7/out/report.docx
Normal file
BIN
algorithms/labs/lab7/out/report.docx
Normal file
Binary file not shown.
BIN
algorithms/labs/lab7/out/report.pdf
Normal file
BIN
algorithms/labs/lab7/out/report.pdf
Normal file
Binary file not shown.
Reference in New Issue
Block a user