id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\nint minDays(vector<int>& bloomday, int m, int k) {\n\tint N = bloomday.size();\n\tvector<int> bloom(N, 0); // continuous bloom flowers\n\tvector<pair<int,int>> vec; // bloomday , idx\n\tfor(int i = 0; i < N; i++) {\n\t\tvec.push_back({bloomday[i], i});\n\t}\n\tsort(vec.begin(), vec.end()); // sort by day\n\tint cnt = 0;\n\tfor(auto p : vec) {\n\t\tint day = p.first;\n\t\tint idx = p.second;\n\t\tint left = (idx-1 >= 0) ? bloom[idx-1] : 0;\n\t\tint right = (idx+1 < N) ? bloom[idx+1] : 0;\n\t\tint totalflower = left + right + 1;\n\t\tbloom[idx - left] = bloom[idx + right] = totalflower;\n// bouquet num = \n\t\tint leftnum = (left - k + 1) > 0 ? (left/k) : 0;\n int rightnum = (right - k + 1) > 0 ? (right/k) : 0;\n int totalnum = (totalflower -k + 1) > 0 ? (totalflower/k) : 0;\n cnt += (totalnum - leftnum - rightnum);\n if(cnt >= m)\n\t return day;\n\t }\n\treturn -1;\n}\n\n};", "memory": "83879" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\nint minDays(vector<int>& bloomday, int m, int k) {\n\tint N = bloomday.size();\n\tvector<int> bloom(N, 0); // continuous bloom flowers\n\tvector<pair<int,int>> vec; // bloomday , idx\n\tfor(int i = 0; i < N; i++) {\n\t\tvec.push_back({bloomday[i], i});\n\t}\n\tsort(vec.begin(), vec.end()); // sort by day\n\tint cnt = 0;\n\tfor(auto p : vec) {\n\t\tint day = p.first;\n\t\tint idx = p.second;\n\t\tint left = (idx-1 >= 0) ? bloom[idx-1] : 0;\n\t\tint right = (idx+1 < N) ? bloom[idx+1] : 0;\n\t\tint totalflower = left + right + 1;\n\t\tbloom[idx - left] = bloom[idx + right] = totalflower;\n // maintain bouquet num\n\t\tint leftnum = left/k;\n int rightnum = right/k;\n int totalnum = totalflower/k;\n cnt += (totalnum - leftnum - rightnum);\n if(cnt >= m)\n\t return day;\n\t }\n\treturn -1;\n}\n};", "memory": "83879" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\tint minDays(vector<int>& bloomday, int numbouq, int adjcnt)\n\t{\n\t\tint L = 0;\n\t\tint R = *max_element(bloomday.begin(), bloomday.end()) + 1;\n\t\twhile (L + 1 < R) {\n\t\t\tint mid = (L + R) / 2;\n\t\t\tint cnt = countbouquets(bloomday, adjcnt, mid);\n\t\t\tif (cnt >= numbouq)\n\t\t\t\tR = mid;\n\t\t\telse\n\t\t\t\tL = mid;\n\t\t}\n\n\t\treturn R == *max_element(bloomday.begin(), bloomday.end()) + 1 ? -1 : R;\n\t}\nprivate:\n\tint countbouquets(vector<int>& bloomday, int adjcnt, int daycnt)\n\t{\n\t\tvector<char>usable(bloomday.size(), false);\n\t\tfor (int i = 0; i < bloomday.size(); i++)\n\t\t\tif (bloomday[i] <= daycnt)\n\t\t\t\tusable[i] = true;\n\n\t\tint count = 0;\n\t\tvector<int>Stack;\n\t\tfor (int i = 0; i < usable.size(); i++) {\n\t\t\tif (!usable[i]) \n\t\t\t\tStack.clear();\n\t\t\telse if (Stack.size() == 0 && adjcnt != 1)\n\t\t\t\tStack.push_back(1);\n\t\t\telse if (Stack.size() == 0 && adjcnt == 1)\n\t\t\t\tcount++;\n\t\t\telse if (Stack.back() + 1 == adjcnt) {\n\t\t\t\tStack.resize(Stack.size() - adjcnt + 1);\n\t\t\t\tcount++;\n\t\t\t}\n\t\t\telse\n\t\t\t\tStack.push_back(Stack.back() + 1);\n\t\t}\n\n\t\treturn count;\n\t}\n}; ", "memory": "85290" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\tint minDays(vector<int>& bloomday, int numbouq, int adjcnt)\n\t{\n\t\tint L = 0;\n\t\tint R = *max_element(bloomday.begin(), bloomday.end()) + 1;\n\t\twhile (L + 1 < R) {\n\t\t\tint mid = (L + R) / 2;\n\t\t\tint cnt = countbouquets(bloomday, adjcnt, mid);\n\t\t\tif (cnt >= numbouq)\n\t\t\t\tR = mid;\n\t\t\telse\n\t\t\t\tL = mid;\n\t\t}\n\n\t\treturn R == *max_element(bloomday.begin(), bloomday.end()) + 1 ? -1 : R;\n\t}\nprivate:\n\tint countbouquets(vector<int>& bloomday, int adjcnt, int daycnt)\n\t{\n\t\tvector<char>usable(bloomday.size(), false);\n\t\tfor (int i = 0; i < bloomday.size(); i++)\n\t\t\tif (bloomday[i] <= daycnt)\n\t\t\t\tusable[i] = true;\n\n\t\tint count = 0;\n\t\tvector<int>Stack;\n\t\tfor (int i = 0; i < usable.size(); i++) {\n\t\t\tif (!usable[i]) {\n\t\t\t\tStack.clear();\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tif (Stack.size() == 0 && adjcnt != 1)\n\t\t\t\tStack.push_back(1);\n\t\t\telse if (Stack.size() == 0 && adjcnt == 1)\n\t\t\t\tcount++;\n\t\t\telse if (Stack.back() + 1 == adjcnt) {\n\t\t\t\tStack.resize(Stack.size() - adjcnt + 1);\n\t\t\t\tcount++;\n\t\t\t}\n\t\t\telse\n\t\t\t\tStack.push_back(Stack.back() + 1);\n\t\t}\n\n\t\treturn count;\n\t}\n}; ", "memory": "86701" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int func(vector<int>&vec, int n, int k){\n stack<int>st;\n int cnt = 0;\n for(int i=0;i<vec.size();i++){\n if(vec[i]<=n){\n st.push(vec[i]);\n }\n if(st.size()==k){\n cnt++;\n while(!st.empty()) st.pop();\n }\n if(vec[i]>n){\n while(!st.empty()) st.pop();\n }\n }\n return cnt;\n }\n int minDays(vector<int>& a, int m, int k) {\n if(a.size()/k<m) return -1;\n int hi = *max_element(a.begin(), a.end());\n int lo=1;\n while(lo<=hi){\n int mid = (hi+lo)/2;\n int bqs = func(a,mid,k);\n if(bqs >= m){\n hi = mid-1;\n }\n else lo=mid+1;\n }\n return lo;\n }\n};", "memory": "88113" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minDays(vector<int>& bloomDay, int m, int k) {\n int l=bloomDay.size();\n int low=1e9;\n int high=0;\n for(int i=0;i<l;i++)\n {\n high=max(high,bloomDay[i]);\n low=min(low,bloomDay[i]);\n }\n if(low==1e9 && high==1e9)\n {\n return 1e9;\n }\n int mini=1e9;\n while(low<=high)\n {\n int mid=(low+high)/2;\n int sum=0;\n stack<int>st;\n for(int i=0;i<l;i++)\n {\n if(st.empty())\n {\n if(bloomDay[i]<=mid)\n {\n st.push(i);\n }\n }\n else\n {\n if(bloomDay[i]<=mid)\n {\n if(i==st.top()+1)\n {\n st.push(i);\n }\n else\n {\n while(!st.empty())\n {\n st.pop();\n }\n st.push(i);\n }\n }\n }\n if(st.size()==k)\n {\n sum++;\n while(!st.empty())\n {\n st.pop();\n }\n }\n }\n if(sum<m)\n {\n low=mid+1;\n }\n else\n {\n mini=min(mini,mid);\n high=mid-1;\n }\n }\n int mi=1e9;\n for(int i=0;i<l;i++)\n {\n if(bloomDay[i]>=mini)\n {\n mi=min(mi,bloomDay[i]);\n }\n }\n if(mini==1e9)\n {\n return -1;\n }\n return mini;\n }\n};", "memory": "89524" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "#define ll long long\nclass Solution {\npublic:\n int minDays(vector<int>& bloomDay, int m, int k) {\n ll n = bloomDay.size();\n ll w = (ll)m * (ll)k;\n if (w > n) return -1;\n ll low = 0, high = 1e9, ans = 1e9;\n while (low <= high) {\n ll mid = (low + high) / 2;\n vector<ll> v;\n v.push_back(-1);\n for (ll i = 0; i < n; ++i) {\n if (bloomDay[i] > mid) v.push_back(i);\n }\n v.push_back(n);\n ll cnt = 0;\n for (ll i = 0; i < (ll)v.size() - 1; ++i) {\n cnt += (v[i + 1] - v[i] - 1) / k;\n }\n if (cnt >= m) {\n ans = min(ans, mid);\n high = mid - 1;\n }\n else {\n low = mid + 1;\n }\n }\n return ans;\n }\n};", "memory": "90935" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "#define ll long long int\nclass Solution {\n bool isPossible(vector<int> &nums, ll &m, ll &k, ll day){\n\n vector<ll> temp;\n temp.push_back(-1);\n ll n = nums.size();\n for(ll i=0;i<n;i++){\n if(nums[i]>day) temp.push_back(i);\n }\n\n ll res = 0;\n for(ll i=1;i<temp.size();i++){\n res+=((temp[i]-temp[i-1]-1)/k);\n }\n\n ll t = temp.size();\n res+=((n-temp[t-1]-1)/k);\n\n return (res>=m);\n }\npublic:\n int minDays(vector<int>& bloomDay, int m, int k) {\n ll n = bloomDay.size();\n ll m1 = m;\n ll k1 = k;\n if(n<(m1*k1)) return -1;\n ll lo = 0, hi = 0;\n for(ll i=0;i<n;i++){\n ll t = bloomDay[i];\n hi = max(hi, t);\n }\n \n while(hi-lo>1){\n ll mid = lo + (hi-lo)/2;\n\n if(isPossible(bloomDay,m1,k1,mid)) hi = mid;\n else lo = mid;\n }\n\n return hi;\n }\n};", "memory": "92346" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int func(vector<int>& arr, int m, int k, int mid){\n int l=0, r=0, n=arr.size();\n deque<int> q;\n //int maxi=-1;\n int count=0;\n // sliding window maximum implementation\n while(r<n){\n if(r-l+1<k){\n // maxi=max(maxi, arr[r]);\n while(!q.empty() && q.back()<arr[r]){\n q.pop_back();\n }\n q.push_back(arr[r]);\n r++;\n }\n else if(r-l+1==k){\n // maxi=max(maxi, arr[r]);\n while(!q.empty() && q.back()<arr[r]){\n q.pop_back();\n }\n q.push_back(arr[r]);\n if(q.front()<=mid){\n l=r+1;\n r=r+1;\n count++;\n q.clear();\n }else{\n if(q.front()==arr[l]){\n q.pop_front();\n }\n l++;\n r++;\n }\n }\n }\n return count;\n }\n\n\n int minDays(vector<int>& bloomDay, int m, int k) {\n \n int l=*min_element(bloomDay.begin(), bloomDay.end());\n int r=*max_element(bloomDay.begin(), bloomDay.end());\n bool found=false;\n while(l<=r){\n int mid = l + (r-l)/2;\n int ans = func(bloomDay, m, k, mid);\n if(ans>=m){\n found=true;\n r=mid-1;\n }else{\n l=mid+1;\n }\n }\n if(!found) return -1;\n\n return l; // dont know why l\n }\n};", "memory": "93758" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\nprivate:\n bool possible(unordered_map<int,int> &hash,int day,int m,int k,int n)\n {\n int i=k-1;\n while(i<n)\n {\n if(hash[i]<=day)\n {\n m--;\n if(i+k<n) i+=k;\n else break; \n }\n else i++;\n \n }\n \n return m<=0;\n }\npublic:\n int minDays(vector<int>& arr, int m, int k) {\n if((long)m*k>(long)arr.size()) return -1;\n int n=arr.size();\n int maxi=-1e9,mini=1e9;\n for(int i:arr)\n {\n mini=min(mini,i);\n maxi=max(maxi,i);\n } \n int left=mini,right=maxi;\n unordered_map<int,int> hash;\n\n //sliding window maximum to store maximum of all windows\n deque<int> dq;\n for(int i=0;i<n;i++)\n {\n if(!dq.empty()&&dq.front()==i-k) dq.pop_front();\n while(!dq.empty()&&arr[dq.back()]<arr[i]) dq.pop_back();\n dq.push_back(i);\n if(i>=k-1) hash[i]=arr[dq.front()]; \n }\n \n while(left<=right)\n {\n int mid=left+(right-left)/2;\n if(possible(hash,mid,m,k,n)) right=mid-1;\n else left=mid+1;\n cout<<left<<\" \"<<right<<endl;\n }\n return left;\n }\n};", "memory": "95169" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\nprivate:\n bool possible(map<int,int> &hash,int day,int m,int k)\n {\n int i=0;\n auto it=hash.begin();\n while(it!=hash.end())\n {\n if(it->second<=day)\n {\n m--;\n int num=k;\n while(it!=hash.end()&&num>0)\n {\n num--;\n it++;\n } \n }\n else it++;\n if(m<=0) return true;\n }\n \n return false;\n }\npublic:\n int minDays(vector<int>& arr, int m, int k) {\n if((long)m*k>(long)arr.size()) return -1;\n int n=arr.size();\n int maxi=-1e9;\n for(int i:arr) maxi=max(maxi,i);\n int left=1,right=maxi;\n map<int,int> hash;\n\n //sliding window maximum to store maximum of all windows\n deque<int> dq;\n for(int i=0;i<n;i++)\n {\n if(!dq.empty()&&dq.front()==i-k) dq.pop_front();\n while(!dq.empty()&&arr[dq.back()]<arr[i]) dq.pop_back();\n dq.push_back(i);\n if(i>=k-1) hash[i]=arr[dq.front()]; \n }\n \n while(left<=right)\n {\n int mid=left+(right-left)/2;\n if(possible(hash,mid,m,k)) right=mid-1;\n else left=mid+1;\n cout<<left<<\" \"<<right<<endl;\n }\n return left;\n }\n};", "memory": "96580" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\nprivate:\n bool possible(map<int,int> &hash,int day,int m,int k)\n {\n int i=0;\n auto it=hash.begin();\n while(it!=hash.end())\n {\n if(it->second<=day)\n {\n m--;\n int num=k;\n while(it!=hash.end()&&num>0)\n {\n num--;\n it++;\n } \n }\n else it++;\n }\n if(m<=0) return true;\n return false;\n }\npublic:\n int minDays(vector<int>& arr, int m, int k) {\n if((long)m*k>(long)arr.size()) return -1;\n int n=arr.size();\n int maxi=-1e9;\n for(int i:arr) maxi=max(maxi,i);\n int left=1,right=maxi;\n map<int,int> hash;\n\n //sliding window maximum to store maximum of all windows\n deque<int> dq;\n for(int i=0;i<n;i++)\n {\n if(!dq.empty()&&dq.front()==i-k) dq.pop_front();\n while(!dq.empty()&&arr[dq.back()]<arr[i]) dq.pop_back();\n dq.push_back(i);\n if(i>=k-1) hash[i]=arr[dq.front()]; \n }\n \n while(left<=right)\n {\n int mid=left+(right-left)/2;\n if(possible(hash,mid,m,k)) right=mid-1;\n else left=mid+1;\n cout<<left<<\" \"<<right<<endl;\n }\n return left;\n }\n};", "memory": "97991" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool checkOk(int val, vector<int>& bloomDay, int m, int k) {\n int countAj = 0;\n int count = 0;\n for (int j = 0; j < bloomDay.size(); ++j) {\n if (bloomDay[j] <= val) {\n countAj++;\n }\n else {\n countAj = 0;\n }\n\n if (countAj == k) {\n count++;\n countAj = 0;\n }\n\n if (count == m) return true;\n }\n return false;\n }\n\n int minDays(vector<int>& bloomDay, int m, int k) {\n int n = bloomDay.size();\n if (m > (n / k)) return -1;\n \n set<int> setDay(bloomDay.begin(), bloomDay.end());\n\n int l = 0;\n int r = setDay.size() - 1;\n int ans = -1;\n\n while (l <= r) {\n int mid = (l + r) / 2;\n auto it = setDay.begin();\n advance(it, mid);\n // cout << mid << \" \" << *it << endl;\n if (checkOk(*it, bloomDay, m, k)) {\n ans = *it;\n r = mid - 1;\n }\n else {\n l = mid + 1;\n }\n }\n\n return ans;\n }\n};", "memory": "99403" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minDays(vector<int>& bloomDay, int m, int k) {\n if ((long)m*k>bloomDay.size()) return -1;\n if ((long)m*k==bloomDay.size()) return *max_element(bloomDay.begin(),bloomDay.end());;\n set <int> s1(bloomDay.begin(),bloomDay.end());\n vector <int> v1(s1.begin(),s1.end());\n int low=0,high=v1.size()-1;\n while(low<=high){\n int mid=(low+high)/2,m1=0,c=0;\n for (int i=0;i<bloomDay.size();i++){\n if(bloomDay[i]<=v1[mid]) {c++;\n if(c==k) {m1++; c=0;}\n if (m1==m) {high=mid-1; break;}}\n else c=0;\n }\n if (m1!=m) low=mid+1;\n }\n return v1[low];\n }\n};", "memory": "103636" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minDays(vector<int>& bloomDay, int m, int k) {\n int n = bloomDay.size();\n\n if (n < (long long)m * k) {\n return -1;\n }\n\n unordered_map<int, int> mp;\n\n for (const auto& d : bloomDay) {\n mp[d]++;\n }\n\n vector<pair<int, int>> v(mp.begin(), mp.end());\n\n sort(v.begin(), v.end(),\n [](const auto& a, const auto& b) { return a.first < b.first; });\n\n int accum = 0;\n for (auto& p : v) {\n accum += p.second;\n p.second = accum;\n }\n\n int low = 0, high = v.size(), day = -1;\n\n while (low <= high) {\n int mid = (low + high) / 2;\n\n if (v[mid].second >= m * k &&\n isValid(bloomDay, m, k, v[mid].first)) {\n day = v[mid].first;\n high = mid - 1;\n } else {\n low = mid + 1;\n }\n }\n\n return day;\n }\n\n bool isValid(vector<int>& bd, int m, int k, int day) {\n int cnt = 0;\n\n cout << day << endl;\n\n for (const auto& d : bd) {\n if (d > day) {\n cnt = 0;\n } else {\n cnt++;\n }\n\n if (cnt == k) {\n m--;\n cnt = 0;\n }\n\n if (m == 0) {\n return true;\n }\n }\n\n return false;\n }\n};", "memory": "103636" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool canMakeBouquets(const std::vector<int>& bloomDay, int m, int k, int days) {\n int adj = 0, bouquet = 0;\n for (int bloom : bloomDay) {\n if (bloom <= days) {\n adj++;\n if (adj == k) {\n bouquet++;\n adj = 0;\n }\n } else {\n adj = 0;\n }\n if (bouquet >= m) {\n return true;\n }\n }\n return false;\n }\n\n int minDays(std::vector<int>& bloomDay, int m, int k) {\n long long totalFlowers = static_cast<long long>(m) * k;\n if (bloomDay.size() < totalFlowers) {\n return -1;\n }\n\n std::set<int> uniqueDays(bloomDay.begin(), bloomDay.end());\n std::vector<int> sortedDays(uniqueDays.begin(), uniqueDays.end());\n\n int left = 0;\n int right = sortedDays.size() - 1;\n\n while (left < right) {\n int mid = left + (right - left) / 2;\n if (canMakeBouquets(bloomDay, m, k, sortedDays[mid])) {\n right = mid;\n } else {\n left = mid + 1;\n }\n }\n\n return sortedDays[left];\n }\n};\n", "memory": "105048" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n\n int f(vector<int>& bloomDay,int k,int dayvalue){ int bouquets=0;\n\n int count=0;\n\n\n for(int i=0;i<bloomDay.size();i++){ \n\n if(bloomDay[i]<=dayvalue){ \n \n count++;\n\n\n }\n\n else {\n\n bouquets+=count/k;\n count=0;\n\n\n }\n\n\n\n } bouquets+=count/k;\n \n\n\n return bouquets;\n\n\n\n }\n\n\n int minDays(vector<int>& bloomDay, int m, int k) {\n\n /*int n=bloomDay.size();\n\n set<int> st;\n\n for(int i=0;i>n;i++){\n\n st.insert(bloomDay[i]);\n\n\n }\n\n\n if(m*k>n)return -1;\n\n\n int ans;\n\n auto t=st.begin();\n\n while(m>0){ int count=0; \n\n for(int i=0;i<n;i++){\n\n if(bloomDay[i]<=(*t)&&bloomDay[i]>=0){\n count++;\n\n if(count>=k){\n\n m--;\n \n for(int j=i;j>i-k;j--){\n\n bloomDay[j]=-1;\n\n }\n \n count=0; \n\n }\n\n }\n\n else count=0;\n\n\n }\n \n \n t++;\n \n\n }\n\n return (*t);*/\n\n\n //we will see why the above bute force doesnt work but we know here we ahve to apply bs as range of ans --->>> minimum of arr to maximum of array as on last day all will blooom and we can easily make.\n\n //and we have to find minimum from that range.\n\n //it is a very easy question now.\n\n //first strore all values in a set.\n\n int n=bloomDay.size();\n\n\n if((long long)m*(long long)k>n)return -1;\n\n set<int> st;\n\n for(int i=0;i<n;i++){\n\n st.insert(bloomDay[i]);\n\n }\n\n //store these set elemst in vector to allow indexing.\n\n vector<int> v(st.begin(),st.end());\n\n\n //now we will aplly bs on indices as values are unique ans sorted.\n\n int low=0;\n int high=v.size()-1;\n\n\n\n while(low<=high){\n\n int mid=low+((high-low)/2);\n\n int bouquets=f(bloomDay,k,v[mid]);\n\n //this function will return us how many bouquets can be made for that bloomday value in set it.\n\n if(bouquets<m)low=mid+1;\n\n else{ \n\n high=mid-1;\n\n\n }\n\n\n\n }\n\n\n\n return v[low];\n\n \n\n \n }\n};", "memory": "106459" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n\n int f(vector<int>& bloomDay,int k,int dayvalue){ int bouquets=0;\n\n int count=0;\n\n\n for(int i=0;i<bloomDay.size();i++){ \n\n if(bloomDay[i]<=dayvalue){ \n \n count++;\n\n\n }\n\n else {\n\n bouquets+=count/k;\n count=0;\n\n\n }\n\n\n\n } bouquets+=count/k;\n \n\n\n return bouquets;\n\n\n\n }\n\n\n int minDays(vector<int>& bloomDay, int m, int k) {\n\n /*int n=bloomDay.size();\n\n set<int> st;\n\n for(int i=0;i>n;i++){\n\n st.insert(bloomDay[i]);\n\n\n }\n\n\n if(m*k>n)return -1;\n\n\n int ans;\n\n auto t=st.begin();\n\n while(m>0){ int count=0; \n\n for(int i=0;i<n;i++){\n\n if(bloomDay[i]<=(*t)&&bloomDay[i]>=0){\n count++;\n\n if(count>=k){\n\n m--;\n \n for(int j=i;j>i-k;j--){\n\n bloomDay[j]=-1;\n\n }\n \n count=0; \n\n }\n\n }\n\n else count=0;\n\n\n }\n \n \n t++;\n \n\n }\n\n return (*t);*/\n\n\n //we will see why the above bute force doesnt work but we know here we ahve to apply bs as range of ans --->>> minimum of arr to maximum of array as on last day all will blooom and we can easily make.\n\n //and we have to find minimum from that range.\n\n //it is a very easy question now.\n\n //first strore all values in a set.\n\n int n=bloomDay.size();\n\n\n if((long long)m*(long long)k>n)return -1;\n\n set<int> st;\n\n for(int i=0;i<n;i++){\n\n st.insert(bloomDay[i]);\n\n }\n\n //store these set elemst in vector to allow indexing.\n\n vector<int> v(st.begin(),st.end());\n\n\n //now we will aplly bs on indices as values are unique ans sorted.\n\n int low=0;\n int high=v.size()-1;\n\n\n\n while(low<=high){\n\n int mid=low+((high-low)/2);\n\n int bouquets=f(bloomDay,k,v[mid]);\n\n //this function will return us how many bouquets can be made for that bloomday value in set it.\n\n if(bouquets<m)low=mid+1;\n\n else{ \n\n high=mid-1;\n\n\n }\n\n\n\n }\n\n\n\n return v[low];\n\n \n\n \n }\n};", "memory": "107870" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n\n int f(vector<int>& bloomDay,int k,int dayvalue){ int bouquets=0;\n\n int count=0;\n\n\n for(int i=0;i<bloomDay.size();i++){ \n\n if(bloomDay[i]<=dayvalue){ \n \n count++;\n\n\n }\n\n else {\n\n bouquets+=count/k;\n count=0;\n\n\n }\n\n\n\n } bouquets+=count/k;\n \n\n\n return bouquets;\n\n\n\n }\n\n\n int minDays(vector<int>& bloomDay, int m, int k) {\n\n /*int n=bloomDay.size();\n\n set<int> st;\n\n for(int i=0;i>n;i++){\n\n st.insert(bloomDay[i]);\n\n\n }\n\n\n if(m*k>n)return -1;\n\n\n int ans;\n\n auto t=st.begin();\n\n while(m>0){ int count=0; \n\n for(int i=0;i<n;i++){\n\n if(bloomDay[i]<=(*t)&&bloomDay[i]>=0){\n count++;\n\n if(count>=k){\n\n m--;\n \n for(int j=i;j>i-k;j--){\n\n bloomDay[j]=-1;\n\n }\n \n count=0; \n\n }\n\n }\n\n else count=0;\n\n\n }\n \n \n t++;\n \n\n }\n\n return (*t);*/\n\n\n //we will see why the above bute force doesnt work but we know here we ahve to apply bs as range of ans --->>> minimum of arr to maximum of array as on last day all will blooom and we can easily make.\n\n //and we have to find minimum from that range.\n\n //it is a very easy question now.\n\n //first strore all values in a set.\n\n int n=bloomDay.size();\n\n\n if((long long)m*(long long)k>n)return -1;\n\n set<int> st;\n\n for(int i=0;i<n;i++){\n\n st.insert(bloomDay[i]);\n\n }\n\n //store these set elemst in vector to allow indexing.\n\n vector<int> v(st.begin(),st.end());\n\n\n //now we will aplly bs on indices as values are unique ans sorted.\n\n int low=0;\n int high=v.size()-1;\n\n\n\n while(low<=high){\n\n int mid=low+((high-low)/2);\n\n int bouquets=f(bloomDay,k,v[mid]);\n\n //this function will return us how many bouquets can be made for that bloomday value in set it.\n\n if(bouquets<m)low=mid+1;\n\n else{ \n\n high=mid-1;\n\n\n }\n\n\n\n }\n\n\n\n return v[low];\n\n \n\n \n }\n};", "memory": "107870" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int bouqets(vector<int> bloomDay, int m, int k, int day){\n int ans = 0;\n int adjacentFlowers = k;\n for(int i=0;i<bloomDay.size();i++){\n bloomDay[i] -= day;\n if(bloomDay[i]<=0) adjacentFlowers--;\n else adjacentFlowers = k;\n\n if(adjacentFlowers == 0){\n ans++;\n adjacentFlowers = k;\n }\n if(ans == m) return ans;\n }\n return ans;\n\n }\n\n vector<int> maxi(vector<int>& bloomDay){\n int minElement = bloomDay[0], maxElement = bloomDay[0];\n for(int i=1;i<bloomDay.size();i++){\n minElement = min(minElement, bloomDay[i]);\n maxElement = max(maxElement, bloomDay[i]);\n }\n return {minElement, maxElement};\n }\n\n int minDays(vector<int>& bloomDay, int m, int k) {\n int n = bloomDay.size();\n long long int check = (long long int) m*k;\n if(check > n) return -1;\n vector<int>minMax = maxi(bloomDay);\n int minElement = minMax[0], maxElement = minMax[1];\n if(k==1 & m==1) return minElement;\n if(m*k == n) return maxElement;\n int ans = -1;\n int start = 1, end = maxElement;\n while(start<=end){\n int mid = start + (end-start)/2;\n int bouqetsMade = bouqets(bloomDay, m, k, mid);\n cout<<bouqetsMade<<\" ds \"<<mid<<endl;\n if(bouqetsMade >= m){\n ans = mid;\n end = mid-1;\n }\n // else if (bouqetsMade >m) end = mid-1;\n else start = mid+1;\n }\n return ans;\n }\n};", "memory": "109281" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int countThem(vector<int> arr,int mid , int k)\n {\n int i=0,j=0,count=0;\n while(j<arr.size())\n {\n if(arr[j]>mid)\n {\n j++;\n i=j;\n continue;\n }\n if(j-i+1==k)\n {\n count++;\n j++;\n i=j;\n continue;\n }\n j++;\n }\n return count;\n }\n int minDays(vector<int>& bloomDay, int m, int k) {\n vector<int> days=bloomDay;\n sort(days.begin(),days.end());\n int i=0,j=days.size()-1;\n while(i<=j)\n {\n int mid=(i+j)/2;\n int ans=countThem(bloomDay,days[mid],k);\n if(ans>=m)\n {\n j=mid-1;\n }\n else\n {\n i=mid+1;\n }\n }\n if(i>=days.size())\n {\n return -1;\n }\n return days[i];\n\n \n }\n};", "memory": "110693" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int bouqets(vector<int> bloomDay, int m, int k, int day){\n int ans = 0;\n int adjacentFlowers = k;\n for(int i=0;i<bloomDay.size();i++){\n bloomDay[i] -= day;\n if(bloomDay[i]<=0) adjacentFlowers--;\n else adjacentFlowers = k;\n\n if(adjacentFlowers == 0){\n ans++;\n adjacentFlowers = k;\n }\n if(ans == m) return ans;\n }\n return ans;\n\n }\n\n vector<int> maxi(vector<int>& bloomDay){\n int minElement = bloomDay[0], maxElement = bloomDay[0];\n for(int i=1;i<bloomDay.size();i++){\n minElement = min(minElement, bloomDay[i]);\n maxElement = max(maxElement, bloomDay[i]);\n }\n return {minElement, maxElement};\n }\n\n int minDays(vector<int>& bloomDay, int m, int k) {\n //T.C. -> O(N x Log2 (maxElement - minElement +1))\n //S.C. -> O(1)\n int n = bloomDay.size();\n long long int check = (long long int) m*k;\n if(check > n) return -1;\n\n vector<int>minMax = maxi(bloomDay);\n int minElement = minMax[0], maxElement = minMax[1];\n\n if(k==1 & m==1) return minElement;\n if(m*k == n) return maxElement;\n\n // int ans = -1;\n int start = minElement, end = maxElement;\n while(start<=end){\n int mid = start + (end-start)/2;\n int bouqetsMade = bouqets(bloomDay, m, k, mid);\n cout<<bouqetsMade<<\" ds \"<<mid<<endl;\n if(bouqetsMade >= m){\n // ans = mid;\n end = mid-1;\n }\n // else if (bouqetsMade >m) end = mid-1;\n else start = mid+1;\n }\n //return ans;\n return start;\n }\n};", "memory": "112104" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int bouqets(vector<int> bloomDay, int m, int k, int day){\n int ans = 0;\n int adjacentFlowers = k;\n for(int i=0;i<bloomDay.size();i++){\n bloomDay[i] -= day;\n if(bloomDay[i]<=0) adjacentFlowers--;\n else adjacentFlowers = k;\n\n if(adjacentFlowers == 0){\n ans++;\n adjacentFlowers = k;\n }\n if(ans == m) return ans;\n }\n return ans;\n\n }\n\n vector<int> maxi(vector<int>& bloomDay){\n int minElement = bloomDay[0], maxElement = bloomDay[0];\n for(int i=1;i<bloomDay.size();i++){\n minElement = min(minElement, bloomDay[i]);\n maxElement = max(maxElement, bloomDay[i]);\n }\n return {minElement, maxElement};\n }\n\n int minDays(vector<int>& bloomDay, int m, int k) {\n int n = bloomDay.size();\n long long int check = (long long int) m*k;\n if(check > n) return -1;\n vector<int>minMax = maxi(bloomDay);\n int minElement = minMax[0], maxElement = minMax[1];\n if(k==1 & m==1) return minElement;\n if(m*k == n) return maxElement;\n int ans = -1;\n int start = minElement, end = maxElement;\n while(start<=end){\n int mid = start + (end-start)/2;\n int bouqetsMade = bouqets(bloomDay, m, k, mid);\n cout<<bouqetsMade<<\" ds \"<<mid<<endl;\n if(bouqetsMade >= m){\n ans = mid;\n end = mid-1;\n }\n // else if (bouqetsMade >m) end = mid-1;\n else start = mid+1;\n }\n return ans;\n }\n};", "memory": "113515" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int bouqets(vector<int> bloomDay, int m, int k, int day){\n int ans = 0;\n int adjacentFlowers = k;\n for(int i=0;i<bloomDay.size();i++){\n bloomDay[i] -= day;\n if(bloomDay[i]<=0) adjacentFlowers--;\n else adjacentFlowers = k;\n\n if(adjacentFlowers == 0){\n ans++;\n adjacentFlowers = k;\n }\n if(ans == m) return ans;\n }\n return ans;\n\n }\n\n vector<int> maxi(vector<int>& bloomDay){\n int minElement = bloomDay[0], maxElement = bloomDay[0];\n for(int i=1;i<bloomDay.size();i++){\n minElement = min(minElement, bloomDay[i]);\n maxElement = max(maxElement, bloomDay[i]);\n }\n return {minElement, maxElement};\n }\n\n int minDays(vector<int>& bloomDay, int m, int k) {\n int n = bloomDay.size();\n long long int check = (long long int) m*k;\n if(check > n) return -1;\n\n vector<int>minMax = maxi(bloomDay);\n int minElement = minMax[0], maxElement = minMax[1];\n\n if(k==1 & m==1) return minElement;\n if(m*k == n) return maxElement;\n \n int ans = -1;\n int start = minElement, end = maxElement;\n while(start<=end){\n int mid = start + (end-start)/2;\n int bouqetsMade = bouqets(bloomDay, m, k, mid);\n cout<<bouqetsMade<<\" ds \"<<mid<<endl;\n if(bouqetsMade >= m){\n ans = mid;\n end = mid-1;\n }\n // else if (bouqetsMade >m) end = mid-1;\n else start = mid+1;\n }\n return ans;\n }\n};", "memory": "113515" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minDays(vector<int>& bloomDay, int m, int k) {\n int n=bloomDay.size();\n if((double)m*k>n)return -1;\n unordered_set<int>unique_elements(bloomDay.begin(),bloomDay.end());\n vector<int>searchSpace;\n for(auto element:unique_elements){\n searchSpace.push_back(element);\n }\n sort(searchSpace.begin(),searchSpace.end());\n int low=0,high=searchSpace.size()-1;\n while(low<=high){\n int mid=low+(high-low)/2;\n int bouquetsDone=0,tempk=k;\n for(int i=0;i<n;i++){\n if(bloomDay[i]<=searchSpace[mid]){\n tempk--;\n if(tempk==0){\n tempk=k;\n ++bouquetsDone;\n }\n }\n else tempk=k;\n }\n if(bouquetsDone>=m) high=mid-1;\n else low=mid+1;\n }\n return searchSpace[low];\n }\n};", "memory": "114926" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\nprivate:\n int count(vector<int>& bloomDay, int k, int day) {\n int res = 0;\n int l = bloomDay.size();\n\n int cnt = 0;\n for (int i = 0; i < l; i++) {\n if (bloomDay[i] <= day) cnt++;\n else cnt = 0;\n\n if (cnt >= k) {\n res++;\n cnt = 0;\n }\n }\n\n return res;\n }\npublic:\n int minDays(vector<int>& bloomDay, int m, int k) {\n int l = bloomDay.size();\n if (l < (long long)m * k) return -1;\n\n unordered_set<int> dayset;\n for (int i = 0; i < l; i++) dayset.insert(bloomDay[i]);\n\n vector<int> days;\n for (int day: dayset) days.push_back(day); \n sort(days.begin(), days.end());\n\n int ld = days.size();\n int sti = 0;\n int endi = ld - 1;\n\n while(sti < endi) {\n int midi = (sti + endi) / 2;\n int lm = count(bloomDay, k, days[midi]);\n if (lm >= m) endi = midi;\n else sti = midi+1;\n }\n\n return days[sti];\n }\n};", "memory": "116338" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "\n#pragma GCC optimize(\"Ofast,no-stack-protector\")\n#pragma GCC optimize(\"no-math-errno,unroll-loops\")\n#pragma GCC target(\"sse,sse2,sse3,ssse3,sse4\")\n\n#include <new> // For hardware_constructive_interference_size\n\nstatic int speedup = []() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n\n constexpr size_t cacheLineSize = 64; // Common cache line size in bytes\n constexpr size_t intSize = sizeof(int); // Size of an int in bytes\n constexpr size_t firstFew = cacheLineSize / intSize; // Number of ints that fit into a cache line\n\n std::cout << \"Number of integers that fit into a cache line: \" << firstFew << std::endl;\n\n return 0;\n}();\n\nclass Solution {\npublic:\n int fn(int day,vector<int>& bloomDay, int m, int k, int n) {\n int cur = 0, b = 0;\n for(int i = 0; i < n; i++) {\n if(bloomDay[i]<=day) {\n cur++;\n if(cur==k) {\n cur = 0;\n b++;\n if(b==m) return 1;\n }\n }\n else cur = 0;\n }\n return 0;\n }\n int minDays(vector<int>& bloomDay, int m, int k) {\n int n = bloomDay.size();\n set<int> days(bloomDay.begin(),bloomDay.end());\n vector<int> daysv(days.begin(),days.end());\n int l = 0, r = daysv.size()-1;\n long long int temp = (long long int)m*k;\n if(temp>n) return -1;\n while(l<=r) {\n int mid = l+(r-l)/2;\n if(fn(daysv[mid],bloomDay, m, k, n)) {\n r = mid - 1;\n }\n else {\n l = mid + 1;\n }\n }\n return daysv[l];\n }\n};", "memory": "117749" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int fn(int day,vector<int>& bloomDay, int m, int k, int n) {\n int cur = 0, b = 0;\n for(int i = 0; i < n; i++) {\n if(bloomDay[i]<=day) {\n cur++;\n if(cur==k) {\n cur = 0;\n b++;\n if(b==m) return 1;\n }\n }\n else cur = 0;\n }\n return 0;\n }\n int minDays(vector<int>& bloomDay, int m, int k) {\n int n = bloomDay.size();\n set<int> days(bloomDay.begin(),bloomDay.end());\n vector<int> daysv(days.begin(),days.end());\n int l = 0, r = daysv.size()-1;\n long long int temp = (long long int)m*k;\n if(temp>n) return -1;\n while(l<=r) {\n int mid = l+(r-l)/2;\n if(fn(daysv[mid],bloomDay, m, k, n)) {\n r = mid - 1;\n }\n else {\n l = mid + 1;\n }\n }\n return daysv[l];\n }\n};", "memory": "119160" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minDays(vector<int>& nums, int m, int k) {\n \n set<int> st;\n for (auto x : nums)\n st.insert(x);\n vector<int> nn(st.begin(), st.end());\n int l = 0, h = nn.size() - 1, ans = -1;\n while (l <= h) {\n int mid = ((l + h) / 2);\n int bqts = 0, cnt = 0;\n for (auto x : nums) {\n if (x <= nn[mid]) {\n cnt++;\n } else {\n bqts += (cnt / k);\n cnt = 0;\n }\n }\n bqts += (cnt / k);\n cout<<nn[mid]<<\" \"<<bqts<<endl;\n if (bqts >= m) {\n ans = nn[mid];\n h = mid - 1;\n } else {\n l = mid + 1;\n }\n }\n return ans;\n }\n};", "memory": "119160" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int flower(vector<int> &bloomDay,int n,int day,int &b,int k)\n {\n int f=0;\n int ct=0;\n for(int i=0;i<n;i++)\n {\n if(bloomDay[i]<=day)\n {\n f++;\n ct++;\n if(ct==k)\n {\n b++;\n ct=0;\n }\n }\n else\n {\n ct=0;\n }\n }\n return f;\n }\n int minDays(vector<int>& bloomDay,long long int m,long long int k) {\n long long int n=bloomDay.size();\n if(m*k>n) return -1;\n set<int> s;\n for(int i=0;i<n;i++)\n {\n s.insert(bloomDay[i]);\n }\n vector<int> v;\n for(auto i:s)\n {\n v.push_back(i);\n }\n int low=0;\n int high=v.size()-1;\n while(low<high)\n {\n \n int mid=low+(high-low)/2;\n int b=0;\n int f=flower(bloomDay,n,v[mid],b,k);\n cout<<mid<<\" \"<<f<<\" \"<<b<<endl;\n // if(low==high && b>=m) return v[low] ;\n if(b<m) low=mid+1;\n else high=mid;\n }\n return v[low];\n /* for(int i=0;i<v.size();i++)\n {\n int b=0;\n int f=flower(bloomDay,n,v[i],b,k);\n int total=b;\n \n if(total==m) return v[i];\n if(total>m && i>0)\n {\n return v[i-1];\n }\n else if(total>m && i==0)\n {\n return v[i];\n }\n }*/\n //return -1;\n }\n};", "memory": "120571" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int flower(vector<int> &bloomDay,int n,int day,int &b,int k)\n {\n int f=0;\n int ct=0;\n for(int i=0;i<n;i++)\n {\n if(bloomDay[i]<=day)\n {\n f++;\n ct++;\n if(ct==k)\n {\n b++;\n ct=0;\n }\n }\n else\n {\n ct=0;\n }\n }\n return f;\n }\n int minDays(vector<int>& bloomDay,long long int m,long long int k) {\n long long int n=bloomDay.size();\n if(m*k>n) return -1;\n set<int> s;\n for(int i=0;i<n;i++)\n {\n s.insert(bloomDay[i]);\n }\n vector<int> v;\n for(auto i:s)\n {\n v.push_back(i);\n }\n int low=0;\n int high=v.size()-1;\n while(low<high)\n {\n \n int mid=low+(high-low)/2;\n int b=0;\n int f=flower(bloomDay,n,v[mid],b,k);\n cout<<mid<<\" \"<<f<<\" \"<<b<<endl;\n // if(low==high && b>=m) return v[low] ;\n if(b<m) low=mid+1;\n else high=mid;\n }\n return v[low];\n /* for(int i=0;i<v.size();i++)\n {\n int b=0;\n int f=flower(bloomDay,n,v[i],b,k);\n int total=b;\n \n if(total==m) return v[i];\n if(total>m && i>0)\n {\n return v[i-1];\n }\n else if(total>m && i==0)\n {\n return v[i];\n }\n }*/\n //return -1;\n }\n};", "memory": "120571" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int numOfBouquets(vector<int> bloomDay, int mid,int k){\n int numOfBouquets = 0;\n int flowers = 0;\n for(auto d : bloomDay){\n if(d<=mid){\n flowers++;\n if(flowers==k){\n numOfBouquets++;\n flowers=0;\n }\n }\n else flowers = 0;\n }\n return numOfBouquets;\n }\n int minDays(vector<int>& bloomDay, int m, int k) {\n int n = bloomDay.size();\n if (static_cast<long long>(m) * k > n) return -1;\n int upper = 0;\n upper = *max_element(bloomDay.begin(),bloomDay.end());\n int lower = 0, minDays=-1;\n\n while(lower<=upper){\n int mid = lower + (upper-lower)/2;\n if(numOfBouquets(bloomDay,mid,k)>=m){\n minDays = mid;\n upper = mid - 1;\n } else lower = mid + 1;\n }\n return minDays;\n }\n};", "memory": "121983" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\nprivate:\n long long count_bouquet(vector<int> bloomday,int k,int day){\n int n=bloomday.size();\n long long count=0,ans=0;\n for(int i=0;i<n;i++){\n if(bloomday[i]<=day){\n count++;\n }\n else{\n count=0;\n }\n if(count==k){\n ans++;\n count=0;\n }\n }\n return ans;\n }\npublic:\n int minDays(vector<int>& bloomDay, int m, int k) {\n long long low=1,high=*max_element(bloomDay.begin(),bloomDay.end());\n long long ans=high;\n if(static_cast<long long>(m) * k>bloomDay.size()) return -1;\n while(low<=high){\n long long mid=(low+high)/2;\n long long countbouquet=count_bouquet(bloomDay,k,mid);\n if(countbouquet>=m){\n ans=mid;\n high=mid-1;\n }\n else low=mid+1;\n }\n return ans;\n }\n};", "memory": "121983" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minDays(vector<int>& a, int m, int k) \n {\n int n=a.size();\n multiset<int> s;\n vector<int> b;\n for(int i=0;i<n;i++)\n {\n if(i<(k-1))\n {\n s.insert(a[i]);\n continue;\n }\n s.insert(a[i]);\n b.push_back(*s.rbegin());\n s.erase(s.find(a[i-k+1]));\n }\n int l=1,r=1e9;\n int ans=INT_MAX;\n while(l<=r)\n {\n int mid=l+(r-l)/2;\n int x=0;\n for(int i=0;i<b.size();i++)\n {\n if(b[i]<=mid)\n {\n x++;\n i+=(k-1);\n }\n }\n if(x>=m)\n {\n r=mid-1;\n ans=min(ans,mid);\n }\n else\n l=mid+1;\n }\n if(ans==INT_MAX)\n return -1;\n return ans;\n }\n};", "memory": "123394" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minDays(vector<int>& a, int m, int k) \n {\n int n=a.size();\n multiset<int> s;\n vector<int> b;\n for(int i=0;i<n;i++)\n {\n if(i<(k-1))\n {\n s.insert(a[i]);\n continue;\n }\n s.insert(a[i]);\n b.push_back(*s.rbegin());\n s.erase(s.find(a[i-k+1]));\n }\n int l=1,r=1e9;\n int ans=INT_MAX;\n while(l<=r)\n {\n int mid=l+(r-l)/2;\n int x=0;\n for(int i=0;i<b.size();i++)\n {\n if(b[i]<=mid)\n {\n x++;\n i+=(k-1);\n }\n }\n if(x>=m)\n {\n r=mid-1;\n ans=min(ans,mid);\n }\n else\n l=mid+1;\n }\n if(ans==INT_MAX)\n return -1;\n return ans;\n }\n};", "memory": "123394" }
289
<p>According to&nbsp;<a href="https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life" target="_blank">Wikipedia&#39;s article</a>: &quot;The <b>Game of Life</b>, also known simply as <b>Life</b>, is a cellular automaton devised by the British mathematician John Horton Conway in 1970.&quot;</p> <p>The board is made up of an <code>m x n</code> grid of cells, where each cell has an initial state: <b>live</b> (represented by a <code>1</code>) or <b>dead</b> (represented by a <code>0</code>). Each cell interacts with its <a href="https://en.wikipedia.org/wiki/Moore_neighborhood" target="_blank">eight neighbors</a> (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):</p> <ol> <li>Any live cell with fewer than two live neighbors dies as if caused by under-population.</li> <li>Any live cell with two or three live neighbors lives on to the next generation.</li> <li>Any live cell with more than three live neighbors dies, as if by over-population.</li> <li>Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.</li> </ol> <p><span>The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously. Given the current state of the <code>m x n</code> grid <code>board</code>, return <em>the next state</em>.</span></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/12/26/grid1.jpg" style="width: 562px; height: 322px;" /> <pre> <strong>Input:</strong> board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]] <strong>Output:</strong> [[0,0,0],[1,0,1],[0,1,1],[0,1,0]] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/12/26/grid2.jpg" style="width: 402px; height: 162px;" /> <pre> <strong>Input:</strong> board = [[1,1],[1,0]] <strong>Output:</strong> [[1,1],[1,1]] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == board.length</code></li> <li><code>n == board[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 25</code></li> <li><code>board[i][j]</code> is <code>0</code> or <code>1</code>.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong></p> <ul> <li>Could you solve it in-place? Remember that the board needs to be updated simultaneously: You cannot update some cells first and then use their updated values to update other cells.</li> <li>In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches upon the border of the array (i.e., live cells reach the border). How would you address these problems?</li> </ul>
0
{ "code": "class Solution {\npublic:\n void gameOfLife(vector<vector<int>>& board) {\n int n = board.size();\n int m = board[0].size();\n \n // Auxiliary matrix to store the new state of the board\n vector<vector<int>> newBoard(n, vector<int>(m, 0));\n \n // Directions for 8 possible neighbors\n int delrow[] = {-1, -1, -1, 0, 1, 1, 1, 0};\n int delcol[] = {-1, 0, 1, 1, 1, 0, -1, -1};\n \n // Iterate through each cell in the board\n for (int row = 0; row < n; row++) {\n for (int col = 0; col < m; col++) {\n int liveNeighbors = 0;\n \n // Count live neighbors\n for (int i = 0; i < 8; i++) {\n int nrow = row + delrow[i];\n int ncol = col + delcol[i];\n if (nrow >= 0 && ncol >= 0 && nrow < n && ncol < m && board[nrow][ncol] == 1) {\n liveNeighbors++;\n }\n }\n \n // Apply the Game of Life rules\n if (board[row][col] == 1) {\n if (liveNeighbors < 2 || liveNeighbors > 3) {\n newBoard[row][col] = 0;\n } else {\n newBoard[row][col] = 1;\n }\n } else {\n if (liveNeighbors == 3) {\n newBoard[row][col] = 1;\n } else {\n newBoard[row][col] = 0;\n }\n }\n }\n }\n \n // Copy the new state back to the original board\n board = newBoard;\n }\n};", "memory": "8400" }
289
<p>According to&nbsp;<a href="https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life" target="_blank">Wikipedia&#39;s article</a>: &quot;The <b>Game of Life</b>, also known simply as <b>Life</b>, is a cellular automaton devised by the British mathematician John Horton Conway in 1970.&quot;</p> <p>The board is made up of an <code>m x n</code> grid of cells, where each cell has an initial state: <b>live</b> (represented by a <code>1</code>) or <b>dead</b> (represented by a <code>0</code>). Each cell interacts with its <a href="https://en.wikipedia.org/wiki/Moore_neighborhood" target="_blank">eight neighbors</a> (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):</p> <ol> <li>Any live cell with fewer than two live neighbors dies as if caused by under-population.</li> <li>Any live cell with two or three live neighbors lives on to the next generation.</li> <li>Any live cell with more than three live neighbors dies, as if by over-population.</li> <li>Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.</li> </ol> <p><span>The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously. Given the current state of the <code>m x n</code> grid <code>board</code>, return <em>the next state</em>.</span></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/12/26/grid1.jpg" style="width: 562px; height: 322px;" /> <pre> <strong>Input:</strong> board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]] <strong>Output:</strong> [[0,0,0],[1,0,1],[0,1,1],[0,1,0]] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/12/26/grid2.jpg" style="width: 402px; height: 162px;" /> <pre> <strong>Input:</strong> board = [[1,1],[1,0]] <strong>Output:</strong> [[1,1],[1,1]] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == board.length</code></li> <li><code>n == board[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 25</code></li> <li><code>board[i][j]</code> is <code>0</code> or <code>1</code>.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong></p> <ul> <li>Could you solve it in-place? Remember that the board needs to be updated simultaneously: You cannot update some cells first and then use their updated values to update other cells.</li> <li>In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches upon the border of the array (i.e., live cells reach the border). How would you address these problems?</li> </ul>
0
{ "code": "class Solution {\npublic:\n \n int dirX[8] = {1, -1, 0, 0, +1, -1, +1, -1};\n int dirY[8] = {0, 0, 1, -1 , -1 , 1, 1, -1};\n\n void checkForLiveCell(vector<vector<int>>& board, int i, int j){\n\n int n = board.size();\n int m = board[0].size();\n \n int cnt = 0;\n for(int k = 0; k < 8; k++){\n int x = i + dirX[k];\n int y = j + dirY[k];\n if(x >= 0 && x < n && y >= 0 && y < m){\n if(board[x][y] == 1 || board[x][y] == -2)\n cnt++;\n }\n \n }\n\n if(cnt < 2)board[i][j] = -2;\n else if(cnt > 3) board[i][j] = -2;\n \n }\n\n \n void checkForDeadCell(vector<vector<int>>& board, int i, int j){\n\n int n = board.size();\n int m = board[0].size();\n \n int cnt = 0;\n for(int k = 0; k < 8; k++){\n int x = i + dirX[k];\n int y = j + dirY[k];\n if(x >= 0 && x < n && y >= 0 && y < m){\n if(board[x][y] == 1 || board[x][y] == -2)\n cnt++;\n }\n \n }\n\n if(cnt == 3)board[i][j] = -1; \n \n }\n\n void gameOfLife(vector<vector<int>>& board) {\n int n = board.size();\n int m = board[0].size();\n\n for(int i = 0; i < n; i++){\n for(int j = 0; j < m; j++){\n if(board[i][j] == 0){\n checkForDeadCell(board,i,j);\n }else {\n checkForLiveCell(board,i,j);\n }\n }\n }\n\n for(int i = 0; i < n ;i++){\n for(int j = 0; j < m; j++){\n if(board[i][j] == -1)board[i][j] = 1;\n else if (board[i][j] == -2) board[i][j] = 0;\n }\n }\n \n }\n};", "memory": "8400" }
289
<p>According to&nbsp;<a href="https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life" target="_blank">Wikipedia&#39;s article</a>: &quot;The <b>Game of Life</b>, also known simply as <b>Life</b>, is a cellular automaton devised by the British mathematician John Horton Conway in 1970.&quot;</p> <p>The board is made up of an <code>m x n</code> grid of cells, where each cell has an initial state: <b>live</b> (represented by a <code>1</code>) or <b>dead</b> (represented by a <code>0</code>). Each cell interacts with its <a href="https://en.wikipedia.org/wiki/Moore_neighborhood" target="_blank">eight neighbors</a> (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):</p> <ol> <li>Any live cell with fewer than two live neighbors dies as if caused by under-population.</li> <li>Any live cell with two or three live neighbors lives on to the next generation.</li> <li>Any live cell with more than three live neighbors dies, as if by over-population.</li> <li>Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.</li> </ol> <p><span>The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously. Given the current state of the <code>m x n</code> grid <code>board</code>, return <em>the next state</em>.</span></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/12/26/grid1.jpg" style="width: 562px; height: 322px;" /> <pre> <strong>Input:</strong> board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]] <strong>Output:</strong> [[0,0,0],[1,0,1],[0,1,1],[0,1,0]] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/12/26/grid2.jpg" style="width: 402px; height: 162px;" /> <pre> <strong>Input:</strong> board = [[1,1],[1,0]] <strong>Output:</strong> [[1,1],[1,1]] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == board.length</code></li> <li><code>n == board[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 25</code></li> <li><code>board[i][j]</code> is <code>0</code> or <code>1</code>.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong></p> <ul> <li>Could you solve it in-place? Remember that the board needs to be updated simultaneously: You cannot update some cells first and then use their updated values to update other cells.</li> <li>In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches upon the border of the array (i.e., live cells reach the border). How would you address these problems?</li> </ul>
0
{ "code": "class Solution {\npublic:\n void gameOfLife(vector<vector<int>>& board) {\n int n = board.size();\n int m = board[0].size();\n \n // Auxiliary matrix to store the new state of the board\n vector<vector<int>> newBoard(n, vector<int>(m, 0));\n \n // Directions for 8 possible neighbors\n int delrow[] = {-1, -1, -1, 0, 1, 1, 1, 0};\n int delcol[] = {-1, 0, 1, 1, 1, 0, -1, -1};\n \n // Iterate through each cell in the board\n for (int row = 0; row < n; row++) {\n for (int col = 0; col < m; col++) {\n int liveNeighbors = 0;\n \n // Count live neighbors\n for (int i = 0; i < 8; i++) {\n int nrow = row + delrow[i];\n int ncol = col + delcol[i];\n if (nrow >= 0 && ncol >= 0 && nrow < n && ncol < m && board[nrow][ncol] == 1) {\n liveNeighbors++;\n }\n }\n \n // Apply the Game of Life rules\n if (board[row][col] == 1) {\n if (liveNeighbors < 2 || liveNeighbors > 3) {\n newBoard[row][col] = 0;\n } else {\n newBoard[row][col] = 1;\n }\n } else {\n if (liveNeighbors == 3) {\n newBoard[row][col] = 1;\n } else {\n newBoard[row][col] = 0;\n }\n }\n }\n }\n \n // Copy the new state back to the original board\n board = newBoard;\n }\n};", "memory": "8500" }
289
<p>According to&nbsp;<a href="https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life" target="_blank">Wikipedia&#39;s article</a>: &quot;The <b>Game of Life</b>, also known simply as <b>Life</b>, is a cellular automaton devised by the British mathematician John Horton Conway in 1970.&quot;</p> <p>The board is made up of an <code>m x n</code> grid of cells, where each cell has an initial state: <b>live</b> (represented by a <code>1</code>) or <b>dead</b> (represented by a <code>0</code>). Each cell interacts with its <a href="https://en.wikipedia.org/wiki/Moore_neighborhood" target="_blank">eight neighbors</a> (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):</p> <ol> <li>Any live cell with fewer than two live neighbors dies as if caused by under-population.</li> <li>Any live cell with two or three live neighbors lives on to the next generation.</li> <li>Any live cell with more than three live neighbors dies, as if by over-population.</li> <li>Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.</li> </ol> <p><span>The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously. Given the current state of the <code>m x n</code> grid <code>board</code>, return <em>the next state</em>.</span></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/12/26/grid1.jpg" style="width: 562px; height: 322px;" /> <pre> <strong>Input:</strong> board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]] <strong>Output:</strong> [[0,0,0],[1,0,1],[0,1,1],[0,1,0]] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/12/26/grid2.jpg" style="width: 402px; height: 162px;" /> <pre> <strong>Input:</strong> board = [[1,1],[1,0]] <strong>Output:</strong> [[1,1],[1,1]] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == board.length</code></li> <li><code>n == board[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 25</code></li> <li><code>board[i][j]</code> is <code>0</code> or <code>1</code>.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong></p> <ul> <li>Could you solve it in-place? Remember that the board needs to be updated simultaneously: You cannot update some cells first and then use their updated values to update other cells.</li> <li>In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches upon the border of the array (i.e., live cells reach the border). How would you address these problems?</li> </ul>
0
{ "code": "class Solution {\npublic:\n void gameOfLife(vector<vector<int>>& board) {\n int m = board.size();\n int n = board[0].size();\n for(int i=0;i<m;i++){\n for(int j=0;j<n;j++){\n int sumN = 0;\n if(i+1<m && j+1<n)sumN += board[i+1][j+1]%2;\n if(i+1<m)sumN += board[i+1][j]%2;\n if(j+1<n)sumN += board[i][j+1]%2;\n if(i>0 && j+1<n)sumN += board[i-1][j+1]%2;\n if(i>0)sumN += board[i-1][j]%2;\n if(j>0)sumN += board[i][j-1]%2;\n if(i>0 && j>0)sumN += board[i-1][j-1]%2;\n if(i+1<m && j>0)sumN += board[i+1][j-1]%2;\n if(board[i][j]==0){\n if(sumN==3)board[i][j]=2;\n }\n else{\n if(sumN==2 || sumN==3)board[i][j]=3;\n }\n }\n }\n for(int i=0;i<m;i++){\n for(int j=0;j<n;j++){\n board[i][j]/=2;\n }\n }\n }\n};", "memory": "8500" }
289
<p>According to&nbsp;<a href="https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life" target="_blank">Wikipedia&#39;s article</a>: &quot;The <b>Game of Life</b>, also known simply as <b>Life</b>, is a cellular automaton devised by the British mathematician John Horton Conway in 1970.&quot;</p> <p>The board is made up of an <code>m x n</code> grid of cells, where each cell has an initial state: <b>live</b> (represented by a <code>1</code>) or <b>dead</b> (represented by a <code>0</code>). Each cell interacts with its <a href="https://en.wikipedia.org/wiki/Moore_neighborhood" target="_blank">eight neighbors</a> (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):</p> <ol> <li>Any live cell with fewer than two live neighbors dies as if caused by under-population.</li> <li>Any live cell with two or three live neighbors lives on to the next generation.</li> <li>Any live cell with more than three live neighbors dies, as if by over-population.</li> <li>Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.</li> </ol> <p><span>The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously. Given the current state of the <code>m x n</code> grid <code>board</code>, return <em>the next state</em>.</span></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/12/26/grid1.jpg" style="width: 562px; height: 322px;" /> <pre> <strong>Input:</strong> board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]] <strong>Output:</strong> [[0,0,0],[1,0,1],[0,1,1],[0,1,0]] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/12/26/grid2.jpg" style="width: 402px; height: 162px;" /> <pre> <strong>Input:</strong> board = [[1,1],[1,0]] <strong>Output:</strong> [[1,1],[1,1]] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == board.length</code></li> <li><code>n == board[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 25</code></li> <li><code>board[i][j]</code> is <code>0</code> or <code>1</code>.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong></p> <ul> <li>Could you solve it in-place? Remember that the board needs to be updated simultaneously: You cannot update some cells first and then use their updated values to update other cells.</li> <li>In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches upon the border of the array (i.e., live cells reach the border). How would you address these problems?</li> </ul>
1
{ "code": "class Solution {\npublic:\n void gameOfLife(vector<vector<int>>& board) {\n int m = board.size(), n = board[0].size();\n vector<vector<int>> dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}, {1, 1}, {-1, 1}, {1, -1}, {-1, -1}};\n for(int i = 0; i < m; i++){\n for(int j = 0; j < n; j++){\n int zero = 0, one = 0;\n for(auto& dir : dirs){\n int newx = i + dir[0];\n int newy = j + dir[1];\n if(newx < m && newy < n && newx >= 0 && newy >= 0){\n if(board[newx][newy]&1)one++;\n }\n }\n if(board[i][j] == 1 && (one == 2 || one == 3)){\n board[i][j] |= (1<<1);\n } \n else if(board[i][j] == 0 && one == 3){\n board[i][j] |= (1<<1);\n } \n }\n }\n\n for(auto& x : board){\n for(int& y : x){\n y = y >> 1;\n }\n }\n }\n};", "memory": "8600" }
289
<p>According to&nbsp;<a href="https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life" target="_blank">Wikipedia&#39;s article</a>: &quot;The <b>Game of Life</b>, also known simply as <b>Life</b>, is a cellular automaton devised by the British mathematician John Horton Conway in 1970.&quot;</p> <p>The board is made up of an <code>m x n</code> grid of cells, where each cell has an initial state: <b>live</b> (represented by a <code>1</code>) or <b>dead</b> (represented by a <code>0</code>). Each cell interacts with its <a href="https://en.wikipedia.org/wiki/Moore_neighborhood" target="_blank">eight neighbors</a> (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):</p> <ol> <li>Any live cell with fewer than two live neighbors dies as if caused by under-population.</li> <li>Any live cell with two or three live neighbors lives on to the next generation.</li> <li>Any live cell with more than three live neighbors dies, as if by over-population.</li> <li>Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.</li> </ol> <p><span>The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously. Given the current state of the <code>m x n</code> grid <code>board</code>, return <em>the next state</em>.</span></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/12/26/grid1.jpg" style="width: 562px; height: 322px;" /> <pre> <strong>Input:</strong> board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]] <strong>Output:</strong> [[0,0,0],[1,0,1],[0,1,1],[0,1,0]] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/12/26/grid2.jpg" style="width: 402px; height: 162px;" /> <pre> <strong>Input:</strong> board = [[1,1],[1,0]] <strong>Output:</strong> [[1,1],[1,1]] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == board.length</code></li> <li><code>n == board[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 25</code></li> <li><code>board[i][j]</code> is <code>0</code> or <code>1</code>.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong></p> <ul> <li>Could you solve it in-place? Remember that the board needs to be updated simultaneously: You cannot update some cells first and then use their updated values to update other cells.</li> <li>In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches upon the border of the array (i.e., live cells reach the border). How would you address these problems?</li> </ul>
1
{ "code": "class Solution {\n private:\n int solve(int i,int j,vector<vector<int>>& board,int m,int n){\n int count=0;\n if(i+1<m&&j+1<n){\n count+=board[i+1][j+1];\n }\n if(i-1>=0&&j-1>=0){\n count+=board[i-1][j-1];\n }\n if(j-1>=0&&i+1<m){\n count+=board[i+1][j-1];\n }\n if(i-1>=0&&j+1<n){\n count+=board[i-1][j+1];\n }\n if(i-1>=0){\n count+=board[i-1][j];\n }\n if(i+1<m){\n count+=board[i+1][j];\n }\n if(j+1<n){\n count+=board[i][j+1];\n }\n if(j-1>=0){\n count+=board[i][j-1];\n }\n return count;\n }\npublic:\n void gameOfLife(vector<vector<int>>& board) {\n int m=board.size();\n int n=board[0].size();\n vector<vector<int>>dummy(m,vector<int>(n,0));\n for(int i=0;i<m;i++){\n for(int j=0;j<n;j++){\n int count=solve(i,j,board,m,n);\n if(count==2){\n dummy[i][j]=board[i][j];\n }\n else if(count==3){\n dummy[i][j]=1;\n\n \n }else if(count>3){\n dummy[i][j]=0;\n }\n\n \n }\n }\n board=dummy;\n }\n};", "memory": "8600" }
289
<p>According to&nbsp;<a href="https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life" target="_blank">Wikipedia&#39;s article</a>: &quot;The <b>Game of Life</b>, also known simply as <b>Life</b>, is a cellular automaton devised by the British mathematician John Horton Conway in 1970.&quot;</p> <p>The board is made up of an <code>m x n</code> grid of cells, where each cell has an initial state: <b>live</b> (represented by a <code>1</code>) or <b>dead</b> (represented by a <code>0</code>). Each cell interacts with its <a href="https://en.wikipedia.org/wiki/Moore_neighborhood" target="_blank">eight neighbors</a> (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):</p> <ol> <li>Any live cell with fewer than two live neighbors dies as if caused by under-population.</li> <li>Any live cell with two or three live neighbors lives on to the next generation.</li> <li>Any live cell with more than three live neighbors dies, as if by over-population.</li> <li>Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.</li> </ol> <p><span>The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously. Given the current state of the <code>m x n</code> grid <code>board</code>, return <em>the next state</em>.</span></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/12/26/grid1.jpg" style="width: 562px; height: 322px;" /> <pre> <strong>Input:</strong> board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]] <strong>Output:</strong> [[0,0,0],[1,0,1],[0,1,1],[0,1,0]] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/12/26/grid2.jpg" style="width: 402px; height: 162px;" /> <pre> <strong>Input:</strong> board = [[1,1],[1,0]] <strong>Output:</strong> [[1,1],[1,1]] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == board.length</code></li> <li><code>n == board[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 25</code></li> <li><code>board[i][j]</code> is <code>0</code> or <code>1</code>.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong></p> <ul> <li>Could you solve it in-place? Remember that the board needs to be updated simultaneously: You cannot update some cells first and then use their updated values to update other cells.</li> <li>In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches upon the border of the array (i.e., live cells reach the border). How would you address these problems?</li> </ul>
3
{ "code": "class Solution {\npublic:\n void gameOfLife(vector<vector<int>>& board) {\n vector<vector<int>> copiedBoard = board;\n int m = board.size(), n = board[0].size();\n vector<vector<int>> directions = {{-1, -1}, {-1, 0}, {-1, 1}, {0, 1},\n {1, 1}, {1, 0}, {1, -1}, {0, -1}};\n for (int row = 0; row < m; row++) {\n for (int col = 0; col < n; col++) {\n int liveNbr = 0;\n for (int d = 0; d < 8; d++) {\n int dx = directions[d][0];\n int dy = directions[d][1];\n\n int newRow = row + dx;\n int newCol = col + dy;\n\n bool rowInBound = newRow >= 0 && newRow < m;\n bool colInBound = newCol >= 0 && newCol < n;\n\n if (rowInBound && colInBound && copiedBoard[newRow][newCol]) {\n liveNbr++;\n }\n }\n\n if (copiedBoard[row][col]) {\n if ((liveNbr < 2) or (liveNbr > 3)) {\n board[row][col] = 0;\n } else {\n board[row][col] = 1;\n }\n } else {\n if (liveNbr == 3) {\n board[row][col] = 1;\n }\n }\n }\n }\n return;\n }\n};", "memory": "8700" }
289
<p>According to&nbsp;<a href="https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life" target="_blank">Wikipedia&#39;s article</a>: &quot;The <b>Game of Life</b>, also known simply as <b>Life</b>, is a cellular automaton devised by the British mathematician John Horton Conway in 1970.&quot;</p> <p>The board is made up of an <code>m x n</code> grid of cells, where each cell has an initial state: <b>live</b> (represented by a <code>1</code>) or <b>dead</b> (represented by a <code>0</code>). Each cell interacts with its <a href="https://en.wikipedia.org/wiki/Moore_neighborhood" target="_blank">eight neighbors</a> (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):</p> <ol> <li>Any live cell with fewer than two live neighbors dies as if caused by under-population.</li> <li>Any live cell with two or three live neighbors lives on to the next generation.</li> <li>Any live cell with more than three live neighbors dies, as if by over-population.</li> <li>Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.</li> </ol> <p><span>The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously. Given the current state of the <code>m x n</code> grid <code>board</code>, return <em>the next state</em>.</span></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/12/26/grid1.jpg" style="width: 562px; height: 322px;" /> <pre> <strong>Input:</strong> board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]] <strong>Output:</strong> [[0,0,0],[1,0,1],[0,1,1],[0,1,0]] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/12/26/grid2.jpg" style="width: 402px; height: 162px;" /> <pre> <strong>Input:</strong> board = [[1,1],[1,0]] <strong>Output:</strong> [[1,1],[1,1]] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == board.length</code></li> <li><code>n == board[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 25</code></li> <li><code>board[i][j]</code> is <code>0</code> or <code>1</code>.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong></p> <ul> <li>Could you solve it in-place? Remember that the board needs to be updated simultaneously: You cannot update some cells first and then use their updated values to update other cells.</li> <li>In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches upon the border of the array (i.e., live cells reach the border). How would you address these problems?</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int lnc(vector<vector<int>>& board,int i,int j)\n {\n vector<pair<int,int>>directions={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};\n int live_neigh=0;\n for(auto dir:directions)\n {\n int ni=i+dir.first;\n int nj=j+dir.second;\n if(ni>=0 && ni<board.size() && nj>=0 && nj<board[0].size() && abs(board[ni][nj])==1)\n {\n live_neigh++;\n }\n }\n return live_neigh;\n }\n void gameOfLife(vector<vector<int>>& board) \n {\n int n=board.size();\n int m=board[0].size();\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<m;j++)\n {\n int count=lnc(board,i,j);\n if(board[i][j]==0 && count==3)\n {\n board[i][j]=2;\n }\n if(board[i][j]==1 && (count<2 ||count>3))\n {\n board[i][j]=-1;\n }\n }\n }\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<m;j++)\n {\n if(board[i][j]==2)\n {\n board[i][j]=1;\n }\n else if(board[i][j]==-1)\n {\n board[i][j]=0;\n }\n }\n }\n }\n};", "memory": "8700" }
289
<p>According to&nbsp;<a href="https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life" target="_blank">Wikipedia&#39;s article</a>: &quot;The <b>Game of Life</b>, also known simply as <b>Life</b>, is a cellular automaton devised by the British mathematician John Horton Conway in 1970.&quot;</p> <p>The board is made up of an <code>m x n</code> grid of cells, where each cell has an initial state: <b>live</b> (represented by a <code>1</code>) or <b>dead</b> (represented by a <code>0</code>). Each cell interacts with its <a href="https://en.wikipedia.org/wiki/Moore_neighborhood" target="_blank">eight neighbors</a> (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):</p> <ol> <li>Any live cell with fewer than two live neighbors dies as if caused by under-population.</li> <li>Any live cell with two or three live neighbors lives on to the next generation.</li> <li>Any live cell with more than three live neighbors dies, as if by over-population.</li> <li>Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.</li> </ol> <p><span>The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously. Given the current state of the <code>m x n</code> grid <code>board</code>, return <em>the next state</em>.</span></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/12/26/grid1.jpg" style="width: 562px; height: 322px;" /> <pre> <strong>Input:</strong> board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]] <strong>Output:</strong> [[0,0,0],[1,0,1],[0,1,1],[0,1,0]] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/12/26/grid2.jpg" style="width: 402px; height: 162px;" /> <pre> <strong>Input:</strong> board = [[1,1],[1,0]] <strong>Output:</strong> [[1,1],[1,1]] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == board.length</code></li> <li><code>n == board[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 25</code></li> <li><code>board[i][j]</code> is <code>0</code> or <code>1</code>.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong></p> <ul> <li>Could you solve it in-place? Remember that the board needs to be updated simultaneously: You cannot update some cells first and then use their updated values to update other cells.</li> <li>In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches upon the border of the array (i.e., live cells reach the border). How would you address these problems?</li> </ul>
3
{ "code": "class Solution {\nprotected:\n vector<vector<int>> b1;\npublic:\n void gameOfLife(vector<vector<int>>& board) {\n b1 = board;\n int n = board.size(), m = board[0].size();\n for(int i=0; i<n; i++) for(int j=0; j<m; j++) updateCell(board, i, j, n, m);\n }\n\n void updateCell(vector<vector<int>>& board, int x, int y, int n, int m){\n int val = 0;\n vector<int> rows = {-1, -1, -1, 0, 0, 1, 1, 1};\n vector<int> cols = {-1, 0, 1, -1, 1, -1, 0, 1};\n for(int i=0; i<rows.size(); i++){\n int nx = x + rows[i], ny = y + cols[i];\n if(nx>=0 && ny>=0 && nx<n && ny<m) val += b1[nx][ny];\n }\n if(b1[x][y] && val < 2) board[x][y] = 0;\n else if(b1[x][y] && 2 <= val && val <= 3) board[x][y] = 1;\n else if(b1[x][y] && val > 3) board[x][y] = 0;\n else if(!b1[x][y] && val == 3) board[x][y] = 1;\n }\n};\n// 1 2 3\n// 4 6\n// 7 8 9", "memory": "8800" }
289
<p>According to&nbsp;<a href="https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life" target="_blank">Wikipedia&#39;s article</a>: &quot;The <b>Game of Life</b>, also known simply as <b>Life</b>, is a cellular automaton devised by the British mathematician John Horton Conway in 1970.&quot;</p> <p>The board is made up of an <code>m x n</code> grid of cells, where each cell has an initial state: <b>live</b> (represented by a <code>1</code>) or <b>dead</b> (represented by a <code>0</code>). Each cell interacts with its <a href="https://en.wikipedia.org/wiki/Moore_neighborhood" target="_blank">eight neighbors</a> (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):</p> <ol> <li>Any live cell with fewer than two live neighbors dies as if caused by under-population.</li> <li>Any live cell with two or three live neighbors lives on to the next generation.</li> <li>Any live cell with more than three live neighbors dies, as if by over-population.</li> <li>Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.</li> </ol> <p><span>The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously. Given the current state of the <code>m x n</code> grid <code>board</code>, return <em>the next state</em>.</span></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/12/26/grid1.jpg" style="width: 562px; height: 322px;" /> <pre> <strong>Input:</strong> board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]] <strong>Output:</strong> [[0,0,0],[1,0,1],[0,1,1],[0,1,0]] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/12/26/grid2.jpg" style="width: 402px; height: 162px;" /> <pre> <strong>Input:</strong> board = [[1,1],[1,0]] <strong>Output:</strong> [[1,1],[1,1]] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == board.length</code></li> <li><code>n == board[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 25</code></li> <li><code>board[i][j]</code> is <code>0</code> or <code>1</code>.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong></p> <ul> <li>Could you solve it in-place? Remember that the board needs to be updated simultaneously: You cannot update some cells first and then use their updated values to update other cells.</li> <li>In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches upon the border of the array (i.e., live cells reach the border). How would you address these problems?</li> </ul>
3
{ "code": "class Solution {\nprotected:\n vector<vector<int>> b1;\npublic:\n void gameOfLife(vector<vector<int>>& board) {\n b1 = board;\n int n = board.size(), m = board[0].size();\n for(int i=0; i<n; i++) for(int j=0; j<m; j++) updateCell(board, i, j, n, m);\n for(int i=0; i<n; i++){\n for(int j=0; j<m; j++){\n cout << board[i][j] << \" \";\n }\n cout << endl;\n }\n }\n\n void updateCell(vector<vector<int>>& board, int x, int y, int n, int m){\n int val = 0;\n vector<int> rows = {-1, -1, -1, 0, 0, 1, 1, 1};\n vector<int> cols = {-1, 0, 1, -1, 1, -1, 0, 1};\n for(int i=0; i<rows.size(); i++){\n int nx = x + rows[i], ny = y + cols[i];\n if(nx>=0 && ny>=0 && nx<n && ny<m) val += b1[nx][ny];\n }\n cout << x << \", \" << y << \", new = \" << val << endl;\n if(b1[x][y] && val < 2) board[x][y] = 0;\n else if(b1[x][y] && 2 <= val && val <= 3) board[x][y] = 1;\n else if(b1[x][y] && val > 3) board[x][y] = 0;\n else if(!b1[x][y] && val == 3) board[x][y] = 1;\n }\n};\n// 1 2 3\n// 4 6\n// 7 8 9", "memory": "8900" }
290
<p>Given a <code>pattern</code> and a string <code>s</code>, find if <code>s</code>&nbsp;follows the same pattern.</p> <p>Here <b>follow</b> means a full match, such that there is a bijection between a letter in <code>pattern</code> and a <b>non-empty</b> word in <code>s</code>. Specifically:</p> <ul> <li>Each letter in <code>pattern</code> maps to <strong>exactly</strong> one unique word in <code>s</code>.</li> <li>Each unique word in <code>s</code> maps to <strong>exactly</strong> one letter in <code>pattern</code>.</li> <li>No two letters map to the same word, and no two words map to the same letter.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">pattern = &quot;abba&quot;, s = &quot;dog cat cat dog&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>The bijection can be established as:</p> <ul> <li><code>&#39;a&#39;</code> maps to <code>&quot;dog&quot;</code>.</li> <li><code>&#39;b&#39;</code> maps to <code>&quot;cat&quot;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">pattern = &quot;abba&quot;, s = &quot;dog cat cat fish&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">pattern = &quot;aaaa&quot;, s = &quot;dog cat cat dog&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= pattern.length &lt;= 300</code></li> <li><code>pattern</code> contains only lower-case English letters.</li> <li><code>1 &lt;= s.length &lt;= 3000</code></li> <li><code>s</code> contains only lowercase English letters and spaces <code>&#39; &#39;</code>.</li> <li><code>s</code> <strong>does not contain</strong> any leading or trailing spaces.</li> <li>All the words in <code>s</code> are separated by a <strong>single space</strong>.</li> </ul>
0
{ "code": "#include <algorithm>\n#include <array>\n#include <string>\n#include <string_view>\n#include <unordered_set>\n#include <utility>\n\nclass Solution {\npublic:\n bool wordPattern(std::string pattern, std::string s) {\n std::array<std::string_view, 26> fwd_map;\n std::unordered_set<std::string_view> bwd_set;\n\n auto p_end = cend(pattern);\n auto p_it = cbegin(pattern);\n auto s_end = cend(s);\n auto s_it = cbegin(s);\n\n for (;;) {\n auto p = *(p_it++);\n auto w_end = std::find(next(s_it), s_end, ' ');\n std::string_view sv = {s_it, w_end};\n if (empty(fwd_map[p - 'a'])) {\n auto [it, inserted] = bwd_set.insert(sv);\n if (not inserted) return false;\n fwd_map[p - 'a'] = sv;\n } else {\n if (fwd_map[p - 'a'] != sv) return false;\n }\n\n if (w_end == s_end) return p_it == p_end;\n if (p_it == p_end) return false;\n s_it = next(w_end);\n }\n std::unreachable();\n }\n};", "memory": "7500" }
290
<p>Given a <code>pattern</code> and a string <code>s</code>, find if <code>s</code>&nbsp;follows the same pattern.</p> <p>Here <b>follow</b> means a full match, such that there is a bijection between a letter in <code>pattern</code> and a <b>non-empty</b> word in <code>s</code>. Specifically:</p> <ul> <li>Each letter in <code>pattern</code> maps to <strong>exactly</strong> one unique word in <code>s</code>.</li> <li>Each unique word in <code>s</code> maps to <strong>exactly</strong> one letter in <code>pattern</code>.</li> <li>No two letters map to the same word, and no two words map to the same letter.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">pattern = &quot;abba&quot;, s = &quot;dog cat cat dog&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>The bijection can be established as:</p> <ul> <li><code>&#39;a&#39;</code> maps to <code>&quot;dog&quot;</code>.</li> <li><code>&#39;b&#39;</code> maps to <code>&quot;cat&quot;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">pattern = &quot;abba&quot;, s = &quot;dog cat cat fish&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">pattern = &quot;aaaa&quot;, s = &quot;dog cat cat dog&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= pattern.length &lt;= 300</code></li> <li><code>pattern</code> contains only lower-case English letters.</li> <li><code>1 &lt;= s.length &lt;= 3000</code></li> <li><code>s</code> contains only lowercase English letters and spaces <code>&#39; &#39;</code>.</li> <li><code>s</code> <strong>does not contain</strong> any leading or trailing spaces.</li> <li>All the words in <code>s</code> are separated by a <strong>single space</strong>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool wordPattern(string pattern, string s) {\n unordered_map<char, string>mapping;\n unordered_map<char, bool>found;\n int n = pattern.length();\n int len = s.length();\n\n int j=0;\n\n for(int i=0;i<n;i++) {\n string word = \"\";\n while(j<len && s[j]!=' ') {\n word += s[j];\n j++;\n }\n j++;\n if(word == \"\") return false;\n if(found.find(pattern[i])!=found.end()) {\n if(mapping[pattern[i]] != word) return false;\n }\n else {\n for(const auto& it: mapping) {\n if(it.second == word) return false;\n }\n found[pattern[i]] = true;\n mapping[pattern[i]] = word;\n }\n }\n if(j<len) return false;\n return true;\n }\n};", "memory": "7600" }
290
<p>Given a <code>pattern</code> and a string <code>s</code>, find if <code>s</code>&nbsp;follows the same pattern.</p> <p>Here <b>follow</b> means a full match, such that there is a bijection between a letter in <code>pattern</code> and a <b>non-empty</b> word in <code>s</code>. Specifically:</p> <ul> <li>Each letter in <code>pattern</code> maps to <strong>exactly</strong> one unique word in <code>s</code>.</li> <li>Each unique word in <code>s</code> maps to <strong>exactly</strong> one letter in <code>pattern</code>.</li> <li>No two letters map to the same word, and no two words map to the same letter.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">pattern = &quot;abba&quot;, s = &quot;dog cat cat dog&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>The bijection can be established as:</p> <ul> <li><code>&#39;a&#39;</code> maps to <code>&quot;dog&quot;</code>.</li> <li><code>&#39;b&#39;</code> maps to <code>&quot;cat&quot;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">pattern = &quot;abba&quot;, s = &quot;dog cat cat fish&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">pattern = &quot;aaaa&quot;, s = &quot;dog cat cat dog&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= pattern.length &lt;= 300</code></li> <li><code>pattern</code> contains only lower-case English letters.</li> <li><code>1 &lt;= s.length &lt;= 3000</code></li> <li><code>s</code> contains only lowercase English letters and spaces <code>&#39; &#39;</code>.</li> <li><code>s</code> <strong>does not contain</strong> any leading or trailing spaces.</li> <li>All the words in <code>s</code> are separated by a <strong>single space</strong>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool wordPattern(string pattern, string s) {\n std::unordered_map<std::string_view, char> um;\n int was[26] = {0};\n int first = 0, second = 0;\n int word_id = 0;\n for (int i = 0; i <= s.size(); ++i) {\n if (s[i] == ' ' || i == int(s.size())) {\n second = i;\n std::string_view word = {s.begin() + first, s.begin() + second};\n std::cout << pattern[word_id] << \" \" << word << \"\\n\";\n if (um.find(word) == um.end()) {\n if (was[pattern[word_id] - 'a']) {\n return false;\n }\n um[word] = pattern[word_id];\n was[pattern[word_id] - 'a'] = true;\n } else {\n if (um[word] != pattern[word_id]) {\n return false;\n }\n }\n\n word_id++;\n if (i != int(s.size()) && word_id == pattern.size()) {\n return false;\n }\n first = second + 1;\n }\n }\n if (word_id != pattern.size()) {\n return false;\n }\n\n return true;\n }\n};", "memory": "7600" }
290
<p>Given a <code>pattern</code> and a string <code>s</code>, find if <code>s</code>&nbsp;follows the same pattern.</p> <p>Here <b>follow</b> means a full match, such that there is a bijection between a letter in <code>pattern</code> and a <b>non-empty</b> word in <code>s</code>. Specifically:</p> <ul> <li>Each letter in <code>pattern</code> maps to <strong>exactly</strong> one unique word in <code>s</code>.</li> <li>Each unique word in <code>s</code> maps to <strong>exactly</strong> one letter in <code>pattern</code>.</li> <li>No two letters map to the same word, and no two words map to the same letter.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">pattern = &quot;abba&quot;, s = &quot;dog cat cat dog&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>The bijection can be established as:</p> <ul> <li><code>&#39;a&#39;</code> maps to <code>&quot;dog&quot;</code>.</li> <li><code>&#39;b&#39;</code> maps to <code>&quot;cat&quot;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">pattern = &quot;abba&quot;, s = &quot;dog cat cat fish&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">pattern = &quot;aaaa&quot;, s = &quot;dog cat cat dog&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= pattern.length &lt;= 300</code></li> <li><code>pattern</code> contains only lower-case English letters.</li> <li><code>1 &lt;= s.length &lt;= 3000</code></li> <li><code>s</code> contains only lowercase English letters and spaces <code>&#39; &#39;</code>.</li> <li><code>s</code> <strong>does not contain</strong> any leading or trailing spaces.</li> <li>All the words in <code>s</code> are separated by a <strong>single space</strong>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool wordPattern(string pattern, string s) {\n unordered_map<char, string> map; //<pattern, s>\n string temp;\n vector<string> word;\n for (int i = 0; i <= s.length(); i++) {\n if (i == s.length() || s[i] == ' ') {\n word.push_back(temp);\n temp.clear();\n }\n else\n temp += s[i];\n }\n if (word.size() != pattern.size())\n return false;\n for(int i = 0; i < pattern.size(); i++){\n if(map.count(pattern[i])){\n if(map[pattern[i]] != word[i])\n return false;\n }\n else{\n for(const auto& lit : map){\n if(lit.second == word[i])\n return false;\n }\n map[pattern[i]] = word[i];\n }\n }\n return true;\n }\n};", "memory": "7700" }
290
<p>Given a <code>pattern</code> and a string <code>s</code>, find if <code>s</code>&nbsp;follows the same pattern.</p> <p>Here <b>follow</b> means a full match, such that there is a bijection between a letter in <code>pattern</code> and a <b>non-empty</b> word in <code>s</code>. Specifically:</p> <ul> <li>Each letter in <code>pattern</code> maps to <strong>exactly</strong> one unique word in <code>s</code>.</li> <li>Each unique word in <code>s</code> maps to <strong>exactly</strong> one letter in <code>pattern</code>.</li> <li>No two letters map to the same word, and no two words map to the same letter.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">pattern = &quot;abba&quot;, s = &quot;dog cat cat dog&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>The bijection can be established as:</p> <ul> <li><code>&#39;a&#39;</code> maps to <code>&quot;dog&quot;</code>.</li> <li><code>&#39;b&#39;</code> maps to <code>&quot;cat&quot;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">pattern = &quot;abba&quot;, s = &quot;dog cat cat fish&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">pattern = &quot;aaaa&quot;, s = &quot;dog cat cat dog&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= pattern.length &lt;= 300</code></li> <li><code>pattern</code> contains only lower-case English letters.</li> <li><code>1 &lt;= s.length &lt;= 3000</code></li> <li><code>s</code> contains only lowercase English letters and spaces <code>&#39; &#39;</code>.</li> <li><code>s</code> <strong>does not contain</strong> any leading or trailing spaces.</li> <li>All the words in <code>s</code> are separated by a <strong>single space</strong>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool wordPattern(string pattern, string s) {\n unordered_map<char, string> mapp; //<pattern, s>\n string temp;\n int j = 0;\n for (int i = 0; i <= s.length(); i++) {\n if (s[i] == '\\0' || s[i] == ' ') {\n if(pattern[j] == '\\0')\n return false;\n if (!mapp.count(pattern[j])) {\n for (const auto& k : mapp) {\n if (k.second == temp)\n return false;\n }\n mapp[pattern[j]] = temp;\n }\n else if (mapp[pattern[j]] != temp)\n return false;\n j++;\n temp.clear();\n } else\n temp += s[i];\n }\n if(pattern.size() > j)\n return false;\n return true;\n }\n};", "memory": "7800" }
290
<p>Given a <code>pattern</code> and a string <code>s</code>, find if <code>s</code>&nbsp;follows the same pattern.</p> <p>Here <b>follow</b> means a full match, such that there is a bijection between a letter in <code>pattern</code> and a <b>non-empty</b> word in <code>s</code>. Specifically:</p> <ul> <li>Each letter in <code>pattern</code> maps to <strong>exactly</strong> one unique word in <code>s</code>.</li> <li>Each unique word in <code>s</code> maps to <strong>exactly</strong> one letter in <code>pattern</code>.</li> <li>No two letters map to the same word, and no two words map to the same letter.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">pattern = &quot;abba&quot;, s = &quot;dog cat cat dog&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>The bijection can be established as:</p> <ul> <li><code>&#39;a&#39;</code> maps to <code>&quot;dog&quot;</code>.</li> <li><code>&#39;b&#39;</code> maps to <code>&quot;cat&quot;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">pattern = &quot;abba&quot;, s = &quot;dog cat cat fish&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">pattern = &quot;aaaa&quot;, s = &quot;dog cat cat dog&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= pattern.length &lt;= 300</code></li> <li><code>pattern</code> contains only lower-case English letters.</li> <li><code>1 &lt;= s.length &lt;= 3000</code></li> <li><code>s</code> contains only lowercase English letters and spaces <code>&#39; &#39;</code>.</li> <li><code>s</code> <strong>does not contain</strong> any leading or trailing spaces.</li> <li>All the words in <code>s</code> are separated by a <strong>single space</strong>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool wordPattern(string pattern, string s) {\n unordered_map<string, char> word_to_letter;\n vector<int> used(26);\n\n string word = \"\";\n int curr_letter = 0;\n\n for(int i=0; i<s.size(); i++){\n if(s[i] == ' ' || i == s.size()-1){\n if(i == s.size()-1) word += s[i];\n if(word_to_letter.count(word) == 0){\n if(curr_letter == pattern.size() || used[pattern[curr_letter]-'a'] == 1) return false;\n\n word_to_letter[word] = pattern[curr_letter];\n used[pattern[curr_letter]-'a'] = 1;\n }else{\n if(curr_letter == pattern.size() || word_to_letter[word] != pattern[curr_letter])\n return false;\n }\n word = \"\";\n curr_letter++; \n }else\n word += s[i];\n }\n if(curr_letter != pattern.size())\n return false;\n return true;\n }\n};", "memory": "7800" }
290
<p>Given a <code>pattern</code> and a string <code>s</code>, find if <code>s</code>&nbsp;follows the same pattern.</p> <p>Here <b>follow</b> means a full match, such that there is a bijection between a letter in <code>pattern</code> and a <b>non-empty</b> word in <code>s</code>. Specifically:</p> <ul> <li>Each letter in <code>pattern</code> maps to <strong>exactly</strong> one unique word in <code>s</code>.</li> <li>Each unique word in <code>s</code> maps to <strong>exactly</strong> one letter in <code>pattern</code>.</li> <li>No two letters map to the same word, and no two words map to the same letter.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">pattern = &quot;abba&quot;, s = &quot;dog cat cat dog&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>The bijection can be established as:</p> <ul> <li><code>&#39;a&#39;</code> maps to <code>&quot;dog&quot;</code>.</li> <li><code>&#39;b&#39;</code> maps to <code>&quot;cat&quot;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">pattern = &quot;abba&quot;, s = &quot;dog cat cat fish&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">pattern = &quot;aaaa&quot;, s = &quot;dog cat cat dog&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= pattern.length &lt;= 300</code></li> <li><code>pattern</code> contains only lower-case English letters.</li> <li><code>1 &lt;= s.length &lt;= 3000</code></li> <li><code>s</code> contains only lowercase English letters and spaces <code>&#39; &#39;</code>.</li> <li><code>s</code> <strong>does not contain</strong> any leading or trailing spaces.</li> <li>All the words in <code>s</code> are separated by a <strong>single space</strong>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool wordPattern(string pattern, string s) {\n unordered_map<unsigned long, char> hashmap;\n unordered_map<char, unsigned long> hashmap2;\n unsigned long hash = 5381;\n int beg = 0;\n int end = 0;\n int count;\n for(count = 0; count < pattern.length(); count++){\n while(true){\n if(s[end] == ' ' || s[end] == '\\0')\n break;\n end++;\n }\n\n if(beg == end)\n break;\n\n unsigned long val = hash;\n\n for(int i = beg; i < end; i++){\n val = ((val << 5) + val) + s[i];\n }\n\n if(hashmap.find(val) == hashmap.end()){\n if(hashmap2.find(pattern[count]) == hashmap2.end())\n {\n hashmap.insert({val, pattern[count]});\n hashmap2.insert({pattern[count], val});\n }\n else\n return false;\n }\n else{\n char comp = hashmap[val];\n if(comp != pattern[count])\n return false;\n }\n end++;\n beg = end;\n }\n if(count != pattern.length() || end < s.length())\n return false;\n return true;\n }\n};", "memory": "7900" }
290
<p>Given a <code>pattern</code> and a string <code>s</code>, find if <code>s</code>&nbsp;follows the same pattern.</p> <p>Here <b>follow</b> means a full match, such that there is a bijection between a letter in <code>pattern</code> and a <b>non-empty</b> word in <code>s</code>. Specifically:</p> <ul> <li>Each letter in <code>pattern</code> maps to <strong>exactly</strong> one unique word in <code>s</code>.</li> <li>Each unique word in <code>s</code> maps to <strong>exactly</strong> one letter in <code>pattern</code>.</li> <li>No two letters map to the same word, and no two words map to the same letter.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">pattern = &quot;abba&quot;, s = &quot;dog cat cat dog&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>The bijection can be established as:</p> <ul> <li><code>&#39;a&#39;</code> maps to <code>&quot;dog&quot;</code>.</li> <li><code>&#39;b&#39;</code> maps to <code>&quot;cat&quot;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">pattern = &quot;abba&quot;, s = &quot;dog cat cat fish&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">pattern = &quot;aaaa&quot;, s = &quot;dog cat cat dog&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= pattern.length &lt;= 300</code></li> <li><code>pattern</code> contains only lower-case English letters.</li> <li><code>1 &lt;= s.length &lt;= 3000</code></li> <li><code>s</code> contains only lowercase English letters and spaces <code>&#39; &#39;</code>.</li> <li><code>s</code> <strong>does not contain</strong> any leading or trailing spaces.</li> <li>All the words in <code>s</code> are separated by a <strong>single space</strong>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool wordPattern(string pattern, string s) {\n vector<string> v(26,\"\");\n set<string> seen;\n stringstream ss(s);\n string word;\n int n = pattern.size();\n int i = 0;\n while(ss>>word){\n if(i==n) return false;\n if(v[pattern[i]-'a']==\"\"){\n if(seen.count(word)) return false;\n seen.insert(word);\n v[pattern[i]-'a'] = word;\n } \n else if(v[pattern[i]-'a']!=word) return false;\n i++;\n }\n if(i<n) return false;\n return true;\n }\n};", "memory": "7900" }
290
<p>Given a <code>pattern</code> and a string <code>s</code>, find if <code>s</code>&nbsp;follows the same pattern.</p> <p>Here <b>follow</b> means a full match, such that there is a bijection between a letter in <code>pattern</code> and a <b>non-empty</b> word in <code>s</code>. Specifically:</p> <ul> <li>Each letter in <code>pattern</code> maps to <strong>exactly</strong> one unique word in <code>s</code>.</li> <li>Each unique word in <code>s</code> maps to <strong>exactly</strong> one letter in <code>pattern</code>.</li> <li>No two letters map to the same word, and no two words map to the same letter.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">pattern = &quot;abba&quot;, s = &quot;dog cat cat dog&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>The bijection can be established as:</p> <ul> <li><code>&#39;a&#39;</code> maps to <code>&quot;dog&quot;</code>.</li> <li><code>&#39;b&#39;</code> maps to <code>&quot;cat&quot;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">pattern = &quot;abba&quot;, s = &quot;dog cat cat fish&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">pattern = &quot;aaaa&quot;, s = &quot;dog cat cat dog&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= pattern.length &lt;= 300</code></li> <li><code>pattern</code> contains only lower-case English letters.</li> <li><code>1 &lt;= s.length &lt;= 3000</code></li> <li><code>s</code> contains only lowercase English letters and spaces <code>&#39; &#39;</code>.</li> <li><code>s</code> <strong>does not contain</strong> any leading or trailing spaces.</li> <li>All the words in <code>s</code> are separated by a <strong>single space</strong>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool wordPattern(string pattern, string s) {\n unordered_map<char,string> p;\n unordered_map<string,char> q;\n string word=\"\";\n int j=0;\n for(int i=0;i<s.length();i++){\n if(s[i]==' '){\n if(p.count(pattern[j])){\n if(p[pattern[j]]!=word){\n return false;\n }\n }\n else{\n p[pattern[j]]=word;\n }\n if(q.count(word)){\n if(q[word]!=pattern[j]){\n return false;\n }\n }\n else{\n q[word]=pattern[j];\n }\n \n j++;\n word=\"\";\n continue;\n }\n word+=s[i];\n if(j>=pattern.size()){\n return false;\n }\n }\n \n if(p.count(pattern[j])){\n if(p[pattern[j]]!=word){\n return false;\n }\n }\n if(q.count(word)){\n if(q[word]!=pattern[j]){\n return false;\n }\n }\n j++;\n if(j<pattern.size()){\n return false;\n }\n return true;\n }\n};", "memory": "8000" }
290
<p>Given a <code>pattern</code> and a string <code>s</code>, find if <code>s</code>&nbsp;follows the same pattern.</p> <p>Here <b>follow</b> means a full match, such that there is a bijection between a letter in <code>pattern</code> and a <b>non-empty</b> word in <code>s</code>. Specifically:</p> <ul> <li>Each letter in <code>pattern</code> maps to <strong>exactly</strong> one unique word in <code>s</code>.</li> <li>Each unique word in <code>s</code> maps to <strong>exactly</strong> one letter in <code>pattern</code>.</li> <li>No two letters map to the same word, and no two words map to the same letter.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">pattern = &quot;abba&quot;, s = &quot;dog cat cat dog&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>The bijection can be established as:</p> <ul> <li><code>&#39;a&#39;</code> maps to <code>&quot;dog&quot;</code>.</li> <li><code>&#39;b&#39;</code> maps to <code>&quot;cat&quot;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">pattern = &quot;abba&quot;, s = &quot;dog cat cat fish&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">pattern = &quot;aaaa&quot;, s = &quot;dog cat cat dog&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= pattern.length &lt;= 300</code></li> <li><code>pattern</code> contains only lower-case English letters.</li> <li><code>1 &lt;= s.length &lt;= 3000</code></li> <li><code>s</code> contains only lowercase English letters and spaces <code>&#39; &#39;</code>.</li> <li><code>s</code> <strong>does not contain</strong> any leading or trailing spaces.</li> <li>All the words in <code>s</code> are separated by a <strong>single space</strong>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool wordPattern(string pattern, string s) {\n map<char, string> pmap;\n map<string, char> smap;\n vector<string> words;\n stringstream ss(s); \n string word;\n \n while (ss >> word) {\n words.push_back(word); \n }\n \n if (words.size() != pattern.size()) {\n return false;\n }\n for (int i = 0; i < pattern.size(); i++) {\n char pchar = pattern[i];\n string w = words[i];\n if (pmap.find(pchar) != pmap.end()) {\n if (pmap[pchar] != w) {\n return false;\n }\n } else {\n if (smap.find(w) != smap.end() && smap[w] != pchar) {\n return false;\n }\n pmap[pchar] = w;\n smap[w] = pchar;\n }\n }\n \n return true; \n }\n};\n", "memory": "8000" }
290
<p>Given a <code>pattern</code> and a string <code>s</code>, find if <code>s</code>&nbsp;follows the same pattern.</p> <p>Here <b>follow</b> means a full match, such that there is a bijection between a letter in <code>pattern</code> and a <b>non-empty</b> word in <code>s</code>. Specifically:</p> <ul> <li>Each letter in <code>pattern</code> maps to <strong>exactly</strong> one unique word in <code>s</code>.</li> <li>Each unique word in <code>s</code> maps to <strong>exactly</strong> one letter in <code>pattern</code>.</li> <li>No two letters map to the same word, and no two words map to the same letter.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">pattern = &quot;abba&quot;, s = &quot;dog cat cat dog&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>The bijection can be established as:</p> <ul> <li><code>&#39;a&#39;</code> maps to <code>&quot;dog&quot;</code>.</li> <li><code>&#39;b&#39;</code> maps to <code>&quot;cat&quot;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">pattern = &quot;abba&quot;, s = &quot;dog cat cat fish&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">pattern = &quot;aaaa&quot;, s = &quot;dog cat cat dog&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= pattern.length &lt;= 300</code></li> <li><code>pattern</code> contains only lower-case English letters.</li> <li><code>1 &lt;= s.length &lt;= 3000</code></li> <li><code>s</code> contains only lowercase English letters and spaces <code>&#39; &#39;</code>.</li> <li><code>s</code> <strong>does not contain</strong> any leading or trailing spaces.</li> <li>All the words in <code>s</code> are separated by a <strong>single space</strong>.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool wordPattern(string pattern, string s) {\n int pattern_len = pattern.size(), s_len = s.size();\n\n vector<char> patterns;\n for (int i = 0; i < pattern_len; i++) patterns.push_back(pattern[i]);\n vector<string> strings;\n for (int i = 0; i < s_len; i++) {\n string tmp;\n while (i < s_len && s[i] != ' ') tmp += s[i++];\n strings.push_back(tmp);\n }\n\n int s_num = strings.size();\n\n if (pattern_len != s_num) return false;\n\n unordered_map<char, string> patterns_to_strings_to_patterns;\n unordered_map<string, char> strings_to_patterns;\n\n bool result = true;\n\n int idx = 0;\n for (int i = 0; i < pattern_len; i++) {\n char curr_pattern = patterns[i];\n string curr_s = strings[i];\n if (patterns_to_strings_to_patterns.find(curr_pattern) == patterns_to_strings_to_patterns.end()) {\n if (strings_to_patterns.find(curr_s) != strings_to_patterns.end()) {\n result = false;\n break;\n } else {\n patterns_to_strings_to_patterns[curr_pattern] = curr_s;\n strings_to_patterns[curr_s] = curr_pattern;\n }\n } else {\n if (patterns_to_strings_to_patterns[curr_pattern] != curr_s) {\n result = false;\n break;\n }\n }\n }\n\n\n return result;\n }\n};", "memory": "8100" }
290
<p>Given a <code>pattern</code> and a string <code>s</code>, find if <code>s</code>&nbsp;follows the same pattern.</p> <p>Here <b>follow</b> means a full match, such that there is a bijection between a letter in <code>pattern</code> and a <b>non-empty</b> word in <code>s</code>. Specifically:</p> <ul> <li>Each letter in <code>pattern</code> maps to <strong>exactly</strong> one unique word in <code>s</code>.</li> <li>Each unique word in <code>s</code> maps to <strong>exactly</strong> one letter in <code>pattern</code>.</li> <li>No two letters map to the same word, and no two words map to the same letter.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">pattern = &quot;abba&quot;, s = &quot;dog cat cat dog&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>The bijection can be established as:</p> <ul> <li><code>&#39;a&#39;</code> maps to <code>&quot;dog&quot;</code>.</li> <li><code>&#39;b&#39;</code> maps to <code>&quot;cat&quot;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">pattern = &quot;abba&quot;, s = &quot;dog cat cat fish&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">pattern = &quot;aaaa&quot;, s = &quot;dog cat cat dog&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= pattern.length &lt;= 300</code></li> <li><code>pattern</code> contains only lower-case English letters.</li> <li><code>1 &lt;= s.length &lt;= 3000</code></li> <li><code>s</code> contains only lowercase English letters and spaces <code>&#39; &#39;</code>.</li> <li><code>s</code> <strong>does not contain</strong> any leading or trailing spaces.</li> <li>All the words in <code>s</code> are separated by a <strong>single space</strong>.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool wordPattern(string pattern, string s) {\n s=s+' ';\n unordered_map<string,char>mp1;\n unordered_map<char,string>mp2;\n string temp=\"\";\n int ind=0;\n for(auto i:s){\n if(i==' '){\n if(mp1.count(temp)>0 && mp1[temp]!=pattern[ind]){\n return false;\n }\n else{\n mp1[temp]=pattern[ind];\n }\n\n if(mp2.count(pattern[ind])>0 && mp2[pattern[ind]]!=temp){\n return false;\n }\n else{\n mp2[pattern[ind]]=temp;\n }\n ind++;\n temp.clear();\n }\n else{\n temp+=i;\n }\n\n }\n \n return ind == pattern.size(); \n }\n};", "memory": "8100" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int n = nums.size();\n for(int i=0; i<n; i++) {\n //cout << i << endl;\n while(nums[i] != nums[nums[i]-1] && nums[i] != i+1) {\n //cout<<nums[i]<<endl;\n int tmp = nums[nums[i]-1];\n nums[nums[i]-1] = nums[i];\n nums[i] = tmp;\n }\n if(nums[i] == i+1) {\n continue;\n }\n if(nums[i] == nums[nums[i]-1]) {\n return nums[i];\n }\n }\n return 0;\n }\n};", "memory": "17603" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int slow = 0, fast = 0;\n\n while(true)\n {\n slow = nums[slow];\n fast = nums[nums[fast]];\n if(slow == fast)\n break;\n }\n\n int temp = 0;\n while(temp != slow)\n {\n temp = nums[temp];\n slow = nums[slow];\n }\n\n return slow;\n\n }\n};", "memory": "17603" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int n=nums.size();//\n int i=0;\n while(i<n)\n {\n int crt=nums[i]-1;\n if(nums[i]==i+1)i++;\n else if(nums[i]!=nums[crt])swap(nums[i],nums[crt]);\n else i++;\n \n }\n for(int i=0;i<n;i++)\n {\n if(nums[i]!=i+1)return nums[i];\n }\n return nums[i];\n }\n};", "memory": "18724" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n for(int i=0;i<nums.size();i++){\n if(nums[i]<0){\n int n=nums[i]*-1;\n if(nums[n]<0){\n return n;\n }\n else{\n nums[n]=nums[n]*-1;\n }\n }\n else{\n if(nums[nums[i]]<0){\n return nums[i];\n }\n nums[nums[i]]=nums[nums[i]]*-1;\n }\n }\n return 0;\n }\n};", "memory": "18724" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int slow = nums[0];\n int fast = nums[0];\n\n while(true){\n slow = nums[slow];\n fast = nums[nums[fast]];\n if(fast==slow){\n break;\n }\n }\n\n fast = nums[0];\n while(slow!=fast){\n slow = nums[slow];\n fast = nums[fast];\n }\n return slow;\n }\n};", "memory": "19845" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int n=nums.size();\n for(int i=0;i<n;i++)\n {\n int index=nums[i]%n;\n nums[index]+=n;\n }\n for(int i=0;i<n;i++)\n {\n int val=nums[i];\n if(val/n>=2)\n return i;\n }\n return -1;\n }\n};", "memory": "19845" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n for(int i = 0; i < nums.size(); i++){\n int idx = abs(nums[i]) - 1;\n if(nums[idx] < 0) return abs(nums[i]);\n else nums[idx] = -1*nums[idx];\n }\n return -1;\n }\n};", "memory": "20966" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n // int n= nums.size();\n \n // for(int i=0; i<n; i++){\n // for(int j=i+1; j<n; j++){\n // if(nums[i]==nums[j]){\n // return nums[i];\n // };\n // };\n // };\n // return -1;\n \n\n //another method\n while(nums[0] != nums[nums[0]]){\n swap(nums[0], nums[nums[0]]);\n }\n return nums[0];\n };\n};", "memory": "20966" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
1
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int slow=nums[0];\n int fast=nums[slow];\n while(fast!=slow){\n fast = nums[nums[fast]];\n slow = nums[slow];\n }\n int s=0 ;\n while(s!=fast){\n s=nums[s];\n fast=nums[fast];\n }\n return s;\n }\n};", "memory": "22088" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
1
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int tortoise = nums[0];\n int hare = nums[0];\n \n do {\n tortoise = nums[tortoise];\n hare = nums[nums[hare]];\n } while (tortoise != hare);\n \n tortoise = nums[0]; \n while (tortoise != hare) {\n tortoise = nums[tortoise];\n hare = nums[hare];\n }\n \n return hare; \n }\n};\n", "memory": "22088" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int n = nums.size();\n vector<bool> vis(n+1, false);\n\n for(auto num:nums){\n if(vis[num]){\n return num;\n }\n vis[num] = true;\n }\n\n return -1;\n }\n};", "memory": "23209" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int n = nums.size();\n vector<bool> visit(n,false);\n for(int i:nums){\n if(visit[i]==false) visit[i] = true;\n else\n return i;\n }\n return -1;\n }\n};", "memory": "23209" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int vis[nums.size()];\n\n for(int i = 0; i < nums.size(); i++) {\n vis[i] = 0;\n }\n\n for(int i = 0; i < nums.size(); i++) {\n if(vis[nums[i]] == 1) {\n return nums[i];\n }\n vis[nums[i]] = 1;\n }\n return 0;\n }\n};", "memory": "24330" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int a[100000]={0};\n for(int i=0;i<nums.size();i++){\n a[nums[i]-1]++;\n }\n for(int i=0;i<100000;i++){\n if(a[i]>1){\n return i+1;\n }\n }\n return 0;\n }\n};", "memory": "25451" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int a[100000]={0};\n for(int i=0;i<nums.size();i++){\n a[nums[i]-1]++;\n }\n for(int i=0;i<100000;i++){\n if(a[i]>1){\n return i+1;\n }\n }\n return 0;\n }\n};", "memory": "25451" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int ans;\n int hash[100001]={0};\n for(int i=0;i<nums.size();i++){\n hash[nums[i]]+=1;\n }\n for(int i=0;i<nums.size();i++){\n if(hash[nums[i]]>1){\n ans=nums[i];\n break;\n }\n }\n return ans;\n }\n \n};", "memory": "26573" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findDuplicate(const vector<int>& nums) {\n vector<char> seen(nums.size(), 0);\n for (auto x : nums) {\n if (std::exchange(seen[x], true)) return x;\n }\n return -1; \n }\n};", "memory": "27694" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n bool* check = new bool[nums.size()+1];\n for(int i = 1; i < nums.size()+1; i++){\n check[i] = false;\n }\n for(auto it : nums){\n if(check[it]) return it;\n check[it] = true;\n }\n return 69;\n }\n};", "memory": "28815" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int num=-1;\n\n // for(int i=0; i<nums.size(); i++){\n // for(int j=i+1; j<nums.size(); j++){\n // if(nums[i]==nums[j]){\n // num = nums[i];\n // break;\n // }\n // }\n // }\n\n // return num;\n\n sort(nums.begin(), nums.end());\n\n for(int i =1; i<nums.size();i++) {\n\n if(nums[i]==nums[i-1]) {\n num= nums[i];\n }\n }\n \n return num;\n \n \n }\n};", "memory": "29936" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n sort(nums.begin(),nums.end());\n int n = nums.size();\n for(int i=0;i<n-1;i++){\n if(nums[i]==nums[i+1]) return nums[i];\n }\n return 0;\n }\n};", "memory": "31058" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n for(int i = 1; i < nums.size(); i++) {\n if(nums[i] == nums[i-1]) return nums[i];\n }\n return -1;\n }\n};", "memory": "31058" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n sort(nums.begin(),nums.end());\n // 1 2 2 3 4\nint ans=-1;\n for(int i=0;i<nums.size()-1;i++)\n {\n if(nums[i]==nums[i+1]){\n ans= nums[i];\n break;\n }\n }\n\nreturn ans;\n }\n};", "memory": "32179" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
2
{ "code": "class Solution {\npublic:\n // int duplicatenumber(vector<int> &nums){\n // int i,j;\n // int n= nums.size();\n // while(i< n){\n // for(int i=0; i<=n; i++){\n // for(int j=i+1; j<=n; j++){\n // if(nums[i]==nums[j]){\n // return nums[i];\n // }\n // else\n // i++;\n \n // }\n // }\n \n\n // }\n // return -1;\n // }\n int findDuplicate(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n\n for(int i=0; i<nums.size()-1; i++){\n if(nums[i]==nums[i+1]){\n return nums[i];\n }\n \n }\n return -1;\n }\n};", "memory": "32179" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int n = nums.size();\n sort(nums.begin(), nums.end());\n\n for(int i = 0; i < n-1; i++){\n if(nums[i] == nums[i+1]){\n return nums[i];\n }\n }\n return -1;\n }\n};", "memory": "33300" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n for(int i=1;i<nums.size();i++){\n if(nums[i-1]==nums[i]){\n return nums[i];\n }\n }\n return nums[0];\n }\n};", "memory": "33300" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n sort(nums.begin(),nums.end());\n for(int i;i<nums.size();i++){\n if(nums[i]==nums[i+1]){\n return nums[i];\n }\n }\n return -1;\n \n \n \n }\n \n \n};", "memory": "34421" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int n=nums.size();\n sort(nums.begin(),nums.end());\n for(int i=1;i<n;i++){\n if(nums[i-1]==nums[i]){\n return nums[i];\n }\n \n \n }\n return -1;\n }\n};", "memory": "34421" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n sort(nums.begin(),nums.end());\n for(int i=0;i<nums.size()-1;i++){\n if(nums[i]==nums[i+1]){\n return nums[i];\n }\n }\n return -1;\n }\n};", "memory": "35543" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n sort(nums.begin(),nums.end());\n for(int i = 1 ; i < nums.size(); i++)\n {\n if(nums[i-1] == nums[i])\n {\n return nums[i];\n }\n }\n\n return 0;}\n};", "memory": "35543" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int size=nums.size();\n int value;\n int arr[size];\n for(int i=0;i<size;i++)\n {\n arr[i]=nums[i];\n \n }\n sort(arr,arr+size);\n for(int i=0;i<size;i++)\n {\n if(arr[i]==arr[i+1]){\n value=arr[i];\n break;\n\n } \n }\n \n return value;\n\n \n }\n};", "memory": "36664" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int n = nums.size();\n int arr[n];\n for(int i = 0 ; i < n ; i++){\n arr[i] = nums[i]; \n }\n sort(arr , arr+n);\n for(int i = 0 ; i < n-1 ; i++){\n if(arr[i] == arr[i +1]){\n return arr[i];\n }\n }\n return 0;\n }\n};", "memory": "37785" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int ans = 0;\n sort(nums.begin(),nums.end());\n int vecsize = nums.size();\n // Initializing the maximum array\n // cout<<nums[vecsize -1] + 1;\n int dup[nums[vecsize -1] + 1];\n for(int i = 0;i<(nums[vecsize -1] + 1);i++)\n {\n dup[i] = 0;\n }\n // int dup[nums[vecsize -1] + 1] = {0};\n for(int i = 0;i<vecsize;i++)\n {\n ++dup[nums[i]];\n }\n for(int i = 1;i<(nums[vecsize -1] + 1);i++)\n {\n if(dup[i] >= 2)\n {\n ans = i;\n break;\n }\n else\n {\n continue;\n }\n }\n return ans;\n\n }\n};", "memory": "38906" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int n=nums.size();\n vector<int>temp(n+1,0);\n for(int i=0;i<nums.size();i++){\n temp[nums[i]-1]++;\n }\n for(int i=0;i<temp.size();i++){\n if(temp[i]>=2)return i+1;\n }\n return {};\n }\n};", "memory": "40028" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n //approch 1: sort\n // int ans=0;\n // sort(nums.begin(),nums.end());\n // for(int i=1;i<nums.size();i++){\n // if(nums[i]==nums[i-1]){\n // ans=nums[i];\n // break;\n // }\n // }\n // return ans;\n\n //approch 2: hasmap\n int n=nums.size();\n vector<int> map(n,0);\n for(int i=0;i<nums.size();i++){\n map[nums[i]]++;\n }\n int ans=0;\n for(int i=0;i<n;i++){\n if(map[i]>1){\n ans=i;\n break;\n }\n }\n return ans;\n \n\n }\n};", "memory": "43391" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n \n int n=nums.size();\n\n vector<int>ans(n,0);\n\n for(int i=0;i<n;i++){\n\n ans[nums[i]]++;\n\n }\n\n for(int i=0;i<ans.size();i++){\n\n if(ans[i]>=2){\n\n return i;\n }\n\n }\n\n return -1;\n }\n};", "memory": "44513" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int n = nums.size();\n vector<int> count(n+1, 0);\n\n for(int i = 0; i < n; i++){\n count[nums[i]]++;\n if(count[nums[i]] > 1){\n return nums[i];\n } \n }\n return -1;\n }\n};", "memory": "44513" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n\n int n = nums.size();\n vector<int> frq(n+1,0);\n for(int i=0;i<n;i++){\n if(frq[nums[i]]==0){\n frq[nums[i]]+=1;\n }else {\n return nums[i];\n }\n }\n return -1;\n }\n};", "memory": "45634" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int> n) {\n sync;\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n \n sort(n.begin(),n.end());\n\n for(int i=0;i<n.size();i++){\n if(n[i]==n[i+1]){\n return n[i];\n }\n }\n\n return -1;\n }\n};", "memory": "46755" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int> nums) {\n sort(nums.begin(),nums.end());\n for(int i = 0 ; i < nums.size()-1; i++ ){\n if(nums[i] == nums[i+1]){\n return nums[i];\n }\n }\n return 0;\n }\n};", "memory": "47876" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int n=nums.size();\n vector<int> arr(nums.begin(),nums.end());\n sort(arr.begin(),arr.end());\n for(int i=0;i<n-1;i++){\n if(arr[i]==arr[i+1])\n return arr[i];\n }\n return 0;\n }\n};", "memory": "48998" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n vector<int> V=nums;\n sort(V.begin(),V.end());\n for(int i=0;i<V.size()-1;i++){\n if(V[i]==V[i+1]) return V[i];\n }\n return V[V.size()-1];\n }\n};", "memory": "50119" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n vector<int> V=nums;\n sort(V.begin(),V.end());\n for(int i=0;i<V.size()-1;i++){\n if(V[i]==V[i+1]) return V[i];\n }\n return V[V.size()-1];\n }\n};", "memory": "50119" }
287
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,4,2,2] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,3,4,2] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3,3,3] <strong>Output:</strong> 3</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n + 1</code></li> <li><code>1 &lt;= nums[i] &lt;= n</code></li> <li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li> </ul> <p>&nbsp;</p> <p><b>Follow up:</b></p> <ul> <li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li> <li>Can you solve the problem in linear runtime complexity?</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findDuplicate(vector<int>& nums) {\n int i=0,c=0;\n vector<int> v=nums;\n sort(v.begin(),v.end());\n while(i!=(v.size()-1)){\n if(v[i]==v[i+1]){\n c=v[i];\n break;\n }\n i++;\n }\n return c;\n }\n};", "memory": "51240" }