forked from Ritesh25696/interviewBit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFind Permutation
38 lines (29 loc) · 929 Bytes
/
Find Permutation
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
Given a positive integer n and a string s consisting only of letters D or I, you have to find any permutation of first n positive integer that satisfy the given input string.
D means the next number is smaller, while I means the next number is greater.
Notes
- Length of given string s will always equal to n - 1
- Your solution should run in linear time and space.
Example :
Input 1:
n = 3
s = ID
Return: [1, 3, 2]
******************************************************************************************************************
vector<int> Solution::findPerm(const string A, int B) {
vector<int> res;
int max = B;
int min =1;
for(int i=B-2 ; i>=0 ; i--){
if(A[i] == 'I'){
res.push_back(max);
max--;
}
else{
res.push_back(min);
min++;
}
}
res.push_back(max);
reverse(res.begin(),res.end());
return res;
}