Can somebody explain this test case?


  • 0
    X

    Input: [3,9,2,8,1,6,10,5,4,7]
    k: 1

    Expected output is 6, I can understand that. But what is wrong with 8?

    Below is my code

    public int kEmptySlots(int[] flowers, int k) {
        if (flowers == null || flowers.length < k + 2) return -1;
        Map<Integer, Integer> map = new HashMap<>(); // <flower, day>
        for (int i = 0; i < flowers.length; i++) map.put(flowers[i], i);
        for (int i = 1; i < flowers.length - k; i++) {
            int max = Math.max(map.get(i), map.get(i + k + 1));
            boolean match = true;
            for (int j = i + 1; j < i + k + 1; j++) {
                if (map.get(j) <= max) {
                    match = false;
                    break;
                }
            }
            if (match) return max + 1;
        }
        return -1;
    }
    

  • 0
    B

    @xsuoownit Actually I got the same confusion. Did you figure this out?


  • 0
    X

    @bingkunyang I still believe it is a valid result for this test case. I complained to LeetCode, but unfortunately, I haven't got any response yet.


Log in to reply
 

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