-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathalmostIncreasingSequence.R
More file actions
executable file
·65 lines (64 loc) · 2.32 KB
/
almostIncreasingSequence.R
File metadata and controls
executable file
·65 lines (64 loc) · 2.32 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
64
65
# Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array.
#
# Note: sequence a0, a1, ..., an is considered to be a strictly increasing if a0 < a1 < ... < an. Sequence containing only one element is also considered to be strictly increasing.
#
# Example
#
# For sequence = [1, 3, 2, 1], the output should be
# almostIncreasingSequence(sequence) = false.
#
# There is no one element in this array that can be removed in order to get a strictly increasing sequence.
#
# For sequence = [1, 3, 2], the output should be
# almostIncreasingSequence(sequence) = true.
#
# You can remove 3 from the array to get the strictly increasing sequence [1, 2]. Alternately, you can remove 2 to get the strictly increasing sequence [1, 3].
#
# Input/Output
#
# [execution time limit] 5 seconds (r)
#
# [input] array.integer sequence
#
# Guaranteed constraints:
# 2 ≤ sequence.length ≤ 105,
# -105 ≤ sequence[i] ≤ 105.
#
# [output] boolean
# Return true if it is possible to remove one element from the array in order to get a strictly increasing sequence, otherwise return false.
# sequence = list(1,5,3,4)
# sequence = list(1, 1, 2, 3, 4, 4)
# sequence = list(1, 2, 3, 4, 5, 3, 5, 6)
# sequence = list(1,1)
almostIncreasingSequence <- function(sequence) {
sequence <- as.vector(unlist(sequence))
getEdges <- diff(sequence)
if (length(getEdges) == 1 && getEdges == 0) {
return (TRUE)
} else if (sum(getEdges > 0) == length(getEdges)) {
return (TRUE)
} else if (length(getEdges[getEdges <= 0]) > 1) {
return(FALSE)
} else if (which(getEdges < 0) == length(getEdges)) {
return(TRUE)
} else {
#try removing the 2 options and see if it is a strictly increasing sequence
newSequence = sequence
newSequence[which(getEdges < 0)] <- NA
newSequence <- newSequence[!is.na(newSequence)]
getNewEdges <- diff(newSequence)
if (sum(getNewEdges > 0) == length(getNewEdges)) {
return (TRUE)
} else {
newSequence = sequence
newSequence[which(getEdges < 0) + 1] <- NA
newSequence <- newSequence[!is.na(newSequence)]
getNewEdges <- diff(newSequence)
if (sum(getNewEdges > 0) == length(getNewEdges)) {
return(TRUE)
} else {
return(FALSE)
}
}
}
}