| 模型 | 模型答案 | 正确答案 |
|---|---|---|
| 金声玉亮2.0 | ```python curr += x result = max(result, curr - prefix[x - k], curr - prefix[x + k]) return result if result != float("-inf") else 0 ``` |
class Solution(object): def maximumSubarraySum(self, nums, k): """ :type nums: List[int] :type k: int :rtype: int """ prefix = collections.defaultdict(lambda: float("inf")) curr = 0 result = float("-inf") for x in nums: prefix[x] = min(prefix[x], curr) curr += x result = max(result, curr-prefix[x-k], curr-prefix[x+k]) return result if result != float("-inf") else 0 |
| deepseek-r1 | ```python current_sum = curr + x if (x - k) in prefix: candidate = current_sum - prefix[x - k] if candidate > result: result = candidate if (x + k) in prefix: candidate = current_sum - prefix[x + k] if candidate > result: result = candidate curr += x return result if result != float('-inf') else 0 ``` |
class Solution(object): def maximumSubarraySum(self, nums, k): """ :type nums: List[int] :type k: int :rtype: int """ prefix = collections.defaultdict(lambda: float("inf")) curr = 0 result = float("-inf") for x in nums: prefix[x] = min(prefix[x], curr) curr += x result = max(result, curr-prefix[x-k], curr-prefix[x+k]) return result if result != float("-inf") else 0 |
| gpto1 | ```python curr += x if x - k in prefix: result = max(result, curr - prefix[x - k]) if x + k in prefix: result = max(result, curr - prefix[x + k]) return result if result != float('-inf') else 0 ``` |
class Solution(object): def maximumSubarraySum(self, nums, k): """ :type nums: List[int] :type k: int :rtype: int """ prefix = collections.defaultdict(lambda: float("inf")) curr = 0 result = float("-inf") for x in nums: prefix[x] = min(prefix[x], curr) curr += x result = max(result, curr-prefix[x-k], curr-prefix[x+k]) return result if result != float("-inf") else 0 |
| gpt-4o-2024-08-06 | ```python curr += x if x + k in prefix: result = max(result, curr - prefix[x + k]) if x - k in prefix: result = max(result, curr - prefix[x - k]) return result if result != float("-inf") else 0 ``` |
class Solution(object): def maximumSubarraySum(self, nums, k): """ :type nums: List[int] :type k: int :rtype: int """ prefix = collections.defaultdict(lambda: float("inf")) curr = 0 result = float("-inf") for x in nums: prefix[x] = min(prefix[x], curr) curr += x result = max(result, curr-prefix[x-k], curr-prefix[x+k]) return result if result != float("-inf") else 0 |
| Meta-Llama-3.1-405B-Instruct-Turbo | ```python curr += x result = max(result, curr - prefix[x - k], curr - prefix[x + k]) return result if result != float("-inf") else 0 ``` |
class Solution(object): def maximumSubarraySum(self, nums, k): """ :type nums: List[int] :type k: int :rtype: int """ prefix = collections.defaultdict(lambda: float("inf")) curr = 0 result = float("-inf") for x in nums: prefix[x] = min(prefix[x], curr) curr += x result = max(result, curr-prefix[x-k], curr-prefix[x+k]) return result if result != float("-inf") else 0 |
| Meta-Llama-3.1-70B-Instruct-Turbo | ```python curr = min(curr + x, x) if x - k in prefix: result = max(result, curr - prefix[x - k]) return max(result, 0) ``` |
class Solution(object): def maximumSubarraySum(self, nums, k): """ :type nums: List[int] :type k: int :rtype: int """ prefix = collections.defaultdict(lambda: float("inf")) curr = 0 result = float("-inf") for x in nums: prefix[x] = min(prefix[x], curr) curr += x result = max(result, curr-prefix[x-k], curr-prefix[x+k]) return result if result != float("-inf") else 0 |
| DeepSeek-Coder-V2-Lite-Instruct | ```python class Solution(object): def maximumSubarraySum(self, nums, k): """ :type nums: List[int] :type k: int :rtype: int """ prefix = collections.defaultdict(lambda: float("inf")) curr = 0 result = float("-inf") for i, x in enumerate(nums): curr += x if i >= k - 1: if curr - nums[i - k] == k and prefix[nums[i - k]] < float("inf"): result = max(result, curr - prefix[nums[i - k]]) prefix[x] = min(prefix[x], curr) curr -= nums[i - k + 1] return result if result != float("-inf") else 0 ``` |
class Solution(object): def maximumSubarraySum(self, nums, k): """ :type nums: List[int] :type k: int :rtype: int """ prefix = collections.defaultdict(lambda: float("inf")) curr = 0 result = float("-inf") for x in nums: prefix[x] = min(prefix[x], curr) curr += x result = max(result, curr-prefix[x-k], curr-prefix[x+k]) return result if result != float("-inf") else 0 |
| Qwen2-72B-Instruct | ```python if x + k in prefix: result = max(result, curr - prefix[x + k] + x) if x - k in prefix: result = max(result, curr - prefix[x - k] + x) curr += x return result ``` |
class Solution(object): def maximumSubarraySum(self, nums, k): """ :type nums: List[int] :type k: int :rtype: int """ prefix = collections.defaultdict(lambda: float("inf")) curr = 0 result = float("-inf") for x in nums: prefix[x] = min(prefix[x], curr) curr += x result = max(result, curr-prefix[x-k], curr-prefix[x+k]) return result if result != float("-inf") else 0 |
| baichuan4 | ```python curr += x if x - k in prefix: result = max(result, curr - prefix[x - k]) if x + k in prefix: result = max(result, curr - prefix[x + k]) return result if result != float("-inf") else 0 ``` |
class Solution(object): def maximumSubarraySum(self, nums, k): """ :type nums: List[int] :type k: int :rtype: int """ prefix = collections.defaultdict(lambda: float("inf")) curr = 0 result = float("-inf") for x in nums: prefix[x] = min(prefix[x], curr) curr += x result = max(result, curr-prefix[x-k], curr-prefix[x+k]) return result if result != float("-inf") else 0 |
| doubao | ```python curr += x if x - nums[curr - k] == k: result = max(result, curr - prefix[nums[curr - k]]) return max(0, result) ``` |
class Solution(object): def maximumSubarraySum(self, nums, k): """ :type nums: List[int] :type k: int :rtype: int """ prefix = collections.defaultdict(lambda: float("inf")) curr = 0 result = float("-inf") for x in nums: prefix[x] = min(prefix[x], curr) curr += x result = max(result, curr-prefix[x-k], curr-prefix[x+k]) return result if result != float("-inf") else 0 |
| gpto1 | ```python curr += x if x - k in prefix: result = max(result, curr - prefix[x - k]) if x + k in prefix: result = max(result, curr - prefix[x + k]) return result if result != float('-inf') else 0 ``` |
class Solution(object): def maximumSubarraySum(self, nums, k): """ :type nums: List[int] :type k: int :rtype: int """ prefix = collections.defaultdict(lambda: float("inf")) curr = 0 result = float("-inf") for x in nums: prefix[x] = min(prefix[x], curr) curr += x result = max(result, curr-prefix[x-k], curr-prefix[x+k]) return result if result != float("-inf") else 0 |
| kimi | ```python curr += x if abs(x - k) in prefix: result = max(result, curr - prefix[abs(x - k)]) if x + k in prefix: result = max(result, curr - prefix[x + k]) prefix[x] = min(prefix[x], curr) return max(result, 0) if result != float("-inf") else 0 ``` |
class Solution(object): def maximumSubarraySum(self, nums, k): """ :type nums: List[int] :type k: int :rtype: int """ prefix = collections.defaultdict(lambda: float("inf")) curr = 0 result = float("-inf") for x in nums: prefix[x] = min(prefix[x], curr) curr += x result = max(result, curr-prefix[x-k], curr-prefix[x+k]) return result if result != float("-inf") else 0 |
| minimax | ```python class Solution(object): def maximumSubarraySum(self, nums, k): """ :type nums: List[int] :type k: int :rtype: int """ prefix = collections.defaultdict(lambda: float("inf")) curr = 0 result = float("-inf") for x in nums: prefix[x] = min(prefix[x], curr) curr = min(curr, x) + 1 if x - k in prefix: result = max(result, curr - prefix[x - k]) return result if result != float("-inf") else 0 ``` |
class Solution(object): def maximumSubarraySum(self, nums, k): """ :type nums: List[int] :type k: int :rtype: int """ prefix = collections.defaultdict(lambda: float("inf")) curr = 0 result = float("-inf") for x in nums: prefix[x] = min(prefix[x], curr) curr += x result = max(result, curr-prefix[x-k], curr-prefix[x+k]) return result if result != float("-inf") else 0 |
| wenxinyiyan | ```python curr_diff = curr - x if curr_diff in prefix: result = max(result, curr - prefix[curr_diff]) curr += x target = curr - k if target in prefix: result = max(result, curr - prefix[target]) target = curr + k if target in prefix: result = max(result, curr - prefix[target]) return max(result, 0) ``` ### Explanation: The provided solution uses a prefix sum approach with a dictionary to keep track of the minimum prefix sum encountered so far for each element in the `nums` array. The `prefix` dictionary is initialized with a default value of positive infinity for each key, representing that we haven't encountered any prefix sum for that particular element yet. The `curr` variable is used to calculate the running prefix sum as we iterate over the `nums` array. The `result` variable is initialized with negative infinity and will be updated with the maximum sum of a good subarray found so far. Within the loop, for each element `x` in `nums`, we perform the following steps: 1. Update the `prefix` dictionary with the current prefix sum `curr` for the element `x` if it is smaller than the previously stored value. This ensures that we always have the minimum prefix sum encountered so far for each element. 2. Calculate the difference between the current prefix sum and the current element `x` as `curr_diff = curr - x`. If `curr_diff` exists in the `prefix` dictionary, it means we have found a good subarray with an absolute difference of `k` between its first and last elements (since `|nums[i] - nums[j]| == k` can be rewritten as `|(curr - x) - nums[i]| == k` for some `i < j`). We update the `result` with the maximum value between its current value and the sum of the good subarray (`curr - prefix[curr_diff]`). 3. Update the current prefix sum by adding the current element `x` to it: `curr += x`. 4. Check if there exists a prefix sum `target = curr - k` in the `prefix` dictionary. If so, it means we have found a good subarray with the desired absolute difference of `k`. We update the `result` accordingly. 5. Similarly, check if there exists a prefix sum `target = curr + k` in the `prefix` dictionary and update the `result` if a good subarray is found. Finally, we return the maximum value between the `result` and 0 to handle the case where no good subarrays exist. Note: The solution assumes that the `collections` module has been imported before using the `defaultdict` class. If not, you need to add `import collections` at the beginning of the code. |
class Solution(object): def maximumSubarraySum(self, nums, k): """ :type nums: List[int] :type k: int :rtype: int """ prefix = collections.defaultdict(lambda: float("inf")) curr = 0 result = float("-inf") for x in nums: prefix[x] = min(prefix[x], curr) curr += x result = max(result, curr-prefix[x-k], curr-prefix[x+k]) return result if result != float("-inf") else 0 |
| zhipu | ```python curr += x if x - k in prefix: result = max(result, curr - prefix[x - k]) if x + k in prefix: result = max(result, curr - prefix[x + k]) return max(result, 0) ``` |
class Solution(object): def maximumSubarraySum(self, nums, k): """ :type nums: List[int] :type k: int :rtype: int """ prefix = collections.defaultdict(lambda: float("inf")) curr = 0 result = float("-inf") for x in nums: prefix[x] = min(prefix[x], curr) curr += x result = max(result, curr-prefix[x-k], curr-prefix[x+k]) return result if result != float("-inf") else 0 |
| deepseek-v3 | ```python curr += x # Check for x - k if (x - k) in prefix: result = max(result, curr - prefix[x - k]) # Check for x + k if (x + k) in prefix: result = max(result, curr - prefix[x + k]) return max(result, 0) if result != float("-inf") else 0 ``` |
class Solution(object): def maximumSubarraySum(self, nums, k): """ :type nums: List[int] :type k: int :rtype: int """ prefix = collections.defaultdict(lambda: float("inf")) curr = 0 result = float("-inf") for x in nums: prefix[x] = min(prefix[x], curr) curr += x result = max(result, curr-prefix[x-k], curr-prefix[x+k]) return result if result != float("-inf") else 0 |