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的位只算了一次

相关推荐
技术卷4 小时前
详解力扣高频SQL50题之610. 判断三角形【简单】
sql·leetcode·oracle
小新学习屋6 小时前
《剑指offer》-数据结构篇-哈希表/数组/矩阵/字符串
数据结构·leetcode·哈希表
guozhetao6 小时前
【ST表、倍增】P7167 [eJOI 2020] Fountain (Day1)
java·c++·python·算法·leetcode·深度优先·图论
吃着火锅x唱着歌6 小时前
LeetCode 611.有效三角形的个数
算法·leetcode·职场和发展
技术卷6 小时前
详解力扣高频SQL50题之619. 只出现一次的最大数字【简单】
sql·leetcode·oracle
qq_5139704414 小时前
力扣 hot100 Day56
算法·leetcode
爱喝矿泉水的猛男17 小时前
非定长滑动窗口(持续更新)
算法·leetcode·职场和发展
YuTaoShao17 小时前
【LeetCode 热题 100】131. 分割回文串——回溯
java·算法·leetcode·深度优先
大锦终19 小时前
【算法】前缀和经典例题
算法·leetcode
cccc来财20 小时前
Java实现大根堆与小根堆详解
数据结构·算法·leetcode