Why is the code, below, throwing a TLE? Even the time complexity is O(n) it is throwing TLE QUESTION:https://leetcode.com/problems/sliding-window-maximum/ You can take this website as your reference: https://www.geeksforgeeks.org/sliding-window-maximum-maximum-of-all-subarrays-of-size-k/
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
PriorityQueue<Integer> ans = new PriorityQueue<>(Collections.reverseOrder());
ArrayList<Integer> res = new ArrayList<>();
int i = 0, n = nums.length;
int a[] = new int[n - k + 1];
int j = 0, index = 0;
while (j < n) {
ans.add(nums[j]);
if (j - i + 1 == k) {
a[index++] = ans.peek();
System.out.print(nums[i] + " ");
ans.remove(nums[i]);
i++;
}
j++;
}
System.out.println();
return a;
}
}