LeetCode-Feedback / LeetCode-Feedback

677 stars 333 forks source link

Incorrect Test Case - 2461. Maximum Sum of Distinct Subarrays With Length K #25440

Open davidfrtala opened 14 hours ago

davidfrtala commented 14 hours ago

LeetCode Username

davidfrtala

Problem Number, Title, and Link

  1. Maximum Sum of Distinct Subarrays With Length K https://leetcode.com/problems/maximum-sum-of-distinct-subarrays-with-length-k

Bug Category

Incorrect test case (Output of test case is incorrect as per the problem statement)

Bug Description

For a given test case

[9,9,9,1,2,3]
3

LeetCode expects an output of 6, however I believe that the answer should be 12.

Language Used for Code

JavaScript

Code used for Submit/Run operation

/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number}
 */
var maximumSubarraySum = function(nums, k) {
  const freq = new Set();
  let max = window = 0;
  let tail, head;

  for (let i = 0; i < nums.length; i++) {
    tail = nums[i - k];
    head = nums[i];

    if (i >= k) {
      window -= tail
      freq.delete(tail)
    }

    window += head;
    freq.add(head)

    if (freq.size === k) {
      max = Math.max(max, window)
    }
  }

  return max
};

Expected behavior

A Subarray of [9, 1, 2] meets the description criteria for a distinct subarray, providing a maximum sum of 12 as an output.

However, LeetCode suggest output 6 indicating a subarray of [1, 2, 3]

Screenshots

image

Additional context

No response

exalate-issue-sync[bot] commented 14 hours ago

LeetCode Support commented: Hello davidfrtala,

Thank you for reaching out regarding the test case in the problem "2461. Maximum Sum of Distinct Subarrays With Length K". After reviewing your report, it seems that there might be a small misunderstanding with the problem requirements. The problem specifically requests finding subarrays of length k where all elements are distinct. In the provided case, the sequence [1, 2, 3] is the valid subarray of length 3 that meets these criteria, resulting in a sum of 6.

I recommend double-checking the problem example explanations and constraints, as they clarify how the distinct condition should be implemented. If you continue to encounter difficulties or have further questions, feel free to reach out. We're here to help!

Best, LeetCode Support Team