Advertisement
kevinsf90

CodeSprint2 - Picking Cards

Jan 8th, 2012
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.25 KB | None | 0 0
  1. # A python implementation of a solution to the Picking Cards
  2. # problem for Codesprint 2.
  3. # Author: Kevin Tham
  4.  
  5. def solve(numCards, sortedCards, numPickedUp):
  6.     # O(n): Calculate a table to store number of cards less than a particular card number
  7.     numLessThanOrE = {}
  8.     for index in xrange(numCards):
  9.         cardsPicked = index+1
  10.         numLessThanOrE[sortedCards[index]]= cardsPicked
  11.    
  12.     # O(n): Fill in missing values
  13.     lastValue = 0
  14.     for index in xrange(numCards):
  15.         if numLessThanOrE.has_key(index):
  16.             lastValue = numLessThanOrE[index]
  17.         else:
  18.             numLessThanOrE[index] = lastValue
  19.            
  20.     # O(n): Calculate the number of possibilities
  21.     totalPossibilities = 1
  22.     for cardsPicked in xrange(numCards):
  23.         possibleCards = numLessThanOrE[cardsPicked] - cardsPicked
  24.         if possibleCards >= 0:
  25.             totalPossibilities = (totalPossibilities * possibleCards) % 1000000007
  26.     return totalPossibilities
  27.  
  28. def main():
  29.     numCases = int(raw_input())
  30.     for eachCase in range(numCases):
  31.         numCards = int(raw_input())
  32.         sortedCards = sorted(map(int, raw_input().split()))
  33.         print solve(numCards, sortedCards, 0)
  34.    
  35. if __name__ == '__main__':
  36.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement