日志3月4日

题目:

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值