Skip to content

Commit

Permalink
feat: add solutions to lcof problem: No.59.1
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglbme committed Feb 4, 2023
1 parent a6a98bb commit cce241b
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 206 deletions.
196 changes: 86 additions & 110 deletions lcof/面试题59 - I. 滑动窗口的最大值/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

<!-- 这里可写通用的实现逻辑 -->

单调队列
**方法一:单调队列**

单调队列常见模型:找出滑动窗口中的最大值/最小值。模板:

Expand All @@ -48,6 +48,8 @@ for i in range(n):
q.append(i)
```

时间复杂度 $O(n)$,空间复杂度 $O(k)$。其中 $n$ 为数组长度。

<!-- tabs:start -->

### **Python3**
Expand All @@ -57,17 +59,17 @@ for i in range(n):
```python
class Solution:
def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
q, res = deque(), []
for i, num in enumerate(nums):
if q and i - k + 1 > q[0]:
q = deque()
ans = []
for i, x in enumerate(nums):
if q and i - q[0] + 1 > k:
q.popleft()
while q and nums[q[-1]] <= num:
while q and nums[q[-1]] <= x:
q.pop()
q.append(i)
if i >= k - 1:
res.append(nums[q[0]])
return res

ans.append(nums[q[0]])
return ans
```

### **Java**
Expand All @@ -77,82 +79,45 @@ class Solution:
```java
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
int index = 0, n = nums.length;
if (k == 0 || n == 0) {
return new int[0];
}
int[] res = new int[n - k + 1];
LinkedList<Integer> q = new LinkedList<>();
int n = nums.length;
int[] ans = new int[n - k + 1];
Deque<Integer> q = new ArrayDeque<>();
for (int i = 0; i < n; ++i) {
if (!q.isEmpty() && i - q.peek() + 1 > k) {
q.poll();
}
while (!q.isEmpty() && nums[q.peekLast()] <= nums[i]) {
q.pollLast();
}
q.addLast(i);
if (q.peekFirst() == i - k) {
q.pollFirst();
}
q.offer(i);
if (i >= k - 1) {
res[index++] = nums[q.peekFirst()];
ans[i - k + 1] = nums[q.peek()];
}
}
return res;
return ans;
}
}
```

### **JavaScript**

```js
/**
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*/
var maxSlidingWindow = function (nums, k) {
if (!nums.length || !k) return [];
if (k === 1) return nums;
let res = [];
let tmpMax = -Infinity;
let len = nums.length;
let window = [];
for (let i = 0; i < k; i++) {
tmpMax = Math.max(nums[i], tmpMax);
window.push(nums[i]);
}
res.push(tmpMax);
for (let i = k; i < len; i++) {
let a = window.shift();
window.push(nums[i]);
if (nums[i] > tmpMax) {
tmpMax = nums[i];
} else if (tmpMax === a) {
tmpMax = Math.max(...window);
}
res.push(tmpMax);
}
return res;
};
```

### **C++**

```cpp
class Solution {
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
vector<int> ans;
deque<int> window;
deque<int> q;
int n = nums.size();
for (int i = 0; i < n; ++i) {
while (!window.empty() && nums[window.back()] <= nums[i]) {
window.pop_back();
if (!q.empty() && i - q.front() + 1 > k) {
q.pop_front();
}
window.push_back(i);
if (window.front() == i - k) {
window.pop_front();
while (!q.empty() && nums[q.back()] <= nums[i]) {
q.pop_back();
}
q.push_back(i);
if (i >= k - 1) {
ans.push_back(nums[window.front()]);
ans.push_back(nums[q.front()]);
}
}
return ans;
Expand All @@ -163,53 +128,72 @@ public:
### **Go**
```go
func maxSlidingWindow(nums []int, k int) []int {
ans := make([]int, 0, len(nums)-k+1)
window := make([]int, 0)
for i, num := range nums {
for len(window) != 0 && nums[window[len(window)-1]] <= num {
window = window[:len(window)-1]
func maxSlidingWindow(nums []int, k int) (ans []int) {
q := []int{}
for i, x := range nums {
for len(q) > 0 && i-q[0]+1 > k {
q = q[1:]
}
window = append(window, i)
if window[0] == i-k {
window = window[1:]
for len(q) > 0 && nums[q[len(q)-1]] <= x {
q = q[:len(q)-1]
}
q = append(q, i)
if i >= k-1 {
ans = append(ans, nums[window[0]])
ans = append(ans, nums[q[0]])
}
}
return ans
return
}
```

### **JavaScript**

```js
/**
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*/
var maxSlidingWindow = function (nums, k) {
const q = [];
const n = nums.length;
const ans = [];
for (let i = 0; i < n; ++i) {
while (q.length && i - q[0] + 1 > k) {
q.shift();
}
while (q.length && nums[q[q.length - 1]] <= nums[i]) {
q.pop();
}
q.push(i);
if (i >= k - 1) {
ans.push(nums[q[0]]);
}
}
return ans;
};
```

### **TypeScript**

```ts
function maxSlidingWindow(nums: number[], k: number): number[] {
const q: number[] = [];
const n = nums.length;
const res = [];
if (n === 0 || k === 0) {
return res;
}
const queue = [];
for (let i = 0; i < k; i++) {
while (queue.length !== 0 && queue[queue.length - 1] < nums[i]) {
queue.pop();
const ans: number[] = [];
for (let i = 0; i < n; ++i) {
while (q.length && i - q[0] + 1 > k) {
q.shift();
}
queue.push(nums[i]);
}
res.push(queue[0]);
for (let i = k; i < n; i++) {
if (queue[0] === nums[i - k]) {
queue.shift();
while (q.length && nums[q[q.length - 1]] <= nums[i]) {
q.pop();
}
while (queue.length !== 0 && queue[queue.length - 1] < nums[i]) {
queue.pop();
q.push(i);
if (i >= k - 1) {
ans.push(nums[q[0]]);
}
queue.push(nums[i]);
res.push(queue[0]);
}
return res;
return ans;
}
```

Expand All @@ -221,29 +205,21 @@ impl Solution {
pub fn max_sliding_window(nums: Vec<i32>, k: i32) -> Vec<i32> {
let k = k as usize;
let n = nums.len();
if n == 0 || k == 0 {
return Vec::new();
}
let mut res = vec![0; n - k + 1];
let mut queue = VecDeque::new();
for i in 0..k {
while !queue.is_empty() && *queue.back().unwrap() < nums[i] {
queue.pop_back();
let mut ans = vec![0; n - k + 1];
let mut q = VecDeque::new();
for i in 0..n {
while !q.is_empty() && i - q[0] + 1 > k {
q.pop_front();
}
queue.push_back(nums[i]);
}
res[0] = queue[0];
for i in k..n {
if nums[i - k] == queue[0] {
queue.pop_front();
while !q.is_empty() && nums[*q.back().unwrap()] <= nums[i] {
q.pop_back();
}
while !queue.is_empty() && *queue.back().unwrap() < nums[i] {
queue.pop_back();
q.push_back(i);
if i >= k - 1 {
ans[i - k + 1] = nums[q[0]]
}
queue.push_back(nums[i]);
res[i - k + 1] = queue[0];
}
res
ans
}
}
```
Expand Down
16 changes: 8 additions & 8 deletions lcof/面试题59 - I. 滑动窗口的最大值/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ class Solution {
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
vector<int> ans;
deque<int> window;
deque<int> q;
int n = nums.size();
for (int i = 0; i < n; ++i) {
while (!window.empty() && nums[window.back()] <= nums[i]) {
window.pop_back();
if (!q.empty() && i - q.front() + 1 > k) {
q.pop_front();
}
window.push_back(i);
if (window.front() == i - k) {
window.pop_front();
while (!q.empty() && nums[q.back()] <= nums[i]) {
q.pop_back();
}
q.push_back(i);
if (i >= k - 1) {
ans.push_back(nums[window.front()]);
ans.push_back(nums[q.front()]);
}
}
return ans;
}
};
};
23 changes: 11 additions & 12 deletions lcof/面试题59 - I. 滑动窗口的最大值/Solution.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
func maxSlidingWindow(nums []int, k int) []int {
ans := make([]int, 0, len(nums)-k+1)
window := make([]int, 0)
for i, num := range nums {
for len(window) != 0 && nums[window[len(window)-1]] <= num {
window = window[:len(window)-1]
func maxSlidingWindow(nums []int, k int) (ans []int) {
q := []int{}
for i, x := range nums {
for len(q) > 0 && i-q[0]+1 > k {
q = q[1:]
}
window = append(window, i)
if window[0] == i-k {
window = window[1:]
for len(q) > 0 && nums[q[len(q)-1]] <= x {
q = q[:len(q)-1]
}
q = append(q, i)
if i >= k-1 {
ans = append(ans, nums[window[0]])
ans = append(ans, nums[q[0]])
}
}
return ans
}
return
}
19 changes: 8 additions & 11 deletions lcof/面试题59 - I. 滑动窗口的最大值/Solution.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
int n = nums.length;
if (n == 0) {
return new int[0];
}
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();
int[] ans = new int[n - k + 1];
Deque<Integer> q = new ArrayDeque<>();
for (int i = 0; i < n; ++i) {
if (!q.isEmpty() && i - q.peek() + 1 > k) {
q.poll();
}
while (!q.isEmpty() && nums[q.peekLast()] <= nums[i]) {
q.pollLast();
}
q.offerLast(i);
q.offer(i);
if (i >= k - 1) {
res[j++] = nums[q.peekFirst()];
ans[i - k + 1] = nums[q.peek()];
}
}
return res;
return ans;
}
}
Loading

0 comments on commit cce241b

Please sign in to comment.