First successful submission of hard task No 239
This commit is contained in:
15
.gitignore
vendored
Normal file
15
.gitignore
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
# Binaries for programs and plugins
|
||||
*.exe
|
||||
*.exe~
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
|
||||
# Test binary, built with `go test -c`
|
||||
*.test
|
||||
|
||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||
*.out
|
||||
|
||||
# Dependency directories (remove the comment below to include it)
|
||||
# vendor/
|
||||
91
239-sliding-window-maximum.go
Normal file
91
239-sliding-window-maximum.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package main
|
||||
|
||||
type Window struct {
|
||||
nums []int
|
||||
begin int
|
||||
end int
|
||||
size int
|
||||
numsCount int
|
||||
}
|
||||
|
||||
func (w *Window) Next() *Window {
|
||||
if w.end+1 >= w.numsCount {
|
||||
return nil
|
||||
}
|
||||
w.begin++
|
||||
w.end++
|
||||
return w
|
||||
}
|
||||
|
||||
func (w *Window) InWindow(index int) bool {
|
||||
return (index >= w.begin) && (index <= w.end)
|
||||
}
|
||||
|
||||
func maxBruteForce(nums []int, begin int, end int) WindowMaximum {
|
||||
max := nums[begin]
|
||||
index := begin
|
||||
for i := begin + 1; i <= end; i++ {
|
||||
if nums[i] > max {
|
||||
max = nums[i]
|
||||
index = i
|
||||
}
|
||||
}
|
||||
|
||||
return WindowMaximum{index: index, value: max, valid: true}
|
||||
}
|
||||
|
||||
func (w *Window) Max(prevMax WindowMaximum) WindowMaximum {
|
||||
if !prevMax.valid {
|
||||
return maxBruteForce(w.nums, w.begin, w.end)
|
||||
}
|
||||
|
||||
if (w.nums[w.begin-1] == w.nums[w.begin]) && (w.nums[w.end-1] == w.nums[w.end]) {
|
||||
return prevMax
|
||||
}
|
||||
|
||||
if !w.InWindow(prevMax.index) {
|
||||
return maxBruteForce(w.nums, w.begin, w.end)
|
||||
}
|
||||
|
||||
if w.nums[w.end] >= prevMax.value {
|
||||
return WindowMaximum{value: w.nums[w.end], valid: true, index: w.end}
|
||||
}
|
||||
|
||||
return prevMax
|
||||
}
|
||||
|
||||
func NewWindow(nums []int, k int) *Window {
|
||||
w := Window{}
|
||||
w.nums = nums
|
||||
w.size = k
|
||||
w.numsCount = len(nums)
|
||||
w.begin = 0
|
||||
w.end = k - 1
|
||||
|
||||
if w.end < 0 {
|
||||
return nil
|
||||
}
|
||||
if k > w.numsCount {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &w
|
||||
}
|
||||
|
||||
type WindowMaximum struct {
|
||||
index int
|
||||
value int
|
||||
valid bool
|
||||
}
|
||||
|
||||
func MaxSlidingWindow(nums []int, k int) []int {
|
||||
maximums := []int{}
|
||||
var max WindowMaximum
|
||||
|
||||
for w := NewWindow(nums, k); w != nil; w = w.Next() {
|
||||
max = w.Max(max)
|
||||
maximums = append(maximums, max.value)
|
||||
}
|
||||
|
||||
return maximums
|
||||
}
|
||||
22
239-sliding-window-maximum_test.go
Normal file
22
239-sliding-window-maximum_test.go
Normal file
File diff suppressed because one or more lines are too long
21
LICENSE
Normal file
21
LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2022 kr0st
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
12
go.mod
Normal file
12
go.mod
Normal file
@@ -0,0 +1,12 @@
|
||||
module github.com/kr0st/leetcode
|
||||
|
||||
go 1.17
|
||||
|
||||
require github.com/stretchr/testify v1.7.0
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.0 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/stretchr/objx v0.1.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
|
||||
)
|
||||
Reference in New Issue
Block a user