Description
Solutions
Find Ideal Days

Alexa is Amazon's virtual AI assistant. It makes it easy to set up your Alexa-enabled devices, listen to music, get weather updates, and much more. The team is working on a new feature that suggests days for camping based on the weather forecast.

According to a survey, a day is ideal for camping if the amount of rainfall has been non-increasing for the prior k days from the considered day, and will be non-decreasing for the following k days from the considered day. Given the predicted rainfall for the next n days, find all the ideal days. Formally, the day is ideal if the following is true:

day[i-k] ≥ day[i-k+1] ≥ ... ≥ day[i-1] ≥ day[i] ≤ day[i+1] ≤ ... ≤ day[i+k-1] ≤ day[i+k]

Return the array of ideal days in ascending order. Note that the ith element of the array represents the data for the day i + 1. It is guaranteed that there is at least one ideal day.

Function Description

Complete the function findIdealDays in the editor.

findIdealDays has the following parameters:

  1. int day[n]: predicted rainfall for each day
  2. k: an integer

Returns

int[]: the ideal days, sorted ascending

Example 1:

Input:  day = [3, 2, 2, 2, 3, 4], k = 2
Output: [3, 4]
Explanation:
The image explaining the example was already very blurry, so I decided not to include it. Anything that is unhelpful for you wouldn't be included 😌

For a day to be ideal, the amount of rainfall has to be non-increasing for the prior 2 days and non-decreasing for the following 2 days.

From the above diagram: (too blurry to be included :)

  • The rainfall trend for day3 is day1 ≥ day2 ≥ day3 ≤ day4 ≤ day5 so day3 is ideal.
  • The rainfall trend for day4 is day2 ≥ day3 ≥ day4 ≤ day5 ≤ day6 so day4 is ideal.

The answer is [3, 4].

Constraints:
    1 ≤ k ≤ n ≤ 2 * 105
    0 ≤ day[i] ≤ 109
Thumbnail 0
Testcase

Result
Case 1

input:

output: