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

这篇具有很好参考价值的文章主要介绍了Leetcode 3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

  • Leetcode 3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K
    • 1. 解题思路
    • 2. 代码实现
  • 题目链接:3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K

1. 解题思路

这一题我的思路上就是一个二分的思路,先确定一个上下界,然后不断通过二分来找到最大的price不超过k的值。

因此,剩下的问题就在于对于任何一个给定的 n n n,如果确定 ∑ i = 1 n p ( i ) \sum\limits_{i=1}^{n}p(i) i=1np(i)的结果。

这里,我们使用的是一个迭代的结果,我们将所有数用二进制进行表示,考察 n n n的最大一位(不妨设为第 k k k位),如果这个位刚好是 x x x的整数倍且为 1 1 1,那么考察 2 k − 1 2^{k-1} 2k1 n n n的这段区间,其贡献的price就是 n − 2 k − 1 + 1 n-2^{k-1}+1 n2k1+1联合上 ∑ i = 2 k − 1 n p ( i − 2 2 k − 1 ) \sum\limits_{i=2^{k-1}}^{n} p(i - 2^{2^{k-1}}) i=2k1np(i22k1)的部分,以及剩下的 1 1 1 2 k − 1 − 1 2^{k-1}-1 2k11的部分。

同样的,如果最高位不为 x x x的整倍数,那么直接忽略掉最高位,结果就是 ∑ i = 2 k − 1 n p ( i − 2 2 k − 1 ) \sum\limits_{i=2^{k-1}}^{n} p(i - 2^{2^{k-1}}) i=2k1np(i22k1)的部分与剩下的 1 1 1 2 k − 1 − 1 2^{k-1}-1 2k11的部分之和。

我们将其翻译为代码语言即可。

2. 代码实现

给出python代码实现如下:

class Solution:
    def findMaximumNumber(self, k: int, x: int) -> int:
        
        @lru_cache(None)
        def dp(num):
            s = bin(num)[2:]
            n = len(s)
            if n < x or num == 0:
                return 0
            a, b = num ^ (1 << (n-1)), 1<<(n-1)
            if n % x == 0:
                return a+1 + dp(num-b) + dp(b-1)
            else:
                return dp(num-b) + dp(b-1)
            
        i, j = 0, 10**16
        while j - i > 1:
            m = (i+j) // 2
            if dp(m) > k:
                j = m
            else:
                i = m
        return i

提交代码评测得到:耗时412ms,占用内存26MB。文章来源地址https://www.toymoban.com/news/detail-807071.html

到了这里,关于Leetcode 3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • java.net.ConnectException: [NACOS HTTP-POST] The maximum number of tolerable server reconnection

    当使用nacos作为注册中心使用的时候,启动项目,正常启动, 但是控制台一直打印报错,报错如下: 出现此错误的原因为在你的项目中,pom.xml文件中使用了spring-cloud-starter-alibaba-nacos-config依赖 第一种方法: 将上述依赖注释掉 第二种方法: 创建一个boostrap.yml的文件 如果经过

    2024年02月11日
    浏览(37)
  • LeetCode每日一题——2520. Count the Digits That Divide a Number

    2520. Count the Digits That Divide a Number Given an integer num, return the number of digits in num that divide num. An integer val divides nums if nums % val == 0. Example 1: Input: num = 7 Output: 1 Explanation: 7 divides itself, hence the answer is 1. Example 2: Input: num = 121 Output: 2 Explanation: 121 is divisible by 1, but not 2. Since 1 occurs twic

    2024年02月08日
    浏览(29)
  • MobaXterm 连接服务器超过指定连接数量(默认14个)Warning: you have reached the maximum number of saved sessions for the

    错误提示: Warning: you have reached the maximum number of saved sessions for the personal edition of MobaXterm. You can start a new session but it wil not be automatically saved. Please support MobaXterm by subscribing to the Professional edition here: https://mobaxterm.mobatek.net 意思就是:警告:您已达到个人版MobaXterm的最大保存会话

    2024年02月14日
    浏览(71)
  • SpringCloud-11-解决[NACOS HTTP-GET] The maximum number of tolerable server reconnection errors has bee

    错误日志显示的是nacos的服务数量已达最大,实际原因是配置中心出问题了。 若仅使用了nacos的发现功能(discovery),则不需要引入配置依赖“spring-cloud-starter-alibaba-nacos-config”,否则将会报错,如下: 解决办法1: 移除config依赖: 解决办法2: bootstrap.yml中将config关闭:

    2024年02月12日
    浏览(36)
  • leetcode - 2616. Minimize the Maximum Difference of Pairs

    You are given a 0-indexed integer array nums and an integer p. Find p pairs of indices of nums such that the maximum difference amongst all the pairs is minimized. Also, ensure no index appears more than once amongst the p pairs. Note that for a pair of elements at the index i and j, the difference of this pair is |nums[i] - nums[j]|, where |x| represents th

    2024年02月13日
    浏览(31)
  • The maximum length of cell contents (text) is 32,767 charactersExcel导出单元格长度超长

    一、问题描述 导出excel接口报错,错误信息如下: ava.lang.IllegalArgumentException: The maximum length of cell contents (text) is 32767 characters at org.apache.poi.ss.usermodel.CellBase.checkLength(CellBase.java:309) at org.apache.poi.ss.usermodel.CellBase.setCellValue(CellBase.java:290) 二、定位问题 从错误信息查看源码,定位

    2024年02月16日
    浏览(34)
  • Java异常 #Number of lines annotated by Git is not equal to number of lines in the file, check file …

    在项目中某个 java 文件左边栏右键查看代码版本履历(Annotate)时无法显示,IDEA 提示:Number of lines annotated by Git is not equal to number of lines in the file, check file encoding and line separators.   这个问题涉及到不同操作系统下文本文件的换行符差异引起的。在不同操作系统中,文本文件的

    2024年02月03日
    浏览(35)
  • LeetCode //C - 918. Maximum Sum Circular Subarray

    Given a circular integer array nums of length n, return the maximum possible sum of a non-empty subarray of nums. A circular array means the end of the array connects to the beginning of the array. Formally, the next element of nums[i] is nums[(i + 1) % n] and the previous element of nums[i] is nums[(i - 1 + n) % n]. A subarray may only include each element

    2024年02月07日
    浏览(30)
  • kafka消费者报错Offset commit ......it is likely that the consumer was kicked out of the group的解决

    2022年10月份接到一个小功能,对接kafka将数据写到数据库,开始的需求就是无脑批量insert,随着时间的推移,业务需求有变更,kafka的生产消息频次越来越高,到今年7月份为止就每秒会有几十条甚至上百条,然后消费消息的代码就报错: Caused by: org.apache.kafka.clients.consumer.Com

    2024年02月07日
    浏览(42)
  • LeetCode //C - 124. Binary Tree Maximum Path Sum

    A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence at most once. Note that the path does not need to pass through the root. The path sum of a path is the sum of the node’s values in the path. Given the root of a binary tree, return the maximum

    2024年02月09日
    浏览(30)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包