国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

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

這篇具有很好參考價(jià)值的文章主要介紹了Leetcode 3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

  • Leetcode 3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K
    • 1. 解題思路
    • 2. 代碼實(shí)現(xiàn)
  • 題目鏈接:3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K

1. 解題思路

這一題我的思路上就是一個(gè)二分的思路,先確定一個(gè)上下界,然后不斷通過二分來找到最大的price不超過k的值。

因此,剩下的問題就在于對(duì)于任何一個(gè)給定的 n n n,如果確定 ∑ i = 1 n p ( i ) \sum\limits_{i=1}^{n}p(i) i=1n?p(i)的結(jié)果。

這里,我們使用的是一個(gè)迭代的結(jié)果,我們將所有數(shù)用二進(jìn)制進(jìn)行表示,考察 n n n的最大一位(不妨設(shè)為第 k k k位),如果這個(gè)位剛好是 x x x的整數(shù)倍且為 1 1 1,那么考察 2 k ? 1 2^{k-1} 2k?1 n n n的這段區(qū)間,其貢獻(xiàn)的price就是 n ? 2 k ? 1 + 1 n-2^{k-1}+1 n?2k?1+1聯(lián)合上 ∑ 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=2k?1n?p(i?22k?1)的部分,以及剩下的 1 1 1 2 k ? 1 ? 1 2^{k-1}-1 2k?1?1的部分。

同樣的,如果最高位不為 x x x的整倍數(shù),那么直接忽略掉最高位,結(jié)果就是 ∑ 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=2k?1n?p(i?22k?1)的部分與剩下的 1 1 1 2 k ? 1 ? 1 2^{k-1}-1 2k?1?1的部分之和。

我們將其翻譯為代碼語言即可。

2. 代碼實(shí)現(xiàn)

給出python代碼實(shí)現(xiàn)如下:

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

提交代碼評(píng)測得到:耗時(shí)412ms,占用內(nèi)存26MB。文章來源地址http://www.zghlxwxcb.cn/news/detail-807071.html

到了這里,關(guān)于Leetcode 3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

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

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

    當(dāng)使用nacos作為注冊(cè)中心使用的時(shí)候,啟動(dòng)項(xiàng)目,正常啟動(dòng), 但是控制臺(tái)一直打印報(bào)錯(cuò),報(bào)錯(cuò)如下: 出現(xiàn)此錯(cuò)誤的原因?yàn)樵谀愕捻?xiàng)目中,pom.xml文件中使用了spring-cloud-starter-alibaba-nacos-config依賴 第一種方法: 將上述依賴注釋掉 第二種方法: 創(chuàng)建一個(gè)boostrap.yml的文件 如果經(jīng)過

    2024年02月11日
    瀏覽(22)
  • 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日
    瀏覽(19)
  • MobaXterm 連接服務(wù)器超過指定連接數(shù)量(默認(rèn)14個(gè))Warning: you have reached the maximum number of saved sessions for the

    MobaXterm 連接服務(wù)器超過指定連接數(shù)量(默認(rèn)14個(gè))Warning: you have reached the maximum number of saved sessions for the

    錯(cuò)誤提示: 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 意思就是:警告:您已達(dá)到個(gè)人版MobaXterm的最大保存會(huì)話

    2024年02月14日
    瀏覽(77)
  • SpringCloud-11-解決[NACOS HTTP-GET] The maximum number of tolerable server reconnection errors has bee

    錯(cuò)誤日志顯示的是nacos的服務(wù)數(shù)量已達(dá)最大,實(shí)際原因是配置中心出問題了。 若僅使用了nacos的發(fā)現(xiàn)功能(discovery),則不需要引入配置依賴“spring-cloud-starter-alibaba-nacos-config”,否則將會(huì)報(bào)錯(cuò),如下: 解決辦法1: 移除config依賴: 解決辦法2: bootstrap.yml中將config關(guān)閉:

    2024年02月12日
    瀏覽(18)
  • 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日
    瀏覽(22)
  • The maximum length of cell contents (text) is 32,767 charactersExcel導(dǎo)出單元格長度超長

    The maximum length of cell contents (text) is 32,767 charactersExcel導(dǎo)出單元格長度超長

    一、問題描述 導(dǎo)出excel接口報(bào)錯(cuò),錯(cuò)誤信息如下: 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) 二、定位問題 從錯(cuò)誤信息查看源碼,定位

    2024年02月16日
    瀏覽(21)
  • Java異常 #Number of lines annotated by Git is not equal to number of lines in the file, check file …

    Java異常 #Number of lines annotated by Git is not equal to number of lines in the file, check file …

    在項(xiàng)目中某個(gè) java 文件左邊欄右鍵查看代碼版本履歷(Annotate)時(shí)無法顯示,IDEA 提示:Number of lines annotated by Git is not equal to number of lines in the file, check file encoding and line separators. ? 這個(gè)問題涉及到不同操作系統(tǒng)下文本文件的換行符差異引起的。在不同操作系統(tǒng)中,文本文件的

    2024年02月03日
    瀏覽(59)
  • 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日
    瀏覽(19)
  • kafka消費(fèi)者報(bào)錯(cuò)Offset commit ......it is likely that the consumer was kicked out of the group的解決

    2022年10月份接到一個(gè)小功能,對(duì)接kafka將數(shù)據(jù)寫到數(shù)據(jù)庫,開始的需求就是無腦批量insert,隨著時(shí)間的推移,業(yè)務(wù)需求有變更,kafka的生產(chǎn)消息頻次越來越高,到今年7月份為止就每秒會(huì)有幾十條甚至上百條,然后消費(fèi)消息的代碼就報(bào)錯(cuò): Caused by: org.apache.kafka.clients.consumer.Com

    2024年02月07日
    瀏覽(23)
  • LeetCode 918. Maximum Sum Circular Subarray【數(shù)組,動(dòng)態(tài)規(guī)劃】中等

    LeetCode 918. Maximum Sum Circular Subarray【數(shù)組,動(dòng)態(tài)規(guī)劃】中等

    本文屬于「征服LeetCode」系列文章之一,這一系列正式開始于2021/08/12。由于LeetCode上部分題目有鎖,本系列將至少持續(xù)到刷完所有無鎖題之日為止;由于LeetCode還在不斷地創(chuàng)建新題,本系列的終止日期可能是永遠(yuǎn)。在這一系列刷題文章中,我不僅會(huì)講解多種解題思路及其優(yōu)化,

    2024年02月15日
    瀏覽(18)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包