Skip to content

Commit

Permalink
Add kattis prob walrusweights in python2 + notes
Browse files Browse the repository at this point in the history
  • Loading branch information
iandioch committed May 29, 2018
1 parent e2d239b commit 2efbb3d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions kattis/walrusweights/notes.md
@@ -0,0 +1 @@
Iterate through the numbers, summing each one and every other number you have ever seen before (a set called `seen`), and making this your new `seen` set. However, this will run out of time. But you can throw away any numbers that are obviously too large and will not get you any closer to your goal of `1000`. This solution runs in 0.05s in python2, and 0.25s in python3.
21 changes: 21 additions & 0 deletions kattis/walrusweights/solution.py
@@ -0,0 +1,21 @@
def main():
n = int(raw_input())
m = sorted(map(int, (raw_input() for _ in range(n))))
seen = set()
seen.add(0)
best = 0
bestdiff = 1000
for i in m:
for o in list(seen):
d = abs(1000 - (i+o))
if d == bestdiff and best < (i+o):
best = (i+o)
elif d < bestdiff:
best = (i+o)
bestdiff = d
if d > bestdiff and (i+o) > 1000:
continue
seen.add(i+o)
print(best)

main()

0 comments on commit 2efbb3d

Please sign in to comment.