中级农民
- 积分
- 276
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2014-5-21
- 最后登录
- 1970-1-1
|
1.
不知道怎么获取最左下的点。有点感觉像graham第一步。但是不会。mark下
2. follow up
- # k additional people
- import heapq
- def maxCloestPeopleForK(nums, k):
- # lenth of 0 and isEdge?
- min_heap = []
- for val, group in itertools.groupby(nums):
- if val == 0 :
- g = list(group)
- heapq.heappush(min_heap, ((-len(g), False)))
- if nums[0] == 0:
- heapq.heappush(min_heap, (-nums.index(1), True))
- if nums[-1] == 0:
- heapq.heappush(min_heap, (-nums[::-1].index(1), True))
- for _ in range(k):
- l, isEdge = heapq.heappop(min_heap)
- l = -l
- if isEdge:
- print l
- heapq.heappush((-(l-1), False))
- else:
- print (l + 1) /2
- heapq.heappush(min_heap, (-(l-1)/2, False))
- heapq.heappush(min_heap, (-l/2, False))
- print 'do k people'
- maxCloestPeopleForK([1,0,0,0,0,0,0,1,0,0,0],3)
复制代码
3. 摸牌
- from itertools import combinations
- def possible(cards):
- result = []
- for k in range(1,4):
- for c in combinations(cards, k):
- result.append(set(c))
- return result
- def stoneGame(cards):
- N = len(cards)
- idxs = set(range(N))
- seen = {}
- def dp(handA, handB, Aturn):
- if (frozenset(handA), frozenset(handB), Aturn) in seen:
- print 'hit'
- return seen[(frozenset(handA), frozenset(handB), Aturn) ]
- if len(handA) + len(handB) == N: return handA, handB
- cards_this_turn = possible(idxs - handA - handB)
- if Aturn:
- result = sorted([dp(handA|taken, handB, False) for taken in cards_this_turn], key = lambda x: -sum(x[0]))[0]
- seen[frozenset(handA), frozenset(handB), Aturn] = result
- return result
- else:
- result = sorted([dp(handA, handB|taken, True) for taken in cards_this_turn], key = lambda x: sum(x[0]))[0]
- seen[frozenset(handA), frozenset(handB), Aturn] = result
- return result
- result = dp(set(), set(), True)
- print(result)
- stoneGame([10,10,10,11])
- stoneGame([-10000,10,10])
- stoneGame([-10000,-10,10])
复制代码 |
|