-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1243-ArrayTransformation.cs
45 lines (38 loc) · 1.17 KB
/
1243-ArrayTransformation.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//-----------------------------------------------------------------------------
// Runtime: 220ms
// Memory Usage: 29.6 MB
// Link: https://leetcode.com/submissions/detail/344211899/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _1243_ArrayTransformation
{
public IList<int> TransformArray(int[] arr)
{
if (arr.Length <= 2) return arr;
var changed = true;
while (changed)
{
changed = false;
int prev = arr[0];
for (int i = 1; i < arr.Length - 1; i++)
{
int curr = arr[i], next = arr[i + 1];
if (prev < curr && curr > next)
{
arr[i]--;
changed = true;
}
if (prev > curr && curr < next)
{
arr[i]++;
changed = true;
}
prev = curr;
}
}
return arr;
}
}
}