forked from keineahnung2345/leetcode-cpp-practices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1163. Last Substring in Lexicographical Order.cpp
75 lines (67 loc) · 2.5 KB
/
1163. Last Substring in Lexicographical Order.cpp
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
//TLE
//22 / 24 test cases passed.
class Solution {
public:
string lastSubstring(string s) {
int maxIdx = 0;
char maxC = '\0';
for(int i = 0; i < s.size(); i++){
if(s[i] > maxC){
maxIdx = i;
maxC = s[i];
}
}
vector<string> candidates;
for(int i = 0; i < s.size(); i++){
if(s[i] == maxC){
candidates.push_back(s.substr(i));
}
}
// cout << candidates.size() << endl;
// sort(candidates.begin(), candidates.end(),
// [](const string& a, const string& b){
// return lexicographical_compare(a.begin(), a.end(), b.begin(), b.end());
// });
nth_element(candidates.begin(), candidates.begin()+candidates.size()-1, candidates.end(),
[](const string& a, const string& b){
return lexicographical_compare(a.begin(), a.end(), b.begin(), b.end());
});
return candidates[candidates.size()-1];
}
};
//https://leetcode.com/problems/last-substring-in-lexicographical-order/discuss/363662/Short-python-code-O(n)-time-and-O(1)-space-with-proof-and-visualization
//Runtime: 104 ms, faster than 77.02% of C++ online submissions for Last Substring in Lexicographical Order.
//Memory Usage: 14.4 MB, less than 34.68% of C++ online submissions for Last Substring in Lexicographical Order.
class Solution {
public:
string lastSubstring(string s) {
int i = 0; //the start index of final substr
int j = i+1; //the start index of the substr for comparison
int k = 0; //k = the length of final substr - 1
int n = s.size();
while(j + k < n){
// cout << s.substr(i, k+1) << " " << s.substr(j, k+1) << endl;
if(s[i+k] == s[j+k]){
++k;
}else if(s[i+k] < s[j+k]){
/*
all char in s[i+1...j-1] are smaller than s[j],
so we can safely skip them
*/
i = j;
++j;
k = 0;
}else{
//s[i+k] > s[j+k]
/*
because s[j...j+k] must be <= s[i],
(otherwise i will be updated by j),
so we can start from j+k+1
*/
j = j+k+1;
k = 0;
}
}
return s.substr(i);
}
};