Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add kattis prob walrusweights in python2 + notes
- Loading branch information
Showing
2 changed files
with
22 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |