id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
3,373
<p>You are given an integer array <code>nums</code>.</p> <p>Return an integer that is the <strong>maximum</strong> distance between the <strong>indices</strong> of two (not necessarily different) prime numbers in <code>nums</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,5,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> <code>nums[1]</code>, <code>nums[3]</code>, and <code>nums[4]</code> are prime. So the answer is <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,2,8]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> <code>nums[2]</code> is prime. Because there is just one prime number, the answer is <code>|2 - 2| = 0</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> <li>The input is generated such that the number of prime numbers in the <code>nums</code> is at least one.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool isPrime(int n) {\n if (n <= 1) return false;\n if (n == 2 || n == 3) return true;\n if (n % 2 == 0 || n % 3 == 0) return false;\n\n for (int i = 5; i * i <= n; i += 6) {\n if (n % i == 0 || n % (i + 2) == 0) return false;\n }\n\n return true;\n }\n int maximumPrimeDifference(vector<int>& nums) {\n int mini = INT_MAX;\n int maxi = INT_MIN;\n for(int i=0;i<nums.size();i++){\n if(isPrime(nums[i])){\n mini = min(i, mini);\n maxi = max(maxi, i);\n }\n }\n \n if(mini == INT_MAX && maxi == INT_MIN) return 0;\n // cout<<maxi<<mini<<endl;\n return maxi - mini;\n }\n};", "memory": "107500" }
3,373
<p>You are given an integer array <code>nums</code>.</p> <p>Return an integer that is the <strong>maximum</strong> distance between the <strong>indices</strong> of two (not necessarily different) prime numbers in <code>nums</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,5,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> <code>nums[1]</code>, <code>nums[3]</code>, and <code>nums[4]</code> are prime. So the answer is <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,2,8]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> <code>nums[2]</code> is prime. Because there is just one prime number, the answer is <code>|2 - 2| = 0</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> <li>The input is generated such that the number of prime numbers in the <code>nums</code> is at least one.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool isPrime(int num)\n {\n if(num==1) return false;\n else if(num==2) return true;\n int i=2;\n float div=num;\n while(i<=div)\n {\n if(num%i==0) return false;\n i++;\n div=num/i;\n }\n return true;\n }\n int maximumPrimeDifference(vector<int>& nums) {\n int first=-1;\n int last=-1;\n bool flag=true;\n for(int i=0;i<nums.size();i++)\n {\n bool prime=isPrime(nums[i]);\n if(prime && flag)\n {\n first=i;\n flag=false;\n }\n if(prime) last=i;\n \n }\n return last-first;\n \n }\n};", "memory": "107600" }
3,373
<p>You are given an integer array <code>nums</code>.</p> <p>Return an integer that is the <strong>maximum</strong> distance between the <strong>indices</strong> of two (not necessarily different) prime numbers in <code>nums</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,5,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> <code>nums[1]</code>, <code>nums[3]</code>, and <code>nums[4]</code> are prime. So the answer is <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,2,8]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> <code>nums[2]</code> is prime. Because there is just one prime number, the answer is <code>|2 - 2| = 0</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> <li>The input is generated such that the number of prime numbers in the <code>nums</code> is at least one.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int prime(int val) {\n if (val < 2) {\n return false;\n }\n for (int i = 2; i <= sqrt(val); i++) {\n if (val % i == 0) {\n return false;\n }\n }\n return true;\n }\n int maximumPrimeDifference(vector<int>& nums) {\n int start = 0, end = nums.size() - 1;\n while (start < end) {\n if (prime(nums[start])) {\n break;\n }\n start++;\n }\n while (end >= start) {\n if (prime(nums[end])) {\n break;\n }\n end--;\n }\n return (end - start);\n }\n};", "memory": "107700" }
3,373
<p>You are given an integer array <code>nums</code>.</p> <p>Return an integer that is the <strong>maximum</strong> distance between the <strong>indices</strong> of two (not necessarily different) prime numbers in <code>nums</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,5,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> <code>nums[1]</code>, <code>nums[3]</code>, and <code>nums[4]</code> are prime. So the answer is <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,2,8]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> <code>nums[2]</code> is prime. Because there is just one prime number, the answer is <code>|2 - 2| = 0</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> <li>The input is generated such that the number of prime numbers in the <code>nums</code> is at least one.</li> </ul>
0
{ "code": "class Solution {\npublic:\n\n bool isPrime(int n)\n {\n if(n<=1)\n {\n return false;\n }\n for(int i=2;i*i<=n;i++)\n {\n if(n%i==0) return false;\n }\n return true;\n }\n int maximumPrimeDifference(vector<int>& nums) {\n int start=0;\n int end=nums.size()-1;\n for(int i=0;i<=end;i++)\n {\n if(isPrime(nums[i]))\n {\n start=i;\n break;\n }\n }\n for(int i=end;i>=start;i--)\n {\n if(isPrime(nums[i]))\n {\n end=i;\n break;\n }\n }\n return abs(start-end);\n }\n};", "memory": "107700" }
3,373
<p>You are given an integer array <code>nums</code>.</p> <p>Return an integer that is the <strong>maximum</strong> distance between the <strong>indices</strong> of two (not necessarily different) prime numbers in <code>nums</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,5,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> <code>nums[1]</code>, <code>nums[3]</code>, and <code>nums[4]</code> are prime. So the answer is <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,2,8]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> <code>nums[2]</code> is prime. Because there is just one prime number, the answer is <code>|2 - 2| = 0</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> <li>The input is generated such that the number of prime numbers in the <code>nums</code> is at least one.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int maximumPrimeDifference(vector<int>& nums) {\n int minIndex = INT_MAX, maxIndex = INT_MIN;\n for (int i = 0; i < nums.size(); i++) {\n if (isPrime(nums[i])) {\n minIndex = min(minIndex, i);\n maxIndex = max(maxIndex, i);\n }\n }\n return abs(maxIndex - minIndex);\n }\n \n bool isPrime(int x) {\n if (x == 1) return false;\n for (int i = 2; i * i <= x; i++) {\n if (x % i == 0) return false;\n }\n return true;\n \n }\n};", "memory": "107800" }
3,373
<p>You are given an integer array <code>nums</code>.</p> <p>Return an integer that is the <strong>maximum</strong> distance between the <strong>indices</strong> of two (not necessarily different) prime numbers in <code>nums</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,5,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> <code>nums[1]</code>, <code>nums[3]</code>, and <code>nums[4]</code> are prime. So the answer is <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,2,8]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> <code>nums[2]</code> is prime. Because there is just one prime number, the answer is <code>|2 - 2| = 0</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> <li>The input is generated such that the number of prime numbers in the <code>nums</code> is at least one.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool prime(int num) {\n if (num == 2) {\n return true;\n }\n if (num < 2) {\n return false;\n }\n for (int i = 2; i <= sqrt(num); i++) {\n if (num % i == 0) {\n return false;\n }\n }\n return true;\n }\n int maximumPrimeDifference(vector<int>& nums) {\n int n = nums.size() - 1, small = 0, large = n;\n while (small <= large) {\n if (prime(nums[small])) {\n break;\n }\n small++;\n }\n while (small <= large) {\n if (prime(nums[large])) {\n break;\n }\n large--;\n }\n return large - small;\n }\n};", "memory": "107800" }
3,373
<p>You are given an integer array <code>nums</code>.</p> <p>Return an integer that is the <strong>maximum</strong> distance between the <strong>indices</strong> of two (not necessarily different) prime numbers in <code>nums</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,5,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> <code>nums[1]</code>, <code>nums[3]</code>, and <code>nums[4]</code> are prime. So the answer is <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,2,8]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> <code>nums[2]</code> is prime. Because there is just one prime number, the answer is <code>|2 - 2| = 0</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> <li>The input is generated such that the number of prime numbers in the <code>nums</code> is at least one.</li> </ul>
1
{ "code": "class Solution {\npublic:\n \n bool isprime(int x)\n {\n if(x==1) return false;\n for(int i=2;i<=sqrt(x);i++)\n {\n if((x%i)==0)\n {\n return false;\n }\n }\n return true;\n }\n \n int maximumPrimeDifference(vector<int>& nums) {\n int st=-1,en=-1;\n for(int i=0;i<nums.size();i++)\n {\n if(isprime(nums[i]))\n {\n if(st==-1) st=i;\n en=i;\n }\n }\n return en-st;\n }\n};", "memory": "107900" }
3,373
<p>You are given an integer array <code>nums</code>.</p> <p>Return an integer that is the <strong>maximum</strong> distance between the <strong>indices</strong> of two (not necessarily different) prime numbers in <code>nums</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,5,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> <code>nums[1]</code>, <code>nums[3]</code>, and <code>nums[4]</code> are prime. So the answer is <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,2,8]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> <code>nums[2]</code> is prime. Because there is just one prime number, the answer is <code>|2 - 2| = 0</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> <li>The input is generated such that the number of prime numbers in the <code>nums</code> is at least one.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int maximumPrimeDifference(vector<int>& nums) {\n bool primes[101];\n for(int i=2;i<=100;i++)primes[i]=1;\n primes[0]=primes[1]=0;\n int first=-1,last=-1;\n for(int i=2;i<=100;i++){\n if(primes[i]==1){\n for(int j=i*i;j<=100;j+=i)primes[j]=0;\n }\n }\n for(int i=0;i<nums.size();i++){\n if(primes[nums[i]]==1){\n if(first==-1){\n first=i;last=i;\n }\n else last=i;\n }\n }\n return last-first;\n }\n};", "memory": "108000" }
3,373
<p>You are given an integer array <code>nums</code>.</p> <p>Return an integer that is the <strong>maximum</strong> distance between the <strong>indices</strong> of two (not necessarily different) prime numbers in <code>nums</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,5,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> <code>nums[1]</code>, <code>nums[3]</code>, and <code>nums[4]</code> are prime. So the answer is <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,2,8]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> <code>nums[2]</code> is prime. Because there is just one prime number, the answer is <code>|2 - 2| = 0</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> <li>The input is generated such that the number of prime numbers in the <code>nums</code> is at least one.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int maximumPrimeDifference(vector<int>& nums) {\n vector<int> sieve(101, 1);\n sieve[1] = 0;\n for(int i = 2;i<=10;i++) {\n for(int j = 2*i;j<=100;j+=i) {\n if(sieve[j] != 0) {\n sieve[j] = 0;\n }\n }\n }\n int mini = 1e5;\n int maxi = -1;\n for(int i = 0;i<nums.size();i++) {\n if(sieve[nums[i]] == 1) {\n mini = min(mini, i);\n maxi = max(maxi, i);\n }\n }\n if(mini == 1e5 && maxi == -1) return 0;\n return maxi - mini;\n }\n};", "memory": "108100" }
3,373
<p>You are given an integer array <code>nums</code>.</p> <p>Return an integer that is the <strong>maximum</strong> distance between the <strong>indices</strong> of two (not necessarily different) prime numbers in <code>nums</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,5,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> <code>nums[1]</code>, <code>nums[3]</code>, and <code>nums[4]</code> are prime. So the answer is <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,2,8]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> <code>nums[2]</code> is prime. Because there is just one prime number, the answer is <code>|2 - 2| = 0</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> <li>The input is generated such that the number of prime numbers in the <code>nums</code> is at least one.</li> </ul>
1
{ "code": "class Solution {\nprivate:\n vector<bool> prime;\n void sieve(){\n prime[0] = prime[1] = false;\n\n for(int i = 2; i*i <= 104; i++){\n if(prime[i]){\n for(int j = i*i; j <= 104; j += i){\n prime[j] = false;\n }\n }\n }\n }\npublic:\n int maximumPrimeDifference(vector<int>& nums) {\n prime = vector<bool>(105, true);\n sieve();\n\n int ans = 0, first_idx = -1;\n for(int i = 0; i < nums.size(); i++){\n if(prime[nums[i]]){\n if(first_idx == -1){\n first_idx = i;\n }\n else\n ans = i - first_idx;\n }\n }\n\n return ans;\n }\n};", "memory": "108200" }
3,373
<p>You are given an integer array <code>nums</code>.</p> <p>Return an integer that is the <strong>maximum</strong> distance between the <strong>indices</strong> of two (not necessarily different) prime numbers in <code>nums</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,5,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> <code>nums[1]</code>, <code>nums[3]</code>, and <code>nums[4]</code> are prime. So the answer is <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,2,8]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> <code>nums[2]</code> is prime. Because there is just one prime number, the answer is <code>|2 - 2| = 0</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> <li>The input is generated such that the number of prime numbers in the <code>nums</code> is at least one.</li> </ul>
2
{ "code": "class Solution {\npublic:\n void sieve(int psz,vector<int> &isprime){\n isprime[1]=0;\n for(int i=2;i*i<psz;i++){\n if(!isprime[i])continue;\n for(int j=i*i;j<psz;j+=i){\n isprime[j]=0;\n }\n }\n }\n int maximumPrimeDifference(vector<int>& nums) {\n //find first prime and last and find their distance \n //euler seive to find the prime\n int psz=105;\n vector<int> isprime(psz,1);\n sieve(psz,isprime);\n int first=-1,last=-1;\n for(int i=0;i<nums.size();i++){\n if(!isprime[nums[i]])continue;\n if(first==-1)first=i;\n last=i;\n }\n return last-first;\n }\n};", "memory": "108300" }
3,373
<p>You are given an integer array <code>nums</code>.</p> <p>Return an integer that is the <strong>maximum</strong> distance between the <strong>indices</strong> of two (not necessarily different) prime numbers in <code>nums</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,5,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> <code>nums[1]</code>, <code>nums[3]</code>, and <code>nums[4]</code> are prime. So the answer is <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,2,8]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> <code>nums[2]</code> is prime. Because there is just one prime number, the answer is <code>|2 - 2| = 0</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> <li>The input is generated such that the number of prime numbers in the <code>nums</code> is at least one.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumPrimeDifference(vector<int>& nums) {\n vector<bool> isPrime(101, true);\n isPrime[0] = isPrime[1] = false;\n for(int i = 2; i <= sqrt(100); i++){\n if(isPrime[i]){\n for(int j = i*i; j <= 100; j+=i){\n isPrime[j] = false;\n }\n }\n }\n int s = -1, e = -1;\n for(int i = 0; i < nums.size(); i++){\n if(isPrime[nums[i]]){\n s = i;\n break;\n }\n }\n for(int i = nums.size()-1; i >= 0; i--){\n if(isPrime[nums[i]]){\n e = i;\n break;\n }\n }\n \n return e-s;\n }\n};", "memory": "108400" }
3,373
<p>You are given an integer array <code>nums</code>.</p> <p>Return an integer that is the <strong>maximum</strong> distance between the <strong>indices</strong> of two (not necessarily different) prime numbers in <code>nums</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,5,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> <code>nums[1]</code>, <code>nums[3]</code>, and <code>nums[4]</code> are prime. So the answer is <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,2,8]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> <code>nums[2]</code> is prime. Because there is just one prime number, the answer is <code>|2 - 2| = 0</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> <li>The input is generated such that the number of prime numbers in the <code>nums</code> is at least one.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumPrimeDifference(vector<int>& nums) {\n vector<bool>isP(101,true);\n isP[0] = isP[1] = false;\n\n for(int p = 2; p * p <= 100; p++){\n if(isP[p]){\n for(int i = p*p; i <= 100; i+=p){\n isP[i] = false;\n }\n }\n }\n\n int n = nums.size();\n int l,r;\n for(int i=0; i<n; i++){\n if(isP[nums[i]]){\n l = i;\n break;\n }\n }\n\n for(int i=n-1; i>=0; i--){\n if(isP[nums[i]]){\n r = i;\n break;\n }\n }\n return r - l;\n }\n};", "memory": "108400" }
3,373
<p>You are given an integer array <code>nums</code>.</p> <p>Return an integer that is the <strong>maximum</strong> distance between the <strong>indices</strong> of two (not necessarily different) prime numbers in <code>nums</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,5,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> <code>nums[1]</code>, <code>nums[3]</code>, and <code>nums[4]</code> are prime. So the answer is <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,2,8]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> <code>nums[2]</code> is prime. Because there is just one prime number, the answer is <code>|2 - 2| = 0</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> <li>The input is generated such that the number of prime numbers in the <code>nums</code> is at least one.</li> </ul>
2
{ "code": "#include <vector>\n#include <unordered_set>\nusing namespace std;\n\nclass Solution {\npublic:\n bool isPrime(int num) {\n static unordered_set<int> prime_set = {\n 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, \n 53, 59, 61, 67, 71, 73, 79, 83, 89, 97\n };\n return prime_set.find(num) != prime_set.end();\n }\n\n int maximumPrimeDifference(vector<int>& nums) {\n int first_prime_index = -1;\n int last_prime_index = -1;\n\n for (int i = 0; i < nums.size(); ++i) {\n if (isPrime(nums[i])) {\n if (first_prime_index == -1) {\n first_prime_index = i;\n }\n last_prime_index = i;\n }\n }\n\n int max_distance = last_prime_index - first_prime_index;\n\n return max_distance;\n }\n};\n", "memory": "108500" }
3,373
<p>You are given an integer array <code>nums</code>.</p> <p>Return an integer that is the <strong>maximum</strong> distance between the <strong>indices</strong> of two (not necessarily different) prime numbers in <code>nums</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,5,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> <code>nums[1]</code>, <code>nums[3]</code>, and <code>nums[4]</code> are prime. So the answer is <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,2,8]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> <code>nums[2]</code> is prime. Because there is just one prime number, the answer is <code>|2 - 2| = 0</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> <li>The input is generated such that the number of prime numbers in the <code>nums</code> is at least one.</li> </ul>
2
{ "code": "class Solution {\npublic:\n\n bool isPrime(int n)\n {\n if (n <= 1) \n return false;\n if (n <= 3)\n return true;\n if (n % 2 == 0 || n % 3 == 0)\n return false;\n for (int i = 5; i * i <= n; i += 6)\n if (n % i == 0 || n % (i + 2) == 0)\n return false;\n return true;\n }\n\n int maximumPrimeDifference(vector<int>& nums) {\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n int l, r;\n for(int i = 0; i < nums.size(); i++)\n if(isPrime(nums[i]))\n {\n l = i;\n break;\n }\n for(int i = nums.size() - 1; i >= 0; i--)\n if(isPrime(nums[i]))\n {\n r = i;\n break;\n }\n return r - l;\n }\n};", "memory": "108600" }
3,373
<p>You are given an integer array <code>nums</code>.</p> <p>Return an integer that is the <strong>maximum</strong> distance between the <strong>indices</strong> of two (not necessarily different) prime numbers in <code>nums</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,5,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> <code>nums[1]</code>, <code>nums[3]</code>, and <code>nums[4]</code> are prime. So the answer is <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,2,8]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> <code>nums[2]</code> is prime. Because there is just one prime number, the answer is <code>|2 - 2| = 0</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> <li>The input is generated such that the number of prime numbers in the <code>nums</code> is at least one.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumPrimeDifference(vector<int>& nums) {\n vector<bool> primes(101,1);\n primes[0]=primes[1]=0;\n for(int i=2; i<=100; i++){\n if(primes[i]){\n for(int j=i*2; j<=100; j+=i){\n primes[j]=0;\n }\n }\n }\n int n=nums.size();\n vector<bool> mark(n,0);\n for(int i=0; i<n; i++){\n if(primes[nums[i]]){\n mark[i]=1;\n }\n }\n int ans=0;\n int left=0,right=0;\n for(int i=0; i<n; i++){\n if(mark[i]==1){\n left=(i+1);\n break;\n }\n }\n for(int i=n-1; i>=0; i--){\n if(mark[i]==1){\n right=(i+1);\n break;\n }\n }\n return right-left;\n }\n};", "memory": "108700" }
3,373
<p>You are given an integer array <code>nums</code>.</p> <p>Return an integer that is the <strong>maximum</strong> distance between the <strong>indices</strong> of two (not necessarily different) prime numbers in <code>nums</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,5,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> <code>nums[1]</code>, <code>nums[3]</code>, and <code>nums[4]</code> are prime. So the answer is <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,2,8]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> <code>nums[2]</code> is prime. Because there is just one prime number, the answer is <code>|2 - 2| = 0</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> <li>The input is generated such that the number of prime numbers in the <code>nums</code> is at least one.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumPrimeDifference(vector<int>& arr) \n {\n int n=arr.size();\n int is_prime[105];\n for(int i=0;i<=101;i++)is_prime[i]=1;\n is_prime[0]=0;\n is_prime[1]=0;\n for(int i=2;i<=101;i++)\n {\n if(is_prime[i])\n {\n for(int j=i*i;j<=101;j+=i)is_prime[j]=0;\n }\n }\n\n int i=0;int j=n-1;\n while(i<=j)\n {\n if(is_prime[arr[i]] && is_prime[arr[j]])\n {\n break;\n }\n else if(is_prime[arr[i]])j--;\n else i++;\n }\n if(i>j) return 0;\n else return j-i;\n } \n};", "memory": "108800" }
3,373
<p>You are given an integer array <code>nums</code>.</p> <p>Return an integer that is the <strong>maximum</strong> distance between the <strong>indices</strong> of two (not necessarily different) prime numbers in <code>nums</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,5,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> <code>nums[1]</code>, <code>nums[3]</code>, and <code>nums[4]</code> are prime. So the answer is <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,2,8]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> <code>nums[2]</code> is prime. Because there is just one prime number, the answer is <code>|2 - 2| = 0</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> <li>The input is generated such that the number of prime numbers in the <code>nums</code> is at least one.</li> </ul>
2
{ "code": "class Solution {\npublic:\nbool isPrime(int n) {\n if (n < 2) {\n return false;\n }\n if (n == 2) {\n return true;\n }\n if (n % 2 == 0) {\n return false;\n }\n int sqrt_n = static_cast<int>(sqrt(n));\n for (int i = 3; i <= sqrt_n; i += 2) {\n if (n % i == 0) {\n return false;\n }\n }\n return true;\n}\n\nint maximumPrimeDifference(vector<int>& nums) {\n int size = nums.size();\n int left = -1, right = -1;\n unordered_set<int> mySet;\n for (int i = 0; i < size; i++) {\n\n if ( mySet.find(nums[i]) != mySet.end() || isPrime(nums[i])) {\n if (left == -1) {\n left = i;\n }\n right = i;\n mySet.insert(nums[i]);\n }\n }\n \n if (left == -1) { // No prime numbers found\n return 0;\n }\n \n return right - left;\n}\n};", "memory": "109400" }
3,373
<p>You are given an integer array <code>nums</code>.</p> <p>Return an integer that is the <strong>maximum</strong> distance between the <strong>indices</strong> of two (not necessarily different) prime numbers in <code>nums</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,5,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> <code>nums[1]</code>, <code>nums[3]</code>, and <code>nums[4]</code> are prime. So the answer is <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,2,8]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> <code>nums[2]</code> is prime. Because there is just one prime number, the answer is <code>|2 - 2| = 0</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> <li>The input is generated such that the number of prime numbers in the <code>nums</code> is at least one.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumPrimeDifference(vector<int>& nums) {\n fillPrime(100, _setPrime);\n\n int posMin=-1, posMax=-1;\n for(int idx=0; idx<nums.size(); ++idx){\n if(_setPrime.contains(nums[idx])){\n if (posMin==-1) posMin = idx;\n posMax = idx;\n }\n }\n return posMax - posMin;\n }\nprivate:\n set<int> _setPrime;\n void fillPrime(int bound, set<int> &setPrime){\n for(int i=0; i<=bound; ++i)\n if(isPrime(i)) setPrime.insert(i);\n }\n bool isPrime(int number){\n if(number < 2) return false;\n if(number == 2) return true;\n if(number % 2 == 0) return false;\n for(int i=3; (i*i)<=number; i+=2){\n if(number % i == 0 ) return false;\n }\n return true;\n }\n};", "memory": "109500" }
3,373
<p>You are given an integer array <code>nums</code>.</p> <p>Return an integer that is the <strong>maximum</strong> distance between the <strong>indices</strong> of two (not necessarily different) prime numbers in <code>nums</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,5,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> <code>nums[1]</code>, <code>nums[3]</code>, and <code>nums[4]</code> are prime. So the answer is <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,2,8]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> <code>nums[2]</code> is prime. Because there is just one prime number, the answer is <code>|2 - 2| = 0</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> <li>The input is generated such that the number of prime numbers in the <code>nums</code> is at least one.</li> </ul>
2
{ "code": "class Solution {\npublic:\n\n set<int> primes;\n\n void initPrime(int n){\n\n for(int i = 2; i <= n; i++){\n bool isPrime = true;\n for(int j = 2; j <= sqrt(i); j++){\n if(i % j == 0){\n isPrime = false;\n break;\n }\n }\n if(isPrime){\n this->primes.insert(i);\n cout << \" \" << i << \" \";\n }\n }\n\n }\n\n int maximumPrimeDifference(vector<int>& nums) {\n\n // all the primes below 100\n initPrime(100);\n \n int min = -1, max = -1;\n\n for(int i = 0; i < nums.size(); i++){\n\n if(this->primes.find(nums[i]) != this->primes.end()){\n if(min == -1){\n min = i;\n }else{\n max = i;\n }\n }\n\n }\n\n if(max == -1){\n return 0;\n }\n\n return max - min;\n }\n};", "memory": "109700" }
3,373
<p>You are given an integer array <code>nums</code>.</p> <p>Return an integer that is the <strong>maximum</strong> distance between the <strong>indices</strong> of two (not necessarily different) prime numbers in <code>nums</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,5,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> <code>nums[1]</code>, <code>nums[3]</code>, and <code>nums[4]</code> are prime. So the answer is <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,2,8]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> <code>nums[2]</code> is prime. Because there is just one prime number, the answer is <code>|2 - 2| = 0</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> <li>The input is generated such that the number of prime numbers in the <code>nums</code> is at least one.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumPrimeDifference(vector<int>& nums) {\n set <int> primes ={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};\n int l=-1,h=-1;\n for(int i=0; i<nums.size(); i++){\n if(primes.count(nums[i])){\n h=i;\n if(l==-1){\n l=i;\n }\n }\n }\n return h-l;\n }\n};", "memory": "110000" }
3,373
<p>You are given an integer array <code>nums</code>.</p> <p>Return an integer that is the <strong>maximum</strong> distance between the <strong>indices</strong> of two (not necessarily different) prime numbers in <code>nums</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,5,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> <code>nums[1]</code>, <code>nums[3]</code>, and <code>nums[4]</code> are prime. So the answer is <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,2,8]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> <code>nums[2]</code> is prime. Because there is just one prime number, the answer is <code>|2 - 2| = 0</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> <li>The input is generated such that the number of prime numbers in the <code>nums</code> is at least one.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumPrimeDifference(vector<int>& nums) {\n set <int> primes ={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};\n int l=-1,h=-1;\n for(int i=0; i<nums.size(); i++){\n if(primes.count(nums[i])){\n h=i;\n if(l==-1){\n l=i;\n }\n }\n }\n return h-l;\n }\n};", "memory": "110100" }
3,373
<p>You are given an integer array <code>nums</code>.</p> <p>Return an integer that is the <strong>maximum</strong> distance between the <strong>indices</strong> of two (not necessarily different) prime numbers in <code>nums</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,5,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> <code>nums[1]</code>, <code>nums[3]</code>, and <code>nums[4]</code> are prime. So the answer is <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,2,8]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> <code>nums[2]</code> is prime. Because there is just one prime number, the answer is <code>|2 - 2| = 0</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> <li>The input is generated such that the number of prime numbers in the <code>nums</code> is at least one.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumPrimeDifference(vector<int>& nums) {\n int n = nums.size();\n set<int> st = {2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 , 41 , 43 , 47 , \n 53 , 59 , 61 , 67 , 71 , 73 , 79 , 83 , 89 , 97};\n \n int first = -1;\n int last = -1;\n\n for(int i=0 ; i<n ; i++){\n if(st.count(nums[i])){\n if(first == -1){\n first = i;\n }\n else last = i;\n }\n }\n\n return last == -1 ? 0 : last-first;\n \n }\n};", "memory": "110200" }
3,373
<p>You are given an integer array <code>nums</code>.</p> <p>Return an integer that is the <strong>maximum</strong> distance between the <strong>indices</strong> of two (not necessarily different) prime numbers in <code>nums</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,5,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> <code>nums[1]</code>, <code>nums[3]</code>, and <code>nums[4]</code> are prime. So the answer is <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,2,8]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> <code>nums[2]</code> is prime. Because there is just one prime number, the answer is <code>|2 - 2| = 0</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> <li>The input is generated such that the number of prime numbers in the <code>nums</code> is at least one.</li> </ul>
2
{ "code": "class Solution {\nprivate:\n bool is_prime(int number)\n {\n if(number == 0 || number == 1)\n {\n return false;\n }\n if(number == 2 || number == 3)\n {\n return true;\n }\n for(int i = 2 ; i <= number/2 ; i++)\n {\n if(number % i == 0 )\n {\n return false;\n }\n }\n return true;\n }\npublic:\n int maximumPrimeDifference(vector<int>& nums)\n {\n int leng = nums.size();\n stack<int> stacker;\n for(int i = 0 ; i < leng ; i++)\n {\n if(is_prime(nums[i]))\n {\n stacker.push(i);\n }\n }\n if(stacker.size() == 0 || stacker.size() == 1)\n {\n return 0;\n }\n int max = stacker.top();\n int min ;\n while(!stacker.empty())\n {\n min = stacker.top();\n stacker.pop();\n }\n return max - min;\n }\n};", "memory": "110300" }
3,373
<p>You are given an integer array <code>nums</code>.</p> <p>Return an integer that is the <strong>maximum</strong> distance between the <strong>indices</strong> of two (not necessarily different) prime numbers in <code>nums</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,5,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> <code>nums[1]</code>, <code>nums[3]</code>, and <code>nums[4]</code> are prime. So the answer is <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,2,8]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> <code>nums[2]</code> is prime. Because there is just one prime number, the answer is <code>|2 - 2| = 0</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> <li>The input is generated such that the number of prime numbers in the <code>nums</code> is at least one.</li> </ul>
2
{ "code": "class Solution {\npublic:\n void is_prime(unordered_set<int> &prime){\n prime.insert(2);\n for(int x=3; x<=100; x++){\n bool flag= true;\n for(int i= 2; i<= (x/2); i++){\n if(x % i ==0){\n flag= false;\n break;\n }\n }\n\n if(flag){\n prime.insert(x);\n }\n }\n }\n\n int maximumPrimeDifference(vector<int>& nums) {\n unordered_set<int> prime;\n is_prime(prime);\n\n int f=-1, s=-1;\n for(int i=0; i<nums.size(); i++){\n if(prime.find(nums[i]) != prime.end()){\n // cout<<i<<endl;\n if(f== -1){\n f=i;\n s=i;\n }\n else{\n s=i;\n }\n }\n }\n return s-f;\n }\n};", "memory": "110400" }
3,373
<p>You are given an integer array <code>nums</code>.</p> <p>Return an integer that is the <strong>maximum</strong> distance between the <strong>indices</strong> of two (not necessarily different) prime numbers in <code>nums</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,5,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> <code>nums[1]</code>, <code>nums[3]</code>, and <code>nums[4]</code> are prime. So the answer is <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,2,8]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> <code>nums[2]</code> is prime. Because there is just one prime number, the answer is <code>|2 - 2| = 0</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> <li>The input is generated such that the number of prime numbers in the <code>nums</code> is at least one.</li> </ul>
2
{ "code": "class Solution {\npublic:\n // Maximum value can be 100 so store all the prime numbers between 1 to 100.\n bool isPrime(int n) {\n if (n <= 1) return false; \n if (n <= 3) return true; \n if (n % 2 == 0 || n % 3 == 0) return false;\n\n for (int i = 5; i * i <= n; i += 6) {\n if (n % i == 0 || n % (i + 2) == 0) return false;\n }\n return true;\n }\n int maximumPrimeDifference(vector<int>& nums) {\n unordered_set<int> st;\n for(int i = 2; i <= 100; i++) {\n if(isPrime(i)) {\n st.insert(i);\n }\n }\n int i = 0, start, end, n = nums.size();\n for(i = 0; i < nums.size(); i++) {\n if(st.find(nums[i]) != st.end()) {\n start = i;\n break;\n }\n }\n for(int j = n-1; j >= i; j--) {\n if(st.find(nums[j]) != st.end()) {\n end = j;\n break;\n }\n }\n return end-start;\n }\n};", "memory": "110500" }
3,373
<p>You are given an integer array <code>nums</code>.</p> <p>Return an integer that is the <strong>maximum</strong> distance between the <strong>indices</strong> of two (not necessarily different) prime numbers in <code>nums</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,5,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> <code>nums[1]</code>, <code>nums[3]</code>, and <code>nums[4]</code> are prime. So the answer is <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,2,8]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> <code>nums[2]</code> is prime. Because there is just one prime number, the answer is <code>|2 - 2| = 0</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> <li>The input is generated such that the number of prime numbers in the <code>nums</code> is at least one.</li> </ul>
2
{ "code": "class Solution {\npublic:\n unordered_set<int> getAllPrimes(int n){\n unordered_set<int> s;\n for(int i=2;i<=n;i++){\n bool t = true;\n for(int j=2;j<i;j++){\n if(i%j==0){\n t=false;\n break;\n }\n }\n if(t)\n s.insert(i);\n }\n return s;\n }\n int maximumPrimeDifference(vector<int>& nums) {\n unordered_set<int> s = getAllPrimes(100);\n /*for(auto itr=s.begin();itr!=s.end();itr++){\n cout<<*itr<<\" \";\n }*/\n cout<<endl;\n int x;\n int y;\n for(int i=0;i<nums.size();i++){\n if(s.find(nums[i])!=s.end()){\n x = i;\n break;\n }\n }\n for(int i=nums.size()-1;i>=0;i--){\n if(s.find(nums[i])!=s.end()){\n y = i;\n break;\n }\n }\n return y-x;\n }\n};", "memory": "110600" }
3,373
<p>You are given an integer array <code>nums</code>.</p> <p>Return an integer that is the <strong>maximum</strong> distance between the <strong>indices</strong> of two (not necessarily different) prime numbers in <code>nums</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,5,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> <code>nums[1]</code>, <code>nums[3]</code>, and <code>nums[4]</code> are prime. So the answer is <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,2,8]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> <code>nums[2]</code> is prime. Because there is just one prime number, the answer is <code>|2 - 2| = 0</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> <li>The input is generated such that the number of prime numbers in the <code>nums</code> is at least one.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumPrimeDifference(vector<int>& nums) {\n\n vector<int> v;\n\n for (int i = 0; i < nums.size(); i++) {\n if(nums[i]==1) continue;\n int flag = 1;\n for (int j = 1; j < nums[i]; j++) {\n if(j==1) continue;\n if (nums[i] % j == 0) {\n flag=0;\n break;\n }\n }\n if (flag == 1) {\n v.push_back(i);\n }\n }\n\n if (v.size()<2)\n return 0;\n\n else\n return v[v.size() - 1] - v[0];\n }\n};", "memory": "110700" }
3,373
<p>You are given an integer array <code>nums</code>.</p> <p>Return an integer that is the <strong>maximum</strong> distance between the <strong>indices</strong> of two (not necessarily different) prime numbers in <code>nums</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,5,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> <code>nums[1]</code>, <code>nums[3]</code>, and <code>nums[4]</code> are prime. So the answer is <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,2,8]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> <code>nums[2]</code> is prime. Because there is just one prime number, the answer is <code>|2 - 2| = 0</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> <li>The input is generated such that the number of prime numbers in the <code>nums</code> is at least one.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumPrimeDifference(vector<int>& nums) {\n\n vector<int> v;\n\n for (int i = 0; i < nums.size(); i++) {\n if(nums[i]==1) continue;\n int flag = 1;\n for (int j = 1; j * j <= nums[i]; j++) {\n if(j==1) continue;\n if (nums[i] % j == 0) {\n flag=0;\n break;\n }\n }\n if (flag == 1) {\n v.push_back(i);\n }\n }\n\n if (v.size()<2)\n return 0;\n\n else\n return v[v.size() - 1] - v[0];\n }\n};", "memory": "110800" }
3,373
<p>You are given an integer array <code>nums</code>.</p> <p>Return an integer that is the <strong>maximum</strong> distance between the <strong>indices</strong> of two (not necessarily different) prime numbers in <code>nums</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,5,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> <code>nums[1]</code>, <code>nums[3]</code>, and <code>nums[4]</code> are prime. So the answer is <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,2,8]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> <code>nums[2]</code> is prime. Because there is just one prime number, the answer is <code>|2 - 2| = 0</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> <li>The input is generated such that the number of prime numbers in the <code>nums</code> is at least one.</li> </ul>
3
{ "code": "#include <vector>\n#include <cmath>\n#include <algorithm>\nusing namespace std;\n\nclass Solution {\npublic:\n bool isPrime(int n) {\n if (n <= 1) return false;\n if (n <= 3) return true;\n if (n % 2 == 0 || n % 3 == 0) return false;\n for (int i = 5; i * i <= n; i += 6) {\n if (n % i == 0 || n % (i + 2) == 0) return false;\n }\n return true;\n }\n\n int maximumPrimeDifference(vector<int>& nums) {\n vector<int> primeIndices;\n for (int i = 0; i < nums.size(); ++i) {\n if (isPrime(nums[i])) {\n primeIndices.push_back(i);\n }\n }\n if (primeIndices.size() < 2) return 0;\n return primeIndices.back() - primeIndices.front();\n }\n};", "memory": "110900" }
3,373
<p>You are given an integer array <code>nums</code>.</p> <p>Return an integer that is the <strong>maximum</strong> distance between the <strong>indices</strong> of two (not necessarily different) prime numbers in <code>nums</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,5,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> <code>nums[1]</code>, <code>nums[3]</code>, and <code>nums[4]</code> are prime. So the answer is <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,2,8]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> <code>nums[2]</code> is prime. Because there is just one prime number, the answer is <code>|2 - 2| = 0</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> <li>The input is generated such that the number of prime numbers in the <code>nums</code> is at least one.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int maximumPrimeDifference(vector<int>& nums) {\n\n vector<int> v;\n\n for (int i = 0; i < nums.size(); i++) {\n if(nums[i]==1) continue;\n int flag = 1;\n for (int j = 2; j*j <= nums[i]; j++) {\n \n if (nums[i] % j == 0) {\n flag=0;\n break;\n }\n }\n if (flag == 1) {\n v.push_back(i);\n }\n }\n\n if (v.size()<2)\n return 0;\n\n else\n return v[v.size() - 1] - v[0];\n }\n};", "memory": "110900" }
3,373
<p>You are given an integer array <code>nums</code>.</p> <p>Return an integer that is the <strong>maximum</strong> distance between the <strong>indices</strong> of two (not necessarily different) prime numbers in <code>nums</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,5,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> <code>nums[1]</code>, <code>nums[3]</code>, and <code>nums[4]</code> are prime. So the answer is <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,2,8]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> <code>nums[2]</code> is prime. Because there is just one prime number, the answer is <code>|2 - 2| = 0</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> <li>The input is generated such that the number of prime numbers in the <code>nums</code> is at least one.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int maximumPrimeDifference(vector<int>& nums) {\n ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);\n int first = 0, last = nums.size() - 1, size = nums.size();\n for (first; first < size; first++) if (prime.count(nums[first])) break;\n for (last; last >= 0; last--) if (prime.count(nums[last])) break;\n\n return last - first;\n }\n\nprivate:\n set<int> prime = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59,\n 61, 67, 71, 73, 79, 83, 89, 97};\n};", "memory": "111000" }
3,373
<p>You are given an integer array <code>nums</code>.</p> <p>Return an integer that is the <strong>maximum</strong> distance between the <strong>indices</strong> of two (not necessarily different) prime numbers in <code>nums</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,5,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> <code>nums[1]</code>, <code>nums[3]</code>, and <code>nums[4]</code> are prime. So the answer is <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,2,8]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> <code>nums[2]</code> is prime. Because there is just one prime number, the answer is <code>|2 - 2| = 0</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> <li>The input is generated such that the number of prime numbers in the <code>nums</code> is at least one.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int maximumPrimeDifference(vector<int>& nums) {\n int N = 100;\n vector<int>spf(N+1);\n for(int i =2;i<=N;i++){\n spf[i] = i;\n }\n for(int i =2;i<=N;i++){\n if(spf[i] == i){\n for(int j = i*i;j<=N;j+=i){\n if(spf[j] == j){\n spf[j] = i;\n }\n }\n }\n }\n // for(int i =0;i<=100;i++)cout<<spf[i]<<\" \";\n vector<int>ans;\n for(int i =0;i<nums.size();i++){\n if(spf[nums[i]] == nums[i]){\n ans.push_back(i);\n }\n }\n // for(int i =0;i<ans.size();i++)cout<<ans[i]<<\" \";\n int finalans = ans[ans.size()-1] - ans[0];\n return finalans;\n }\n};", "memory": "111300" }
3,373
<p>You are given an integer array <code>nums</code>.</p> <p>Return an integer that is the <strong>maximum</strong> distance between the <strong>indices</strong> of two (not necessarily different) prime numbers in <code>nums</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,5,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> <code>nums[1]</code>, <code>nums[3]</code>, and <code>nums[4]</code> are prime. So the answer is <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,2,8]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> <code>nums[2]</code> is prime. Because there is just one prime number, the answer is <code>|2 - 2| = 0</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> <li>The input is generated such that the number of prime numbers in the <code>nums</code> is at least one.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int maximumPrimeDifference(vector<int>& nums) {\n int ma = 0;\n for(int i : nums){\n ma = max(ma , i);\n }\n vector<bool> primes(ma + 1 , false);\n\n for(int i=2;i<=ma;i++){\n if(!primes[i]){\n for(int j = 2 ; i*j <=ma ; j++){\n primes[i*j] = true;\n }\n }\n }\n vector<int> index;\n for(int o =0 ;o<nums.size();o++){\n if(!primes[nums[o]]){\n if(nums[o] == 0 || nums[o] == 1){\n continue;\n }\n index.push_back(o);\n }\n }\n return index[index.size()-1] - index[0];\n }\n};", "memory": "111400" }
3,373
<p>You are given an integer array <code>nums</code>.</p> <p>Return an integer that is the <strong>maximum</strong> distance between the <strong>indices</strong> of two (not necessarily different) prime numbers in <code>nums</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,5,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> <code>nums[1]</code>, <code>nums[3]</code>, and <code>nums[4]</code> are prime. So the answer is <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,2,8]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> <code>nums[2]</code> is prime. Because there is just one prime number, the answer is <code>|2 - 2| = 0</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> <li>The input is generated such that the number of prime numbers in the <code>nums</code> is at least one.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int maximumPrimeDifference(vector<int>& nums) {\n int p=*max_element(nums.begin(),nums.end());\n vector<int>seive(p+1,0);\n seive[0]=1,seive[1]=1;\n for(int i=2;i*i<=p;i++){\n if(seive[i]==0){\n for(int j=i*i;j<=p;j+=i){\n if(seive[j]==0)\n seive[j]=1;\n }\n }\n }\n vector<int>a;\n for(int i=0;i<nums.size();i++){\n if(seive[nums[i]]==0){\n a.push_back(i);\n }\n }\n return a[a.size()-1]-a[0];\n }\n};", "memory": "111500" }
3,373
<p>You are given an integer array <code>nums</code>.</p> <p>Return an integer that is the <strong>maximum</strong> distance between the <strong>indices</strong> of two (not necessarily different) prime numbers in <code>nums</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,5,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> <code>nums[1]</code>, <code>nums[3]</code>, and <code>nums[4]</code> are prime. So the answer is <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,2,8]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> <code>nums[2]</code> is prime. Because there is just one prime number, the answer is <code>|2 - 2| = 0</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> <li>The input is generated such that the number of prime numbers in the <code>nums</code> is at least one.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool isPrime(int n){\n if(n == 1) return false;\n for(int i=2;i<n;i++){\n if(n%i == 0) return false;\n }\n return true;\n }\n int maximumPrimeDifference(vector<int>& nums) {\n vector <int> v;\n\n for(int i=0;i<nums.size();i++){\n if(isPrime(nums[i])){\n v.push_back(i);\n }\n }\n\n for(auto x : v) cout<<x<<\" \";\n cout<<endl;\n\n sort(v.begin(),v.end());\n\n if(v.size() <= 1) return 0;\n else return abs(v[0] - v[v.size()-1]);\n }\n};", "memory": "111600" }
3,373
<p>You are given an integer array <code>nums</code>.</p> <p>Return an integer that is the <strong>maximum</strong> distance between the <strong>indices</strong> of two (not necessarily different) prime numbers in <code>nums</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,5,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> <code>nums[1]</code>, <code>nums[3]</code>, and <code>nums[4]</code> are prime. So the answer is <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,2,8]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> <code>nums[2]</code> is prime. Because there is just one prime number, the answer is <code>|2 - 2| = 0</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> <li>The input is generated such that the number of prime numbers in the <code>nums</code> is at least one.</li> </ul>
3
{ "code": "class Solution {\npublic:\nbool checkprime(long long n){\n if(n<=1)return false;\n for(int i =2;i*i<=n;i++){\n if(n%i==0){\n return false;\n }\n }\n return true;\n\n}\n int maximumPrimeDifference(vector<int>& nums) {\n vector<int>ans;\n for(int i =0;i<nums.size();i++){\n if(checkprime(nums[i])){\n ans.push_back(i);\n }\n }\n sort(ans.begin(),ans.end());\n return abs(ans[0]-ans[ans.size()-1]);\n\n \n \n }\n};", "memory": "111700" }
3,373
<p>You are given an integer array <code>nums</code>.</p> <p>Return an integer that is the <strong>maximum</strong> distance between the <strong>indices</strong> of two (not necessarily different) prime numbers in <code>nums</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,5,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> <code>nums[1]</code>, <code>nums[3]</code>, and <code>nums[4]</code> are prime. So the answer is <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,2,8]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> <code>nums[2]</code> is prime. Because there is just one prime number, the answer is <code>|2 - 2| = 0</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> <li>The input is generated such that the number of prime numbers in the <code>nums</code> is at least one.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int maximumPrimeDifference(vector<int>& nums) \n {\n vector<int> arr;\n for(int i=0;i<nums.size();i++)\n {\n if(nums[i]!=1 && ((nums[i]==2 || nums[i]==3 || nums[i]==5 || nums[i]==7) ||nums[i]%2!=0 && nums[i]%3!=0 && nums[i]%5!=0 && nums[i]%7!=0))\n {\n arr.push_back(i);\n }\n }\n if(arr.size()==1 || arr.size()==0)\n {\n return 0;\n }\n else\n {\n sort(begin(arr),end(arr));\n return arr[arr.size()-1]-arr[0];\n }\n }\n};", "memory": "111800" }
3,375
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>k</code>.</p> <p>You have an infinite number of coins of each denomination. However, you are <strong>not allowed</strong> to combine coins of different denominations.</p> <p>Return the <code>k<sup>th</sup></code> <strong>smallest</strong> amount that can be made using these coins.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; ">coins = [3,6,9], k = 3</span></p> <p><strong>Output:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 9</span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 3 produces multiples of 3: 3, 6, 9, 12, 15, etc.<br /> Coin 6 produces multiples of 6: 6, 12, 18, 24, etc.<br /> Coin 9 produces multiples of 9: 9, 18, 27, 36, etc.<br /> All of the coins combined produce: 3, 6, <u><strong>9</strong></u>, 12, 15, etc.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> coins = [5,2], k = 7</span></p> <p><strong>Output:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 12 </span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 5 produces multiples of 5: 5, 10, 15, 20, etc.<br /> Coin 2 produces multiples of 2: 2, 4, 6, 8, 10, 12, etc.<br /> All of the coins combined produce: 2, 4, 5, 6, 8, 10, <u><strong>12</strong></u>, 14, 15, etc.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 15</code></li> <li><code>1 &lt;= coins[i] &lt;= 25</code></li> <li><code>1 &lt;= k &lt;= 2 * 10<sup>9</sup></code></li> <li><code>coins</code> contains pairwise distinct integers.</li> </ul>
0
{ "code": "// ABHI\ntypedef long long int ll;\nclass Solution {\npublic:\n ll count(vector<int>& coins,ll mid){\n ll ans = 0;\n int n = coins.size();\n for(int i=1;i<(1<<n);i++){\n int cnt = __builtin_popcount(i);\n ll g = 1;\n for(int j=0;j<n;j++){\n if((i>>j)&1)g = lcm(g,coins[j]); \n }\n if(cnt&1)ans+=(mid/g);\n else ans-=(mid/g);\n }\n return ans;\n }\n long long findKthSmallest(vector<int>& coins, int k) {\n ll l=1,r=*max_element(coins.begin(),coins.end())*1LL*k;\n ll ans = -1;\n while(l<=r){\n ll mid = l+(r-l)/2;\n if(count(coins,mid)>=k){\n ans=mid;\n r=mid-1;\n }else l=mid+1;\n }\n return ans;\n }\n};", "memory": "19900" }
3,375
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>k</code>.</p> <p>You have an infinite number of coins of each denomination. However, you are <strong>not allowed</strong> to combine coins of different denominations.</p> <p>Return the <code>k<sup>th</sup></code> <strong>smallest</strong> amount that can be made using these coins.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; ">coins = [3,6,9], k = 3</span></p> <p><strong>Output:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 9</span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 3 produces multiples of 3: 3, 6, 9, 12, 15, etc.<br /> Coin 6 produces multiples of 6: 6, 12, 18, 24, etc.<br /> Coin 9 produces multiples of 9: 9, 18, 27, 36, etc.<br /> All of the coins combined produce: 3, 6, <u><strong>9</strong></u>, 12, 15, etc.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> coins = [5,2], k = 7</span></p> <p><strong>Output:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 12 </span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 5 produces multiples of 5: 5, 10, 15, 20, etc.<br /> Coin 2 produces multiples of 2: 2, 4, 6, 8, 10, 12, etc.<br /> All of the coins combined produce: 2, 4, 5, 6, 8, 10, <u><strong>12</strong></u>, 14, 15, etc.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 15</code></li> <li><code>1 &lt;= coins[i] &lt;= 25</code></li> <li><code>1 &lt;= k &lt;= 2 * 10<sup>9</sup></code></li> <li><code>coins</code> contains pairwise distinct integers.</li> </ul>
0
{ "code": "class Solution {\npublic:\n long long gcd(long long a,long long b){\n if(a==0)return b;\n if(b==0)return a;\n if(a>b)return gcd(a%b,b);\n return gcd(b%a,a);\n }\n\n void give(long long curr,long long i,long long taken,long long &cnt,vector<int> &coins,long long k){\n if(i==(long long)coins.size()){\n if(curr!=0){\n if(taken%2!=0){\n cnt+=(k/curr);\n }\n else{\n cnt-=(k/curr);\n }\n }\n return;\n\n }\n if(curr==0){\n give((long long)coins[i],i+1,taken+1,cnt,coins,k);\n give(curr,i+1,taken,cnt,coins,k);\n }\n else{\n long long g=gcd(curr,(long long)coins[i]);\n long long l=(curr*(long long)coins[i])/(g);\n give(l,i+1,taken+1,cnt,coins,k);\n give(curr,i+1,taken,cnt,coins,k);\n\n }\n }\n long long findKthSmallest(vector<int>& coins, int k) {\n long long n=coins.size();\n long long low=0;\n long long high=1e11;long long ans=-1;\n while(high>=low){\n long long mid=(high+low)/2;\n long long cnt=0;\n give(0LL,0,0,cnt,coins,mid);\n if(cnt>=k){\n ans=mid;\n high=mid-1;\n }else{\n low=mid+1;\n }\n\n\n }\n return ans;\n\n }\n};", "memory": "20000" }
3,375
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>k</code>.</p> <p>You have an infinite number of coins of each denomination. However, you are <strong>not allowed</strong> to combine coins of different denominations.</p> <p>Return the <code>k<sup>th</sup></code> <strong>smallest</strong> amount that can be made using these coins.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; ">coins = [3,6,9], k = 3</span></p> <p><strong>Output:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 9</span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 3 produces multiples of 3: 3, 6, 9, 12, 15, etc.<br /> Coin 6 produces multiples of 6: 6, 12, 18, 24, etc.<br /> Coin 9 produces multiples of 9: 9, 18, 27, 36, etc.<br /> All of the coins combined produce: 3, 6, <u><strong>9</strong></u>, 12, 15, etc.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> coins = [5,2], k = 7</span></p> <p><strong>Output:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 12 </span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 5 produces multiples of 5: 5, 10, 15, 20, etc.<br /> Coin 2 produces multiples of 2: 2, 4, 6, 8, 10, 12, etc.<br /> All of the coins combined produce: 2, 4, 5, 6, 8, 10, <u><strong>12</strong></u>, 14, 15, etc.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 15</code></li> <li><code>1 &lt;= coins[i] &lt;= 25</code></li> <li><code>1 &lt;= k &lt;= 2 * 10<sup>9</sup></code></li> <li><code>coins</code> contains pairwise distinct integers.</li> </ul>
0
{ "code": "class Solution {\npublic:\n long long cnt(long long n, vector<int>&coins, int k) {\n\n int m = coins.size();\n int s = 1<<m;\n long long total = 0;\n\n for(int i=1; i<s; i++) {\n\n long long l = 1;\n long long cnt = 0;\n\n for(int j=0; j<m; j++) {\n\n if(i & (1<<j)) {\n l = lcm(l, coins[j]);\n cnt++;\n }\n }\n\n if(cnt & 1) {\n total += (n/l);\n }\n\n else {\n total -= (n/l);\n }\n }\n\n return total >= k;\n }\n long long findKthSmallest(vector<int>& coins, int k) {\n \n long long l = 1, r = 1e11;\n\n while(l < r) {\n\n long long mid = l + ((r-l) >> 1);\n\n if(cnt(mid, coins, k)){\n r = mid;\n }\n\n else l = mid+1;\n }\n return l;\n }\n}; ", "memory": "20100" }
3,375
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>k</code>.</p> <p>You have an infinite number of coins of each denomination. However, you are <strong>not allowed</strong> to combine coins of different denominations.</p> <p>Return the <code>k<sup>th</sup></code> <strong>smallest</strong> amount that can be made using these coins.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; ">coins = [3,6,9], k = 3</span></p> <p><strong>Output:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 9</span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 3 produces multiples of 3: 3, 6, 9, 12, 15, etc.<br /> Coin 6 produces multiples of 6: 6, 12, 18, 24, etc.<br /> Coin 9 produces multiples of 9: 9, 18, 27, 36, etc.<br /> All of the coins combined produce: 3, 6, <u><strong>9</strong></u>, 12, 15, etc.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> coins = [5,2], k = 7</span></p> <p><strong>Output:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 12 </span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 5 produces multiples of 5: 5, 10, 15, 20, etc.<br /> Coin 2 produces multiples of 2: 2, 4, 6, 8, 10, 12, etc.<br /> All of the coins combined produce: 2, 4, 5, 6, 8, 10, <u><strong>12</strong></u>, 14, 15, etc.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 15</code></li> <li><code>1 &lt;= coins[i] &lt;= 25</code></li> <li><code>1 &lt;= k &lt;= 2 * 10<sup>9</sup></code></li> <li><code>coins</code> contains pairwise distinct integers.</li> </ul>
0
{ "code": "#define ll long long int\nclass Solution {\nprivate:\n ll cnt_less_than_eql_x(vector<int>& coins, ll x){\n int midx = coins.size();\n int all_ones = (1 << midx) - 1;\n ll cnt = 0;\n for(int mask = 1; mask <= all_ones; mask++){\n ll set_bits_lcm = 1;\n for(int j = 0; j < midx; j++){\n if(mask & (1 << j))\n set_bits_lcm = lcm(set_bits_lcm, coins[j]);\n }\n if(__builtin_popcount(mask) & 1)\n cnt += x / set_bits_lcm;\n else\n cnt -= x / set_bits_lcm;\n }\n return cnt;\n }\n\npublic:\n long long findKthSmallest(vector<int>& coins, int k) {\n ll l = 1, r = 25 * 2 * 1e9 + 25, ans = 0;\n\n while(l <= r){\n ll midx = (l + r)/2;\n ll cnt = cnt_less_than_eql_x(coins, midx);\n if(cnt < k)\n l = midx + 1;\n else{\n ans = midx;\n r = midx - 1;\n }\n }\n\n return ans;\n }\n};", "memory": "20200" }
3,375
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>k</code>.</p> <p>You have an infinite number of coins of each denomination. However, you are <strong>not allowed</strong> to combine coins of different denominations.</p> <p>Return the <code>k<sup>th</sup></code> <strong>smallest</strong> amount that can be made using these coins.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; ">coins = [3,6,9], k = 3</span></p> <p><strong>Output:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 9</span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 3 produces multiples of 3: 3, 6, 9, 12, 15, etc.<br /> Coin 6 produces multiples of 6: 6, 12, 18, 24, etc.<br /> Coin 9 produces multiples of 9: 9, 18, 27, 36, etc.<br /> All of the coins combined produce: 3, 6, <u><strong>9</strong></u>, 12, 15, etc.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> coins = [5,2], k = 7</span></p> <p><strong>Output:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 12 </span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 5 produces multiples of 5: 5, 10, 15, 20, etc.<br /> Coin 2 produces multiples of 2: 2, 4, 6, 8, 10, 12, etc.<br /> All of the coins combined produce: 2, 4, 5, 6, 8, 10, <u><strong>12</strong></u>, 14, 15, etc.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 15</code></li> <li><code>1 &lt;= coins[i] &lt;= 25</code></li> <li><code>1 &lt;= k &lt;= 2 * 10<sup>9</sup></code></li> <li><code>coins</code> contains pairwise distinct integers.</li> </ul>
2
{ "code": "class Solution {\npublic:\n long long findKthSmallest(vector<int>& coins, int k) {\n long long lo=1,hi=1e13;\n int n=coins.size();\n while(lo<=hi)\n {\n long long mid=(lo+hi)/2;\n long long count=0;\n long long val=pow(2,n);\n for(long long bit=1;bit<val;bit++)\n {\n long long val1=bit;\n long long ind=0,ct=0;\n long long lc=1;\n while(val1)\n {\n if((val1&1))\n {\n lc=lcm(lc,coins[ind]);\n ct++;\n }\n ind++;\n val1=val1>>1;\n }\n if((ct&1))\n {\n count+=(mid/lc);\n }\n else\n {\n count-=(mid/lc);\n }\n }\n //cout<<count<<endl;\n if(count>=k)\n {\n hi=mid-1;\n }\n else\n {\n lo=mid+1;\n }\n }\n return lo;\n }\n};", "memory": "20300" }
3,375
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>k</code>.</p> <p>You have an infinite number of coins of each denomination. However, you are <strong>not allowed</strong> to combine coins of different denominations.</p> <p>Return the <code>k<sup>th</sup></code> <strong>smallest</strong> amount that can be made using these coins.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; ">coins = [3,6,9], k = 3</span></p> <p><strong>Output:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 9</span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 3 produces multiples of 3: 3, 6, 9, 12, 15, etc.<br /> Coin 6 produces multiples of 6: 6, 12, 18, 24, etc.<br /> Coin 9 produces multiples of 9: 9, 18, 27, 36, etc.<br /> All of the coins combined produce: 3, 6, <u><strong>9</strong></u>, 12, 15, etc.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> coins = [5,2], k = 7</span></p> <p><strong>Output:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 12 </span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 5 produces multiples of 5: 5, 10, 15, 20, etc.<br /> Coin 2 produces multiples of 2: 2, 4, 6, 8, 10, 12, etc.<br /> All of the coins combined produce: 2, 4, 5, 6, 8, 10, <u><strong>12</strong></u>, 14, 15, etc.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 15</code></li> <li><code>1 &lt;= coins[i] &lt;= 25</code></li> <li><code>1 &lt;= k &lt;= 2 * 10<sup>9</sup></code></li> <li><code>coins</code> contains pairwise distinct integers.</li> </ul>
2
{ "code": "class Solution {\npublic:\n long long lcm(long long a,long long b){\n return (a/__gcd(a,b))*b;\n }\n\n long long chk(long long mid,vector<int>&coins){\n long long n = coins.size();\n long long ans = 0;\n long long cnt;\n for(long long mask = 1;mask<pow(2,n);mask++){\n cnt = 0;\n long long l = 1;\n for(int i = 0;i<n;i++){\n if(mask & 1<<i){\n cnt++;\n l = lcm(l,coins[i]);\n }\n }\n // cout<<mask<<endl;\n if(cnt%2)ans+=mid/l;\n else ans-=mid/l;\n }\n cout<<\". \"<<mid<<\" \"<<ans<<endl;\n return ans;\n }\n\n long long findKthSmallest(vector<int>& coins, int k) {\n \n long long l = 0,r = 1e12;\n long long ans = 0;\n while(l<=r){\n long long mid = l + (r-l)/2;\n if(chk(mid,coins)>=k){\n ans = mid;\n r = mid-1;\n\n }\n else l = mid+1;\n }\n return ans;\n }\n};", "memory": "20400" }
3,375
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>k</code>.</p> <p>You have an infinite number of coins of each denomination. However, you are <strong>not allowed</strong> to combine coins of different denominations.</p> <p>Return the <code>k<sup>th</sup></code> <strong>smallest</strong> amount that can be made using these coins.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; ">coins = [3,6,9], k = 3</span></p> <p><strong>Output:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 9</span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 3 produces multiples of 3: 3, 6, 9, 12, 15, etc.<br /> Coin 6 produces multiples of 6: 6, 12, 18, 24, etc.<br /> Coin 9 produces multiples of 9: 9, 18, 27, 36, etc.<br /> All of the coins combined produce: 3, 6, <u><strong>9</strong></u>, 12, 15, etc.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> coins = [5,2], k = 7</span></p> <p><strong>Output:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 12 </span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 5 produces multiples of 5: 5, 10, 15, 20, etc.<br /> Coin 2 produces multiples of 2: 2, 4, 6, 8, 10, 12, etc.<br /> All of the coins combined produce: 2, 4, 5, 6, 8, 10, <u><strong>12</strong></u>, 14, 15, etc.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 15</code></li> <li><code>1 &lt;= coins[i] &lt;= 25</code></li> <li><code>1 &lt;= k &lt;= 2 * 10<sup>9</sup></code></li> <li><code>coins</code> contains pairwise distinct integers.</li> </ul>
2
{ "code": "class Solution {\npublic:\n long long findKthSmallest(vector<int>& c, int k) {\n vector<int> a;\n for (auto i : c) {\n int key = 1;\n for (auto j : c)\n if (i > j)\n if (gcd(i, j) == j)\n key = 0;\n if (key)\n a.push_back(i);\n }\n auto good = [&](long long mid) {\n long long sm = 0;\n for (int i = 1; i < 1 << a.size(); i++) {\n long long d = 0;\n int mm = -1;\n for (int j = 0; j < a.size(); j++)\n if ((1 << j) & i){\n mm *= -1;\n if (d == 0)\n d = a[j];\n d = lcm(a[j], d);\n }\n if (d)\n sm += mid / d * mm;\n }\n return sm >= k;\n };\n\n long long l = 0, r = 5e10;\n while (r - l > 1) {\n long long mid = (l + r) / 2;\n if (good(mid))\n r = mid;\n else\n l = mid;\n }\n return r;\n}\n};", "memory": "20500" }
3,375
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>k</code>.</p> <p>You have an infinite number of coins of each denomination. However, you are <strong>not allowed</strong> to combine coins of different denominations.</p> <p>Return the <code>k<sup>th</sup></code> <strong>smallest</strong> amount that can be made using these coins.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; ">coins = [3,6,9], k = 3</span></p> <p><strong>Output:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 9</span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 3 produces multiples of 3: 3, 6, 9, 12, 15, etc.<br /> Coin 6 produces multiples of 6: 6, 12, 18, 24, etc.<br /> Coin 9 produces multiples of 9: 9, 18, 27, 36, etc.<br /> All of the coins combined produce: 3, 6, <u><strong>9</strong></u>, 12, 15, etc.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> coins = [5,2], k = 7</span></p> <p><strong>Output:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 12 </span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 5 produces multiples of 5: 5, 10, 15, 20, etc.<br /> Coin 2 produces multiples of 2: 2, 4, 6, 8, 10, 12, etc.<br /> All of the coins combined produce: 2, 4, 5, 6, 8, 10, <u><strong>12</strong></u>, 14, 15, etc.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 15</code></li> <li><code>1 &lt;= coins[i] &lt;= 25</code></li> <li><code>1 &lt;= k &lt;= 2 * 10<sup>9</sup></code></li> <li><code>coins</code> contains pairwise distinct integers.</li> </ul>
2
{ "code": "class Solution {\n vector<long> _processOut(vector<int>& coins) {\n ranges::sort(coins);\n int n = coins.size();\n if (coins[0] == 1) return {1};\n vector<long> longCoins;\n for (int coin: coins) {\n for (int lcoin: longCoins) {\n if (coin % lcoin == 0) goto next;\n }\n longCoins.push_back(coin);\n next:;\n }\n return longCoins;\n }\n bool _next(vector<int>& indexes, int n) {\n int i = n - 1;\n while (i >= 0 && indexes[i] == indexes[i + 1] - 1) --i;\n if (i < 0) return false;\n indexes[i]++;\n for (++i; i < n; ++i) indexes[i] = indexes[i - 1] + 1;\n return true;\n }\n void _appendWithCombinations(vector<long>& coins, long upper) {\n int sign{-1};\n const int n = coins.size();\n vector<int> indexes(n + 1);\n for (int i{2}; i <= n; ++i, sign = -sign) {\n for (int j{}; j < i; ++j) indexes[j] = j;\n indexes[i] = n;\n do {\n long prod = 1;\n for (int j{}; j < i; ++j) prod = lcm(prod, coins[indexes[j]]);\n if (prod <= upper) coins.push_back(sign*prod);\n } while (_next(indexes, i));\n }\n }\npublic:\n long findKthSmallest(vector<int>& poorCoins, int k) {\n vector<long> coins = _processOut(poorCoins);\n const int n = coins.size();\n if (n == 1) return k*coins[0];\n double divider{};\n for (auto coin: coins) divider += 1./coin;\n long upper{coins[0]*k + 1};\n long lower = k/divider;\n _appendWithCombinations(coins, upper);\n while (lower < upper) {\n long mid = (lower + upper)/2;\n long term{};\n long minReminder{numeric_limits<long>::max()};\n for (long coin: coins) {\n auto [q, r] = div(mid, coin);\n term += q;\n minReminder = min(minReminder, r);\n }\n if (term == k) return mid - minReminder;\n if (term < k) lower = mid;\n else upper = mid - minReminder;\n while (upper < abs(coins.back())) coins.pop_back();\n }\n return -1;\n }\n};", "memory": "20600" }
3,375
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>k</code>.</p> <p>You have an infinite number of coins of each denomination. However, you are <strong>not allowed</strong> to combine coins of different denominations.</p> <p>Return the <code>k<sup>th</sup></code> <strong>smallest</strong> amount that can be made using these coins.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; ">coins = [3,6,9], k = 3</span></p> <p><strong>Output:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 9</span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 3 produces multiples of 3: 3, 6, 9, 12, 15, etc.<br /> Coin 6 produces multiples of 6: 6, 12, 18, 24, etc.<br /> Coin 9 produces multiples of 9: 9, 18, 27, 36, etc.<br /> All of the coins combined produce: 3, 6, <u><strong>9</strong></u>, 12, 15, etc.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> coins = [5,2], k = 7</span></p> <p><strong>Output:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 12 </span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 5 produces multiples of 5: 5, 10, 15, 20, etc.<br /> Coin 2 produces multiples of 2: 2, 4, 6, 8, 10, 12, etc.<br /> All of the coins combined produce: 2, 4, 5, 6, 8, 10, <u><strong>12</strong></u>, 14, 15, etc.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 15</code></li> <li><code>1 &lt;= coins[i] &lt;= 25</code></li> <li><code>1 &lt;= k &lt;= 2 * 10<sup>9</sup></code></li> <li><code>coins</code> contains pairwise distinct integers.</li> </ul>
2
{ "code": "typedef long long ll;\n\nclass Solution {\npublic:\n long long findKthSmallest(vector<int>& coins, int k) {\n // setup\n sort(coins.begin(), coins.end());\n int n = coins.size(), maskLimit = 1<<n;\n lcmOfMask[0] = 1;\n numberBitOneOfMask[0] = 0;\n for (int mask=1; mask < maskLimit; ++mask) {\n numberBitOneOfMask[mask] = 0;\n lcmOfMask[mask] = 1;\n bool calculated = false;\n for (int i = 0; i < n; ++i)\n if (mask & (1<<i)) {\n ++numberBitOneOfMask[mask];\n if (!calculated) {\n lcmOfMask[mask] = lcm(lcmOfMask[mask ^ (1<<i)], coins[i]);\n calculated = true;\n }\n }\n }\n\n // find\n ll l = coins[0], r = ll(k) * coins[0], ans = -1;\n while (l <= r) {\n ll mid = (l+r)>>1;\n ll nDistinct = countDistinction(coins, mid);\n if (nDistinct >= k) {\n if (nDistinct == k && isDivisible(mid, coins))\n ans = mid;\n r = mid-1;\n }\n else l = mid+1;\n }\n return ans;\n }\n\nprivate:\n static const int MAX_N = 15;\n static const int MAX_MASK = 1<<MAX_N;\n ll lcmOfMask[MAX_MASK];\n int numberBitOneOfMask[MAX_MASK];\n\n ll countDistinction(const vector<int>& coins, ll limit) {\n ll count = limit / coins[0];\n for (int i = 1, n = coins.size(); i < n; ++i) {\n int maskLimit = 1<<i; // covers combinations of coins[0..i-1]\n for (int mask = 0; mask < maskLimit; ++mask) {\n // inclusive-exclusive\n ll maskLcm = lcmOfMask[mask | (1<<i)];\n if (numberBitOneOfMask[mask] & 1) count -= (limit / maskLcm);\n else count += (limit / maskLcm);\n }\n }\n return count;\n }\n\n bool isDivisible(ll value, const vector<int>& coins) {\n for (int i = 0, n = coins.size(); i < n; ++i)\n if (value % coins[i] == 0)\n return true;\n return false;\n }\n\n ll lcm(ll a, ll b) {\n return a*b / gcd(a, b); \n }\n\n ll gcd(ll a, ll b) {\n if (a < b) swap(a, b);\n if (b == 0) return a;\n return gcd(b, a%b);\n }\n};", "memory": "20700" }
3,375
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>k</code>.</p> <p>You have an infinite number of coins of each denomination. However, you are <strong>not allowed</strong> to combine coins of different denominations.</p> <p>Return the <code>k<sup>th</sup></code> <strong>smallest</strong> amount that can be made using these coins.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; ">coins = [3,6,9], k = 3</span></p> <p><strong>Output:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 9</span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 3 produces multiples of 3: 3, 6, 9, 12, 15, etc.<br /> Coin 6 produces multiples of 6: 6, 12, 18, 24, etc.<br /> Coin 9 produces multiples of 9: 9, 18, 27, 36, etc.<br /> All of the coins combined produce: 3, 6, <u><strong>9</strong></u>, 12, 15, etc.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> coins = [5,2], k = 7</span></p> <p><strong>Output:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 12 </span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 5 produces multiples of 5: 5, 10, 15, 20, etc.<br /> Coin 2 produces multiples of 2: 2, 4, 6, 8, 10, 12, etc.<br /> All of the coins combined produce: 2, 4, 5, 6, 8, 10, <u><strong>12</strong></u>, 14, 15, etc.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 15</code></li> <li><code>1 &lt;= coins[i] &lt;= 25</code></li> <li><code>1 &lt;= k &lt;= 2 * 10<sup>9</sup></code></li> <li><code>coins</code> contains pairwise distinct integers.</li> </ul>
2
{ "code": "class Solution {\npublic:\n long long findKthSmallest(vector<int>& coins, int k) {\n \n using ll = long long;\n int n = coins.size();\n vector<ll> mult(1 << n, 1);\n for (int b = 0; b < (1 << n); ++b) {\n for (int i = 0; i < n; ++i) {\n if (b & (1 << i)) {\n mult[b] = mult[b] / gcd(mult[b], coins[i]) * coins[i];\n }\n }\n }\n ll lo = 1, hi = 1e12;\n while (lo < hi) {\n ll mid = (lo + hi) / 2;\n ll cnt = 0;\n for (int b = 1; b < (1 << n); ++b) {\n ll v = mid / mult[b];\n if (__builtin_popcount(b) & 1) cnt += v;\n else cnt -= v;\n }\n if (cnt < k) lo = mid + 1;\n else hi = mid;\n }\n return lo;\n \n }\n};", "memory": "20800" }
3,375
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>k</code>.</p> <p>You have an infinite number of coins of each denomination. However, you are <strong>not allowed</strong> to combine coins of different denominations.</p> <p>Return the <code>k<sup>th</sup></code> <strong>smallest</strong> amount that can be made using these coins.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; ">coins = [3,6,9], k = 3</span></p> <p><strong>Output:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 9</span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 3 produces multiples of 3: 3, 6, 9, 12, 15, etc.<br /> Coin 6 produces multiples of 6: 6, 12, 18, 24, etc.<br /> Coin 9 produces multiples of 9: 9, 18, 27, 36, etc.<br /> All of the coins combined produce: 3, 6, <u><strong>9</strong></u>, 12, 15, etc.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> coins = [5,2], k = 7</span></p> <p><strong>Output:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 12 </span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 5 produces multiples of 5: 5, 10, 15, 20, etc.<br /> Coin 2 produces multiples of 2: 2, 4, 6, 8, 10, 12, etc.<br /> All of the coins combined produce: 2, 4, 5, 6, 8, 10, <u><strong>12</strong></u>, 14, 15, etc.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 15</code></li> <li><code>1 &lt;= coins[i] &lt;= 25</code></li> <li><code>1 &lt;= k &lt;= 2 * 10<sup>9</sup></code></li> <li><code>coins</code> contains pairwise distinct integers.</li> </ul>
2
{ "code": "class Solution {\npublic:\n long long findKthSmallest(vector<int>& coins, int k) {\n \n using ll = long long;\n int n = coins.size();\n vector<ll> mult(1 << n, 1);\n for (int b = 0; b < (1 << n); ++b) {\n for (int i = 0; i < n; ++i) {\n if (b & (1 << i)) {\n mult[b] = mult[b] / gcd(mult[b], coins[i]) * coins[i];\n }\n }\n }\n ll lo = 1, hi = 1e12;\n while (lo < hi) {\n ll mid = (lo + hi) / 2;\n ll cnt = 0;\n for (int b = 1; b < (1 << n); ++b) {\n ll v = mid / mult[b];\n if (__builtin_popcount(b) & 1) cnt += v;\n else cnt -= v;\n }\n if (cnt < k) lo = mid + 1;\n else hi = mid;\n }\n return lo;\n \n }\n};", "memory": "20900" }
3,375
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>k</code>.</p> <p>You have an infinite number of coins of each denomination. However, you are <strong>not allowed</strong> to combine coins of different denominations.</p> <p>Return the <code>k<sup>th</sup></code> <strong>smallest</strong> amount that can be made using these coins.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; ">coins = [3,6,9], k = 3</span></p> <p><strong>Output:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 9</span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 3 produces multiples of 3: 3, 6, 9, 12, 15, etc.<br /> Coin 6 produces multiples of 6: 6, 12, 18, 24, etc.<br /> Coin 9 produces multiples of 9: 9, 18, 27, 36, etc.<br /> All of the coins combined produce: 3, 6, <u><strong>9</strong></u>, 12, 15, etc.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> coins = [5,2], k = 7</span></p> <p><strong>Output:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 12 </span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 5 produces multiples of 5: 5, 10, 15, 20, etc.<br /> Coin 2 produces multiples of 2: 2, 4, 6, 8, 10, 12, etc.<br /> All of the coins combined produce: 2, 4, 5, 6, 8, 10, <u><strong>12</strong></u>, 14, 15, etc.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 15</code></li> <li><code>1 &lt;= coins[i] &lt;= 25</code></li> <li><code>1 &lt;= k &lt;= 2 * 10<sup>9</sup></code></li> <li><code>coins</code> contains pairwise distinct integers.</li> </ul>
2
{ "code": "#define ll long long int\nclass Solution {\npublic:\n long long findKthSmallest(vector<int>& coins, int k) {\n //kth smallest integer you can make from coins provided given you cannot combine coins of different denominations\n //note that you have only sums which is multiple of only one type of coin \n //like coin x*k\n //x1, 2*x1, 3*x1, 4*x1, ...\n //x2, 2*x2, 3*x2, 4*x2, ..\n //x3, 2*x3, 3*x3, 4*x3, ..\n //x4, 2*x4, 3*x4, 4*x4, ..\n //among all these sort in increasing order and take the kth number\n //worst solution -> multiple of k around k say and sort klogk\n int n=coins.size();\n ll lo=0,hi=1e12, ans=0;\n sort(coins.begin(),coins.end());\n vector<ll> cnt(1<<n);\n for(ll msk=1;msk<(1<<n);msk++){\n for(int i=0;i<n;i++){\n if((msk>>i)&1){\n if(cnt[msk]==0)cnt[msk]=coins[i];\n else\n cnt[msk]=lcm(cnt[msk],coins[i]); \n }\n }\n //cout<<cnt[msk]<<\" \";\n } \n while(lo<=hi){\n //check if hi is the max coin sum we are taking.. how many coins getting accomodated till here ?\n ll mid= (lo+hi)/2;\n ll tot=0;\n for(ll msk=1;msk<(1<<n);msk++){//msk shows what coins are we taking and cnt[msk] is lcm of coin combination.. we need to check how many is possible in mid range\n if(__builtin_popcount(msk)&1)tot+=(mid/cnt[msk]);\n else tot-=(mid/cnt[msk]);\n }\n //cout<<\"mid:\"<<mid<<\" cnt:\"<<tot<<\"\\n\";\n if(tot>=k){\n //need to decrease range since this is covering more than required.. \n //even if equal try to decrease range so that we get the exact kth value\n ans=mid;\n hi=mid-1;\n }else{\n lo=mid+1; //the kth is not lying till mid increase lo\n }\n }\n return ans;\n }\n};", "memory": "21000" }
3,375
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>k</code>.</p> <p>You have an infinite number of coins of each denomination. However, you are <strong>not allowed</strong> to combine coins of different denominations.</p> <p>Return the <code>k<sup>th</sup></code> <strong>smallest</strong> amount that can be made using these coins.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; ">coins = [3,6,9], k = 3</span></p> <p><strong>Output:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 9</span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 3 produces multiples of 3: 3, 6, 9, 12, 15, etc.<br /> Coin 6 produces multiples of 6: 6, 12, 18, 24, etc.<br /> Coin 9 produces multiples of 9: 9, 18, 27, 36, etc.<br /> All of the coins combined produce: 3, 6, <u><strong>9</strong></u>, 12, 15, etc.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> coins = [5,2], k = 7</span></p> <p><strong>Output:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 12 </span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 5 produces multiples of 5: 5, 10, 15, 20, etc.<br /> Coin 2 produces multiples of 2: 2, 4, 6, 8, 10, 12, etc.<br /> All of the coins combined produce: 2, 4, 5, 6, 8, 10, <u><strong>12</strong></u>, 14, 15, etc.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 15</code></li> <li><code>1 &lt;= coins[i] &lt;= 25</code></li> <li><code>1 &lt;= k &lt;= 2 * 10<sup>9</sup></code></li> <li><code>coins</code> contains pairwise distinct integers.</li> </ul>
3
{ "code": "class Solution {\npublic:\n long long findKthSmallest(vector<int>& coins, int k) {\n \n using ll = long long;\n int n = coins.size();\n vector<ll> mult(1 << n, 1);\n for (int b = 0; b < (1 << n); ++b) {\n for (int i = 0; i < n; ++i) {\n if (b & (1 << i)) {\n mult[b] = mult[b] / gcd(mult[b], coins[i]) * coins[i];\n }\n }\n }\n ll lo = 1, hi = 1e12;\n while (lo < hi) {\n ll mid = (lo + hi) / 2;\n ll cnt = 0;\n for (int b = 1; b < (1 << n); ++b) {\n ll v = mid / mult[b];\n if (__builtin_popcount(b) & 1) cnt += v;\n else cnt -= v;\n }\n if (cnt < k) lo = mid + 1;\n else hi = mid;\n }\n return lo;\n \n }\n};", "memory": "21100" }
3,375
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>k</code>.</p> <p>You have an infinite number of coins of each denomination. However, you are <strong>not allowed</strong> to combine coins of different denominations.</p> <p>Return the <code>k<sup>th</sup></code> <strong>smallest</strong> amount that can be made using these coins.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; ">coins = [3,6,9], k = 3</span></p> <p><strong>Output:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 9</span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 3 produces multiples of 3: 3, 6, 9, 12, 15, etc.<br /> Coin 6 produces multiples of 6: 6, 12, 18, 24, etc.<br /> Coin 9 produces multiples of 9: 9, 18, 27, 36, etc.<br /> All of the coins combined produce: 3, 6, <u><strong>9</strong></u>, 12, 15, etc.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> coins = [5,2], k = 7</span></p> <p><strong>Output:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 12 </span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 5 produces multiples of 5: 5, 10, 15, 20, etc.<br /> Coin 2 produces multiples of 2: 2, 4, 6, 8, 10, 12, etc.<br /> All of the coins combined produce: 2, 4, 5, 6, 8, 10, <u><strong>12</strong></u>, 14, 15, etc.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 15</code></li> <li><code>1 &lt;= coins[i] &lt;= 25</code></li> <li><code>1 &lt;= k &lt;= 2 * 10<sup>9</sup></code></li> <li><code>coins</code> contains pairwise distinct integers.</li> </ul>
3
{ "code": "#define ll long long\nclass Solution {\npublic:\n bool check(vector<int> v, ll x, int k)\n {\n int n = v.size();\n ll sum = 0;\n for(int i = 1; i < (1 << n); i++)\n {\n ll cnt = 0;\n ll lcm = 1;\n for(int j = 0; j < n; j++)\n {\n if(i & (1 << j))\n {\n cnt++;\n lcm = (lcm * v[j]) / __gcd(lcm, (ll)v[j]);\n }\n }\n if(cnt & 1)\n sum += x / lcm;\n else\n sum -= x / lcm;\n }\n return sum >= k;\n }\n\n long long findKthSmallest(vector<int>& v, int k) {\n ll low = 1, high = 5 * (ll)1e10;\n ll mid;\n while(high - low > 0)\n {\n mid = low + (high - low) / 2;\n bool val = check(v, mid, k);\n if(val)\n high = mid;\n else\n low = mid + 1;\n }\n return high;\n }\n};", "memory": "21200" }
3,375
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>k</code>.</p> <p>You have an infinite number of coins of each denomination. However, you are <strong>not allowed</strong> to combine coins of different denominations.</p> <p>Return the <code>k<sup>th</sup></code> <strong>smallest</strong> amount that can be made using these coins.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; ">coins = [3,6,9], k = 3</span></p> <p><strong>Output:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 9</span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 3 produces multiples of 3: 3, 6, 9, 12, 15, etc.<br /> Coin 6 produces multiples of 6: 6, 12, 18, 24, etc.<br /> Coin 9 produces multiples of 9: 9, 18, 27, 36, etc.<br /> All of the coins combined produce: 3, 6, <u><strong>9</strong></u>, 12, 15, etc.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> coins = [5,2], k = 7</span></p> <p><strong>Output:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 12 </span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 5 produces multiples of 5: 5, 10, 15, 20, etc.<br /> Coin 2 produces multiples of 2: 2, 4, 6, 8, 10, 12, etc.<br /> All of the coins combined produce: 2, 4, 5, 6, 8, 10, <u><strong>12</strong></u>, 14, 15, etc.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 15</code></li> <li><code>1 &lt;= coins[i] &lt;= 25</code></li> <li><code>1 &lt;= k &lt;= 2 * 10<sup>9</sup></code></li> <li><code>coins</code> contains pairwise distinct integers.</li> </ul>
3
{ "code": "class Solution {\npublic:\n long long lcm(long long a, long long b) { return (a * b) / (long long)gcd(a, b); }\n long long min(long long a, long long b) { return a > b ? b : a; }\n long long getPosition(long long int k, vector<int> v, long long int check) {\n // cout << \"---------->>\" << check << endl;\n long long int n = v.size();\n long long ans = 0;\n for (long long int i = 1; i < (1 << n); i++) {\n long long int x = i;\n long long int setbits = 0, lcmx = 1, count = 0;\n while (x > 0) {\n if (x % 2) {\n setbits++;\n lcmx = lcm(lcmx, (long long)v[count]);\n }\n x /= 2;\n count++;\n }\n\n if (setbits % 2 == 1) {\n ans += (check / lcmx);\n } else {\n ans -= (check / lcmx);\n }\n }\n return ans;\n }\n\n long long findKthSmallest(vector<int>& coins, int k) {\n int n = coins.size();\n long long int start = 1, end = 1000000000000;\n long long int ans = 10000000000000;\n while (end >= start) {\n long long mid = start + (end - start) / 2;\n long long int x = getPosition(k, coins, mid);\n if (x == (long long)k) {\n ans = min(ans, mid);\n }\n if (x < k) {\n start = mid + 1;\n } else {\n end = mid - 1;\n }\n }\n return ans;\n }\n};", "memory": "21300" }
3,375
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>k</code>.</p> <p>You have an infinite number of coins of each denomination. However, you are <strong>not allowed</strong> to combine coins of different denominations.</p> <p>Return the <code>k<sup>th</sup></code> <strong>smallest</strong> amount that can be made using these coins.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; ">coins = [3,6,9], k = 3</span></p> <p><strong>Output:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 9</span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 3 produces multiples of 3: 3, 6, 9, 12, 15, etc.<br /> Coin 6 produces multiples of 6: 6, 12, 18, 24, etc.<br /> Coin 9 produces multiples of 9: 9, 18, 27, 36, etc.<br /> All of the coins combined produce: 3, 6, <u><strong>9</strong></u>, 12, 15, etc.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> coins = [5,2], k = 7</span></p> <p><strong>Output:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 12 </span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 5 produces multiples of 5: 5, 10, 15, 20, etc.<br /> Coin 2 produces multiples of 2: 2, 4, 6, 8, 10, 12, etc.<br /> All of the coins combined produce: 2, 4, 5, 6, 8, 10, <u><strong>12</strong></u>, 14, 15, etc.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 15</code></li> <li><code>1 &lt;= coins[i] &lt;= 25</code></li> <li><code>1 &lt;= k &lt;= 2 * 10<sup>9</sup></code></li> <li><code>coins</code> contains pairwise distinct integers.</li> </ul>
3
{ "code": "class Solution {\npublic:\n using ll = long long;\n\n ll lcm(ll a, ll b) {\n ll p = a * b;\n ll t = 0;\n while(b) {\n t = b;\n b = a % b;\n a = t;\n }\n return p / a;\n }\n\n long long findKthSmallest(vector<int>& coins, int k) {\n size_t n = coins.size();\n\n ll cnt = 0, _lcm = 1, sgn = -1, cur = 0, res = 0;\n\n function<void(ll)> bt = [&](ll i) {\n ll bak = _lcm;\n _lcm = lcm(_lcm, coins[i]);\n sgn=-sgn;\n cnt+=sgn*floor(cur/_lcm);\n\n ++i;\n if(i<n) bt(i);\n\n _lcm = bak;\n sgn = -sgn;\n if(i<n) bt(i);\n };\n\n ll l = 1;\n ll r = 50000000000;\n while(l<=r) {\n cur = (l+r)/2;\n cnt=0;\n bt(0);\n\n if(cnt>=k) {\n res = cur;\n r = cur - 1;\n }\n else {\n l=cur + 1;\n }\n }\n return res;\n }\n};", "memory": "21400" }
3,375
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>k</code>.</p> <p>You have an infinite number of coins of each denomination. However, you are <strong>not allowed</strong> to combine coins of different denominations.</p> <p>Return the <code>k<sup>th</sup></code> <strong>smallest</strong> amount that can be made using these coins.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; ">coins = [3,6,9], k = 3</span></p> <p><strong>Output:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 9</span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 3 produces multiples of 3: 3, 6, 9, 12, 15, etc.<br /> Coin 6 produces multiples of 6: 6, 12, 18, 24, etc.<br /> Coin 9 produces multiples of 9: 9, 18, 27, 36, etc.<br /> All of the coins combined produce: 3, 6, <u><strong>9</strong></u>, 12, 15, etc.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> coins = [5,2], k = 7</span></p> <p><strong>Output:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 12 </span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 5 produces multiples of 5: 5, 10, 15, 20, etc.<br /> Coin 2 produces multiples of 2: 2, 4, 6, 8, 10, 12, etc.<br /> All of the coins combined produce: 2, 4, 5, 6, 8, 10, <u><strong>12</strong></u>, 14, 15, etc.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 15</code></li> <li><code>1 &lt;= coins[i] &lt;= 25</code></li> <li><code>1 &lt;= k &lt;= 2 * 10<sup>9</sup></code></li> <li><code>coins</code> contains pairwise distinct integers.</li> </ul>
3
{ "code": "class Solution {\npublic:\n using ll = long long;\n\n ll lcm(ll a, ll b) {\n ll p = a * b;\n ll t = 0;\n while(b) {\n t = b;\n b = a % b;\n a = t;\n }\n return p / a;\n }\n\n long long findKthSmallest(vector<int>& coins, int k) {\n size_t n = coins.size();\n\n ll cnt = 0, _lcm = 1, sgn = -1, cur = 0, res = 0;\n\n function<void(ll)> bt = [&](ll i) {\n ll bak = _lcm;\n _lcm = lcm(_lcm, coins[i]);\n sgn=-sgn;\n cnt+=sgn*cur/_lcm;\n\n ++i;\n if(i<n) bt(i);\n\n _lcm = bak;\n sgn = -sgn;\n if(i<n) bt(i);\n };\n\n ll l = 1;\n ll r = 50000000000;\n while(l<=r) {\n cur = (l+r)/2;\n cnt=0;\n bt(0);\n\n if(cnt>=k) {\n res = cur;\n r = cur - 1;\n }\n else {\n l=cur + 1;\n }\n }\n return res;\n }\n};", "memory": "21400" }
3,375
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>k</code>.</p> <p>You have an infinite number of coins of each denomination. However, you are <strong>not allowed</strong> to combine coins of different denominations.</p> <p>Return the <code>k<sup>th</sup></code> <strong>smallest</strong> amount that can be made using these coins.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; ">coins = [3,6,9], k = 3</span></p> <p><strong>Output:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 9</span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 3 produces multiples of 3: 3, 6, 9, 12, 15, etc.<br /> Coin 6 produces multiples of 6: 6, 12, 18, 24, etc.<br /> Coin 9 produces multiples of 9: 9, 18, 27, 36, etc.<br /> All of the coins combined produce: 3, 6, <u><strong>9</strong></u>, 12, 15, etc.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> coins = [5,2], k = 7</span></p> <p><strong>Output:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 12 </span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 5 produces multiples of 5: 5, 10, 15, 20, etc.<br /> Coin 2 produces multiples of 2: 2, 4, 6, 8, 10, 12, etc.<br /> All of the coins combined produce: 2, 4, 5, 6, 8, 10, <u><strong>12</strong></u>, 14, 15, etc.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 15</code></li> <li><code>1 &lt;= coins[i] &lt;= 25</code></li> <li><code>1 &lt;= k &lt;= 2 * 10<sup>9</sup></code></li> <li><code>coins</code> contains pairwise distinct integers.</li> </ul>
3
{ "code": "class Solution {\n bool isPossible(vector<long long>& LCMS, int k, long long mid){\n long long res = 0;\n int n = LCMS.size();\n\n for(int i = 0; i < n; i++){\n res += mid/LCMS[i];\n }\n return res >= k;\n }\n\npublic:\n long long findKthSmallest(vector<int>& coins, int k) {\n vector<long long> LCMS;\n int n = coins.size();\n\n //as there are 2^n subsets of array of size n\n int m = 1 << n;\n \n //Outer loop runs for O(m) = O(2^n)\n for(int i = 1; i < m; i++){\n int tmp = i, j = 0, setBits = 0;\n long long lccm = 1;\n\n //This inner loop can run atmost 25 times as tmp can have\n //atmost 25 set bits, thus making the complexity 25 * O(2^n)\n while( tmp > 0 ){\n if ( tmp & 1 ){\n lccm = lcm(lccm, coins[j]);\n setBits++;\n }\n tmp = tmp>>1;\n j++;\n }\n\n //setting appropriate sign of lccm according to parity of\n //number of elements in subset\n if( setBits % 2 == 0 )\n lccm = -lccm;\n \n LCMS.push_back(lccm);\n }\n\n\n //This outer loop runs for log k times and makes the call to isPossible func\n //The TC for isPossible function is again O(2^n)\n //Thus making overall TC O(2^n * log k)\n long long low = 0, high = 1e18, mid, ans;\n while( low <= high ){\n mid = low + (high - low)/2;\n if( isPossible(LCMS, k, mid) ){\n ans = mid;\n high = mid - 1;\n }else{\n low = mid + 1;\n }\n }\n return ans;\n }\n};", "memory": "21500" }
3,375
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>k</code>.</p> <p>You have an infinite number of coins of each denomination. However, you are <strong>not allowed</strong> to combine coins of different denominations.</p> <p>Return the <code>k<sup>th</sup></code> <strong>smallest</strong> amount that can be made using these coins.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; ">coins = [3,6,9], k = 3</span></p> <p><strong>Output:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 9</span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 3 produces multiples of 3: 3, 6, 9, 12, 15, etc.<br /> Coin 6 produces multiples of 6: 6, 12, 18, 24, etc.<br /> Coin 9 produces multiples of 9: 9, 18, 27, 36, etc.<br /> All of the coins combined produce: 3, 6, <u><strong>9</strong></u>, 12, 15, etc.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> coins = [5,2], k = 7</span></p> <p><strong>Output:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 12 </span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 5 produces multiples of 5: 5, 10, 15, 20, etc.<br /> Coin 2 produces multiples of 2: 2, 4, 6, 8, 10, 12, etc.<br /> All of the coins combined produce: 2, 4, 5, 6, 8, 10, <u><strong>12</strong></u>, 14, 15, etc.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 15</code></li> <li><code>1 &lt;= coins[i] &lt;= 25</code></li> <li><code>1 &lt;= k &lt;= 2 * 10<sup>9</sup></code></li> <li><code>coins</code> contains pairwise distinct integers.</li> </ul>
3
{ "code": "class Solution {\n bool isPossible(vector<long long>& LCMS, int k, long long mid){\n long long res = 0;\n int n = LCMS.size();\n\n for(int i = 0; i < n; i++){\n res += mid/LCMS[i];\n }\n return res >= k;\n }\n\npublic:\n long long findKthSmallest(vector<int>& coins, int k) {\n vector<long long> LCMS;\n int n = coins.size();\n\n //as there are 2^n subsets of array of size n\n int m = 1 << n;\n \n //Outer loop runs for O(m) = O(2^n)\n for(int i = 1; i < m; i++){\n int tmp = i, j = 0, setBits = 0;\n long long lccm = 1;\n\n //This inner loop can run atmost 25 times as tmp can have\n //atmost 25 set bits, thus making the complexity 25 * O(2^n)\n while( tmp > 0 ){\n if ( tmp & 1 ){\n lccm = lcm(lccm, coins[j]);\n setBits++;\n }\n tmp = tmp>>1;\n j++;\n }\n\n //setting appropriate sign of lccm according to parity of\n //number of elements in subset\n if( setBits % 2 == 0 )\n lccm = -lccm;\n \n LCMS.push_back(lccm);\n }\n for(auto i:LCMS){\n cout<<i<<\" \";\n }\n cout<<endl;\n\n\n //This outer loop runs for log k times and makes the call to isPossible func\n //The TC for isPossible function is again O(2^n)\n //Thus making overall TC O(2^n * log k)\n long long low = 0, high = 1e18, mid, ans;\n while( low <= high ){\n mid = low + (high - low)/2;\n if( isPossible(LCMS, k, mid) ){\n ans = mid;\n high = mid - 1;\n }else{\n low = mid + 1;\n }\n }\n return ans;\n }\n};", "memory": "21600" }
3,375
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>k</code>.</p> <p>You have an infinite number of coins of each denomination. However, you are <strong>not allowed</strong> to combine coins of different denominations.</p> <p>Return the <code>k<sup>th</sup></code> <strong>smallest</strong> amount that can be made using these coins.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; ">coins = [3,6,9], k = 3</span></p> <p><strong>Output:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 9</span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 3 produces multiples of 3: 3, 6, 9, 12, 15, etc.<br /> Coin 6 produces multiples of 6: 6, 12, 18, 24, etc.<br /> Coin 9 produces multiples of 9: 9, 18, 27, 36, etc.<br /> All of the coins combined produce: 3, 6, <u><strong>9</strong></u>, 12, 15, etc.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> coins = [5,2], k = 7</span></p> <p><strong>Output:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 12 </span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 5 produces multiples of 5: 5, 10, 15, 20, etc.<br /> Coin 2 produces multiples of 2: 2, 4, 6, 8, 10, 12, etc.<br /> All of the coins combined produce: 2, 4, 5, 6, 8, 10, <u><strong>12</strong></u>, 14, 15, etc.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 15</code></li> <li><code>1 &lt;= coins[i] &lt;= 25</code></li> <li><code>1 &lt;= k &lt;= 2 * 10<sup>9</sup></code></li> <li><code>coins</code> contains pairwise distinct integers.</li> </ul>
3
{ "code": "class Solution {\npublic:\n long long lcm(long long a, long long b) {\n return (a / gcd(a, b)) * b;\n }\n\n long long findKthSmallest(vector<int>& coins1, int k1) {\n vector<long long> coins;\n for(auto it: coins1)coins.push_back(it);\n long long k = k1;\n sort(coins.begin(), coins.end());\n long long low = coins[0];\n long long n=coins.size();\n long long high = k*coins[n-1] + 1;\n long long temp = pow(2,n);\n vector<long long> add;\n vector<long long> subtract;\n for(long long i = 1; i<temp; i++) {\n long long num_ones = __builtin_popcount(i);\n long long tt = i;\n long long j = 0;\n long long curr_lcm = 1;\n while(tt != 0) {\n long long pick = tt & 1;\n tt/=2;\n if(pick)curr_lcm = lcm(curr_lcm, coins[j]);\n j++;\n }\n if(num_ones & 1)add.push_back(curr_lcm);\n else subtract.push_back(curr_lcm);\n }\n for(auto it: add)cout<<it<<\" \";\n cout<<endl;\n for(auto it: subtract)cout<<it<<\" \";\n while(low<high) {\n long long mid = low + (high-low)/2;\n long long check = 0;\n for(auto it: add)check+=mid/it;\n for(auto it: subtract)check-=mid/it;\n if(check>=k)high = mid;\n else low=mid+1;\n }\n return low;\n }\n};", "memory": "22000" }
3,375
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>k</code>.</p> <p>You have an infinite number of coins of each denomination. However, you are <strong>not allowed</strong> to combine coins of different denominations.</p> <p>Return the <code>k<sup>th</sup></code> <strong>smallest</strong> amount that can be made using these coins.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; ">coins = [3,6,9], k = 3</span></p> <p><strong>Output:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 9</span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 3 produces multiples of 3: 3, 6, 9, 12, 15, etc.<br /> Coin 6 produces multiples of 6: 6, 12, 18, 24, etc.<br /> Coin 9 produces multiples of 9: 9, 18, 27, 36, etc.<br /> All of the coins combined produce: 3, 6, <u><strong>9</strong></u>, 12, 15, etc.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> coins = [5,2], k = 7</span></p> <p><strong>Output:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 12 </span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 5 produces multiples of 5: 5, 10, 15, 20, etc.<br /> Coin 2 produces multiples of 2: 2, 4, 6, 8, 10, 12, etc.<br /> All of the coins combined produce: 2, 4, 5, 6, 8, 10, <u><strong>12</strong></u>, 14, 15, etc.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 15</code></li> <li><code>1 &lt;= coins[i] &lt;= 25</code></li> <li><code>1 &lt;= k &lt;= 2 * 10<sup>9</sup></code></li> <li><code>coins</code> contains pairwise distinct integers.</li> </ul>
3
{ "code": "class Solution {\npublic:\n long long lcm(long long a, long long b) {\n return (a / gcd(a, b)) * b;\n }\n\n long long findKthSmallest(vector<int>& coins1, int k1) {\n vector<long long> coins;\n for(auto it: coins1)coins.push_back(it);\n long long k = k1;\n sort(coins.begin(), coins.end());\n long long low = coins[0];\n long long n=coins.size();\n long long high = k*coins[0] + 1;\n long long temp = pow(2,n);\n vector<long long> add;\n vector<long long> subtract;\n for(long long i = 1; i<temp; i++) {\n long long num_ones = __builtin_popcount(i);\n long long tt = i;\n long long j = 0;\n long long curr_lcm = 1;\n while(tt != 0) {\n long long pick = tt & 1;\n tt/=2;\n if(pick)curr_lcm = lcm(curr_lcm, coins[j]);\n j++;\n }\n if(num_ones & 1)add.push_back(curr_lcm);\n else subtract.push_back(curr_lcm);\n }\n for(auto it: add)cout<<it<<\" \";\n cout<<endl;\n for(auto it: subtract)cout<<it<<\" \";\n while(low<high) {\n long long mid = low + (high-low)/2;\n long long check = 0;\n for(auto it: add)check+=mid/it;\n for(auto it: subtract)check-=mid/it;\n if(check>=k)high = mid;\n else low=mid+1;\n }\n return low;\n }\n};", "memory": "22000" }
3,375
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>k</code>.</p> <p>You have an infinite number of coins of each denomination. However, you are <strong>not allowed</strong> to combine coins of different denominations.</p> <p>Return the <code>k<sup>th</sup></code> <strong>smallest</strong> amount that can be made using these coins.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; ">coins = [3,6,9], k = 3</span></p> <p><strong>Output:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 9</span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 3 produces multiples of 3: 3, 6, 9, 12, 15, etc.<br /> Coin 6 produces multiples of 6: 6, 12, 18, 24, etc.<br /> Coin 9 produces multiples of 9: 9, 18, 27, 36, etc.<br /> All of the coins combined produce: 3, 6, <u><strong>9</strong></u>, 12, 15, etc.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> coins = [5,2], k = 7</span></p> <p><strong>Output:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 12 </span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 5 produces multiples of 5: 5, 10, 15, 20, etc.<br /> Coin 2 produces multiples of 2: 2, 4, 6, 8, 10, 12, etc.<br /> All of the coins combined produce: 2, 4, 5, 6, 8, 10, <u><strong>12</strong></u>, 14, 15, etc.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 15</code></li> <li><code>1 &lt;= coins[i] &lt;= 25</code></li> <li><code>1 &lt;= k &lt;= 2 * 10<sup>9</sup></code></li> <li><code>coins</code> contains pairwise distinct integers.</li> </ul>
3
{ "code": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <numeric> // for gcd\n#include <cmath>\n\nusing namespace std;\n\nclass Solution {\npublic:\n typedef long long ll;\n ll gcd(ll a, ll b) {\n while (b) {\n ll temp = b;\n b = a % b;\n a = temp;\n }\n return a;\n }\n ll lcm(ll a, ll b) {\n return a / gcd(a, b) * b;\n }\n \n ll count(ll x, const vector<ll>& lcm_values, const vector<int>& signs) {\n ll total = 0;\n int n = lcm_values.size();\n for (int i = 0; i < n; ++i) {\n if (lcm_values[i] == 0 || lcm_values[i] > x) continue;\n total += signs[i] * (x / lcm_values[i]);\n }\n return total;\n }\n \n long long findKthSmallest(vector<int>& coins, int k) {\n int m = coins.size();\n // Precompute all subsets and their LCMs\n vector<ll> lcm_values;\n vector<int> signs;\n int total_subsets = 1 << m;\n for (int mask = 1; mask < total_subsets; ++mask) {\n ll lcm_val = 1;\n int bits = __builtin_popcount(mask);\n bool valid = true;\n for (int i = 0; i < m; ++i) {\n if (mask & (1 << i)) {\n ll gcd_val = gcd(lcm_val, (ll)coins[i]);\n if (lcm_val > (1e18 / coins[i])) {\n // Overflow, skip this subset\n valid = false;\n break;\n }\n lcm_val = lcm_val / gcd_val * coins[i];\n if (lcm_val > 1e18) {\n valid = false;\n break;\n }\n }\n }\n if (valid) {\n lcm_values.push_back(lcm_val);\n signs.push_back(bits % 2 == 1 ? 1 : -1);\n }\n }\n \n ll low = 1, high = 1e18;\n while (low < high) {\n ll mid = low + (high - low) / 2;\n ll cnt = count(mid, lcm_values, signs);\n if (cnt >= k)\n high = mid;\n else\n low = mid + 1;\n }\n return low;\n }\n};\n", "memory": "22100" }
3,375
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>k</code>.</p> <p>You have an infinite number of coins of each denomination. However, you are <strong>not allowed</strong> to combine coins of different denominations.</p> <p>Return the <code>k<sup>th</sup></code> <strong>smallest</strong> amount that can be made using these coins.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; ">coins = [3,6,9], k = 3</span></p> <p><strong>Output:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 9</span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 3 produces multiples of 3: 3, 6, 9, 12, 15, etc.<br /> Coin 6 produces multiples of 6: 6, 12, 18, 24, etc.<br /> Coin 9 produces multiples of 9: 9, 18, 27, 36, etc.<br /> All of the coins combined produce: 3, 6, <u><strong>9</strong></u>, 12, 15, etc.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> coins = [5,2], k = 7</span></p> <p><strong>Output:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 12 </span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 5 produces multiples of 5: 5, 10, 15, 20, etc.<br /> Coin 2 produces multiples of 2: 2, 4, 6, 8, 10, 12, etc.<br /> All of the coins combined produce: 2, 4, 5, 6, 8, 10, <u><strong>12</strong></u>, 14, 15, etc.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 15</code></li> <li><code>1 &lt;= coins[i] &lt;= 25</code></li> <li><code>1 &lt;= k &lt;= 2 * 10<sup>9</sup></code></li> <li><code>coins</code> contains pairwise distinct integers.</li> </ul>
3
{ "code": "class Solution {\npublic:\n long long findKthSmallest(vector<int>& coins, int k) {\n int n=coins.size();\n vector<vector<long long>> t(n+1);\n\n //calculating lcm\n auto lcm=[&](long long a, long long b){\n return (a*b)/__gcd(a, b);\n };\n //for generating subsets\n auto subsets = [&](){\n int n=coins.size();\n for(int i=0;i<(1<<n);i++){\n long long cnt=0;\n long long l=1;\n for(int j=0;j<n;j++){\n if(i&(1<<j)){\n cnt++;\n l=lcm(l, coins[j]);\n }\n }\n t[cnt].push_back(l);\n }\n };\n subsets();\n\n //for binary searching x\n auto solve = [&](long long x){\n long long cnt=0;\n bool f=1;\n for(int i=1;i<=n;i++){\n for(auto& l: t[i]){\n if(f)\n cnt+=x/l;\n else\n cnt-=x/l;\n }\n f^=1;\n }\n return cnt;\n };\n\n long long l=1, r=1e11;\n while(l<r){\n long long mid=l+(r-l)/2;\n if(solve(mid)>=k)\n r=mid;\n else\n l=mid+1;\n }\n return r;\n }\n};", "memory": "22200" }
3,375
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>k</code>.</p> <p>You have an infinite number of coins of each denomination. However, you are <strong>not allowed</strong> to combine coins of different denominations.</p> <p>Return the <code>k<sup>th</sup></code> <strong>smallest</strong> amount that can be made using these coins.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; ">coins = [3,6,9], k = 3</span></p> <p><strong>Output:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 9</span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 3 produces multiples of 3: 3, 6, 9, 12, 15, etc.<br /> Coin 6 produces multiples of 6: 6, 12, 18, 24, etc.<br /> Coin 9 produces multiples of 9: 9, 18, 27, 36, etc.<br /> All of the coins combined produce: 3, 6, <u><strong>9</strong></u>, 12, 15, etc.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> coins = [5,2], k = 7</span></p> <p><strong>Output:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 12 </span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 5 produces multiples of 5: 5, 10, 15, 20, etc.<br /> Coin 2 produces multiples of 2: 2, 4, 6, 8, 10, 12, etc.<br /> All of the coins combined produce: 2, 4, 5, 6, 8, 10, <u><strong>12</strong></u>, 14, 15, etc.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 15</code></li> <li><code>1 &lt;= coins[i] &lt;= 25</code></li> <li><code>1 &lt;= k &lt;= 2 * 10<sup>9</sup></code></li> <li><code>coins</code> contains pairwise distinct integers.</li> </ul>
3
{ "code": "class Solution {\npublic:\n long long findKthSmallest(vector<int>& coins, int k) {\n const int n = static_cast<int>(coins.size());\n const unsigned int max_mask = 1 << n;\n std::vector<std::vector<long> > size_to_lcm(n + 1);\n for (unsigned int mask = 1; mask < max_mask; ++mask)\n {\n long lcm_coins = 1;\n for (int i = 0; i < n; ++i)\n if (mask >> i & 1)\n lcm_coins = std::lcm(lcm_coins, coins[i]);\n size_to_lcm[std::popcount(mask)].push_back(lcm_coins);\n }\n auto amountDenominations = [&](long m) -> long\n {\n long result = 0;\n for (int sz = 1; sz <= n; ++sz)\n for (const long lcm : size_to_lcm[sz])\n result += m / lcm * (2L * (sz & 1) - 1L);\n return result;\n };\n \n long l = 0, r = static_cast<long>(k) * *std::min_element(coins.begin(), coins.end());\n while (l < r)\n {\n long m = (l + r) / 2;\n if (amountDenominations(m) >= k)\n r = m;\n else l = m + 1;\n }\n return l;\n }\n};", "memory": "22200" }
3,375
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>k</code>.</p> <p>You have an infinite number of coins of each denomination. However, you are <strong>not allowed</strong> to combine coins of different denominations.</p> <p>Return the <code>k<sup>th</sup></code> <strong>smallest</strong> amount that can be made using these coins.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; ">coins = [3,6,9], k = 3</span></p> <p><strong>Output:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 9</span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 3 produces multiples of 3: 3, 6, 9, 12, 15, etc.<br /> Coin 6 produces multiples of 6: 6, 12, 18, 24, etc.<br /> Coin 9 produces multiples of 9: 9, 18, 27, 36, etc.<br /> All of the coins combined produce: 3, 6, <u><strong>9</strong></u>, 12, 15, etc.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> coins = [5,2], k = 7</span></p> <p><strong>Output:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 12 </span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 5 produces multiples of 5: 5, 10, 15, 20, etc.<br /> Coin 2 produces multiples of 2: 2, 4, 6, 8, 10, 12, etc.<br /> All of the coins combined produce: 2, 4, 5, 6, 8, 10, <u><strong>12</strong></u>, 14, 15, etc.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 15</code></li> <li><code>1 &lt;= coins[i] &lt;= 25</code></li> <li><code>1 &lt;= k &lt;= 2 * 10<sup>9</sup></code></li> <li><code>coins</code> contains pairwise distinct integers.</li> </ul>
3
{ "code": "class Solution {\n public:\n long long findKthSmallest(vector<int>& coins, int k) {\n const vector<vector<long>> sizeToLcms = getSizeToLcms(coins);\n long l = 0;\n long r = static_cast<long>(k) * ranges::min(coins);\n\n while (l < r) {\n const long m = (l + r) / 2;\n if (numDenominationsNoGreaterThan(sizeToLcms, m) >= k)\n r = m;\n else\n l = m + 1;\n }\n\n return l;\n }\n\n private:\n // Returns the number of denominations <= m.\n long numDenominationsNoGreaterThan(const vector<vector<long>>& sizeToLcms,\n long m) {\n long res = 0;\n for (int sz = 1; sz < sizeToLcms.size(); ++sz)\n for (const long lcm : sizeToLcms[sz])\n // Principle of Inclusion-Exclusion (PIE)\n res += m / lcm * pow(-1, sz + 1);\n return res;\n };\n\n // Returns the LCMs for each number of combination of coins.\n vector<vector<long>> getSizeToLcms(const vector<int>& coins) {\n const int n = coins.size();\n const int maxMask = 1 << n;\n vector<vector<long>> sizeToLcms(n + 1);\n\n for (unsigned mask = 1; mask < maxMask; ++mask) {\n long lcmOfSelectedCoins = 1;\n for (int i = 0; i < n; ++i)\n if (mask >> i & 1)\n lcmOfSelectedCoins = lcm(lcmOfSelectedCoins, coins[i]);\n sizeToLcms[popcount(mask)].push_back(lcmOfSelectedCoins);\n }\n\n return sizeToLcms;\n }\n};", "memory": "22300" }
3,375
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>k</code>.</p> <p>You have an infinite number of coins of each denomination. However, you are <strong>not allowed</strong> to combine coins of different denominations.</p> <p>Return the <code>k<sup>th</sup></code> <strong>smallest</strong> amount that can be made using these coins.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; ">coins = [3,6,9], k = 3</span></p> <p><strong>Output:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 9</span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 3 produces multiples of 3: 3, 6, 9, 12, 15, etc.<br /> Coin 6 produces multiples of 6: 6, 12, 18, 24, etc.<br /> Coin 9 produces multiples of 9: 9, 18, 27, 36, etc.<br /> All of the coins combined produce: 3, 6, <u><strong>9</strong></u>, 12, 15, etc.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> coins = [5,2], k = 7</span></p> <p><strong>Output:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 12 </span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 5 produces multiples of 5: 5, 10, 15, 20, etc.<br /> Coin 2 produces multiples of 2: 2, 4, 6, 8, 10, 12, etc.<br /> All of the coins combined produce: 2, 4, 5, 6, 8, 10, <u><strong>12</strong></u>, 14, 15, etc.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 15</code></li> <li><code>1 &lt;= coins[i] &lt;= 25</code></li> <li><code>1 &lt;= k &lt;= 2 * 10<sup>9</sup></code></li> <li><code>coins</code> contains pairwise distinct integers.</li> </ul>
3
{ "code": "\nclass Solution {\n public:\n long long findKthSmallest(vector<int>& coins, int k) {\n const vector<vector<long>> sizeToLcms = getSizeToLcms(coins);\n long l = 0;\n long r = static_cast<long>(k) * ranges::min(coins);\n\n while (l < r) {\n const long m = (l + r) / 2;\n if (numDenominationsNoGreaterThan(sizeToLcms, m) >= k)\n r = m;\n else\n l = m + 1;\n }\n\n return l;\n }\n\n private:\n // Returns the number of denominations <= m.\n long numDenominationsNoGreaterThan(const vector<vector<long>>& sizeToLcms,\n long m) {\n long res = 0;\n for (int sz = 1; sz < sizeToLcms.size(); ++sz)\n for (const long lcm : sizeToLcms[sz])\n // Principle of Inclusion-Exclusion (PIE)\n res += m / lcm * pow(-1, sz + 1);\n return res;\n };\n\n // Returns the LCMs for each number of combination of coins.\n vector<vector<long>> getSizeToLcms(const vector<int>& coins) {\n const int n = coins.size();\n const int maxMask = 1 << n;\n vector<vector<long>> sizeToLcms(n + 1);\n\n for (unsigned mask = 1; mask < maxMask; ++mask) {\n long lcmOfSelectedCoins = 1;\n for (int i = 0; i < n; ++i)\n if (mask >> i & 1)\n lcmOfSelectedCoins = lcm(lcmOfSelectedCoins, coins[i]);\n sizeToLcms[popcount(mask)].push_back(lcmOfSelectedCoins);\n }\n\n return sizeToLcms;\n }\n};", "memory": "22400" }
3,375
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>k</code>.</p> <p>You have an infinite number of coins of each denomination. However, you are <strong>not allowed</strong> to combine coins of different denominations.</p> <p>Return the <code>k<sup>th</sup></code> <strong>smallest</strong> amount that can be made using these coins.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; ">coins = [3,6,9], k = 3</span></p> <p><strong>Output:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 9</span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 3 produces multiples of 3: 3, 6, 9, 12, 15, etc.<br /> Coin 6 produces multiples of 6: 6, 12, 18, 24, etc.<br /> Coin 9 produces multiples of 9: 9, 18, 27, 36, etc.<br /> All of the coins combined produce: 3, 6, <u><strong>9</strong></u>, 12, 15, etc.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> coins = [5,2], k = 7</span></p> <p><strong>Output:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 12 </span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 5 produces multiples of 5: 5, 10, 15, 20, etc.<br /> Coin 2 produces multiples of 2: 2, 4, 6, 8, 10, 12, etc.<br /> All of the coins combined produce: 2, 4, 5, 6, 8, 10, <u><strong>12</strong></u>, 14, 15, etc.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 15</code></li> <li><code>1 &lt;= coins[i] &lt;= 25</code></li> <li><code>1 &lt;= k &lt;= 2 * 10<sup>9</sup></code></li> <li><code>coins</code> contains pairwise distinct integers.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<pair<int, long long>> lcms;\n\n long long gcd(long long a, long long b) {\n if (a < b) {\n long long tmp = a; \n a = b; \n b = tmp;\n }\n while (a % b != 0) {\n long long r = a% b;\n a = b;\n b = r;\n }\n return b;\n }\n\n long long lcm(long long a, long long b) {\n return (a / gcd(a, b)) * b;\n }\n\n long long count_denom(long long value, vector<int> &coins, int k) {\n int sz = lcms.size();\n long long cnt = 0;\n\n for (int i = 0; i < sz; ++i) {\n long long lcm = lcms[i].second;\n int sign = 1;\n if (lcms[i].first % 2 == 0) sign = -1;\n\n cnt += (value / lcm)*sign;\n }\n return cnt;\n }\n\n void init_lcms(vector<int> &coins) {\n lcms = vector<pair<int, long long>> (0);\n int n = coins.size();\n\n for (int i = 1; i < (1 << n); ++i) {\n long long lcm_i = 1; \n int x = i;\n int cnt = 0;\n for (int b = 0; b < n; ++b) {\n if (x % 2 == 1) {\n lcm_i = lcm(lcm_i, coins[b]);\n ++cnt;\n }\n x = (x >> 1);\n }\n lcms.push_back({cnt, lcm_i});\n }\n }\n\n long long findKthSmallest(vector<int>& coins, int k) {\n int n = coins.size();\n init_lcms(coins);\n\n int min_coin = coins[0];\n for (int i = 1; i < n; ++i)\n if (coins[i] < min_coin) min_coin = coins[i];\n\n long long l = 1, r = k;\n while (l <= r) {\n long long t = (l + r)/2; \n long long max_trial = min_coin * t;\n long long cnt_trial = count_denom(max_trial, coins, k);\n\n if (cnt_trial < k) {\n l = t+1;\n continue;\n }\n for (int i = 0; i < n; ++i) {\n long long trial = (max_trial / coins[i]) * coins[i];\n long long cnt_trial = count_denom(trial, coins, k);\n\n if (cnt_trial == k)\n return trial;\n }\n r = t - 1;\n }\n return -1;\n }\n};", "memory": "22500" }
3,375
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>k</code>.</p> <p>You have an infinite number of coins of each denomination. However, you are <strong>not allowed</strong> to combine coins of different denominations.</p> <p>Return the <code>k<sup>th</sup></code> <strong>smallest</strong> amount that can be made using these coins.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; ">coins = [3,6,9], k = 3</span></p> <p><strong>Output:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 9</span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 3 produces multiples of 3: 3, 6, 9, 12, 15, etc.<br /> Coin 6 produces multiples of 6: 6, 12, 18, 24, etc.<br /> Coin 9 produces multiples of 9: 9, 18, 27, 36, etc.<br /> All of the coins combined produce: 3, 6, <u><strong>9</strong></u>, 12, 15, etc.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> coins = [5,2], k = 7</span></p> <p><strong>Output:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 12 </span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 5 produces multiples of 5: 5, 10, 15, 20, etc.<br /> Coin 2 produces multiples of 2: 2, 4, 6, 8, 10, 12, etc.<br /> All of the coins combined produce: 2, 4, 5, 6, 8, 10, <u><strong>12</strong></u>, 14, 15, etc.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 15</code></li> <li><code>1 &lt;= coins[i] &lt;= 25</code></li> <li><code>1 &lt;= k &lt;= 2 * 10<sup>9</sup></code></li> <li><code>coins</code> contains pairwise distinct integers.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<pair<int, long long>> lcms;\n\n long long gcd(long long a, long long b) {\n if (a < b) {\n long long tmp = a; \n a = b; \n b = tmp;\n }\n while (a % b != 0) {\n long long r = a % b;\n a = b;\n b = r;\n }\n return b;\n }\n\n long long lcm(long long a, long long b) {\n return (a / gcd(a, b)) * b;\n }\n\n long long count_denom(long long value, vector<int> &coins, int k) {\n int sz = lcms.size();\n long long cnt = 0;\n\n for (int i = 0; i < sz; ++i) {\n long long lcm = lcms[i].second;\n int sign = 1;\n if (lcms[i].first % 2 == 0) sign = -1;\n\n cnt += (value / lcm)*sign;\n }\n return cnt;\n }\n\n void init_lcms(vector<int> &coins) {\n lcms = vector<pair<int, long long>> (0);\n int n = coins.size();\n\n for (int i = 1; i < (1 << n); ++i) {\n long long lcm_i = 1; \n int x = i;\n int cnt = 0;\n for (int b = 0; b < n; ++b) {\n if (x % 2 == 1) {\n lcm_i = lcm(lcm_i, coins[b]);\n ++cnt;\n }\n x = (x >> 1);\n }\n lcms.push_back({cnt, lcm_i});\n }\n }\n\n long long findKthSmallest(vector<int>& coins, int k) {\n int n = coins.size();\n init_lcms(coins);\n\n int min_coin = coins[0];\n int j = 0;\n for (int i = 1; i < n; ++i)\n if (coins[i] < min_coin) {\n min_coin = coins[i];\n j = i;\n }\n coins[j] = coins[0]; coins[0] = min_coin;\n\n long long l = 1, r = k;\n while (l <= r) {\n long long t = (l + r)/2; \n long long max_trial = min_coin * t;\n long long cnt_trial = count_denom(max_trial, coins, k);\n\n if (cnt_trial < k) {\n l = t+1;\n continue;\n }\n if (cnt_trial == k)\n return max_trial;\n\n for (int i = 1; i < n; ++i) {\n long long trial = (max_trial / coins[i]) * coins[i];\n long long cnt_trial = count_denom(trial, coins, k);\n\n if (cnt_trial == k)\n return trial;\n }\n r = t - 1;\n }\n return -1;\n }\n};", "memory": "22500" }
3,375
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>k</code>.</p> <p>You have an infinite number of coins of each denomination. However, you are <strong>not allowed</strong> to combine coins of different denominations.</p> <p>Return the <code>k<sup>th</sup></code> <strong>smallest</strong> amount that can be made using these coins.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; ">coins = [3,6,9], k = 3</span></p> <p><strong>Output:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 9</span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 3 produces multiples of 3: 3, 6, 9, 12, 15, etc.<br /> Coin 6 produces multiples of 6: 6, 12, 18, 24, etc.<br /> Coin 9 produces multiples of 9: 9, 18, 27, 36, etc.<br /> All of the coins combined produce: 3, 6, <u><strong>9</strong></u>, 12, 15, etc.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> coins = [5,2], k = 7</span></p> <p><strong>Output:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 12 </span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 5 produces multiples of 5: 5, 10, 15, 20, etc.<br /> Coin 2 produces multiples of 2: 2, 4, 6, 8, 10, 12, etc.<br /> All of the coins combined produce: 2, 4, 5, 6, 8, 10, <u><strong>12</strong></u>, 14, 15, etc.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 15</code></li> <li><code>1 &lt;= coins[i] &lt;= 25</code></li> <li><code>1 &lt;= k &lt;= 2 * 10<sup>9</sup></code></li> <li><code>coins</code> contains pairwise distinct integers.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<pair<int, long long>> lcms;\n\n long long gcd(long long a, long long b) {\n if (a < b) {\n long long tmp = a; \n a = b; \n b = tmp;\n }\n while (a % b != 0) {\n long long r = a% b;\n a = b;\n b = r;\n }\n return b;\n }\n\n long long lcm(long long a, long long b) {\n return (a / gcd(a, b)) * b;\n }\n\n long long count_denom(long long value, vector<int> &coins, int k) {\n int sz = lcms.size();\n long long cnt = 0;\n\n for (int i = 0; i < sz; ++i) {\n long long lcm = lcms[i].second;\n int sign = 1;\n if (lcms[i].first % 2 == 0) sign = -1;\n\n cnt += (value / lcm)*sign;\n }\n return cnt;\n }\n\n void init_lcms(vector<int> &coins) {\n lcms = vector<pair<int, long long>> (0);\n int n = coins.size();\n\n for (int i = 1; i < (1 << n); ++i) {\n long long lcm_i = 1; \n int x = i;\n int cnt = 0;\n for (int b = 0; b < n; ++b) {\n if (x % 2 == 1) {\n lcm_i = lcm(lcm_i, coins[b]);\n ++cnt;\n }\n x = (x >> 1);\n }\n lcms.push_back({cnt, lcm_i});\n }\n // sort(lcms.begin(), lcms.end());\n }\n\n long long findKthSmallest(vector<int>& coins, int k) {\n int n = coins.size();\n sort(coins.begin(), coins.end());\n init_lcms(coins);\n\n long long l = 1, r = k;\n while (l <= r) {\n long long t = (l + r)/2; //res = a * t where a is value of a coin and t <= k\n bool is_left = false;\n long long max_trial = coins[0] * t;\n for (int i = 0; i < n; ++i) {\n long long trial = (max_trial / coins[i]) * coins[i];\n\n long long cnt_trial = count_denom(trial, coins, k);\n if (cnt_trial == k)\n return trial;\n else {\n if (cnt_trial > k)\n is_left = true;\n }\n }\n if (is_left)\n r = t - 1;\n else \n l = t + 1;\n }\n return -1;\n }\n};", "memory": "22600" }
3,375
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>k</code>.</p> <p>You have an infinite number of coins of each denomination. However, you are <strong>not allowed</strong> to combine coins of different denominations.</p> <p>Return the <code>k<sup>th</sup></code> <strong>smallest</strong> amount that can be made using these coins.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; ">coins = [3,6,9], k = 3</span></p> <p><strong>Output:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 9</span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 3 produces multiples of 3: 3, 6, 9, 12, 15, etc.<br /> Coin 6 produces multiples of 6: 6, 12, 18, 24, etc.<br /> Coin 9 produces multiples of 9: 9, 18, 27, 36, etc.<br /> All of the coins combined produce: 3, 6, <u><strong>9</strong></u>, 12, 15, etc.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> coins = [5,2], k = 7</span></p> <p><strong>Output:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 12 </span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 5 produces multiples of 5: 5, 10, 15, 20, etc.<br /> Coin 2 produces multiples of 2: 2, 4, 6, 8, 10, 12, etc.<br /> All of the coins combined produce: 2, 4, 5, 6, 8, 10, <u><strong>12</strong></u>, 14, 15, etc.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 15</code></li> <li><code>1 &lt;= coins[i] &lt;= 25</code></li> <li><code>1 &lt;= k &lt;= 2 * 10<sup>9</sup></code></li> <li><code>coins</code> contains pairwise distinct integers.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<pair<int, long long>> lcms;\n\n long long gcd(long long a, long long b) {\n if (a < b) {\n long long tmp = a; \n a = b; \n b = tmp;\n }\n while (a % b != 0) {\n long long r = a% b;\n a = b;\n b = r;\n }\n return b;\n }\n\n long long lcm(long long a, long long b) {\n return (a / gcd(a, b)) * b;\n }\n\n long long count_denom(long long value, vector<int> &coins, int k) {\n int sz = lcms.size();\n long long cnt = 0;\n\n for (int i = 0; i < sz; ++i) {\n long long lcm = lcms[i].second;\n int sign = 1;\n if (lcms[i].first % 2 == 0) sign = -1;\n\n cnt += (value / lcm)*sign;\n }\n return cnt;\n }\n\n void init_lcms(vector<int> &coins) {\n lcms = vector<pair<int, long long>> (0);\n int n = coins.size();\n\n for (int i = 1; i < (1 << n); ++i) {\n long long lcm_i = 1; \n int x = i;\n int cnt = 0;\n for (int b = 0; b < n; ++b) {\n if (x % 2 == 1) {\n lcm_i = lcm(lcm_i, coins[b]);\n ++cnt;\n }\n x = (x >> 1);\n }\n lcms.push_back({cnt, lcm_i});\n }\n }\n\n long long findKthSmallest(vector<int>& coins, int k) {\n int n = coins.size();\n init_lcms(coins);\n\n int min_coin = coins[0];\n for (int i = 1; i < n; ++i)\n if (coins[i] < min_coin) min_coin = coins[i];\n\n long long l = 1, r = k;\n while (l <= r) {\n long long t = (l + r)/2; \n long long max_trial = min_coin * t;\n long long cnt_trial = count_denom(max_trial, coins, k);\n\n if (cnt_trial < k) {\n l = t+1;\n continue;\n }\n for (int i = 0; i < n; ++i) {\n long long trial = (max_trial / coins[i]) * coins[i];\n long long cnt_trial = count_denom(trial, coins, k);\n\n if (cnt_trial == k)\n return trial;\n }\n r = t - 1;\n }\n return -1;\n }\n};", "memory": "22600" }
3,375
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>k</code>.</p> <p>You have an infinite number of coins of each denomination. However, you are <strong>not allowed</strong> to combine coins of different denominations.</p> <p>Return the <code>k<sup>th</sup></code> <strong>smallest</strong> amount that can be made using these coins.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; ">coins = [3,6,9], k = 3</span></p> <p><strong>Output:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 9</span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 3 produces multiples of 3: 3, 6, 9, 12, 15, etc.<br /> Coin 6 produces multiples of 6: 6, 12, 18, 24, etc.<br /> Coin 9 produces multiples of 9: 9, 18, 27, 36, etc.<br /> All of the coins combined produce: 3, 6, <u><strong>9</strong></u>, 12, 15, etc.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> coins = [5,2], k = 7</span></p> <p><strong>Output:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 12 </span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 5 produces multiples of 5: 5, 10, 15, 20, etc.<br /> Coin 2 produces multiples of 2: 2, 4, 6, 8, 10, 12, etc.<br /> All of the coins combined produce: 2, 4, 5, 6, 8, 10, <u><strong>12</strong></u>, 14, 15, etc.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 15</code></li> <li><code>1 &lt;= coins[i] &lt;= 25</code></li> <li><code>1 &lt;= k &lt;= 2 * 10<sup>9</sup></code></li> <li><code>coins</code> contains pairwise distinct integers.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<pair<int, long long>> lcms;\n\n long long gcd(long long a, long long b) {\n if (a < b) {\n long long tmp = a; \n a = b; \n b = tmp;\n }\n while (a % b != 0) {\n long long r = a% b;\n a = b;\n b = r;\n }\n return b;\n }\n\n long long lcm(long long a, long long b) {\n return (a / gcd(a, b)) * b;\n }\n\n long long count_denom(long long value, vector<int> &coins, int k) {\n int sz = lcms.size();\n long long cnt = 0;\n\n for (int i = 0; i < sz; ++i) {\n long long lcm = lcms[i].second;\n int sign = 1;\n if (lcms[i].first % 2 == 0) sign = -1;\n\n cnt += (value / lcm)*sign;\n }\n return cnt;\n }\n\n void init_lcms(vector<int> &coins) {\n lcms = vector<pair<int, long long>> (0);\n int n = coins.size();\n\n for (int i = 1; i < (1 << n); ++i) {\n long long lcm_i = 1; \n int x = i;\n int cnt = 0;\n for (int b = 0; b < n; ++b) {\n if (x % 2 == 1) {\n lcm_i = lcm(lcm_i, coins[b]);\n ++cnt;\n }\n x = (x >> 1);\n }\n lcms.push_back({cnt, lcm_i});\n }\n\n sort(lcms.begin(), lcms.end());\n }\n\n long long findKthSmallest(vector<int>& coins, int k) {\n int n = coins.size();\n sort(coins.begin(), coins.end());\n init_lcms(coins);\n\n long long l = 1, r = k;\n while (l <= r) {\n long long t = (l + r)/2; //res = a * t where a is value of a coin and t <= k\n bool is_left = false;\n long long max_trial = coins[0] * t;\n for (int i = 0; i < n; ++i) {\n long long trial = (max_trial / coins[i]) * coins[i];\n\n long long cnt_trial = count_denom(trial, coins, k);\n if (cnt_trial == k)\n return trial;\n else {\n if (cnt_trial > k)\n is_left = true;\n }\n }\n if (is_left)\n r = t - 1;\n else \n l = t + 1;\n }\n return -1;\n }\n};", "memory": "22700" }
3,375
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>k</code>.</p> <p>You have an infinite number of coins of each denomination. However, you are <strong>not allowed</strong> to combine coins of different denominations.</p> <p>Return the <code>k<sup>th</sup></code> <strong>smallest</strong> amount that can be made using these coins.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; ">coins = [3,6,9], k = 3</span></p> <p><strong>Output:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 9</span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 3 produces multiples of 3: 3, 6, 9, 12, 15, etc.<br /> Coin 6 produces multiples of 6: 6, 12, 18, 24, etc.<br /> Coin 9 produces multiples of 9: 9, 18, 27, 36, etc.<br /> All of the coins combined produce: 3, 6, <u><strong>9</strong></u>, 12, 15, etc.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> coins = [5,2], k = 7</span></p> <p><strong>Output:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 12 </span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 5 produces multiples of 5: 5, 10, 15, 20, etc.<br /> Coin 2 produces multiples of 2: 2, 4, 6, 8, 10, 12, etc.<br /> All of the coins combined produce: 2, 4, 5, 6, 8, 10, <u><strong>12</strong></u>, 14, 15, etc.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 15</code></li> <li><code>1 &lt;= coins[i] &lt;= 25</code></li> <li><code>1 &lt;= k &lt;= 2 * 10<sup>9</sup></code></li> <li><code>coins</code> contains pairwise distinct integers.</li> </ul>
3
{ "code": "int gcd(int a, int b){\n if(a == 0) return b;\n else if(b == 0) return a;\n if(a == b) return a;\n int mini = min(a, b);\n b = max(a, b);\n a = mini;\n\n return gcd(b%a, a);\n}\n\nclass Solution {\npublic:\n long long findKthSmallest(vector<int>& C, int k) {\n #define int long long\n long long l = 0;\n long long kk = k;\n long long r = 1LL*C[0]*k;\n\n int ans = 0;\n\n int n = C.size();\n\n vector<vector<int>> V(n+1);\n vector<int> L(1<<n, 0);\n\n for(int i = 1; i < (1<<n); i++){\n long long pro = 1;\n int g = 0;\n\n int now = __builtin_popcount(i);\n\n for(int j = 0; j < n; j++){\n \n if(((i>>j)&1) && now == 1){\n pro = C[j];\n break;\n }\n else if(((i>>j)&1)){\n int ind = (i^(1LL<<j));\n // cout<<ind<<\" AAAAAAAa\\n\";\n g = gcd(C[j], L[ind]);\n // cout<<C\n\n pro = C[j]*L[ind];\n pro = pro/g;\n break;\n }\n }\n \n \n \n V[now].push_back(pro);\n // cout<<i<<\" \"<<pro<<endl;\n L[i] = pro;\n \n }\n\n while(l <= r){\n int mid = (l+r)/2;\n\n int cnt = 0;\n // for(int i = 0; i < n; i++){\n // int tem = mid/C[i];\n // cnt = cnt + tem;\n // }\n\n for(int i = 1; i <= n; i++){\n int b = i;\n for(auto u: V[i]){\n int now = mid/u;\n if(b%2 == 0) cnt = cnt - now;\n else cnt = cnt + now;\n }\n }\n\n if(cnt >= k){\n ans = mid;\n r = mid-1;\n }\n else l = mid+1;\n }\n #undef int\n return ans;\n }\n};", "memory": "22800" }
3,375
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>k</code>.</p> <p>You have an infinite number of coins of each denomination. However, you are <strong>not allowed</strong> to combine coins of different denominations.</p> <p>Return the <code>k<sup>th</sup></code> <strong>smallest</strong> amount that can be made using these coins.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; ">coins = [3,6,9], k = 3</span></p> <p><strong>Output:</strong> <span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 9</span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 3 produces multiples of 3: 3, 6, 9, 12, 15, etc.<br /> Coin 6 produces multiples of 6: 6, 12, 18, 24, etc.<br /> Coin 9 produces multiples of 9: 9, 18, 27, 36, etc.<br /> All of the coins combined produce: 3, 6, <u><strong>9</strong></u>, 12, 15, etc.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block" style=" border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem; "> <p><strong>Input:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> coins = [5,2], k = 7</span></p> <p><strong>Output:</strong><span class="example-io" style=" font-family: Menlo,sans-serif; font-size: 0.85rem; "> 12 </span></p> <p><strong>Explanation:</strong> The given coins can make the following amounts:<br /> Coin 5 produces multiples of 5: 5, 10, 15, 20, etc.<br /> Coin 2 produces multiples of 2: 2, 4, 6, 8, 10, 12, etc.<br /> All of the coins combined produce: 2, 4, 5, 6, 8, 10, <u><strong>12</strong></u>, 14, 15, etc.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 15</code></li> <li><code>1 &lt;= coins[i] &lt;= 25</code></li> <li><code>1 &lt;= k &lt;= 2 * 10<sup>9</sup></code></li> <li><code>coins</code> contains pairwise distinct integers.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<pair<long long, int>> lcms;\n\n long long gcd(long long a, long long b) {\n if (a < b) {\n long long tmp = a; \n a = b; \n b = tmp;\n }\n while (a % b != 0) {\n long long r = a % b;\n a = b;\n b = r;\n }\n return b;\n }\n\n long long lcm(long long a, long long b) {\n return (a / gcd(a, b)) * b;\n }\n\n long long count_denom(long long value, vector<int> &coins, int k) {\n int sz = lcms.size();\n long long cnt = 0;\n\n for (int i = 0; i < sz; ++i) {\n long long lcm = lcms[i].first;\n if (value < lcm) break;\n int sign = 1;\n if (lcms[i].second % 2 == 0) sign = -1;\n\n cnt += (value / lcm)*sign;\n }\n return cnt;\n }\n\n void init_lcms(vector<int> &coins) {\n lcms = vector<pair<long long, int>> (0);\n int n = coins.size();\n\n for (int i = 1; i < (1 << n); ++i) {\n long long lcm_i = 1; \n int x = i;\n int cnt = 0;\n for (int b = 0; b < n; ++b) {\n if (x % 2 == 1) {\n lcm_i = lcm(lcm_i, coins[b]);\n ++cnt;\n }\n x = (x >> 1);\n }\n lcms.push_back({lcm_i, cnt});\n }\n sort(lcms.begin(), lcms.end());\n }\n\n long long findKthSmallest(vector<int>& coins, int k) {\n int n = coins.size();\n init_lcms(coins);\n\n int min_coin = coins[0];\n int j = 0;\n for (int i = 1; i < n; ++i)\n if (coins[i] < min_coin) {\n min_coin = coins[i];\n j = i;\n }\n coins[j] = coins[0]; coins[0] = min_coin;\n\n long long l = 1, r = k;\n while (l <= r) {\n long long t = (l + r)/2; \n long long max_trial = min_coin * t;\n long long cnt_trial = count_denom(max_trial, coins, k);\n\n if (cnt_trial < k) {\n l = t+1;\n continue;\n }\n if (cnt_trial == k)\n return max_trial;\n\n for (int i = 1; i < n; ++i) {\n long long trial = (max_trial / coins[i]) * coins[i];\n long long cnt_trial = count_denom(trial, coins, k);\n\n if (cnt_trial == k)\n return trial;\n }\n r = t - 1;\n }\n return -1;\n }\n};", "memory": "22900" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
0
{ "code": "#include <bits/stdc++.h>\n\nusing namespace std;\ntypedef long long ll;\n#define int ll\n\nconst int N = 1e5 + 1;\nint n;\nint and_tree[2 * N];\nint min_tree[2 * N];\nint dp[2 * N];\n\nfunction<int(int, int)> min_op = [](int a, int b)\n{ return min(a, b); };\nfunction<int(int, int)> and_op = [](int a, int b)\n{ return a & b; };\n\nvoid build(int *t, function<int(int, int)> &operation)\n{ // build the tree\n for (int i = n - 1; i > 0; --i)\n t[i] = operation(t[i << 1], t[i << 1 | 1]);\n}\n\nvoid modify(int p, int value, int *t, function<int(int, int)> &operation)\n{ // set value at position p\n for (t[p += n] = value; p > 1; p >>= 1)\n t[p >> 1] = operation(t[p], t[p ^ 1]);\n}\n\nint query(int l, int r, int *t, function<int(int, int)> &operation, int start = 0)\n{ // sum on interval [l, r)\n int res = start;\n for (l += n, r += n; l < r; l >>= 1, r >>= 1)\n {\n if (l & 1)\n res = operation(res, t[l++]);\n if (r & 1)\n res = operation(res, t[--r]);\n }\n return res;\n}\n\nint ones = (1 << 30) - 1;\n\nclass Solution\n{\npublic:\n #undef int\n int minimumValueSum(vector<int> &nums, vector<int> &andValues)\n {\n #define int ll\n n = nums.size();\n int m = andValues.size();\n for (int i = 0; i < n; ++i)\n {\n and_tree[i + n] = nums[i];\n min_tree[i + n] = ll(1e10);\n }\n build(and_tree, and_op);\n memset(dp, 0x3f, sizeof(dp));\n for (int j = 1; j <= m; ++j)\n {\n for (int i = 0; i < n; ++i)\n {\n min_tree[i + n] = dp[i];\n }\n build(min_tree, min_op);\n memset(dp, 0x3f, sizeof(dp));\n for (int i = 1; i <= n; ++i)\n {\n int lo = -1, hi = i;\n while (lo + 1 < hi)\n {\n int mid = (lo + hi) >> 1;\n if (query(mid, i, and_tree, and_op, ones) <= andValues[j - 1])\n lo = mid;\n else\n hi = mid;\n }\n int closest_point = lo;\n lo = -1, hi = i;\n while (lo + 1 < hi)\n {\n int mid = (lo + hi) >> 1;\n if (query(mid, i, and_tree, and_op, ones) < andValues[j - 1])\n lo = mid;\n else\n hi = mid;\n }\n int furthest_point = lo;\n if (closest_point - furthest_point > 0)\n {\n if (j == 1 && furthest_point == -1)\n {\n dp[i - 1] = nums[i - 1];\n }\n else\n {\n furthest_point = max(furthest_point, 0ll);\n dp[i - 1] = query(furthest_point, closest_point, min_tree, min_op, int(1e10)) + nums[i - 1];\n }\n }\n }\n }\n if (dp[n - 1] > 1e9) return -1;\n return dp[n - 1];\n }\n};\n#undef int", "memory": "45617" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
0
{ "code": "#include <bits/stdc++.h>\n\nusing namespace std;\ntypedef long long ll;\n#define int ll\n\nconst int N = 1e5 + 1;\nint n;\nint and_tree[2 * N];\nint min_tree[2 * N];\nint dp[2 * N];\n\nfunction<int(int, int)> min_op = [](int a, int b)\n{ return min(a, b); };\nfunction<int(int, int)> and_op = [](int a, int b)\n{ return a & b; };\n\nvoid build(int *t, function<int(int, int)> &operation)\n{ // build the tree\n for (int i = n - 1; i > 0; --i)\n t[i] = operation(t[i << 1], t[i << 1 | 1]);\n}\n\nvoid modify(int p, int value, int *t, function<int(int, int)> &operation)\n{ // set value at position p\n for (t[p += n] = value; p > 1; p >>= 1)\n t[p >> 1] = operation(t[p], t[p ^ 1]);\n}\n\nint query(int l, int r, int *t, function<int(int, int)> &operation, int start = 0)\n{ // sum on interval [l, r)\n int res = start;\n for (l += n, r += n; l < r; l >>= 1, r >>= 1)\n {\n if (l & 1)\n res = operation(res, t[l++]);\n if (r & 1)\n res = operation(res, t[--r]);\n }\n return res;\n}\n\nint ones = (1 << 30) - 1;\n\nclass Solution\n{\npublic:\n #undef int\n int minimumValueSum(vector<int> &nums, vector<int> &andValues)\n {\n #define int ll\n n = nums.size();\n int m = andValues.size();\n for (int i = 0; i < n; ++i)\n {\n and_tree[i + n] = nums[i];\n min_tree[i + n] = ll(1e10);\n }\n build(and_tree, and_op);\n memset(dp, 0x3f, sizeof(dp));\n for (int j = 1; j <= m; ++j)\n {\n for (int i = 0; i < n; ++i)\n {\n min_tree[i + n] = dp[i];\n }\n build(min_tree, min_op);\n memset(dp, 0x3f, sizeof(dp));\n for (int i = 1; i <= n; ++i)\n {\n int lo = -1, hi = i;\n while (lo + 1 < hi)\n {\n int mid = (lo + hi) >> 1;\n if (query(mid, i, and_tree, and_op, ones) <= andValues[j - 1])\n lo = mid;\n else\n hi = mid;\n }\n int closest_point = lo;\n lo = -1, hi = i;\n while (lo + 1 < hi)\n {\n int mid = (lo + hi) >> 1;\n if (query(mid, i, and_tree, and_op, ones) < andValues[j - 1])\n lo = mid;\n else\n hi = mid;\n }\n int furthest_point = lo;\n if (closest_point - furthest_point > 0)\n {\n if (j == 1 && furthest_point == -1)\n {\n dp[i - 1] = nums[i - 1];\n }\n else\n {\n furthest_point = max(furthest_point, 0ll);\n dp[i - 1] = query(furthest_point, closest_point, min_tree, min_op, int(1e10)) + nums[i - 1];\n }\n }\n }\n }\n if (dp[n - 1] > 1e9) return -1;\n return dp[n - 1];\n }\n};\n#undef int", "memory": "45617" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int n,m;\n int minValue(int start, int part, vector<int>& nums, vector<int>& andValues, vector<vector<int>>& dp){\n\n if(start == n){\n if(part == m) return 0;\n return 1e9;\n }\n if(part == m){\n if((andValues[part - 1] & nums[start]) == andValues[part - 1]){\n \n return nums[start] - nums[start-1] + minValue(start + 1, part, nums, andValues, dp); \n }\n return 1e9;\n }\n if(dp[start][part] != -1){\n return dp[start][part];\n }\n int minsum = 1e9;\n if(part > 0 && ((andValues[part - 1] & nums[start]) == andValues[part - 1])){\n\n minsum= min(minsum, nums[start] - nums[start-1] + minValue(start + 1, part, nums, andValues, dp));\n }\n \n int i= start + 1;\n int andval = nums[start];\n while(i < n && andval > andValues[part]){\n andval &= nums[i];\n i++;\n }\n if(andval == andValues[part]){\n\n minsum= min(minsum, nums[i - 1] + minValue(i, part + 1, nums, andValues, dp));\n }\n dp[start][part]= minsum;\n return minsum;\n\n }\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n \n n= nums.size();\n m= andValues.size();\n vector<vector<int>>dp(n, vector<int>(m, -1));\n int ans= minValue(0, 0, nums, andValues, dp);\n\n return ans == 1e9 ? -1 : ans;\n }\n};", "memory": "51852" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
0
{ "code": "#include <bits/stdc++.h>\n#define dbg(x) cout << #x << \": \" << x << endl\n\nconst int INF = 1e9 + 9;\nconst int NEUTRAL = (1LL << 31) - 1;\n\nclass Solution {\npublic:\n\n struct MinDeque {\n stack<pair<int, int>> left, right;\n\n MinDeque() {\n left = stack<pair<int, int>>();\n right = stack<pair<int, int>>();\n }\n\n int size() {\n return left.size() + right.size();\n }\n\n void push(int x) {\n if (right.empty()) {\n right.push(make_pair(x, x));\n } else {\n int topMin = right.top().second;\n right.push(make_pair(x, min(x, topMin)));\n }\n }\n\n void pop() {\n if (left.empty()) {\n while (!right.empty()) {\n int x = right.top().first;\n right.pop();\n if (left.empty()) {\n left.push(make_pair(x, x));\n } else {\n int topMin = left.top().second;\n left.push(make_pair(x, min(x, topMin)));\n }\n }\n }\n if (left.empty()) {\n cout << \"popping from empty\";\n }\n left.pop();\n }\n\n int get() {\n if (left.empty() && right.empty()) {\n cout << \"getting from both empty\";\n return INF;\n }\n if (left.empty()) {\n return right.top().second;\n }\n if (right.empty()) {\n return left.top().second;\n }\n return min(left.top().second, right.top().second);\n }\n\n void clear() {\n while (!left.empty()) {\n left.pop();\n }\n while (!right.empty()) {\n right.pop();\n }\n }\n };\n\n void buildSparse(vector<int>& nums, vector<vector<int>>& sparseAnd, int lg, int n, vector<int>& pw) {\n for (int i = 0; i < n; i++) {\n sparseAnd[0][i] = nums[i];\n }\n for (int h = 0; h < lg - 1; h++) {\n for (int i = 0; i < n; i++) {\n int nxt = min(i + (1 << h), n - 1);\n sparseAnd[h + 1][i] = (sparseAnd[h][i] & sparseAnd[h][nxt]);\n }\n }\n pw[1] = 0;\n for (int len = 2; len <= n; len++) {\n pw[len] = pw[len >> 1] + 1;\n }\n }\n\n struct SegTree {\n int n;\n vector<int> mn;\n\n SegTree(int n): n(n) {\n mn = vector<int>(n << 2, INF);\n }\n\n void update(int pos, int value) {\n update(pos, value, 0, 0, n);\n }\n\n void update(int pos, int value, int vertex, int left, int right) {\n // cout << \"pos: \" << pos << \" value: \" << value << \" left: \" << left << \" right: \" << right << endl;\n if (right - left == 1) {\n // if (left == 2) {\n // dbg(value);\n // }\n mn[vertex] = value;\n return;\n }\n int mid = (left + right) >> 1;\n if (pos < mid) {\n update(pos, value, vertex * 2 + 1, left, mid);\n } else {\n update(pos, value, vertex * 2 + 2, mid, right);\n }\n mn[vertex] = min(mn[vertex * 2 + 1], mn[vertex * 2 + 2]);\n }\n\n int get(int left, int right) {\n return get(left, right, 0, 0, n);\n }\n\n int get(int queryLeft, int queryRight, int vertex, int left, int right) {\n // cout << \"Qleft: \" << queryLeft << \" Qright: \" << queryRight << endl;\n // cout << \"left: \" << left << \" right: \" << right << endl;\n if (queryLeft >= right || left >= queryRight) {\n return INF;\n }\n if (queryLeft <= left && right <= queryRight) {\n // cout << \"mn[\" << vertex << \"]: \" << mn[vertex] << endl;\n return mn[vertex];\n }\n int mid = (left + right) >> 1;\n return min(\n get(queryLeft, queryRight, vertex * 2 + 1, left, mid), \n get(queryLeft, queryRight, vertex * 2 + 2, mid, right));\n }\n };\n\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n int n = nums.size();\n int lg = int(log2(n)) + 1;\n vector<vector<int>> sparseAnd(lg, vector<int>(n));\n vector<int> pw(n + 1);\n buildSparse(nums, sparseAnd, lg, n, pw);\n auto getAnd = [&](int left, int right) {\n // cout << \"getAnd: left: \" << left << \" right: \" << right << endl;\n // cout << \"I'm in\" << endl;\n if (left == right) {\n return NEUTRAL;\n }\n int len = right - left;\n int e = pw[len];\n int half1 = sparseAnd[e][left];\n int half2 = sparseAnd[e][right - (1 << e)];\n // cout << \"I'm out\" << endl;\n return (half1 & half2);\n };\n int m = andValues.size();\n vector<int> curDp(n + 1);\n auto lowerBound = [&](int i, int x) {\n int left = 0;\n int right = i;\n int ans = i + 1;\n while (left <= right) {\n int mid = (left + right) >> 1;\n if (getAnd(mid, i + 1) >= x) {\n ans = mid;\n right = mid - 1;\n } else {\n left = mid + 1;\n }\n }\n return ans;\n };\n SegTree st(n + 1);\n st.update(0, 0);\n for (int groups = 1; groups <= m; groups++) {\n int target = andValues[groups - 1];\n vector<int> updates;\n // dbg(groups);\n for (int i = 1; i <= n; i++) {\n // dbg(i);\n int num = nums[i - 1];\n int right = lowerBound(i - 1, target + 1) - 1;\n if (right >= 0 && getAnd(right, i) == target) {\n int left = lowerBound(i - 1, target);\n assert(left >= 0);\n left++;\n right++;\n // dbg(left);\n // dbg(right);\n // int s = (groups == 2 && i == 2);\n int request = st.get(left - 1, right);\n // if (s) {\n // dbg(st.get(0, 1));\n // dbg(st.get(1, 2));\n // dbg(st.get(2, 3));\n // } \n // dbg(request);\n curDp[i] = request + num;\n // dbg(curDp[i]);\n } else {\n // cout << \"fail\" << endl;\n curDp[i] = INF;\n }\n updates.push_back(curDp[i]);\n }\n st.update(0, INF);\n for (int i = 0; i < n; i++) {\n // cout << \"putting \" << updates[i] << \" at \" << i + 1 << endl;\n st.update(i + 1, updates[i]);\n }\n }\n return (curDp[n] >= INF ? -1 : curDp[n]);\n }\n};", "memory": "51852" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
0
{ "code": "\nconst int INF = 0x3f3f3f3f;\nclass Solution {\npublic:\ntemplate<invocable<int,int> Op, int Id>\nstruct SegTree{\n int size;\n vector<int> tr;\n \n SegTree(vector<int> &arr): size(arr.size()), tr(5*size,Id){\n build(arr,1,size,1);\n }\n\n void build(vector<int> &arr, int s, int t, int root) {\n if (s == t) {\n tr[root] = arr[s-1];\n return;\n }\n int m = s + ((t - s) >> 1);\n build(arr, s, m, root * 2), build(arr, m + 1, t, root * 2 + 1);\n tr[root] = Op()(tr[root * 2] , tr[(root * 2) + 1]);\n }\n\n int getsum(int l, int r, int s, int t, int p) {\n if (l <= s && t <= r)\n return tr[p]; \n int m = s + ((t - s) >> 1), sum = Id;\n if (l <= m) sum = Op()(sum,getsum(l, r, s, m, p * 2));\n if (r > m) sum = Op()(sum,getsum(l, r, m + 1, t, p * 2 + 1));\n return sum;\n }\n\n int getsum(int l, int r){\n return getsum(l+1,r+1,1,size,1);\n }\n\n};\n\nstruct mine{\n constexpr int operator()(int a, int b) const{\n return min(a,b);\n }\n};\n\nint queryl(int k, int s, vector<int>&nums, SegTree<bit_and<int>,-1>& andsum){\n int l = 0, r = k, fl = -1;\n while (l <= r) {\n int mid = l + (r - l) / 2, p = andsum.getsum(mid,k);\n if (p == s) fl = mid, r = mid - 1;\n else if (p < s) l = mid + 1;\n else r = mid - 1;\n }\n return fl;\n}\n\nint queryr(int k, int s, vector<int>&nums, SegTree<bit_and<int>,-1>& andsum){\n int l = 0, r = k, fr = -1;\n while (l <= r) {\n int mid = l + (r - l) / 2, p = andsum.getsum(mid,k);\n if (p == s) fr = mid, l = mid + 1;\n else if (p < s) l = mid + 1;\n else r = mid - 1;\n }\n return fr;\n};\n\nint minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n int n = nums.size(), m = andValues.size(), acc = -1;\n int dp[20][10001] = {0};\n \n SegTree<bit_and<int>,-1> andsum(nums);\n\n for(int i = 0; i < n; i++){\n acc &= nums[i];\n if(acc==andValues[0]) dp[0][i] = nums[i];\n else dp[0][i] = INF;\n }\n\n for(int k = 1; k < m; k++){\n vector<int> last(dp[k-1],dp[k-1]+n);\n SegTree<mine,INF> maxt(last); \n dp[k][0]=INF;\n for(int i = 1; i < n; i++){\n int l = queryl(i,andValues[k],nums,andsum), r = queryr(i,andValues[k],nums,andsum);\n if(l==-1||r==-1||r==0) {dp[k][i] = INF; continue;}\n if(l==0) l = 1;\n dp[k][i] = nums[i]+maxt.getsum(l-1,r-1);\n }\n }\n\n if(dp[m-1][n-1]>=INF) return -1;\n return dp[m-1][n-1];\n}\n};", "memory": "58087" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
0
{ "code": "constexpr int kBits = 32;\nconstexpr int kInf = numeric_limits<int>::max();\n\nstruct Segtree {\n vector<int> tv;\n\n Segtree(int len) : tv(4 * len, kInf) {}\n\n void Set(int pos, int value, int l = 0, int r = -1, int v = 0) {\n if (!v) r = tv.size() / 4 - 1;\n\n if (l == r) {\n tv[v] = value;\n return;\n }\n const int m = (l + r) / 2;\n if (pos <= m) {\n Set(pos, value, l, m, 2 * v + 1);\n } else {\n Set(pos, value, m+1, r, 2 * v + 2);\n }\n tv[v] = min(tv[2*v+1], tv[2*v+2]);\n }\n\n int Query(int b, int e, int l = 0, int r = -1, int v = 0) {\n if (!v) r = tv.size() / 4 - 1;\n\n if (b <= l and r <= e) {\n return tv[v];\n }\n if (r < b or e < l) {\n return kInf;\n }\n const int m = (l + r) / 2;\n return min(Query(b, e, l, m, 2 * v + 1), Query(b, e, m + 1, r, 2 * v + 2));\n }\n};\n\nclass Solution {\npublic:\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n // for every `andValue`, and every j, the indices i's\n // such that AND on [i, j] is andValue form a range [l, r].\n vector<vector<int>> bv(nums.size(), vector<int>(kBits, 0));\n for (int i = 0; i < nums.size(); ++i) {\n for (int b = 0; b < kBits; ++b) {\n bv[i][b] = (nums[i] >> b) & 1;\n }\n }\n for (int b = 0; b < kBits; ++b) {\n for (int i = 1; i < nums.size(); ++i) {\n if (bv[i][b]) {\n bv[i][b] += bv[i - 1][b];\n }\n }\n }\n vector<int> next_dp(nums.size() + 1, kInf);\n // dp transition is then max(dp) + nums[j] on the range [l-1, r-1]\n Segtree tree(next_dp.size());\n tree.Set(0, 0);\n for (int lv : andValues) {\n std::fill(begin(next_dp), end(next_dp), kInf);\n for (int i = 0; i < nums.size(); ++i) {\n // a. find the range where AND [j, i] is lv\n int min_length = 1, max_length = i + 1;\n for (int b = 0; b < kBits; ++b) {\n if ((lv >> b) & 1) {\n max_length = min(max_length, bv[i][b]);\n } else {\n min_length = max(min_length, bv[i][b] + 1);\n }\n }\n if (max_length < min_length) {\n continue;\n }\n // [(i + 1) - max_length, (i + 1) - min_length] is the possible range\n // b. Take min. TODO: replace this via segment tree\n int prev = tree.Query((i + 1) - max_length, (i + 1) - min_length);\n if (prev < kInf) {\n next_dp[i + 1] = nums[i] + prev;\n }\n }\n for (int i = 0; i < next_dp.size(); ++i) {\n tree.Set(i, next_dp[i]);\n }\n }\n return (next_dp.back() == kInf ? -1 : next_dp.back());\n }\n};\n", "memory": "64322" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int n ;\n vector<int> tree;\n vector<vector<int>> mintree;\n\n void update(vector<vector<int>>& dp,int size ,int j){\n int w = size;\n for(int i=w;i<2*w;i++) mintree[i][j] = dp[i-w][j];\n for(int i=w-1;i>0;i--) mintree[i][j] = min(mintree[i*2][j],mintree[i*2+1][j]);\n\n }\n int mini(int i,int j,int t,int size ){\n i += size;\n j += size;\n \n int answer = mintree[i][t];\n while(i<=j){\n\n if(i%2) {answer = min(answer, mintree[i][t]);i++;}\n if(j%2==0) {answer = min(answer, mintree[j][t]) ;j--; }\n i = i>>1;\n j = j>>1;\n\n\n }\n return answer;\n\n }\n\n\n\n int val(int i ,int j ){\n i += n;\n j += n;\n int answer = tree[i];\n while(i<=j){\n\n if(i%2) {answer &= tree[i];i++;}\n if(j%2==0) {answer &= tree[j];j--;}\n i = i>>1;\n j = j>>1;\n\n\n }\n return answer;\n\n }\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n n = nums.size();\n tree.resize(2*n+1,0);\n int m = andValues.size();\n \n mintree = vector<vector<int>>(2*(n+1)+1,vector<int>(m+2,1e8));\n \n for(int i=n;i<2*n;i++) tree[i]=nums[i-n];\n for(int i =n-1;i>0;i--) tree[i] = tree[2*i]&tree[2*i+1];\n\n vector<vector<int>> dp(n+1,vector<int>(m+1,1e6));\n // for(int i =0;i<=n;i++) dp[i][0]=0;\n // for(int i=0;i<=m;i++) dp[0][i]=0;\n dp[0][0]=0;\n\n update(dp,n+1,0);\n\n for(int j = 1;j<=m;j++){\n\n for(int i =1;i<=n;i++){\n\n int x = i;\n int y = 0;\n int value = 0;\n int start = 0;\n int end = i-1;\n while(start<=end){\n int mid = start+(end-start)/2;\n int d = val(mid,i-1);\n if(d>andValues[j-1]){\n end = mid-1;\n }\n else if(d <= andValues[j-1]){\n if(d==andValues[j-1]) y= mid;\n start = mid+1;\n }\n }\n start = 0;\n end = i-1;\n while(start<=end){\n int mid = start+(end-start)/2;\n int d = val(mid,i-1);\n if(d>=andValues[j-1]){\n if(d==andValues[j-1]) x = mid;\n end = mid-1;\n }\n else if(d<andValues[j-1]){\n \n start = mid+1;\n }\n }\n\n\n // cout<<j<<\" \"<<i<<\" \"<<x<<\" \"<<y<<endl;\n if(x<=y){\n // cout<<x<<\" \"<<y<<\" \"<<i<<endl; \n dp[i][j] = min(dp[i][j], mini(x,y,j-1,n+1)+nums[i-1]);\n // cout<<mini(x,y,j-1,n+1)<<\"itittj\"<<endl;\n\n // for(int a = x;a<=y;a++) dp[i][j] = min(dp[i][j],dp[a][j-1]+nums[i-1]);\n }\n \n \n }\n\n // for(int i =0;i<n+1;i++) cout<<dp[i][j]<<\" \";\n // cout<<endl;\n update(dp,n+1,j);\n \n\n\n }\n\n return dp[n][m]==1e6 ? -1: dp[n][m];\n }\n};", "memory": "70557" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n int n = nums.size();\n int m = andValues.size();\n int inf = 1e9+1;\n\n vector<vector<int> > ans(n, vector<int>(m));\n\n map<int, pair<int, int>> dp;\n for (int i = 0; i < n; i++) {\n // cout << i << endl;\n map<int, pair<int, int>> tmp;\n\n tmp[nums[i]] = make_pair(i, i);\n \n for (auto u : dp) {\n \n int andd = (u.first & nums[i]);\n // cout << andd << \" \";\n if (tmp.count(andd)) {\n tmp[andd] = make_pair(min(tmp[andd].first, u.second.first), max(tmp[andd].second, u.second.second));\n } else {\n tmp[andd] = u.second;\n }\n }\n // for (auto u : dp) {\n // cout << u.first << \" \";\n // }\n // cout << endl;\n // for (auto u : tmp) {\n // cout << u.first << \" \";\n // }\n // cout << endl;\n\n\n for (int j = 0; j < m; j++) {\n ans[i][j] = inf;\n if (dp.count(andValues[j]) && tmp.count(andValues[j])) {\n int s = dp[andValues[j]].second;\n int e = tmp[andValues[j]].second;\n for (int k = s; k < e && j > 0; k++) {\n ans[i][j] = min(ans[k][j-1] + nums[i], ans[i][j]);\n }\n if (ans[i-1][j] != inf) {\n ans[i][j] = min(ans[i-1][j] - nums[i-1] + nums[i], ans[i][j]);\n }\n } \n else if(tmp.count(andValues[j])) {\n int s = tmp[andValues[j]].first-1;\n int e = tmp[andValues[j]].second;\n // cout << s << \" \" << e << \" \" << j << \" \" << andValues[j] << \" \" << i << endl;\n\n if (j > 0) {\n for (int k = s; k < e; k++) {\n if (k >= 0) {\n ans[i][j] = min(ans[k][j-1] + nums[i], ans[i][j]);\n }\n } \n } else if(s == -1) {\n ans[i][j] = nums[i];\n }\n }\n // cout << ans[i][j] << \" i: \" << i << \" j: \" << j << endl;\n }\n // cout << endl;\n swap(dp, tmp);\n }\n\n return ans[n-1][m-1] != inf ? ans[n-1][m-1] : -1;\n }\n};", "memory": "70557" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n void update(vector<int>& tree, int node, int st, int en, int idx, int val) {\n if (st == en) {\n tree[node] = val;\n return;\n }\n int mid = (st + en) / 2;\n if (idx <= mid) {\n update(tree, 2* node + 1, st, mid, idx, val);\n } else {\n update(tree, 2* node + 2, mid + 1, en, idx, val);\n }\n int mini = INT_MAX;\n if (tree[2 * node + 1] != -1) mini = min(mini, tree[2 * node + 1]);\n if (tree[2 * node + 2] != -1) mini = min(mini, tree[2 * node + 2]);\n if (mini == INT_MAX) mini = -1;\n tree[node] = mini;\n }\n int query(vector<int>& tree, int node, int st, int en, int l, int r) {\n if (l <= st && en <= r) {\n return tree[node];\n }\n if (en < l || r < st) {\n return -1;\n }\n int mid = (st + en) / 2;\n int val1 = query(tree, 2 * node + 1, st, mid, l, r);\n int val2 = query(tree, 2 * node + 2, mid + 1, en, l, r);\n int mini = INT_MAX;\n if (val1 != -1) mini = min(mini, val1);\n if (val2 != -1) mini = min(mini, val2);\n if (mini == INT_MAX) mini = -1;\n return mini;\n }\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n int n = nums.size(), m = andValues.size();\n vector<int> tree[m];\n for (int i = 0; i < 4 * n; i++) {\n for (int j = 0; j < m; j++) {\n tree[j].push_back(-1);\n }\n }\n int dp[n][m];\n int preAnd = nums[0];\n for (int i = 0; i < n; i++) {\n preAnd = preAnd & nums[i];\n dp[i][0] = (preAnd == andValues[0]) ? nums[i] : -1;\n update(tree[0], 0, 0, n - 1, i, dp[i][0]);\n int andValueRange[n];\n int and1 = nums[i];\n for (int k = i; k >= 0; k--) {\n and1 = and1 & nums[k];\n andValueRange[k] = and1;\n }\n for (int j = 1; j < m; j++) {\n if (j > i) {\n dp[i][j] = -1;\n continue;\n }\n dp[i][j] = INT_MAX;\n int and1 = nums[i];\n int lo = 1, hi = i;\n while (lo < hi) {\n int mid = (lo + hi) / 2;\n if (andValueRange[mid] >= andValues[j]) {\n hi = mid;\n } else {\n lo = mid + 1;\n }\n }\n int x = hi;\n lo = 1, hi = i;\n int y = -1;\n while (lo <= hi) {\n int mid = (lo + hi) / 2;\n if (andValueRange[mid] < andValues[j]) {\n lo = mid + 1;\n } else if (andValueRange[mid] > andValues[j]) {\n hi = mid - 1;\n } else {\n lo = mid + 1;\n y = mid;\n }\n }\n if (y == -1) {\n dp[i][j] = -1;\n } else {\n int mini = query(tree[j - 1], 0, 0, n - 1, x - 1, y - 1);\n if (mini != -1) {\n dp[i][j] = nums[i] + mini;\n } else {\n dp[i][j] = -1;\n }\n }\n update(tree[j], 0, 0, n - 1, i, dp[i][j]);\n }\n }\n return dp[n - 1][m - 1];\n }\n};", "memory": "76792" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<vector<int>> v;\n vector<vector<int>> t;\n int findRangeAnd(int i,int j)\n {\n int ans=0;\n for(int k=0;k<20;k++)\n {\n if(v[j][k]-(i ? v[i-1][k] : 0)==j-i+1)\n ans|=(1<<k);\n }\n return ans;\n }\n\n pair<int,int> andQueryRange(int i,int val)\n {\n pair<int,int> ans={i,i};\n int n=v.size(),low=i,high=n-1;\n bool flag=0;\n while (low<=high)\n {\n int mid=(low+high)/2;\n int qval=findRangeAnd(i,mid);\n if(qval<=val)\n {\n if(qval==val)\n flag=1;\n ans.first=mid;\n high=mid-1;\n }\n else low=mid+1;\n }\n if(!flag)\n return {-1,-1};\n low=i,high=n-1;\n while (low<=high)\n {\n int mid=(low+high)/2;\n int qval=findRangeAnd(i,mid);\n\n if(qval>=val)\n {\n ans.second=mid;\n low=mid+1;\n }\n else high=mid-1;\n }\n return ans;\n }\n\n void update(vector<int>& t,int idx,int l,int r,int qi,int val)\n {\n if(l>qi || r<qi)\n return;\n if(l==r)\n {\n t[idx]=val;\n return;\n }\n int mid=(l+r)/2;\n update(t,2*idx,l,mid,qi,val);\n update(t,2*idx+1,mid+1,r,qi,val);\n t[idx]=min(t[2*idx],t[2*idx+1]);\n }\n\n int query(vector<int>& t,int idx,int l,int r,int lq,int rq)\n {\n if(l>rq || r<lq)\n return 1e9;\n if(lq<=l && r<=rq)\n return t[idx];\n int mid=(l+r)/2;\n return min(query(t,2*idx,l,mid,lq,rq),query(t,2*idx+1,mid+1,r,lq,rq));\n }\n\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n int n=nums.size(),m=andValues.size();\n v=vector<vector<int>> (n,vector<int> (20));\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<20;j++)\n {\n v[i][j]=(i ? v[i-1][j] : 0)+(1&(nums[i]>>j));\n }\n }\n t=vector<vector<int>> (m+1,vector<int> (4*n+10,1e9));\n for(int i=0;i<n;i++)\n update(t[m],1,0,n-1,i,nums[i]);\n // dp[i][j]= min over k : dp[k+1][j+1]+nums[k] such that And(i,k)==andval[j] \n vector<vector<int>> dp(n,vector<int> (m));\n for(int i=n-1;i>=0;i--)\n {\n for(int j=0;j<m;j++)\n {\n if(j==m-1)\n {\n auto val=findRangeAnd(i,n-1);\n if(val==andValues[j])\n dp[i][j]=nums.back();\n else dp[i][j]=1e9;\n }\n else {\n auto ks=andQueryRange(i,andValues[j]);\n if(ks.first!=-1)\n {\n dp[i][j]=query(t[j+1],1,0,n-1,ks.first,ks.second);\n }\n else dp[i][j]=1e9;\n }\n if(i>=1)\n update(t[j],1,0,n-1,i-1,dp[i][j]+nums[i-1]);\n }\n }\n int ans=dp[0][0];\n if(ans==1e9)\n ans=-1;\n return ans;\n }\n};", "memory": "83027" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
0
{ "code": "void build(int ind,int low, int high, vector<int>&vec_seg, vector<int>&a){\n if(low==high){\n vec_seg[ind] = a[low];\n return;\n }\n int mid = (low+high)/2;\n build(2*ind+1,low,mid,vec_seg,a);\n build(2*ind+2,mid+1,high,vec_seg,a);\n vec_seg[ind] = min(vec_seg[2*ind+1],vec_seg[2*ind+2]);\n}\n\nint query(int ind,int low,int high, int l, int r, vector<int>&vec_seg){\n // cout << ind << \" - \" << low << \" \" << high << \" \" << l << \" \" << r << endl;\n if (l <= low && high <= r)\n return vec_seg[ind];\n if(l > high || r < low) return INT_MAX;\n int mid = (low+high)/2;\n int t1 = query(2*ind+1,low,mid,l,r,vec_seg);\n int t2 = query(2*ind+2,mid+1,high,l,r,vec_seg);\n return min(t1,t2);\n}\n\nvoid update(int ind,int low, int high, vector<int>&vec_seg, int index, int val){\n if(index<low || index>high){return;}\n if(low==high && index==low){\n vec_seg[ind] = val;\n return;\n }\n int mid = (low+high)/2;\n update(2*ind+1,low,mid,vec_seg,index,val);\n update(2*ind+2,mid+1,high,vec_seg,index,val);\n vec_seg[ind] = min(vec_seg[2*ind+1],vec_seg[2*ind+2]);\n}\n\n\nclass Solution {\npublic:\n int minimumValueSum(vector<int>& nums, vector<int>& val) {\n int n = nums.size();\n vector<vector<int>>sm(n,vector<int>(31,-1));\n for(int i=0;i<n;i++){\n for(int j=0;j<31;j++){\n int bit = ((1 << j) & nums[i]) >> j;\n if(bit==0) sm[i][j] = i;\n else {\n if(i>0){\n sm[i][j] = sm[i-1][j];\n }\n }\n }\n }\n\n int m = val.size();\n vector<vector<int>>dp(n,vector<int>(m,1e9));\n vector<vector<int>>vec_seg(m,vector<int>(4*n+4,1e9));\n\n // for dp[i][j] // update(0,0,n-1,vec_seg[j],i,dp[i][j]);\n\n int t=INT_MAX;;\n for(int i=0;i<n;i++){\n t = t&nums[i];\n if(t==val[0]) dp[i][0] = nums[i];\n if(dp[i][0] != 1e9) update(0,0,n-1,vec_seg[0],i,dp[i][0]);\n }\n\n cout << dp[0][0] << endl;\n // return 0;\n\n for(int i=1;i<n;i++){\n for(int j=1;j<m;j++){\n if(j>i) break;\n int sm1 = 0; int sm2=i;\n for(int p=0;p<31;p++){\n int bit = ((1 << p) & val[j]) >> p;\n if(bit==1){\n sm1 = max(sm1,sm[i][p]);\n }\n else {\n sm2 = min(sm2,sm[i][p]);\n }\n }\n // sm2 = min(sm2,i-1);\n // cout << i << \" \" << j << \" \" << sm1 << \" \" << sm2 << endl;\n if(sm1<=sm2-1){\n // cout << (sm1) << \" \" << (sm2-1) << endl;\n int u = query(0,0,n-1,sm1,sm2-1,vec_seg[j-1]);\n if(u!=1e9){\n dp[i][j] = nums[i] + u ;\n } \n }\n update(0,0,n-1,vec_seg[j],i,dp[i][j]);\n }\n }\n\n if(dp[n-1][m-1]==1e9) return -1; else return dp[n-1][m-1];\n }\n};", "memory": "89262" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
0
{ "code": "class SparseTable {\npublic:\n SparseTable(const vector<int>& arr) {\n build(arr);\n }\n\n // Function to build the Sparse Table\n void build(const vector<int>& arr) {\n int n = arr.size();\n int k = log2(n) + 1; // Maximum power of 2 <= n\n\n table.resize(n, vector<int>(k));\n\n // Initialize the first column of the table\n for (int i = 0; i < n; ++i) {\n table[i][0] = arr[i];\n }\n\n // Fill the table using the dynamic programming approach\n for (int j = 1; (1 << j) <= n; ++j) {\n for (int i = 0; (i + (1 << j) - 1) < n; ++i) {\n table[i][j] = merge(table[i][j-1], table[i + (1 << (j-1))][j-1]);\n }\n }\n }\n // Function to query the maximum value in range [L, R]\n int query(int L, int R) {\n int j = log2(R - L + 1);\n return merge(table[L][j], table[R - (1 << j) + 1][j]);\n }\n\nprivate:\n int merge(int left, int right){\n return left&right;\n }\n vector<vector<int>> table;\n};\n\nclass Node {\npublic:\n// example what is the argument need to solve your problem\n long long val; \n\n Node() : val(1e9) {}\n\n Node(long long val) : val(val) {}\n};\n\nNode merge(Node &left, Node &right) {\n return Node(min(left.val,right.val));\n}\n\nclass SegmentTree {\nprivate:\n int n,m;\n vector<vector<Node>> tree;\n\n\n void update(int idx, int value, int node, int start, int end, int level) {\n if (start == end) {\n tree[level][node] = Node(value);\n } else {\n int mid = (start + end) / 2;\n if (idx <= mid) {\n update(idx, value, 2 * node + 1, start, mid,level);\n } else {\n update(idx, value, 2 * node + 2, mid + 1, end,level);\n }\n tree[level][node] = merge(tree[level][2 * node + 1], tree[level][2 * node + 2]);\n }\n }\n\n Node query_range(int l, int r, int node, int start, int end, int level) {\n if (start > end || start > r || end < l) {\n return Node();\n }\n if (start >= l && end <= r) {\n return tree[level][node];\n }\n int mid = (start + end) / 2;\n Node left_result = query_range(l, r, 2 * node + 1, start, mid,level);\n Node right_result = query_range(l, r, 2 * node + 2, mid + 1, end,level);\n return merge(left_result, right_result);\n }\n\npublic:\n SegmentTree(int n, int m):n(n),m(m) {\n tree.resize(m);\n for(int i=0;i<m;i++){\n tree[i].resize(n*4);\n }\n }\n\n void update(int idx, int value,int level) {\n update(idx, value, 0, 0, n - 1,level);\n }\n\n long long query_range(int l, int r, int level) {\n Node result = query_range(l, r, 0, 0, n - 1, level);\n return result.val;\n }\n};\nclass Solution {\npublic:\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n int n=nums.size(),m=andValues.size();\n SparseTable sparseTable(nums);\n vector<vector<int>> dp(n+1,vector<int>(m+1,1e9));\n SegmentTree seg(n+2,m+2);\n seg.update(0,0,0);\n dp[0][0]=0; // base case\n for(int i=1;i<=n;i++){\n for(int j=1;j<=m;j++){\n // define range x - y such that [i....y] & = andValues[j]\n int l=1,r=i,mid,leftMost=-1,rightMost=-1,cmp;\n while(l<=r){\n mid=(l+r)/2;\n cmp=sparseTable.query(mid-1,i-1);\n if(cmp>andValues[j-1]) r=mid-1;\n else if(cmp<andValues[j-1]) l=mid+1;\n else leftMost=mid,r=mid-1;\n }\n l=1,r=i;\n while(l<=r){\n mid=(l+r)/2;\n cmp=sparseTable.query(mid-1,i-1);\n if(cmp>andValues[j-1]) r=mid-1;\n else if(cmp<andValues[j-1]) l=mid+1;\n else rightMost=mid,l=mid+1;\n }\n if(leftMost==-1) continue;\n // here make query from leftMost - rightMost to find min dp[w][j-1]\n int mn=seg.query_range(leftMost-1,rightMost-1,j-1);\n if(mn==1e9) continue;\n dp[i][j]=nums[i-1]+mn;\n // update dp[i][j] with min + nums[i-1]\n seg.update(i,dp[i][j],j);\n }\n }\n return dp[n][m]==1e9?-1:dp[n][m];\n }\n};", "memory": "95497" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
0
{ "code": "typedef long long ll;\ntypedef vector<int> vi;\ntypedef pair<int, int> pi;\nconst ll INF = LONG_LONG_MAX - 1;\n\nclass Solution {\npublic:\n ll highestPowerof2LessThanOrEq(ll x) {\n // check for the set bits\n x |= x >> 1;\n x |= x >> 2;\n x |= x >> 4;\n x |= x >> 8;\n x |= x >> 16;\n x |= x >> 32;\n return x ^ (x >> 1);\n}\n \n template <class T> class Segtree {\n\nprivate:\n\n const T DEFAULT = (1ll << 60); // Will overflow if T is an int\n\n\n vector<T> segtree;\n\n int len;\n\n\npublic:\n\n Segtree(int len) : len(len), segtree(len * 2, DEFAULT) {}\n\n\n /** Sets the value at ind to val. */\n\n void set(int ind, T val) {\n\n ind += len;\n\n segtree[ind] = val;\n\n for (; ind > 1; ind /= 2) {\n\n segtree[ind / 2] = std::min(segtree[ind], segtree[ind ^ 1]);\n\n }\n\n }\n\n\n /** @return the max element in the range [start, end] */\n\n T rmin(int start, int end) {\n end++;\n T sum = DEFAULT;\n\n for (start += len, end += len; start < end; start /= 2, end /= 2) {\n\n if (start % 2 == 1) { sum = std::min(sum, segtree[start++]); }\n\n if (end % 2 == 1) { sum = std::min(sum, segtree[--end]); }\n\n }\n\n return sum;\n\n }\n\n};\n \n ll dp[(int) 1e5 + 3][15];\n int sp[(int) 3e5][30];\nint query(int l, int r){\n if (r == l)\n return sp[r][0];\n int d = (int) log2(highestPowerof2LessThanOrEq(r-l));\n return sp[l][d] & sp[r-(1<<d)+1][d];\n}\n \n int minimumValueSum(vector<int>& a, vector<int>& b) {\n\n int n = a.size();\n int m = b.size();\n\n for (int i = 0; i < n; ++i)\n sp[i][0] = a[i];\n\n\n int LOGN = ceil(log2(n));\n for (int j = 1; j < LOGN; ++j) {\n for (int i = 0; i < n - (1 << j) + 1; ++i) {\n sp[i][j] = sp[i][j-1] & sp[i+(1<<(j-1))][j-1];\n }\n }\n\n vector<Segtree<ll>> P(m + 2,Segtree<ll>(n+3));\n for (int w = 0; w <= m+2; ++w)\n for (int i = 0; i <= n+2; ++i)\n dp[i][w] = INF/3;\n dp[n][m] = 0;\n\n P[m].set(n,a[n-1]);\n\n\n for (int w = m-1; w >= 0; --w){\n for (int i = n-1; i >= 0; --i){\n int L = n;\n int l = i;\n int r = n-1;\n while ((r-l) > 1){\n int mi = (r+l) >> 1;\n if (query(i,mi) > b[w])\n l = mi+1;\n else\n r = mi;\n }\n if (query(i,l) == b[w])\n L = l;\n else if (query(i,r) == b[w])\n L = r;\n\n int R = i;\n l = i;\n r = n-1;\n while ((r-l) > 1){\n int mi = (r+l) >> 1;\n if (query(i,mi) < b[w])\n r = mi-1;\n else\n l = mi;\n }\n if (query(i,r) == b[w])\n R = r;\n else if (query(i,l) == b[w])\n R = l;\n\n /*for (int j = L; j <= R; ++j){\n dp[i][w] = min(dp[i][w], a[j] + dp[j+1][w+1]);\n }*/\n if (L <= R)\n dp[i][w] = P[w+1].rmin(L+1,R+1);\n if (i > 0)\n P[w].set(i,dp[i][w] + a[i-1]);\n }\n }\n\n return dp[0][0] > INF/10 ? -1 : dp[0][0];\n\n\n }\n};", "memory": "101732" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
0
{ "code": "struct RMQ\n{\n int n;\n vector<int> log2;\n vector<vector<int>> rmq;\n void Build(vector<int> &vi)\n {\n n=vi.size();\n log2.assign(n+1,0);\n for(int i=2;i<=n;++i)\n {\n log2[i]=log2[i/2]+1;\n }\n rmq.assign(log2[n]+1,vector<int>(n));\n rmq[0]=vi;\n for(int p=1;p<=log2[n];++p)\n {\n for(int j=0;j+(1<<p)-1<n;++j)\n {\n rmq[p][j]=min(rmq[p-1][j],rmq[p-1][j+(1<<(p-1))]);\n }\n }\n }\n int Query(int le,int ri)\n {\n int p=log2[ri-le+1];\n return min(rmq[p][le],rmq[p][ri-(1<<p)+1]);\n }\n};\n\nclass Solution {\npublic:\n const int BIT=17;\n const int INF=INT_MAX/2;\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n int n=nums.size();\n vector<vector<int>> mi0(BIT,vector<int>(1,-1));\n for(int i=0;i<n;++i)\n {\n for(int j=0;j<BIT;++j)\n {\n if((nums[i]&(1<<j))==0)\n {\n mi0[j].push_back(i);\n }\n }\n }\n vector<int> mem(n,INF);\n for(int i=0,andval=INF;i<n;++i)\n {\n andval&=nums[i];\n if(andval==andValues[0]) mem[i]=nums[i];\n }\n for(int k=1;k<(int)andValues.size();++k)\n {\n RMQ rmq;\n rmq.Build(mem);\n if(rmq.Query(0,n-1)>=INF) return -1;\n fill(mem.begin(),mem.end(),INF);\n for(int i=k;i<n;++i)\n {\n int le=0,ri=i-1;\n for(int j=0;j<BIT;++j)\n {\n int p=*prev(upper_bound(mi0[j].begin(),mi0[j].end(),i));\n if(andValues[k]&(1<<j)) le=max(le,p);\n else ri=min(ri,p-1);\n }\n if(le>ri || ri<0) continue;\n mem[i]=rmq.Query(le,ri)+nums[i];\n }\n }\n return mem.back()>=INF ? -1 : mem.back();\n }\n};", "memory": "107967" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
0
{ "code": "#include <bits/stdc++.h>\nusing namespace std;\n// using namespace __gnu_pbds;\n #define vb vector<bool>\n #define ff first\n #define ss second\n #define pb push_back\n #define gout(tno) cout << \"Case #\" << tno++ <<\": \"\n #define ld long double\n #define f(i, a, b) for (int(i) = int(a); (i) < int(b); ++(i))\n #define ll long long\nconst ll mod= 998244353\n ,//1e9+7 //998244353\ninf=1e9;\nconst int MAXN=9e5+1,N=5e4;\nclass Solution {\npublic:\n ll shortest(ll mid,ll i,vector<vector<int>> &pf){\n ll ans=0;\n for(ll bt=0;bt<17;bt++){\n ll ad=pf[i][bt];\n // cout<<ad<<endl;\n if(mid-1>=0) ad-=pf[mid-1][bt];\n // cout<<ad<<endl;\n if(ad==i-mid+1) ans+=(1<<bt);\n }\n return ans;\n }\n\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n int n=nums.size();\n int m=andValues.size();\n vector<int> num(n+1);\n f(i,1,n+1) num[i]=nums[i-1];\n vector<vector<pair<int,int>>> v(n+1,vector<pair<int,int>>(m+1));\n vector<vector<int>> a(n+1,vector<int>(17)),pf(n+1,vector<int>(17));\n f(i,1,n+1){\n f(j,0,17){\n if(((num[i]>>j)&1)) a[i][j]=1;\n }\n }\n f(j,0,20) if(((num[1]>>j)&1))pf[1][j]=1;\n f(i,2,n+1){\n f(j,0,17){\n pf[i][j]+=pf[i-1][j]+(a[i][j]);\n }\n }\n\n // cout<<pf[2][0]<<\" \"<<pf[2][1]<<\" \"<<endl;\n // return 0;\n // show(pf[1]);cout<<endl;\n\n\n for(int i=1;i<=n;i++){\n for(int j=1;j<=m;j++){\n ll k=andValues[j-1];\n ll st=i+1,ed=i+1;\n ll l=1,r=i;\n ll mid;\n ll val;\n while(l<=r){\n mid=(l+r)/2;\n //shortest\n val=shortest(mid,i,pf);\n // cout<<mid<<endl;\n // cout<<val<<\" \"<<l<<\" \"<<r<<\" \"<<mid<<endl;\n if(val<=k){//agar value chhota toh array chhota hoga\n if(val==k){\n // cout<<val<<endl;\n ed=mid;\n }\n l=mid+1;\n }\n else r=mid-1;\n }\n l=1,r=i;\n while(l<=r){\n mid=(l+r)/2;\n //longest\n val=shortest(mid,i,pf);\n if(val>=k){// agar value badda toh badda hoga array\n if(val==k)\n st=mid;\n r=mid-1;\n }\n else l=mid+1;\n }\n // cout<<st<<\" \"<<ed<<endl;\n // return 0;\n // if(ed==i+1 ) st=i+1;\n v[i][j]={st,ed};\n }\n }\n // f(i,1,n+1){\n // cout<<i<<\": \"<<endl;\n // for(int j=1;j<v[i].size();j++){\n // cout<<v[i][j].ff<<\" \"<<v[i][j].ss<<endl;\n // }\n // }\n // cout<<v[2][1].ss<<endl;\n // return 0;\n // return 0;\n // ith index tak making jth completely AND cost\n ll dp[n+1][m+1];\n f(i,0,n+1) f(j,0,m+1) dp[i][j]=inf;\n dp[0][0]=0;\n // vector<priority_queue<pair<ll, ll>>> mn(m+1);\n vector<multiset<pair<ll,ll>>> mn(m+1);\n // cout<<v[1][1].ff<<endl;\n for(ll i=1;i<=n;i++){\n for(ll j=m;j>=1;j--){\n if(j==1){\n pair<ll, ll> rng=v[i][j];\n if(v[i][j].ff==1){\n dp[i][j]=num[i];\n }\n mn[j].insert({dp[i][j],i});\n }\n else{\n // dp[i][j]=dp[i-1][j];\n pair<ll, ll> rng=v[i][j];\n // previous AND completed and looking for new\n // continue;\n if(v[i][j].ff!=i+1 && mn[j-1].empty()==false){\n // pair<ll,ll> it1={-1,-1};\n auto it=mn[j-1].begin();\n while(mn[j-1].empty()==false && it!=mn[j-1].end()){\n // auto it=mn[j-1].top();\n // if(i==2 && j==2) cout<<it.ss<<\" \"<<v[i][j].ff-1<<\" \"<<v[i][j].ss<<endl;\n // it++;\n if((*it).ss<(v[i][j].ff-1)){\n // if(it.ff==2) cout<<i<<endl;\n // if(it.ss==v[i][j].ss) it1=it;\n auto it1=it;\n it++;\n mn[j-1].erase(it1);\n }\n else{\n if((*it).ss<v[i][j].ss){\n dp[i][j]=min(dp[i][j],(*it).ff+num[i]);\n break;\n }\n else it++;\n }\n }\n // if(it1.ss!=-1) mn[j-1].push(it1);\n if(dp[i][j]!=inf)\n mn[j].insert({dp[i][j],i});\n\n }\n // extending the current AND if possible\n\n\n }\n }\n }\n //i==5 && j==2\n\n // f(i,1,n+1){\n // f(j,1,m+1){\n // if(dp[i][j]!=inf)\n // cout<<dp[i][j]<<\" \";\n // else cout<<-1<<\" \";\n // }cout<<endl;\n // }\n\n ll ans=dp[n][m];\n if(ans!=inf)\n return ans;\n return -1;\n\n\n\n\n\n\n }\n};\n\n// int main(){\n\n// ll tt=1;\n// cin>>tt;\n// while(tt--){\n// ll n,m;\n// cin>>n>>m;\n// vector<int> a(n),b(m);\n// f(i,0,n) cin>>a[i];\n// f(i,0,m) cin>>b[i];\n// Solution s;\n// ll val=s.minimumValueSum(a,b);cout<<val<<endl;\n// cout<<(383&238&483)<<endl;\n\n \n// }\n\n \n \n\n\n\n \n\n\n// return 0;\n\n\n// }", "memory": "114202" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
0
{ "code": "#define ll long long\n#define av andValues\nclass ST{\n public:\n vector<ll>seg;\n ST(int n)\n {\n seg.resize(4*n);\n }\n void build(int ind,int low,int high)\n {\n if(low==high)\n {\n seg[ind]=INT_MAX;\n return;\n }\n int mid=(low+high)/2;\n build(2*ind+1,low,mid);\n build(2*ind+2,mid+1,high);\n seg[ind]=min(seg[2*ind+1],seg[2*ind+2]);\n }\n void update(int ind,int low,int high,int pos,ll val)\n {\n if(low==high)\n {\n seg[ind]=min(seg[ind],val);\n return;\n }\n int mid=(low+high)/2;\n if(pos<=mid) update(2*ind+1,low,mid,pos,val);\n else update(2*ind+2,mid+1,high,pos,val);\n seg[ind]=min(seg[2*ind+1],seg[2*ind+2]);\n }\n ll query(int ind,int low,int high,int l,int r)\n {\n //no\n if(r<low || high<l) return INT_MAX;\n //comp\n if(low>=l && high<=r) return seg[ind];\n //par\n int mid=(low+high)/2;\n ll left=query(2*ind+1,low,mid,l,r);\n ll right=query(2*ind+2,mid+1,high,l,r);\n return min(left,right);\n }\n};\nclass Solution {\npublic:\n int fun(int start,vector<vector<int>>& prefix,int val)\n {\n int low=start,high=prefix.size()-1;\n int ans=-1;\n while(low<=high)\n {\n int mid=(low+high)/2;\n int x=0;\n for(int i=0;i<32;i++)\n {\n if(start!=0)\n {\n if(prefix[mid][i]-prefix[start-1][i]==0)\n {\n //this bit can be set\n x+=(1<<i);\n }\n }\n else\n {\n if(prefix[mid][i]==0)\n {\n x+=(1<<i);\n }\n }\n }\n if(x>val) \n {\n //mid is on left side of \n low=mid+1;\n }\n else\n {\n //we are on right side\n high=mid-1;\n if(x==val) ans=mid;\n }\n }\n //at the end high will be shifter from correct value to it's -1 index so \n return ans;\n }\n int fun2(int start,vector<vector<int>>& prefix,int val)\n {\n int low=start,high=prefix.size()-1;\n int ans=-1;\n while(low<=high)\n {\n int mid=(low+high)/2;\n int x=0;\n for(int i=0;i<32;i++)\n {\n if(start!=0)\n {\n if(prefix[mid][i]-prefix[start-1][i]==0)\n {\n //this bit can be set\n x+=(1<<i);\n }\n }\n else\n {\n if(prefix[mid][i]==0)\n {\n x+=(1<<i);\n }\n }\n }\n if(x>=val) \n {\n //mid is on left side of \n low=mid+1;\n if(x==val) ans=mid;\n }\n else\n {\n //we are on right side\n high=mid-1;\n }\n }\n //at the end high will be shifter from correct value to it's -1 index so \n return ans;\n }\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n //convert the recursive solution to iterative solution\n\n int n=nums.size();\n int m=av.size();\n //computing prefix array to efficiently find and values\n vector<vector<int>>prefix(n,vector<int>(32,0));\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<32;j++)\n {\n //if jth bit is off then we will do cnt++\n if(!(nums[i]&(1<<j)))\n {\n prefix[i][j]++;\n }\n }\n if(i!=0)\n {\n for(int j=0;j<32;j++)\n {\n prefix[i][j]+=prefix[i-1][j];\n }\n }\n }\n vector<ST>st;\n for(int i=0;i<=m;i++)\n {\n ST temp(n+1);//since we are going till 0 to n\n temp.build(0,0,n);\n st.push_back(temp);\n }\n vector<vector<ll>>dp(n+1,vector<ll>(m+1,INT_MAX));\n for(int i=n;i>=0;i--)\n {\n for(int j=m;j>=0;j--)\n {\n if(i==n && j==m) dp[i][j]=0;\n else if(i==n && j!=m) dp[i][j]=INT_MAX;\n else if(i!=n && j==m) dp[i][j]=INT_MAX;\n else\n {\n //since and value will decrease the value we have to find the range\n //x to y such that x>=i && y>=i ,the and value in this range is equal to av[j]\n //we will use Binary search to find x the first greater equal to i that gives and value equal to av[j]\n //then we will use binary search again to find y the last index greater equal to i that gives and value equal to av[j]\n int x=fun(i,prefix,av[j]);\n int y=fun2(i,prefix,av[j]);\n // if(x!=-1 && y!=-1)\n // {\n // for(int k=x;k<=y;k++)\n // {\n // dp[i][j]=min(dp[i][j],(ll)nums[k]+dp[k+1][j+1]);\n // }\n // } \n // this is now min range query problem we have to use segement tree\n if(x!=-1 && y!=-1) dp[i][j]=st[j+1].query(0,0,n,x+1,y+1);\n }\n //update the value we got in seg tree\n if(i!=0) st[j].update(0,0,n,i,nums[i-1]+dp[i][j]);\n }\n \n }\n if(dp[0][0]==INT_MAX) return -1;\n return dp[0][0];\n }\n};", "memory": "120437" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
0
{ "code": "#define ll long long\n#define av andValues\nclass ST{\n public:\n vector<ll>seg;\n ST(int n)\n {\n seg.resize(4*n);\n }\n void build(int ind,int low,int high)\n {\n if(low==high)\n {\n seg[ind]=INT_MAX;\n return;\n }\n int mid=(low+high)/2;\n build(2*ind+1,low,mid);\n build(2*ind+2,mid+1,high);\n seg[ind]=min(seg[2*ind+1],seg[2*ind+2]);\n }\n void update(int ind,int low,int high,int pos,ll val)\n {\n if(low==high)\n {\n seg[ind]=min(seg[ind],val);\n return;\n }\n int mid=(low+high)/2;\n if(pos<=mid) update(2*ind+1,low,mid,pos,val);\n else update(2*ind+2,mid+1,high,pos,val);\n seg[ind]=min(seg[2*ind+1],seg[2*ind+2]);\n }\n ll query(int ind,int low,int high,int l,int r)\n {\n //no\n if(r<low || high<l) return INT_MAX;\n //comp\n if(low>=l && high<=r) return seg[ind];\n //par\n int mid=(low+high)/2;\n ll left=query(2*ind+1,low,mid,l,r);\n ll right=query(2*ind+2,mid+1,high,l,r);\n return min(left,right);\n }\n};\nclass Solution {\npublic:\n int fun(int start,vector<vector<int>>& prefix,int val)\n {\n int low=start,high=prefix.size()-1;\n int ans=-1;\n while(low<=high)\n {\n int mid=(low+high)/2;\n int x=0;\n for(int i=0;i<32;i++)\n {\n if(start!=0)\n {\n if(prefix[mid][i]-prefix[start-1][i]==0)\n {\n //this bit can be set\n x+=(1<<i);\n }\n }\n else\n {\n if(prefix[mid][i]==0)\n {\n x+=(1<<i);\n }\n }\n }\n if(x>val) \n {\n //mid is on left side of \n low=mid+1;\n }\n else\n {\n //we are on right side\n high=mid-1;\n if(x==val) ans=mid;\n }\n }\n //at the end high will be shifter from correct value to it's -1 index so \n return ans;\n }\n int fun2(int start,vector<vector<int>>& prefix,int val)\n {\n int low=start,high=prefix.size()-1;\n int ans=-1;\n while(low<=high)\n {\n int mid=(low+high)/2;\n int x=0;\n for(int i=0;i<32;i++)\n {\n if(start!=0)\n {\n if(prefix[mid][i]-prefix[start-1][i]==0)\n {\n //this bit can be set\n x+=(1<<i);\n }\n }\n else\n {\n if(prefix[mid][i]==0)\n {\n x+=(1<<i);\n }\n }\n }\n if(x>=val) \n {\n //mid is on left side of \n low=mid+1;\n if(x==val) ans=mid;\n }\n else\n {\n //we are on right side\n high=mid-1;\n }\n }\n //at the end high will be shifter from correct value to it's -1 index so \n return ans;\n }\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n //convert the recursive solution to iterative solution\n\n int n=nums.size();\n int m=av.size();\n //computing prefix array to efficiently find and values\n vector<vector<int>>prefix(n,vector<int>(32,0));\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<32;j++)\n {\n //if jth bit is off then we will do cnt++\n if(!(nums[i]&(1<<j)))\n {\n prefix[i][j]++;\n }\n }\n if(i!=0)\n {\n for(int j=0;j<32;j++)\n {\n prefix[i][j]+=prefix[i-1][j];\n }\n }\n }\n vector<ST>st;\n for(int i=0;i<=m;i++)\n {\n ST temp(n+1);//since we are going till 0 to n\n temp.build(0,0,n);\n st.push_back(temp);\n }\n vector<vector<ll>>dp(n+1,vector<ll>(m+1,INT_MAX));\n for(int i=n;i>=0;i--)\n {\n for(int j=m;j>=0;j--)\n {\n if(i==n && j==m) dp[i][j]=0;\n else if(i==n && j!=m) dp[i][j]=INT_MAX;\n else if(i!=n && j==m) dp[i][j]=INT_MAX;\n else\n {\n //since and value will decrease the value we have to find the range\n //x to y such that x>=i && y>=i ,the and value in this range is equal to av[j]\n //we will use Binary search to find x the first greater equal to i that gives and value equal to av[j]\n //then we will use binary search again to find y the last index greater equal to i that gives and value equal to av[j]\n int x=fun(i,prefix,av[j]);\n int y=fun2(i,prefix,av[j]);\n // if(x!=-1 && y!=-1)\n // {\n // for(int k=x;k<=y;k++)\n // {\n // dp[i][j]=min(dp[i][j],(ll)nums[k]+dp[k+1][j+1]);\n // }\n // } \n // this is now min range query problem we have to use segement tree\n if(x!=-1 && y!=-1) dp[i][j]=st[j+1].query(0,0,n,x+1,y+1);\n }\n //update the value we got in seg tree\n if(i!=0) st[j].update(0,0,n,i,nums[i-1]+dp[i][j]);\n }\n \n }\n if(dp[0][0]==INT_MAX) return -1;\n return dp[0][0];\n }\n};", "memory": "126672" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
0
{ "code": "namespace atcoder {\n\nnamespace internal {\n\n#if __cplusplus >= 202002L\n\nusing std::bit_ceil;\n\n#else\n\n// @return same with std::bit::bit_ceil\nunsigned int bit_ceil(unsigned int n) {\n unsigned int x = 1;\n while (x < (unsigned int)(n)) x *= 2;\n return x;\n}\n\n#endif\n\n// @param n `1 <= n`\n// @return same with std::bit::countr_zero\nint countr_zero(unsigned int n) {\n#ifdef _MSC_VER\n unsigned long index;\n _BitScanForward(&index, n);\n return index;\n#else\n return __builtin_ctz(n);\n#endif\n}\n\n// @param n `1 <= n`\n// @return same with std::bit::countr_zero\nconstexpr int countr_zero_constexpr(unsigned int n) {\n int x = 0;\n while (!(n & (1 << x))) x++;\n return x;\n}\n\n} // namespace internal\n\n\n#if __cplusplus >= 201703L\n\ntemplate <class S, auto op, auto e> struct segtree {\n static_assert(std::is_convertible_v<decltype(op), std::function<S(S, S)>>,\n \"op must work as S(S, S)\");\n static_assert(std::is_convertible_v<decltype(e), std::function<S()>>,\n \"e must work as S()\");\n\n#else\n\ntemplate <class S, S (*op)(S, S), S (*e)()> struct segtree {\n\n#endif\n\n public:\n segtree() : segtree(0) {}\n explicit segtree(int n) : segtree(std::vector<S>(n, e())) {}\n explicit segtree(const std::vector<S>& v) : _n(int(v.size())) {\n size = (int)internal::bit_ceil((unsigned int)(_n));\n log = internal::countr_zero((unsigned int)size);\n d = std::vector<S>(2 * size, e());\n for (int i = 0; i < _n; i++) d[size + i] = v[i];\n for (int i = size - 1; i >= 1; i--) {\n update(i);\n }\n }\n\n void set(int p, S x) {\n assert(0 <= p && p < _n);\n p += size;\n d[p] = x;\n for (int i = 1; i <= log; i++) update(p >> i);\n }\n\n S get(int p) const {\n assert(0 <= p && p < _n);\n return d[p + size];\n }\n\n S prod(int l, int r) const {\n assert(0 <= l && l <= r && r <= _n);\n S sml = e(), smr = e();\n l += size;\n r += size;\n\n while (l < r) {\n if (l & 1) sml = op(sml, d[l++]);\n if (r & 1) smr = op(d[--r], smr);\n l >>= 1;\n r >>= 1;\n }\n return op(sml, smr);\n }\n\n S all_prod() const { return d[1]; }\n\n template <bool (*f)(S)> int max_right(int l) const {\n return max_right(l, [](S x) { return f(x); });\n }\n template <class F> int max_right(int l, F f) const {\n assert(0 <= l && l <= _n);\n assert(f(e()));\n if (l == _n) return _n;\n l += size;\n S sm = e();\n do {\n while (l % 2 == 0) l >>= 1;\n if (!f(op(sm, d[l]))) {\n while (l < size) {\n l = (2 * l);\n if (f(op(sm, d[l]))) {\n sm = op(sm, d[l]);\n l++;\n }\n }\n return l - size;\n }\n sm = op(sm, d[l]);\n l++;\n } while ((l & -l) != l);\n return _n;\n }\n\n template <bool (*f)(S)> int min_left(int r) const {\n return min_left(r, [](S x) { return f(x); });\n }\n template <class F> int min_left(int r, F f) const {\n assert(0 <= r && r <= _n);\n assert(f(e()));\n if (r == 0) return 0;\n r += size;\n S sm = e();\n do {\n r--;\n while (r > 1 && (r % 2)) r >>= 1;\n if (!f(op(d[r], sm))) {\n while (r < size) {\n r = (2 * r + 1);\n if (f(op(d[r], sm))) {\n sm = op(d[r], sm);\n r--;\n }\n }\n return r + 1 - size;\n }\n sm = op(d[r], sm);\n } while ((r & -r) != r);\n return 0;\n }\n\n private:\n int _n, size, log;\n std::vector<S> d;\n\n void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }\n};\n\n} // namespace atcoder\n\nint op(int a, int b) {\n return min(a, b);\n}\n\nint e() {\n return INT_MAX;\n}\n\nclass Solution {\npublic:\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n int n = nums.size(), m = andValues.size();\n const int inf = 1E9 + 10, M = 20;\n vector pre(n + 1, vector<int>(M));\n for (int i = 0; i < n; i++) {\n pre[i + 1] = pre[i];\n for (int j = 0; j < M; j++) {\n pre[i + 1][j] += nums[i] >> j & 1;\n }\n }\n // vector b(n + 1, vector(m, array<int, 2>()));\n unordered_map<int, map<int, array<int, 2>>> b;\n auto get = [&](int l, int r) -> int {\n int v = 0;\n for (int j = 0; j < M; j++) {\n if (pre[r][j] - pre[l - 1][j] == r - l + 1) {\n v |= 1 << j;\n }\n }\n return v;\n };\n // cout << get(1, 2) << \"?\\n\";\n for (int i = 1; i <= n; i++) {\n for (int j = 1; j <= m; j++) {\n int target = andValues[j - 1];\n int lo = 1, hi = i;\n while (lo < hi) {\n int mid = (lo + hi) / 2;\n if (get(mid, i) >= target) {\n hi = mid;\n } else {\n lo = mid + 1;\n }\n }\n if (get(hi, i) != target) {\n continue;\n }\n int l = hi;\n lo = 1, hi = i;\n while (lo < hi) {\n int mid = (1 + lo + hi) / 2;\n if (get(mid, i) <= target) {\n lo = mid;\n } else {\n hi = mid - 1;\n }\n }\n int r = lo;\n b[i][j] = {l, r};\n // cout << i << \" \" << j << \" \" << l << \" \" << r << \"?\\n\";\n }\n }\n vector dp(m + 1, vector<int>(n + 1, inf));\n vector segs(m + 1, atcoder::segtree<int, op, e>(dp[0]));\n dp[0][0] = 0;\n segs[0].set(0, 0);\n for (int i = 1; i <= n; i++) {\n for (int j = 1; j <= min(i, m); j++) {\n if (b.count(i) && b[i].count(j)) {\n auto [l, r] = b[i][j];\n int Mn = segs[j - 1].prod(l - 1, r);\n if (Mn + nums[i - 1] < dp[j][i]) {\n dp[j][i] = min(dp[j][i], Mn + nums[i - 1]);\n segs[j].set(i, dp[j][i]);\n // cout << i << \" \" << j << \" \" << l << \" \" << r << \" \" << Mn << \" \" << dp[j][i] << \"\\n\";\n }\n }\n }\n }\n return dp[m][n] == inf ? -1 : dp[m][n];\n }\n};", "memory": "132907" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Node {\npublic:\n Node(int v, Node* l, Node* r) {\n val = v;\n left = l;\n right = r;\n }\n int val;\n Node* left;\n Node* right;\n};\n\nclass SegmentTree {\npublic:\n SegmentTree(vector<int>& nums) {\n len = nums.size() - 1;\n root = createTree(0, len, nums);\n }\n\n Node* createTree(int l, int r, vector<int>& nums) {\n if (l == r) return new Node(nums[l], nullptr, nullptr);\n int m = (l + r) / 2;\n Node* left = createTree(l, m, nums);\n Node* right = createTree(m+1, r, nums);\n return new Node(min(left->val, right->val), left, right);\n }\n\n int query(int ql, int qr) {\n return queryTree(root, ql, qr, 0, len);\n }\n\n int queryTree(Node* node, int ql, int qr, int l, int r) {\n int m = (l + r) / 2;\n if (ql <= l && qr >= r) return node->val;\n if (ql > r || qr < l) return INT_MAX;\n return min(queryTree(node->left, ql, qr, l, m), queryTree(node->right, ql, qr, m+1, r));\n }\n\n ~SegmentTree() {\n deleteTree(root);\n }\n void deleteTree(Node* node) {\n if (node == nullptr) return;\n deleteTree(node->left);\n deleteTree(node->right);\n delete node;\n }\n\n int len;\n Node* root;\n};\n\nclass Solution {\npublic:\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n if ((nums[0] & andValues[0]) != andValues[0]) return -1;\n\n //(i) -> length of a sequence that stops at this point for previous andValue\n vector<int> dp(nums.size(), INT_MAX);\n int current = nums[0];\n for (int i = 0; i < nums.size(); i++) {\n current = current & nums[i];\n if ((nums[i] & andValues[0]) != andValues[0]) break; \n if (current == andValues[0]) dp[i] = nums[i];\n }\n for (int j = 1; j < andValues.size(); j++) {\n vector<int> new_dp(nums.size(), INT_MAX);\n SegmentTree t(dp);\n for (int i = j; i < nums.size(); i++) {\n vector<int> range = findRange(i, andValues[j], nums);\n if (range[1] < range[0]) continue;\n new_dp[i] = t.query(max(range[0]-1, j-1), max(range[1]-1, j-1));\n if (new_dp[i] != INT_MAX) new_dp[i] += nums[i];\n }\n dp = new_dp; \n }\n if (dp[dp.size()-1] == INT_MAX) return -1;\n return dp[dp.size()-1];\n }\n\n vector<int> findRange2(int index, int andValue, vector<int>& nums) {\n\n //for a given index, if nums[index] & andValue != andValue return {start, stop}\n //if\n //find the first index below (inclusive) that does not have a 0 where \n int current = nums[index];\n int start = INT_MAX;\n int stop = -1;\n for (int i = index; i >= 0; i--) {\n current = current & nums[i];\n if (current == andValue) {\n stop = max(stop, i);\n start = min(start, i);\n }\n }\n return {start, stop};\n }\n\n vector<int> findRange(int index, int andValue, vector<int>& nums) {\n int current = nums[index];\n int start = INT_MAX;\n int stop = -1;\n if (nums[index] & andValue != andValue) return {start, stop};\n bool flag = false;\n for (int i = index; i >= 0; i--) {\n current = current & nums[i];\n if (current == andValue) {\n stop = max(stop, i);\n start = min(start, i);\n flag = true;\n } else {\n if (flag) break;\n }\n }\n return {start, stop};\n }\n\n int findMin(int start, int stop, vector<int>& dp) {\n int ret = INT_MAX;\n for (int i = start; i <= stop; i++) {\n ret = min(ret, dp[i]);\n }\n return ret;\n }\n};", "memory": "139142" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Node {\npublic:\n Node(int v, Node* l, Node* r) {\n val = v;\n left = l;\n right = r;\n }\n int val;\n Node* left;\n Node* right;\n};\n\nclass SegmentTree {\npublic:\n SegmentTree(vector<int>& nums) {\n len = nums.size() - 1;\n root = createTree(0, len, nums);\n }\n\n Node* createTree(int l, int r, vector<int>& nums) {\n if (l == r) return new Node(nums[l], nullptr, nullptr);\n int m = (l + r) / 2;\n Node* left = createTree(l, m, nums);\n Node* right = createTree(m+1, r, nums);\n return new Node(min(left->val, right->val), left, right);\n }\n\n int query(int ql, int qr) {\n return queryTree(root, ql, qr, 0, len);\n }\n\n int queryTree(Node* node, int ql, int qr, int l, int r) {\n int m = (l + r) / 2;\n if (ql <= l && qr >= r) return node->val;\n if (ql > r || qr < l) return INT_MAX;\n return min(queryTree(node->left, ql, qr, l, m), queryTree(node->right, ql, qr, m+1, r));\n }\n\n ~SegmentTree() {\n deleteTree(root);\n }\n void deleteTree(Node* node) {\n if (node == nullptr) return;\n deleteTree(node->left);\n deleteTree(node->right);\n delete node;\n }\n\n int len;\n Node* root;\n};\n\nclass Solution {\npublic:\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n if ((nums[0] & andValues[0]) != andValues[0]) return -1;\n\n //(i) -> length of a sequence that stops at this point for previous andValue\n vector<int> dp(nums.size(), INT_MAX);\n int current = nums[0];\n for (int i = 0; i < nums.size(); i++) {\n current = current & nums[i];\n if ((nums[i] & andValues[0]) != andValues[0]) break; \n if (current == andValues[0]) dp[i] = nums[i];\n }\n for (int j = 1; j < andValues.size(); j++) {\n vector<int> new_dp(nums.size(), INT_MAX);\n SegmentTree t(dp);\n for (int i = j; i < nums.size(); i++) {\n //first valid starting point for sequence that ends at i\n //last valid starting point\n vector<int> range = findRange(i, andValues[j], nums);\n if (range[1] < range[0]) continue;\n //new_dp[i] = findMin(max(range[0]-1, j-1), max(range[1]-1, j-1), dp);\n new_dp[i] = t.query(max(range[0]-1, j-1), max(range[1]-1, j-1));\n\n if (new_dp[i] != INT_MAX) new_dp[i] += nums[i];\n }\n dp = new_dp; \n }\n if (dp[dp.size()-1] == INT_MAX) return -1;\n return dp[dp.size()-1];\n }\n\n vector<int> findRange(int index, int andValue, vector<int>& nums) {\n int current = nums[index];\n int start = INT_MAX;\n int stop = -1;\n for (int i = index; i >= 0; i--) {\n current = current & nums[i];\n if (current == andValue) {\n stop = max(stop, i);\n start = min(start, i);\n }\n }\n return {start, stop};\n }\n\n int findMin(int start, int stop, vector<int>& dp) {\n int ret = INT_MAX;\n for (int i = start; i <= stop; i++) {\n ret = min(ret, dp[i]);\n }\n return ret;\n }\n};", "memory": "145377" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Node {\npublic:\n Node(int v, Node* l, Node* r) {\n val = v;\n left = l;\n right = r;\n }\n int val;\n Node* left;\n Node* right;\n};\n\nclass SegmentTree {\npublic:\n SegmentTree(vector<int>& nums) {\n len = nums.size() - 1;\n root = createTree(0, len, nums);\n }\n\n Node* createTree(int l, int r, vector<int>& nums) {\n if (l == r) return new Node(nums[l], nullptr, nullptr);\n int m = (l + r) / 2;\n Node* left = createTree(l, m, nums);\n Node* right = createTree(m+1, r, nums);\n return new Node(min(left->val, right->val), left, right);\n }\n\n int query(int ql, int qr) {\n return queryTree(root, ql, qr, 0, len);\n }\n\n int queryTree(Node* node, int ql, int qr, int l, int r) {\n int m = (l + r) / 2;\n if (ql <= l && qr >= r) return node->val;\n if (ql > r || qr < l) return INT_MAX;\n return min(queryTree(node->left, ql, qr, l, m), queryTree(node->right, ql, qr, m+1, r));\n }\n\n ~SegmentTree() {\n deleteTree(root);\n }\n void deleteTree(Node* node) {\n if (node == nullptr) return;\n deleteTree(node->left);\n deleteTree(node->right);\n delete node;\n }\n\n int len;\n Node* root;\n};\n\nclass Solution {\npublic:\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n if ((nums[0] & andValues[0]) != andValues[0]) return -1;\n vector<vector<int>> zeros(32, vector<int>(nums.size()));\n for (int i = 0; i < 32; i++) {\n int val = -1;\n for (int j = 0; j < nums.size(); j++) {\n if (((nums[j] >> i) & 1) == 0) val = j;\n zeros[i][j] = val;\n }\n }\n //(i) -> length of a sequence that stops at this point for previous andValue\n vector<int> dp(nums.size(), INT_MAX);\n int current = nums[0];\n for (int i = 0; i < nums.size(); i++) {\n current = current & nums[i];\n if ((nums[i] & andValues[0]) != andValues[0]) break; \n if (current == andValues[0]) dp[i] = nums[i];\n }\n for (int j = 1; j < andValues.size(); j++) {\n vector<int> new_dp(nums.size(), INT_MAX);\n SegmentTree t(dp);\n for (int i = j; i < nums.size(); i++) {\n vector<int> range = findRange(i, andValues[j], nums, zeros);\n if (range[1] < range[0]) continue;\n new_dp[i] = t.query(max(range[0]-1, j-1), max(range[1]-1, j-1));\n if (new_dp[i] != INT_MAX) new_dp[i] += nums[i];\n }\n dp = new_dp; \n }\n if (dp[dp.size()-1] == INT_MAX) return -1;\n return dp[dp.size()-1];\n }\n\n vector<int> findRange(int index, int andValue, vector<int>& nums, vector<vector<int>>& zeros) {\n if ((nums[index] & andValue) != andValue) return {INT_MAX, -1};\n int start = 0;\n int stop = INT_MAX;\n for (int i = 0; i < 32; i++) {\n int target_bit = ((andValue >> i) & 1);\n int index_bit = ((nums[index] >> i) & 1);\n if (target_bit == 1) {\n start = max(start, zeros[i][index]+1);\n start = max(start, zeros[i][index]+1);\n \n } else {\n if (zeros[i][index] == -1) {\n return {INT_MAX, -1};\n } else {\n stop = min(stop, zeros[i][index]);\n }\n }\n }\n return {start, stop};\n }\n\n\n vector<int> findRange2(int index, int andValue, vector<int>& nums) {\n int current = nums[index];\n int start = INT_MAX;\n int stop = -1;\n bool flag = false;\n for (int i = index; i >= 0; i--) {\n current = current & nums[i];\n if (current == andValue) {\n stop = max(stop, i);\n start = min(start, i);\n flag = true;\n } else {\n if (flag) break;\n }\n }\n return {start, stop};\n }\n\n int findMin(int start, int stop, vector<int>& dp) {\n int ret = INT_MAX;\n for (int i = start; i <= stop; i++) {\n ret = min(ret, dp[i]);\n }\n return ret;\n }\n};", "memory": "151612" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n const int INF = 1e9 + 7;\n int n = nums.size(), m = andValues.size();\n std::unordered_map<long long, int> memo;\n // k: AND result\n auto dfs = [&](int i, int j, int k, auto&& dfs) {\n if (i == n && j == m) {\n return 0;\n }\n else if (n - i < m - j || i == n || j == m) {\n return INF;\n }\n k &= nums[i];\n\n long long mask = (long long)i << 42 | (long long)j << 21 | k;\n if (memo.contains(mask)) {\n return memo[mask];\n }\n\n int& res = memo[mask] = dfs(i + 1, j, k, dfs);\n if (k == andValues[j]) {\n res = std::min(res, dfs(i + 1, j + 1, -1, dfs) + nums[i]);\n }\n return res;\n };\n\n int ans = dfs(0, 0, -1, dfs);\n return ans == INF ? -1 : ans;\n }\n};", "memory": "157847" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n int n = nums.size(), m = andValues.size();\n const int mx = INT_MAX / 2;\n unordered_map<long long, int> memo;//14 + 4 + 17\n auto dfs = [&](auto&& dfs, int i, int j, int and_) -> int{\n if(n - i < m - j) return mx;\n if(j == m) return (i == n) ? 0 : mx;\n and_ &= nums[i];\n long long mask = (long long) i << 21 | (long long) j << 17 | and_;\n if(memo.contains(mask)) return memo[mask];\n int res = dfs(dfs, i + 1, j, and_);\n if(and_ == andValues[j]) res = min(res, dfs(dfs, i + 1, j + 1, -1) + nums[i]);\n return memo[mask] = res;\n };\n int ans = dfs(dfs, 0, 0, -1);\n return ans < mx ? ans : -1;\n }\n};", "memory": "164082" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int dfs(vector<int>& nums, vector<int>& andValues, int i, int j, int curr, unordered_map<long long, int>& memo){\n if (nums.size() - i < andValues.size() - j) return INT_MAX;\n if (j == andValues.size()) return i == nums.size()?0:INT_MAX;\n curr = curr & nums[i];\n long long mask = (long long) i << 36 | (long long) j << 32 | curr;\n if (memo.contains(mask)) return memo[mask];\n int mi = dfs(nums, andValues, i + 1, j, curr, memo);\n if (curr == andValues[j]){\n int nx = dfs(nums, andValues, i + 1, j + 1, -1, memo);\n if (nx != INT_MAX) mi = min(mi, nums[i] + nx);\n }\n return memo[mask] = mi;\n }\n\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n int n = nums.size(), m = andValues.size();\n unordered_map<long long, int> memo;\n int res = dfs(nums, andValues, 0, 0, nums[0], memo);\n return res == INT_MAX?-1:res;\n }\n};", "memory": "170317" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n int n = nums.size(), m = andValues.size();\n vector<vector<map<int,int>>> dp;\n dp.resize(n, vector<map<int,int>> (m));\n\n int currentAndValue = -1;\n for (int i = 0; i < n; i++) {\n currentAndValue &= nums[i];\n if ((currentAndValue & andValues[0]) == andValues[0]) {\n dp[i][0][currentAndValue] = nums[i];\n } else {\n break;\n }\n }\n\n\n for (int i = 1; i < n; i++) {\n for (int j = 1; j < m; j++) {\n for (auto it: dp[i-1][j]) {\n // Note: This inner loop will have complexity as O(17), as max 17 different values can be there for\n // `&` of values till i-1 index. \n // Why? for every prefix starting from 0 till i-1, i.e. [0, i-1], [1, i-1], .. [i-1], there can be max 17 values \n // possible. Because at max 17 bits can be changed. \n int tempAndValue = it.first & nums[i];\n if ((tempAndValue & andValues[j]) == andValues[j]) {\n if (dp[i][j][tempAndValue]) {\n dp[i][j][tempAndValue] = min(dp[i][j][tempAndValue], it.second - nums[i-1] + nums[i]);\n } else {\n dp[i][j][tempAndValue] = it.second - nums[i-1] + nums[i];\n }\n }\n }\n if ((nums[i] & andValues[j]) == andValues[j] && (dp[i-1][j-1][andValues[j-1]])) {\n if (dp[i][j][nums[i]]) {\n dp[i][j][nums[i]] = min(dp[i][j][nums[i]], nums[i] + dp[i-1][j-1][andValues[j-1]]);\n } else {\n dp[i][j][nums[i]] = nums[i] + dp[i-1][j-1][andValues[j-1]];\n }\n }\n }\n }\n if ((dp[n-1][m-1][andValues[m-1]]) != 0) {\n return dp[n-1][m-1][andValues[m-1]];\n }\n return -1;\n }\n};", "memory": "176552" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n int n = nums.size(), m = andValues.size();\n unordered_map<long long, long long> memo;//14 + 4 + 17\n auto&& dfs = [&](auto dfs, long long i, long long j, int and_) -> long long{\n if(n - i < m - j) return INT_MAX;\n if(j == m) return (i == n) ? 0 : INT_MAX;\n and_ &= nums[i];\n long long mask = (i << 36) | (j << 32) | and_;\n if(memo.contains(mask)) return memo[mask];\n long long res = dfs(dfs, i + 1, j, and_);\n if(and_ == andValues[j]) res = min(res, dfs(dfs, i + 1, j + 1, -1) + nums[i]);\n return memo[mask] = res;\n };\n int ans = dfs(dfs, 0, 0, -1);\n return ans < INT_MAX ? ans : -1;\n }\n};", "memory": "182787" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n int n = nums.size(), m = andValues.size();\n const int mx = INT_MAX / 2;\n unordered_map<long long, int> memo;//14 + 4 + 17\n auto&& dfs = [&](auto dfs, long long i, long long j, int and_) -> int{\n if(n - i < m - j) return mx;\n if(j == m) return (i == n) ? 0 : mx;\n and_ &= nums[i];\n long long mask = (i << 21) | (j << 17) | and_;\n if(memo.contains(mask)) return memo[mask];\n int res = dfs(dfs, i + 1, j, and_);\n if(and_ == andValues[j]) res = min(res, dfs(dfs, i + 1, j + 1, -1) + nums[i]);\n return memo[mask] = res;\n };\n int ans = dfs(dfs, 0, 0, -1);\n return ans < mx ? ans : -1;\n }\n};", "memory": "182787" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n int n = nums.size(), m = andValues.size();\n unordered_map<long long, long long> memo;//14 + 4 + 17\n auto&& dfs = [&](auto dfs, long long i, long long j, int and_) -> long long{\n if(n - i < m - j) return INT_MAX;\n if(j == m) return (i == n) ? 0 : INT_MAX;\n and_ &= nums[i];\n long long mask = (i << 36) | (j << 32) | and_;\n if(memo.contains(mask)) return memo[mask];\n long long res = dfs(dfs, i + 1, j, and_);\n if(and_ == andValues[j]) res = min(res, dfs(dfs, i + 1, j + 1, -1) + nums[i]);\n return memo[mask] = res;\n };\n int ans = dfs(dfs, 0, 0, -1);\n return ans < INT_MAX ? ans : -1;\n }\n};", "memory": "189022" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n int n = nums.size(), m = andValues.size();\n const int mx = INT_MAX / 2;\n unordered_map<long long, int> memo;//14 + 4 + 17\n auto&& dfs = [&](auto dfs, int i, int j, int and_) -> int{\n if(n - i < m - j) return mx;\n if(j == m) return (i == n) ? 0 : mx;\n and_ &= nums[i];\n long long mask = (long long) i << 21 | (long long) j << 17 | and_;\n if(memo.contains(mask)) return memo[mask];\n int res = dfs(dfs, i + 1, j, and_);\n if(and_ == andValues[j]) res = min(res, dfs(dfs, i + 1, j + 1, -1) + nums[i]);\n return memo[mask] = res;\n };\n int ans = dfs(dfs, 0, 0, -1);\n return ans < mx ? ans : -1;\n }\n};", "memory": "195257" }