Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions 08_greedy_algorithms/python/01_set_covering.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,23 @@

final_stations = set()

while states_needed:
best_station = None
states_covered = set()
for station, states_for_station in stations.items():
covered = states_needed & states_for_station
if len(covered) > len(states_covered):
best_station = station
states_covered = covered
def my_set_covering(states_needed, stations):
final_stations = set()
#while states_needed is not None: 这个不对,Set()而不是None
while states_needed:
best_station = None
states_covered = set()
for station, states_for_station in stations.items():
covered = states_needed & states_for_station
if len(covered) > len(states_covered) and station not in final_stations:
best_station = station
states_covered = covered
if best_station is not None:
states_needed -= states_covered
final_stations.add(best_station)
stations.pop(best_station)
else:
return None
return final_stations

states_needed -= states_covered
final_stations.add(best_station)

print(final_stations)
print(my_set_covering(states_needed, stations))
19 changes: 13 additions & 6 deletions 09_dynamic_programming/python/01_longest_common_subsequence.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
if word_a[i] == word_b[j]:
# The letters match.
cell[i][j] = cell[i-1][j-1] + 1
else:
# The letters don't match.
cell[i][j] = max(cell[i-1][j], cell[i][j-1])
dp_table_blue = ["b", "l", "u", "e"]
dp_table_clues = ["c", "l", "u", "e", "s"]
dp_table = [[0 for i in range(len(dp_table_blue))] for i in range(len(dp_table_clues))] # (5,4)
print(dp_table)

for i in range(0, len(dp_table_blue)):
for j in range(0, len(dp_table_clues)):
if dp_table_clues[j] == dp_table_blue[i]:
dp_table[j][i] = dp_table[j-1][i-1] + 1
else:
dp_table[j][i] = max(dp_table[j-1][i], dp_table[j][i-1])

print(dp_table)
11 changes: 11 additions & 0 deletions 09_dynamic_programming/python/02_longest_common_substring.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
dp_table_blue = ["b", "l", "u", "e"]
dp_table_clues = ["c", "l", "u", "e", "s"]
dp_table = [[0 for i in range(len(dp_table_blue))] for i in range(len(dp_table_clues))] # (5,4)
print(dp_table)

for i in range(0, len(dp_table_blue)):
for j in range(0, len(dp_table_clues)):
if dp_table_clues[j] == dp_table_blue[i]:
dp_table[i][j] = dp_table[i-1][i-1] + 1

print(dp_table)