Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # A python implementation of a solution to the Picking Cards
- # problem for Codesprint 2.
- # Author: Kevin Tham
- def solve(numCards, sortedCards, numPickedUp):
- # O(n): Calculate a table to store number of cards less than a particular card number
- numLessThanOrE = {}
- for index in xrange(numCards):
- cardsPicked = index+1
- numLessThanOrE[sortedCards[index]]= cardsPicked
- # O(n): Fill in missing values
- lastValue = 0
- for index in xrange(numCards):
- if numLessThanOrE.has_key(index):
- lastValue = numLessThanOrE[index]
- else:
- numLessThanOrE[index] = lastValue
- # O(n): Calculate the number of possibilities
- totalPossibilities = 1
- for cardsPicked in xrange(numCards):
- possibleCards = numLessThanOrE[cardsPicked] - cardsPicked
- if possibleCards >= 0:
- totalPossibilities = (totalPossibilities * possibleCards) % 1000000007
- return totalPossibilities
- def main():
- numCases = int(raw_input())
- for eachCase in range(numCases):
- numCards = int(raw_input())
- sortedCards = sorted(map(int, raw_input().split()))
- print solve(numCards, sortedCards, 0)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement