-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNonDecreasingArrayWithOneChange.swift
More file actions
63 lines (46 loc) · 1.46 KB
/
Copy pathNonDecreasingArrayWithOneChange.swift
File metadata and controls
63 lines (46 loc) · 1.46 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
LeetCode 665. Non-decreasing Array
Given an array nums with n integers, your task is to check if it could become non-decreasing by modifying at most 1 element.
We define an array is non-decreasing if nums[i] <= nums[i + 1] holds for every i (0-based) such that (0 <= i <= n - 2).
Example 1:
Input: nums = [4,2,3]
Output: true
Explanation: You could modify the first 4 to 1 to get a non-decreasing array.
Example 2:
Input: nums = [4,2,1]
Output: false
Explanation: You can't get a non-decreasing array by modify at most one element.
Constraints:
1 <= n <= 10 ^ 4
- 10 ^ 5 <= nums[i] <= 10 ^ 5
*/
class Solution {
func checkPossibility(_ nums: [Int]) -> Bool {
var A = nums
var i = 0
var M = 0
while (i < A.count - 1) {
if A[i + 1] < A[i] {
if M == 1 {
return false
} else if i == 0 {
A[i] = A[i + 1]
M += 1
} else if (i == (A.count - 2)) {
A[i + 1] = A[i]
M += 1
} else if A[i + 1] >= A[i - 1] {
A[i] = A[i - 1]
M += 1
} else if A[i + 2] >= A[i] {
A[i + 1] = A[i]
M += 1
} else {
return false
}
}
i += 1
}
return true
}
}