-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsolution.js
More file actions
176 lines (142 loc) · 5.77 KB
/
Copy pathsolution.js
File metadata and controls
176 lines (142 loc) · 5.77 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
function solution(sequence) {
result = false
ChecksIfTheArrayIsStrictlyIncreasingByCheckingFromTheEndToTheBeginning()
if (result = true) {
return result
}
ChecksIfTheArrayIsStrictlyIncreasingByCheckingFromTheBeginningToTheEnd()
return result
function ChecksIfTheArrayIsStrictlyIncreasingByCheckingFromTheEndToTheBeginning() {
let sequenceWithoutEnd = cutTheCorrectElementsFromTheEndOfTheSequence(sequence)
removePenultimateElementOfArray(sequenceWithoutEnd)
let sequenceWithoutStart = cutTheCorrectElementsFromTheBeginningOfTheSequence(sequenceWithoutEnd)
checkIfThereIsASingleElementLeftInTheArray(sequenceWithoutStart)
}
function ChecksIfTheArrayIsStrictlyIncreasingByCheckingFromTheBeginningToTheEnd() {
sequenceWithoutStart = cutTheCorrectElementsFromTheBeginningOfTheSequence(sequence)
removeTheSecondElementFromTheArray(sequenceWithoutStart)
sequenceWithoutEnd = cutTheCorrectElementsFromTheEndOfTheSequence(sequenceWithoutStart)
checkIfThereIsASingleElementLeftInTheArray(sequenceWithoutEnd)
}
function checkIfThereIsASingleElementLeftInTheArray(sequence) {
if (sequence.length <= 1) {
result = true
}
}
function cutTheCorrectElementsFromTheEndOfTheSequence(sequence) {
let modifiedSequence = [...sequence]
for (let index = sequence.length-1; index > 0; index--) {
const element = sequence[index];
const previousElementToCompare = sequence[index-1]
if (element <= previousElementToCompare) {
break
} else {
modifiedSequence.pop()
}
}
return modifiedSequence
}
function removePenultimateElementOfArray(sequence) {
let element = sequence[sequence.length-1]
sequence.pop()
sequence.pop()
sequence.push(element)
}
function cutTheCorrectElementsFromTheBeginningOfTheSequence(sequence) {
let modifiedSequence = [...sequence]
for (let index = 0; index < sequence.length-1; index++) {
const element = sequence[index];
const nextElementToCompare = sequence[index+1]
if (element >= nextElementToCompare) {
break
} else {
modifiedSequence.shift()
}
}
return modifiedSequence
}
function removeTheSecondElementFromTheArray(sequence) {
let element = sequence[0]
sequence.shift()
sequence.shift()
sequence.unshift(element)
}
}
sequence = [3, 5, 67, 98, 3]
solution(sequence)
// function comparingRepeatedElements() {
// for (let index = sequence.length-1; index > 0; index--) {
// let lastElementEqual = sequence.pop()
// let firstElemetEqual = sequence.find(element => element == lastElementEqual)
// if (lastElementEqual == firstElemetEqual) {
// let indexOfFirstEqual = sequence.indexOf(lastElementEqual)
// amountElementToRemove++
// if (amountElementToRemove > 1) {
// break
// }
// decidingWhichOfTheDuplicatesToRemove(index, indexOfFirstEqual)
// }
// }
// }
// function decidingWhichOfTheDuplicatesToRemove(index, indexOfFirstEqual) {
// elementInIndex = sequence[index]
// elementBeforeIndex = sequence[index-1]
// if(elementBeforeIndex > elementInIndex) {
// removeElement(index)
// } else {
// removeElement(indexOfFirstEqual)
// }
// }
// function removeElement(index) {
// sequence.splice(index,1)
// }
// let amountElementToRemove = 0
// let elementToCut = 0
// function compareElements() {
// for (let index = 0; index < sequence.length-1; index++) {
// const element = sequence[index];
// const nextElementToCompare = sequence[index+1]
// if (element >= nextElementToCompare) {
// amountElementToRemove++
// if (amountElementToRemove > 1) {
// break
// }
// if (amountElementToRemove == 1) {
// elementToCut = index
// }
// }
// }
// }
// compareElements()
// if (amountElementToRemove <= 1) {
// amountElementToRemove = 0
// comparingRepeatedElements()
// } if (amountElementToRemove <= 1) {
// amountElementToRemove = 0
// removeElement(elementToCut)
// compareElements()
// amountElementToRemove++
// }
// isStrictlyIncreasing = amountElementToRemove <= 1
// console.log(isStrictlyIncreasing)
// return isStrictlyIncreasing
// }
// Codewriting
// 300
// 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
// solution(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
// solution(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] 4 seconds (js)
// [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.