My C# solution with array


  • 0
    L
    List<int> llistResult = new List<int>();
    
    if (nums != null && nums.Length > 0 && k > 0) {
        int[] lintQueue = new int[nums.Length];
    
        int lintBegin = 0;
        int lintEnd = -1;
    
        for (int i = 0; i < nums.Length; ++i) {
            while (lintBegin <= lintEnd && lintQueue[lintBegin] < (i - k + 1)) {
                lintBegin++;
            }
    
            while (lintEnd >= lintBegin && nums[lintQueue[lintEnd]] < nums[i]) {
                lintEnd--;
            }
    
            lintQueue[++lintEnd] = i;
    
            if (i >= k - 1)
                llistResult.Add(nums[lintQueue[lintBegin]]);
        }
    }
    
    return llistResult.ToArray<int>();

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.