-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution7-1.py
More file actions
43 lines (31 loc) · 1.02 KB
/
solution7-1.py
File metadata and controls
43 lines (31 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import sys
def main():
file = open('input7.txt', 'r')
answer = -1
input = file.readline()
input = input.split(',')
input = [int(x) for x in input]
maxDist = input[0]
dp = []
dp.append(0)
inputfreq = {}
for x in input:
maxDist = max(x, maxDist)
dp[0]+= x
inputfreq[x] = inputfreq.get(x,0) + 1
crabsAboveCurrentPosition = 0
crabsBelowCurrentPosition = 0
for x in inputfreq.keys():
if x >= 0:
crabsAboveCurrentPosition += inputfreq[x]
for position in range(1, maxDist+1):
# print("Cost for position " + str(position) + ": " + str(dp[position-1]))
previousCost = dp[position-1]
crabsBelowCurrentPosition += inputfreq.get(position-1, 0)
crabsAboveCurrentPosition -= inputfreq.get(position-1, 0)
cost = previousCost + crabsBelowCurrentPosition - crabsAboveCurrentPosition
dp.append(cost)
answer = cost if answer == -1 else min(answer, cost)
pass
print(answer)
main()