题目:
class Solution:
def numberOfSteps (self, num: int) -> int:
for ans in itertools.count():
if not num:
return ans
num = num - 1 if num & 1 else num >> 1
class Solution:
def largestAltitude(self, gain: List[int]) -> int:
highest = 0
now = 0
for i in gain:
now += i
if now > highest:
highest = now
return highest
class Solution:
def generateParenthesis(self, n: int) -> List[str]:
def f(l, r, s):
l == r == n and (yield s)
l < n and (yield from f(l + 1, r, s + '('))
r < l and (yield from f(l, r + 1, s + ')'))
return [*f(0, 0, '')]
class Solution:
def maxEnvelopes(self, envelopes: List[List[int]]) -> int:
if not envelopes:
return 0
N = len(envelopes)
envelopes.sort()
res = 0
dp = [1] * N
for i in range(N):
for j in range(i):
if envelopes[j][0] < envelopes[i][0] and envelopes[j][1] < envelopes[i][1]:
dp[i] = max(dp[i], dp[j] + 1)
return max(dp)
class Solution:
def maxEnvelopes(self, envelopes: List[List[int]]) -> int:
if not envelopes:
return 0
n = len(envelopes)
envelopes.sort(key=lambda x: (x[0], -x[1]))
tails, res = [0] * len(envelopes), 0
for env in envelopes:
i, j = 0, res
while i < j:
m = (i + j) // 2
if tails[m] < env[1]: i = m + 1 # 如果要求非严格递增,将此行 '<' 改为 '<=' 即可。
else: j = m
tails[i] = env[1]
if j == res: res += 1
return res
class Solution:
def numTeams(self, rating: List[int]) -> int:
res = 0
for i in range(len(rating)):
for j in range(i+1, len(rating)):
if rating[i] == rating[j]:
continue
# 设置一个标签位,判断当前是递增or递减
FLAG = 0
if rating[i] < rating[j]:
FLAG = 1
for t in range(j+1, len(rating)):
if rating[t] > rating[j] and FLAG:
res += 1
elif rating[t] < rating[j] and not FLAG:
res += 1
return res