Created
June 12, 2018 17:10
-
-
Save jianminchen/ef504ca7caeb35e64ac6ab9db495c768 to your computer and use it in GitHub Desktop.
Leetcode 239 - sliding window maximum - using Dictionary and also SortedSet - binary search tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public int[] MaxSlidingWindow(int[] nums, int k) { | |
if (k < 1 || nums.Length == 0) | |
{ | |
return new int[0]; | |
} | |
var result = new int[nums.Length - k + 1]; | |
var map = new Dictionary<int, int>(nums.Length); | |
var binarySearchTree = new SortedSet<int>(); | |
for (int i = 0; i < nums.Length; i++) | |
{ | |
var visit = nums[i]; | |
binarySearchTree.Add(visit); // O(LogK) | |
map[visit] = i; // O(1), duplicate number will be updated with latest index | |
if (i < k - 1) | |
{ | |
continue; | |
} | |
if (i >= k && map[nums[i - k]] == (i - k)) // O(1) | |
{ | |
var kStepAway = nums[i - k]; | |
binarySearchTree.Remove(kStepAway); // O(logK) | |
map.Remove(kStepAway); // O(1) | |
} | |
result[i - k + 1] = binarySearchTree.Max; // O(1) | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment