My solution is in Python. I didn't pass all of the Test Cases (although I would have passed the test cases on my local machine, on the server in which it was marked, it exceeded the timeout period. My algorithm is therefore too inefficient. If anybody can make any suggestions).
Spoiler:Code:#!/usr/local/bin/python2.7 import sys,random,time, urllib import bisect def solve(cards): total = 1 cards.sort() for i in range(0, len(cards)): total *= (bisect.bisect_right(cards, i) - i) return total % 1000000007 amount = 0 count = 0 problems = [] while True: try: line = raw_input() if amount == 0: amount = int(line) else: if count % 2 == 1 and len(problems) < amount: problems.append(line.split(" ")) arr = [] for element in line.split(" "): arr.append( int(element) ) print solve(arr) count += 1 except: break
My initial thought is to write a custom sort function, and then store the location in the list for all possible values N (in which case I won't ever have to call upon the bisect_right method). Calling sort and then frequently calling bisect_right seems quite inefficient (though I suspect I would have passed with this algorithm in a fast, compiled language like C).
Results 1 to 1 of 1
- 15 Jan. 2012 03:55am #1
Interviewstreet CS 2 - Picking Cards
Last edited by Artificial; 15 Jan. 2012 at 03:59am.