All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0201_0300.s0239_sliding_window_maximum.solution.go Maven / Gradle / Ivy

There is a newer version: 1.8
Show newest version
package s0239_sliding_window_maximum

// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Heap_Priority_Queue
// #Sliding_Window #Queue #Monotonic_Queue #Udemy_Arrays #Big_O_Time_O(n*k)_Space_O(n+k)
// #2024_03_18_Time_168_ms_(99.41%)_Space_9.3_MB_(64.37%)

func maxSlidingWindow(nums []int, k int) []int {
	if k == 1 {
		return nums
	}
	i := 1
	prev := nums
	var reuse []int
	for ; (1 << i) <= k; i++ {
		next := maxReduce(prev, 1<<(i-1), reuse)
		reuse = prev
		prev = next
	}
	if 1<