8000 New Problem Solution - "1718. Construct the Lexicographically Largest… · royeLin/leetcode_cpp@e836fa7 · GitHub
[go: up one dir, main page]

Skip to content

Commit e836fa7

Browse files
committed
New Problem Solution - "1718. Construct the Lexicographically Largest Valid Sequence"
1 parent 724a718 commit e836fa7

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ LeetCode
8484
|1733|[Minimum Number of People to Teach](https://leetcode.com/problems/minimum-number-of-people-to-teach/) | [C++](./algorithms/cpp/minimumNumberOfPeopleToTeach/MinimumNumberOfPeopleToTeach.cpp)|Medium|
8585
|1732|[Find the Highest Altitude](https://leetcode.com/problems/find-the-highest-altitude/) | [C++](./algorithms/cpp/findTheHighestAltitude/FindTheHighestAltitude.cpp)|Easy|
8686
|1725|[Number Of Rectangles That Can Form The Largest Square](https://leetcode.com/problems/number-of-rectangle 8000 s-that-can-form-the-largest-square/) | [C++](./algorithms/cpp/numberOfRectanglesThatCanFormTheLargestSquare/NumberOfRectanglesThatCanFormTheLargestSquare.cpp)|Easy|
87+
|1718|[Construct the Lexicographically Largest Valid Sequence](https://leetcode.com/problems/construct-the-lexicographically-largest-valid-sequence/) | [C++](./algorithms/cpp/constructTheLexicographicallyLargestValidSequence/ConstructTheLexicographicallyLargestValidSequence.cpp)|Medium|
8788
|1717|[Maximum Score From Removing Substrings](https://leetcode.com/problems/maximum-score-from-removing-substrings/) | [C++](./algorithms/cpp/maximumScoreFromRemovingSubstrings/MaximumScoreFromRemovingSubstrings.cpp)|Medium|
8889
|1716|[Calculate Money in Leetcode Bank](https://leetcode.com/problems/calculate-money-in-leetcode-bank/) | [C++](./algorithms/cpp/calculateMoneyInLeetcodeBank/CalculateMoneyInLeetcodeBank.cpp)|Easy|
8990
|1711|[Count Good Meals](https://leetcode.com/problems/count-good-meals/) | [C++](./algorithms/cpp/countGoodMeals/CountGoodMeals.cpp)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Source : https://leetcode.com/problems/construct-the-lexicographically-largest-valid-sequence/
2+
// Author : Hao Chen
3+
// Date : 2021-04-24
4+
5+
/*****************************************************************************************************
6+
*
7+
* Given an integer n, find a sequence that satisfies all of the following:
8+
*
9+
* The integer 1 occurs once in the sequence.
10+
* Each integer between 2 and n occurs twice in the sequence.
11+
* For every integer i between 2 and n, the distance between the two occurrences of i is
12+
* exactly i.
13+
*
14+
* The distance between two numbers on the sequence, a[i] and a[j], is the absolute difference of
15+
* their indices, |j - i|.
16+
*
17+
* Return the lexicographically largest sequence. It is guaranteed that under the given constraints,
18+
* there is always a solution.
19+
*
20+
* A sequence a is lexicographically larger than a sequence b (of the same length) if in the first
21+
* position where a and b differ, sequence a has a number greater than the corresponding number in b.
22+
* For example, [0,1,9,0] is lexicographically larger than [0,1,5,6] because the first position they
23+
* differ is at the third number, and 9 is greater than 5.
24+
*
25+
* Example 1:
26+
*
27+
* Input: n = 3
28+
* Output: [3,1,2,3,2]
29+
* Explanation: [2,3,2,1,3] is also a valid sequence, but [3,1,2,3,2] is the lexicographically largest
30+
* valid sequence.
31+
*
32+
* Example 2:
33+
*
34+
* Input: n = 5
35+
* Output: [5,3,1,4,3,5,2,4,2]
36+
*
37+
* Constraints:
38+
*
39+
* 1 <= n <= 20
40+
******************************************************************************************************/
41+
42+
class Solution {
43+
private:
44+
void print(vector<int>& v) {
45+
cout << "[" ;
46+
for(int i=0; i<v.size()-1; i++){
47+
cout << v[i] << ",";
48+
}
49+
cout << v[v.size()-1] << "]" << endl;
50+
}
51+
public:
52+
vector<int> constructDistancedSequence(int n) {
53+
vector<int> result(2*n-1, 0);
54+
vector<bool> available(n+1, true); // an array remember which num hasn't been chosen.
55+
available[0] = false;
56+
57+
dfs(available, result, 0, n);
58+
59+
return result;
60+
}
61+
62+
bool dfs(vector<bool>& available, vector<int>& result, int pos, int cnt){
63+
//the `cnt` means how many number has been processed.
64+
if (cnt == 0) return true;
65+
66+
//start from the bigger number.
67+
int n = 0;
68+
for(int i = available.size()-1; i > 0; i--){
69+
// if the number has already been chosen, skip to next one.
70+
if (!available[i]) continue;
71+
//if the number cannot be put into the array, skip to next one
72+
if ( i > 1 && pos + i >= result.size()) continue;
73+
if ( i > 1 && (result[pos] != 0 || result[pos+i] != 0)) continue;
74+
75+
// choose the current number `i`
76+
available[i] = false;
77+
result[pos] = i;
78+
if (i > 1) result[pos+i] = i;
79+
int next_pos = pos;
80+
while( next_pos < result.size() && result[next_pos]!=0) next_pos++;
81+
82+
//print(result);
83+
if (dfs(available, result, next_pos, cnt-1)) return true;
84+
85+
// if failed to find the answer. roll back.
86+
available[i] = true;
87+
result[pos] = 0;
88+
if (i > 1) result[pos+i] = 0;
89+
}
90+
91+
return false;
92+
}
93+
};

0 commit comments

Comments
 (0)
0