Found a slow brute force way to cheat


  • 0
    X

    I hate c# doesn't have priority queue, so I try to use linq and see what happen, of course it time out, so I added some improvement, lucky it pass. If the timeout test case is sorted then it won't pass. ;-)

    public int[] MaxSlidingWindow(int[] nums, int k) {
        if (nums.Length == 0) return new int[] { };
        List<int> result = new List<int>();
        int lastMax = 0;
        for (int i = 0; i < nums.Length - k + 1; i++)
        {
            if (i == 0 || lastMax <= nums[i - 1] || lastMax <= nums[i + k - 1])
            {
                lastMax = nums.Skip(i).Take(k).Max();
            }
            result.Add(lastMax);
        }
        return result.ToArray();
    }

Log in to reply
 

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