id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n // Edge case\n if (nums.size() <= 1) return 0;\n\n // Lenghth of subarray\n int length = 0;\n\n // Tracking vector\n unordered_map<int, pair<int, int>> tracker;\n tracker[0] = {0, 0};\n\n int num = 0;\n \n for (int i = 0; i < nums.size(); i++) {\n if (nums[i] == 0) num++;\n else num--;\n if (tracker.find(num) == tracker.end()) {\n tracker[num] = {i+1, 0};\n }\n else {\n tracker[num].second = i+1;\n length = max(length, tracker[num].second - tracker[num].first);\n }\n }\n\n return length;\n }\n};", "memory": "89300" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
3
{ "code": "//bit manipulation, sliding window, dp, prefix\nclass Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n int n = nums.size();\n unordered_map<int, pair<int, int>> m;\n\n for(int i=0; i<n; i++){\n if(nums[i]==0) nums[i]=-1;\n }\n\n m[0].first = -1;\n m[0].second = -1;\n\n int sum =0;\n for(int i=0; i<n; i++){\n sum+= nums[i];\n if(m.find(sum)==m.end()) m[sum].first = i;\n else m[sum].second = i;\n }\n\n int res=0;\n for(auto a:m){\n res = max(res, a.second.second - a.second.first); \n }\n\n return res;\n }\n};", "memory": "89400" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
3
{ "code": "/*\n v\n 0 1 2 3 4 5 6 7 8\n 0 0 1 0 0 0 1 1 0\n \n -1 0 1 4 5 \nb = 5\nmax=2\n\n*/\nusing iter = vector<int>::iterator;\nclass Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n unordered_map<int, iter> balances;\n balances[0] = prev(nums.begin());\n\n int balance = 0;\n int maxLength = 0;\n for (iter it = nums.begin(); it != nums.end(); ++it) {\n balance += (*it == 0) ? 1 : -1;\n if (balances.count(balance))\n maxLength = max(maxLength, static_cast<int>(it - balances[balance]));\n else balances[balance] = it;\n }\n\n return maxLength;\n }\n};", "memory": "89500" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n vector<int> sums;\n int sum = 0;\n for (auto num : nums) {\n sum += num;\n sums.push_back(sum);\n }\n\n int max_length = nums.size();\n if (nums.size() % 2 != 0) {\n max_length -= 1;\n }\n\n while(max_length > 0) {\n int l = 0;\n int r = max_length - 1;\n while (r < nums.size()) {\n if (l == 0 && sums[r] == max_length / 2) {\n return max_length;\n } else if(l != 0 && (sums[r] - sums[l-1]) == max_length / 2) {\n return max_length;\n } else {\n l += 1;\n r += 1;\n }\n }\n max_length -= 2;\n }\n return 0;\n }\n};", "memory": "90300" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool isValid(vector<int>& integral_nums, int k) {\n for(int i = k;i < integral_nums.size();i++) {\n if (integral_nums[i] - integral_nums[i-k] == k/2) {\n return true;\n }\n }\n return false;\n }\n\n int findMaxLength(vector<int>& nums) {\n if (nums.size() <= 1) {\n return 0;\n }\n \n vector<int> integral_nums(1, 0);\n int integral = 0;\n for(int i = 0;i < nums.size();i++) {\n integral += nums[i];\n integral_nums.push_back(integral);\n }\n \n int max_len = nums.size();\n if (max_len % 2 == 1) max_len--;\n\n while(max_len >= 2) {\n if (isValid(integral_nums, max_len)) {\n return max_len;\n }\n max_len -= 2;\n }\n\n return max_len;\n }\n};", "memory": "90400" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n unordered_map<int, int> mp;\n vector<int> pfx(nums.size());\n mp[0] = -1;\n int len = 0;\n\n for(auto i = 0; i < nums.size(); i++) {\n if(nums[i] == 0) {\n pfx[i] = (i > 0 ? pfx[i-1] : 0) - 1;\n }\n else {\n pfx[i] = (i > 0 ? pfx[i-1] : 0) + 1;\n }\n }\n\n for(auto i = 0; i < nums.size(); i++) {\n if(mp.find(pfx[i]) != mp.end()) {\n len = max(len, i - mp[pfx[i]]);\n }\n else mp[pfx[i]] = i;\n }\n\n return len;\n }\n};", "memory": "91400" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n int n=nums.size();\n vector<int> p(n,1);\n for(int i=0;i<n;i++) if(nums[i]==0) nums[i]=-1;\n p[0]=nums[0];\n for(int i=1;i<n;i++) p[i]=p[i-1]+nums[i];\n // for(auto i:p) cout<<i<<\" \";\n int ans=0;\n unordered_map<int,int> mp;\n mp[0]=-1;\n for(int i=0;i<n;i++){\n if(mp.find(p[i])!=mp.end()){\n ans=max(ans,i-mp[p[i]]);\n }\n else{\n mp[p[i]]=i;\n }\n }\n return ans;\n }\n};", "memory": "91500" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n unordered_map<int, int> mp;\n vector<int> pfx(nums.size());\n mp[0] = -1;\n int len = 0;\n\n for(auto i = 0; i < nums.size(); i++) {\n if(nums[i] == 0) {\n pfx[i] = (i > 0 ? pfx[i-1] : 0) - 1;\n }\n else {\n pfx[i] = (i > 0 ? pfx[i-1] : 0) + 1;\n }\n }\n\n for(auto i : pfx) {\n cout << i << \" \";\n }\n\n for(auto i = 0; i < nums.size(); i++) {\n if(mp.find(pfx[i]) != mp.end()) {\n len = max(len, i - mp[pfx[i]]);\n }\n else mp[pfx[i]] = i;\n }\n\n return len;\n }\n};", "memory": "91500" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n int one=0,zero=0,ans=0,n=nums.size();\n vector<int> prefix(n,0);\n unordered_map<int,int> mp;\n for(int i=0;i<n;i++){\n if(nums[i]==1){\n one++;\n }else{\n zero++;\n }\n prefix[i]=(one-zero);\n if(mp.find(prefix[i])==mp.end() || prefix[i]==0){\n mp[prefix[i]]=i;\n }else{\n ans=max(ans,i-mp[prefix[i]]);\n }\n }\n if(mp[0]){\n ans=max(ans,mp[0]+1);\n }\n return ans;\n }\n};", "memory": "91600" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n int n = nums.size();\n vector<int> pre(n);\n if (nums[0] == 0) {\n pre[0] = -1;\n } else {\n pre[0] = 1;\n }\n for (int i = 1; i < n; i++) {\n if (nums[i] == 1) {\n pre[i] += pre[i - 1] + 1;\n } else {\n pre[i] += pre[i - 1] - 1;\n }\n }\n for (int i = 0; i < n; i++) {\n cout << pre[i] << \" \"; \n }\n cout << endl;\n map<int, int> mp;\n int ans = 0, curr = 0;\n for (int i = 0; i < n; i++) {\n if (pre[i] == 0) {\n mp[0] = i;\n }\n if (mp.find(pre[i]) != mp.end()) {\n curr = i - mp[pre[i]];\n ans = max(ans, curr); \n } else {\n mp[pre[i]] = i;\n }\n }\n if (mp.find(0) == mp.end() and ans == 0) {\n return 0;\n }\n return max(ans, mp[0] + 1);\n }\n};", "memory": "91700" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int>a;\n map<int,int>preSumMax;\n int findMaxLength(vector<int>& nums) {\n int n = nums.size();\n a.resize(n);\n for(int i=0;i<n;i++){\n if(nums[i]==0)nums[i]=-1;\n a[i] = nums[i];\n }\n int maxLen = 0;\n int sum=0;\n for(int i=0;i<n;i++){\n sum += a[i];\n\n if(sum == 0) maxLen = max(maxLen, i+1);\n\n if(preSumMax.find(sum) != preSumMax.end()){\n int len = i - preSumMax[sum];\n maxLen = max(maxLen, len);\n }\n if(preSumMax.find(sum) == preSumMax.end()){\n preSumMax[sum] = i;\n \n }\n }\n return maxLen;\n }\n};", "memory": "91800" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n int n = nums.size();\n vector<int> pref(n);\n map<int, int> mp;\n int sum = 0;\n mp[0] = -1;\n int maxi = 0;\n\n for (int i = 0; i < n; i++) {\n if (nums[i] == 1)\n sum += nums[i];\n else\n sum -= 1;\n\n if (mp.find(sum) != mp.end()) {\n maxi = max(maxi, i - mp[sum]);\n } else\n mp[sum] = i;\n }\n return maxi;\n }\n};", "memory": "91900" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int solve(int i,int n,int mask){\n if(i>n){\n return 1;\n } \n int cnt=0; \n for(int j=1;j<=n;j++){\n if((i%j==0||j%i==0) && ((mask&(1<<j))==(1<<j))){\n mask=mask^(1<<j);\n cnt+=solve(i+1,n,mask);\n mask|=(1<<j);\n }\n }\n \n return cnt; \n }\n int countArrangement(int n) {\n return solve(1,n,(1<<(n+1))-1);\n }\n};", "memory": "7917" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool seen[16] = {};\n int res = 0;\n int dfs(int n, int pos = 1) {\n if (pos > n) return res++;\n for (int i = 1; i <= n; i++) {\n if (!seen[i] && (i % pos == 0 || pos % i == 0)) {\n // marking i as seen\n seen[i] = true;\n dfs(n, pos + 1);\n // backtracking\n seen[i] = false;\n }\n }\n return res;\n }\n int countArrangement(int n) {\n if (n < 4) return n;\n return dfs(n);\n }\n};", "memory": "7917" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "#include <vector>\n\nclass Solution {\npublic:\n int helper(std::vector<std::vector<int>>& vec, int n, std::vector<bool>& vis, int begin) {\n if (begin >= n) {\n return 1;\n }\n \n int ans = 0;\n for (int i = 0; i < vec[begin].size(); i++) {\n int num = vec[begin][i];\n if (vis[num]) continue;\n vis[num] = true;\n ans += helper(vec, n, vis, begin + 1);\n vis[num] = false;\n }\n return ans;\n }\n\n int countArrangement(int n) {\n if (n <= 2) return n;\n \n std::vector<std::vector<int>> vec(n);\n \n for (int i = 0; i < n; i++) {\n for (int j = 1; j <= n; j++) {\n if (i + 1 == j) continue;\n if ((i + 1) % j == 0 || j % (i + 1) == 0) {\n vec[i].push_back(j);\n }\n }\n vec[i].push_back(i + 1);\n }\n \n std::vector<bool> vis(n + 1, false);\n return helper(vec, n, vis, 0);\n }\n};\n", "memory": "9552" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> match;\n vector<bool> vis;\n int res = 0;\n\n void dfs(int u, int n){\n if(u == n + 1){\n res ++;\n return;\n }\n for(auto a: match[u]){\n if(!vis[a]){\n vis[a] = true;\n dfs(u + 1, n);\n vis[a] = false;\n }\n }\n return;\n }\n\n int countArrangement(int n) {\n vis.resize(n + 1);\n match.resize(n + 1);\n for(int i = 1;i <= n;i ++)\n for(int j = 1;j <= n;j ++)\n if(i % j == 0 || j % i == 0)\n match[i].push_back(j);\n dfs(1, n);\n return res;\n }\n};", "memory": "9552" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n void fun(int n,int ind,vector<int>&nums,int& cnt,vector<int>&v,vector<vector<int>>&a){\n if(ind==n){\n a.push_back(v);\n return ;\n }\n for(int i=ind;i<n;i++){\n \n if(nums[i]%(ind+1)==0 || (ind+1)%nums[i]==0){\n swap(nums[i],nums[ind]);\n fun(n,ind+1,nums,cnt,v,a);\n swap(nums[i],nums[ind]);\n }\n \n }\n }\n int countArrangement(int n) {\n vector<int>nums;\n for(int i=1;i<=n;i++){\n nums.push_back(i);\n }\n vector<int>v;int cnt=0;\n vector<vector<int>>a;\n fun(n,0,nums,cnt,v,a);\n return a.size();\n }\n};", "memory": "11187" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int m;\n int dp[(1<<15)+1][17];\n int func(int i, int marks, unordered_map<int, set<int>>& mp){\n if(marks==((1<<m)-1)) return 1;\n if(i==(m+1)) return 0;\n\n if(dp[marks][i]!=-1) return dp[marks][i];\n\n int ans=0;\n ans=func(i+1, marks, mp);\n\n for(auto j : mp[i]){\n if((marks & (1<<(j-1)))==0){\n ans+=func(i+1, (marks | (1<<(j-1))), mp);\n }\n }\n\n return dp[marks][i] = ans;\n }\n\n int countArrangement(int n) {\n m=n;\n unordered_map<int, set<int>> mp;\n for(int i=1; i<=n; i++){\n for(int k=i; k<=n; k+=i){\n mp[i].insert(k);\n mp[k].insert(i);\n }\n }\n\n memset(dp, -1, sizeof(dp));\n\n return func(1, 0, mp);\n }\n};", "memory": "11187" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int num;\n int ans = 0;\n int dp[17][66000];\n int func(int index, int bitmask, int left){\n if (index == num + 1){\n if (left == 0) return 1;\n return 0;\n }\n if (dp[index][bitmask] != -1) return dp[index][bitmask];\n int ans = 0;\n \n for (int i=1; i<=num; i++){\n if ((bitmask & (1 << i)) == 0){\n if (i % index == 0 || index % i == 0){\n ans += func(index + 1, bitmask ^ (1 << i), left - 1);\n }\n }\n }\n return dp[index][bitmask] = ans;\n }\n\n int countArrangement(int n) {\n int bitmask = 0;\n num = n;\n memset(dp, -1, sizeof(dp));\n return func(1, 0, n);\n }\n};", "memory": "12822" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long solve(vector<vector<int>>& dp, int mask, int idx, int n) {\n if (idx == n) return 1;\n if (dp[idx][mask] != -1) return dp[idx][mask];\n int a = 0;\n for(int i = 0; i < n; i++) {\n if (((i + 1) % (idx + 1) == 0 || (idx + 1) % (i + 1) == 0) && (mask & 1 << i)) \n a += solve(dp, mask ^ (1 << i), idx + 1, n);\n }\n dp[idx][mask] = a;\n return a;\n }\n int countArrangement(int n) {\n vector<vector<int>> dp(n, vector<int>(pow(2, n), -1));\n return solve(dp, pow(2, n) - 1, 0, n);\n }\n};", "memory": "12822" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "#include <bits/stdc++.h>\n#define ss second\n#define ff first\n#define pii pair<int,int>\n#define pb push_back\n#define ll long long\n#define vi vector<int>\n#define vvi vector<vi>\n#define vll vector<ll>\t\n#define pll pair<ll,ll>\n#define vpll vector<pll>\n#define vvll vector<vll>\n#define ld long double\n#define um unordered_map\n#define all(x) x.begin(),x.end()\n\nclass Solution {\npublic:\n ll n;\n\n struct h_{\n size_t operator()(const pll& key) const {\n ll h1=hash<ll>()(key.ff);\n ll h2= hash<ll>()(key.ss);\n\n return h1 ^ (h2<<1);\n }\n };\n unordered_map<pll,ll,h_> memo;\n ll foo(ll pos, ll mask){\n if(pos>n)return 1;\n\n if(memo.count({pos,mask}))return memo[{pos,mask}];\n\n ll res=0;\n for(ll bit = 0;bit<16;bit++){\n if(mask & (1<<bit)){\n ll set_ele = bit+1;\n if(set_ele<=n && (pos%set_ele==0 || set_ele%pos==0))res+=foo(pos+1,mask^(1<<bit));\n }\n }\n return memo[{pos,mask}] = res; \n }\n int countArrangement(int n_) {\n n=n_;\n\n ll mask=(1<<17)-1;\n return foo(1,mask); \n }\n};", "memory": "14457" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int dp[16][100000];\n int solve(int i,int n,int state)\n {\n if(i>n)\n {\n return 1;\n }\n if(dp[i][state]!=-1)\n return dp[i][state];\n int ans = 0;\n for(int j = 1;j<=n;j++)\n {\n if(((1<<j)&state)==0 && (j%i==0||i%j==0))\n {\n cout<<state<<endl;\n ans+=solve(i+1,n,(state|(1<<j)));\n }\n }\n return dp[i][state] = ans;\n }\n int countArrangement(int n) {\n memset(dp,-1,sizeof(dp));\n return solve(1,n,0);\n }\n};", "memory": "14457" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(int i, int n, unordered_set<int> &us){\n // base case\n if (i==0) return ((int)us.size()==n-1);\n\n int ans=0;\n // choices 1, 2, 3, 4, 5 ... n\n for(int j=1; j<n+1; j++){\n if (us.find(j)!=us.end()) continue;\n if ((i+1)%(j) == 0 or (j)%(i+1) == 0) {\n // use j\n us.insert(j);\n ans += solve(i-1, n, us);\n us.erase(j);\n }\n }\n return ans;\n }\n int countArrangement(int n) {\n // 1 3 2\n // 1 2 3\n unordered_set<int> us;\n int ans = solve(n-1, n, us);\n return ans;\n }\n};", "memory": "16092" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n void solve(int idx,int n,vector<int> &arr,vector<int> &vis,vector<vector<int>>&ans){\n if(idx==n+1){\n ans.push_back(arr);\n return ;\n }\n for(int i=0;i<n;i++){\n int num=i+1;\n if(vis[i+1]==0 && ( num%idx==0 || idx%num==0) ){\n arr[idx]=i+1;\n vis[i+1]=1;\n solve(idx+1,n,arr,vis,ans);\n vis[i+1]=0;\n }\n }\n return ;\n }\n int countArrangement(int n) {\n vector<vector<int>>ans;\n vector<int>vis(n+1,0);\n vector<int>arr(n+1);\n solve(1,n,arr,vis,ans);\n return ans.size();\n }\n};", "memory": "16092" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> ans;\n \n // Recursive function to place numbers in the arrangement\n void solve(int n, vector<int>& curr, vector<bool>& used, int i) {\n if (i > n) {\n ans.push_back(curr); // If all positions are filled, store the arrangement\n return;\n }\n \n // Try placing each number from 1 to n in position 'i'\n for (int j = 1; j <= n; j++) {\n if (!used[j] && (j % i == 0 || i % j == 0)) {\n used[j] = true; // Mark number 'j' as used\n curr.push_back(j);\n \n // Recur for the next position\n solve(n, curr, used, i + 1);\n \n // Backtrack\n used[j] = false;\n curr.pop_back();\n }\n }\n }\n \n // Main function to count all valid arrangements\n int countArrangement(int n) {\n vector<int> curr; // Stores the current arrangement\n vector<bool> used(n + 1, false); // Keeps track of used numbers\n \n solve(n, curr, used, 1); // Start solving from position 1\n \n return ans.size(); // Return the number of valid arrangements\n }\n};\n", "memory": "17727" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> permutation;\n void get_permute(vector<int>& nums, vector<int>& curr, vector<bool>& freq) {\n if (curr.size() == nums.size()) {\n permutation.push_back(curr);\n return;\n }\n for (int i = 0; i < nums.size(); i++) {\n if (!freq[i]&&(nums[i]%(curr.size()+1)==0||(curr.size()+1)%(nums[i])==0)) {\n curr.push_back(nums[i]);\n freq[i] = true;\n get_permute(nums, curr, freq);\n curr.pop_back();\n freq[i] = false;\n }\n }\n }\n int countArrangement(int n) {\n vector<bool> freq(n + 1, false);\n vector<int> nums(n , 0);\n for (int i = 0; i < nums.size(); i++) {\n nums[i] = i+1;\n }\n vector<int> curr;\n get_permute(nums, curr, freq);\n int res=0;\n for(int i=0;i<permutation.size();i++)\n {\n bool flag=0;\n for(int j=0;j<permutation[i].size();j++)\n {\n if(permutation[i][j]%(j+1)!=0&&(j+1)%permutation[i][j]!=0)\n {\n flag=true;\n }\n }\n if(!flag)\n {\n res++;\n }\n }\n\n return res;\n }\n};", "memory": "17727" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n void solve(set<vector<int>>& ans, vector<int>& cur, vector<int>& used, int i, int n) {\n if (i == n + 1)\n ans.insert(cur);\n else {\n for (int x = 1; x <= n; ++x)\n if (used[x - 1] == 0 && (x % i == 0 || i % x == 0)) {\n used[x - 1] = 1;\n cur.push_back(x);\n solve(ans, cur, used, i + 1, n);\n used[x - 1] = 0;\n cur.pop_back();\n }\n }\n }\n\n int countArrangement(int n) {\n set<vector<int>> ans;\n vector<int> used(n, 0);\n vector<int> cur;\n solve(ans, cur, used, 1, n);\n return ans.size();\n }\n};", "memory": "19362" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(int index,int subset,int &n,vector<vector<int>>&dp){\n if(index>n)return 1;\n if(dp[index-1][subset]!=-1)return dp[index-1][subset];\n int ans=0;\n for(int i=1;i<=n;i++){\n if( (subset&(1<<i))==false && (i%index==0 || index%i==0)){\n ans+=solve(index+1,subset | (1<<i),n,dp);\n }\n }\n return dp[index-1][subset]= ans;\n }\n int countArrangement(int n) {\n vector<vector<int>>dp(n+5,vector<int>((1<<(n+1)),-1));\n return solve(1,0,n,dp);\n }\n};", "memory": "19362" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\nprivate:\n void swap( vector<int> & perms, int i ,int j ){\n int temp = perms[i];\n perms[i] = perms[j];\n perms[j] = temp;\n }\n void getCount(vector<int> & perms,vector<vector<int>> & ans,int pos,int n){\n if(pos >= perms.size()){\n ans.push_back(perms);\n return;\n }\n\n for( int i = pos ; i <perms.size() ; i++){\n swap(perms,pos, i );\n if((perms[pos])%(pos+1) == 0 || (pos + 1)%perms[pos] ==0)\n getCount(perms,ans,pos+1,n);\n swap(perms,pos,i);\n }\n }\npublic:\n int countArrangement(int n) {\n vector<int> perms;\n for( int i = 1 ; i <= n ; i++){\n perms.push_back(i);\n }\n vector<vector<int>> ans;\n getCount(perms,ans,0,n);\n \n\n int count = 0;\n\n for( auto ele : ans){\n int i ;\n for( i = 0 ; i < ele.size() ; i++){\n \n if((ele[i])%(i+1) == 0 || (i + 1)%ele[i] ==0 ){\n continue;\n }\n break;\n }\n if(i == ele.size()){\n count++;\n }\n }\n return count;\n\n }\n};", "memory": "20997" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\nprivate:\n void generate_perm(int n, int mask, vector<int>& v, int ct, vector<vector<int>>& perms){\n if(ct==n){\n perms.push_back(v);\n return;\n }\n\n for(int i=1;i<=n;i++){\n int curr_mask=1<<(i-1);\n if(!(mask&curr_mask)){\n if(i%(ct+1)==0 || (ct+1)%i==0){\n v.push_back(i);\n generate_perm(n,(mask|curr_mask),v,ct+1,perms);\n v.pop_back();\n }\n }\n }\n\n return;\n }\npublic:\n int countArrangement(int n) {\n vector<int> v;\n vector<vector<int>> perms;\n int ct=0,mask=0;\n generate_perm(n,mask,v,ct,perms);\n\n int ans=0;\n for(auto it : perms){\n int flag=1;\n for(int i=0;i<n;i++){\n if(!(it[i]%(i+1)==0 || (i+1)%it[i]==0)){\n flag=0;\n break;\n }\n }\n\n if(flag)ans++;\n }\n\n return ans;\n }\n};", "memory": "20997" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "#include<vector> \n#include<math.h> \n\nclass Solution {\npublic: \n void resolve(int index,std::vector<int>& source,int range\n ,std::vector<bool>& marks,std::vector<std::vector<int>>& nested){\n if(index>range){\n std::vector<int> clone(source) ; \n nested.push_back(clone) ; \n return ; \n } \n for( int dx=1;dx<=range;dx++){ \n if(marks[dx-1]==true){continue ; }\n if(((index>=dx)&& (index%dx==0))||((dx>index)&&(dx%index==0))){ \n marks[dx-1] = true ; \n source.push_back(dx) ; \n resolve(index+1,source,range,marks,nested) ;\n source.erase(--source.end()) ; \n marks[dx-1] = false ; } \n } \n }\n int countArrangement(int n) {\n std::vector<int> vectors ;\n std::vector<std::vector<int>> nested ; \n std::vector<bool> signs(n+1,false) ; \n resolve(1,vectors,n,signs,nested) ; \n int width = nested.size() ; \n return width ; \n }\n} ;", "memory": "22632" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\n set<vector<int>>ans;\n void recursion(int n , vector<int>&temp, int ind){\n if(ind==n-1){\n if((ind+1)%temp[ind]==0)\n ans.insert(temp);\n \n return;\n }\n if(((ind+1)%temp[ind])==0 || (temp[ind]%(ind+1)==0) ){\n recursion(n , temp , ind+1);\n }\n for(int i=ind+1;i<n;i++){\n if(((ind+1)%temp[i]) && (temp[i]%(ind+1))){continue;}\n swap(temp[ind] , temp[i]);\n recursion(n , temp , ind+1);\n swap(temp[ind] , temp[i]);\n }\n }\npublic:\n int countArrangement(int n) {\n vector<int>temp;\n for(int i=0;i<n;i++)temp.push_back(i+1);\n recursion(n , temp , 0);\n \n return ans.size();\n }\n};", "memory": "22632" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\nvector<bool> mapp;\nmap<pair<int,vector<bool>>,int> dp;\nvector<int> possible[16];\n\n int cal(int index, int n) {\n\n if(index>n)\n return 1;\n if(dp.find({index,mapp})!=dp.end()){\n return dp[{index,mapp}];\n }\n int ret=0;\n for(int j:possible[index]){\n if(mapp[j]){\n mapp[j]=0;\n ret+= cal(index+1,n);\n mapp[j]=1;\n }\n }\n return dp[{index,mapp}] =ret;\n }\n\n int countArrangement(int n) {\n \n mapp.resize(n+1);\n for(int i=1;i<n+1;i++)\n mapp[i]=1;\n\n for(int i=1;i<n+1;i++){\n for(int j=1;j<n+1;j++){\n if(i%j==0 or j%i==0){\n possible[i].push_back(j);\n }\n }\n }\n\n return cal(1,n);\n }\n};", "memory": "24267" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\nvector<bool> mapp;\nmap<pair<int,vector<bool>>,int> dp;\nvector<int> possible[16];\n\n int cal(int index, int n) {\n\n if(index>n)\n return 1;\n if(dp.find({index,mapp})!=dp.end()){\n return dp[{index,mapp}];\n }\n int ret=0;\n for(int j:possible[index]){\n if(mapp[j]){\n mapp[j]=0;\n ret+= cal(index+1,n);\n mapp[j]=1;\n }\n }\n return dp[{index,mapp}] =ret;\n }\n\n int countArrangement(int n) {\n \n mapp.resize(n+1);\n for(int i=1;i<n+1;i++)\n mapp[i]=1;\n\n for(int i=1;i<n+1;i++){\n for(int j=1;j<n+1;j++){\n if(i%j==0 or j%i==0){\n possible[i].push_back(j);\n }\n }\n }\n\n return cal(1,n);\n }\n};", "memory": "24267" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\nint ans(vector<int>&vis,int idx,int n, map<pair<vector<int>,int>,int>&mp){\n if(idx>n) return 1;\n if(mp.find({vis,idx})!=mp.end()) return mp[{vis,idx}];\n int anss=0;\n for(int i=1;i<=n;i++){\n if(!vis[i]&&(idx%i==0||i%idx==0)){\n vis[i]=1;\n anss+=ans(vis,idx+1,n,mp);\n vis[i]=0;\n }\n }\n mp[{vis,idx}]=anss;\n return anss;\n}\n int countArrangement(int n) {\n vector<int> vis(n+1,0);\n map<pair<vector<int>,int>,int>mp;\n\n return ans(vis,1,n,mp);\n }\n};", "memory": "30807" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\nvector<int> mapp;\nmap<pair<int,vector<int>>,int> dp;\n\n int cal(int index, int n, vector<int>* possible) {\n\n if(index>n)\n return 1;\n if(dp.find({index,mapp})!=dp.end()){\n return dp[{index,mapp}];\n }\n int ret=0;\n for(int j:possible[index]){\n if(mapp[j]!=0){\n mapp[j]=0;\n ret+= cal(index+1,n,possible);\n mapp[j]=1;\n }\n }\n return dp[{index,mapp}] =ret;\n }\n\n int countArrangement(int n) {\n \n vector<int> possible[n+1];\n mapp.resize(n+1);\n for(int i=1;i<n+1;i++)\n mapp[i]++;\n\n for(int i=1;i<n+1;i++){\n for(int j=1;j<n+1;j++){\n if(i%j==0 or j%i==0){\n possible[i].push_back(j);\n }\n }\n }\n\n return cal(1,n,possible);\n }\n};", "memory": "32442" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int res;\n int countArrangement(int n) {\n res = 0;\n set<int> cur;\n dfs(1,cur,n);\n\n return res;\n }\n \n void dfs(int index, set<int>& cur, int n){\n if(index==n){\n res++;\n return ;\n }\n \n for(int i=1;i<=n;i++){\n if(cur.find(i)!=cur.end()) continue;\n\n if(i%(index+1)!=0 && (index+1)%i!=0) continue;\n\n cur.insert(i);\n dfs(index+1,cur,n);\n cur.erase(i);\n }\n }\n};", "memory": "32442" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int countArrangement(int n) {\n vector<int> vis(n + 1);\n map<pair<int,vector<int>>,int> dp;\n return solve(n, 1, vis, dp);\n }\n\n int solve(int n, int idx, vector<int> vis, map<pair<int,vector<int>>,int>& dp){\n if(idx > n) {\n return 1;\n }\n if(dp.count({idx, vis})){\n return dp[{idx, vis}];\n }\n //what number to put here\n int ret = 0;\n for(int i = 1;i <= n;i ++) {\n if(vis[i] == 0) {\n vis[i] = 1;\n if(i%idx == 0 || idx % i == 0) {\n ret += solve(n, idx + 1, vis, dp);\n }\n vis[i] = 0;\n }\n }\n\n return dp[{idx, vis}] = ret;\n }\n};", "memory": "34077" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\nint Helper(int i,int n,int mask,vector<vector<int>>& dp){\n if(i==n)return 1;\n if(dp[i][mask]!=-1)return dp[i][mask];\n int ans=0;\n for(int k=1;k<=n;k++){\n if(mask & (1<<k))continue;\n if(k%(i+1)==0 || (i+1)%k==0)\n {\n //int nm=mask|(1<<k);\n ans+=Helper(i+1,n,mask|(1<<k),dp);\n }\n }\n return dp[i][mask]=ans;\n}\n int countArrangement(int n) {\n vector<vector<int>> dp(n,vector<int>(1<<16,-1));\n int mask=0;\n return Helper(0,n,mask,dp);\n }\n};", "memory": "35712" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(int i, int& taken, int n, vector<vector<int>>&dp){\n if(i==n+1)\n return 1;\n if(dp[i][taken]!=-1) return dp[i][taken];\n int ans = 0;\n for(int j = 1;j<=n;j++){\n if(!(taken & (1<<j)) && (j%i==0 || i%j==0)){\n taken|=(1<<j);\n ans+=solve(i+1, taken, n, dp);\n taken^=(1<<j);\n }\n }\n return dp[i][taken] = ans;\n }\n int countArrangement(int n) {\n int taken = 0;\n vector<vector<int>>dp(n+1, vector<int>(65536, -1));\n return solve(1, taken, n, dp);\n }\n};", "memory": "37347" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool isThere(int vis,int i)\n {\n if(((1<<i) & vis)!=0)\n {\n return 1;\n }\n return 0;\n }\n int fun(int ind,int n,int &vis,vector<vector<int>> &dp)\n {\n if(ind==n+1) return 1;\n\n if(dp[ind][vis]!=-1) return dp[ind][vis];\n\n int ans = 0;\n\n for(int i=1;i<=n;i++)\n {\n if(!isThere(vis,i))\n {\n if(i%ind==0 || ind%i==0)\n {\n // vis[i] = 1;\n vis ^= (1 << i);\n ans += fun(ind+1,n,vis,dp);\n vis ^= (1 << i);\n }\n }\n }\n\n return dp[ind][vis] = ans;\n }\n int countArrangement(int n) {\n // vector<int> vis(n+1,0);\n int vis = 0;\n vector<vector<int>> dp(n+1, vector<int> (65540, -1));\n return fun(1,n,vis,dp);\n }\n};", "memory": "38982" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool check(int num, int i) {\n return (num % i == 0) || (i % num == 0);\n }\n\n int solve(int i, int n, int mask,vector<vector<int>>& dp) {\n if (i == n + 1) {\n return 1;\n }\n\n if(dp[i][mask] != -1) return dp[i][mask];\n\n int ans = 0;\n for (int num = 1; num <= n; num++) {\n if (((mask & (1 << num)) == 0) && check(num, i)) {\n ans += solve(i + 1, n, mask | (1 << num),dp);\n }\n }\n\n return dp[i][mask] = ans;\n }\n\n int countArrangement(int n) {\n vector<vector<int>> dp(n+1,vector<int>((1 << 16),-1));\n return solve(1, n, 0, dp);\n }\n};\n", "memory": "40617" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int ans = 0;\n \n void rec(int level, set<int> &choices){\n if(level==0){\n ans++;\n return;\n }\n\n set<int> temp = choices;\n\n for(auto it: temp){\n if(it%level ==0 || level%it == 0){\n choices.erase(it);\n rec(level-1, choices);\n choices.insert(it);\n }\n }\n return;\n }\n\n int countArrangement(int n) {\n set<int> choices;\n for(int i=1;i<=n;i++){\n choices.insert(i);\n }\n rec(n, choices);\n return ans;\n }\n};", "memory": "42252" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\n int ans;\n void rec(int n, set<int>& st) {\n if (n == 0) {\n ans++;\n return;\n }\n\n set<int> temp = st;\n for (auto x : temp) {\n if (x % n == 0|| n % x == 0) {\n st.erase(x);\n rec(n-1, st);\n st.insert(x);\n }\n }\n }\npublic:\n int countArrangement(int n) {\n ans = 0;\n set<int> st;\n for (int i=1; i<=n; i++) st.insert(i);\n rec(n, st);\n return ans;\n }\n};", "memory": "43887" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int goFind(int num, int mask, int &n, unordered_map<int, vector<int>> &mp, vector<vector<int>> &dp) {\n if(num==0) {\n return 1;\n }\n if(dp[num][mask] != -1) {\n return dp[num][mask];\n }\n\n int cnt = 0;\n for(auto &it: mp[num]) {\n if(((mask>>it)&1) == 0) {\n cnt += goFind(num-1, ((mask) | (1<<it)), n, mp, dp);\n }\n }\n return dp[num][mask] = cnt;\n }\n int countArrangement(int n) {\n unordered_map<int, vector<int>> mp;\n for(int i=1;i<=n;i++) {\n for(int j=1;j<=n;j++) {\n if(i%j == 0 || j%i == 0) {\n mp[i].push_back(j);\n }\n }\n // for(auto &it: mp[i]) {\n // cout<<it<<\" \";\n // }\n // cout<<(1<<15)<<\"\\n\";\n }\n vector<vector<int>> dp(n+1, vector<int>((1<<16)+2, -1));\n return goFind(n, 0, n, mp, dp);\n }\n};", "memory": "45522" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n \n void helper(int ind ,int n, unordered_map<int,int>&mp, int &ans){\n if(ind>n){\n ans++;\n return;\n }\n for(int i=1;i<=n;i++){\n if((i%ind==0 || ind%i==0) && mp.find(i)==mp.end()){\n mp[i]=1;\n helper(ind+1,n,mp,ans);\n mp.erase(i);\n }\n }\n }\n\n int countArrangement(int n) {\n unordered_map<int,int>mp;\n int ans=0;\n helper(1,n,mp,ans);\n return ans;\n }\n};", "memory": "47157" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\nunordered_map<int,int> x;\n int countArrangement(int n) {\n vector<int> res;\n return findp(n,res,1);\n }\n int findp(int n,vector<int> &res,int idx)\n {\n if(idx>n) return 1;\n int a=0;\n for(int i=1;i<=n;i++)\n {\n if((idx%i==0 || i%idx==0) && x.find(i)==x.end())\n {\n x[i]=1;\n a+=findp(n,res,idx+1);\n x.erase(i);\n }\n }\n return a;\n }\n};", "memory": "48792" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int find(int pos , int n , unordered_map<int,int>& mp , vector<int>& ans){\n \n if(pos > n) {\n \n //for(auto x : ans) cout << x << \" \";\n cout << endl;\n \n return 1;\n }\n \n int cnt = 0;\n \n for(int i = 1 ; i <= n ; i++){\n if(((pos % i == 0)|| (i % pos == 0) )&& (mp.count(i) == 0)){\n \n mp[i]++; \n // ans.push_back(i);\n \n cnt += find(pos+1, n , mp , ans);\n mp.erase(i);\n \n // ans.pop_back();\n }\n }\n \n return cnt;\n }\n \n int countArrangement(int n) {\n \n vector<int> ans;\n \n unordered_map<int,int> mp;\n return find(1,n , mp , ans);\n }\n};", "memory": "50427" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\nprivate:\n int f(int index, int& n, unordered_set<int>& seen){\n if(index == n) return 1;\n\n int ways = 0; \n for(int i = 1 ; i <= n ; i++){\n if(seen.find(i) != seen.end()) continue;\n\n if(i % (index+1) == 0 || (index+1) % i == 0){\n seen.insert(i);\n\n ways += f(index+1, n, seen);\n\n seen.erase(i);\n }\n }\n\n return ways;\n }\npublic:\n int countArrangement(int n) {\n unordered_set<int> seen;\n\n return f(0, n, seen);\n }\n};", "memory": "52062" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(int index,int n,unordered_set<int>& st){\n if(index>=n){\n return 1;\n }\n int count=0;\n for(int i=1;i<=n;i++){\n if(st.find(i)==st.end() && ((index+1)%i==0 ||i%(index+1)==0)){\n st.insert(i);\n count+=solve(index+1,n,st);\n st.erase(i);\n }\n }\n return count;\n }\n int countArrangement(int n) {\n // vector<int>dp(n,-1);\n unordered_set<int>st;\n return solve(0,n,st);\n }\n};", "memory": "53697" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(int index,int n,unordered_set<int>& st){\n if(index>=n){\n return 1;\n }\n int count=0;\n for(int i=1;i<=n;i++){\n if(st.find(i)==st.end() && ((index+1)%i==0 ||i%(index+1)==0)){\n st.insert(i);\n count+=solve(index+1,n,st);\n st.erase(i);\n }\n }\n return count;\n }\n int countArrangement(int n) {\n unordered_set<int>st;\n vector<int>curr;\n return solve(0,n,st);\n }\n};", "memory": "55332" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int countArrangement(int n) {\n\n std::unordered_set<int> visited {};\n\n std::function<int(int)> bt = [&](int i) {\n if (i == n + 1) {\n return 1;\n }\n int cnt = 0;\n for (int j = 1; j <= n; j++) {\n if (visited.find(j) != visited.end()) {\n continue;\n }\n if (i % j == 0 || j % i == 0) {\n visited.insert(j);\n cnt += bt(i + 1);\n visited.erase(j);\n }\n }\n\n return cnt;\n };\n\n return bt(1);\n \n }\n};", "memory": "56967" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n void backtrack(vector<vector<int>>& matrix,int row,unordered_set<int>& usedCols,int& count){\n int n=matrix.size();\n if(row==n){\n count++;\n return;\n }\n for(int col=0;col<n;col++) {\n if(matrix[row][col]==1 && usedCols.find(col)==usedCols.end()){\n usedCols.insert(col);\n backtrack(matrix, row + 1, usedCols, count);\n usedCols.erase(col);\n }\n }\n }\n int countWays(vector<vector<int>>& matrix) {\n int count=0;\n unordered_set<int> usedCols;\n backtrack(matrix,0,usedCols,count);\n return count;\n }\n int countArrangement(int n) {\n vector<vector<int>> v;\n for(int i=1;i<=n;i++){\n vector<int> temp;\n for(int j=1;j<=n;j++){\n if(j%i==0 || i%j==0) temp.push_back(1);\n else temp.push_back(0);\n }\n v.push_back(temp);\n }\n vector<vector<int>> dp(n+1,vector<int>(n+1,-1));\n int ans=countWays(v);\n return ans;\n }\n};", "memory": "58602" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n void per(int n,int &c, vector<bool> vis,int pos){\n if(pos>n){\n c++;\n return;\n }\n for(int i=1;i<=n;i++){\n if(!vis[i] and ( pos % i ==0 or i%pos ==0)){\n vis[i]=true;\n per(n,c,vis,pos+1);\n vis[i]=false;\n }\n }\n }\n\n int countArrangement(int n) {\n int c=0;\n int pos=1;\n vector<bool> vis(n+1,false);\n per(n,c,vis,pos);\n return c;\n }\n};", "memory": "60237" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nclass Solution {\n public:\n int countArrangement(int n) {\n vector<bool> visited(n + 1, false);\n return calculate(visited, 1);\n }\n\n private:\n int calculate(vector<bool> visited, int nums) {\n if (nums >= visited.size()) {\n return 1;\n }\n int count = 0;\n for (int i = 1; i < visited.size(); ++i) {\n if (!visited[i] && (i % nums == 0 || nums % i == 0)) {\n visited[i] = true;\n count += calculate(visited, nums + 1);\n visited[i] = false;\n }\n }\n return count;\n }\n};", "memory": "61872" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int c=0;\n void func(int n,vector<bool> b,int ind){\n if(ind==n){\n c++;\n return;\n }\n for(int i=0;i<n;i++){\n if(!b[i]&&((i+1)%(ind+1)==0||(ind+1)%(i+1)==0)){\n b[i]=true;\n func(n,b,ind+1);\n b[i]=false;\n }\n }\n }\n int countArrangement(int n) {\n func(n,vector<bool> (n,false),0);\n return c;\n }\n};", "memory": "61872" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(int i,int n,unordered_set<int> &vis){\n if(i>n){\n return 1;\n } \n int cnt=0; \n for(int j=1;j<=i;j++){\n if(i%j==0 && vis.count(j)==0){\n vis.insert(j);\n cnt+=solve(i+1,n,vis);\n vis.erase(j);\n }\n }\n for(int j=i+1;j<=n;j++){\n if(j%i==0 && vis.count(j)==0){\n vis.insert(j);\n cnt+=solve(i+1,n,vis);\n vis.erase(j);\n }\n }\n return cnt; \n }\n int countArrangement(int n) {\n unordered_set<int> vis;\n return solve(1,n,vis);\n }\n};", "memory": "63507" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(int i,int n,unordered_set<int> &vis){\n if(i==n+1){\n return 1;\n } \n int cnt=0; \n for(int j=1;j<=i;j++){\n if(i%j==0 && vis.count(j)==0){\n //cout<<j<<\" \";\n vis.insert(j);\n cnt+=solve(i+1,n,vis);\n vis.erase(j);\n }\n }\n for(int j=i+1;j<=n;j++){\n if(j%i==0 && vis.count(j)==0){\n vis.insert(j);\n cnt+=solve(i+1,n,vis);\n vis.erase(j);\n }\n }\n return cnt; \n }\n int countArrangement(int n) {\n unordered_set<int> vis;\n return solve(1,n,vis);\n }\n};", "memory": "65142" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int dfs(unordered_set <int> &s , int n , int pos){\n if (pos > n) return 1;\n int ans = 0;\n for (int i=1 ; i<=n ; i++){\n if (s.count(i)) continue;\n if (i%pos == 0){\n s.insert(i);\n ans+= dfs(s , n , pos+1);\n s.erase(i);\n }\n else if (pos%i == 0){\n s.insert(i);\n ans+= dfs(s , n , pos+1);\n s.erase(i);\n }\n }\n return ans;\n }\n int countArrangement(int n){\n unordered_set <int> s;\n return dfs(s , n , 1);\n }\n};", "memory": "65142" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(int ind, int n, vector<int> &s, map<pair<vector<int>, int>, int> &dp)\n {\n if(ind > n) return 1;\n\n if(dp[{s, ind}] != 0) return dp[{s, ind}];\n\n int ans = 0;\n for(int i=1; i<=n; i++)\n {\n if(s[i] == 1 && (i % ind == 0 || ind % i == 0)){\n s[i] = 0;\n ans += solve(ind+1, n, s, dp);\n s[i] = 1;\n }\n } \n\n dp[{s, ind}] = ans;\n return ans;\n }\n int countArrangement(int n) {\n vector<int> s(n+1, 1);\n map<pair<vector<int>, int>, int> dp;\n return solve(1, n, s, dp);\n }\n};", "memory": "66777" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n \n //cur_ind & mask -> for prev used values\n int bitmaskdp[20][1<<20]; \n int dfs(int ind, int mask , int n) {\n if(ind==n+1) {\n return 1; \n }\n if(bitmaskdp[ind][mask]!=-1) return bitmaskdp[ind][mask] ; \n int res=0; \n for(int i=1; i<=n; i++) {\n if((1<<i)&mask) continue; \n if((ind%i==0) or (i%ind==0))\n res=(res+dfs(ind+1, (mask ^ (1<<i)), n));\n }\n return bitmaskdp[ind][mask]=res; \n }\n int countArrangement(int n) {\n memset(bitmaskdp,-1, sizeof(bitmaskdp)) ; \n return dfs(1, 0, n) ; \n }\n};", "memory": "73317" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(int idx,int n,set<int>&st)\n {\n if(idx>n)\n {\n return 1;\n }\n int ans=0;\n for(int i=1;i<=n;i++)\n {\n if(st.find(i)==st.end() && (i%idx==0 || idx%i==0))\n {\n st.insert(i);\n ans+=solve(idx+1,n,st);\n st.erase(i);\n }\n }\n return ans;\n }\n int countArrangement(int n) {\n set<int>st;\n return solve(1,n,st);\n }\n};", "memory": "74952" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n void ans(int n,set<int>&s,int &c,int j)\n {\n if(s.size()==n)\n {\n c++;\n return;\n }\n for(int i=1;i<=n;i++)\n {\n if(s.find(i)==s.end() && (i%j==0 || j%i==0))\n {\n s.insert(i);\n ans(n,s,c,j+1);\n s.erase(i);\n }\n \n }\n }\n int countArrangement(int n) {\n set<int> s;\n int c=0;\n ans(n,s,c,1);\n return c;\n }\n};", "memory": "76587" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int countArrangement(int n) {\n vector<int> nums(n+1,-1);\n for(int i=1; i<=n; ++i) nums[i]=i;\n int ans=0;\n set<int> cset;\n backtrack(nums,1,cset,ans);\n return ans;\n }\n \n void backtrack(vector<int>& nums, int i, set<int>& cset, int& ans){\n int n=nums.size();\n if(i==n){\n ans++;\n return;\n }\n \n for(int j=1; j<n; ++j){\n if(cset.count(nums[j])) continue;\n if(nums[j]%i==0||i%nums[j]==0){\n cset.insert(nums[j]);\n backtrack(nums,i+1,cset,ans);\n cset.erase(nums[j]);\n }\n }\n }\n};", "memory": "78222" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int countArrangement(int n) {\n int ans=0;\n set<int> cset;\n backtrack(n,1,cset,ans);\n return ans;\n }\n \n void backtrack(int n, int i, set<int>& cset, int& ans){\n if(i>n){\n ans++;\n return;\n }\n \n for(int j=1; j<=n; ++j){\n if(cset.count(j)) continue;\n if(j%i==0||i%j==0){\n cset.insert(j);\n backtrack(n,i+1,cset,ans);\n cset.erase(j);\n }\n }\n }\n};", "memory": "78222" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n void helper(vector<int>& curr, int& res, int n, vector<int>& num, set<int>& visited){\n if(curr.size() == n){\n res++;\n return;\n }\n for(int i = 0; i < num.size(); i++){\n if(!visited.contains(num[i]) && ((curr.size()+1) % num[i] == 0 || num[i] % (curr.size()+1) == 0)){\n curr.push_back(num[i]);\n visited.insert(num[i]);\n helper(curr, res, n, num, visited);\n visited.erase(num[i]);\n curr.pop_back();\n }\n }\n }\n int countArrangement(int n) {\n int res = 0;\n vector<int> v;\n for(int i = 1; i <= n; i++){\n v.push_back(i);\n }\n vector<int> t;\n set<int> s;\n helper(t, res, n, v, s);\n return res;\n }\n};", "memory": "79857" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int count;\n\n void solve(int idx, int n, vector<int>& temp, set<int>& vis) {\n if (idx == n+1) {\n count++;\n return;\n }\n\n for (int i = 1; i <= n; i++) {\n if (!vis.count(i) && (i%idx == 0 || idx%i == 0) ) {\n temp[idx-1] = i;\n vis.insert(i);\n solve(idx + 1, n, temp, vis);\n vis.erase(i); \n }\n }\n }\n\n int countArrangement(int n) {\n count = 0;\n vector<int> temp(n); \n set<int> vis; \n solve(1, n, temp, vis);\n\n return count;\n }\n};\n", "memory": "81492" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(int n,int cnt,map<int,int>& mp)\n {\n \n if(mp.size()==n)\n return 1;\n if(mp.size()<cnt)\n return 0;\n int ans=0;\n for(int i=1;i<=n;i++)\n {\n if(i%(cnt+1)==0 || (cnt+1)%i==0)\n {\n mp[i]++;\n ans+=solve(n,cnt+1,mp);\n mp[i]--;\n if(mp[i]==0)\n mp.erase(i);\n }\n }\n return ans;\n }\n int countArrangement(int n) {\n map<int,int>mp;\n return solve(n,0,mp);\n }\n};", "memory": "83127" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int rec(int level,set<int>&ch,set<int>&pos){\n if(level==0){ \n return 1;\n }\n int ans=0;\n set<int>temp=ch;\n map<int,vector<int>>opt;\n for(auto v:pos){\n for(auto x:ch){\n if(!(v%x)||!(x%v)){\n opt[v].push_back(x);\n }\n }\n }\n int minop=*pos.begin();\n for(auto v:pos){\n if(opt[v].size()<opt[minop].size())minop=v;\n if(!(opt[v].size()))return 0;\n }\n pos.erase(minop);\n for(auto c:opt[minop])\n {\n ch.erase(c);\n ans+=rec(level-1,ch,pos);\n ch.insert(c);\n }\n pos.insert(minop);\n return ans;\n }\n int countArrangement(int n) {\n set<int>st;\n set<int>pos;\n for(int i=1;i<=n;i++){\n st.insert(i);\n pos.insert(i);\n }\n return rec(n,st,pos);\n }\n};", "memory": "84762" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int rec(int level,set<int>&ch,set<int>&pos){\n if(level==0){ \n return 1;\n }\n int ans=0;\n set<int>temp=ch;\n map<int,vector<int>>opt;\n for(auto v:pos){\n for(auto x:ch){\n if(!(v%x)||!(x%v)){\n opt[v].push_back(x);\n }\n }\n }\n int minop=*pos.begin();\n for(auto v:pos){\n if(opt[v].size()<opt[minop].size())minop=v;\n }\n pos.erase(minop);\n for(auto c:opt[minop])\n {\n ch.erase(c);\n ans+=rec(level-1,ch,pos);\n ch.insert(c);\n }\n pos.insert(minop);\n return ans;\n }\n int countArrangement(int n) {\n set<int>st;\n set<int>pos;\n for(int i=1;i<=n;i++){\n st.insert(i);\n pos.insert(i);\n }\n return rec(n,st,pos);\n }\n};", "memory": "86397" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int rec(vector<vector<int>>& dp, vector<bool>& vis, int i, int& n, int bit){\n \n if(dp[bit][i]!=-1){\n //cout<<dp[bit][i]<<\" \";\n return dp[bit][i];\n }\n int sm = 0;\n for(int u=1;u<=n;u++){\n if(!vis[u] && (u%i==0 || i%u==0)){\n if(i<n){\n vis[u] = true;\n sm += rec(dp, vis, i+1, n, bit+pow(2, u));\n vis[u] = false;\n }\n else{\n cout<<\"jlfe\"<<endl;\n sm += 1;\n }\n }\n }\n return dp[bit][i] = sm;\n }\n int countArrangement(int n) {\n vector<vector<int>> dp(pow(2, 16), vector<int>(n+1, -1));\n vector<bool> vis(n+1, false);\n return rec(dp, vis, 1, n, 0);\n }\n};", "memory": "88032" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int rec(vector<vector<int>>& dp, vector<bool>& vis, int i, int& n, int bit){\n \n if(dp[bit][i]!=-1){\n //cout<<dp[bit][i]<<\" \";\n return dp[bit][i];\n }\n int sm = 0;\n for(int u=1;u<=n;u++){\n if(!vis[u] && (u%i==0 || i%u==0)){\n if(i<n){\n vis[u] = true;\n sm += rec(dp, vis, i+1, n, bit+pow(2, u));\n vis[u] = false;\n }\n else{\n sm += 1;\n }\n }\n }\n return dp[bit][i] = sm;\n }\n int countArrangement(int n) {\n vector<vector<int>> dp(pow(2, 16), vector<int>(n+1, -1));\n vector<bool> vis(n+1, false);\n return rec(dp, vis, 1, n, 0);\n }\n};", "memory": "88032" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int rec(int level,set<int>&ch,set<int>&pos){\n if(level==0){ \n return 1;\n }\n int ans=0;\n set<int>temp=ch;\n unordered_map<int,vector<int>>opt;\n for(auto v:pos){\n for(auto x:ch){\n if(!(v%x)||!(x%v)){\n opt[v].push_back(x);\n }\n }\n }\n int minop=*pos.begin();\n for(auto v:pos){\n if(!(opt[v].size()))return 0;\n if(opt[v].size()<opt[minop].size())minop=v;\n }\n pos.erase(minop);\n for(auto c:opt[minop])\n {\n ch.erase(c);\n ans+=rec(level-1,ch,pos);\n ch.insert(c);\n }\n pos.insert(minop);\n return ans;\n }\n int countArrangement(int n) {\n set<int>st;\n set<int>pos;\n for(int i=1;i<=n;i++){\n st.insert(i);\n pos.insert(i);\n }\n return rec(n,st,pos);\n }\n};", "memory": "89667" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n map<pair<vector<int>, int>, int> dp;\n\n int f(vector<int> q, int ind, int n) {\n \n if (q.size() > n + 1 - ind) return 0;\n if (ind == n + 1) return 1;\n\n int ans = 0;\n for (auto ele : q) {\n if (ele % ind == 0 || ind % ele == 0) {\n vector<int> new_q = q;\n new_q.erase(find(new_q.begin(), new_q.end(), ele)); // Remove the element from the vector\n ans += f(new_q, ind + 1, n);\n }\n }\n return ans;\n }\n\n int countArrangement(int n) {\n vector<int> q;\n for (int i = 1; i <= n; i++) {\n q.push_back(i);\n }\n return f(q, 1, n);\n }\n};", "memory": "91302" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int rec(int level,unordered_set<int>&ch,unordered_set<int>&pos){\n if(level==0){ \n return 1;\n }\n int ans=0;\n unordered_set<int>temp=ch;\n unordered_map<int,vector<int>>opt;\n for(auto v:pos){\n for(auto x:ch){\n if(!(v%x)||!(x%v)){\n opt[v].push_back(x);\n }\n }\n }\n int minop=*pos.begin();\n for(auto v:pos){\n if(!(opt[v].size()))return 0;\n if(opt[v].size()<opt[minop].size())minop=v;\n }\n pos.erase(minop);\n for(auto c:opt[minop])\n {\n ch.erase(c);\n ans+=rec(level-1,ch,pos);\n ch.insert(c);\n }\n pos.insert(minop);\n return ans;\n }\n int countArrangement(int n) {\n unordered_set<int>st;\n unordered_set<int>pos;\n for(int i=1;i<=n;i++){\n st.insert(i);\n pos.insert(i);\n }\n return rec(n,st,pos);\n }\n};", "memory": "92937" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n //vector<vector<int>>dp(18,vector<int>(100005,-1));\n int solve(int last , int mask , int curr,vector<vector<int>>&dp,int n){\n if(mask == (1<<n)-1)return 1;\n int &res = dp[last][mask];\n if(res!=-1)return res;\n res=0;\n for(int i=1;i<=n;i++){\n if(mask & (1<<(i-1)))continue;\n if(i%curr==0 || curr%i==0){\n res+=solve(i,mask | (1<<(i-1)),curr+1,dp,n);\n }\n }\n return res;\n }\n \n int countArrangement(int n) {\n vector<vector<int>>dp(18,vector<int>(100005,-1));\n int ans=0;\n for(int i=1;i<=n;i++){\n ans+=solve(i,(1<<(i-1)),2,dp,n);\n }\n return ans;\n }\n};", "memory": "94572" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n void helper(vector<int> nums,int pst,int &cnt){\n \n if(pst==nums.size()){\n cnt++;\n return ;\n \n }\n for(int i=pst;i<nums.size();i++){\n swap(nums[pst],nums[i]);\n if((nums[pst]%(pst+1)==0) || ((pst+1)% nums[pst]==0)) helper(nums,pst+1,cnt);\n //swap(nums[i],nums[pst]);\n }\n\n }\n \n int countArrangement(int n) {\n vector<int> nums;\n for(int i=0;i<n;i++){\n nums.push_back(i+1);\n }\n int cnt=0;\n helper(nums,0,cnt);\n return cnt;\n \n }\n};", "memory": "96207" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n void helper(vector<int> nums, int start, int& ans) {\n if (start == nums.size()) {\n ans++;\n return;\n }\n for (int i = start; i < nums.size(); i++) {\n swap(nums[start], nums[i]);\n if (nums[start] % (start + 1) == 0 ||\n (start + 1) % nums[start] == 0)\n helper(nums, start + 1, ans);\n }\n }\n\n int countArrangement(int n) {\n vector<int> nums;\n int ans = 0;\n for (int i = 1; i <= n; i++) {\n nums.push_back(i);\n }\n helper(nums, 0, ans);\n return ans;\n }\n};", "memory": "97842" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\n \npublic:\nvoid helper(vector <int> nums, int start, int &ans){\n\tif(start==nums.size()){\n ans++;\n return;\n }\n for(int i=start;i<nums.size();i++){\n swap(nums[start],nums[i]);\n if(nums[start]%(start+1)==0 || (start+1)%nums[start]==0)\n helper(nums,start+1,ans);\n\n }\n}\n \n int countArrangement(int n) {\n vector <int> nums;\n\tint ans=0;\n\tfor(int i=1; i <= n; i++){\n\t\tnums.push_back(i);\n\t}\n\thelper(nums, 0, ans);\n\treturn ans; \n \n }\n};", "memory": "99477" }
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</strong> of the following is true:</p> <ul> <li><code>perm[i]</code> is divisible by <code>i</code>.</li> <li><code>i</code> is divisible by <code>perm[i]</code>.</li> </ul> <p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 2 <b>Explanation:</b> The first beautiful arrangement is [1,2]: - perm[1] = 1 is divisible by i = 1 - perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]: - perm[1] = 2 is divisible by i = 1 - i = 2 is divisible by perm[2] = 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 15</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n void helper(vector<int> nums, int start, int& ans) {\n if (start == nums.size()) {\n ans++;\n return;\n }\n for (int i = start; i < nums.size(); i++) {\n swap(nums[start], nums[i]);\n if (nums[start] % (start + 1) == 0 ||\n (start + 1) % nums[start] == 0)\n helper(nums, start + 1, ans);\n }\n }\n\n int countArrangement(int n) {\n vector<int> nums;\n int ans = 0;\n for (int i = 1; i <= n; i++) {\n nums.push_back(i);\n }\n helper(nums, 0, ans);\n return ans;\n }\n};", "memory": "99477" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;,&quot;pickIndex&quot;] [[[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> [&quot;Solution&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;] [[[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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= w.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= w[i] &lt;= 10<sup>5</sup></code></li> <li><code>pickIndex</code> will be called at most <code>10<sup>4</sup></code> times.</li> </ul>
0
{ "code": "#define kFactor 102\n\nclass Solution {\nprivate:\n vector<int> w_normalized;\n\n auto perform_sum(vector<int>& w) -> int {\n int value = 0;\n for (int i = 0; i < w.size(); i++) { value += w[i]; }\n return value;\n }\n\n auto normalize(vector<int>& w) -> vector<int> {\n double sum = static_cast<double>(this->perform_sum(w));\n vector<int> normalized = vector<int>();\n int amount = sum > kFactor ? kFactor : sum;\n normalized.reserve(amount);\n\n for (int i = 0; i < w.size(); i++) {\n const double& value = static_cast<double>(w[i]);\n double prob = value / sum;\n int normalized_prob = prob * (amount);\n\n for (int j = 0; j < normalized_prob; j++) {\n normalized.push_back(i);\n }\n }\n\n return normalized;\n }\n\npublic:\n Solution(vector<int>& w) : w_normalized(this->normalize(w)) {}\n\n int pickIndex() {\n if (this->w_normalized.size() <= 1) { return 0; }\n\n vector<int>::iterator it = this->w_normalized.begin();\n std::advance(it, std::rand() % this->w_normalized.size());\n return *it;\n }\n};", "memory": "44700" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;,&quot;pickIndex&quot;] [[[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> [&quot;Solution&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;] [[[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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= w.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= w[i] &lt;= 10<sup>5</sup></code></li> <li><code>pickIndex</code> will be called at most <code>10<sup>4</sup></code> times.</li> </ul>
0
{ "code": "#define kFactor 102\n\nclass Solution {\nprivate:\n vector<int> w_normalized;\n\n auto perform_sum(vector<int>& w) -> int {\n int value = 0;\n for (int i = 0; i < w.size(); i++) { value += w[i]; }\n return value;\n }\n\n auto normalize(vector<int>& w) -> vector<int> {\n double sum = static_cast<double>(this->perform_sum(w));\n vector<int> normalized = vector<int>();\n int amount = sum > kFactor ? kFactor : sum;\n normalized.reserve(amount);\n\n for (int i = 0; i < w.size(); i++) {\n const double& value = static_cast<double>(w[i]);\n double prob = value / sum;\n int normalized_prob = prob * (amount);\n\n for (int j = 0; j < normalized_prob; j++) {\n normalized.push_back(i);\n }\n }\n\n return normalized;\n }\n\npublic:\n Solution(vector<int>& w) : w_normalized(this->normalize(w)) {}\n\n int pickIndex() {\n if (this->w_normalized.size() <= 1) { return 0; }\n\n vector<int>::iterator it = this->w_normalized.begin();\n std::advance(it, std::rand() % this->w_normalized.size());\n return *it;\n }\n};", "memory": "44700" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;,&quot;pickIndex&quot;] [[[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> [&quot;Solution&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;] [[[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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= w.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= w[i] &lt;= 10<sup>5</sup></code></li> <li><code>pickIndex</code> will be called at most <code>10<sup>4</sup></code> times.</li> </ul>
0
{ "code": "class Solution {\nprivate:\n vector<int> &w_normalized;\n\n auto normalize(vector<int>& w) -> vector<int> {\n int sum = std::accumulate(w.begin(), w.end(), 0);\n vector<int> normalized = vector<int>();\n\n for (int i = 0; i < w.size(); i++) {\n const double& value = static_cast<double>(w[i]);\n double prob = static_cast<double>(value)/static_cast<double>(sum);\n int normalized_prob = prob * 102;\n\n for (int j = 0; j < normalized_prob; j++) {\n normalized.push_back(i);\n }\n }\n\n return normalized;\n }\n\npublic:\n Solution(vector<int>& w) : w_normalized(w) {\n this->w_normalized = this->normalize(w);\n }\n \n int pickIndex() {\n if (this->w_normalized.size() == 0) {\n return 0;\n }\n\n vector<int>::iterator it = this->w_normalized.begin();\n std::advance(it, std::rand() % this->w_normalized.size());\n return *it;\n }\n};", "memory": "44800" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;,&quot;pickIndex&quot;] [[[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> [&quot;Solution&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;] [[[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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= w.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= w[i] &lt;= 10<sup>5</sup></code></li> <li><code>pickIndex</code> will be called at most <code>10<sup>4</sup></code> times.</li> </ul>
0
{ "code": "class Solution {\nprivate:\n vector<int> w_normalized;\n\n auto perform_sum(vector<int>& w) -> int {\n int value = 0;\n for (int i = 0; i < w.size(); i++) {\n value += w[i];\n }\n return value;\n }\n\n auto normalize(vector<int>& w) -> vector<int> {\n int sum = this->perform_sum(w);\n vector<int> normalized = vector<int>();\n normalized.reserve(102);\n\n for (int i = 0; i < w.size(); i++) {\n const double& value = static_cast<double>(w[i]);\n double prob = value/static_cast<double>(sum);\n int normalized_prob = prob * 102;\n\n for (int j = 0; j < normalized_prob; j++) {\n normalized.push_back(i);\n }\n }\n\n return normalized;\n }\n\npublic:\n Solution(vector<int>& w) : w_normalized(this->normalize(w)) {}\n \n int pickIndex() {\n if (this->w_normalized.size() <= 1) {\n return 0;\n }\n\n vector<int>::iterator it = this->w_normalized.begin();\n std::advance(it, std::rand() % this->w_normalized.size());\n return *it;\n }\n};", "memory": "44800" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;,&quot;pickIndex&quot;] [[[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> [&quot;Solution&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;] [[[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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= w.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= w[i] &lt;= 10<sup>5</sup></code></li> <li><code>pickIndex</code> will be called at most <code>10<sup>4</sup></code> times.</li> </ul>
0
{ "code": "class Solution {\npublic:\n Solution(vector<int>& w) {\n record = vector<int>(w.size(), 0);\n for (int i = 0; i < w.size(); i++)\n {\n record[i] = sum;\n sum += w[i];\n }\n srand(time(NULL));\n }\n\n int pickIndex() {\n int target = rand() % sum;\n int L = 0, R = record.size() - 1;\n while (L <= R)\n {\n int mid = (L + R) / 2;\n if (record[mid] == target)\n return mid;\n else if (record[mid] < target)\n L = mid + 1;\n else\n R = mid - 1;\n }\n return R;\n }\nprivate:\n vector<int> record;\n int sum = 0;\n};", "memory": "44900" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;,&quot;pickIndex&quot;] [[[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> [&quot;Solution&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;] [[[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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= w.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= w[i] &lt;= 10<sup>5</sup></code></li> <li><code>pickIndex</code> will be called at most <code>10<sup>4</sup></code> times.</li> </ul>
0
{ "code": "class Solution {\npublic:\n Solution(vector<int>& w) : total_(0), prefix_sums_(w.size()) {\n for(int i = 0; i < w.size(); ++i) {\n total_ += w[i];\n prefix_sums_[i] = total_;\n }\n }\n \n int pickIndex() {\n int r = rand() % total_;\n int start = 0, end = prefix_sums_.size() - 1;\n while(start <= end) {\n int mid = (start + end) / 2;\n if(r < prefix_sums_[mid] && (mid + 1 >= prefix_sums_.size() || r >= prefix_sums_[mid+1])) {\n return mid;\n }\n \n if(r < prefix_sums_[mid]) {\n end = mid - 1;\n }\n else {\n start = mid + 1;\n }\n }\n return start;\n }\n\n int total_;\n vector<int> prefix_sums_;\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": "45000" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;,&quot;pickIndex&quot;] [[[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> [&quot;Solution&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;] [[[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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= w.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= w[i] &lt;= 10<sup>5</sup></code></li> <li><code>pickIndex</code> will be called at most <code>10<sup>4</sup></code> times.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int i = 0;\n vector<int> g;\n \n Solution(vector<int>& w) {\n int n = w.size();\n g.resize(n);\n for(int i=0;i<n;i++)\n {\n if(i == 0) \n {\n g[i] = w[i];\n continue;\n }\n g[i] = g[i-1]+ w[i];\n }\n }\n \n int pickIndex() {\n int ind = rand() % g.back();\n int index = upper_bound(g.begin(), g.end(), ind) - g.begin();\n return 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": "45000" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;,&quot;pickIndex&quot;] [[[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> [&quot;Solution&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;] [[[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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= w.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= w[i] &lt;= 10<sup>5</sup></code></li> <li><code>pickIndex</code> will be called at most <code>10<sup>4</sup></code> times.</li> </ul>
0
{ "code": "class Solution {\n vector<int> arr;\npublic:\n Solution(vector<int>& w) {\n int sum = 0;\n for (int i = 1 ;i < w.size(); i++) {\n w[i] += w[i-1];\n }\n arr = w;\n }\n\n int pickIndex() {\n int len = arr.size();\n int val = rand()%arr[len-1];\n auto it = upper_bound(arr.begin(), arr.end(), val);\n return it - arr.begin();\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": "45100" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;,&quot;pickIndex&quot;] [[[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> [&quot;Solution&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;] [[[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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= w.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= w[i] &lt;= 10<sup>5</sup></code></li> <li><code>pickIndex</code> will be called at most <code>10<sup>4</sup></code> times.</li> </ul>
0
{ "code": "class Solution {\n public:\n Solution(vector<int>& w) : prefix(w.size()) {\n partial_sum(w.begin(), w.end(), prefix.begin());\n }\n\n int pickIndex() {\n const int target = rand() % prefix.back();\n int l = 0;\n int r = prefix.size();\n\n while (l < r) {\n const int m = (l + r) / 2;\n if (prefix[m] > target)\n r = m;\n else\n l = m + 1;\n }\n\n return l;\n }\n\n private:\n vector<int> prefix;\n};", "memory": "45100" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;,&quot;pickIndex&quot;] [[[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> [&quot;Solution&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;] [[[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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= w.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= w[i] &lt;= 10<sup>5</sup></code></li> <li><code>pickIndex</code> will be called at most <code>10<sup>4</sup></code> times.</li> </ul>
0
{ "code": "class Solution {\npublic: \n vector<int>weight;\n int sum =0;\n Solution(vector<int>& w) {\n int n = w.size();\n weight.resize(n);\n for(int i=0;i<w.size();i++){\n if(i==0){\n weight[i] = w[i];\n continue;\n }\n weight[i] = weight[i-1]+w[i];\n }\n } \n int pickIndex() {\n int ind = rand()%weight.back();\n int index = upper_bound(weight.begin(),weight.end(),ind)-weight.begin();\n return 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": "45200" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;,&quot;pickIndex&quot;] [[[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> [&quot;Solution&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;] [[[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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= w.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= w[i] &lt;= 10<sup>5</sup></code></li> <li><code>pickIndex</code> will be called at most <code>10<sup>4</sup></code> times.</li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> w;\n Solution(vector<int>& w) {\n for(int i=1;i<w.size();i++) {\n w[i] += w[i-1];\n }\n srand(time(NULL));\n this->w = w;\n }\n \n int pickIndex() {\n double num = (double)rand()/RAND_MAX*(w[w.size()-1]);\n int l=0;\n int r=w.size()-1;\n int res = -1;\n while(l<=r) {\n int mid = l+(r-l)/2;\n if (w[mid] < num) {\n l = mid+1;\n } else if (w[mid] >= num) {\n res = mid;\n r = mid-1;\n }\n }\n return res;\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": "45200" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;,&quot;pickIndex&quot;] [[[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> [&quot;Solution&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;] [[[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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= w.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= w[i] &lt;= 10<sup>5</sup></code></li> <li><code>pickIndex</code> will be called at most <code>10<sup>4</sup></code> times.</li> </ul>
1
{ "code": "class Solution {\npublic:\n Solution(vector<int>& w) {\n srand(time(NULL));\n int runsum = 0;\n for (int i = 0; i < w.size(); i++) {\n runsum += w[i];\n prefix.push_back(runsum);\n }\n }\n \n int pickIndex() {\n float rng = (float) rand() / RAND_MAX;\n float target = rng * prefix.back();\n \n int lo = 0;\n int hi = prefix.size() - 1;\n \n while (lo < hi) {\n int mi = lo + (hi - lo) / 2;\n if (prefix[mi] >= target) {\n hi = mi;\n }\n else {\n lo = mi + 1;\n }\n }\n return lo;\n }\nprivate:\n vector<int> prefix;\n};\n\n/* weighted inputs {2, 5, 4, 1}\nprefix: {2, 7, 11, 12}\nranges: 0: [0,2) | 1: [2)\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": "45300" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;,&quot;pickIndex&quot;] [[[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> [&quot;Solution&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;] [[[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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= w.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= w[i] &lt;= 10<sup>5</sup></code></li> <li><code>pickIndex</code> will be called at most <code>10<sup>4</sup></code> times.</li> </ul>
1
{ "code": "class Solution {\npublic:\n Solution(vector<int>& w) {\n srand(time(NULL));\n int runsum = 0;\n for (int i = 0; i < w.size(); i++) {\n runsum += w[i];\n prefix.push_back(runsum);\n }\n }\n \n int pickIndex() {\n float rng = (float) rand() / RAND_MAX;\n float target = rng * prefix.back();\n \n int lo = 0;\n int hi = prefix.size() - 1;\n \n while (lo < hi) {\n int mi = lo + (hi - lo) / 2;\n if (prefix[mi] >= target) {\n hi = mi;\n }\n else {\n lo = mi + 1;\n }\n }\n return lo;\n }\nprivate:\n vector<int> prefix;\n};\n\n/* weighted inputs {2, 5, 4, 1}\nprefix: {2, 7, 11, 12}\nranges: 0: [0,2) | 1: [2)\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": "45300" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;,&quot;pickIndex&quot;] [[[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> [&quot;Solution&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;] [[[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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= w.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= w[i] &lt;= 10<sup>5</sup></code></li> <li><code>pickIndex</code> will be called at most <code>10<sup>4</sup></code> times.</li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<int> prefixSum;\n int n;\n Solution(vector<int>& w) {\n\n \n \n for(int i=1;i<w.size();i++)\n {\n \n \n w[i]+=w[i-1];\n }\n\n for(int i=0;i<w.size();i++)\n {\n prefixSum.push_back(w[i]);\n }\n\n n = prefixSum[w.size()-1];\n\n\n }\n \n int pickIndex() {\n int p = rand()%n;\n\n cout<<n<<endl;\n\n\n\n auto it = upper_bound(prefixSum.begin(), prefixSum.end(), p);\n\n return it - prefixSum.begin();\n\n \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": "45400" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;,&quot;pickIndex&quot;] [[[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> [&quot;Solution&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;] [[[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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= w.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= w[i] &lt;= 10<sup>5</sup></code></li> <li><code>pickIndex</code> will be called at most <code>10<sup>4</sup></code> times.</li> </ul>
1
{ "code": "class Solution {\n vector<int> s;\npublic:\n Solution(vector<int>& w) {\n \n for(auto k: w) {\n if(s.empty()) s.push_back(k);\n else s.push_back(k+s.back());\n }\n }\n \n int pickIndex() {\n \n int r = rand() % s[s.size()-1];\n auto it = upper_bound(s.begin(), s.end(), r);\n return it-s.begin();\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": "45400" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;,&quot;pickIndex&quot;] [[[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> [&quot;Solution&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;] [[[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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= w.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= w[i] &lt;= 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>prefixSum;\n int totalSum;\n Solution(vector<int>& w) {\n totalSum=0;\n for(int i=0;i<w.size();i++){\n totalSum+=w[i];\n prefixSum.push_back(totalSum);\n }\n }\n \n int pickIndex() {\n int randomNum = rand() % totalSum +1;\n return lower_bound(prefixSum.begin(), prefixSum.end(), randomNum)-prefixSum.begin();\n \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": "45500" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;,&quot;pickIndex&quot;] [[[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> [&quot;Solution&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;] [[[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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= w.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= w[i] &lt;= 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> v;\n Solution(vector<int>& w) {\n v.push_back(w[0]);\n for(int i=1;i<w.size();i++)\n {\n v.push_back(v[i-1] + w[i]);\n }\n }\n \n int pickIndex() {\n int rd = rand()%v[v.size()-1];\n auto x = upper_bound(v.begin(),v.end(),rd);\n return x-v.begin();\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": "45500" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;,&quot;pickIndex&quot;] [[[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> [&quot;Solution&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;] [[[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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= w.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= w[i] &lt;= 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<double> weights;\n Solution(vector<int>& v) {\n int sum=accumulate(v.begin(), v.end(), 0);\n double prev=0.0;\n\n for(int i=0;i<v.size();i++){\n double d=static_cast<double>(v[i])/static_cast<double>(sum);\n weights.push_back(prev+d);\n \n prev+=d;\n } \n }\n \n int pickIndex() {\n double r = ((double) rand() / (RAND_MAX));\n return lower_bound(weights.begin(), weights.end(), r) - weights.begin();\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": "45600" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;,&quot;pickIndex&quot;] [[[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> [&quot;Solution&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;] [[[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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= w.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= w[i] &lt;= 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 {\nprivate:\n vector<int> nums;\n long long sum;\npublic:\n Solution(vector<int>& w) {\n nums.clear();\n sum = 0;\n for (auto n: w) {\n nums.push_back(n);\n sum += n;\n }\n }\n \n int pickIndex() {\n double x = (double) rand() / (RAND_MAX + 1.0) * sum;\n cout << sum << endl;\n cout << x << endl;\n int value = (int) x;\n cout << value << endl;\n\n for (int i = 0; i < nums.size(); i++) {\n int n = nums[i];\n x -= n;\n if (x <= 0) return i;\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": "45600" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;,&quot;pickIndex&quot;] [[[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> [&quot;Solution&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;,&quot;pickIndex&quot;] [[[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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= w.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= w[i] &lt;= 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 {\nprivate:\n vector<double> table;\npublic:\n Solution(vector<int>& w)\n {\n int sum = 0;\n for(auto val: w)\n {\n sum += val;\n }\n double cur = 0;\n for(auto& val: w)\n {\n cur += (double)val/sum;\n table.push_back(cur);\n }\n }\n int pickIndex()\n {\n double d = (double)rand() / RAND_MAX;\n return lower_bound(table.begin(), table.end(), d) - table.begin();\n }\n};", "memory": "45700" }