0
点赞
收藏
分享

微信扫一扫

DAY16-T1342&面试题 05.08 -2022-01-31-非自己作答

小布_cvg 2022-02-01 阅读 23
class Solution:
    def numberOfSteps(self, num: int) -> int:
        ans = 0
        while num:
            ans += num & 1
            if num > 1:
                ans += 1
            num >>= 1
        return ans

# 作者:LeetCode-Solution
# 链接:https://leetcode-cn.com/problems/number-of-steps-to-reduce-a-number-to-zero/solution/jiang-shu-zi-bian-cheng-0-de-cao-zuo-ci-ucaa4/
class Solution:
    def findMaxValueOfEquation(self, points: List[List[int]], k: int) -> int:
        max_count = float('-inf')
        left = right = 0
        while right < points.__len__():
            # 右指针必然大于左指针
            if left >= right:
                right += 1
                continue
            # 寻找xj-xi <= k
            if points[right][0] - points[left][0] > k:
                left += 1
                continue
            # 找到最大值yi+yj+xj-xi
            max_count = max(max_count, points[left][1] + points[right][1] + points[right][0] - points[left][0])
            # yi2-xi2>yi1-xi1
            if points[right][1] - points[right][0] > points[left][1] - points[left][0]:
                left += 1
                continue
            right += 1
        return max_count

# 作者:yerikshu
# 链接:https://leetcode-cn.com/problems/max-value-of-equation/solution/hua-dong-chuang-kou-tao-lu-jie-by-yeriks-c82u/
举报

相关推荐

0 条评论