# O(N) LinkedList & O(N(LogK)) Dictionary + SortedSet Solution

``````    public int[] MaxSlidingWindow2(int[] nums, int k){
if (nums.Length == 0 || k == 0) return new int[0];
int[] result = new int[nums.Length - k + 1];
for (int i = 0; i < nums.Length; i++){
while (list.Count > 0 && nums[i] > list.Last.Value)
list.RemoveLast();
if (i < k - 1) continue;
result[i - k + 1] = list.First.Value;
if (nums[i - k + 1] == list.First.Value) list.RemoveFirst();
}
return result;
}
``````

O(N(LogK)) Dictionary + SortedSet

``````public int[] MaxSlidingWindow(int[] nums, int k) {
if (k < 1 || nums.Length == 0) return new int[0];
int[] result = new int[nums.Length - k + 1];
Dictionary<int, int> map = new Dictionary<int, int>(nums.Length);
SortedSet<int> sortSet = new SortedSet<int>();
for (int i = 0; i < nums.Length; i++){
map[nums[i]] = i;       // O(1)
if(i < k - 1) continue;
if (i >= k && map[nums[i - k]] == i - k)// O(1)
{
sortSet.Remove(nums[i - k]);    // O(logK)
map.Remove(nums[i - k]);        // O(1)
}
result[i - k + 1] = sortSet.Max;    // O(1)
}
return result;
}``````

The two lines in your main `if` are the same as the first two lines of your main `else`, btw. Why not take them out?