Description
Description
Editorial
Editorial
Solutions
Solutions
Submissions
Submissions
Easy

You are given an integer array nums of length n and an integer k.

Initially, updated is a copy of nums.

In each step, updated is modified using nums. For each index i, independently apply the following rules:

  • If i > 0 and nums[i] > nums[i - 1], increment updated[i] by 1.
  • If i < n - 1 and nums[i] <= nums[i + 1], decrement updated[i] by 1.

All updates are performed simultaneously, using only the values from the current array nums to determine the changes.

After computing the updated array for the current step, set nums = updated for the next step.

Return nums after exactly k steps.

 

Example 1:

Input: nums = [3,1,2,4,2], k = 1

Output: [3,0,2,5,2]

Explanation:

Initially, updated = nums = [3, 1, 2, 4, 2]. The Step 1 updates are as follows:

inums[i]nums[i] > nums[i - 1]nums[i] <= nums[i + 1]Changeupdated[i]
03No left neighborFalse03
11FalseTrue-10
22TrueTrue(+1) + (-1) = 02
34TrueFalse+15
42FalseNo right neighbor02

After Step 1, nums = updated = [3, 0, 2, 5, 2]. Since k = 1, we return [3, 0, 2, 5, 2].

Example 2:

Input: nums = [1,1,2,2], k = 2

Output: [-1,-1,2,2]

Explanation:

Initially, updated = nums = [1, 1, 2, 2]. The Step 1 updates are as follows:

inums[i]nums[i] > nums[i - 1]nums[i] <= nums[i + 1]Changeupdated[i]
01No left neighborTrue-10
11FalseTrue-10
22TrueTrue(+1) + (-1) = 02
32FalseNo right neighbor02

After Step 1, nums = updated = [0, 0, 2, 2]. The Step 2 updates are as follows:

inums[i]nums[i] > nums[i - 1]nums[i] <= nums[i + 1]Changeupdated[i]
00No left neighborTrue-1-1
10FalseTrue-1-1
22TrueTrue(+1) + (-1) = 02
32FalseNo right neighbor02

After Step 2, nums = updated = [-1, -1, 2, 2]. Since k = 2, we return [-1, -1, 2, 2].

 

Constraints:

  • 2 <= n == nums.length <= 100
  • 1 <= nums[i] <= 100
  • 1 <= k <= 100
 
Code
Code
Testcase
Testcase
Test Result
Test Result