class Solution(object):
def judgeCircle(self, moves):
"""
:type moves: str
:rtype: bool
"""
cnt = collections.Counter(moves)
return cnt['L']==cnt['R'] and cnt['D']==cnt['U']


class Solution(object):
def findClosestElements(self, arr, k, x):
"""
:type arr: List[int]
:type k: int
:type x: int
:rtype: List[int]
"""
arr.sort(key=lambda n: (abs(n-x), n))
return sorted(arr[:k])


need[0], need[1], need[2]

need[0] = min(need[0], cnt[n])+need[1]

need[1]只能来自于need[2]，所以need[1] = need[2]。

need[2] = max(0, cnt[n]-need[0])

class Solution(object):
def isPossible(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
uni = []
cnt = []
for n in nums:
if len(uni) > 0 and uni[-1]==n:
cnt[-1] += 1
else:
uni.append(n)
cnt.append(1)

stack = []
i = 0
while i < len(uni):
j = i+1
while j < len(uni) and uni[j]-1==uni[j-1]:
j += 1
if j-i<3:
return False
stack.append((i, j))
i = j

for i, j in stack:
need = [0] * 3
for k in range(i, j):
if cnt[k] < need[1]+need[2]:
return False
cnt[k] -= (need[1]+need[2])
need = min(cnt[k], need[0])+need[1], need[2], max(0, cnt[k]-need[0])
if need[1]+need[2]>0:
return False
return True


class Solution(object):
def newInteger(self, n):
"""
:type n: int
:rtype: int
"""
ans, p = 0, 1
while n > 0:
ans += (n % 9) * p
n /= 9
p *= 10
return ans