The sequence [0, 1, ..., N] has been jumbled, and the only clue you have for its order is an array representing whether each number is larger or smaller than the last. Given this information, reconstruct an array that is consistent with it.
Input: [None,+,+,-,+]Output: [1,2,3,0,4]Explanation: Starting with any valid number, we follow the pattern: increase, increase, decrease, increase to get a valid sequence.
We can solve this greedily by maintaining available numbers and choosing the appropriate next number based on the comparison operator. When we see a ‘+’, we want to pick the smallest available number that’s larger than current. When we see a ‘-’, we want to pick the largest available number that’s smaller than current.
classSolution {
public: vector<int> reconstructSequence(vector<string>& clues) {
int n = clues.size();
vector<int> ans;
set<int> available;
for (int i =0; i < n; i++) {
available.insert(i);
}
int curr = n /2;
ans.push_back(curr);
available.erase(curr);
for (int i =1; i < n; i++) {
if (clues[i] =="+") {
auto it = available.upper_bound(curr);
curr =*it;
} else {
auto it = available.lower_bound(curr);
--it;
curr =*it;
}
ans.push_back(curr);
available.erase(curr);
}
return ans;
}
};
classSolution {
funreconstructSequence(clues: Array<String>): IntArray {
val n = clues.size
val ans = IntArray(n)
val available = sortedSetOf<Int>()
for (i in0 until n) {
available.add(i)
}
var curr = n / 2 ans[0] = curr
available.remove(curr)
for (i in1 until n) {
curr = if (clues[i] =="+") {
available.tailSet(curr + 1).first()
} else {
available.headSet(curr).last()
}
ans[i] = curr
available.remove(curr)
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution:
defreconstructSequence(self, clues: List[str]) -> List[int]:
n = len(clues)
ans = []
available = set(range(n))
curr = n //2 ans.append(curr)
available.remove(curr)
for i in range(1, n):
if clues[i] =="+":
curr = min(x for x in available if x > curr)
else:
curr = max(x for x in available if x < curr)
ans.append(curr)
available.remove(curr)
return ans
We can use a more sophisticated approach by treating this as placing numbers optimally. We can use stacks to handle increasing and decreasing subsequences, which allows us to construct the sequence more efficiently.
classSolution {
public: vector<int> reconstructSequenceOptimal(vector<string>& clues) {
int n = clues.size();
vector<int> ans(n);
int low =0, high = n -1;
int i =1;
while (i < n) {
if (clues[i] =="+") {
ans[i -1] = low++;
while (i < n && clues[i] =="+") {
ans[i] = low++;
i++;
}
} else {
int start = i -1;
while (i < n && clues[i] =="-") {
i++;
}
for (int j = start; j < i; j++) {
ans[j] = high--;
}
}
}
if (ans[n -1] ==0) {
ans[n -1] = (ans[n -2] == low) ? high : low;
}
return ans;
}
};
classSolution {
publicint[]reconstructSequenceOptimal(String[] clues) {
int n = clues.length;
int[] ans =newint[n];
int low = 0, high = n - 1;
int i = 1;
while (i < n) {
if (clues[i].equals("+")) {
ans[i - 1]= low++;
while (i < n && clues[i].equals("+")) {
ans[i]= low++;
i++;
}
} else {
int start = i - 1;
while (i < n && clues[i].equals("-")) {
i++;
}
for (int j = start; j < i; j++) {
ans[j]= high--;
}
}
}
if (ans[n - 1]== 0) {
ans[n - 1]= (ans[n - 2]== low) ? high : low;
}
return ans;
}
}
classSolution {
funreconstructSequenceOptimal(clues: Array<String>): IntArray {
val n = clues.size
val ans = IntArray(n)
var low = 0var high = n - 1var i = 1while (i < n) {
if (clues[i] =="+") {
ans[i - 1] = low++while (i < n && clues[i] =="+") {
ans[i] = low++ i++ }
} else {
val start = i - 1while (i < n && clues[i] =="-") {
i++ }
for (j in start until i) {
ans[j] = high-- }
}
}
if (ans[n - 1] ==0) {
ans[n - 1] = if (ans[n - 2] == low) high else low
}
return ans
}
}
classSolution:
defreconstructSequenceOptimal(self, clues: List[str]) -> List[int]:
n = len(clues)
ans = [0] * n
low, high =0, n -1 i =1while i < n:
if clues[i] =="+":
ans[i -1] = low
low +=1while i < n and clues[i] =="+":
ans[i] = low
low +=1 i +=1else:
start = i -1while i < n and clues[i] =="-":
i +=1for j in range(start, i):
ans[j] = high
high -=1if ans[n -1] ==0:
ans[n -1] = high if ans[n -2] == low else low
return ans
impl Solution {
pubfnreconstruct_sequence_optimal(clues: Vec<String>) -> Vec<i32> {
let n = clues.len();
letmut ans =vec![0; n];
letmut low =0;
letmut high = (n -1) asi32;
letmut i =1;
while i < n {
if clues[i] =="+" {
ans[i -1] = low;
low +=1;
while i < n && clues[i] =="+" {
ans[i] = low;
low +=1;
i +=1;
}
} else {
let start = i -1;
while i < n && clues[i] =="-" {
i +=1;
}
for j in start..i {
ans[j] = high;
high -=1;
}
}
}
if ans[n -1] ==0 {
ans[n -1] =if ans[n -2] == low { high } else { low };
}
ans
}
}