任务 · 算法 困难 JavaScript ·
Sliding window maximum
Return the maximum of every window of k consecutive elements, from left to right (len(nums) - k + 1 values). 1 ≤ k ≤ len(nums) ≤ 300,000, and k can be large.
函数签名
/**
* @param {number[]} nums
* @param {number} k
* @returns {number[]}
*/
function window_max(nums, k)示例
window_max([1, 3, -1, -3, 5, 3, 6, 7], 3) // -> [3, 3, 5, 5, 6, 7]
window_max([1], 1) // -> [1]另有 6 个隐藏测试和 2 个限时海量数据测试;其输入数据不公开。 任务以英文提示词提供给模型。
第 1 名Anthropic Claude Opus 5.5已解决 · 通过 10/10 个测试生成耗时 6.4 秒 · 执行耗时 679 毫秒 · $0.01 ✓ window_max([1, 3, -1, -3, 5, 3, 6, 7], 3)预期 [3, 3, 5, 5, 6, 7] 1 毫秒 ✓ window_max([1], 1)预期 [1] 1 毫秒 ✓ <1 毫秒✓ 1 毫秒✓ <1 毫秒✓ <1 毫秒✓ <1 毫秒✓ <1 毫秒⏱ 401 毫秒⏱ 270 毫秒代码
function window_max(nums, k) { const n = nums.length; const m = n - k + 1; const res = new Array(m); const dq = new Int32Array(n); let h = 0, t = 0; for (let i = 0; i < n; i++) { const v = nums[i]; // Remove indices whose values can no longer be a window maximum. while (t > h && nums[dq[t - 1]] <= v) t--; dq[t++] = i; // Drop the front index once it leaves the window. if (dq[h] <= i - k) h++; if (i >= k - 1) res[i - k + 1] = nums[dq[h]]; } return res; }第 1 名DeepSeek V4.1 Flash已解决 · 通过 10/10 个测试生成耗时 755 毫秒 · 执行耗时 717 毫秒 · $0.0001 ✓ window_max([1, 3, -1, -3, 5, 3, 6, 7], 3)预期 [3, 3, 5, 5, 6, 7] 2 毫秒 ✓ window_max([1], 1)预期 [1] 1 毫秒 ✓ 3 毫秒✓ 1 毫秒✓ 2 毫秒✓ 3 毫秒✓ 1 毫秒✓ 1 毫秒⏱ 396 毫秒⏱ 306 毫秒代码
function window_max(nums, k) { const n = nums.length; const res = new Array(n - k + 1); const dq = new Int32Array(n); // indices, monotonic decreasing by value let head = 0, tail = 0, ri = 0; for (let i = 0; i < n; i++) { const v = nums[i]; // pop smaller/equal from back while (tail > head && nums[dq[tail - 1]] <= v) tail--; dq[tail++] = i; // remove out-of-window from front if (dq[head] <= i - k) head++; if (i >= k - 1) res[ri++] = nums[dq[head]]; } return res; }第 1 名OpenAI GPT-6.1 Terra已解决 · 通过 10/10 个测试生成耗时 3.0 秒 · 执行耗时 754 毫秒 · $0.0011 ✓ window_max([1, 3, -1, -3, 5, 3, 6, 7], 3)预期 [3, 3, 5, 5, 6, 7] 2 毫秒 ✓ window_max([1], 1)预期 [1] 1 毫秒 ✓ <1 毫秒✓ 1 毫秒✓ 1 毫秒✓ <1 毫秒✓ 3 毫秒✓ 3 毫秒⏱ 411 毫秒⏱ 332 毫秒代码
function window_max(nums, k) { const n = nums.length; const result = new Array(n - k + 1); const deque = new Int32Array(n); let head = 0; let tail = 0; for (let i = 0; i < n; i++) { while (head < tail && deque[head] <= i - k) head++; while (head < tail && nums[deque[tail - 1]] <= nums[i]) tail--; deque[tail++] = i; if (i >= k - 1) result[i - k + 1] = nums[deque[head]]; } return result; }第 4 名Qwen3 Coder Next未解决 · 通过 9/10 个测试生成耗时 3.4 秒 · 执行耗时 956 毫秒 · $0.0002 ✓ window_max([1, 3, -1, -3, 5, 3, 6, 7], 3)预期 [3, 3, 5, 5, 6, 7] 4 毫秒 ✓ window_max([1], 1)预期 [1] 4 毫秒 ✓ <1 毫秒✓ 1 毫秒✓ 3 毫秒✓ 2 毫秒✓ <1 毫秒✓ 1 毫秒⏱ 440 毫秒⏱ 超出时间限制代码
/** * @param {number[]} nums * @param {number} k * @returns {number[]} */ function window_max(nums, k) { if (nums.length === 0 || k <= 0) return []; if (k > nums.length) k = nums.length; const result = []; const deque = []; // stores indices of elements in decreasing order for (let i = 0; i < nums.length; i++) { // Remove indices that are out of the current window while (deque.length > 0 && deque[0] <= i - k) { deque.shift(); } // Remove indices whose corresponding values are less than current element // This maintains decreasing order in the deque while (deque.length > 0 && nums[deque[deque.length - 1]] <= nums[i]) { deque.pop(); } // Add current index to deque deque.push(i); // If we have processed at least k elements, add maximum to result if (i >= k - 1) { result.push(nums[deque[0]]); } } return result; }二次尝试
未解决 · 通过 9/10 个测试生成耗时 3.0 秒 · $0.0003模型可以看到失败的测试数量、包含预期值和返回值的失败示例,以及隐藏测试未通过的原因(不含输入数据)。此成绩不计入排名或评分。
✓ window_max([1, 3, -1, -3, 5, 3, 6, 7], 3)预期 [3, 3, 5, 5, 6, 7] 2 毫秒 ✓ window_max([1], 1)预期 [1] 3 毫秒 ✓ <1 毫秒✓ 4 毫秒✓ 2 毫秒✓ 17 毫秒✓ 2 毫秒✓ <1 毫秒⏱ 468 毫秒⏱ 超出时间限制代码
/** * @param {number[]} nums * @param {number} k * @returns {number[]} */ function window_max(nums, k) { if (nums.length === 0 || k <= 0) return []; if (k > nums.length) k = nums.length; const result = []; const deque = []; // stores indices of elements in decreasing order for (let i = 0; i < nums.length; i++) { // Remove indices that are out of the current window while (deque.length > 0 && deque[0] <= i - k) { deque.shift(); } // Remove indices whose corresponding values are less than current element while (deque.length > 0 && nums[deque[deque.length - 1]] <= nums[i]) { deque.pop(); } deque.push(i); if (i >= k - 1) { result.push(nums[deque[0]]); } } return result; }
Token 成本: $0.01 · 轮次运行与评分规则 →