/* 自定義代碼塊樣式 */

herrDeng網內搜尋

自訂搜尋

Ads

2025年9月5日 星期五

C++ C Py3 Rust數bits解Leetcode 2749 Minimum Operations to Make the Intege...


C++ C Py3 Rust數bits解Leetcode 2749  Minimum Operations to Make the Integer Zero
[Py3解請進]
解Leetcode 2749. Minimum Operations to Make the Integer Zero。就程式而言,並不困難,難就在當num2為負數時如何去評估可能的上界,這真需要複雜度分析,利用成長曲線的特定,可以確定不會無上界,而導致無窮迴圈,但精確的上界就很難。
------
From a programming perspective, it's not difficult. The difficulty lies in how to evaluate the possible upper bound when num2 is negative. This really requires complexity analysis. By using the specificity of the growth curve, we can ensure that there cannot be no upper bound, which will lead to an infinite loop, but an exact upper bound is difficult to determine.
class Solution:
    def makeTheIntegerZero(self, num1: int, num2: int) -> int:
        x, k=num1, 1
        while True:
            x-=num2
            if x<k: return -1
            if k>=x.bit_count():
                return k
            k+=1
        return -1

        

沒有留言:

Related Posts Plugin for WordPress, Blogger...

熱門文章