|
| 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