- Train compartment problem
cat-only-icon
CAT Only
中文English
There is a railroad track and a transfer station in the middle of the railroad track. You can imagine a structure like “T”. You can think of this transfer station as a stack - the carriage is FILO(first-in last-out). There are n train carriages arranged on the rails on the right side of the transfer station from 1 to n.
Now we want to transfer these n carriages to the rail on the left side of the transfer station, to be in the order of the array arr. And each carriage enters the transfer station at most once.
Your task is to determine if the order of the arr can be reached. If possible, return the number of cars in the transfer station. If not, return -1.
Example
Example 1:
Input: arr = [4,5,3,2,1]
Output: 3
Explanation:
1 enter the transfer station
2 enter the transfer station
3 enter the transfer station
4 directly to the railroad on the left
5 directly to the railroad on the left
3 from the transfer station to the railroad on the left
2 from the transfer station to the railroad on the left
1 from the transfer station to the railroad on the left
Therefore, [4, 5, 3, 2, 1] is legal, and the number of transfer stations which is the maximum is 3.
Example 2:
Input: arr = [3,1,2]
Output: -1
Explanation:
To make the first carriage is 3, we need to let 1, 2 enter the transfer station continuously.
The transfer station is FILO, that is to say, 1 can not get the railroad on the left before 2.
Notice
n ≤ 10^5
解法1:
类似LintCode 377: Stack Push-Pop Sequence
代码如下:
class Solution {
public:
/**
* @param arr: the arr
* @return: the number of train carriages in this transfer station with the largest number of train carriages
*/
int trainCompartmentProblem(vector<int> &arr) {
int n = arr.size();
if (n == 0) return 0;
stack<int> st;
int result = 0;
int index = 0; // index is for arr, i is from [1,2,3,...]
for (int i = 1; i <= n; ++i) {
st.push(i);
result = max(result, (int)st.size() - 1);
while(!st.empty() && st.top() == arr[index]) {
st.pop();
index++;
}
}
if (!st.empty()) {
return -1;
}
return result;
}
};
本文探讨了一种铁路运输场景下的车厢调度算法,通过使用栈结构实现车厢从右侧轨道到左侧轨道的有序转移,旨在确定是否能按指定顺序完成调度,并找出调度站最大承载车厢数。
1086

被折叠的 条评论
为什么被折叠?



