Aufgabe · Algorithmen schwer 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.
Signatur
/**
* @param {number[]} nums
* @param {number} k
* @returns {number[]}
*/
function window_max(nums, k)Beispiele
window_max([1, 3, -1, -3, 5, 3, 6, 7], 3) // -> [3, 3, 5, 5, 6, 7]
window_max([1], 1) // -> [1]Zusätzlich 6 versteckte Tests und 2 Performance-Tests mit großem Input unter Zeitlimit; deren Inputs werden nicht angezeigt. Die Aufgaben werden den Modellen auf Englisch gestellt.
Platz 1Anthropic Claude Opus 5.5gelöst · 10 von 10 TestsAntwort in 6.4 s · Laufzeit 679 ms · $0.01 ✓ window_max([1, 3, -1, -3, 5, 3, 6, 7], 3)erwartet [3, 3, 5, 5, 6, 7] 1 ms ✓ window_max([1], 1)erwartet [1] 1 ms ✓ <1 ms✓ 1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms⏱ 401 ms⏱ 270 msCode
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; }Platz 1DeepSeek V4.1 Flashgelöst · 10 von 10 TestsAntwort in 755 ms · Laufzeit 717 ms · $0.0001 ✓ window_max([1, 3, -1, -3, 5, 3, 6, 7], 3)erwartet [3, 3, 5, 5, 6, 7] 2 ms ✓ window_max([1], 1)erwartet [1] 1 ms ✓ 3 ms✓ 1 ms✓ 2 ms✓ 3 ms✓ 1 ms✓ 1 ms⏱ 396 ms⏱ 306 msCode
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; }Platz 1OpenAI GPT-6.1 Terragelöst · 10 von 10 TestsAntwort in 3.0 s · Laufzeit 754 ms · $0.0011 ✓ window_max([1, 3, -1, -3, 5, 3, 6, 7], 3)erwartet [3, 3, 5, 5, 6, 7] 2 ms ✓ window_max([1], 1)erwartet [1] 1 ms ✓ <1 ms✓ 1 ms✓ 1 ms✓ <1 ms✓ 3 ms✓ 3 ms⏱ 411 ms⏱ 332 msCode
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; }Platz 4Qwen3 Coder Nextnicht gelöst · 9 von 10 TestsAntwort in 3.4 s · Laufzeit 956 ms · $0.0002 ✓ window_max([1, 3, -1, -3, 5, 3, 6, 7], 3)erwartet [3, 3, 5, 5, 6, 7] 4 ms ✓ window_max([1], 1)erwartet [1] 4 ms ✓ <1 ms✓ 1 ms✓ 3 ms✓ 2 ms✓ <1 ms✓ 1 ms⏱ 440 ms⏱ Zeitlimit überschrittenCode
/** * @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; }Zweiter Versuch
nicht gelöst · 9 von 10 TestsAntwort in 3.0 s · $0.0003Das Modell sah, wie viele Tests fehlschlugen, die fehlerhaften Beispiele samt erwarteten und gelieferten Werten sowie den Grund für das Scheitern versteckter Tests, nicht aber deren Eingaben. Zählt nicht für Platzierungen oder das Rating.
✓ window_max([1, 3, -1, -3, 5, 3, 6, 7], 3)erwartet [3, 3, 5, 5, 6, 7] 2 ms ✓ window_max([1], 1)erwartet [1] 3 ms ✓ <1 ms✓ 4 ms✓ 2 ms✓ 17 ms✓ 2 ms✓ <1 ms⏱ 468 ms⏱ Zeitlimit überschrittenCode
/** * @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-Kosten: $0.01 · So laufen Runden und Wertung ab →