Python 是 |比 or 运算符慢?

发布于 2025-01-15 14:36:07 字数 1775 浏览 6 评论 0原文

对于这个Leetcode问题,似乎如果我使用以下代码,它会通过在线判断:

Given a non-empty array nums containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.

 

Example 1:

Input: nums = [1,5,11,5]
Output: true
Explanation: The array can be partitioned as [1, 5, 5] and [11].
Example 2:

Input: nums = [1,2,3,5]
Output: false
Explanation: The array cannot be partitioned into equal sum subsets.
 

Constraints:

1 <= nums.length <= 200
1 <= nums[i] <= 100

解决方案:

class Solution:

def canPartition(self, nums: List[int]) -> bool:

    @cache
    def dp(i, s):
        if s == 0:
            return True
        if i == 0 or s<0:
            return False
        if nums[i]<=s:
            return dp(i-1, s) or dp(i-1, s-nums[i])
        else:
            return dp(i-1, s)
        
    total = sum(nums)
    if total%2 != 0:
        return False
    half = total//2
    return dp(len(nums)-1, half)

但是,如果我将 dp 函数中的 or 运算符更改为 |,算法将遇到 TLE(Time Limited Exceeded),虽然答案仍然正确:

class Solution:

def canPartition(self, nums: List[int]) -> bool:

    @cache
    def dp(i, s):
        if s == 0:
            return True
        if i == 0 or s<0:
            return False
        if nums[i]<=s:
            return dp(i-1, s) | dp(i-1, s-nums[i])
        else:
            return dp(i-1, s)
        
    total = sum(nums)
    if total%2 != 0:
        return False
    half = total//2
    return dp(len(nums)-1, half)

我知道 | 用于二进制运算。但这是否意味着 | 如果应用于布尔值会很慢?

For this Leetcode question, it seems that if I use the following code, it will pass the online judge:

Given a non-empty array nums containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.

 

Example 1:

Input: nums = [1,5,11,5]
Output: true
Explanation: The array can be partitioned as [1, 5, 5] and [11].
Example 2:

Input: nums = [1,2,3,5]
Output: false
Explanation: The array cannot be partitioned into equal sum subsets.
 

Constraints:

1 <= nums.length <= 200
1 <= nums[i] <= 100

Solution:

class Solution:

def canPartition(self, nums: List[int]) -> bool:

    @cache
    def dp(i, s):
        if s == 0:
            return True
        if i == 0 or s<0:
            return False
        if nums[i]<=s:
            return dp(i-1, s) or dp(i-1, s-nums[i])
        else:
            return dp(i-1, s)
        
    total = sum(nums)
    if total%2 != 0:
        return False
    half = total//2
    return dp(len(nums)-1, half)

However, if I change the or operator in the dp function to |, the algorithm will run into TLE (Time Limited Exceeded), although the answers are still correct:

class Solution:

def canPartition(self, nums: List[int]) -> bool:

    @cache
    def dp(i, s):
        if s == 0:
            return True
        if i == 0 or s<0:
            return False
        if nums[i]<=s:
            return dp(i-1, s) | dp(i-1, s-nums[i])
        else:
            return dp(i-1, s)
        
    total = sum(nums)
    if total%2 != 0:
        return False
    half = total//2
    return dp(len(nums)-1, half)

I know | is meant for binary operations. But does this mean that | is slow if applied to boolean values?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风流物 2025-01-22 14:36:07

我认为@khelwood 和@luk2302 是对的。 短路,|则不然。换句话说,如果第一个深度优先搜索为 True,则 or 将停止评估第二个深度优先搜索,而 | 始终评估两者,导致堆栈过长,导致内存爆炸。

I think @khelwood and @luk2302 are right. or short circuits, | does not. In other words, or will stop evaluating the second depth-first-search if the first is True, while | always evaluate both, resulting an overly long stack that exploded the memory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文