Python – Saving Sum to Variable Seems to Change Value – What is Going On? – Leetcode 424. Longest Repeating Character Replacement

huangapple go评论52阅读模式
英文:

Python - Saving Sum to Variable Seems to Change Value - What is Going On? - Leetcode 424. Longest Repeating Character Replacement

问题

Here is the translated code part:

以下是要翻译的代码部分:

I have two similar versions of the same code - one that passes all tests for Leetcode 424. Longest Repeating Character Replacement (solved in Python) and one that does not. I need help to understand why the first version passes and the second fails.

我有两个相似的版本的相同代码,一个通过了 Leetcode 424. Longest Repeating Character Replacement 的所有测试(使用Python解决),另一个未通过。我需要帮助理解为什么第一个版本通过了,而第二个版本失败。

Here is the version that passes:

以下是通过的版本:

class Solution:
    def characterReplacement(self, s: str, k: int) -> int:
        window_start, max_len = 0, 0
        char_count = defaultdict(int)
        for window_end in range(len(s)):
            new_char = s[window_end]
            char_count[new_char] += 1
            
            while len(char_count) > 1 and (window_end - window_start + 1) - max(char_count.values()) > k:
                # shrink the window
                char_count
展开收缩
] -= 1
if char_count
展开收缩
] == 0:
del char_count
展开收缩
]
window_start += 1 # calculate the length of the window max_len = max(max_len, window_end - window_start + 1) return max_len

以下是通过的版本:

And here is the version that fails:

以下是未通过的版本:

class Solution:    
    def characterReplacement(self, s: str, k: int) -> int:
        window_start, max_len = 0, 0
        char_count = defaultdict(int)
        for window_end in range(len(s)):
            new_char = s[window_end]
            char_count[new_char] += 1
            
            window_size = window_end - window_start + 1
            while len(char_count) > 1 and (window_size) - max(char_count.values()) > k:
                # shrink the window
                char_count
展开收缩
] -= 1
if char_count
展开收缩
] == 0:
del char_count
展开收缩
]
window_start += 1 # calculate the length of the window max_len = max(max_len, window_end - window_start + 1) return max_len

以下是未通过的版本:

You will notice the ONLY difference between the two is that in the failing version I save this sum to a variable: window_end - window_start + 1 whereas in the passing version I calculate the sum in-line. What is going on? Is the act of saving this sum to a variable somehow changing its value? I am stumped.

你会注意到这两者之间唯一的区别是,在未通过的版本中,我将这个总和保存到一个变量中:window_end - window_start + 1,而在通过的版本中,我在行内计算总和。发生了什么?将这个总和保存到一个变量中是否以某种方式改变了它的值?我感到困惑。

Here are some things that I have already tried to solve the issue:

以下是我已经尝试解决这个问题的一些方法:

  • I tried adding parentheses to make sure I didn't have the order of operations wrong but kept getting the same result.

  • 我尝试添加括号以确保我没有操作顺序错误,但结果仍然相同。

  • Initially, I was so stumped when my code wasn't working that I went line by line from my solution to one of the approved solutions on Leetcode, changing any discrepancies. I had the same logical flow as the solution. Eventually, I realized that the only difference between my code and the passing code was that I saved the sum to a variable called window_size. I looked up docs on subtleties of adding integers in Python, but that search yielded few results.

  • 最初,当我的代码无法工作时,我感到非常困惑,我逐行比较了我的解决方案和Leetcode上一个经过批准的解决方案,更正了任何不一致之处。我有与解决方案相同的逻辑流。最终,我意识到我的代码和通过的代码之间唯一的区别是,我将总和保存到一个名为 window_size 的变量中。我查阅了有关在Python中添加整数的微妙之处的文档,但搜索结果不多。

英文:

I have two similar versions of the same code - one that passes all tests for Leetcode 424. Longest Repeating Character Replacement (solved in Python) and one that does not. I need help to understand why the first version passes and the second fails.

Here is the version that passes:

class Solution:
    def characterReplacement(self, s: str, k: int) -> int:
        window_start, max_len = 0, 0
        char_count = defaultdict(int)
        for window_end in range(len(s)):
            new_char = s[window_end]
            char_count[new_char] += 1
            
            while len(char_count) > 1 and (window_end - window_start + 1) - max(char_count.values()) > k:
                # shrink the window
                char_count
展开收缩
] -= 1 if char_count
展开收缩
] == 0: del char_count
展开收缩
] window_start += 1 # calculate the length of the window max_len = max(max_len, window_end - window_start + 1) return max_len

And here is the version that fails:

class Solution:    
    def characterReplacement(self, s: str, k: int) -> int:
        window_start, max_len = 0, 0
        char_count = defaultdict(int)
        for window_end in range(len(s)):
            new_char = s[window_end]
            char_count[new_char] += 1
            
            window_size = window_end - window_start + 1
            while len(char_count) > 1 and (window_size) - max(char_count.values()) > k:
                # shrink the window
                char_count
展开收缩
] -= 1 if char_count
展开收缩
] == 0: del char_count
展开收缩
] window_start += 1 # calculate the length of the window max_len = max(max_len, window_end - window_start + 1) return max_len

You will notice the ONLY difference between the two is that in the failing version I save this sum to a variable: window_end - window_start + 1 whereas in the passing version I calculate the sum in-line. What is going on? Is the act of saving this sum to a variable somehow changing its value? I am stumped.

Here are some things that I have already tried to solve the issue:

  • I tried adding parentheses to make sure I didn't have the order of operations wrong but kept getting the same result.

  • Initially, I was so stumped when my code wasn't working that I went line by line from my solution to one of the approved solutions on Leetcode, changing any discrepancies. I had the same logical flow as the solution. Eventually, I realized that the only difference between my code and the passing code was that I saved the sum to a variable called window_size. I looked up docs on subtleties of adding integers in Python, but that search yielded few results.

答案1

得分: 0

不同之处在于你的第二段代码中,window_sizewhile 循环之前计算一次,然后不再重新计算。因此,while 语句中的 (window_size) - max(char_count.values()) 始终使用最初计算的值。

根据第一段代码的正确逻辑,while 语句应该使用 window_start 的当前值计算 window_end - window_start + 1window_start 在循环的每次迭代中更新)。

英文:

The difference is that in your second piece of code, window_size is calculated once before the while loop, and never recalculated. So the determination of (window_size) - max(char_count.values()) in the while statement always uses that initially calculated value.

The correct logic, as per the first piece of code, is for the while statement to calculate window_end - window_start + 1 using the current value of window_start (which is updated on every iteration of the loop).

huangapple
  • 本文由 发表于 2023年4月6日 19:16:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75948894.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定