3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K

3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K

python 复制代码
class Solution:
    def findMaximumNumber(self, k: int, x: int) -> int:
        def check(v):
            A=list(map(int,bin(v)[2:]))
            n=len(A)
            res=p=0
            for i,v in enumerate(A):
                if v==1:
                    l=n-i-1
                    res+=(p<<l)+((l//x)<<(l-1) if l else 0)
                if (n-i)%x==0:
                    p+=v
            return res+p

        l,r=1,10**15
        while l<r:
            mid=(l+r+1)//2
            if check(mid)<=k:
                l=mid
            else:
                r=mid-1
        return l

p记录的是前面有几位可以是1

l表示空闲位

l//x表示空闲位中可以为1,位的个数

(l-1)表示当其中一位设置为1,剩余位的个数

这里会重复枚举数字,但是每个为1的位只算了一次

相关推荐
CoderYanger5 小时前
A.每日一题——2536. 子矩阵元素加 1
java·线性代数·算法·leetcode·矩阵
倦王7 小时前
力扣日刷251117
算法·leetcode·职场和发展
Swift社区11 小时前
LeetCode 427 - 建立四叉树
算法·leetcode·职场和发展
墨染点香13 小时前
LeetCode 刷题【160. 相交链表】
算法·leetcode·链表
少睡点觉13 小时前
LeetCode 238. 除自身以外数组的乘积 问题分析+解析
java·算法·leetcode
YoungHong199213 小时前
面试经典150题[066]:分隔链表(LeetCode 86)
leetcode·链表·面试
Wenhao.15 小时前
LeetCode 救生艇
算法·leetcode·golang
夏鹏今天学习了吗15 小时前
【LeetCode热题100(69/100)】字符串解码
linux·算法·leetcode
小白程序员成长日记16 小时前
2025.11.18 力扣每日一题
算法·leetcode·职场和发展
未若君雅裁18 小时前
LeetCode 18 - 四数之和 详解笔记
java·数据结构·笔记·算法·leetcode