{"id":"6b093ddfbd4755deb67ee09e","slug":"sliding-window-maximum","title":"Maksimum w przesuwnym oknie","summary":"Dla każdego okna k kolejnych pomiarów podajemy wartość największą – w czasie liniowym, niezależnie od k. Kolejka monotoniczna na dwustronnej kolejce (deque).","servedLang":"pl","availableLangs":["pl","en"],"kind":"code","category":"algorithms","status":"published","difficulty":"hard","topics":[],"techs":["typescript","javascript","python","dotnet","java","go"],"tags":["sliding-window","deque","monotonic-queue","arrays"],"languages":[{"lang":"typescript","runner":"browser","starter":"/** Maximum of every window of k consecutive elements (n - k + 1 values). */\nfunction maxSlidingWindow(nums: number[], k: number): number[] {\n  // TODO\n  throw new Error('Not implemented');\n}\n","testCount":7,"tests":"test('classic example', () => {\n  expect(maxSlidingWindow([1, 3, -1, -3, 5, 3, 6, 7], 3)).toEqual([3, 3, 5, 5, 6, 7]);\n});\n\ntest('window of 1 and window of n', () => {\n  expect(maxSlidingWindow([4, -2, 7], 1)).toEqual([4, -2, 7]);\n  expect(maxSlidingWindow([5, 1, 2], 3)).toEqual([5]);\n});\n\ntest('duplicates', () => {\n  expect(maxSlidingWindow([4, 4, 4, 2, 4], 2)).toEqual([4, 4, 4, 4]);\n  expect(maxSlidingWindow([1, 3, 3, 1, 1], 3)).toEqual([3, 3, 3]);\n});\n\ntest('monotonic sequences', () => {\n  expect(maxSlidingWindow([9, 8, 7, 6, 5], 2)).toEqual([9, 8, 7, 6]);\n  expect(maxSlidingWindow([1, 2, 3, 4, 5], 2)).toEqual([2, 3, 4, 5]);\n});\n\ntest('negative numbers', () => {\n  expect(maxSlidingWindow([-5, -2, -8, -1], 2)).toEqual([-2, -2, -1]);\n});\n\ntest('the input is not modified', () => {\n  const input = [3, 1, 2, 5];\n  maxSlidingWindow(input, 2);\n  expect(input).toEqual([3, 1, 2, 5]);\n});\n\ntest('performance: 200 000 values, windows of 50 000 and 777', () => {\n  const n = 200000;\n  const a = Array.from({ length: n }, (_, i) => ((i * 7919) % 100003) - 50000);\n  const r = maxSlidingWindow(a, 50000);\n  expect(r).toHaveLength(150001);\n  expect(r.reduce((s, x) => s + x, 0)).toBe(7500294628);\n  expect([r[0], r[75000], r[150000]]).toEqual([50001, 50001, 50002]);\n  const b = Array.from({ length: n }, (_, i) => (i * i * 31 + i * 17) % 1000003);\n  const r2 = maxSlidingWindow(b, 777);\n  expect(r2).toHaveLength(199224);\n  expect(r2.reduce((s, x) => s + x, 0)).toBe(198984704137);\n  expect([r2[0], r2[199223]]).toEqual([996314, 998178]);\n});\n"},{"lang":"javascript","runner":"browser","starter":"/**\n * Maximum of every window of k consecutive elements (n - k + 1 values).\n * @param {number[]} nums\n * @param {number} k\n * @returns {number[]}\n */\nfunction maxSlidingWindow(nums, k) {\n  // TODO\n  throw new Error('Not implemented');\n}\n","testCount":7,"tests":"test('classic example', () => {\n  expect(maxSlidingWindow([1, 3, -1, -3, 5, 3, 6, 7], 3)).toEqual([3, 3, 5, 5, 6, 7]);\n});\n\ntest('window of 1 and window of n', () => {\n  expect(maxSlidingWindow([4, -2, 7], 1)).toEqual([4, -2, 7]);\n  expect(maxSlidingWindow([5, 1, 2], 3)).toEqual([5]);\n});\n\ntest('duplicates', () => {\n  expect(maxSlidingWindow([4, 4, 4, 2, 4], 2)).toEqual([4, 4, 4, 4]);\n  expect(maxSlidingWindow([1, 3, 3, 1, 1], 3)).toEqual([3, 3, 3]);\n});\n\ntest('monotonic sequences', () => {\n  expect(maxSlidingWindow([9, 8, 7, 6, 5], 2)).toEqual([9, 8, 7, 6]);\n  expect(maxSlidingWindow([1, 2, 3, 4, 5], 2)).toEqual([2, 3, 4, 5]);\n});\n\ntest('negative numbers', () => {\n  expect(maxSlidingWindow([-5, -2, -8, -1], 2)).toEqual([-2, -2, -1]);\n});\n\ntest('the input is not modified', () => {\n  const input = [3, 1, 2, 5];\n  maxSlidingWindow(input, 2);\n  expect(input).toEqual([3, 1, 2, 5]);\n});\n\ntest('performance: 200 000 values, windows of 50 000 and 777', () => {\n  const n = 200000;\n  const a = Array.from({ length: n }, (_, i) => ((i * 7919) % 100003) - 50000);\n  const r = maxSlidingWindow(a, 50000);\n  expect(r).toHaveLength(150001);\n  expect(r.reduce((s, x) => s + x, 0)).toBe(7500294628);\n  expect([r[0], r[75000], r[150000]]).toEqual([50001, 50001, 50002]);\n  const b = Array.from({ length: n }, (_, i) => (i * i * 31 + i * 17) % 1000003);\n  const r2 = maxSlidingWindow(b, 777);\n  expect(r2).toHaveLength(199224);\n  expect(r2.reduce((s, x) => s + x, 0)).toBe(198984704137);\n  expect([r2[0], r2[199223]]).toEqual([996314, 998178]);\n});\n"},{"lang":"python","runner":"browser","starter":"def max_sliding_window(nums: list[int], k: int) -> list[int]:\n    \"\"\"Maximum of every window of k consecutive elements (n - k + 1 values).\"\"\"\n    raise NotImplementedError\n","testCount":7,"tests":"def check(nums, k, expected):\n    got = max_sliding_window(nums, k)\n    assert got == expected, f\"max_sliding_window({nums[:8]}, {k}): expected {expected[:8]}, got {got[:8]}\"\n\n\ndef test_classic_example():\n    check([1, 3, -1, -3, 5, 3, 6, 7], 3, [3, 3, 5, 5, 6, 7])\n\n\ndef test_window_of_one_and_window_of_n():\n    check([4, -2, 7], 1, [4, -2, 7])\n    check([5, 1, 2], 3, [5])\n\n\ndef test_duplicates():\n    check([4, 4, 4, 2, 4], 2, [4, 4, 4, 4])\n    check([1, 3, 3, 1, 1], 3, [3, 3, 3])\n\n\ndef test_monotonic_sequences():\n    check([9, 8, 7, 6, 5], 2, [9, 8, 7, 6])\n    check([1, 2, 3, 4, 5], 2, [2, 3, 4, 5])\n\n\ndef test_negative_numbers():\n    check([-5, -2, -8, -1], 2, [-2, -2, -1])\n\n\ndef test_input_is_not_modified():\n    data = [3, 1, 2, 5]\n    max_sliding_window(data, 2)\n    assert data == [3, 1, 2, 5], f\"input changed: {data}\"\n\n\ndef test_performance_200k():\n    n = 200_000\n    a = [(i * 7919) % 100003 - 50000 for i in range(n)]\n    r = max_sliding_window(a, 50000)\n    assert len(r) == 150001, f\"expected 150001 maxima, got {len(r)}\"\n    assert sum(r) == 7500294628, f\"sum of maxima: {sum(r)}\"\n    assert (r[0], r[75000], r[150000]) == (50001, 50001, 50002), \"sample maxima\"\n    b = [(i * i * 31 + i * 17) % 1000003 for i in range(n)]\n    r2 = max_sliding_window(b, 777)\n    assert len(r2) == 199224 and sum(r2) == 198984704137, \"window 777\"\n    assert (r2[0], r2[-1]) == (996314, 998178), \"first and last maxima\"\n"},{"lang":"csharp","runner":"judge0","starter":"using System;\n\npublic static class Solution\n{\n    /// <summary>Maximum of every window of k consecutive elements (n - k + 1 values).</summary>\n    public static int[] MaxSlidingWindow(int[] nums, int k)\n    {\n        throw new NotImplementedException();\n    }\n}\n","testCount":7},{"lang":"java","runner":"judge0","starter":"class Solution {\n    /** Maximum of every window of k consecutive elements (n - k + 1 values). */\n    static int[] maxSlidingWindow(int[] nums, int k) {\n        throw new UnsupportedOperationException(\"Not implemented\");\n    }\n}\n","testCount":7},{"lang":"go","runner":"judge0","starter":"package main\n\n// MaxSlidingWindow returns the maximum of every window of k consecutive elements (n - k + 1 values).\nfunc MaxSlidingWindow(nums []int, k int) []int {\n\tpanic(\"not implemented\")\n}\n","testCount":7}],"questionCount":null,"xp":40,"stats":{"attempts":0,"solves":0,"solutions":0,"comments":0},"solveRate":null,"publishedAt":"2026-11-27T08:22:55.681Z","requestedLang":"pl","statement":{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"Panel monitoringu pokazuje dla każdej sekundy "},{"type":"text","text":"szczytowe","marks":[{"type":"bold"}]},{"type":"text","text":" opóźnienie z ostatnich "},{"type":"text","text":"k","marks":[{"type":"code"}]},{"type":"text","text":" sekund. Pomiarów są setki tysięcy, a okno potrafi mieć dziesiątki tysięcy elementów – przeliczanie maksimum od zera dla każdego okna (O(n · k)) jest zbyt wolne."}]},{"type":"heading","attrs":{"level":2,"id":"zadanie"},"content":[{"type":"text","text":"Zadanie"}]},{"type":"paragraph","content":[{"type":"text","text":"Dla tablicy "},{"type":"text","text":"nums","marks":[{"type":"code"}]},{"type":"text","text":" i rozmiaru okna "},{"type":"text","text":"k","marks":[{"type":"code"}]},{"type":"text","text":" funkcja zwraca tablicę maksimów kolejnych okien "},{"type":"text","text":"nums[i..i+k-1]","marks":[{"type":"code"}]},{"type":"text","text":" dla "},{"type":"text","text":"i = 0 … n-k","marks":[{"type":"code"}]},{"type":"text","text":" (wynik ma "},{"type":"text","text":"n - k + 1","marks":[{"type":"code"}]},{"type":"text","text":" elementów). Wejście nie może zostać zmienione."}]},{"type":"paragraph","content":[{"type":"text","text":"Sygnatury: "},{"type":"text","text":"maxSlidingWindow(nums, k)","marks":[{"type":"code"}]},{"type":"text","text":" (JS/TS), "},{"type":"text","text":"max_sliding_window(nums, k)","marks":[{"type":"code"}]},{"type":"text","text":" (Python), "},{"type":"text","text":"Solution.MaxSlidingWindow","marks":[{"type":"code"}]},{"type":"text","text":" (C#), "},{"type":"text","text":"Solution.maxSlidingWindow","marks":[{"type":"code"}]},{"type":"text","text":" (Java), "},{"type":"text","text":"MaxSlidingWindow","marks":[{"type":"code"}]},{"type":"text","text":" (Go)."}]},{"type":"heading","attrs":{"level":2,"id":"przyklady"},"content":[{"type":"text","text":"Przykłady"}]},{"type":"table","content":[{"type":"tableRow","content":[{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"nums","marks":[{"type":"code"}]}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"k","marks":[{"type":"code"}]}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"Wynik"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[1, 3, -1, -3, 5, 3, 6, 7]","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"3","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[3, 3, 5, 5, 6, 7]","marks":[{"type":"code"}]}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[4, 4, 4, 2, 4]","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"2","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[4, 4, 4, 4]","marks":[{"type":"code"}]}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[9, 8, 7, 6, 5]","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"2","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[9, 8, 7, 6]","marks":[{"type":"code"}]}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[5, 1, 2]","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"3","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[5]","marks":[{"type":"code"}]}]}]}]}]},{"type":"heading","attrs":{"level":2,"id":"ograniczenia"},"content":[{"type":"text","text":"Ograniczenia"}]},{"type":"bulletList","content":[{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"1 ≤ k ≤ n ≤ 200 000","marks":[{"type":"code"}]},{"type":"text","text":", wartości z zakresu 32-bitowego "},{"type":"text","text":"int","marks":[{"type":"code"}]},{"type":"text","text":"."}]}]}]},{"type":"heading","attrs":{"level":2,"id":"oczekiwana-zlozonosc"},"content":[{"type":"text","text":"Oczekiwana złożoność"}]},{"type":"paragraph","content":[{"type":"text","text":"Czas "},{"type":"text","text":"O(n)","marks":[{"type":"bold"}]},{"type":"text","text":", dodatkowa pamięć "},{"type":"text","text":"O(k)","marks":[{"type":"bold"}]},{"type":"text","text":"."}]}]},"hints":[],"hintsCount":3,"quiz":null,"solutionNotes":null,"unlocked":false,"external":[{"provider":"leetcode","url":"https://leetcode.com/problems/sliding-window-maximum/","title":"LeetCode 239 – Sliding Window Maximum","difficulty":null}],"articles":[],"topicsInfo":[],"author":{"id":"6b08bd2e80884893657cc8cf","name":"Redakcja Art of Devs","avatar":null,"handle":"redakcja","community":false},"capabilities":{"judge0":false,"aiReview":false},"createdAt":"2026-11-27T08:22:55.683Z","updatedAt":"2026-11-27T08:22:55.683Z"}