8000 sliding_window_maximum · hitzzc/go-leetcode@1cc60e3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1cc60e3

Browse files
committed
sliding_window_maximum
1 parent de6ab40 commit 1cc60e3

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ Golang solution for leetcode. For each problem, there is a simple *_test.go to t
194194
#### [236. Lowest Common Ancestor of a Binary Tree](https://github.com/hitzzc/go-leetcode/tree/master/lowest_common_ancestor_of_a_binary_tree)
195195
#### [237. Delete Node in a Linked List](https://github.com/hitzzc/go-leetcode/tree/master/delete_node_in_a_linked_list)
196196
#### [238. product of array except self](https://github.com/hitzzc/go-leetcode/tree/master/product_of_array_except_self)
197+
#### [239. Sliding Window Maximum](https://github.com/hitzzc/go-leetcode/tree/master/sliding_window_maximum)
198+
197199

198200

199201

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package sliding_window_maximum
2+
3+
func maxSlidingWindow(nums []int, k int) []int {
4+
ret := []int{}
5+
idxArray := []int{}
6+
for i := range nums {
7+
for len(idxArray) != 0 && idxArray[0] <= i-k {
8+
idxArray = idxArray[1:]
9+
}
10+
j := len(idxArray) - 1
11+
for ; j >= 0 && nums[idxArray[j]] <= nums[i]; j-- {
12+
}
13+
idxArray = append(idxArray[:j+1], i)
14+
if i >= k-1 {
15+
ret = append(ret, nums[idxArray[0]])
16+
}
17+
}
18+
return ret
19+
}

0 commit comments

Comments
 (0)
0