forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.0239.Sliding Window Maximum
- Loading branch information
Showing
9 changed files
with
266 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
class Solution: | ||
def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: | ||
q, res = [], [] | ||
q, res = collections.deque(), [] | ||
for i, num in enumerate(nums): | ||
while len(q) != 0 and nums[q[-1]] <= num: | ||
q.pop(-1) | ||
if q and i - k + 1 > q[0]: | ||
q.popleft() | ||
while q and nums[q[-1]] <= num: | ||
q.pop() | ||
q.append(i) | ||
|
||
if q[0] == i - k: | ||
q = q[1:] | ||
if i >= k - 1: | ||
res.append(nums[q[0]]) | ||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
solution/0200-0299/0239.Sliding Window Maximum/Solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class Solution { | ||
public: | ||
vector<int> maxSlidingWindow(vector<int> &nums, int k) { | ||
vector<int> res; | ||
deque<int> q; | ||
for (int i = 0; i < nums.size(); ++i) | ||
{ | ||
if (!q.empty() && i - k + 1 > q.front()) | ||
q.pop_front(); | ||
while (!q.empty() && nums[q.back()] <= nums[i]) | ||
q.pop_back(); | ||
q.push_back(i); | ||
if (i >= k - 1) | ||
res.push_back(nums[q.front()]); | ||
} | ||
return res; | ||
} | ||
}; |
17 changes: 17 additions & 0 deletions
17
solution/0200-0299/0239.Sliding Window Maximum/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
func maxSlidingWindow(nums []int, k int) []int { | ||
var res []int | ||
var q []int | ||
for i, num := range nums { | ||
if len(q) > 0 && i-k+1 > q[0] { | ||
q = q[1:] | ||
} | ||
for len(q) > 0 && nums[q[len(q)-1]] <= num { | ||
q = q[:len(q)-1] | ||
} | ||
q = append(q, i) | ||
if i >= k-1 { | ||
res = append(res, nums[q[0]]) | ||
} | ||
} | ||
return res | ||
} |
22 changes: 11 additions & 11 deletions
22
solution/0200-0299/0239.Sliding Window Maximum/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,23 @@ | ||
class Solution { | ||
public int[] maxSlidingWindow(int[] nums, int k) { | ||
if (nums == null || nums.length == 0 || k <= 0) { | ||
int n = nums.length; | ||
if (n == 0) { | ||
return new int[0]; | ||
} | ||
int[] res = new int[nums.length - k + 1]; | ||
int index = 0; | ||
Deque<Integer> q = new ArrayDeque<>(k); | ||
for (int i = 0; i < nums.length; ++i) { | ||
while (!q.isEmpty() && nums[i] >= nums[q.peekLast()]) { | ||
int[] res = new int[n - k + 1]; | ||
Deque<Integer> q = new LinkedList<>(); | ||
for (int i = 0, j = 0; i < n; ++i) { | ||
if (!q.isEmpty() && i - k + 1 > q.peekFirst()) { | ||
q.pollFirst(); | ||
} | ||
while (!q.isEmpty() && nums[q.peekLast()] <= nums[i]) { | ||
q.pollLast(); | ||
} | ||
q.offerLast(i); | ||
if (i - q.peekFirst() >= k) { | ||
q.pollFirst(); | ||
} | ||
if (i >= k - 1) { | ||
res[index++] = nums[q.peekFirst()]; | ||
res[j++] = nums[q.peekFirst()]; | ||
} | ||
} | ||
return res; | ||
} | ||
} | ||
} |
Oops, something went wrong.