id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
912 | <p>You are given a <strong>0-indexed</strong> array of positive integers <code>w</code> where <code>w[i]</code> describes the <strong>weight</strong> of the <code>i<sup>th</sup></code> index.</p>
<p>You need to implement the function <code>pickIndex()</code>, which <strong>randomly</strong> picks an index in the range <code>[0, w.length - 1]</code> (<strong>inclusive</strong>) and returns it. The <strong>probability</strong> of picking an index <code>i</code> is <code>w[i] / sum(w)</code>.</p>
<ul>
<li>For example, if <code>w = [1, 3]</code>, the probability of picking index <code>0</code> is <code>1 / (1 + 3) = 0.25</code> (i.e., <code>25%</code>), and the probability of picking index <code>1</code> is <code>3 / (1 + 3) = 0.75</code> (i.e., <code>75%</code>).</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input</strong>
["Solution","pickIndex"]
[[[1]],[]]
<strong>Output</strong>
[null,0]
<strong>Explanation</strong>
Solution solution = new Solution([1]);
solution.pickIndex(); // return 0. The only option is to return 0 since there is only one element in w.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input</strong>
["Solution","pickIndex","pickIndex","pickIndex","pickIndex","pickIndex"]
[[[1,3]],[],[],[],[],[]]
<strong>Output</strong>
[null,1,1,1,1,0]
<strong>Explanation</strong>
Solution solution = new Solution([1, 3]);
solution.pickIndex(); // return 1. It is returning the second element (index = 1) that has a probability of 3/4.
solution.pickIndex(); // return 1
solution.pickIndex(); // return 1
solution.pickIndex(); // return 1
solution.pickIndex(); // return 0. It is returning the first element (index = 0) that has a probability of 1/4.
Since this is a randomization problem, multiple answers are allowed.
All of the following outputs can be considered correct:
[null,1,1,1,1,0]
[null,1,1,1,1,1]
[null,1,1,1,0,0]
[null,1,1,1,0,1]
[null,1,0,1,0,0]
......
and so on.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= w.length <= 10<sup>4</sup></code></li>
<li><code>1 <= w[i] <= 10<sup>5</sup></code></li>
<li><code>pickIndex</code> will be called at most <code>10<sup>4</sup></code> times.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n Solution(vector<int>& w) : gen(std::chrono::system_clock::now().time_since_epoch().count()), dis(0,w.size()-1) {\n num_curr_calls = 0;\n max_num_calls = 0;\n last_selected_index = -1;\n index_count_vec = vector<int>(w.size(), 0);\n for (const int elem: w) max_num_calls += elem;\n std::cout << \"[Before] max_num_calls = \" << max_num_calls << \"\\n\";\n // orig_w = std::move(w);\n const int factor = (max_num_calls > 100000) ? 100000 : 1000;\n if (max_num_calls <= factor) orig_w = std::move(w);\n else {\n orig_w = vector<int>(w.size(), 0);\n std::cout << \"orig_w[\" << orig_w.size() << \"] = { \";\n int new_max_num_calls = 0;\n for (int i=0; i<w.size(); ++i) {\n orig_w[i] = (w[i]/static_cast<double>(max_num_calls)) * factor;\n new_max_num_calls += orig_w[i];\n std::cout << orig_w[i] << \" \";\n }\n std::cout << \"}\\n\";\n max_num_calls = new_max_num_calls;\n }\n std::cout << \"[After] max_num_calls = \" << max_num_calls << \"\\n\";\n }\n \n int pickIndex() {\n if (num_curr_calls == max_num_calls) {\n num_curr_calls = 0;\n index_count_vec = vector<int>(orig_w.size(), 0);\n }\n\n ++num_curr_calls;\n int random_index = dis(gen);\n while (index_count_vec[random_index] == orig_w[random_index]) random_index = dis(gen);\n ++index_count_vec[random_index];\n\n // int next_index = (last_selected_index == index_count_vec.size()-1) ? 0 : last_selected_index+1;\n // for (int i=next_index; i<index_count_vec.size(); ++i) {\n // if (index_count_vec[i] == orig_w[i]) continue;\n // last_selected_index = i;\n // random_index = i;\n // ++index_count_vec[i];\n // break;\n // }\n \n return random_index;\n }\n\nprivate:\n int num_curr_calls;\n int max_num_calls;\n int last_selected_index;\n vector<int> index_count_vec;\n vector<int> orig_w;\n \n std::mt19937 gen;\n std::uniform_int_distribution<> dis;\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(w);\n * int param_1 = obj->pickIndex();\n */",
"memory": "45700"
} |
912 | <p>You are given a <strong>0-indexed</strong> array of positive integers <code>w</code> where <code>w[i]</code> describes the <strong>weight</strong> of the <code>i<sup>th</sup></code> index.</p>
<p>You need to implement the function <code>pickIndex()</code>, which <strong>randomly</strong> picks an index in the range <code>[0, w.length - 1]</code> (<strong>inclusive</strong>) and returns it. The <strong>probability</strong> of picking an index <code>i</code> is <code>w[i] / sum(w)</code>.</p>
<ul>
<li>For example, if <code>w = [1, 3]</code>, the probability of picking index <code>0</code> is <code>1 / (1 + 3) = 0.25</code> (i.e., <code>25%</code>), and the probability of picking index <code>1</code> is <code>3 / (1 + 3) = 0.75</code> (i.e., <code>75%</code>).</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input</strong>
["Solution","pickIndex"]
[[[1]],[]]
<strong>Output</strong>
[null,0]
<strong>Explanation</strong>
Solution solution = new Solution([1]);
solution.pickIndex(); // return 0. The only option is to return 0 since there is only one element in w.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input</strong>
["Solution","pickIndex","pickIndex","pickIndex","pickIndex","pickIndex"]
[[[1,3]],[],[],[],[],[]]
<strong>Output</strong>
[null,1,1,1,1,0]
<strong>Explanation</strong>
Solution solution = new Solution([1, 3]);
solution.pickIndex(); // return 1. It is returning the second element (index = 1) that has a probability of 3/4.
solution.pickIndex(); // return 1
solution.pickIndex(); // return 1
solution.pickIndex(); // return 1
solution.pickIndex(); // return 0. It is returning the first element (index = 0) that has a probability of 1/4.
Since this is a randomization problem, multiple answers are allowed.
All of the following outputs can be considered correct:
[null,1,1,1,1,0]
[null,1,1,1,1,1]
[null,1,1,1,0,0]
[null,1,1,1,0,1]
[null,1,0,1,0,0]
......
and so on.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= w.length <= 10<sup>4</sup></code></li>
<li><code>1 <= w[i] <= 10<sup>5</sup></code></li>
<li><code>pickIndex</code> will be called at most <code>10<sup>4</sup></code> times.</li>
</ul>
| 3 | {
"code": "// https://www.youtube.com/watch?v=fWS0TCcr-lE\n// Create Buckets according to the contribution of each index value.\nclass Solution {\npublic:\n vector<int> wt;\n vector<int> pf;\n Solution(vector<int>& w) {\n for(int& i : w)\n {\n wt.push_back(i);\n }\n\n pf.push_back(wt[0]);\n for (int i=1; i<wt.size(); i++)\n {\n pf.push_back(wt[i] + pf[i-1]);\n }\n }\n \n int pickIndex() {\n int randwt = rand()%(pf[pf.size()-1]); // this pf denotes the complete cake available for distribution\n return upper_bound(pf.begin(), pf.end(), randwt) - pf.begin(); // according to this value will be find the corresponding index\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(w);\n * int param_1 = obj->pickIndex();\n */",
"memory": "45800"
} |
912 | <p>You are given a <strong>0-indexed</strong> array of positive integers <code>w</code> where <code>w[i]</code> describes the <strong>weight</strong> of the <code>i<sup>th</sup></code> index.</p>
<p>You need to implement the function <code>pickIndex()</code>, which <strong>randomly</strong> picks an index in the range <code>[0, w.length - 1]</code> (<strong>inclusive</strong>) and returns it. The <strong>probability</strong> of picking an index <code>i</code> is <code>w[i] / sum(w)</code>.</p>
<ul>
<li>For example, if <code>w = [1, 3]</code>, the probability of picking index <code>0</code> is <code>1 / (1 + 3) = 0.25</code> (i.e., <code>25%</code>), and the probability of picking index <code>1</code> is <code>3 / (1 + 3) = 0.75</code> (i.e., <code>75%</code>).</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input</strong>
["Solution","pickIndex"]
[[[1]],[]]
<strong>Output</strong>
[null,0]
<strong>Explanation</strong>
Solution solution = new Solution([1]);
solution.pickIndex(); // return 0. The only option is to return 0 since there is only one element in w.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input</strong>
["Solution","pickIndex","pickIndex","pickIndex","pickIndex","pickIndex"]
[[[1,3]],[],[],[],[],[]]
<strong>Output</strong>
[null,1,1,1,1,0]
<strong>Explanation</strong>
Solution solution = new Solution([1, 3]);
solution.pickIndex(); // return 1. It is returning the second element (index = 1) that has a probability of 3/4.
solution.pickIndex(); // return 1
solution.pickIndex(); // return 1
solution.pickIndex(); // return 1
solution.pickIndex(); // return 0. It is returning the first element (index = 0) that has a probability of 1/4.
Since this is a randomization problem, multiple answers are allowed.
All of the following outputs can be considered correct:
[null,1,1,1,1,0]
[null,1,1,1,1,1]
[null,1,1,1,0,0]
[null,1,1,1,0,1]
[null,1,0,1,0,0]
......
and so on.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= w.length <= 10<sup>4</sup></code></li>
<li><code>1 <= w[i] <= 10<sup>5</sup></code></li>
<li><code>pickIndex</code> will be called at most <code>10<sup>4</sup></code> times.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n Solution(vector<int>& w) : gen(std::chrono::system_clock::now().time_since_epoch().count()), dis(0,w.size()-1) {\n num_curr_calls = 0;\n max_num_calls = 0;\n last_selected_index = -1;\n index_count_vec = vector<int>(w.size(), 0);\n for (const int elem: w) max_num_calls += elem;\n const int factor = (max_num_calls > 100000) ? 100000 : 1000;\n if (max_num_calls <= factor) orig_w = std::move(w);\n else {\n orig_w = vector<int>(w.size(), 0);\n int new_max_num_calls = 0;\n for (int i=0; i<w.size(); ++i) {\n orig_w[i] = (w[i]/static_cast<double>(max_num_calls)) * factor;\n new_max_num_calls += orig_w[i];\n }\n max_num_calls = new_max_num_calls;\n }\n }\n \n int pickIndex() {\n if (num_curr_calls == max_num_calls) {\n num_curr_calls = 0;\n index_count_vec = vector<int>(orig_w.size(), 0);\n }\n\n ++num_curr_calls;\n int random_index = dis(gen);\n while (index_count_vec[random_index] == orig_w[random_index]) random_index = dis(gen);\n ++index_count_vec[random_index];\n \n return random_index;\n }\n\nprivate:\n int num_curr_calls;\n int max_num_calls;\n int last_selected_index;\n vector<int> index_count_vec;\n vector<int> orig_w;\n \n std::mt19937 gen;\n std::uniform_int_distribution<> dis;\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(w);\n * int param_1 = obj->pickIndex();\n */",
"memory": "45800"
} |
912 | <p>You are given a <strong>0-indexed</strong> array of positive integers <code>w</code> where <code>w[i]</code> describes the <strong>weight</strong> of the <code>i<sup>th</sup></code> index.</p>
<p>You need to implement the function <code>pickIndex()</code>, which <strong>randomly</strong> picks an index in the range <code>[0, w.length - 1]</code> (<strong>inclusive</strong>) and returns it. The <strong>probability</strong> of picking an index <code>i</code> is <code>w[i] / sum(w)</code>.</p>
<ul>
<li>For example, if <code>w = [1, 3]</code>, the probability of picking index <code>0</code> is <code>1 / (1 + 3) = 0.25</code> (i.e., <code>25%</code>), and the probability of picking index <code>1</code> is <code>3 / (1 + 3) = 0.75</code> (i.e., <code>75%</code>).</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input</strong>
["Solution","pickIndex"]
[[[1]],[]]
<strong>Output</strong>
[null,0]
<strong>Explanation</strong>
Solution solution = new Solution([1]);
solution.pickIndex(); // return 0. The only option is to return 0 since there is only one element in w.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input</strong>
["Solution","pickIndex","pickIndex","pickIndex","pickIndex","pickIndex"]
[[[1,3]],[],[],[],[],[]]
<strong>Output</strong>
[null,1,1,1,1,0]
<strong>Explanation</strong>
Solution solution = new Solution([1, 3]);
solution.pickIndex(); // return 1. It is returning the second element (index = 1) that has a probability of 3/4.
solution.pickIndex(); // return 1
solution.pickIndex(); // return 1
solution.pickIndex(); // return 1
solution.pickIndex(); // return 0. It is returning the first element (index = 0) that has a probability of 1/4.
Since this is a randomization problem, multiple answers are allowed.
All of the following outputs can be considered correct:
[null,1,1,1,1,0]
[null,1,1,1,1,1]
[null,1,1,1,0,0]
[null,1,1,1,0,1]
[null,1,0,1,0,0]
......
and so on.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= w.length <= 10<sup>4</sup></code></li>
<li><code>1 <= w[i] <= 10<sup>5</sup></code></li>
<li><code>pickIndex</code> will be called at most <code>10<sup>4</sup></code> times.</li>
</ul>
| 3 | {
"code": "\n\n\nstruct AliasTable {\n\n struct Bin {\n float p, q;\n int alias;\n };\n\n vector<Bin> bins;\n\n AliasTable(vector<int>& w) {\n float totalWeight = accumulate(w.begin(), w.end(), 0.0f);\n bins.resize(w.size());\n\n for (int i = 0; i < w.size(); ++i) {\n bins[i].p = w[i] / totalWeight;\n }\n\n struct Outcome {\n float pHat;\n int index;\n };\n\n constexpr float PHatAvg = 1.0f;\n\n vector<Outcome> under, over;\n under.reserve(bins.size() / 2);\n over.reserve(bins.size() / 2);\n\n for (int i = 0; i < w.size(); ++i) {\n float pHat = bins[i].p * bins.size();\n if (pHat < PHatAvg) {\n under.push_back(Outcome{pHat, i});\n } else {\n over.push_back(Outcome{pHat, i});\n }\n }\n\n while (!under.empty() && !over.empty()) {\n Outcome un = under.back(); under.pop_back();\n Outcome ov = over.back(); over.pop_back();\n\n bins[un.index].q = un.pHat;\n bins[un.index].alias = ov.index;\n\n float pExcess = un.pHat + ov.pHat - 1;\n if (pExcess < PHatAvg) {\n under.push_back(Outcome{pExcess, ov.index});\n } else {\n over.push_back(Outcome{pExcess, ov.index});\n }\n }\n\n while (!over.empty()) {\n Outcome ov = over.back(); over.pop_back();\n bins[ov.index].q = 1;\n bins[ov.index].alias = -1;\n }\n\n while (!under.empty()) {\n Outcome un = under.back(); under.pop_back();\n bins[un.index].q = 1;\n bins[un.index].alias = -1;\n }\n }\n\n int sample(float u) {\n int offset = min<int>(u * bins.size(), bins.size() - 1);\n float up = min<float>(u * bins.size() - offset, 0.999999f);\n\n if (up < bins[offset].q) {\n return offset;\n } else {\n return bins[offset].alias;\n }\n }\n\n};\n\nclass Solution {\npublic:\n AliasTable table;\n\n Solution(vector<int>& w) : table(w) {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n }\n \n int pickIndex() {\n float u = static_cast<float>(rand()) / RAND_MAX;\n return table.sample(u);\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(w);\n * int param_1 = obj->pickIndex();\n */",
"memory": "45900"
} |
912 | <p>You are given a <strong>0-indexed</strong> array of positive integers <code>w</code> where <code>w[i]</code> describes the <strong>weight</strong> of the <code>i<sup>th</sup></code> index.</p>
<p>You need to implement the function <code>pickIndex()</code>, which <strong>randomly</strong> picks an index in the range <code>[0, w.length - 1]</code> (<strong>inclusive</strong>) and returns it. The <strong>probability</strong> of picking an index <code>i</code> is <code>w[i] / sum(w)</code>.</p>
<ul>
<li>For example, if <code>w = [1, 3]</code>, the probability of picking index <code>0</code> is <code>1 / (1 + 3) = 0.25</code> (i.e., <code>25%</code>), and the probability of picking index <code>1</code> is <code>3 / (1 + 3) = 0.75</code> (i.e., <code>75%</code>).</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input</strong>
["Solution","pickIndex"]
[[[1]],[]]
<strong>Output</strong>
[null,0]
<strong>Explanation</strong>
Solution solution = new Solution([1]);
solution.pickIndex(); // return 0. The only option is to return 0 since there is only one element in w.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input</strong>
["Solution","pickIndex","pickIndex","pickIndex","pickIndex","pickIndex"]
[[[1,3]],[],[],[],[],[]]
<strong>Output</strong>
[null,1,1,1,1,0]
<strong>Explanation</strong>
Solution solution = new Solution([1, 3]);
solution.pickIndex(); // return 1. It is returning the second element (index = 1) that has a probability of 3/4.
solution.pickIndex(); // return 1
solution.pickIndex(); // return 1
solution.pickIndex(); // return 1
solution.pickIndex(); // return 0. It is returning the first element (index = 0) that has a probability of 1/4.
Since this is a randomization problem, multiple answers are allowed.
All of the following outputs can be considered correct:
[null,1,1,1,1,0]
[null,1,1,1,1,1]
[null,1,1,1,0,0]
[null,1,1,1,0,1]
[null,1,0,1,0,0]
......
and so on.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= w.length <= 10<sup>4</sup></code></li>
<li><code>1 <= w[i] <= 10<sup>5</sup></code></li>
<li><code>pickIndex</code> will be called at most <code>10<sup>4</sup></code> times.</li>
</ul>
| 3 | {
"code": "\n\n\nstruct AliasTable {\n\n struct Bin {\n float p, q;\n int alias;\n };\n\n vector<Bin> bins;\n\n AliasTable(vector<int>& w) {\n float totalWeight = accumulate(w.begin(), w.end(), 0.0f);\n bins.resize(w.size());\n\n for (int i = 0; i < w.size(); ++i) {\n bins[i].p = w[i] / totalWeight;\n }\n\n struct Outcome {\n float pHat;\n int index;\n };\n\n constexpr float PHatAvg = 1.0f;\n vector<Outcome> under, over;\n\n for (int i = 0; i < w.size(); ++i) {\n float pHat = bins[i].p * bins.size();\n if (pHat < PHatAvg) {\n under.push_back(Outcome{pHat, i});\n } else {\n over.push_back(Outcome{pHat, i});\n }\n }\n\n while (!under.empty() && !over.empty()) {\n Outcome un = under.back(); under.pop_back();\n Outcome ov = over.back(); over.pop_back();\n\n bins[un.index].q = un.pHat;\n bins[un.index].alias = ov.index;\n\n float pExcess = un.pHat + ov.pHat - 1;\n if (pExcess < PHatAvg) {\n under.push_back(Outcome{pExcess, ov.index});\n } else {\n over.push_back(Outcome{pExcess, ov.index});\n }\n }\n\n while (!over.empty()) {\n Outcome ov = over.back(); over.pop_back();\n bins[ov.index].q = 1;\n bins[ov.index].alias = -1;\n }\n\n while (!under.empty()) {\n Outcome un = under.back(); under.pop_back();\n bins[un.index].q = 1;\n bins[un.index].alias = -1;\n }\n }\n\n int sample(float u) {\n int offset = min<int>(u * bins.size(), bins.size() - 1);\n float up = min<float>(u * bins.size() - offset, 0.999999f);\n\n if (up < bins[offset].q) {\n return offset;\n } else {\n return bins[offset].alias;\n }\n }\n\n};\n\nclass Solution {\npublic:\n\n AliasTable table;\n\n Solution(vector<int>& w) : table(w) {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n }\n \n int pickIndex() {\n float u = static_cast<float>(rand()) / RAND_MAX;\n return table.sample(u);\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(w);\n * int param_1 = obj->pickIndex();\n */",
"memory": "46000"
} |
912 | <p>You are given a <strong>0-indexed</strong> array of positive integers <code>w</code> where <code>w[i]</code> describes the <strong>weight</strong> of the <code>i<sup>th</sup></code> index.</p>
<p>You need to implement the function <code>pickIndex()</code>, which <strong>randomly</strong> picks an index in the range <code>[0, w.length - 1]</code> (<strong>inclusive</strong>) and returns it. The <strong>probability</strong> of picking an index <code>i</code> is <code>w[i] / sum(w)</code>.</p>
<ul>
<li>For example, if <code>w = [1, 3]</code>, the probability of picking index <code>0</code> is <code>1 / (1 + 3) = 0.25</code> (i.e., <code>25%</code>), and the probability of picking index <code>1</code> is <code>3 / (1 + 3) = 0.75</code> (i.e., <code>75%</code>).</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input</strong>
["Solution","pickIndex"]
[[[1]],[]]
<strong>Output</strong>
[null,0]
<strong>Explanation</strong>
Solution solution = new Solution([1]);
solution.pickIndex(); // return 0. The only option is to return 0 since there is only one element in w.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input</strong>
["Solution","pickIndex","pickIndex","pickIndex","pickIndex","pickIndex"]
[[[1,3]],[],[],[],[],[]]
<strong>Output</strong>
[null,1,1,1,1,0]
<strong>Explanation</strong>
Solution solution = new Solution([1, 3]);
solution.pickIndex(); // return 1. It is returning the second element (index = 1) that has a probability of 3/4.
solution.pickIndex(); // return 1
solution.pickIndex(); // return 1
solution.pickIndex(); // return 1
solution.pickIndex(); // return 0. It is returning the first element (index = 0) that has a probability of 1/4.
Since this is a randomization problem, multiple answers are allowed.
All of the following outputs can be considered correct:
[null,1,1,1,1,0]
[null,1,1,1,1,1]
[null,1,1,1,0,0]
[null,1,1,1,0,1]
[null,1,0,1,0,0]
......
and so on.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= w.length <= 10<sup>4</sup></code></li>
<li><code>1 <= w[i] <= 10<sup>5</sup></code></li>
<li><code>pickIndex</code> will be called at most <code>10<sup>4</sup></code> times.</li>
</ul>
| 3 | {
"code": "\n\n\nstruct AliasTable {\n\n struct Bin {\n float p, q;\n int alias;\n };\n\n vector<Bin> bins;\n\n AliasTable(vector<int>& w) {\n float totalWeight = accumulate(w.begin(), w.end(), 0.0f);\n bins.resize(w.size());\n\n for (int i = 0; i < w.size(); ++i) {\n bins[i].p = w[i] / totalWeight;\n }\n\n struct Outcome {\n float pHat;\n int index;\n };\n\n constexpr float PHatAvg = 1.0f;\n\n vector<Outcome> under, over;\n under.reserve(bins.size() / 2);\n over.reserve(bins.size() / 2);\n\n for (int i = 0; i < w.size(); ++i) {\n float pHat = bins[i].p * bins.size();\n if (pHat < PHatAvg) {\n under.push_back(Outcome{pHat, i});\n } else {\n over.push_back(Outcome{pHat, i});\n }\n }\n\n while (!under.empty() && !over.empty()) {\n Outcome un = under.back(); under.pop_back();\n Outcome ov = over.back(); over.pop_back();\n\n bins[un.index].q = un.pHat;\n bins[un.index].alias = ov.index;\n\n float pExcess = un.pHat + ov.pHat - 1;\n if (pExcess < PHatAvg) {\n under.push_back(Outcome{pExcess, ov.index});\n } else {\n over.push_back(Outcome{pExcess, ov.index});\n }\n }\n\n while (!over.empty()) {\n Outcome ov = over.back(); over.pop_back();\n bins[ov.index].q = 1;\n bins[ov.index].alias = -1;\n }\n\n while (!under.empty()) {\n Outcome un = under.back(); under.pop_back();\n bins[un.index].q = 1;\n bins[un.index].alias = -1;\n }\n }\n\n int sample(float u) {\n int offset = min<int>(u * bins.size(), bins.size() - 1);\n float up = min<float>(u * bins.size() - offset, 0.999999f);\n\n if (up < bins[offset].q) {\n return offset;\n } else {\n return bins[offset].alias;\n }\n }\n\n};\n\nclass Solution {\npublic:\n AliasTable table;\n\n Solution(vector<int>& w) : table(w) {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n }\n \n int pickIndex() {\n float u = static_cast<float>(rand()) / RAND_MAX;\n return table.sample(u);\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(w);\n * int param_1 = obj->pickIndex();\n */",
"memory": "46000"
} |
912 | <p>You are given a <strong>0-indexed</strong> array of positive integers <code>w</code> where <code>w[i]</code> describes the <strong>weight</strong> of the <code>i<sup>th</sup></code> index.</p>
<p>You need to implement the function <code>pickIndex()</code>, which <strong>randomly</strong> picks an index in the range <code>[0, w.length - 1]</code> (<strong>inclusive</strong>) and returns it. The <strong>probability</strong> of picking an index <code>i</code> is <code>w[i] / sum(w)</code>.</p>
<ul>
<li>For example, if <code>w = [1, 3]</code>, the probability of picking index <code>0</code> is <code>1 / (1 + 3) = 0.25</code> (i.e., <code>25%</code>), and the probability of picking index <code>1</code> is <code>3 / (1 + 3) = 0.75</code> (i.e., <code>75%</code>).</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input</strong>
["Solution","pickIndex"]
[[[1]],[]]
<strong>Output</strong>
[null,0]
<strong>Explanation</strong>
Solution solution = new Solution([1]);
solution.pickIndex(); // return 0. The only option is to return 0 since there is only one element in w.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input</strong>
["Solution","pickIndex","pickIndex","pickIndex","pickIndex","pickIndex"]
[[[1,3]],[],[],[],[],[]]
<strong>Output</strong>
[null,1,1,1,1,0]
<strong>Explanation</strong>
Solution solution = new Solution([1, 3]);
solution.pickIndex(); // return 1. It is returning the second element (index = 1) that has a probability of 3/4.
solution.pickIndex(); // return 1
solution.pickIndex(); // return 1
solution.pickIndex(); // return 1
solution.pickIndex(); // return 0. It is returning the first element (index = 0) that has a probability of 1/4.
Since this is a randomization problem, multiple answers are allowed.
All of the following outputs can be considered correct:
[null,1,1,1,1,0]
[null,1,1,1,1,1]
[null,1,1,1,0,0]
[null,1,1,1,0,1]
[null,1,0,1,0,0]
......
and so on.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= w.length <= 10<sup>4</sup></code></li>
<li><code>1 <= w[i] <= 10<sup>5</sup></code></li>
<li><code>pickIndex</code> will be called at most <code>10<sup>4</sup></code> times.</li>
</ul>
| 3 | {
"code": "\n\n\nstruct AliasTable {\n\n struct Bin {\n float p, q;\n int alias;\n };\n\n vector<Bin> bins;\n\n AliasTable(vector<int>& w) {\n float totalWeight = accumulate(w.begin(), w.end(), 0.0f);\n bins.resize(w.size());\n\n for (int i = 0; i < w.size(); ++i) {\n bins[i].p = w[i] / totalWeight;\n }\n\n struct Outcome {\n float pHat;\n int index;\n };\n\n constexpr float PHatAvg = 1.0f;\n vector<Outcome> under, over;\n\n for (int i = 0; i < w.size(); ++i) {\n float pHat = bins[i].p * bins.size();\n if (pHat < PHatAvg) {\n under.push_back(Outcome{pHat, i});\n } else {\n over.push_back(Outcome{pHat, i});\n }\n }\n\n while (!under.empty() && !over.empty()) {\n Outcome un = under.back(); under.pop_back();\n Outcome ov = over.back(); over.pop_back();\n\n bins[un.index].q = un.pHat;\n bins[un.index].alias = ov.index;\n\n float pExcess = un.pHat + ov.pHat - 1;\n if (pExcess < PHatAvg) {\n under.push_back(Outcome{pExcess, ov.index});\n } else {\n over.push_back(Outcome{pExcess, ov.index});\n }\n }\n\n while (!over.empty()) {\n Outcome ov = over.back(); over.pop_back();\n bins[ov.index].q = 1;\n bins[ov.index].alias = -1;\n }\n\n while (!under.empty()) {\n Outcome un = under.back(); under.pop_back();\n bins[un.index].q = 1;\n bins[un.index].alias = -1;\n }\n }\n\n int sample(float u) {\n int offset = min<int>(u * bins.size(), bins.size() - 1);\n float up = min<float>(u * bins.size() - offset, 0.999999f);\n\n if (up < bins[offset].q) {\n return offset;\n } else {\n return bins[offset].alias;\n }\n }\n\n};\n\nclass Solution {\npublic:\n\n AliasTable table;\n\n Solution(vector<int>& w) : table(w) {}\n \n int pickIndex() {\n float u = static_cast<float>(rand()) / RAND_MAX;\n return table.sample(u);\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(w);\n * int param_1 = obj->pickIndex();\n */",
"memory": "46100"
} |
912 | <p>You are given a <strong>0-indexed</strong> array of positive integers <code>w</code> where <code>w[i]</code> describes the <strong>weight</strong> of the <code>i<sup>th</sup></code> index.</p>
<p>You need to implement the function <code>pickIndex()</code>, which <strong>randomly</strong> picks an index in the range <code>[0, w.length - 1]</code> (<strong>inclusive</strong>) and returns it. The <strong>probability</strong> of picking an index <code>i</code> is <code>w[i] / sum(w)</code>.</p>
<ul>
<li>For example, if <code>w = [1, 3]</code>, the probability of picking index <code>0</code> is <code>1 / (1 + 3) = 0.25</code> (i.e., <code>25%</code>), and the probability of picking index <code>1</code> is <code>3 / (1 + 3) = 0.75</code> (i.e., <code>75%</code>).</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input</strong>
["Solution","pickIndex"]
[[[1]],[]]
<strong>Output</strong>
[null,0]
<strong>Explanation</strong>
Solution solution = new Solution([1]);
solution.pickIndex(); // return 0. The only option is to return 0 since there is only one element in w.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input</strong>
["Solution","pickIndex","pickIndex","pickIndex","pickIndex","pickIndex"]
[[[1,3]],[],[],[],[],[]]
<strong>Output</strong>
[null,1,1,1,1,0]
<strong>Explanation</strong>
Solution solution = new Solution([1, 3]);
solution.pickIndex(); // return 1. It is returning the second element (index = 1) that has a probability of 3/4.
solution.pickIndex(); // return 1
solution.pickIndex(); // return 1
solution.pickIndex(); // return 1
solution.pickIndex(); // return 0. It is returning the first element (index = 0) that has a probability of 1/4.
Since this is a randomization problem, multiple answers are allowed.
All of the following outputs can be considered correct:
[null,1,1,1,1,0]
[null,1,1,1,1,1]
[null,1,1,1,0,0]
[null,1,1,1,0,1]
[null,1,0,1,0,0]
......
and so on.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= w.length <= 10<sup>4</sup></code></li>
<li><code>1 <= w[i] <= 10<sup>5</sup></code></li>
<li><code>pickIndex</code> will be called at most <code>10<sup>4</sup></code> times.</li>
</ul>
| 3 | {
"code": "\n\n\nstruct AliasTable {\n\n struct Bin {\n float p, q;\n int alias;\n };\n\n vector<Bin> bins;\n\n AliasTable(vector<int>& w) {\n float totalWeight = accumulate(w.begin(), w.end(), 0.0f);\n bins.resize(w.size());\n\n for (int i = 0; i < w.size(); ++i) {\n bins[i].p = w[i] / totalWeight;\n }\n\n struct Outcome {\n float pHat;\n int index;\n };\n\n constexpr float PHatAvg = 1.0f;\n vector<Outcome> under, over;\n\n for (int i = 0; i < w.size(); ++i) {\n float pHat = bins[i].p * bins.size();\n if (pHat < PHatAvg) {\n under.push_back(Outcome{pHat, i});\n } else {\n over.push_back(Outcome{pHat, i});\n }\n }\n\n while (!under.empty() && !over.empty()) {\n Outcome un = under.back(); under.pop_back();\n Outcome ov = over.back(); over.pop_back();\n\n bins[un.index].q = un.pHat;\n bins[un.index].alias = ov.index;\n\n float pExcess = un.pHat + ov.pHat - 1;\n if (pExcess < PHatAvg) {\n under.push_back(Outcome{pExcess, ov.index});\n } else {\n over.push_back(Outcome{pExcess, ov.index});\n }\n }\n\n while (!over.empty()) {\n Outcome ov = over.back(); over.pop_back();\n bins[ov.index].q = 1;\n bins[ov.index].alias = -1;\n }\n\n while (!under.empty()) {\n Outcome un = under.back(); under.pop_back();\n bins[un.index].q = 1;\n bins[un.index].alias = -1;\n }\n }\n\n int sample(float u) {\n int offset = min<int>(u * bins.size(), bins.size() - 1);\n float up = min<float>(u * bins.size() - offset, 0.999999f);\n\n if (up < bins[offset].q) {\n return offset;\n } else {\n return bins[offset].alias;\n }\n }\n\n};\n\nclass Solution {\npublic:\n\n AliasTable table;\n\n Solution(vector<int>& w) : table(w) {}\n \n int pickIndex() {\n float u = static_cast<float>(rand()) / RAND_MAX;\n return table.sample(u);\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(w);\n * int param_1 = obj->pickIndex();\n */",
"memory": "46100"
} |
912 | <p>You are given a <strong>0-indexed</strong> array of positive integers <code>w</code> where <code>w[i]</code> describes the <strong>weight</strong> of the <code>i<sup>th</sup></code> index.</p>
<p>You need to implement the function <code>pickIndex()</code>, which <strong>randomly</strong> picks an index in the range <code>[0, w.length - 1]</code> (<strong>inclusive</strong>) and returns it. The <strong>probability</strong> of picking an index <code>i</code> is <code>w[i] / sum(w)</code>.</p>
<ul>
<li>For example, if <code>w = [1, 3]</code>, the probability of picking index <code>0</code> is <code>1 / (1 + 3) = 0.25</code> (i.e., <code>25%</code>), and the probability of picking index <code>1</code> is <code>3 / (1 + 3) = 0.75</code> (i.e., <code>75%</code>).</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input</strong>
["Solution","pickIndex"]
[[[1]],[]]
<strong>Output</strong>
[null,0]
<strong>Explanation</strong>
Solution solution = new Solution([1]);
solution.pickIndex(); // return 0. The only option is to return 0 since there is only one element in w.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input</strong>
["Solution","pickIndex","pickIndex","pickIndex","pickIndex","pickIndex"]
[[[1,3]],[],[],[],[],[]]
<strong>Output</strong>
[null,1,1,1,1,0]
<strong>Explanation</strong>
Solution solution = new Solution([1, 3]);
solution.pickIndex(); // return 1. It is returning the second element (index = 1) that has a probability of 3/4.
solution.pickIndex(); // return 1
solution.pickIndex(); // return 1
solution.pickIndex(); // return 1
solution.pickIndex(); // return 0. It is returning the first element (index = 0) that has a probability of 1/4.
Since this is a randomization problem, multiple answers are allowed.
All of the following outputs can be considered correct:
[null,1,1,1,1,0]
[null,1,1,1,1,1]
[null,1,1,1,0,0]
[null,1,1,1,0,1]
[null,1,0,1,0,0]
......
and so on.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= w.length <= 10<sup>4</sup></code></li>
<li><code>1 <= w[i] <= 10<sup>5</sup></code></li>
<li><code>pickIndex</code> will be called at most <code>10<sup>4</sup></code> times.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> expanded;\n Solution(vector<int>& w) {\n expanded.push_back(w[0]);\n for(int i = 1; i < w.size(); i++){\n expanded.push_back(expanded[i-1] + w[i]);\n }\n }\n \n int pickIndex() {\n std::random_device rd;\n std::mt19937 gen(rd());\n uniform_int_distribution<> distrib(1, expanded.back());\n int random = distrib(gen);\n int begin = 0, end = expanded.size()-1;\n while(begin <= end){\n int mid = begin + (end-begin)/2;\n int val = expanded[mid];\n if(random == val) return mid;\n if(random > val) {\n if(mid == end) return mid;\n else begin = mid + 1;\n }\n else if(random < val){\n if(mid == begin) return mid;\n else end = mid;\n }\n }\n return -1;\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(w);\n * int param_1 = obj->pickIndex();\n */",
"memory": "46500"
} |
912 | <p>You are given a <strong>0-indexed</strong> array of positive integers <code>w</code> where <code>w[i]</code> describes the <strong>weight</strong> of the <code>i<sup>th</sup></code> index.</p>
<p>You need to implement the function <code>pickIndex()</code>, which <strong>randomly</strong> picks an index in the range <code>[0, w.length - 1]</code> (<strong>inclusive</strong>) and returns it. The <strong>probability</strong> of picking an index <code>i</code> is <code>w[i] / sum(w)</code>.</p>
<ul>
<li>For example, if <code>w = [1, 3]</code>, the probability of picking index <code>0</code> is <code>1 / (1 + 3) = 0.25</code> (i.e., <code>25%</code>), and the probability of picking index <code>1</code> is <code>3 / (1 + 3) = 0.75</code> (i.e., <code>75%</code>).</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input</strong>
["Solution","pickIndex"]
[[[1]],[]]
<strong>Output</strong>
[null,0]
<strong>Explanation</strong>
Solution solution = new Solution([1]);
solution.pickIndex(); // return 0. The only option is to return 0 since there is only one element in w.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input</strong>
["Solution","pickIndex","pickIndex","pickIndex","pickIndex","pickIndex"]
[[[1,3]],[],[],[],[],[]]
<strong>Output</strong>
[null,1,1,1,1,0]
<strong>Explanation</strong>
Solution solution = new Solution([1, 3]);
solution.pickIndex(); // return 1. It is returning the second element (index = 1) that has a probability of 3/4.
solution.pickIndex(); // return 1
solution.pickIndex(); // return 1
solution.pickIndex(); // return 1
solution.pickIndex(); // return 0. It is returning the first element (index = 0) that has a probability of 1/4.
Since this is a randomization problem, multiple answers are allowed.
All of the following outputs can be considered correct:
[null,1,1,1,1,0]
[null,1,1,1,1,1]
[null,1,1,1,0,0]
[null,1,1,1,0,1]
[null,1,0,1,0,0]
......
and so on.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= w.length <= 10<sup>4</sup></code></li>
<li><code>1 <= w[i] <= 10<sup>5</sup></code></li>
<li><code>pickIndex</code> will be called at most <code>10<sup>4</sup></code> times.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> expanded;\n Solution(vector<int>& w) {\n expanded.push_back(w[0]);\n for(int i = 1; i < w.size(); i++){\n expanded.push_back(expanded[i-1] + w[i]);\n }\n }\n \n int pickIndex() {\n std::random_device rd;\n std::mt19937 gen(rd());\n uniform_int_distribution<> distrib(1, expanded.back());\n int random = distrib(gen);\n int begin = 0, end = expanded.size()-1;\n while(begin <= end){\n int mid = begin + (end-begin)/2;\n int val = expanded[mid];\n if(random == val) return mid;\n if(random > val) {\n if(mid == end) return mid;\n else begin = mid + 1;\n }\n else if(random < val){\n if(mid == begin || random > expanded[mid-1]) return mid;\n else end = mid - 1;\n }\n }\n return -1;\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(w);\n * int param_1 = obj->pickIndex();\n */",
"memory": "46600"
} |
912 | <p>You are given a <strong>0-indexed</strong> array of positive integers <code>w</code> where <code>w[i]</code> describes the <strong>weight</strong> of the <code>i<sup>th</sup></code> index.</p>
<p>You need to implement the function <code>pickIndex()</code>, which <strong>randomly</strong> picks an index in the range <code>[0, w.length - 1]</code> (<strong>inclusive</strong>) and returns it. The <strong>probability</strong> of picking an index <code>i</code> is <code>w[i] / sum(w)</code>.</p>
<ul>
<li>For example, if <code>w = [1, 3]</code>, the probability of picking index <code>0</code> is <code>1 / (1 + 3) = 0.25</code> (i.e., <code>25%</code>), and the probability of picking index <code>1</code> is <code>3 / (1 + 3) = 0.75</code> (i.e., <code>75%</code>).</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input</strong>
["Solution","pickIndex"]
[[[1]],[]]
<strong>Output</strong>
[null,0]
<strong>Explanation</strong>
Solution solution = new Solution([1]);
solution.pickIndex(); // return 0. The only option is to return 0 since there is only one element in w.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input</strong>
["Solution","pickIndex","pickIndex","pickIndex","pickIndex","pickIndex"]
[[[1,3]],[],[],[],[],[]]
<strong>Output</strong>
[null,1,1,1,1,0]
<strong>Explanation</strong>
Solution solution = new Solution([1, 3]);
solution.pickIndex(); // return 1. It is returning the second element (index = 1) that has a probability of 3/4.
solution.pickIndex(); // return 1
solution.pickIndex(); // return 1
solution.pickIndex(); // return 1
solution.pickIndex(); // return 0. It is returning the first element (index = 0) that has a probability of 1/4.
Since this is a randomization problem, multiple answers are allowed.
All of the following outputs can be considered correct:
[null,1,1,1,1,0]
[null,1,1,1,1,1]
[null,1,1,1,0,0]
[null,1,1,1,0,1]
[null,1,0,1,0,0]
......
and so on.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= w.length <= 10<sup>4</sup></code></li>
<li><code>1 <= w[i] <= 10<sup>5</sup></code></li>
<li><code>pickIndex</code> will be called at most <code>10<sup>4</sup></code> times.</li>
</ul>
| 3 | {
"code": "// #include <random>\nclass Solution {\n vector<pair<double, int>> cumprob;\n std::mt19937 rng; // Random number generator\n std::uniform_real_distribution<double> dist;\npublic:\n\n Solution(vector<int>& w) : dist(0.0, 1.0) {\n int sum=0;\n for(auto&e:w)\n sum+=e;\n double c=0.;\n for(int i=0;i<w.size();i++)\n {\n c+=w[i]*1./(sum)*1.;\n cumprob.push_back({c, i});\n }\n rng.seed(static_cast<unsigned>(std::time(0)));\n }\n \n int pickIndex() {\n double r= dist(rng);\n for(auto&e:cumprob)\n if(r<e.first)\n return e.second;\n return cumprob[cumprob.size()-1].second;\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(w);\n * int param_1 = obj->pickIndex();\n */",
"memory": "46600"
} |
912 | <p>You are given a <strong>0-indexed</strong> array of positive integers <code>w</code> where <code>w[i]</code> describes the <strong>weight</strong> of the <code>i<sup>th</sup></code> index.</p>
<p>You need to implement the function <code>pickIndex()</code>, which <strong>randomly</strong> picks an index in the range <code>[0, w.length - 1]</code> (<strong>inclusive</strong>) and returns it. The <strong>probability</strong> of picking an index <code>i</code> is <code>w[i] / sum(w)</code>.</p>
<ul>
<li>For example, if <code>w = [1, 3]</code>, the probability of picking index <code>0</code> is <code>1 / (1 + 3) = 0.25</code> (i.e., <code>25%</code>), and the probability of picking index <code>1</code> is <code>3 / (1 + 3) = 0.75</code> (i.e., <code>75%</code>).</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input</strong>
["Solution","pickIndex"]
[[[1]],[]]
<strong>Output</strong>
[null,0]
<strong>Explanation</strong>
Solution solution = new Solution([1]);
solution.pickIndex(); // return 0. The only option is to return 0 since there is only one element in w.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input</strong>
["Solution","pickIndex","pickIndex","pickIndex","pickIndex","pickIndex"]
[[[1,3]],[],[],[],[],[]]
<strong>Output</strong>
[null,1,1,1,1,0]
<strong>Explanation</strong>
Solution solution = new Solution([1, 3]);
solution.pickIndex(); // return 1. It is returning the second element (index = 1) that has a probability of 3/4.
solution.pickIndex(); // return 1
solution.pickIndex(); // return 1
solution.pickIndex(); // return 1
solution.pickIndex(); // return 0. It is returning the first element (index = 0) that has a probability of 1/4.
Since this is a randomization problem, multiple answers are allowed.
All of the following outputs can be considered correct:
[null,1,1,1,1,0]
[null,1,1,1,1,1]
[null,1,1,1,0,0]
[null,1,1,1,0,1]
[null,1,0,1,0,0]
......
and so on.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= w.length <= 10<sup>4</sup></code></li>
<li><code>1 <= w[i] <= 10<sup>5</sup></code></li>
<li><code>pickIndex</code> will be called at most <code>10<sup>4</sup></code> times.</li>
</ul>
| 3 | {
"code": "#include <vector>\n#include <utility>\n#include <random>\n#include <ctime>\n#include <algorithm> // For std::lower_bound\n\nclass Solution {\n std::vector<std::pair<double, int>> cumprob;\n std::mt19937 rng; // Random number generator\n std::uniform_real_distribution<double> dist; // Uniform distribution\n\npublic:\n Solution(std::vector<int>& w) : dist(0.0, 1.0) {\n int sum = 0;\n for (auto& e : w)\n sum += e;\n\n double cumulative = 0.0;\n for (int i = 0; i < w.size(); ++i) {\n cumulative += w[i] * 1.0 / sum; // Update cumulative probability\n cumprob.push_back({cumulative, i});\n }\n\n rng.seed(static_cast<unsigned>(std::time(0))); // Seed RNG with current time\n }\n \n int pickIndex() {\n double r = dist(rng); // Generate a random number\n // Use binary search to find the right index\n auto it = std::lower_bound(cumprob.begin(), cumprob.end(), std::make_pair(r, -1.0),\n [](const std::pair<double, int>& a, const std::pair<double, int>& b) {\n return a.first < b.first;\n });\n return it->second;\n }\n};\n",
"memory": "46700"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n \n vector<vector<int>> result;\n vector<int> temp;\n dfs(nums, result, temp, 0);\n return result;\n }\n\nprivate:\n void dfs(vector<int>& nums, vector<vector<int>>& result, vector<int>& temp, int index)\n {\n if (temp.size() > 1)\n {\n if (find(result.begin(), result.end(), temp) == result.end())\n result.push_back(temp);\n }\n\n for (int i = index; i < nums.size(); ++i)\n {\n int back = temp.empty() ? -101 : temp.back();\n\n if (nums[i] >= back)\n {\n temp.push_back(nums[i]);\n dfs(nums, result, temp, i + 1);\n temp.pop_back();\n }\n } \n }\n};",
"memory": "18270"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n // Function to find all the increasing subsequences in the given vector.\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vector<vector<int>> subsequences;\n vector<int> currentSubsequence;\n backtrack(0, INT_MIN, nums, currentSubsequence, subsequences); // Assuming -1000 is a lower bound, we use INT_MIN\n return subsequences;\n }\n\nprivate:\n // Uses backtracking to find all subsequences.\n // u is the current index in nums.\n // last is the last number added to the current subsequence.\n // nums is the input array of numbers.\n // currentSubsequence holds the current subsequence being explored.\n // subsequences is the collection of all valid subsequences found.\n void backtrack(int index, int lastNumber, vector<int>& nums, vector<int>& currentSubsequence, vector<vector<int>>& subsequences) {\n if (index == nums.size()) { // Base case: reached the end of nums\n if (currentSubsequence.size() > 1) { // If the subsequence has more than 1 element, add it to the answer.\n subsequences.push_back(currentSubsequence);\n }\n return;\n }\n // If the current number can be added to the subsequence according to the problem definition (non-decreasing order)\n if (nums[index] >= lastNumber) {\n currentSubsequence.push_back(nums[index]); // Add number to the current subsequence.\n backtrack(index + 1, nums[index], nums, currentSubsequence, subsequences); // Recursively call with next index.\n currentSubsequence.pop_back(); // Backtrack: remove the number from current subsequence.\n }\n // If current number is not equal to the last number added to the subsequence, continue to next index.\n // This avoids duplicates in the subsequences list.\n if (nums[index] != lastNumber) { \n backtrack(index + 1, lastNumber, nums, currentSubsequence, subsequences); // Recursively call with next index.\n }\n }\n};",
"memory": "18270"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vector<int> curr;\n vector<vector<int>> res;\n backtrack(0, curr, nums, res);\n return res;\n }\nprivate:\n void backtrack(int start, vector<int>& curr, vector<int>& nums, vector<vector<int>>& res){\n if (curr.size() >= 2)\n res.push_back(curr);\n if (start == nums.size())\n return;\n bool seen[201] = {};\n for (int i = start; i < nums.size(); i++){\n if (seen[nums[i] + 100])\n continue;\n else if (curr.empty() || nums[i] >= curr.back()){\n curr.push_back(nums[i]);\n backtrack(i + 1, curr, nums, res);\n curr.pop_back();\n seen[nums[i] + 100] = true;\n }\n }\n }\n};",
"memory": "19411"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> ans;\n void dfs(int idx, vector<int> &nums, vector<int> &cur) {\n int n = nums.size();\n if (idx >= n) {\n if (cur.size() >= 2) ans.push_back(cur);\n return;\n }\n if (cur.size() == 0 || cur.back() <= nums[idx]) {\n cur.push_back(nums[idx]);\n dfs(idx + 1, nums, cur);\n cur.pop_back();\n }\n if (cur.size() == 0 || nums[idx] != cur.back()) dfs(idx + 1, nums, cur);\n }\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vector<int> t;\n dfs(0, nums, t);\n return ans;\n }\n};",
"memory": "19411"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> ans;\n void dfs(int idx, vector<int> &nums, vector<int> &cur) {\n int n = nums.size();\n if (idx >= n) {\n if (cur.size() >= 2) ans.push_back(cur);\n return;\n }\n if (cur.size() == 0 || cur.back() <= nums[idx]) {\n cur.push_back(nums[idx]);\n dfs(idx + 1, nums, cur);\n cur.pop_back();\n }\n if (cur.size() == 0 || nums[idx] != cur.back()) dfs(idx + 1, nums, cur);\n }\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vector<int> t;\n dfs(0, nums, t);\n return ans;\n }\n};",
"memory": "20553"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> ans;\n void dfs(int idx, vector<int> &nums, vector<int> &cur) {\n int n = nums.size();\n if (idx >= n) {\n if (cur.size() >= 2) ans.push_back(cur);\n return;\n }\n if (cur.size() == 0 || cur.back() <= nums[idx]) {\n cur.push_back(nums[idx]);\n dfs(idx + 1, nums, cur);\n cur.pop_back();\n }\n if (cur.size() == 0 || nums[idx] != cur.back()) dfs(idx + 1, nums, cur);\n }\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vector<int> t;\n dfs(0, nums, t);\n return ans;\n }\n};",
"memory": "20553"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> result;\n vector<int> subset;\n void backtracking(vector<int>& nums, int numsindex){\n if(subset.size()>1) result.push_back(subset);\n for(int i=numsindex; i<nums.size(); i++){\n if(i>numsindex && find(nums.begin()+numsindex, nums.begin()+i, nums[i])!=nums.begin()+i) continue;\n if(subset.size()==0){\n subset.push_back(nums[i]);\n backtracking(nums, i+1);\n subset.pop_back();\n }\n if(subset.size()>0 && nums[i]>=*(subset.end()-1)){\n subset.push_back(nums[i]);\n backtracking(nums, i+1);\n subset.pop_back();\n }\n \n }\n }\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n backtracking(nums, 0);\n return result;\n }\n};",
"memory": "21694"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> result;\n vector<int> subset;\n void backtracking(vector<int>& nums, int numsindex){\n if(subset.size()>1) result.push_back(subset);\n for(int i=numsindex; i<nums.size(); i++){\n if(i>numsindex && find(nums.begin()+numsindex, nums.begin()+i, nums[i])!=nums.begin()+i) continue;\n if(subset.size()==0 || (subset.size()>0 && nums[i]>=*(subset.end()-1))){\n subset.push_back(nums[i]);\n backtracking(nums, i+1);\n subset.pop_back();\n }\n }\n }\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n backtracking(nums, 0);\n return result;\n }\n};",
"memory": "21694"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n dfs(0, INT_MIN, nums);\n return ans;\n }\n\nprivate:\n vector<vector<int>> ans;\n vector<int> temp;\n\n void dfs(int cur, int last, vector<int>& nums) {\n if (cur == nums.size()) {\n if (temp.size() >= 2) {\n ans.push_back(vector<int>(temp));\n }\n return;\n }\n if (nums[cur] >= last) {\n temp.push_back(nums[cur]);\n dfs(cur + 1, nums[cur], nums);\n temp.pop_back();\n }\n if (nums[cur] != last) {\n dfs(cur + 1, last, nums);\n }\n }\n};",
"memory": "22835"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n void solve(vector<int> &nums, vector<int> &temp, vector<vector<int>> &ans, int idx) {\n if (temp.size() > 1) {\n ans.push_back(temp);\n }\n\n set<int> used; // Local set to avoid duplicates at this level\n\n for (int i = idx; i < nums.size(); ++i) {\n if ((temp.empty() || temp.back() <= nums[i]) && used.find(nums[i]) == used.end()) {\n used.insert(nums[i]); // Mark this element as used in this level\n temp.push_back(nums[i]);\n solve(nums, temp, ans, i + 1);\n temp.pop_back(); // Backtrack\n }\n }\n }\n\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vector<int> temp;\n vector<vector<int>> ans;\n solve(nums, temp, ans, 0);\n return ans;\n }\n};\n",
"memory": "22835"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 0 | {
"code": "class Solution {\nprivate:\n void getSequences(int index,vector<int>& inter,vector<vector<int>>& ans,vector<int>& nums){\n if(inter.size()>=2 && index!=nums.size()){\n ans.push_back(inter);\n }\n if(index==nums.size()){\n if(inter.size()>=2){\n ans.push_back(inter);\n }\n return ;\n }\n map<int,int>mpp;\n for(int i=index;i<nums.size();i++){\n \n if(mpp.find(nums[i])!=mpp.end())continue;\n\n if((inter.size()==0 || nums[i] >= inter.back() ) ){\n inter.push_back(nums[i]);\n mpp[nums[i]]=1;\n getSequences(i+1,inter,ans,nums);\n inter.pop_back();\n }\n }\n }\npublic:\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vector<vector<int>>ans;\n vector<int>inter;\n int n=nums.size();\n getSequences(0,inter,ans,nums);\n return ans;\n }\n};",
"memory": "23976"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int n;\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n n = nums.size();\n if(n == 1) return {};\n vector<vector<int>> result;\n vector<int> path;\n backtrack(result, path, nums, 0);\n return result;\n\n }\n\n void backtrack(vector<vector<int>>& result, vector<int>& path, vector<int>& nums, int idx) {\n if(path.size() >= 2) {\n result.push_back(path);\n }\n map<int, bool> mp;\n for(int i = idx; i<n; ++i) {\n if(mp[nums[i]]) continue;\n else if(path.empty() || nums[i] >= path.back()) {\n path.push_back(nums[i]);\n mp[nums[i]] = true;\n backtrack(result, path, nums, i+1);\n path.pop_back();\n }\n }\n }\n};",
"memory": "23976"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n set<vector<int>> result;\n vector<int> subsequence;\n recursiveSubsequences(nums, 0, subsequence, result);\n return vector<vector<int>>(result.begin(), result.end());\n }\n \nprivate:\n void recursiveSubsequences(vector<int>& nums, int index, vector<int>& subsequence, set<vector<int>>& result) {\n if (subsequence.size() >= 2) {\n result.insert(subsequence);\n }\n for (int i = index; i < nums.size(); ++i) {\n if (subsequence.empty() || nums[i] >= subsequence.back()) {\n subsequence.push_back(nums[i]);\n recursiveSubsequences(nums, i + 1, subsequence, result);\n subsequence.pop_back();\n }\n }\n }\n};",
"memory": "25118"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n void fun(int i, vector<int> &nums, vector<int> &ans,set<vector<int>> &set){\n if(i==nums.size()){\n if(ans.size()>1) set.insert(ans);\n return;\n }\n if(ans.empty() || ans.back()<=nums[i]){\n ans.push_back(nums[i]);\n int j = i+1;\n fun(j,nums,ans,set);\n ans.pop_back();\n }\n int j = i+1;\n while(j<nums.size()&&nums[j]==nums[i]) j++;\n fun(j,nums,ans,set);\n }\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vector<int> ans;\n set<vector<int>> set;\n fun(0,nums,ans,set);\n return vector<vector<int>>(set.begin(),set.end());\n }\n};",
"memory": "25118"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 0 | {
"code": "using vi = vector<int>;\nusing vll = vector<long long>;\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pq = priority_queue<int>;\nusing mpq = priority_queue<int, vector<int>, greater<>>;\n#define ump unordered_map\n#define FOR(i, to) for (int i = 0; i < (to); ++i)\n#define rep(i, a, b) for (int i = a; i < b; ++i)\n#define repr(i, a, b) for (int i = a-1; i >= b; --i)\n#define pb push_back\n#define trav(a, x) for (auto &a : x)\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\nconstexpr int INF = 100000000;\nconstexpr int MOD = 100000007;\nconstexpr char en = '\\n';\nconst vector<pii> dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};\n\nclass Solution {\npublic:\n void recurse(vector<vi>& res, vi& nums, vi& curr, int index) {\n if(curr.size() > 1) {\n res.pb(curr);\n }\n unordered_set<int> visited;\n for(int i = index; i < nums.size(); ++i) {\n if( (curr.empty()||nums[i] >= curr.back()) && !visited.count(nums[i]) ) {\n curr.pb(nums[i]);\n visited.insert(nums[i]);\n recurse(res, nums, curr, i+1);\n curr.pop_back();\n }\n }\n }\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vector<vi> res;\n vi curr;\n recurse(res, nums, curr, 0);\n return res;\n }\n};",
"memory": "26259"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int n;\n void find(vector<int>& nums,vector<vector<int>>& result, int idx, vector<int>& curr){\n if(curr.size()>1){\n result.push_back(curr);\n \n }\n unordered_set<int>st;\n for(int i=idx;i<n;i++){\n if((curr.empty()|| nums[i]>=curr.back()) && st.find(nums[i])==st.end()){\n curr.push_back(nums[i]);\n find(nums,result,i+1,curr);\n curr.pop_back();\n st.insert(nums[i]);\n }\n }\n }\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n n=nums.size();\n vector<vector<int>>result;\n vector<int>curr;\n find(nums,result,0,curr);\n return result;\n \n }\n};",
"memory": "26259"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n map<vector<int>,int>mp;\n void solve(vector<vector<int>>&res,vector<int>&nums,int x,vector<int>&temp){\n if(temp.size()>=2)\n { \n mp[temp]++;\n if(mp[temp]>1)\n return;\n res.push_back(temp);\n \n }\n if(x>nums.size())\n {\n return ;\n }\n \n\n for(int i=x;i<nums.size();i++){\n if(temp.size()>=1)\n {if(nums[i]<temp[temp.size()-1])\n continue;}\n \n temp.push_back(nums[i]);\n solve(res,nums,i+1,temp);\n temp.pop_back();//backtracking the changes we made\n \n }\n return;\n }\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vector<vector<int>>result;\n vector<int>temp;\n solve(result,nums,0,temp);\n \n return result;\n }\n};",
"memory": "27400"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n map<vector<int>,int>mp;\n void solve(vector<vector<int>>&res,vector<int>&nums,int x,vector<int>&temp){\n if(temp.size()>=2)\n { \n mp[temp]++;\n if(mp[temp]>1)\n return;\n res.push_back(temp);\n \n }\n if(x>nums.size())\n {\n return ;\n }\n \n\n for(int i=x;i<nums.size();i++){\n if(temp.size()>=1)\n {if(nums[i]<temp[temp.size()-1])\n continue;}\n \n temp.push_back(nums[i]);\n solve(res,nums,i+1,temp);\n temp.pop_back();//backtracking the changes we made\n \n }\n return;\n }\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vector<vector<int>>result;\n vector<int>temp;\n solve(result,nums,0,temp);\n \n return result;\n }\n};",
"memory": "27400"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n set<vector<int>>ans;\n vector<int>level;\n void go(int idx,int prev,vector<int>&nums)\n {\n if(idx==nums.size())\n {\n if(level.size()>1)\n \n ans.insert(level);\n return;\n \n }\n if(prev==-1 || nums[idx]>=nums[prev])\n {\n level.push_back(nums[idx]);\n go(idx+1,idx,nums);\n level.pop_back();\n }\n go(idx+1,prev,nums);\n }\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n go(0,-1,nums);\n vector<vector<int>>finalans;\n for(auto it:ans)\n {\n finalans.push_back(it);\n }\n return finalans;\n }\n};",
"memory": "28541"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n set<vector<int>>ans;\n vector<int>level;\n void go(int idx,int prev,vector<int>&nums)\n {\n if(idx==nums.size())\n {\n if(level.size()>1)\n \n ans.insert(level);\n return;\n \n }\n if(prev==-1 || nums[idx]>=nums[prev])\n {\n level.push_back(nums[idx]);\n go(idx+1,idx,nums);\n level.pop_back();\n }\n go(idx+1,prev,nums);\n }\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n go(0,-1,nums);\n vector<vector<int>>finalans;\n for(auto it:ans)\n {\n finalans.push_back(it);\n }\n return finalans;\n }\n};",
"memory": "28541"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n set<vector<int>> ans;\n void recur(vector<int>& nums, int ind, int n, vector<int> v) {\n if(ind >= n) return;\n\n for(int i = ind; i < n; i++) {\n // if(i > 0 && nums[i] == nums[i-1]) continue;\n int p = v.size();\n if(p > 0 && v[p-1] > nums[i]) continue;\n\n v.push_back(nums[i]);\n recur(nums, i + 1, n, v);\n if(v.size() > 1) ans.insert(v);\n v.pop_back();\n }\n }\n\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n ans.clear();\n vector<int> curr;\n vector<vector<int>> subSeqs;\n recur(nums, 0, nums.size(), curr);\n\n auto it = ans.begin();\n while(it != ans.end()) {\n subSeqs.push_back(*it);\n it++;\n }\n return subSeqs;\n }\n};",
"memory": "34248"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n set<vector<int>> ans;\n void recur(vector<int>& nums, int ind, int n, vector<int> v) {\n if(ind >= n) return;\n\n for(int i = ind; i < n; i++) {\n // if(i > 0 && nums[i] == nums[i-1]) continue;\n int p = v.size();\n if(p > 0 && v[p-1] > nums[i]) continue;\n\n v.push_back(nums[i]);\n recur(nums, i + 1, n, v);\n if(v.size() > 1) {\n ans.insert(v);\n for(auto u : v) cout << u << \" \";\n cout << endl;\n }\n v.pop_back();\n }\n return;\n }\n\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n ans.clear();\n vector<int> curr;\n vector<vector<int>> subSeqs;\n recur(nums, 0, nums.size(), curr);\n\n auto it = ans.begin();\n while(it != ans.end()) {\n subSeqs.push_back(*it);\n it++;\n }\n return subSeqs;\n }\n};",
"memory": "34248"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n set<vector<int>> st{{}};\n for(const auto &num:nums) {\n vector<vector<int>> tmp(st.size());\n copy(st.begin(), st.end(), tmp.begin());\n\n for(auto v:tmp) {\n if(v.empty()||v.back()<=num) {\n v.push_back(num);\n st.insert(v);\n }\n }\n }\n vector<vector<int>> res;\n for(const auto &v:st) {\n if(v.size()>1) res.push_back(v);\n }\n return res;\n }\n};",
"memory": "35389"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n int size = nums.size();\n vector<vector<int>> table;\n vector<vector<int>> ans;\n for(int i=0; i<size; i+=1){\n int len = table.size();\n for(int j=len-1; j>=0; j-=1){\n if(table[j].back()<=nums[i]){\n table.push_back(table[j]);\n table.back().push_back(nums[i]);\n }\n }\n table.emplace_back(1, nums[i]);\n }\n\n vector<int> order(table.size());\n for(int i=0; i<table.size(); i+=1){\n order[i] = i;\n }\n sort(order.begin(), order.end(), [&table](int a, int b){\n return table[a]<table[b];\n });\n for(int i=0; i<order.size(); i+=1){\n if(table[order[i]].size()==1){\n continue;\n }\n if(i>0 && table[order[i]]==table[order[i-1]]){\n continue;\n }\n ans.push_back(table[order[i]]);\n }\n\n return ans;\n }\n};",
"memory": "35389"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n void solve(int ind,vector<int> nums,vector<int> &temp,set<vector<int>> &ans){\n if(temp.size()>=2){\n ans.insert(temp);\n }\n if(ind>=nums.size()) \n return;\n for(int i=ind;i<nums.size();i++){\n if(temp.empty() || nums[i]>=temp.back())\n {\n temp.push_back(nums[i]);\n solve(i+1,nums,temp,ans);\n temp.pop_back();\n }\n }\n }\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n set<vector<int>> ans;\n vector<vector<int>> res;\n vector<int> temp;\n solve(0,nums,temp,ans);\n for(vector<int> t: ans){\n res.push_back(t);\n }\n return res;\n }\n};",
"memory": "36530"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\nprivate:\n vector <vector <int>> ans;\n int n;\npublic:\n void helper(vector <int> &nums,int last,int begin,vector <int> &help){\n if(begin >= n){\n if(help.size() > 1){ans.push_back(help);}\n return ;\n }\n helper(nums,last,begin+1,help);\n if(nums[begin] >= last){\n help.push_back(nums[begin]);\n helper(nums,nums[begin],begin+1,help);\n help.pop_back();\n }\n }\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n n = nums.size();\n vector <int> help;\n helper(nums,-200,0,help);\n sort(ans.begin(),ans.end());\n auto last = unique(ans.begin(),ans.end());\n ans.erase(last,ans.end());\n return ans;\n }\n};\nauto init = [](){\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();",
"memory": "36530"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n map<int,vector<vector<int>>>m;\n map<pair<int,int>,int>v;\n for(int i=1;i<nums.size();i++)\n {\n int j=i-1;\n while(j>=0)\n {\n if(nums[i]>=nums[j])\n {\n if(v.find({nums[j],nums[i]})==v.end())\n m[i].push_back({nums[j],nums[i]});\n v[{nums[j],nums[i]}]++;\n for(auto it:m[j])\n {\n vector<int>y=it;\n y.push_back(nums[i]);\n m[i].push_back(y);\n }\n }\n if(nums[i]==nums[j])\n break;\n j--;\n }\n }\n vector<vector<int>>ans;\n \n for(auto it:m)\n {\n for(auto x:it.second)\n {\n ans.push_back(x);\n }\n }\n return ans;\n }\n};",
"memory": "37671"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<vector<int>> ans;\n set<vector<int>> st;\n void backtrack(vector<int>& nums, int i, vector<int> temp) {\n if (temp.size() >= 2) {\n if (st.find(temp) == st.end()) {\n ans.push_back(temp);\n st.insert(temp);\n }\n }\n if (i == nums.size()) {\n return;\n }\n for (int j = i; j < nums.size(); j++) {\n if (!temp.empty() && nums[j] < temp.back()) {\n continue;\n }\n temp.push_back(nums[j]);\n backtrack(nums, j + 1, temp);\n temp.pop_back();\n }\n }\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n backtrack(nums, 0, {});\n return ans;\n }\n};",
"memory": "37671"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<vector<int>>ans;\n map<vector<int>,int>mp;\n vector<int>cat;\n void BackTrack(int i , int n, vector<int>v)\n {\n if(cat.size()>=2)\n {\n mp[cat]++; \n if(mp[cat]<=1) ans.push_back(cat);\n }\n for(int k = i; k<n; k++)\n {\n if(!cat.empty() and cat.back()>v[k]) continue;\n cat.push_back(v[k]);\n BackTrack(k+1,n,v);\n cat.pop_back();\n }\n }\n vector<vector<int>> findSubsequences(vector<int>& nums) \n {\n int n = nums.size();\n BackTrack(0,n,nums);\n return ans; \n }\n};",
"memory": "38813"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\n vector<vector<int>>ans;set<vector<int>>st;\n\npublic: \n\n vector<vector<int>> findSubsequences(vector<int>& nums) { \nsolve(nums,0,{});\nfor(auto i:st){ans.push_back(i);}\n return ans; \n }\n void solve(vector<int>&nums,int i,vector<int>temp){\n for(int j=i;j<nums.size();j++){\n if(j>0 && temp.size() && temp.back()>nums[j]){continue;}\n temp.push_back(nums[j]);\n if(temp.size()>=2){ st.insert(temp);}\n solve(nums,j+1,temp);\n temp.pop_back();\n }\n }\n};",
"memory": "38813"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\n vector<vector<int>>ans;set<vector<int>>st;\n\npublic: \n\n vector<vector<int>> findSubsequences(vector<int>& nums) { \nsolve(nums,0,{});\nfor(auto i:st){ans.push_back(i);}\n return ans; \n }\n void solve(vector<int>&nums,int i,vector<int>temp){\n for(int j=i;j<nums.size();j++){\n if(j>0 && temp.size() && temp.back()>nums[j]){continue;}\n temp.push_back(nums[j]);\n if(temp.size()>=2){ st.insert(temp);}\n solve(nums,j+1,temp);\n temp.pop_back();\n }\n }\n};",
"memory": "39954"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\nvector<vector<int>>vec;\nset<vector<int>>st;\n vector<vector<int>> findSubsequences(vector<int>& nums){\n vector<int>curr;\n for(int i=0;i<nums.size()-1;i++){\n curr.push_back(nums[i]);\n nondecrease(i+1,curr,nums);\n curr.pop_back();\n }\n for(auto x : st) vec.push_back(x);\n return vec;\n } \n void nondecrease(int i,vector<int>curr,vector<int>&nums){ \n if(i==nums.size()){\n return;\n }\n for(int j=i;j<nums.size();j++){\n if(nums[j]>=curr.back()){\n curr.push_back(nums[j]);\n nondecrease(j+1,curr,nums);\n st.insert(curr);\n curr.pop_back();\n } \n }\n } \n};",
"memory": "39954"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n void solve(vector<int>&nums,int index,set<vector<int>>&result,vector<int>&output){\n if(index>=nums.size()){\n if(output.size()>1){\n result.insert(output);\n }\n return;\n }\n solve(nums,index+1,result,output);\n if(output.empty() || nums[index]>=output.back()){\n output.push_back(nums[index]);\n solve(nums,index+1,result,output);\n output.pop_back();\n }\n }\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n set<vector<int>>result;\n vector<int>output;\n solve(nums,0,result,output);\n vector<vector<int>>ans(result.begin(),result.end());\n return ans;\n }\n};",
"memory": "41095"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 2 | {
"code": "typedef vector<vector<int>> vvi;\ntypedef vector<int> vi;\n\nclass Solution {\nprivate:\n void subSeqGenerator(int i, vi& temp, vi& nums, set<vi>& answer) {\n if(i==nums.size()){\n if(temp.size() >= 2) answer.insert(temp);\n return;\n }\n // Don't Take This\n subSeqGenerator(i+1, temp, nums, answer);\n // take this\n if(temp.size()==0 || temp.back() <= nums[i]){\n temp.push_back(nums[i]);\n subSeqGenerator(i+1, temp, nums, answer);\n temp.pop_back();\n }\n }\n\npublic:\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vi temp;\n set<vi> answer;\n subSeqGenerator(0, temp, nums, answer);\n vvi ans(answer.begin(), answer.end());\n return ans;\n }\n};",
"memory": "41095"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n set<vector<int>> ans;\n vector<int> nums;\n\n void solve(int i, vector<int> &temp){\n\n if(i==nums.size()){\n if(temp.size()>=2){\n ans.insert(temp);\n }\n return;\n }\n\n solve(i+1, temp);\n\n if(temp.size()==0 || nums[i]>=temp.back()){\n temp.push_back(nums[i]);\n solve(i+1, temp);\n temp.pop_back();\n }\n\n return;\n }\n\n vector<vector<int>> findSubsequences(vector<int>& nums_) {\n vector<int> temp;\n nums = nums_;\n\n solve(0, temp);\n vector<vector<int>> res(ans.begin(), ans.end());\n return res;\n }\n};",
"memory": "42236"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\nprivate: \n void solve(int index, int mask, vector<int>& v, vector<int>& nums, vector<vector<int>>& ans, vector<int>& dp){\n if(index==nums.size()){\n if(!dp[mask] && v.size()>1){\n ans.push_back(v);\n dp[mask]=1;\n }\n\n return;\n }\n\n int sz=v.size();\n if(!sz){\n v.push_back(nums[index]);\n solve(index+1,(1<<index),v,nums,ans,dp);\n v.pop_back();\n solve(index+1,0,v,nums,ans,dp);\n }\n else{\n if(nums[index]>=v[sz-1]){\n v.push_back(nums[index]);\n solve(index+1,mask|(1<<index),v,nums,ans,dp);\n v.pop_back();\n }\n solve(index+1,mask,v,nums,ans,dp);\n }\n\n return;\n }\npublic:\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n int n=nums.size();\n vector<int> dp((1<<(n+1)),0);\n vector<vector<int>> ans;\n vector<int> v;\n solve(0,0,v,nums,ans,dp);\n\n set<vector<int>> st;\n for(auto it : ans){\n st.insert(it);\n }\n\n ans.clear();\n for(auto it : st){\n ans.push_back(it);\n }\n return ans;\n }\n};",
"memory": "42236"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 2 | {
"code": "#include <iostream>\n#include <vector>\n#include <set>\n\nusing namespace std;\n\nclass Solution {\npublic:\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vector<vector<int>> dp[nums.size()]; // DP array to store subsequences ending at each element\n set<vector<int>> res; // Set to store unique subsequences\n \n for (int i = 0; i < nums.size(); ++i) {\n for (int j = 0; j < i; ++j) {\n if (nums[j] <= nums[i]) {\n // For each subsequence ending at nums[j], we can append nums[i]\n for (const auto& subseq : dp[j]) {\n vector<int> newSubseq = subseq;\n newSubseq.push_back(nums[i]);\n dp[i].push_back(newSubseq);\n res.insert(newSubseq); // Insert into result set\n }\n }\n }\n // Single element subsequence starting with nums[i]\n dp[i].push_back({nums[i]});\n }\n \n // Add subsequences of length >= 2 to the result\n for (int i = 0; i < nums.size(); ++i) {\n for (const auto& subseq : dp[i]) {\n if (subseq.size() > 1) {\n res.insert(subseq);\n }\n }\n }\n \n return vector<vector<int>>(res.begin(), res.end()); // Convert set to vector of vectors\n }\n};\n\n",
"memory": "43378"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n \n int n=nums.size();\n vector<vector<vector<int>>> V(n,vector<vector<int>>());\n V[0].push_back({nums[0]});\n \n for(int i=1; i<n;i++){\n V[i].push_back({nums[i]});\n for(int j=i-1;j>=0;j--){\n if(nums[i]<nums[j])continue;\n for(auto v:V[j]){\n v.push_back(nums[i]);\n V[i].push_back(v);\n }\n }\n }\n set<vector<int>> used;\n for(auto &v:V){\n for(auto &w:v){\n if(w.size()==1){\n continue;\n }\n used.insert(w);\n }\n }\n vector<vector<int>> ans(used.begin(),used.end());\n return ans;\n }\n};",
"memory": "43378"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic: \n vector<vector<int>> findSubsequences(vector<int>& nums) {\n \n function<void(int, int, vector<int>&)> generate;\n\n set<vector<int>> res;\n\n generate = [&nums, &res, &generate](int idx, int prev, vector<int>& temp)->void {\n if(idx >= nums.size()) {\n if(temp.size() > 1)\n res.insert(temp);\n return;\n }\n\n generate(idx+1, prev, temp);\n\n if(prev==-1 || nums[idx] >= nums[prev]) {\n temp.push_back(nums[idx]);\n generate(idx+1, idx, temp);\n temp.pop_back();\n } \n\n };\n\n generate(0, -1, *new vector<int>());\n\n\n return *new vector<vector<int>>(res.begin(), res.end());\n }\n};",
"memory": "44519"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\n int n;\n set<vector<int>>ans;\n vector<int>part;\n void solve(int i,int j,vector<int>&nums){\n if(part.size()>=2) ans.insert(part);\n if(i==n){\n return;\n }\n if(j==-1||nums[j]<=nums[i]){\n part.push_back(nums[i]);\n solve(i+1,i,nums);\n part.pop_back();\n }\n solve(i+1,j,nums);\n\n }\n \npublic:\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n n=nums.size();\n solve(0,-1,nums);\n vector<vector<int>>res;\n for(auto it:ans){\n res.push_back(it);\n }\n return res;\n }\n};",
"memory": "44519"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n void f(int ind,vector<int>& nums,set<vector<int>>&st,vector<int>&vec)\n {\n int n=nums.size();\n if(ind>=n){\n if(vec.size()>1)\n {\n if(st.find(vec)==st.end())\n st.insert(vec);\n }\n return;\n }\n\n if(vec.empty())\n {\n f(ind+1,nums,st,vec);\n vec.push_back(nums[ind]);\n f(ind+1,nums,st,vec);\n vec.pop_back();\n }\n else \n {\n if(nums[ind]<vec.back())\n f(ind+1,nums,st,vec);\n else \n {\n f(ind+1,nums,st,vec);\n vec.push_back(nums[ind]);\n f(ind+1,nums,st,vec);\n vec.pop_back(); \n }\n }\n }\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vector<vector<int>>res;\n int n=nums.size();\n vector<int>vec;\n set<vector<int>>st;\n f(0,nums,st,vec);\n for(auto it:st )\n res.push_back(it);\n return res;\n }\n};",
"memory": "45660"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n/*We saw that ki kaafi Permutations bnaune aa, hence Problem of backtracking.\nBcz har ek element vaaste check krna thus we use for loop from idx until n.\nWe need not call solve again in the case of exclude bcz i++ automatically ho he\nreha hai loop de vich.\nSet da use krke duplicates khatam krle, We just checked ki agar ek element use krlea\ntn dwara oho use naa hove*/\nvector<vector<int>>ans;\nint n;\nset<vector<int>>ans2;\n// void solve(vector<int>&nums, int idx, vector<int>&output){\n// if(output.size() >= 2)\n// ans.push_back(output);\n \n// unordered_set<int>st; // Har recursive call da apna set hoega\n// for(int i=idx;i<n;i++){\n// if(output.empty() || nums[i] >= output.back() && st.find(nums[i]) == st.end()){\n// output.push_back(nums[i]);\n// st.insert(nums[i]);\n// solve(nums,i+1,output);\n// output.pop_back();\n// // solve(nums,i+1,output);\n// }\n// }\n// }\n\n// using Set of vectors\nvoid solve(vector<int>&nums, int i, vector<int>&output){\n if(output.size() >= 2)\n ans2.insert(output);\n\n if(i >= n)\n return;\n\n if(output.empty() || nums[i] >= output.back()){\n output.push_back(nums[i]); // DO\n solve(nums,i+1,output); // EXPLORE\n output.pop_back();// REVERT\n }\n // Not Include wala case bahar, bcx we just i+1 from the index\n solve(nums,i+1,output);\n}\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n n = nums.size();\n vector<int>output;\n solve(nums,0,output);\n for(auto &vec : ans2)\n ans.push_back(vec);\n return ans;\n }\n};",
"memory": "45660"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n set<vector<int>> res;\n int sz = nums.size();\n vector<vector<vector<int>>> dp(sz);\n for(int i = 0; i < sz; i++) {\n dp[i].push_back({nums[i]});\n for(int j = i - 1; j >= 0; j--) {\n if(nums[i] >= nums[j]) {\n for(auto c : dp[j]) {\n c.push_back(nums[i]);\n res.insert(c);\n dp[i].push_back(c);\n }\n }\n }\n }\n vector<vector<int>> rep;\n for(auto itr : res)\n rep.push_back(itr);\n return rep;\n }\n};",
"memory": "46801"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> t;\n vector<vector<int>> ans;\n set<vector<int>> m;\n \n void fun(vector<int>& v, int i)\n {\n int n = t.size();\n if(n > 1 && t[n - 1] < t[n - 2])\n return;\n \n if(n > 1)\n m.insert(t);\n \n if(i == v.size())\n return;\n \n fun(v, i + 1);\n \n t.push_back(v[i]);\n fun(v, i + 1);\n t.pop_back();\n }\n \n vector<vector<int>> findSubsequences(vector<int>& v) {\n fun(v, 0);\n \n for(auto i : m)\n ans.push_back(i);\n \n return ans;\n }\n};",
"memory": "46801"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> t;\n vector<vector<int>> ans;\n set<vector<int>> m;\n \n void fun(vector<int>& v, int i)\n {\n int n = t.size();\n if(n > 1 && t[n - 1] < t[n - 2])\n return;\n \n if(n > 1)\n m.insert(t);\n \n if(i == v.size())\n return;\n \n fun(v, i + 1);\n \n t.push_back(v[i]);\n fun(v, i + 1);\n t.pop_back();\n }\n \n vector<vector<int>> findSubsequences(vector<int>& v) {\n fun(v, 0);\n \n for(auto i : m)\n ans.push_back(i);\n \n return ans;\n }\n};",
"memory": "47943"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> t;\n vector<vector<int>> ans;\n set<vector<int>> m;\n \n void fun(vector<int>& v, int i)\n {\n int n = t.size();\n if(n > 1 && t[n - 1] < t[n - 2])\n return;\n \n if(n > 1)\n m.insert(t);\n \n if(i == v.size())\n return;\n \n fun(v, i + 1);\n \n t.push_back(v[i]);\n fun(v, i + 1);\n t.pop_back();\n }\n \n vector<vector<int>> findSubsequences(vector<int>& v) {\n fun(v, 0);\n \n for(auto i : m)\n ans.push_back(i);\n \n return ans;\n }\n};",
"memory": "47943"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void rec(vector<int>& nums, int i, vector<vector<int>>& ans, vector<int>& curr, set<vector<int>>& mp) {\n if (i == nums.size()) {\n if (curr.size() > 1 && mp.find(curr) == mp.end()) {\n ans.push_back(curr);\n mp.insert(curr); // Store unique subsequences\n }\n return;\n }\n\n // Case when we don't pick the current number\n rec(nums, i + 1, ans, curr, mp);\n\n // Case when we pick the current number if it maintains the non-decreasing property\n if (curr.size() == 0 || nums[i] >= curr.back()) {\n curr.push_back(nums[i]);\n rec(nums, i + 1, ans, curr, mp);\n curr.pop_back(); // Backtrack\n }\n }\n\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vector<vector<int>> ans;\n vector<int> curr;\n set<vector<int>> mp; // Set to ensure unique subsequences\n rec(nums, 0, ans, curr, mp);\n return ans;\n }\n};\n",
"memory": "49084"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void rec(vector<int>& nums, int i, vector<vector<int>>& ans, vector<int>& curr, set<vector<int>>& mp) {\n if (i == nums.size()) {\n if (curr.size() > 1 && mp.find(curr) == mp.end()) {\n ans.push_back(curr);\n mp.insert(curr); // Store unique subsequences\n }\n return;\n }\n\n // Case when we don't pick the current number\n rec(nums, i + 1, ans, curr, mp);\n\n // Case when we pick the current number if it maintains the non-decreasing property\n if (curr.size() == 0 || nums[i] >= curr.back()) {\n curr.push_back(nums[i]);\n rec(nums, i + 1, ans, curr, mp);\n curr.pop_back(); // Backtrack\n }\n }\n\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vector<vector<int>> ans;\n vector<int> curr;\n set<vector<int>> mp; // Set to ensure unique subsequences\n rec(nums, 0, ans, curr, mp);\n return ans;\n }\n};\n",
"memory": "49084"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void rec(vector<int>& nums,int i, vector<int> v, set<vector<int>>& s){\n s.insert(v);\n\n for(int j=i;j<nums.size();j++){\n if(v.empty() || nums[j]>=v.back()){\n v.push_back(nums[j]);}\n rec(nums,j+1,v,s);\n if(!v.empty()){\n v.pop_back();\n } \n }\n\n // for(int j=i;j<nums.size();j++){\n\n // if(j!=i && nums[j]==nums[j-1]){continue;}\n // v.push_back(nums[j]);\n // rec(nums,);\n // v.pop_back();\n // }\n\n }\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n set<vector<int>> s;\n vector<int> v;\n rec(nums,0,v,s);\n\n vector<vector<int>> ans;\n for(auto& it: s){\n if(it.size()>=2){\n ans.push_back(it);}\n }\n\n return ans;\n }\n};",
"memory": "50225"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n set<vector<int>> map;\n vector<vector<int>> answer;\n vector<int> sub;\n void sum(vector<int>& arr,int index,int n){\n if(index>=n){\n return;\n }\n \n if(sub.size()==0 || arr[index]>=sub.back()){\n sub.push_back(arr[index]);\n if(sub.size()>=2 && map.find(sub)==map.end()){\n map.insert(sub);\n answer.push_back(sub);\n }\n\n sum(arr,index+1,n);\n sub.pop_back();\n }\n sum(arr,index+1,n);\n\n }\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n // sort(nums.begin(),nums.end());\n sum(nums,0,nums.size());\n return answer;\n }\n\n};",
"memory": "50225"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n int n=nums.size();\n set<vector<int>> st;\n for(int i=0;i<nums.size();i++)\n {\n \n set<vector<int>> s=st;\n // st.clear();\n for(auto j:s)\n {\n vector<int> v=j;\n if(v.back()<=nums[i])v.push_back(nums[i]);\n st.insert(v);\n \n }\n vector<int> temp;\n temp.push_back(nums[i]);\n st.insert(temp);\n }\n vector<vector<int>> v;\n for(auto i:st)\n {\n vector<int> temp=i;\n if(temp.size()>=2)\n v.push_back(i);\n }\n \n return v;\n \n }\n};",
"memory": "51366"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n set<vector<int>>fin;\n void fun(vector<int>&nums,int ind, int prev,vector<int>&ans){\n int n=nums.size();\n if(ans.size()>=2)fin.insert(ans);\n if(ind>=n)return;\n \n if(nums[ind]>=prev){\n ans.push_back(nums[ind]);\n fun(nums,ind+1,nums[ind],ans);\n ans.pop_back();\n }\n fun(nums,ind+1,prev,ans);\n }\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vector<int>ans;\n fun(nums,0,-102,ans);\n vector<vector<int>>anss;\n for(auto x:fin){\n vector<int>temp;\n for(auto y:x)temp.push_back(y);\n anss.push_back(temp);\n }\n return anss;\n }\n};",
"memory": "51366"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vector<vector<int>> res;\n stack<pair<int, vector<int>>> st;\n st.push({0, {}});\n\n while (!st.empty()) {\n auto [i, curr] = st.top();\n st.pop();\n\n if (i == nums.size()) {\n if (curr.size() >= 2) {\n res.push_back(curr);\n }\n continue;\n }\n\n // Simulate the \"not choose\" part\n st.push({i + 1, curr});\n\n // Simulate the \"choose\" part\n if (curr.empty() || nums[i] >= curr.back()) {\n curr.push_back(nums[i]);\n st.push({i + 1, curr});\n }\n }\n\n // Remove duplicates\n set<vector<int>> uniqueRes(res.begin(), res.end());\n res.assign(uniqueRes.begin(), uniqueRes.end());\n\n return res;\n }\n};\n",
"memory": "55931"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Node{\npublic:\n unordered_map<int, Node*> mp;\n bool vis;\n Node(){\n vis = false;\n }\n bool add(int num){ \n if(mp.find(num) == mp.end()){\n mp[num] = new Node();\n return true;\n }\n return false;\n }\n};\n\nclass Solution {\npublic:\n vector<vector<int>> res;\n void backtrack(vector<int>& nums, vector<int>& cur, int idx, Node* node){\n if(idx >= nums.size()){\n if(cur.size() >= 2 && !node->vis){\n res.push_back(cur);\n node->vis = true;\n }\n return;\n }\n Node* prev = node;\n if(cur.empty() || nums[idx] >= cur[cur.size()-1]){\n cur.push_back(nums[idx]);\n node->add(nums[idx]);\n node = node->mp[nums[idx]];\n backtrack(nums, cur, idx+1, node);\n cur.pop_back();\n node = prev;\n }\n backtrack(nums, cur, idx+1, node);\n }\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vector<int> cur;\n Node* node = new Node();\n backtrack(nums, cur, 0, node);\n return res;\n }\n};",
"memory": "57073"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vector<vector<int>> result;\n int n = nums.size(),range = 1<<n;\n for(int i=1;i<range;i++)\n {\n if((i&(i-1))!=0)\n {\n bool choice = true;\n vector<int> temp;\n for(int j=0;j<n;j++)\n {\n int x = n-1-j;\n if(i&(1<<x))\n {\n if(temp.empty())\n {\n temp.push_back(nums[j]);\n }\n else if(temp[temp.size()-1]<=nums[j])\n {\n temp.push_back(nums[j]);\n }\n else\n {\n choice = false;\n break;\n }\n }\n }\n if(choice)\n {\n auto it = find(result.begin(),result.end(),temp);\n if(it==result.end())\n {\n result.push_back(temp);\n }\n }\n }\n }\n return result;\n }\n};",
"memory": "57073"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n set<vector<int>>ans;\n void back(vector<int>&nums,vector<int>&cur,int prv,int idx){\n if(idx==nums.size()){\n vector<int>kk=cur;\n if(cur.size()>1)ans.insert(kk);\n return;\n }\n back(nums,cur,prv,idx+1);\n if(nums[idx]>=prv){\n cur.push_back(nums[idx]);\n back(nums,cur,nums[idx],idx+1);\n cur.pop_back();\n }\n }\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vector<int>cur;\n back(nums,cur,-200,0);\n return vector<vector<int>>(ans.begin(),ans.end());\n }\n};",
"memory": "58214"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "#include <set>\n\nclass Solution {\npublic:\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n set<vector<int>> res;\n for (uint32_t m = 1; m != 1 << nums.size(); ++m) {\n if (popcount(m) < 2) continue;\n vector<int> v;\n int prev = numeric_limits<int>::min();\n for (uint32_t t = m; t; t &= t-1) {\n int val = nums[countr_zero(t)];\n if (val < prev) { goto cont; }\n v.push_back(prev = val);\n }\n res.insert(std::move(v));\n cont:;\n }\n return { res.begin(), res.end() };\n }\n};",
"memory": "58214"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void helper(vector<vector<int>> &ans, vector<int>&v1,vector<int>nums,int idx){\n if(idx==nums.size()){\n return;\n }\n for(int i=idx;i<nums.size();i++){\n v1.push_back(nums[i]);\n helper(ans,v1,nums,i+1);\n if(find(ans.begin(),ans.end(),v1) ==ans.end()&& v1.size()>=2){\n bool flag=false;\n for(int i=0;i<v1.size()-1;i++){\n if(v1[i]>v1[i+1]){\n flag=true;\n break;\n }\n }\n if(!flag){\n ans.push_back(v1);\n }\n }\n v1.pop_back();\n }\n }\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vector<vector<int>> ans;\n vector<int> v1;\n helper(ans,v1,nums,0);\n return ans;\n }\n};",
"memory": "59355"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n // void solve(vector<vector<int>>&ans,vector<int>&temp,int prev,int curr,vector<int>&nums,map<int,bool>&m){\n // if(curr>=nums.size()){\n // if(temp.size()>1){\n // ans.push_back(temp);\n // }\n // return;\n // }\n // if(prev==-1||(nums[curr]>=nums[prev]&&m[curr]==false)){\n\n // temp.push_back(nums[curr]);\n \n \n // solve(ans,temp,curr,curr+1,nums,m);\n \n // temp.pop_back();\n \n // } \n // if(prev==-1||nums[curr]!=temp.back()){\n // solve(ans,temp,prev,curr+1,nums,m);\n // }\n // }\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n set<vector<int>>ans;\n vector<int>temp;\n map<int,bool>m;\n\n for(int i=0;i<(1<<nums.size());i++){\n vector<int>temp;\n for(int j=0;j<nums.size();j++){\n if((1&(i>>j))==1){\n if(temp.empty()==true||temp.back()<=nums[j]){\n temp.push_back(nums[j]);\n }\n else{\n break;\n }\n }\n }\n if(temp.size()>1){\n ans.insert(temp);\n }\n }\n \n return vector (ans.begin(),ans.end());\n }\n};",
"memory": "60496"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n set<vector<int>> result;\n for(int i=1; i<pow(2,nums.size()); i++){\n int k=i;\n vector<int> temp;\n int prev=-101;\n bool p=true;\n for(int j=0; j<nums.size(); j++){\n if(k&1){\n if(nums[j]>=prev){\n prev=nums[j];\n temp.push_back(nums[j]);}\n else{\n p=false;\n break;\n }\n }\n k=k>>1;\n }\n if(p && temp.size()>1){\n result.insert(temp);\n }\n }\n vector<vector<int>> result1(result.begin(), result.end());\n return result1;\n }\n};",
"memory": "61638"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n set<vector<int>> uniqueSubsequences;\n int n = nums.size();\n for(int i=3;i<pow(2,n);i++) {\n // cout<<i<<\" \"<<log2(i)<<\"\\n\";\n if(ceil(log2(i))==log2(i)) continue;\n int last = -101;\n bool isValidSubsequence = true;\n vector<int> subsequence;\n // cout<<\" for i : \"<<i<<\"\\n\";\n for(int b=0;b<n;b++) {\n // cout<<\"bit \"<<b<<\"\\n\";\n if(i&(1<<b)) {\n if(nums[b]>=last) {\n subsequence.push_back(nums[b]);\n last = nums[b];\n // cout<<\"inserting : \"<<nums[b]<<\"\\n\";\n } else {\n // cout<<\"breaking\\n\";\n isValidSubsequence = false;\n break;\n }\n }\n }\n if(isValidSubsequence) {\n uniqueSubsequences.insert(subsequence);\n }\n }\n vector<vector<int>> res(uniqueSubsequences.begin(), uniqueSubsequences.end());\n return res;\n }\n};\n// 2^2\n// <4\n\n// 00\n// 01\n// 10\n// 11",
"memory": "62779"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\n // - prashu\n\n // - bit manipulation approach\npublic:\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n set<vector<int>> uniqueSubsequences;\n int n = nums.size();\n for(int i=3;i<pow(2,n);i++) {\n if(ceil(log2(i))==log2(i)) continue;\n int last = -101;\n bool isValidSubsequence = true;\n vector<int> subsequence;\n for(int b=0;b<n;b++) {\n if(i&(1<<b)) {\n if(nums[b]>=last) {\n subsequence.push_back(nums[b]);\n last = nums[b];\n } else {\n isValidSubsequence = false;\n break;\n }\n }\n }\n if(isValidSubsequence) {\n uniqueSubsequences.insert(subsequence);\n }\n }\n vector<vector<int>> res(uniqueSubsequences.begin(), uniqueSubsequences.end());\n return res;\n }\n};",
"memory": "62779"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n int n=nums.size();\n vector<vector<int>> res;\n set<vector<int>> hs;\n \n for(int i=1;i<=(1<<n)-1;i++){\n vector<int> v;\n int f=0;\n for(int j=0;j<n;j++){\n if((i>>j)&1){\n if(v.empty()||v.back()<=nums[j])\n v.push_back(nums[j]);\n else {\n f=1;\n break;\n } \n } \n }\n if(f==0 && v.size()>1 && hs.count(v)==0) res.push_back(v),hs.insert(v);\n }\n return res;\n }\n};",
"memory": "63920"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n int n=nums.size();\n int m=pow(2,n);\n set<vector<int>> st;\n vector<vector<int>> res;\n for(int i=0;i<m;i++){\n int j=i;\n int idx=0;\n vector<int> temp;\n while(j){\n if((j&1) == 1){\n if(!temp.empty() and temp.back()>nums[idx]){\n break;\n }\n temp.push_back(nums[idx]);\n }\n idx++;\n j>>=1;\n }\n if(temp.size()>1 and j==0 and st.find(temp)==st.end())res.push_back(temp);\n st.insert(temp);\n }\n return res;\n }\n};",
"memory": "65061"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n unordered_map<string, bool> map;\n void helper (vector<int>& arr, int start, vector<vector<int>> &ans, vector<int> &curr) {\n if (start == arr.size() && curr.size() >= 2) {\n string s = \"\";\n for(int i=0; i<curr.size(); i++) {\n s += to_string(curr[i]) + \",\";\n }\n if (!map[s]) {\n ans.push_back(curr);\n map[s] = true;\n }\n return;\n }\n\n if (start >= arr.size()) {\n return;\n }\n\n helper(arr, start + 1, ans, curr);\n if (curr.size() == 0 || curr[curr.size() -1] <= arr[start]) {\n curr.push_back(arr[start]);\n helper(arr, start + 1, ans, curr);\n curr.pop_back();\n }\n }\n\n vector<vector<int>> findSubsequences(vector<int>& arr) {\n vector<vector<int>> ans;\n vector<int> curr;\n helper(arr, 0, ans, curr);\n return ans;\n }\n};",
"memory": "65061"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n set<vector<int>> ans;\n vector<int> sub;\n void solve(vector<int> nums, int i){\n if(sub.size()>1){\n ans.insert(sub);\n }\n if(i>=nums.size()) return;\n if(!sub.empty() && sub.back()<=nums[i]){\n sub.push_back(nums[i]);\n solve(nums, i+1);\n sub.pop_back();\n }\n solve(nums, i+1);\n \n }\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n for(int i=0; i<nums.size(); i++){\n sub.push_back(nums[i]);\n solve(nums, i+1);\n sub.pop_back();\n }\n vector<vector<int>> v;\n for(auto i: ans) v.push_back(i);\n return v;\n }\n};",
"memory": "66203"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n set<vector<int>> ans;\n vector<int> sub;\n void solve(vector<int> nums, int i){\n if(sub.size()>1){\n ans.insert(sub);\n }\n if(i>=nums.size()) return;\n if(!sub.empty() && sub.back()<=nums[i]){\n sub.push_back(nums[i]);\n solve(nums, i+1);\n sub.pop_back();\n }\n solve(nums, i+1);\n \n }\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n for(int i=0; i<nums.size(); i++){\n sub.push_back(nums[i]);\n solve(nums, i+1);\n sub.pop_back();\n }\n vector<vector<int>> v;\n for(auto i: ans) v.push_back(i);\n return v;\n }\n};",
"memory": "66203"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vector<vector<int>> res;\n set<vector<int>> s;\n int n=nums.size();\n for(int i=0;i<(1<<n);i++){\n vector<int> temp;\n int prev=INT_MIN,flag=0;\n for(int j=0;j<n;j++){\n if(i & (1<<j)){\n if(nums[j]<prev){\n flag=1;\n break;\n }\n prev=nums[j];\n temp.push_back(nums[j]);\n }\n }\n if(flag==0 && temp.size()>=2){\n //res.push_back(temp);\n s.insert(temp);\n }\n }\n for(auto each: s){\n res.push_back(each);\n }\n //sort(res.begin(),res.end());\n return res;\n }\n};",
"memory": "67344"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n set<vector<int>> ans;\n int n = nums.size();\n for (int i = 0; i < (1 << n); i++) {\n int lastindex = -1;\n vector<int> ss;\n bool k = true;\n for (int j = 0; j < n; j++) {\n if ((i >> j) & 1) {\n if (lastindex == -1) {\n lastindex = j;\n ss.push_back(nums[j]);\n } else {\n if (nums[lastindex] <= nums[j]) {\n ss.push_back(nums[j]);\n lastindex = j;\n } else {\n k = false;\n break;\n }\n }\n }\n }\n if (k && ss.size()>1) {\n\n ans.insert(ss);\n }\n }\n vector<vector<int>>fans;\n for (auto x:ans){\n fans.push_back(x);\n }\n return fans;\n }\n};",
"memory": "67344"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\n private:\n void solve (vector<int> &nums,vector<int> list,vector<vector<int>> &ans, int idx){\n if (idx>=nums.size()){\n if (list.size()>=2){\n ans.push_back(list);\n }\n return;\n }\n //choose\n if (list.empty() || nums[idx]>= list.back()){\n list.push_back(nums[idx]);\n solve (nums,list,ans,idx+1);\n list.pop_back();\n }\n\n //not choose\n if(list.empty()|| nums[idx]!= list.back()){\n solve (nums,list,ans,idx+1);\n }\n }\npublic:\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n // vector<vector<int>> res;\n vector<int> list;\n // int n= nums.size();\n vector<vector<int>> ans;\n solve (nums,list,ans,0);\n return ans;\n }\n};",
"memory": "68485"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vector<vector<int>> subs;\n map<vector<int>, bool> mp;\n int n = nums.size();\n for(int i = 0; i < (1 << n); i++){\n vector<int> sub;\n for (int j = 0; j < n; j++){\n if(i & (1 << j)){\n if(sub.empty())\n sub.push_back(nums[j]);\n else{\n if(nums[j] >= sub.back())\n sub.push_back(nums[j]);\n }\n }\n }\n if(sub.size() >= 2 && !mp[sub]){\n subs.push_back(sub);\n mp[sub] = 1;\n }\n }\n return subs;\n }\n};",
"memory": "69626"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vector<vector<int>> answer;\n set<vector<int>> s;\n int x = pow(2,nums.size());\n for (int i=0;i<x;i++){\n int y=i;\n int j=0;\n if ((y&(y-1))==1 || i==0) continue;\n vector<int> count;\n while(y>0){\n if (y%2==1) {\n if (count.empty()) count.push_back(nums[j]);\n else{\n if (count.back()<=nums[j]) count.push_back(nums[j]);\n }\n }\n j++;\n y/=2;\n }\n if (count.size()>=2){\n int flag=0;\n for (int j=0;j<count.size()-1;j++){\n if (count[j]<=count[j+1]) flag=1;\n else{\n flag=0;\n break;\n }\n }\n if (flag==1) s.insert(count);\n }\n }\n for (auto x:s){\n answer.push_back(x);\n }\n return answer;\n }\n};",
"memory": "70768"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vector<vector<int>> answer;\n set<vector<int>> s;\n int x = pow(2,nums.size());\n for (int i=0;i<x;i++){\n int y=i;\n int j=0;\n vector<int> count;\n while(y>0){\n if (y%2==1) {\n if (count.empty()) count.push_back(nums[j]);\n else{\n if (count.back()<=nums[j]) count.push_back(nums[j]);\n }\n }\n j++;\n y/=2;\n }\n if (count.size()>=2){\n int flag=0;\n for (int j=0;j<count.size()-1;j++){\n if (count[j]<=count[j+1]) flag=1;\n else{\n flag=0;\n break;\n }\n }\n if (flag==1) s.insert(count);\n }\n }\n for (auto x:s){\n answer.push_back(x);\n }\n return answer;\n }\n};",
"memory": "71909"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vector<vector<int>> answer;\n set<vector<int>> s;\n int x = pow(2,nums.size());\n for (int i=0;i<x;i++){\n int y=i;\n int j=0;\n if ((i&(i-1))==0 || i==0) continue;\n vector<int> count;\n while(y>0){\n if (y%2==1) {\n if (count.empty()) count.push_back(nums[j]);\n else{\n if (count.back()<=nums[j]) count.push_back(nums[j]);\n }\n }\n j++;\n y/=2;\n }\n if (count.size()>=2) s.insert(count);\n }\n for (auto x:s){\n answer.push_back(x);\n }\n return answer;\n }\n};",
"memory": "73050"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n vector<vector<int>> ans;\n \n void helpFunc(vector<int>& nums, int currIndex, vector<int> subans)\n {\n if(currIndex >= nums.size())\n {\n if(subans.size() >= 2)\n {\n ans.push_back(subans);\n }\n return;\n } \n \n \n if( subans.empty() || subans.back() <= nums[currIndex])\n {\n subans.push_back(nums[currIndex]); \n helpFunc(nums, currIndex + 1, subans); \n subans.pop_back(); \n }\n\n if( subans.empty() || subans.back() != nums[currIndex])\n helpFunc(nums, currIndex + 1, subans); \n }\n \n vector<vector<int>> findSubsequences(vector<int>& nums)\n {\n if(nums.size() < 2) return {};\n \n vector<int> tmp;\n helpFunc(nums, 0, tmp);\n return ans;\n }\n};",
"memory": "74191"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void f(vector<int>& nums,vector<int> temp,int ind,int prev,set<vector<int>>& st)\n {\n if(ind>=nums.size()) return;\n if(ind==nums.size()-1)\n {\n if(temp.size()>=2)\n {\n st.insert(temp);\n }\n if(nums[ind]>=prev)\n {\n temp.push_back(nums[ind]);\n if(temp.size()>=2)\n st.insert(temp);\n return;\n }\n }\n if(temp.size()>=2) st.insert(temp);\n f(nums,temp,ind+1,prev,st);\n if(nums[ind]>=prev)\n {\n temp.push_back(nums[ind]);\n prev=nums[ind];\n f(nums,temp,ind+1,prev,st);\n }\n }\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n set<vector<int>> st;\n f(nums,{},0,INT_MIN,st);\n vector<vector<int>> res;\n for(auto it:st)\n {\n res.push_back(it);\n }\n return res;\n }\n};",
"memory": "75333"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void sub(set<vector<int>>&s,vector<int>&m,vector<int>nums,int n){\n if(n==nums.size()){\n if(m.size()>1)s.insert(m);\n return ;\n }\n if(m.size()==0 || nums[n]>=m[m.size()-1]){\n m.push_back(nums[n]);\n sub(s,m,nums,n+1);\n if(m.size()!=0)m.pop_back();\n }\n sub(s,m,nums,n+1);\n }\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vector<vector<int>>v;\n set<vector<int>>s;\n vector<int>m;\n sub(s,m,nums,0);\n for(auto it=s.begin();it!=s.end();it++)v.push_back(*it);\n return v;\n }\n};",
"memory": "76474"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> rs;\n void fn(vector<int>& v,int x,int pv,vector<int> k){\n //This will generate all the subsequences\n if(x==v.size()){\n if(k.size()!=1 && k.size()!=0 && find(rs.begin(),rs.end(),k)==rs.end())\n rs.push_back(k);\n return;\n }\n if(pv<=v[x]){\n k.push_back(v[x]);\n fn(v,x+1,v[x],k);\n k.pop_back();\n }\n fn(v,x+1,pv,k);\n k.pop_back();\n }\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vector<int> k;\n fn(nums,0,INT_MIN,k);\n sort(rs.begin(),rs.end());\n return rs;\n }\n};",
"memory": "77615"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\n private:\n void solve (vector<int> &nums,int n, vector<int> list,set<vector<int>> &ans, int idx){\n if (idx>=n){\n if (list.size()>=2){\n ans.insert(list);\n }\n return;\n }\n //choose\n if (list.empty() || nums[idx]>= list.back()){\n list.push_back(nums[idx]);\n solve (nums,n,list,ans,idx+1);\n list.pop_back();\n }\n\n //not choose\n if(list.empty()|| nums[idx]!= list.back()){\n solve (nums,n,list,ans,idx+1);\n }\n }\npublic:\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vector<vector<int>> res;\n vector<int> list;\n int n= nums.size();\n set<vector<int>> ans;\n solve (nums,n,list,ans,0);\n for (auto it: ans){\n res.push_back(it);\n }\n return res;\n }\n};",
"memory": "78756"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\n private:\n void solve (vector<int> &nums,int n, vector<int> list,set<vector<int>> &ans, int idx){\n if (idx>=n){\n if (list.size()>=2){\n ans.insert(list);\n }\n return;\n }\n //choose\n if (list.empty() || nums[idx]>= list.back()){\n list.push_back(nums[idx]);\n solve (nums,n,list,ans,idx+1);\n list.pop_back();\n }\n\n //not choose\n if(list.empty()|| nums[idx]!= list.back()){\n solve (nums,n,list,ans,idx+1);\n }\n }\npublic:\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vector<vector<int>> res;\n vector<int> list;\n int n= nums.size();\n set<vector<int>> ans;\n solve (nums,n,list,ans,0);\n for (auto it: ans){\n res.push_back(it);\n }\n return res;\n }\n};",
"memory": "79898"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vector<int> res;\n vector<vector<int>> ans;\n set<vector<int>> s;\n int n=nums.size();\n fill(0, n, nums, res, ans,s);\n return ans;\n }\n\n void fill(int i, int n, vector<int> nums, vector<int> &res, vector<vector<int>> &ans, set<vector<int>> &s){\n if(res.size()>1){\n if(s.find(res)==s.end()){\n ans.push_back(res);\n s.insert(res);\n }\n \n }\n if(i==n) return;\n \n if(res.empty() || (!res.empty() && nums[i]>=res.back())){\n res.push_back(nums[i]);\n fill(i+1, n, nums, res, ans,s);\n res.pop_back();\n }\n fill(i+1, n, nums, res, ans,s);\n }\n};",
"memory": "81039"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void backtrack(vector<int>& nums, int index, vector<int> ans, set<vector<int>>& result) {\n\n if (ans.size() >= 2) {\n result.insert(ans);\n }\n\n if (index == nums.size()) {\n return;\n }\n\n if (ans.empty() || nums[index] >= ans.back()) {\n ans.push_back(nums[index]);\n backtrack(nums, index + 1, ans, result);\n ans.pop_back();\n }\n backtrack(nums, index + 1, ans, result);\n return;\n }\n\n vector<vector<int>> findSubsequences(vector<int>& nums) { \n set<vector<int>> result;\n vector<int> ans;\n\n backtrack(nums, 0, ans, result);\n vector<vector<int>> res(result.begin(), result.end());\n return res;\n } \n};",
"memory": "85604"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void backtrack(vector<int>& nums, int index, vector<int> ans, set<vector<int>>& result) {\n\n if (ans.size() >= 2) {\n result.insert(ans);\n }\n\n if (index == nums.size()) {\n return;\n }\n\n if (ans.empty() || nums[index] >= ans.back()) {\n ans.push_back(nums[index]);\n backtrack(nums, index + 1, ans, result);\n ans.pop_back();\n }\n backtrack(nums, index + 1, ans, result);\n return;\n }\n\n vector<vector<int>> findSubsequences(vector<int>& nums) { \n set<vector<int>> result;\n vector<int> ans;\n\n backtrack(nums, 0, ans, result);\n vector<vector<int>> res(result.begin(), result.end());\n return res;\n } \n};",
"memory": "86745"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n set<vector<int>> ans;\n\n void solve(vector<int> &nums,int index,vector<int> temp){\n if(index >= nums.size()){\n if(temp.size() >= 2)ans.insert(temp);\n return;\n }\n\n if(temp.size() == 0 || temp.back() <= nums[index]){\n temp.push_back(nums[index]);\n solve(nums,index+1,temp);\n temp.pop_back();\n }\n solve(nums,index+1,temp);\n }\n\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n vector<int> temp;\n\n solve(nums,0,temp);\n vector<vector<int>> t(ans.begin(),ans.end());\n return t;\n }\n};",
"memory": "87886"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "void generate(vector<int> &arr , set<vector<int>>& ans , int i = 0 , vector<int> temp = {}){\n if(i == arr.size()){\n if(temp.size() > 1)\n ans.insert(temp);\n return ;\n }\n\n if(temp.empty()){\n temp.push_back(arr[i]);\n generate(arr,ans,i+1,temp);\n temp.pop_back();\n }\n else if(temp.back() <= arr[i]){\n temp.push_back(arr[i]);\n generate(arr,ans,i+1,temp);\n temp.pop_back();\n }\n generate(arr,ans,i+1,temp);\n}\n\nclass Solution {\npublic:\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n set<vector<int>> ans;\n generate(nums,ans);\n vector<vector<int>> toReturn(ans.begin() , ans.end());\n return toReturn;\n }\n};",
"memory": "89028"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "void generate(vector<int> &arr , set<vector<int>>& ans , int i = 0 , vector<int> temp = {}){\n if(i == arr.size()){\n if(temp.size() > 1)\n ans.insert(temp);\n return ;\n }\n\n if(temp.empty() || temp.back() <= arr[i]){\n temp.push_back(arr[i]);\n generate(arr,ans,i+1,temp);\n temp.pop_back();\n }\n generate(arr,ans,i+1,temp);\n}\n\nclass Solution {\npublic:\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n set<vector<int>> ans;\n generate(nums,ans);\n vector<vector<int>> toReturn(ans.begin() , ans.end());\n return toReturn;\n }\n};",
"memory": "89028"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int n;\n void func(int ind,vector<int>ds,set<vector<int>>&st,vector<int>&nums, int prev){\n \n if(ind==n){\n if(ds.size()>=2){\n st.insert(ds);\n \n }\n return;\n \n }\n\n\n \n\n\n if(nums[ind]>=prev){\n ds.push_back(nums[ind]);\n func(ind+1,ds,st,nums,nums[ind]);\n ds.pop_back();\n\n }\n\n func(ind+1,ds,st,nums,prev);\n\n\n\n\n\n }\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n\n n=nums.size();\n\n set<vector<int>>st;\n vector<int>ds;\n\n \n\n func(0,ds,st,nums,-101);\n\n vector<vector<int>>ans(st.begin(),st.end());\n\n\n return ans;\n \n }\n};",
"memory": "90169"
} |
491 | <p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,6,7,7]
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,3,2,1]
<strong>Output:</strong> [[4,4]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 15</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void solve(set<vector<int>> &ans,vector<int> v,vector<int>& nums,int i,int prev){\n if(i>=nums.size()){\n if(v.size() >= 2){\n ans.insert(v); //set for unique ans\n }\n return;\n } \n //inc\n if(prev == -1 or nums[i] >= nums[prev]){\n v.push_back(nums[i]);\n solve(ans,v,nums,i+1,i);\n v.pop_back();\n }\n //exc\n solve(ans,v,nums,i+1,prev);\n }\n vector<vector<int>> findSubsequences(vector<int>& nums) {\n // own soln\n ios::sync_with_stdio(false);\n cin.tie(0); cout.tie(0);\n\n set<vector<int>> ans;\n vector<int> v;\n\n solve(ans,v,nums,0,-1);\n return vector(ans.begin(),ans.end()); //convert set to vector\n }\n};",
"memory": "91310"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.