Back to course home
0% completed
Vote For New Content
Requesting the time and space complexity of below code?
Sourav Singh
Oct 9, 2024
func (s *Solution) printMax(arr []int, k int) []int { result := []int{} queue := []int{} for i := 0; i < len(arr); i++ { if len(queue) > 0 && queue[0] < i-k+1 { queue = queue[1:] } for len(queue) > 0 && arr[queue[len(queue)-1]] < arr[i] { queue = queue[:len(queue)-1] } queue = append(queue, i) if i >= k-1 { result = append(result, arr[queue[0]]) } } return result }
0
0
Comments
Comments
Shubham Voraa year ago
Time Complexity Analysis:
-
Outer Loop (
for i := 0; i < len(arr); i++):- The outer loop runs once for each element in the input array
arr. Therefore, its time complexity is O(n), wherenis the length of the array.
- The outer loop runs once for each element in the input array
-
**Removing Elements fr...
On this page