Created
January 28, 2011 00:05
-
-
Save Surgo/799569 to your computer and use it in GitHub Desktop.
SRM495 - div2 - level one
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
#! /usr/bin/env python | |
# -*- coding: utf-8 -*- | |
"""SRM495 - div2 - level one | |
Rabbit Hanako has N boxes of carrots numbered 0 through N-1. | |
The i-th box contains carrots[i] carrots. | |
She decides to eat K carrots from these boxes. She will eat the carrots one at a time, | |
each time choosing a carrot from the box with the greatest number of carrots. | |
If there are multiple such boxes, she will choose the lowest numbered box among them. | |
Return the number of the box from which she will eat her last carrot. | |
Definition: | |
Class: CarrotBoxesEasy | |
Method: theIndex | |
Parameters: int[], int | |
Returns: int | |
Method signature: int theIndex(int[] carrots, int K) | |
""" | |
class CarrotBoxesEasy(object): | |
"""Carrot Boxes Easy | |
Constraints: | |
- carrots will contain between 1 and 50 elements, inclusive. | |
- Each element of carrots will be between 1 and 100, inclusive. | |
- K will be between 1 and the sum of all elements of carrots, inclusive. | |
>>> boxes = CarrotBoxesEasy() | |
>>> print boxes.theIndex([5, 8, ], 3) | |
1 | |
>>> print boxes.theIndex([5, 8, ], 4) | |
0 | |
>>> print boxes.theIndex([4, 9, 5, ], 18) | |
2 | |
>>> print boxes.theIndex([13, 75, 24, 55, ], 140) | |
0 | |
>>> print boxes.theIndex([14, 36, 52, 86, 27, 97, 3, 67, ], 300) | |
4 | |
""" | |
def __init__(self): | |
pass | |
def theIndex(self, carrots=[], K=1): | |
boxes = dict((i, carrots[i]) for i in range(len(carrots))) | |
index = 0 | |
for i in range(K): | |
index = max(boxes, key=boxes.get) | |
boxes[index] -= 1 | |
return index | |
if __name__ == '__main__': | |
import doctest | |
doctest.testmod() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment