Задача · алгоритмы сложная 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; }
Стоимость токенов: $0.01 · Как проходят и оцениваются раунды →