id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
118 | <p>Given an integer <code>numRows</code>, return the first numRows of <strong>Pascal's triangle</strong>.</p>
<p>In <strong>Pascal's triangle</strong>, each number is the sum of the two numbers directly above it as shown:</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif" style="height:240px; width:260px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> numRows = 5
<strong>Output:</strong> [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> numRows = 1
<strong>Output:</strong> [[1]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= numRows <= 30</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> generate(int numRows) {\n // Initialize the result vector\n vector<vector<int>> result;\n\n // Generate each row of Pascal's Triangle\n for (int i = 0; i < numRows; ++i) {\n // Create a row with 1s\n vector<int> row(i + 1, 1);\n\n // Compute the middle values for the current row\n for (int j = 1; j < i; ++j) {\n row[j] = result[i-1][j-1] + result[i-1][j];\n }\n\n // Add the row to the result\n result.push_back(row);\n }\n\n return result;\n}\n \n};",
"memory": "7900"
} |
118 | <p>Given an integer <code>numRows</code>, return the first numRows of <strong>Pascal's triangle</strong>.</p>
<p>In <strong>Pascal's triangle</strong>, each number is the sum of the two numbers directly above it as shown:</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif" style="height:240px; width:260px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> numRows = 5
<strong>Output:</strong> [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> numRows = 1
<strong>Output:</strong> [[1]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= numRows <= 30</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> generate(int n) {\n vector<vector<int>> ans = {{1}};\n for(int i = 1; i < n; i++){\n vector<int> temp(i + 1, 1);\n for(int j = 1; j < i; j++){\n temp[j] = ans[i - 1][j] + ans[i - 1][j - 1];\n }\n ans.push_back(temp);\n }\n return ans;\n }\n};",
"memory": "7900"
} |
118 | <p>Given an integer <code>numRows</code>, return the first numRows of <strong>Pascal's triangle</strong>.</p>
<p>In <strong>Pascal's triangle</strong>, each number is the sum of the two numbers directly above it as shown:</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif" style="height:240px; width:260px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> numRows = 5
<strong>Output:</strong> [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> numRows = 1
<strong>Output:</strong> [[1]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= numRows <= 30</code></li>
</ul>
| 0 | {
"code": "class Solution{\nprivate:\n void helper(int row, vector<int>& curr){\n curr.push_back(1);\n int res=1;\n for(int i=1; i<row; i++){\n res*=(row-i);\n res=res/i;\n curr.push_back(res);\n }\n }\n\npublic:\n vector<vector<int>> generate(int N){\n vector<vector<int>> res;\n for(int i=1; i<=N; i++){\n vector<int> curr;\n helper(i,curr);\n res.push_back(curr);\n }\n return res;\n }\n}; ",
"memory": "8000"
} |
118 | <p>Given an integer <code>numRows</code>, return the first numRows of <strong>Pascal's triangle</strong>.</p>
<p>In <strong>Pascal's triangle</strong>, each number is the sum of the two numbers directly above it as shown:</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif" style="height:240px; width:260px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> numRows = 5
<strong>Output:</strong> [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> numRows = 1
<strong>Output:</strong> [[1]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= numRows <= 30</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int>generateRow(int n){\n long long res=1;\n vector<int>ansRow;\n ansRow.push_back(1);\n for(int i=1;i<n;i++){\n res=res*(n-i);\n res=res/(i);\n ansRow.push_back(res);\n }\n return ansRow;\n }\n vector<vector<int>> generate(int numRows) {\n vector<vector<int>>ans;\n for(int i=1;i<=numRows;i++){\n ans.push_back(generateRow(i));\n }\n return ans;\n }\n};",
"memory": "8000"
} |
118 | <p>Given an integer <code>numRows</code>, return the first numRows of <strong>Pascal's triangle</strong>.</p>
<p>In <strong>Pascal's triangle</strong>, each number is the sum of the two numbers directly above it as shown:</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif" style="height:240px; width:260px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> numRows = 5
<strong>Output:</strong> [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> numRows = 1
<strong>Output:</strong> [[1]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= numRows <= 30</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> generate(int numRows) {\n vector<vector<int>> matrix(numRows);\n matrix[0].push_back({1});\n for (int i = 1; i < numRows; i++) {\n\n matrix[i].resize(i + 1); // this is the optimized approach\n\n for (int j = 0; j <= i; j++) {\n if (j == 0 || j == i)\n matrix[i][j] = 1;\n else\n matrix[i][j] = matrix[i - 1][j - 1] + matrix[i - 1][j];\n }\n }\n return matrix;\n }\n};",
"memory": "8100"
} |
118 | <p>Given an integer <code>numRows</code>, return the first numRows of <strong>Pascal's triangle</strong>.</p>
<p>In <strong>Pascal's triangle</strong>, each number is the sum of the two numbers directly above it as shown:</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif" style="height:240px; width:260px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> numRows = 5
<strong>Output:</strong> [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> numRows = 1
<strong>Output:</strong> [[1]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= numRows <= 30</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> generate(int numRows) {\n vector<vector<int>> pascal(numRows);\n int c = 0;\n pascal[0].push_back(1);\n for(int i=1;i<numRows;i++){\n pascal[i].resize(i + 1);\n for(int j=0;j<=i;j++){\n if(j == 0 || j == i){\n c = 1;\n }\n else{\n c = c*(i-j+1)/j; \n }\n pascal[i][j] = c;\n }\n }\n return pascal;\n }\n};",
"memory": "8100"
} |
118 | <p>Given an integer <code>numRows</code>, return the first numRows of <strong>Pascal's triangle</strong>.</p>
<p>In <strong>Pascal's triangle</strong>, each number is the sum of the two numbers directly above it as shown:</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif" style="height:240px; width:260px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> numRows = 5
<strong>Output:</strong> [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> numRows = 1
<strong>Output:</strong> [[1]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= numRows <= 30</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> generate(int numRows) {\n vector<vector<int>> result;\n\n for(int i=0; i<numRows; i++){\n vector<int> rows(i+1, 1);\n\n for(int j=1; j<i; j++){\n rows[j] = result[i-1][j-1] + result[i-1][j];\n }\n result.push_back(rows);\n }\n return result;\n }\n};",
"memory": "8200"
} |
118 | <p>Given an integer <code>numRows</code>, return the first numRows of <strong>Pascal's triangle</strong>.</p>
<p>In <strong>Pascal's triangle</strong>, each number is the sum of the two numbers directly above it as shown:</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif" style="height:240px; width:260px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> numRows = 5
<strong>Output:</strong> [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> numRows = 1
<strong>Output:</strong> [[1]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= numRows <= 30</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> generate(int numRows) {\n vector<vector<int>> vp(numRows);\n vp[0] = {1};\n for(int i=1;i<numRows;i++){\n vp[i].push_back(1);\n\n for(int j=0;j<i-1;j++){\n vp[i].push_back(vp[i-1][j] + vp[i-1][j+1]);\n }\n\n vp[i].push_back(1);\n }\n return vp;\n }\n};",
"memory": "8200"
} |
118 | <p>Given an integer <code>numRows</code>, return the first numRows of <strong>Pascal's triangle</strong>.</p>
<p>In <strong>Pascal's triangle</strong>, each number is the sum of the two numbers directly above it as shown:</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif" style="height:240px; width:260px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> numRows = 5
<strong>Output:</strong> [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> numRows = 1
<strong>Output:</strong> [[1]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= numRows <= 30</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long int fact(int k)\n {\n if(k==0)\n return 1;\n long long int ans=1;\n while(k>0)\n {\n ans*=k;\n k-=1;\n }\n return ans;\n }\n\n int comb(int n, int r)\n {\n int z=min(r,n-r);\n long long int numerator=1,denominator;\n for(int i=0;i<z;i++)\n {\n numerator*=n;\n n-=1;\n }\n denominator=fact(z);\n int ans=numerator/denominator;\n return ans;\n }\n\n vector<int> helper(int num)\n {\n int element;vector<int> row;\n for(int i=0;i<num;i++)\n {\n element=comb(num,i);\n row.push_back(element);\n }\n row.push_back(1);\n return row;\n }\n\n vector<vector<int>> generate(int numRows) \n {\n vector<vector<int>> result;vector<int> item;\n for(int i=0;i<numRows;i++)\n {\n item=helper(i);\n result.push_back(item);\n }\n\n return result;\n \n }\n};",
"memory": "8300"
} |
118 | <p>Given an integer <code>numRows</code>, return the first numRows of <strong>Pascal's triangle</strong>.</p>
<p>In <strong>Pascal's triangle</strong>, each number is the sum of the two numbers directly above it as shown:</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif" style="height:240px; width:260px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> numRows = 5
<strong>Output:</strong> [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> numRows = 1
<strong>Output:</strong> [[1]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= numRows <= 30</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long int fact(int k)\n {\n if(k==0)\n return 1;\n long long int ans=1;\n while(k>0)\n {\n ans*=k;\n k-=1;\n }\n return ans;\n }\n\n int comb(int n, int r)\n {\n int z=min(r,n-r);\n long long int numerator=1,denominator;\n for(int i=0;i<z;i++)\n {\n numerator*=n;\n n-=1;\n }\n denominator=fact(z);\n int ans=numerator/denominator;\n return ans;\n }\n\n vector<int> helper(int num)\n {\n int element;vector<int> row;\n for(int i=0;i<num;i++)\n {\n element=comb(num,i);\n row.push_back(element);\n }\n row.push_back(1);\n return row;\n }\n\n vector<vector<int>> generate(int numRows) \n {\n vector<vector<int>> result;vector<int> item;\n for(int i=0;i<numRows;i++)\n {\n item=helper(i);\n result.push_back(item);\n }\n\n return result;\n \n }\n};",
"memory": "8300"
} |
118 | <p>Given an integer <code>numRows</code>, return the first numRows of <strong>Pascal's triangle</strong>.</p>
<p>In <strong>Pascal's triangle</strong>, each number is the sum of the two numbers directly above it as shown:</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif" style="height:240px; width:260px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> numRows = 5
<strong>Output:</strong> [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> numRows = 1
<strong>Output:</strong> [[1]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= numRows <= 30</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> generate(int numRows) {\n std::vector<std::vector<int>> res;\n res.push_back({1});\n\n for (int i = 0; i < numRows - 1; i++) {\n std::vector<int> dummyRow = {0};\n dummyRow.insert(dummyRow.end(), res.back().begin(), res.back().end());\n dummyRow.push_back(0);\n std::vector<int> row;\n\n for (int j = 0; j < dummyRow.size() - 1; j++) {\n row.push_back(dummyRow[j] + dummyRow[j + 1]);\n }\n\n res.push_back(row);\n }\n\n return res; \n }\n};",
"memory": "8400"
} |
118 | <p>Given an integer <code>numRows</code>, return the first numRows of <strong>Pascal's triangle</strong>.</p>
<p>In <strong>Pascal's triangle</strong>, each number is the sum of the two numbers directly above it as shown:</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif" style="height:240px; width:260px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> numRows = 5
<strong>Output:</strong> [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> numRows = 1
<strong>Output:</strong> [[1]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= numRows <= 30</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> generate(int numRows) {\n vector<int>prev={1};\n vector<int>curr={1,1};\n vector<vector<int>> sol;\n\n if(numRows==1){\n sol.push_back(prev);\n return sol;\n }\n if(numRows==2){\n sol.push_back(prev);\n sol.push_back(curr);\n return sol;\n }\n\n sol.push_back(prev);\n sol.push_back(curr);\n\n while(numRows>2){\n vector<int>newRow;\n\n newRow.push_back(1);\n for(int i=0;i<curr.size()-1;i++){\n newRow.push_back(curr[i]+curr[i+1]);\n }\n newRow.push_back(1);\n\n curr=newRow;\n sol.push_back(newRow);\n --numRows;\n }\n\n return sol;\n }\n};",
"memory": "8400"
} |
118 | <p>Given an integer <code>numRows</code>, return the first numRows of <strong>Pascal's triangle</strong>.</p>
<p>In <strong>Pascal's triangle</strong>, each number is the sum of the two numbers directly above it as shown:</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif" style="height:240px; width:260px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> numRows = 5
<strong>Output:</strong> [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> numRows = 1
<strong>Output:</strong> [[1]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= numRows <= 30</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> generate(int numRows) {\n std::vector<std::vector<int>> res;\n res.push_back({1});\n\n for (int i = 0; i < numRows - 1; i++) {\n std::vector<int> dummyRow = {0};\n dummyRow.insert(dummyRow.end(), res.back().begin(), res.back().end());\n dummyRow.push_back(0);\n std::vector<int> row;\n\n for (int j = 0; j < dummyRow.size() - 1; j++) {\n row.push_back(dummyRow[j] + dummyRow[j + 1]);\n }\n\n res.push_back(row);\n }\n\n return res; \n }\n};",
"memory": "8500"
} |
118 | <p>Given an integer <code>numRows</code>, return the first numRows of <strong>Pascal's triangle</strong>.</p>
<p>In <strong>Pascal's triangle</strong>, each number is the sum of the two numbers directly above it as shown:</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif" style="height:240px; width:260px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> numRows = 5
<strong>Output:</strong> [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> numRows = 1
<strong>Output:</strong> [[1]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= numRows <= 30</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> generate(int numRows) {\n std::vector<std::vector<int>> res;\n res.push_back({1});\n\n for (int i = 0; i < numRows - 1; i++) {\n std::vector<int> dummyRow = {0};\n dummyRow.insert(dummyRow.end(), res.back().begin(), res.back().end());\n dummyRow.push_back(0);\n std::vector<int> row;\n\n for (int j = 0; j < dummyRow.size() - 1; j++) {\n row.push_back(dummyRow[j] + dummyRow[j + 1]);\n }\n\n res.push_back(row);\n }\n\n return res; \n }\n};",
"memory": "8500"
} |
119 | <p>Given an integer <code>rowIndex</code>, return the <code>rowIndex<sup>th</sup></code> (<strong>0-indexed</strong>) row of the <strong>Pascal's triangle</strong>.</p>
<p>In <strong>Pascal's triangle</strong>, each number is the sum of the two numbers directly above it as shown:</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif" style="height:240px; width:260px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> rowIndex = 3
<strong>Output:</strong> [1,3,3,1]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> rowIndex = 0
<strong>Output:</strong> [1]
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> rowIndex = 1
<strong>Output:</strong> [1,1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= rowIndex <= 33</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you optimize your algorithm to use only <code>O(rowIndex)</code> extra space?</p>
| 0 | {
"code": "#include <vector>\nusing namespace std;\n\nclass Solution {\npublic:\n vector<int> getRow(int rowIndex) {\n vector<int> row(rowIndex + 1, 1); // Initialize all elements with 1\n \n // Compute the values in the row using the properties of Pascal's Triangle\n for (int i = 1; i < rowIndex; ++i) {\n for (int j = i; j > 0; --j) {\n row[j] += row[j - 1];\n }\n }\n \n return row;\n }\n};\n",
"memory": "7400"
} |
119 | <p>Given an integer <code>rowIndex</code>, return the <code>rowIndex<sup>th</sup></code> (<strong>0-indexed</strong>) row of the <strong>Pascal's triangle</strong>.</p>
<p>In <strong>Pascal's triangle</strong>, each number is the sum of the two numbers directly above it as shown:</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif" style="height:240px; width:260px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> rowIndex = 3
<strong>Output:</strong> [1,3,3,1]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> rowIndex = 0
<strong>Output:</strong> [1]
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> rowIndex = 1
<strong>Output:</strong> [1,1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= rowIndex <= 33</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you optimize your algorithm to use only <code>O(rowIndex)</code> extra space?</p>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> getRow(int rows) {\n vector<int> ans(rows+1,1);\n \n for(int i=1;i<rows;i++)\n {\n \n for(int j=i;j>0;j--)\n {\n \n ans[j]+=ans[j-1];\n \n }\n \n\n }\n return ans;\n \n }\n};",
"memory": "7400"
} |
119 | <p>Given an integer <code>rowIndex</code>, return the <code>rowIndex<sup>th</sup></code> (<strong>0-indexed</strong>) row of the <strong>Pascal's triangle</strong>.</p>
<p>In <strong>Pascal's triangle</strong>, each number is the sum of the two numbers directly above it as shown:</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif" style="height:240px; width:260px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> rowIndex = 3
<strong>Output:</strong> [1,3,3,1]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> rowIndex = 0
<strong>Output:</strong> [1]
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> rowIndex = 1
<strong>Output:</strong> [1,1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= rowIndex <= 33</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you optimize your algorithm to use only <code>O(rowIndex)</code> extra space?</p>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> getRow(int rowIndex) {\n \n long ans = 1;\n vector<int> ansRow;\n ansRow.push_back(ans);\n for(int i = 0; i<rowIndex; i++){\n ans *= (rowIndex-i);\n ans /= (i+1);\n ansRow.push_back(ans);\n }\n return ansRow;\n }\n};",
"memory": "7500"
} |
119 | <p>Given an integer <code>rowIndex</code>, return the <code>rowIndex<sup>th</sup></code> (<strong>0-indexed</strong>) row of the <strong>Pascal's triangle</strong>.</p>
<p>In <strong>Pascal's triangle</strong>, each number is the sum of the two numbers directly above it as shown:</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif" style="height:240px; width:260px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> rowIndex = 3
<strong>Output:</strong> [1,3,3,1]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> rowIndex = 0
<strong>Output:</strong> [1]
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> rowIndex = 1
<strong>Output:</strong> [1,1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= rowIndex <= 33</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you optimize your algorithm to use only <code>O(rowIndex)</code> extra space?</p>
| 0 | {
"code": "class Solution {\npublic:\nvector<int> rowgen(int row)\n{\n vector<int>rowal;\n long long sum=1;\n rowal.push_back(1);\n for(int i=1;i<=row;i++)\n {\n sum = sum * (row - i + 1) / i;\n rowal.push_back(sum); \n }\n return rowal;\n}\n vector<int> getRow(int rowIndex) {\n \n\n return rowgen(rowIndex); \n }\n};",
"memory": "7500"
} |
119 | <p>Given an integer <code>rowIndex</code>, return the <code>rowIndex<sup>th</sup></code> (<strong>0-indexed</strong>) row of the <strong>Pascal's triangle</strong>.</p>
<p>In <strong>Pascal's triangle</strong>, each number is the sum of the two numbers directly above it as shown:</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif" style="height:240px; width:260px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> rowIndex = 3
<strong>Output:</strong> [1,3,3,1]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> rowIndex = 0
<strong>Output:</strong> [1]
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> rowIndex = 1
<strong>Output:</strong> [1,1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= rowIndex <= 33</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you optimize your algorithm to use only <code>O(rowIndex)</code> extra space?</p>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> getRow(int rowIndex) {\n vector<int> ans;\n int row=rowIndex;\n long long ans1=1;\n ans.push_back(ans1);\n \n for(int col=1;col<=row;col++)\n {\n ans1*=(row-col+1);\n ans1/=col;\n ans.push_back(ans1);\n }\n return ans;\n }\n};",
"memory": "7600"
} |
119 | <p>Given an integer <code>rowIndex</code>, return the <code>rowIndex<sup>th</sup></code> (<strong>0-indexed</strong>) row of the <strong>Pascal's triangle</strong>.</p>
<p>In <strong>Pascal's triangle</strong>, each number is the sum of the two numbers directly above it as shown:</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif" style="height:240px; width:260px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> rowIndex = 3
<strong>Output:</strong> [1,3,3,1]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> rowIndex = 0
<strong>Output:</strong> [1]
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> rowIndex = 1
<strong>Output:</strong> [1,1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= rowIndex <= 33</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you optimize your algorithm to use only <code>O(rowIndex)</code> extra space?</p>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> getRow(int rowIndex) {\n int n = rowIndex+1;\n long long hello = 1;\n vector<int> ans;\n ans.push_back(hello);\n for(int i =1;i<n;i++){\n hello = hello*(n-i);\n hello = hello/i;\n ans.push_back(hello);\n }\n return ans;\n }\n};",
"memory": "7600"
} |
119 | <p>Given an integer <code>rowIndex</code>, return the <code>rowIndex<sup>th</sup></code> (<strong>0-indexed</strong>) row of the <strong>Pascal's triangle</strong>.</p>
<p>In <strong>Pascal's triangle</strong>, each number is the sum of the two numbers directly above it as shown:</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif" style="height:240px; width:260px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> rowIndex = 3
<strong>Output:</strong> [1,3,3,1]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> rowIndex = 0
<strong>Output:</strong> [1]
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> rowIndex = 1
<strong>Output:</strong> [1,1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= rowIndex <= 33</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you optimize your algorithm to use only <code>O(rowIndex)</code> extra space?</p>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> getRow(int rowIndex) {\n vector<int>a;\n vector<vector<int>>b;\n a.push_back(1);\n b.push_back(a);\n a.clear();\n int x1, x2;\n for(int i=1;i<=rowIndex;i++)\n {\n for(int j=0;j<=i;j++)\n {\n if(j - 1 >= 0) x1 = b[i-1][j-1];\n else x1 = 0;\n if(j != i) x2 = b[i-1][j];\n else x2 = 0;\n\n a.push_back(x1 + x2);\n }\n b.push_back(a);\n a.clear();\n }\n return b[b.size()-1];\n }\n};",
"memory": "7700"
} |
119 | <p>Given an integer <code>rowIndex</code>, return the <code>rowIndex<sup>th</sup></code> (<strong>0-indexed</strong>) row of the <strong>Pascal's triangle</strong>.</p>
<p>In <strong>Pascal's triangle</strong>, each number is the sum of the two numbers directly above it as shown:</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif" style="height:240px; width:260px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> rowIndex = 3
<strong>Output:</strong> [1,3,3,1]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> rowIndex = 0
<strong>Output:</strong> [1]
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> rowIndex = 1
<strong>Output:</strong> [1,1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= rowIndex <= 33</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you optimize your algorithm to use only <code>O(rowIndex)</code> extra space?</p>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> getRow(int rowIndex) {\n vector<int> result(rowIndex + 1, 0);\n result[0] = 1;\n\n for (int row = 1; row <= rowIndex; ++row) {\n result[row] = 1;\n\n for (int index = row - 1; index > 0; --index) {\n result[index] += result[index - 1];\n }\n }\n\n return result;\n }\n};",
"memory": "7700"
} |
119 | <p>Given an integer <code>rowIndex</code>, return the <code>rowIndex<sup>th</sup></code> (<strong>0-indexed</strong>) row of the <strong>Pascal's triangle</strong>.</p>
<p>In <strong>Pascal's triangle</strong>, each number is the sum of the two numbers directly above it as shown:</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif" style="height:240px; width:260px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> rowIndex = 3
<strong>Output:</strong> [1,3,3,1]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> rowIndex = 0
<strong>Output:</strong> [1]
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> rowIndex = 1
<strong>Output:</strong> [1,1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= rowIndex <= 33</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you optimize your algorithm to use only <code>O(rowIndex)</code> extra space?</p>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> getRow(int rowIndex) {\n vector<int> row(rowIndex + 1, 1); \n for (int i = 1; i < rowIndex; ++i) {\n for (int j = i; j > 0; --j) {\n row[j] = row[j] + row[j - 1];\n }\n }\n return row;\n }\n};\n",
"memory": "7800"
} |
119 | <p>Given an integer <code>rowIndex</code>, return the <code>rowIndex<sup>th</sup></code> (<strong>0-indexed</strong>) row of the <strong>Pascal's triangle</strong>.</p>
<p>In <strong>Pascal's triangle</strong>, each number is the sum of the two numbers directly above it as shown:</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif" style="height:240px; width:260px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> rowIndex = 3
<strong>Output:</strong> [1,3,3,1]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> rowIndex = 0
<strong>Output:</strong> [1]
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> rowIndex = 1
<strong>Output:</strong> [1,1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= rowIndex <= 33</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you optimize your algorithm to use only <code>O(rowIndex)</code> extra space?</p>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> getRow(int rowIndex) {\n vector<int> cur(rowIndex+1, 1), prev(rowIndex+1, 1);\n for(int i = 1; i <= rowIndex+1; i++){\n for(int j = 1; j < i-1; j++){\n cur[j] = prev[j-1]+prev[j];\n }\n prev = cur;\n }\n return cur;\n }\n};",
"memory": "7800"
} |
119 | <p>Given an integer <code>rowIndex</code>, return the <code>rowIndex<sup>th</sup></code> (<strong>0-indexed</strong>) row of the <strong>Pascal's triangle</strong>.</p>
<p>In <strong>Pascal's triangle</strong>, each number is the sum of the two numbers directly above it as shown:</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif" style="height:240px; width:260px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> rowIndex = 3
<strong>Output:</strong> [1,3,3,1]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> rowIndex = 0
<strong>Output:</strong> [1]
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> rowIndex = 1
<strong>Output:</strong> [1,1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= rowIndex <= 33</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you optimize your algorithm to use only <code>O(rowIndex)</code> extra space?</p>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> getRow(int rowIndex) {\n int n = rowIndex + 1;\n vector<int> curr(n, 0), prev(n, 0);\n prev[0] = 1;\n for(int i= 2; i<= n; i++){\n for(int j = 0; j<i; j++){\n if(prev[j] == 0 || j - 1 < 0) curr[j] = 1;\n else curr[j] = prev[j] + prev[j - 1];\n }\n prev = curr;\n }\n return prev;\n }\n};",
"memory": "7900"
} |
119 | <p>Given an integer <code>rowIndex</code>, return the <code>rowIndex<sup>th</sup></code> (<strong>0-indexed</strong>) row of the <strong>Pascal's triangle</strong>.</p>
<p>In <strong>Pascal's triangle</strong>, each number is the sum of the two numbers directly above it as shown:</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif" style="height:240px; width:260px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> rowIndex = 3
<strong>Output:</strong> [1,3,3,1]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> rowIndex = 0
<strong>Output:</strong> [1]
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> rowIndex = 1
<strong>Output:</strong> [1,1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= rowIndex <= 33</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you optimize your algorithm to use only <code>O(rowIndex)</code> extra space?</p>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> getRow(int rowIndex) {\n vector<int> result(rowIndex + 1, 0);\n result[0] = 1;\n\n for (int row = 1; row <= rowIndex; ++row) {\n result[row] = 1;\n\n for (int index = row - 1; index > 0; --index) {\n result[index] += result[index - 1];\n }\n }\n return result;\n }\n};",
"memory": "7900"
} |
119 | <p>Given an integer <code>rowIndex</code>, return the <code>rowIndex<sup>th</sup></code> (<strong>0-indexed</strong>) row of the <strong>Pascal's triangle</strong>.</p>
<p>In <strong>Pascal's triangle</strong>, each number is the sum of the two numbers directly above it as shown:</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif" style="height:240px; width:260px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> rowIndex = 3
<strong>Output:</strong> [1,3,3,1]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> rowIndex = 0
<strong>Output:</strong> [1]
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> rowIndex = 1
<strong>Output:</strong> [1,1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= rowIndex <= 33</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you optimize your algorithm to use only <code>O(rowIndex)</code> extra space?</p>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> getRow(int rowIndex) {\n vector<vector<int>> result;\n\n for(int i=1; i<=rowIndex+1; i++){\n long ans = 1;\n vector<int> temp;\n temp.push_back(1);\n for(int j=1;j<i;j++){\n ans = ans * (i - j);\n ans = ans / j;\n temp.push_back(ans);\n }\n result.push_back(temp);\n }\n vector<int> ans(1, 1);\n return rowIndex != 0 ? result[rowIndex] : ans;\n }\n};",
"memory": "8000"
} |
119 | <p>Given an integer <code>rowIndex</code>, return the <code>rowIndex<sup>th</sup></code> (<strong>0-indexed</strong>) row of the <strong>Pascal's triangle</strong>.</p>
<p>In <strong>Pascal's triangle</strong>, each number is the sum of the two numbers directly above it as shown:</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif" style="height:240px; width:260px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> rowIndex = 3
<strong>Output:</strong> [1,3,3,1]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> rowIndex = 0
<strong>Output:</strong> [1]
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> rowIndex = 1
<strong>Output:</strong> [1,1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= rowIndex <= 33</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you optimize your algorithm to use only <code>O(rowIndex)</code> extra space?</p>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> getRow(int rowIndex) {\n vector<int>a;\n vector<vector<int>>b;\n a.push_back(1);\n b.push_back(a);\n a.clear();\n for(int i=1;i<=rowIndex;i++)\n {\n for(int j=0;j<=i;j++)\n {\n int x1 = (j - 1 >= 0) ? b[i-1][j-1] : 0;\n int x2 = (j != i) ? b[i-1][j] : 0;\n\n a.push_back(x1 + x2);\n }\n b.push_back(a);\n a.clear();\n }\n return b[b.size()-1];\n }\n};",
"memory": "8000"
} |
119 | <p>Given an integer <code>rowIndex</code>, return the <code>rowIndex<sup>th</sup></code> (<strong>0-indexed</strong>) row of the <strong>Pascal's triangle</strong>.</p>
<p>In <strong>Pascal's triangle</strong>, each number is the sum of the two numbers directly above it as shown:</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif" style="height:240px; width:260px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> rowIndex = 3
<strong>Output:</strong> [1,3,3,1]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> rowIndex = 0
<strong>Output:</strong> [1]
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> rowIndex = 1
<strong>Output:</strong> [1,1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= rowIndex <= 33</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you optimize your algorithm to use only <code>O(rowIndex)</code> extra space?</p>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> getRow(int rowIndex) {\n int size = 1;\n vector<vector<int>> pascal;\n vector<int> prev;\n while (size <= rowIndex + 1) {\n if (prev.size() == 0) {\n prev.push_back(1);\n }\n else {\n vector<int> current;\n for (int id = 0; id < size; id++) {\n if (id == 0 || id == size - 1) {\n current.push_back(prev[0]);\n }\n else {\n current.push_back(prev[id - 1] + prev[id]);\n }\n }\n prev = current;\n }\n size++;\n }\n return prev;\n }\n};",
"memory": "8100"
} |
119 | <p>Given an integer <code>rowIndex</code>, return the <code>rowIndex<sup>th</sup></code> (<strong>0-indexed</strong>) row of the <strong>Pascal's triangle</strong>.</p>
<p>In <strong>Pascal's triangle</strong>, each number is the sum of the two numbers directly above it as shown:</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif" style="height:240px; width:260px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> rowIndex = 3
<strong>Output:</strong> [1,3,3,1]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> rowIndex = 0
<strong>Output:</strong> [1]
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> rowIndex = 1
<strong>Output:</strong> [1,1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= rowIndex <= 33</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you optimize your algorithm to use only <code>O(rowIndex)</code> extra space?</p>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> getRow(int rowIndex) {\n int size = 1;\n vector<vector<int>> pascal;\n vector<int> prev;\n while (size <= rowIndex + 1) {\n if (prev.size() == 0) {\n prev.push_back(1);\n }\n else {\n vector<int> current;\n for (int id = 0; id < size; id++) {\n if (id == 0 || id == size - 1) {\n current.push_back(prev[0]);\n }\n else {\n current.push_back(prev[id - 1] + prev[id]);\n }\n }\n prev = current;\n }\n size++;\n }\n return prev;\n }\n};",
"memory": "8100"
} |
119 | <p>Given an integer <code>rowIndex</code>, return the <code>rowIndex<sup>th</sup></code> (<strong>0-indexed</strong>) row of the <strong>Pascal's triangle</strong>.</p>
<p>In <strong>Pascal's triangle</strong>, each number is the sum of the two numbers directly above it as shown:</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif" style="height:240px; width:260px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> rowIndex = 3
<strong>Output:</strong> [1,3,3,1]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> rowIndex = 0
<strong>Output:</strong> [1]
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> rowIndex = 1
<strong>Output:</strong> [1,1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= rowIndex <= 33</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you optimize your algorithm to use only <code>O(rowIndex)</code> extra space?</p>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> getRow(int rowIndex) {\n vector<vector<int>> result;\n\n for(int i=1; i<=rowIndex+1; i++){\n long ans = 1;\n vector<int> temp;\n temp.push_back(1);\n for(int j=1;j<i;j++){\n ans = ans * (i - j);\n ans = ans / j;\n temp.push_back(ans);\n }\n result.push_back(temp);\n }\n vector<int> ans(1, 1);\n return rowIndex != 0 ? result[rowIndex] : ans;\n }\n};",
"memory": "8200"
} |
119 | <p>Given an integer <code>rowIndex</code>, return the <code>rowIndex<sup>th</sup></code> (<strong>0-indexed</strong>) row of the <strong>Pascal's triangle</strong>.</p>
<p>In <strong>Pascal's triangle</strong>, each number is the sum of the two numbers directly above it as shown:</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif" style="height:240px; width:260px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> rowIndex = 3
<strong>Output:</strong> [1,3,3,1]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> rowIndex = 0
<strong>Output:</strong> [1]
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> rowIndex = 1
<strong>Output:</strong> [1,1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= rowIndex <= 33</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you optimize your algorithm to use only <code>O(rowIndex)</code> extra space?</p>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> getRow(int rowIndex) {\n rowIndex++;\n std::vector<std::vector<int>> ans;\n ans.reserve(rowIndex);\n ans.push_back({1});\n\n for (int n = 2; n <= rowIndex; n++)\n {\n std::vector vec{1};\n for (int i = 0; i < ans.back().size() - 1; i++)\n {\n int sum = ans.back()[i] + ans.back()[i + 1];\n vec.push_back(sum);\n }\n vec.push_back(1);\n ans.push_back(vec);\n }\n\n return ans.back();\n }\n};",
"memory": "8200"
} |
120 | <p>Given a <code>triangle</code> array, return <em>the minimum path sum from top to bottom</em>.</p>
<p>For each step, you may move to an adjacent number of the row below. More formally, if you are on index <code>i</code> on the current row, you may move to either index <code>i</code> or index <code>i + 1</code> on the next row.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
<strong>Output:</strong> 11
<strong>Explanation:</strong> The triangle looks like:
<u>2</u>
<u>3</u> 4
6 <u>5</u> 7
4 <u>1</u> 8 3
The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above).
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> triangle = [[-10]]
<strong>Output:</strong> -10
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= triangle.length <= 200</code></li>
<li><code>triangle[0].length == 1</code></li>
<li><code>triangle[i].length == triangle[i - 1].length + 1</code></li>
<li><code>-10<sup>4</sup> <= triangle[i][j] <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do this using only <code>O(n)</code> extra space, where <code>n</code> is the total number of rows in the triangle? | 0 | {
"code": "class Solution {\npublic:\n int minimumTotal(vector<vector<int>>& triangle) {\n int n = triangle.size();\n for (int i = n - 2; i >= 0; i--) {\n int row_size = triangle[i].size();\n for (int j = 0; j < row_size; j++) {\n triangle[i][j] += min(triangle[i+1][j], triangle[i+1][j+1]);\n }\n }\n return triangle[0][0];\n }\n};",
"memory": "10800"
} |
120 | <p>Given a <code>triangle</code> array, return <em>the minimum path sum from top to bottom</em>.</p>
<p>For each step, you may move to an adjacent number of the row below. More formally, if you are on index <code>i</code> on the current row, you may move to either index <code>i</code> or index <code>i + 1</code> on the next row.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
<strong>Output:</strong> 11
<strong>Explanation:</strong> The triangle looks like:
<u>2</u>
<u>3</u> 4
6 <u>5</u> 7
4 <u>1</u> 8 3
The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above).
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> triangle = [[-10]]
<strong>Output:</strong> -10
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= triangle.length <= 200</code></li>
<li><code>triangle[0].length == 1</code></li>
<li><code>triangle[i].length == triangle[i - 1].length + 1</code></li>
<li><code>-10<sup>4</sup> <= triangle[i][j] <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do this using only <code>O(n)</code> extra space, where <code>n</code> is the total number of rows in the triangle? | 0 | {
"code": "class Solution {\npublic:\n int minimumTotal(vector<vector<int>>& triangle) {\n int n=triangle.size();\n for(int i=n-2;i>=0;i--){\n for(int j=0;j<=i;j++){\n triangle[i][j]+=min(triangle[i+1][j],triangle[i+1][j+1]);\n }\n }\n return triangle[0][0];\n }\n};",
"memory": "10900"
} |
120 | <p>Given a <code>triangle</code> array, return <em>the minimum path sum from top to bottom</em>.</p>
<p>For each step, you may move to an adjacent number of the row below. More formally, if you are on index <code>i</code> on the current row, you may move to either index <code>i</code> or index <code>i + 1</code> on the next row.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
<strong>Output:</strong> 11
<strong>Explanation:</strong> The triangle looks like:
<u>2</u>
<u>3</u> 4
6 <u>5</u> 7
4 <u>1</u> 8 3
The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above).
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> triangle = [[-10]]
<strong>Output:</strong> -10
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= triangle.length <= 200</code></li>
<li><code>triangle[0].length == 1</code></li>
<li><code>triangle[i].length == triangle[i - 1].length + 1</code></li>
<li><code>-10<sup>4</sup> <= triangle[i][j] <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do this using only <code>O(n)</code> extra space, where <code>n</code> is the total number of rows in the triangle? | 0 | {
"code": "class Solution {\npublic:\n int minimumTotal(vector<vector<int>>& triangle) {\n int n = triangle.size();\n for(int i = n-2; i >=0; i--){\n for(int j = 0; j < triangle[i].size(); j++){\n triangle[i][j] += min(triangle[i+1][j], triangle[i+1][j+1]);\n }\n }\n return triangle[0][0];\n }\n};",
"memory": "11000"
} |
120 | <p>Given a <code>triangle</code> array, return <em>the minimum path sum from top to bottom</em>.</p>
<p>For each step, you may move to an adjacent number of the row below. More formally, if you are on index <code>i</code> on the current row, you may move to either index <code>i</code> or index <code>i + 1</code> on the next row.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
<strong>Output:</strong> 11
<strong>Explanation:</strong> The triangle looks like:
<u>2</u>
<u>3</u> 4
6 <u>5</u> 7
4 <u>1</u> 8 3
The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above).
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> triangle = [[-10]]
<strong>Output:</strong> -10
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= triangle.length <= 200</code></li>
<li><code>triangle[0].length == 1</code></li>
<li><code>triangle[i].length == triangle[i - 1].length + 1</code></li>
<li><code>-10<sup>4</sup> <= triangle[i][j] <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do this using only <code>O(n)</code> extra space, where <code>n</code> is the total number of rows in the triangle? | 0 | {
"code": "class Solution {\npublic:\n int minimumTotal(vector<vector<int>>& triangle) {\n int n = triangle.size();\n int m = triangle[n - 1].size();\n\n for (int i = 1; i < n; ++i) {\n for (int j = 0; j < triangle[i].size(); ++j) {\n int x = INT_MAX;\n int y = INT_MAX;\n \n if (j < triangle[i - 1].size()) {\n y = triangle[i - 1][j];\n }\n if (j - 1 >= 0 && j - 1 < triangle[i - 1].size()) {\n x = triangle[i - 1][j - 1];\n }\n\n triangle[i][j] = triangle[i][j] + min(x, y);\n }\n }\n\n return *min_element(triangle[n - 1].begin(), triangle[n - 1].end());\n }\n};",
"memory": "11100"
} |
120 | <p>Given a <code>triangle</code> array, return <em>the minimum path sum from top to bottom</em>.</p>
<p>For each step, you may move to an adjacent number of the row below. More formally, if you are on index <code>i</code> on the current row, you may move to either index <code>i</code> or index <code>i + 1</code> on the next row.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
<strong>Output:</strong> 11
<strong>Explanation:</strong> The triangle looks like:
<u>2</u>
<u>3</u> 4
6 <u>5</u> 7
4 <u>1</u> 8 3
The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above).
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> triangle = [[-10]]
<strong>Output:</strong> -10
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= triangle.length <= 200</code></li>
<li><code>triangle[0].length == 1</code></li>
<li><code>triangle[i].length == triangle[i - 1].length + 1</code></li>
<li><code>-10<sup>4</sup> <= triangle[i][j] <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do this using only <code>O(n)</code> extra space, where <code>n</code> is the total number of rows in the triangle? | 0 | {
"code": "class Solution {\npublic:\n int minimumTotal(vector<vector<int>>& A) {\n for(int i=1;i<A.size();i++)\n {\n for(int j=0;j<A[i].size();j++)\n {\n int p=INT_MAX;\n if(j>0)\n {\n p=min(p,A[i-1][j-1]);\n }\n if(j!=A[i].size()-1)\n {\n p=min(p,A[i-1][j]);\n }\n A[i][j]+=p;\n }\n }\n int sum=INT_MAX;\n for(int j=0;j<A[A.size()-1].size();j++)\n {\n sum=min(sum,A[A.size()-1][j]);\n }\n return sum;\n }\n};",
"memory": "11100"
} |
120 | <p>Given a <code>triangle</code> array, return <em>the minimum path sum from top to bottom</em>.</p>
<p>For each step, you may move to an adjacent number of the row below. More formally, if you are on index <code>i</code> on the current row, you may move to either index <code>i</code> or index <code>i + 1</code> on the next row.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
<strong>Output:</strong> 11
<strong>Explanation:</strong> The triangle looks like:
<u>2</u>
<u>3</u> 4
6 <u>5</u> 7
4 <u>1</u> 8 3
The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above).
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> triangle = [[-10]]
<strong>Output:</strong> -10
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= triangle.length <= 200</code></li>
<li><code>triangle[0].length == 1</code></li>
<li><code>triangle[i].length == triangle[i - 1].length + 1</code></li>
<li><code>-10<sup>4</sup> <= triangle[i][j] <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do this using only <code>O(n)</code> extra space, where <code>n</code> is the total number of rows in the triangle? | 0 | {
"code": "class Solution {\npublic:\n int minimumTotal(vector<vector<int>>& triangle) {\n int n=triangle.size();\n for(int i=n-2;i>=0;i--){\n for(int j=0;j<triangle[i].size();j++){\n int minSum=min(triangle[i+1][j],triangle[i+1][j+1]);\n int currentVal=triangle[i][j];\n triangle[i][j]=currentVal+minSum;\n }\n }\n return triangle[0][0];\n }\n};",
"memory": "11200"
} |
120 | <p>Given a <code>triangle</code> array, return <em>the minimum path sum from top to bottom</em>.</p>
<p>For each step, you may move to an adjacent number of the row below. More formally, if you are on index <code>i</code> on the current row, you may move to either index <code>i</code> or index <code>i + 1</code> on the next row.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
<strong>Output:</strong> 11
<strong>Explanation:</strong> The triangle looks like:
<u>2</u>
<u>3</u> 4
6 <u>5</u> 7
4 <u>1</u> 8 3
The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above).
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> triangle = [[-10]]
<strong>Output:</strong> -10
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= triangle.length <= 200</code></li>
<li><code>triangle[0].length == 1</code></li>
<li><code>triangle[i].length == triangle[i - 1].length + 1</code></li>
<li><code>-10<sup>4</sup> <= triangle[i][j] <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do this using only <code>O(n)</code> extra space, where <code>n</code> is the total number of rows in the triangle? | 0 | {
"code": "#include <vector>\n#include <algorithm>\nusing namespace std;\n\nclass Solution {\npublic:\n int minimumTotal(vector<vector<int>>& triangle) {\n int n = triangle.size();\n // Start from the second last row and move upwards\n for (int i = n - 2; i >= 0; --i) {\n for (int j = 0; j <= i; ++j) {\n // Use long long for calculation to prevent overflow\n triangle[i][j] += min((long long)triangle[i + 1][j], (long long)triangle[i + 1][j + 1]);\n }\n }\n // The minimum path sum is now at the top of the triangle\n return triangle[0][0];\n }\n};\n",
"memory": "11200"
} |
120 | <p>Given a <code>triangle</code> array, return <em>the minimum path sum from top to bottom</em>.</p>
<p>For each step, you may move to an adjacent number of the row below. More formally, if you are on index <code>i</code> on the current row, you may move to either index <code>i</code> or index <code>i + 1</code> on the next row.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
<strong>Output:</strong> 11
<strong>Explanation:</strong> The triangle looks like:
<u>2</u>
<u>3</u> 4
6 <u>5</u> 7
4 <u>1</u> 8 3
The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above).
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> triangle = [[-10]]
<strong>Output:</strong> -10
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= triangle.length <= 200</code></li>
<li><code>triangle[0].length == 1</code></li>
<li><code>triangle[i].length == triangle[i - 1].length + 1</code></li>
<li><code>-10<sup>4</sup> <= triangle[i][j] <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do this using only <code>O(n)</code> extra space, where <code>n</code> is the total number of rows in the triangle? | 0 | {
"code": "class Solution {\npublic:\n int minimumTotal(std::vector<std::vector<int>>& triangle) \n {\n std::vector<int> sums(triangle[triangle.size() - 1].size(), 0);\n for (int i = 0; i < triangle.size(); ++i)\n {\n for (int j = triangle[i].size()-1; j >=0 ; --j)\n {\n if (j == 0)\n sums[j] += triangle[i][j];\n else if (j == triangle[i].size() - 1)\n sums[j] = sums[j - 1] + triangle[i ][j];\n else\n sums[j] =triangle[i][j] + std::min(sums[j - 1], sums[j]);\n }\n }\n return *std::min_element(sums.begin(), sums.end());\n }\n};",
"memory": "11300"
} |
120 | <p>Given a <code>triangle</code> array, return <em>the minimum path sum from top to bottom</em>.</p>
<p>For each step, you may move to an adjacent number of the row below. More formally, if you are on index <code>i</code> on the current row, you may move to either index <code>i</code> or index <code>i + 1</code> on the next row.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
<strong>Output:</strong> 11
<strong>Explanation:</strong> The triangle looks like:
<u>2</u>
<u>3</u> 4
6 <u>5</u> 7
4 <u>1</u> 8 3
The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above).
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> triangle = [[-10]]
<strong>Output:</strong> -10
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= triangle.length <= 200</code></li>
<li><code>triangle[0].length == 1</code></li>
<li><code>triangle[i].length == triangle[i - 1].length + 1</code></li>
<li><code>-10<sup>4</sup> <= triangle[i][j] <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do this using only <code>O(n)</code> extra space, where <code>n</code> is the total number of rows in the triangle? | 0 | {
"code": "class Solution {\npublic:\n int minimumTotal(vector<vector<int>>& triangle) {\n int n = triangle.size();\n vector<int> nums = triangle[n - 1];\n for (int i = n - 2; i >= 0; i--) { \n for (int j = 0; j <= i; j++) {\n nums[j] = triangle[i][j] + min(nums[j], nums[j + 1]);\n }\n }\n return nums[0];\n }\n};\n",
"memory": "11300"
} |
120 | <p>Given a <code>triangle</code> array, return <em>the minimum path sum from top to bottom</em>.</p>
<p>For each step, you may move to an adjacent number of the row below. More formally, if you are on index <code>i</code> on the current row, you may move to either index <code>i</code> or index <code>i + 1</code> on the next row.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
<strong>Output:</strong> 11
<strong>Explanation:</strong> The triangle looks like:
<u>2</u>
<u>3</u> 4
6 <u>5</u> 7
4 <u>1</u> 8 3
The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above).
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> triangle = [[-10]]
<strong>Output:</strong> -10
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= triangle.length <= 200</code></li>
<li><code>triangle[0].length == 1</code></li>
<li><code>triangle[i].length == triangle[i - 1].length + 1</code></li>
<li><code>-10<sup>4</sup> <= triangle[i][j] <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do this using only <code>O(n)</code> extra space, where <code>n</code> is the total number of rows in the triangle? | 0 | {
"code": "class Solution {\npublic:\n int n;\n int dp[201][201];\n int solve(int i,int j,vector<vector<int>>& triangle){\n if(i==n-1)\n return triangle[n-1][j];\n\n int down=triangle[i][j]+solve(i+1,j,triangle);\n int diag=triangle[i][j]+solve(i+1,j+1,triangle);\n return min(down,diag);\n\n\n }\n int minimumTotal(vector<vector<int>>& triangle) {\n n=triangle.size();\n // return solve(0,0,triangle); \n memset(dp,0,sizeof(dp));\n\n for(int j=0;j<n;j++)\n dp[n-1][j]=triangle[n-1][j];\n\n for(int i=n-2;i>=0;i--){\n for(int j=i;j>=0;j--){ //for each row usske col pe jaayega\n\n \n \n int down=triangle[i][j]+dp[i+1][j]; \n int diag=triangle[i][j]+dp[i+1][j+1];\n \n dp[i][j]=min(down,diag);\n }\n }\n return dp[0][0];\n \n }\n};",
"memory": "11400"
} |
120 | <p>Given a <code>triangle</code> array, return <em>the minimum path sum from top to bottom</em>.</p>
<p>For each step, you may move to an adjacent number of the row below. More formally, if you are on index <code>i</code> on the current row, you may move to either index <code>i</code> or index <code>i + 1</code> on the next row.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
<strong>Output:</strong> 11
<strong>Explanation:</strong> The triangle looks like:
<u>2</u>
<u>3</u> 4
6 <u>5</u> 7
4 <u>1</u> 8 3
The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above).
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> triangle = [[-10]]
<strong>Output:</strong> -10
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= triangle.length <= 200</code></li>
<li><code>triangle[0].length == 1</code></li>
<li><code>triangle[i].length == triangle[i - 1].length + 1</code></li>
<li><code>-10<sup>4</sup> <= triangle[i][j] <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do this using only <code>O(n)</code> extra space, where <code>n</code> is the total number of rows in the triangle? | 0 | {
"code": "class Solution {\nprivate:\n int func(vector<vector<int>>& triangle){\n int n=triangle.size();\n\n\n vector<int> dp(n);\n for (int i=0;i<n;i++) dp[i]=triangle[n-1][i];\n\n \n for (int i=n-2;i>=0;i--){\n for (int j=0;j<=i;j++){\n int first=dp[j];\n int sec=dp[j+1];\n dp[j]=triangle[i][j]+min(first,sec);\n }\n }\n return dp[0];\n }\npublic:\n int minimumTotal(vector<vector<int>>& triangle) {\n return func(triangle);\n \n }\n};",
"memory": "11400"
} |
120 | <p>Given a <code>triangle</code> array, return <em>the minimum path sum from top to bottom</em>.</p>
<p>For each step, you may move to an adjacent number of the row below. More formally, if you are on index <code>i</code> on the current row, you may move to either index <code>i</code> or index <code>i + 1</code> on the next row.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
<strong>Output:</strong> 11
<strong>Explanation:</strong> The triangle looks like:
<u>2</u>
<u>3</u> 4
6 <u>5</u> 7
4 <u>1</u> 8 3
The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above).
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> triangle = [[-10]]
<strong>Output:</strong> -10
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= triangle.length <= 200</code></li>
<li><code>triangle[0].length == 1</code></li>
<li><code>triangle[i].length == triangle[i - 1].length + 1</code></li>
<li><code>-10<sup>4</sup> <= triangle[i][j] <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do this using only <code>O(n)</code> extra space, where <code>n</code> is the total number of rows in the triangle? | 0 | {
"code": "class Solution {\npublic:\n /*\n int solve( vector<vector<int>>& triangle, int h, int p,\n vector<vector<int>>& dp){ if(h==triangle.size()) return 0;\n\n if(dp[h][p]!=-1) return dp[h][p];\n\n int a = triangle[h][p] + solve(triangle, h+1, p, dp);\n int b = triangle[h][p+1] + solve(triangle, h+1, p+1, dp);\n return dp[h][p] = min(a,b);\n }\n\n\n int minimumTotal(vector<vector<int>>& triangle) {\n if(triangle.size()==0) return 0;\n\n int h=triangle.size();\n int p=triangle[h-1].size();\n\n vector<vector<int>> dp(h+1, vector<int>(p+1,-1));\n return triangle[0][0] + solve( triangle , 1 , 0, dp);\n }\n */\n\n int minimumTotal(vector<vector<int>>& triangle) {\n if (triangle.size() == 0)\n return 0;\n\n int hh = triangle.size();\n int pp = triangle[hh - 1].size();\n\n vector<vector<int>> dp(hh, vector<int>(pp, 0));\n for (int p = 0; p < triangle[hh - 1].size(); p++) {\n dp[hh - 1][p] = triangle[hh - 1][p];\n }\n for (int h = hh - 2; h >= 0; h--) {\n for (int p = 0; p < triangle[h].size(); p++) {\n int a = triangle[h][p] + dp[h + 1][p];\n int b = triangle[h][p] + dp[h + 1][p + 1];\n dp[h][p] = min(a, b);\n }\n }\n return dp[0][0];\n }\n};",
"memory": "11500"
} |
120 | <p>Given a <code>triangle</code> array, return <em>the minimum path sum from top to bottom</em>.</p>
<p>For each step, you may move to an adjacent number of the row below. More formally, if you are on index <code>i</code> on the current row, you may move to either index <code>i</code> or index <code>i + 1</code> on the next row.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
<strong>Output:</strong> 11
<strong>Explanation:</strong> The triangle looks like:
<u>2</u>
<u>3</u> 4
6 <u>5</u> 7
4 <u>1</u> 8 3
The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above).
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> triangle = [[-10]]
<strong>Output:</strong> -10
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= triangle.length <= 200</code></li>
<li><code>triangle[0].length == 1</code></li>
<li><code>triangle[i].length == triangle[i - 1].length + 1</code></li>
<li><code>-10<sup>4</sup> <= triangle[i][j] <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do this using only <code>O(n)</code> extra space, where <code>n</code> is the total number of rows in the triangle? | 0 | {
"code": "class Solution {\npublic:\n int minimumTotal(vector<vector<int>>& triangle) {\n int row = triangle.size();\n int index = triangle[row - 1].size();\n\n vector<int>dp(index,INT_MAX);\n\n for(int i = 0;i < index;i++){\n dp[i] = triangle[row-1][i];\n }\n\n for(int i = row - 2;i >= 0;i--){\n vector<int>t(triangle[i].size());\n for(int j = 0;j < triangle[i].size();j++){\n int same = dp[j];\n int diff = dp[j + 1];\n\n t[j] = min(same,diff) + triangle[i][j];\n }\n dp = t;\n }\n return dp[0];\n }\n};",
"memory": "11500"
} |
120 | <p>Given a <code>triangle</code> array, return <em>the minimum path sum from top to bottom</em>.</p>
<p>For each step, you may move to an adjacent number of the row below. More formally, if you are on index <code>i</code> on the current row, you may move to either index <code>i</code> or index <code>i + 1</code> on the next row.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
<strong>Output:</strong> 11
<strong>Explanation:</strong> The triangle looks like:
<u>2</u>
<u>3</u> 4
6 <u>5</u> 7
4 <u>1</u> 8 3
The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above).
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> triangle = [[-10]]
<strong>Output:</strong> -10
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= triangle.length <= 200</code></li>
<li><code>triangle[0].length == 1</code></li>
<li><code>triangle[i].length == triangle[i - 1].length + 1</code></li>
<li><code>-10<sup>4</sup> <= triangle[i][j] <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do this using only <code>O(n)</code> extra space, where <code>n</code> is the total number of rows in the triangle? | 1 | {
"code": "class Solution {\npublic:\n int f(int i,int j,int m,vector<vector<int>>& triangle,vector<vector<int>>&dp){\n if(i==m-1){\n return triangle[i][j];\n }\n if(dp[i][j]!=-1) return dp[i][j];\n int down=triangle[i][j]+f(i+1,j,m,triangle,dp);\n int diag=triangle[i][j]+f(i+1,j+1,m,triangle,dp);\n return dp[i][j]= min(down,diag);\n }\n int minimumTotal(vector<vector<int>>& triangle) {\n int m=triangle.size();\n vector<vector<int>> dp(m,vector<int>(m,-1));\n return f(0,0,m,triangle,dp);\n }\n};",
"memory": "11600"
} |
120 | <p>Given a <code>triangle</code> array, return <em>the minimum path sum from top to bottom</em>.</p>
<p>For each step, you may move to an adjacent number of the row below. More formally, if you are on index <code>i</code> on the current row, you may move to either index <code>i</code> or index <code>i + 1</code> on the next row.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
<strong>Output:</strong> 11
<strong>Explanation:</strong> The triangle looks like:
<u>2</u>
<u>3</u> 4
6 <u>5</u> 7
4 <u>1</u> 8 3
The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above).
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> triangle = [[-10]]
<strong>Output:</strong> -10
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= triangle.length <= 200</code></li>
<li><code>triangle[0].length == 1</code></li>
<li><code>triangle[i].length == triangle[i - 1].length + 1</code></li>
<li><code>-10<sup>4</sup> <= triangle[i][j] <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do this using only <code>O(n)</code> extra space, where <code>n</code> is the total number of rows in the triangle? | 1 | {
"code": "class Solution {\npublic:\n int solve(vector<vector<int>>& triangle, int i, int j, vector<vector<int>> &dp){\n int n = triangle.size();\n if(i == n-1){\n return triangle[n-1][j];\n }\n\n if(dp[i][j] != -1){\n return dp[i][j];\n }\n\n int down = triangle[i][j] + solve(triangle, i+1, j, dp );\n int dgDown = triangle[i][j] + solve(triangle, i+1, j+1, dp);\n\n return dp[i][j] = min(down, dgDown);\n }\n\n int minimumTotal(vector<vector<int>>& triangle) {\n int n = triangle.size();\n vector<vector<int>> dp(n, vector<int> (n+1, -1) );\n int ans = solve(triangle, 0, 0, dp);\n return ans;\n }\n};",
"memory": "11600"
} |
120 | <p>Given a <code>triangle</code> array, return <em>the minimum path sum from top to bottom</em>.</p>
<p>For each step, you may move to an adjacent number of the row below. More formally, if you are on index <code>i</code> on the current row, you may move to either index <code>i</code> or index <code>i + 1</code> on the next row.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
<strong>Output:</strong> 11
<strong>Explanation:</strong> The triangle looks like:
<u>2</u>
<u>3</u> 4
6 <u>5</u> 7
4 <u>1</u> 8 3
The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above).
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> triangle = [[-10]]
<strong>Output:</strong> -10
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= triangle.length <= 200</code></li>
<li><code>triangle[0].length == 1</code></li>
<li><code>triangle[i].length == triangle[i - 1].length + 1</code></li>
<li><code>-10<sup>4</sup> <= triangle[i][j] <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do this using only <code>O(n)</code> extra space, where <code>n</code> is the total number of rows in the triangle? | 1 | {
"code": "class Solution {\npublic:\n int minimumTotal(vector<vector<int>>& triangle) {\n /*int m = triangle.size();\n if(m<1)return 0;\n int ans = 0;\n for(int i=0; i<m; i++)\n {\n int n = triangle[i].size();\n int mini = INT_MAX;\n for(int j=0; j<n; j++)\n {\n if(triangle[i][j]<mini)\n {\n mini = triangle[i][j];\n }\n }\n ans += mini;\n }\n return ans;*/\n int n = triangle.size();\n vector<vector<int>> dp(n+1, vector<int>(n+1,0));\n for(int i=0; i<n; i++)\n {\n dp[n-1][i] = triangle[n-1][i];\n }\n for(int i=n-1; i>=0; i--)\n {\n for(int j=i; j>=0; j--)\n {\n dp[i][j] = triangle[i][j] + min(dp[i+1][j], dp[i+1][j+1]);\n }\n }\n return dp[0][0];\n }\n};",
"memory": "11700"
} |
120 | <p>Given a <code>triangle</code> array, return <em>the minimum path sum from top to bottom</em>.</p>
<p>For each step, you may move to an adjacent number of the row below. More formally, if you are on index <code>i</code> on the current row, you may move to either index <code>i</code> or index <code>i + 1</code> on the next row.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
<strong>Output:</strong> 11
<strong>Explanation:</strong> The triangle looks like:
<u>2</u>
<u>3</u> 4
6 <u>5</u> 7
4 <u>1</u> 8 3
The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above).
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> triangle = [[-10]]
<strong>Output:</strong> -10
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= triangle.length <= 200</code></li>
<li><code>triangle[0].length == 1</code></li>
<li><code>triangle[i].length == triangle[i - 1].length + 1</code></li>
<li><code>-10<sup>4</sup> <= triangle[i][j] <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do this using only <code>O(n)</code> extra space, where <code>n</code> is the total number of rows in the triangle? | 1 | {
"code": "class Solution {\npublic:\n int helper(int i, int j, vector<vector<int>>& triangle,int m, vector<vector<int>>& dp)\n {\n if(i==m-1) return triangle[m-1][j];\n if(dp[i][j]!=-1) return dp[i][j];\n int down = triangle[i][j] + helper(i+1,j,triangle,m,dp);\n int diag = triangle[i][j] + helper(i+1,j+1,triangle,m,dp);\n dp[i][j] = min(down,diag);\n return dp[i][j];\n }\n int minimumTotal(vector<vector<int>>& triangle) \n {\n int m = triangle.size();\n vector<vector<int>> dp(m,vector<int>(m,-1));\n return helper(0,0,triangle,m,dp);\n }\n};",
"memory": "11700"
} |
120 | <p>Given a <code>triangle</code> array, return <em>the minimum path sum from top to bottom</em>.</p>
<p>For each step, you may move to an adjacent number of the row below. More formally, if you are on index <code>i</code> on the current row, you may move to either index <code>i</code> or index <code>i + 1</code> on the next row.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
<strong>Output:</strong> 11
<strong>Explanation:</strong> The triangle looks like:
<u>2</u>
<u>3</u> 4
6 <u>5</u> 7
4 <u>1</u> 8 3
The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above).
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> triangle = [[-10]]
<strong>Output:</strong> -10
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= triangle.length <= 200</code></li>
<li><code>triangle[0].length == 1</code></li>
<li><code>triangle[i].length == triangle[i - 1].length + 1</code></li>
<li><code>-10<sup>4</sup> <= triangle[i][j] <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do this using only <code>O(n)</code> extra space, where <code>n</code> is the total number of rows in the triangle? | 2 | {
"code": "class Solution {\npublic:\n int f(int i,int j,vector<vector<int>>& tr,vector<vector<int>>& dp)\n {\n if(i>=tr.size()||j>i||j<0)\n return INT_MAX;\n if(i==tr.size()-1)\n return tr[i][j];\n if(dp[i][j]!=-1)\n return dp[i][j];\n int d=f(i+1,j,tr,dp);\n int r=f(i+1,j+1,tr,dp);\n return dp[i][j]=tr[i][j]+min(r,d);\n }\n int minimumTotal(vector<vector<int>>& tr) {\n int n=tr.size();\n int m=tr[tr.size()-1].size();\n vector<vector<int>> dp(m,vector<int>(m,-1));\n return f(0,0,tr,dp);\n }\n};",
"memory": "11800"
} |
120 | <p>Given a <code>triangle</code> array, return <em>the minimum path sum from top to bottom</em>.</p>
<p>For each step, you may move to an adjacent number of the row below. More formally, if you are on index <code>i</code> on the current row, you may move to either index <code>i</code> or index <code>i + 1</code> on the next row.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
<strong>Output:</strong> 11
<strong>Explanation:</strong> The triangle looks like:
<u>2</u>
<u>3</u> 4
6 <u>5</u> 7
4 <u>1</u> 8 3
The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above).
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> triangle = [[-10]]
<strong>Output:</strong> -10
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= triangle.length <= 200</code></li>
<li><code>triangle[0].length == 1</code></li>
<li><code>triangle[i].length == triangle[i - 1].length + 1</code></li>
<li><code>-10<sup>4</sup> <= triangle[i][j] <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do this using only <code>O(n)</code> extra space, where <code>n</code> is the total number of rows in the triangle? | 2 | {
"code": "class Solution {\npublic:\n \n // vector<vector<int>> dp;\n // Solution() : dp(201,vector<int>(201,-1)) {}\n // int solve(vector<vector<int>>& triangle,int i,int j){\n // if(j==triangle.size()-1) return triangle[j][i];\n // if(dp[j][i]!=-1){\n // return dp[j][i];\n // }\n\n // int down=solve(triangle,i,j+1);\n // int dia=solve(triangle,i+1,j+1);\n\n // return dp[j][i]=min(down,dia)+triangle[j][i];\n\n // }\n\n int minimumTotal(vector<vector<int>>& triangle) {\n\n vector<vector<int>> dp(triangle.size()+1,vector<int> (triangle.size()+1,0));\n \n for(int i=0;i<triangle.size();i++){\n dp[triangle.size()-1][i]=triangle[triangle.size()-1][i];\n }\n\n for(int i=triangle.size()-2;i>=0;i--){\n for(int j=0;j<=i;j++){\n int down=dp[i+1][j];\n int dia=dp[i+1][j+1];\n\n dp[i][j]=min(down,dia)+triangle[i][j];\n\n }\n }\n \n return dp[0][0];\n \n }\n};",
"memory": "11800"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 0 | {
"code": "class Solution {\n // copied solution\n // https://www.hackerearth.com/practice/notes/binary-indexed-tree-or-fenwick-tree/\nprivate:\n int c[100001];\n // update the number of equal or less than x\n void update(int x) {\n while (x < 100001) {\n c[x]++;\n x += x & -x;\n }\n }\n // get the number of equal or less than x\n int get(int x) {\n int res = 0;\n while (x > 0) {\n res += c[x];\n x -= x & -x;\n }\n return res;\n }\npublic:\n int createSortedArray(vector<int>& instructions) {\n memset(c, 0, sizeof(c));\n int res = 0, n = instructions.size(), mod = 1e9 + 7;\n for (int i = 0; i < n; ++i) {\n res = (res + min(get(instructions[i] - 1), i - get(instructions[i]))) % mod;\n update(instructions[i]);\n // look up the algorithm\n #if 0\n cout << \" i:\" << i << \" num:\" << instructions[i] << \" c:\";\n for (int k = 0; k < 40; k++)\n cout << c[k] << \",\";\n cout << endl;\n for (int k = 0; k < 8; k++)\n cout << \" get(\" << k << \") \" << get(k);\n cout << endl;\n #endif\n }\n return res;\n }\n};",
"memory": "118956"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 0 | {
"code": "class Solution {\npublic:\n struct fwtree {\n int arr[100001];\n void clear() {\n memset(arr, 0, sizeof(arr));\n }\n void insert(int x) {\n if(x == 0) arr[0]++;\n while(x <= 100000) {\n arr[x]++;\n x += x & -x;\n }\n }\n int get(int x) {\n if(x < 0) return 0;\n if(x == 0) return arr[0];\n int val = 0;\n while(x) {\n val += arr[x];\n x -= x & -x;\n }\n return val;\n }\n };\n int createSortedArray(vector<int>& instructions) {\n static fwtree lt;\n lt.clear();\n size_t ans = 0;\n for(int i = 0; i < instructions.size(); i++) {\n int x = instructions[i];\n ans = (ans + min(lt.get(x - 1), i - lt.get(x))) % 1000000007;\n lt.insert(x);\n }\n return ans;\n }\n};",
"memory": "118956"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 1 | {
"code": "#define ll long long\nclass Solution {\npublic:\n ll mod = 1e9 + 7;\n vector<ll> left;\n int createSortedArray(vector<int>& instructions) {\n int n = instructions.size();\n int maxele = *max_element(instructions.begin(), instructions.end());\n left.resize(maxele + 1, 0l);\n int count = 0;\n for(int num: instructions){\n ll smallerLeft = prefixSum(num - 1);\n ll greaterLeft = prefixSum(maxele) - prefixSum(num);\n count = (count % mod + (min(smallerLeft, greaterLeft)) % mod) % mod;\n update(num, 1, maxele);\n }\n return count;\n }\n ll prefixSum(int ind){\n ll sum = 0l;\n while(ind > 0){\n sum = (sum % mod + left[ind] % mod) % mod;\n ind -= (ind & -ind);\n }\n return sum;\n }\n void update(int ind, int val, int maxele){\n while(ind <= maxele){\n left[ind] = (left[ind] % mod + val % mod) % mod;\n ind += (ind & -ind);\n }\n }\n};",
"memory": "123268"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 1 | {
"code": "class Solution {\npublic:\n using ll = long long;\n \n struct FT {\n vector<ll> s;\n FT(int n) : s(n) {}\n void update(int pos, ll dif) { // a[pos] += dif\n for (; pos < ssize(s); pos |= pos + 1) s[pos] += dif;\n }\n ll query(int pos) { // sum of values in [0, pos)\n ll res = 0;\n for (; pos > 0; pos &= pos - 1) res += s[pos-1];\n return res;\n }\n };\n\n int createSortedArray(vector<int>& instructions) {\n constexpr ll MOD = 1e9 + 7;\n ll cost = 0;\n\n ll N = *max_element(begin(instructions), end(instructions)) + 1;\n FT ft(N);\n\n for (int x : instructions) {\n ft.update(x, 1);\n cost += min(ft.query(x), ft.query(N) - ft.query(x + 1));\n cost %= MOD;\n }\n\n return cost;\n }\n};",
"memory": "123268"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 1 | {
"code": "class Solution {\npublic:\n const int mod=1e9+7;\n vector<int>segtree;\n void update(int node,int start,int end,int pos,int val){\n if(pos>end||pos<start)return;\n if(start==end && start==pos){\n segtree[node]+=val;\n return;\n }\n int mid=(start+end)/2;\n update(2*node,start,mid,pos,val);\n update(2*node+1,mid+1,end,pos,val);\n segtree[node]=segtree[2*node]+segtree[2*node+1];\n }\n int query(int node,int start,int end,int l,int r){\n if(start>r||end<l)return 0;\n if(start==l&&end==r)return segtree[node];\n else if(start>=l&&end<=r)return segtree[node];\n else{\n int mid=(start+end)/2;\n return query(2*node,start,mid,l,r)+\n query(2*node+1,mid+1,end,l,r);\n }\n }\n int createSortedArray(vector<int>&nums){\n int n = nums.size();\n int maxi = *max_element(nums.begin(),nums.end());\n vector<pair<int,int>>v;\n segtree.resize(4*maxi,0);\n int cost=0;\n for(int i=0;i<nums.size();i++){\n int left = query(1,0,maxi,0,nums[i]-1);\n int right= query(1,0,maxi,nums[i]+1,maxi);\n cost = (cost + min(left,right))%mod;\n update(1,0,maxi,nums[i],1);\n }\n return cost;\n }\n};",
"memory": "127581"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 1 | {
"code": "class SGT{\npublic:\n vector<int> sgt;\n int n;\n SGT(int n):n(n){\n sgt.resize(4*n, 0);\n }\n\n void build(int idx, int root, int left, int right){\n if(left == right){\n sgt[root]++;\n return;\n }\n int mid = left + ((right - left) >> 1);\n if(idx <= mid){\n build(idx, 2*root + 1, left, mid);\n } else {\n build(idx, 2*root + 2, mid + 1, right);\n }\n sgt[root] = sgt[2*root + 1] + sgt[2*root + 2];\n }\n\n int query(int start, int end, int root, int left, int right){\n if(left > end || right < start){\n return 0;\n }\n if(start <= left && right <= end){\n return sgt[root];\n }\n int mid = left + ((right - left) >> 1);\n return query(start, end, 2*root + 1, left, mid) + query(start, end, 2*root + 2, mid + 1, right);\n }\n};\n\nclass Solution {\npublic:\n int createSortedArray(vector<int>& inst) {\n int mx = *max_element(begin(inst), end(inst));\n SGT sgt(mx + 1);\n long long ans = 0; // Use long long to prevent overflow\n int n = inst.size();\n int mod = 1e9 + 7;\n for(int i = 0; i < n; i++){\n int left_count = sgt.query(0, inst[i] - 1, 0, 0, mx);\n int right_count = i - sgt.query(0, inst[i], 0, 0, mx);\n ans = (ans + min(left_count, right_count)) % mod;\n sgt.build(inst[i], 0, 0, mx);\n }\n return (int)ans;\n }\n};\n",
"memory": "127581"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 1 | {
"code": "class Solution {\npublic:\n struct tree {\n int l, r, val;\n } cnt[1100011];\n void Tree(int k, int l, int r) {\n cnt[k].l = l, cnt[k].r = r;\n cnt[k].val = 0;\n if (l != r) {\n int mid = (l + r) / 2;\n Tree(2 * k, l, mid);\n Tree(2 * k + 1, mid + 1, r);\n }\n }\n void update(int k, int num) {\n if (cnt[k].l == num && cnt[k].r == num) {\n cnt[k].val++;\n return;\n }\n int mid = (cnt[k].l + cnt[k].r) / 2;\n if (num <= mid) { update(2 * k, num); }\n if (num > mid) { update(2 * k + 1, num); }\n cnt[k].val = cnt[2 * k].val + cnt[2 * k + 1].val;\n }\n int query(int k, int l, int r) {\n if (l <= cnt[k].l && cnt[k].r <= r) {\n return cnt[k].val;\n }\n int ans = 0;\n int mid = (cnt[k].l + cnt[k].r) / 2;\n if (l <= mid) { ans += query(2 * k, l, r); }\n if (r > mid) { ans += query(2 * k + 1, l, r); }\n return ans;\n }\n int createSortedArray(vector<int>& instructions) {\n Tree(1, 0, 100001);\n long long ans = 0;\n long long mod = 1e9 + 7;\n for (auto t : instructions) {\n int l = query(1, 0, t - 1);\n int r = query(1, t + 1, 100001);\n ans = (ans + min(l, r)) % mod;\n update(1, t);\n }\n return ans;\n }\n};",
"memory": "131893"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 1 | {
"code": "class segtree\n{\n vector<int> v;\n int n;\npublic:\n segtree(int n1)\n {\n n = n1;\n v = vector<int>(2 * n - 1, 0);\n }\n\n void update(int a, int b)\n {\n a += n - 1;\n v[a] += b;\n while(a)\n {\n a = (a - 1) / 2;\n v[a] = v[2 * a + 1] + v[2 * a + 2];\n } \n }\n\n int range(int l, int r)\n {\n l += n - 1, r += n - 1;\n int ans = 0;\n\n while(l < r)\n {\n if(l % 2 == 0) ans += v[l++];\n if(r % 2 == 1) ans += v[r--];\n l = (l - 1) / 2, r = (r - 1) / 2;\n }\n\n return l == r ? ans + v[l] : ans;\n }\n};\n\n#define ll long long\nclass Solution \n{\npublic:\n int createSortedArray(vector<int>& instructions) \n {\n vector<int> v = instructions;\n sort(begin(v), end(v));\n segtree s(v.size());\n ll cost = 0;\n int mod = 1e9 + 7;\n\n for(auto i: instructions)\n {\n int idx = lower_bound(begin(v), end(v), i) - begin(v);\n int ans = min(s.range(0, idx - 1), s.range(idx + 1, v.size() - 1));\n s.update(idx, 1);\n cost = (cost + ans) % mod;\n }\n\n return cost;\n }\n};",
"memory": "131893"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 1 | {
"code": "#include <vector>\nusing namespace std;\n\nclass FenwickTree {\npublic:\n FenwickTree(int n) : tree(n + 1, 0) {}\n\n void update(int i, int delta) {\n while (i < tree.size()) {\n tree[i] += delta;\n i += i & -i;\n }\n }\n\n int query(int i) const {\n int sum = 0;\n while (i > 0) {\n sum += tree[i];\n i -= i & -i;\n }\n return sum;\n }\n\nprivate:\n vector<int> tree;\n};\n\nclass Solution {\npublic:\n int createSortedArray(vector<int>& instructions) {\n const int MOD = 1e9 + 7;\n int maxVal = 100000;\n FenwickTree fenwickTree(maxVal);\n long long cost = 0;\n\n for (int i = 0; i < instructions.size(); ++i) {\n int num = instructions[i];\n int lessThan = fenwickTree.query(num - 1);\n int greaterThan = i - fenwickTree.query(num);\n cost = (cost + min(lessThan, greaterThan)) % MOD;\n fenwickTree.update(num, 1);\n }\n\n return cost;\n }\n};\n",
"memory": "136206"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 1 | {
"code": "class FenwickTree{\nprivate:\n int n;\n vector<int> arr;\n vector<int> farr;\n\npublic:\n FenwickTree(){\n n = 100001;\n farr.resize(100001, 0);\n }\n\n FenwickTree(vector<int>& ar){\n n = 100001;\n // arr.resize(n);\n farr.resize(100001, 0);\n\n // no need here\n // for(int i = 0; i < ar.size(); i++){\n // arr[i + 1] = ar[i];\n // update(i + 1, ar[i]);\n // }\n }\n\n int query(int x){\n int ans = 0;\n while(x > 0){\n ans += farr[x];\n x -= (x & -x);\n }\n return ans;\n }\n\n void update(int x, int val){\n while(x < n){\n farr[x] += val;\n x += (x & -x);\n }\n }\n};\n\nclass Solution {\npublic:\n int createSortedArray(vector<int>& instructions) {\n FenwickTree fn;\n\n int ans = 0;\n for(int i = 0; i < instructions.size(); i++){\n int big = i - fn.query(instructions[i]);\n int small = fn.query(instructions[i] - 1);\n\n ans = (ans + min(big, small)) % 1000000007;\n fn.update(instructions[i], 1);\n }\n\n return ans;\n }\n};",
"memory": "140518"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 1 | {
"code": "class Solution {\n int m = 1e5+2;\n vector<long long>fw;\n int mod = 1e9 + 7;\npublic:\n void update(int num, int cnt){\n while(num < m){\n fw[num] += cnt;\n num += (num & (-num));\n }\n }\n int sum(int i){\n int ans = 0;\n for(int j=i;j>0;j-=(j&(-j))) ans+= fw[j];\n return ans;\n }\n int createSortedArray(vector<int>& ins) {\n long long ans = 0;\n fw.resize(m,0);\n int n = ins.size();\n\n for(int i =0;i<n;i++){\n int cnt = 1;\n int s = i;\n while(i+1 < n && ins[i] == ins[i+1]){\n cnt++;\n i++;\n }\n ans += (cnt*min(sum(ins[i]-1), s- sum(ins[i])))%mod;\n update(ins[i],cnt);\n }\n return (int)(ans%mod);\n }\n};",
"memory": "170706"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 1 | {
"code": "class Solution {\npublic:\n int MOD = 1000000007; \n int createSortedArray(vector<int>& instructions) {\n int m = 100001; \n vector<int> tree(m * 2, 0); \n\n long cost = 0;\n for (int x : instructions) {\n cost += min(query(0, x, tree, m), query(x + 1, m, tree, m));\n update(x, 1, tree, m);\n }\n return static_cast<int>(cost % MOD); \n }\n\n void update(int index, int value, vector<int>& tree, int m) {\n index += m; \n while (index > 0) {\n tree[index] += value; \n index >>= 1; \n }\n }\n\n int query(int left, int right, vector<int>& tree, int m) {\n int result = 0;\n left += m; \n right += m; \n\n while (left < right) {\n if (left & 1) { \n result += tree[left++]; \n }\n if (right & 1) { \n result += tree[--right]; \n }\n left >>= 1; \n right >>= 1; \n }\n return result;\n }\n};\n",
"memory": "175018"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 1 | {
"code": "class Solution {\npublic:\n int createSortedArray(vector<int>& instructions) {\n int m = (int)1e5 + 1;\n vector<int> tree(m * 2);\n\n long cost = 0;\n long MOD = (int)1e9 + 7;\n for (int x : instructions) {\n cost += min(query(0, x, tree, m), query(x + 1, m, tree, m));\n update(x, 1, tree, m);\n }\n return (int)(cost % MOD);\n }\n\n // implement Segment Tree\n void update(int index, int value, vector<int>& tree, int m) {\n index += m;\n tree[index] += value;\n for (index >>= 1; index > 0; index >>= 1)\n tree[index] = tree[index << 1] + tree[(index << 1) + 1];\n }\n\n int query(int left, int right, vector<int>& tree, int m) {\n int result = 0;\n for (left += m, right += m; left < right; left >>= 1, right >>= 1) {\n if ((left & 1) == 1) result += tree[left++];\n if ((right & 1) == 1) result += tree[--right];\n }\n return result;\n }\n};",
"memory": "175018"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 2 | {
"code": "class Solution {\npublic:\n int mod = 1000000007;\n int createSortedArray(vector<int>& instructions) {\n int n = 100002;\n vector<int> bitree(n);\n unordered_map<int, int> mp;\n int ans = 0;\n for( int i=0 ; i<instructions.size() ; i++ ){\n int num = instructions[i];\n mp[num]++;\n while( num < bitree.size() ){\n bitree[num]++;\n num += num&(-num);\n }\n num = instructions[i]-1;\n int numLess = 0;\n while( num > 0 ){\n numLess += bitree[num];\n num -= num&(-num);\n }\n num = instructions[i];\n int numMore = i+1-mp[num]-numLess;\n int res = min( numLess, numMore );\n ans = (ans+res)%mod;\n\n }\n return ans;\n }\n};",
"memory": "179331"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 2 | {
"code": "#include <vector>\n#include <unordered_map>\n#include <algorithm>\n\nusing std::vector;\nusing std::unordered_map;\n\nnamespace cust\t\t\t//customized / non-standard\n{\n\tclass segment_tree\n\t{\n\t\tpublic:\n\t\t\tsegment_tree(int n) : n { n }\n\t\t\t{\n\t\t\t\timpl.assign(2 * n - 1, 0);\n\t\t\t}\n\n\t\t\tvoid update(int idx)\n\t\t\t{\n\t\t\t\tif (0 <= idx && idx < n)\n\t\t\t\t\tupdate(idx, 0, n - 1, 0);\n\t\t\t}\n\n\t\t\tint query(int left, int right) const\n\t\t\t{\n\t\t\t\treturn 0 <= left && left <= right && right < n ? query(left, right, 0, n - 1, 0) : 0;\n\t\t\t}\n\n\t\tprivate:\n\t\t\tvoid update(int idx, int idxL, int idxR, int pos)\n\t\t\t{\n\t\t\t\t++impl[pos];\n\n\t\t\t\tif (idxL < idxR)\n\t\t\t\t{\n\t\t\t\t\tauto idxM { idxL + (idxR - idxL) / 2 };\n\n\t\t\t\t\tif (idx <= idxM)\n\t\t\t\t\t\tupdate(idx, idxL, idxM, pos + 1);\n\t\t\t\t\telse\n\t\t\t\t\t\tupdate(idx, idxM + 1, idxR, pos + 2 * (idxM - idxL + 1));\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tint query(int left, int right, int idxL, int idxR, int pos) const\n\t\t\t{\n\t\t\t\tauto idxM { idxL + (idxR - idxL) / 2 };\n\n\t\t\t\tif (left == idxL && right == idxR)\n\t\t\t\t\treturn impl[pos];\n\t\t\t\telse if (right <= idxM)\n\t\t\t\t\treturn query(left, right, idxL, idxM, pos + 1);\n\t\t\t\telse if (idxM < left)\n\t\t\t\t\treturn query(left, right, idxM + 1, idxR, pos + 2 * (idxM - idxL + 1));\n\t\t\t\telse\n\t\t\t\t\treturn query(left, idxM, idxL, idxM, pos + 1) + query(idxM + 1, right, idxM + 1, idxR, pos + 2 * (idxM - idxL + 1));\n\t\t\t}\n\n\t\t\tconst int n;\n\n\t\t\tvector<int> impl {};\n\t};\n}\n\nclass Solution\n{\n\tpublic:\n\t\tint createSortedArray(vector<int>& instructions)\n\t\t{\n\t\t\tunordered_map<int, int> umap {};\n\t\t\tvector<int> sorted {};\n\t\t\tlong long total_cost { 0LL };\n\n\t\t\tfor(auto num : instructions)\n\t\t\t\tumap.emplace(num, 0);\n\n\t\t\tsorted.reserve(umap.size());\n\n\t\t\tfor(const auto& pr : umap)\n\t\t\t\tsorted.push_back(pr.first);\n\n\t\t\tstd::sort(sorted.begin(), sorted.end());\n\n\t\t\tfor(int i { 0 }; i < sorted.size(); ++i)\n\t\t\t\tumap[sorted[i]] = i;\n\n\t\t\tcust::segment_tree seg_tree(sorted.size());\n\n\t\t\tfor(auto num : instructions)\n\t\t\t{\n\t\t\t\tauto idx { umap[num] };\n\n\t\t\t\ttotal_cost += std::min(seg_tree.query(0, idx - 1), seg_tree.query(idx + 1, sorted.size() - 1));\n\n\t\t\t\tseg_tree.update(idx);\n\t\t\t}\n\n\t\t\treturn static_cast<int>(total_cost % 1000000007LL);\n\t\t}\n};\n",
"memory": "183643"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 2 | {
"code": "class Solution {\npublic:\n class BIT {\n public:\n BIT(int size): n(size), bit(n+1, 0) {}\n\n void add(int x) {\n for (int i=x; i<=n; i+=lowbit(i)) {\n bit[i]++;\n }\n }\n\n int count(int x) {\n int sum = 0;\n for (int i=x; i>0; i-=lowbit(i))\n sum += bit[i];\n return sum;\n }\n private:\n int lowbit(int x) {return x & (-x);}\n int n;\n vector<int> bit;\n };\n int MOD = 1e9 + 7;\n int createSortedArray(vector<int>& instructions) {\n set<int> s; // ordered\n for (int k : instructions)\n s.insert(k);\n int idx = 0;\n unordered_map<int,int> mp;\n for (int k : s)\n mp[k] = ++idx;\n \n BIT bit(idx);\n int n = instructions.size();\n int ret = 0;\n for (int i = 0; i < n; i++) {\n int val = mp[instructions[i]];\n assert(val <= n);\n int smaller = bit.count(val - 1);\n int same = bit.count(val) - smaller;\n int larger = i - smaller - same;\n assert(smaller >= 0);\n assert(larger >= 0);\n int cost = min(smaller, larger);\n ret = (ret + cost) % MOD;\n bit.add(val);\n }\n return ret;\n }\n};\n\n/**\n\nthe problem is closely related to finding #inversion of array\n\nif arr is sorted, cost = 0\n\nIn a ascending sorted array a = [1,2,3,4,5]\n\nif a[i] == a[j], cost = 0\nswap [2,1,3,4,5], cost= 0\nswap [3,2,1,4,5], cost = 0\nswap [4,2,3,1,5], cost = 1 \n\nswap [1,5,3,4,2], cost = 3\n-: (5,3)(5,4)(5,2)(3,2)(4,2)\n+: \nxi , xi+1, xi+2, ... xj\n\n(i-1), xj, xi+1, xi+2, ... ,xj-1, xi,\n\nfor xi+1 to xj-1 may introduce min(1, cur-2)\nfor xi cost = min(i-1, j-i-1)\n\n\n[]\n\n*/\n\n",
"memory": "187956"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 2 | {
"code": "class Solution {\npublic:\n class BIT {\n public:\n BIT(int size): n(size), bit(n+1, 0) {}\n\n void add(int x) {\n for (int i=x; i<=n; i+=lowbit(i)) {\n bit[i]++;\n }\n }\n\n int count(int x) {\n int sum = 0;\n for (int i=x; i>0; i-=lowbit(i))\n sum += bit[i];\n return sum;\n }\n private:\n int lowbit(int x) {return x & (-x);}\n int n;\n vector<int> bit;\n };\n int MOD = 1e9 + 7;\n int createSortedArray(vector<int>& instructions) {\n set<int> s; // ordered\n for (int k : instructions)\n s.insert(k);\n int idx = 0;\n unordered_map<int,int> mp;\n for (int k : s)\n mp[k] = ++idx;\n \n BIT bit(idx);\n int n = instructions.size();\n int ret = 0;\n for (int i = 0; i < n; i++) {\n int val = mp[instructions[i]];\n int smaller = bit.count(val - 1);\n int larger = i - bit.count(val);\n int cost = min(smaller, larger);\n ret = (ret + cost) % MOD;\n bit.add(val);\n }\n return ret;\n }\n};\n\n/**\n\nthe problem is closely related to finding #inversion of array\n\nif arr is sorted, cost = 0\n\nIn a ascending sorted array a = [1,2,3,4,5]\n\nif a[i] == a[j], cost = 0\nswap [2,1,3,4,5], cost= 0\nswap [3,2,1,4,5], cost = 0\nswap [4,2,3,1,5], cost = 1 \n\nswap [1,5,3,4,2], cost = 3\n-: (5,3)(5,4)(5,2)(3,2)(4,2)\n+: \nxi , xi+1, xi+2, ... xj\n\n(i-1), xj, xi+1, xi+2, ... ,xj-1, xi,\n\nfor xi+1 to xj-1 may introduce min(1, cur-2)\nfor xi cost = min(i-1, j-i-1)\n\nN = len(instructions), M = #different value\nMethod1 Segment Tree NlogM\nMethod2 BIT (adopted NlogM)\n\n*/\n\n",
"memory": "192268"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 2 | {
"code": "class Solution {\npublic:\n void update(int i, int val, vector<int>& bit){\n int ind=i;\n while (ind<bit.size()){\n bit[ind]+=val;\n ind+=(ind&(-ind));\n }\n }\n int sum(int i, vector<int>& bit){\n int ans=0;\n for (int j=i; j>0; j-=(j&(-j))){\n ans+=bit[j];\n }\n return ans;\n }\n int createSortedArray(vector<int>& a) {\n int n=a.size();\n map<int, int> mp, mp2;\n for (auto x: a)mp[x]++;\n int i=1;\n for (auto x: mp){\n mp2[x.first]=i;\n i++;\n }\n vector<int> bit(i, 0);\n // cout<<i<<endl;\n int ans=0, mod=1e9+7;\n for (int i=0; i<n; i++){\n // cout<<mp2[a[i]]<<\" \";\n update(mp2[a[i]], 1, bit);\n int cnt1=sum(mp2[a[i]]-1, bit);\n int cnt2=sum(mp2[a[i]], bit);\n // cout<<cnt1<<\" \"<<i+1-cnt2<<endl;\n ans+=min(cnt1, i+1-cnt2);\n ans%=mod;\n }\n return ans;\n }\n};",
"memory": "196581"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 2 | {
"code": "class Node{\n public:\n int start,end,value;\n Node*left,*right;\n Node(int start,int end){\n this->start = start;\n this->end = end;\n this->value = 0;\n this->left = NULL;\n this->right = NULL;\n }\n};\nNode* build(int start,int end){\n Node*node = new Node(start,end);\n if(start == end)return node;\n int mid = (start + (end - start) / 2);\n node->left = build(start,mid);\n node->right = build(mid + 1,end);\n return node;\n}\n\nvoid update(Node*node,int index){\n if(node->start == node->end){\n node->value++;\n return;\n }\n int mid = (node->start + (node->end - node->start) / 2);\n if(index <= mid){\n update(node->left,index);\n } else {\n update(node->right,index);\n }\n node->value = node->left->value + node->right->value;\n}\n\nint query(Node*node,int start,int end){\n if(start <= node->start && node->end <= end){\n return node->value;\n }\n if(node->start > end || node->end < start){\n return 0;\n }\n\n int mid = node->start + (node->end - node->start) / 2;\n if(end <= mid){\n return query(node->left,start,end);\n } else if(start > mid){\n return query(node->right,start,end);\n }\n return query(node->left,start,end) + query(node->right,start,end);\n}\n\nclass Solution {\npublic:\n int createSortedArray(vector<int>& instructions) {\n int n = instructions.size();\n int minE = 1e9;\n int maxE = -1e9;\n for(int i = 0;i < n;i++){\n minE = min(minE,instructions[i]);\n maxE = max(maxE,instructions[i]);\n }\n int ans = 0;\n int mod = 1e9 + 7;\n Node*root = build(minE,maxE);\n for(int i = 0;i < n;i++){\n int element = instructions[i];\n update(root,element);\n ans = (ans + min(query(root,minE,element - 1),query(root,element + 1,maxE)))%mod;\n }\n return ans;\n }\n};",
"memory": "200893"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 2 | {
"code": "struct Node {\n Node* lft = NULL;\n Node* rht = NULL;\n int V = 0;\n int S = 0;\n int C = 0;\n Node(int V) {\n this -> V = V;\n }\n};\nconst int mod = 1e9 + 7;\nclass Solution {\npublic:\n int createSortedArray(vector<int>& A) {\n auto build = [&] (auto &&build, int L, int R, vector <int> &V) -> Node* {\n if (L > R)\n return NULL;\n int M = (L + R) >> 1;\n Node* root = new Node(V[M]);\n root -> lft = build(build, L, M - 1, V);\n root -> rht = build(build, M + 1, R, V);\n return root;\n };\n auto add = [&] (auto &&add, Node* cur, int v) -> void {\n if (v < cur -> V) \n add(add, cur -> lft, v);\n else if (v > cur -> V) \n add(add, cur -> rht, v);\n else \n cur -> C++;\n cur -> S++;\n };\n\n set <int> st;\n for (int &a : A)\n st.insert(a);\n vector <int> V;\n for (int x : st)\n V.push_back(x);\n Node* root = build(build, 0, (int) V.size() - 1, V);\n \n int Ans = 0;\n for (int i = 0; i < (int) A.size(); ++i) {\n add(add, root, A[i]);\n Node* cur = root;\n int L = 0, R = 0;\n while (cur -> V != A[i]) {\n if (A[i] < cur -> V) {\n R += (cur -> rht ? cur -> rht -> S : 0) + cur -> C;\n cur = cur -> lft;\n }\n else if (A[i] > cur -> V) {\n L += (cur -> lft ? cur -> lft -> S : 0) + cur -> C;\n cur = cur -> rht;\n }\n }\n L += (cur -> lft ? cur -> lft -> S : 0);\n R += (cur -> rht ? cur -> rht -> S : 0);\n Ans += min(L, R);\n Ans %= mod;\n }\n return Ans;\n }\n};",
"memory": "205206"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 2 | {
"code": "class Node{\n public:\n Node* left ;\n Node* right ;\n int s ;\n int e ;\n int cnt ;\n\n Node(int ss , int ee){\n s = ss ;\n e = ee ;\n cnt = 0 ;\n left = NULL ;\n right = NULL ;\n }\n};\n\nclass Segment{\n public:\n Node* root ;\n\n Segment(){\n root = new Node(0 , 1e6);\n }\n\n void update(Node* node , int index){\n if(node == NULL ) return ; \n int s = node->s , e = node->e ;\n\n if( index < s || index > e) return ;\n\n if(s == e){\n node->cnt += 1 ;\n return ; \n }\n\n int mid = s+(e-s)/2 ;\n if(node->left == NULL){\n node->left = new Node(s , mid);\n node->right = new Node(mid+1 , e);\n }\n\n update(node->left , index) ;\n update(node->right , index);\n\n node->cnt = node->left->cnt + node->right->cnt ;\n return ;\n }\n\n int justSmaller(Node* node , int val){\n if(node == NULL ) return 0 ;\n \n int s = node->s , e = node->e ;\n\n if(s >= val ) return 0 ;\n if(e < val) return node->cnt ;\n\n if(s == e) {\n return node->cnt ;\n }\n\n int mid= s+(e-s)/2 ;\n int leftRes = justSmaller(node->left , val);\n int rightRes = justSmaller(node->right , val);\n\n return leftRes + rightRes ;\n }\n\n int justGreater(Node* node , int val){\n if(node == NULL ) return 0 ;\n \n int s = node->s , e = node->e ;\n\n if(e <= val) return 0 ;\n if(s > val) return node->cnt ;\n\n if(s == e) {\n return node->cnt ;\n }\n\n int mid= s+(e-s)/2 ;\n int leftRes = justGreater(node->left , val);\n int rightRes = justGreater(node->right , val);\n\n return leftRes + rightRes ;\n }\n\n\n};\nclass Solution {\npublic:\n int createSortedArray(vector<int>& instructions) {\n Segment seg ;\n\n long long ans = 0 ;\n int mod = 1e9+7 ;\n for(int i=0;i<instructions.size() ; i++){\n\n int smaller = seg.justSmaller(seg.root , instructions[i]);\n int greater = seg.justGreater(seg.root , instructions[i]);\n ans += min(smaller , greater);\n ans = ans%mod ;\n seg.update(seg.root , instructions[i]);\n }\n\n return ans ;\n }\n};",
"memory": "209518"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 2 | {
"code": "class SegmentTree {\n vector<int> tree;\n int maxVal;\n\npublic:\n SegmentTree(int maxVal) : maxVal(maxVal) {\n tree.resize(4 * maxVal, 0);\n }\n\n void update(int node, int start, int end, int idx, int value) {\n if (start == end) {\n // Leaf node: update the frequency\n tree[node] += value;\n } else {\n int mid = (start + end) / 2;\n if (idx <= mid) {\n update(2 * node, start, mid, idx, value);\n } else {\n update(2 * node + 1, mid + 1, end, idx, value);\n }\n // Internal node: sum of left and right children\n tree[node] = tree[2 * node] + tree[2 * node + 1];\n }\n }\n\n int query(int node, int start, int end, int l, int r) {\n if (r < start || end < l) {\n // Completely outside the range\n return 0;\n }\n if (l <= start && end <= r) {\n // Completely inside the range\n return tree[node];\n }\n // Partially inside and outside\n int mid = (start + end) / 2;\n int left_query = query(2 * node, start, mid, l, r);\n int right_query = query(2 * node + 1, mid + 1, end, l, r);\n return left_query + right_query;\n }\n\n void update(int idx, int value) {\n update(1, 0, maxVal, idx, value);\n }\n\n int query(int l, int r) {\n return query(1, 0, maxVal, l, r);\n }\n};\n\nclass Solution {\npublic:\n int createSortedArray(vector<int>& instructions) {\n int mod = 1e9 + 7;\n int maxVal = 1e5;\n SegmentTree segTree(maxVal);\n long long cost = 0;\n\n for (int i = 0; i < instructions.size(); ++i) {\n int num = instructions[i];\n int lessCount = segTree.query(0, num - 1);\n int greaterCount = segTree.query(num + 1, maxVal);\n cost += min(lessCount, greaterCount);\n cost %= mod;\n segTree.update(num, 1);\n }\n\n return cost;\n }\n};\n",
"memory": "213831"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 2 | {
"code": "\nclass segmentTree {\n\nprivate:\n\n vector<int> segTree;\n int n;\n\n int mod = 1e9 + 7;\n\npublic:\n\n segmentTree() {\n n = 100007;\n segTree.resize(4*n, 0);\n }\n\n void updateVal(int uIdx, int i, int j, int segIdx) {\n\n if (i == j && i == uIdx) {\n segTree[segIdx] = ((long long)segTree[segIdx] + 1) % mod;\n return;\n }\n\n // outside\n if (uIdx < i || uIdx > j) {\n return;\n }\n\n int mid = (i + j) / 2;\n\n updateVal(uIdx, i, mid, 2*segIdx+1);\n updateVal(uIdx, mid+1, j, 2*segIdx+2);\n\n segTree[segIdx] = (segTree[2*segIdx+1] % mod + segTree[2*segIdx+2] % mod) % mod;\n\n }\n\n int query(int l, int r, int i, int j, int segIdx) {\n\n // completly lie inside\n if (i >= l && j <= r) {\n return segTree[segIdx];\n }\n\n // completly outide\n if (r < i || l > j) {\n return 0;\n }\n\n int mid = (i + j) / 2;\n\n return query(l, r, i, mid, 2*segIdx+1) + query(l, r, mid+1, j, 2*segIdx+2); \n\n }\n\n\n};\n\n\n\nclass Solution {\n\nprivate:\n\n int mod = 1e9 + 7;\n\npublic:\n\n int createSortedArray(vector<int>& instructions) {\n \n segmentTree sTree;\n\n int cost = 0;\n\n for (auto instruction: instructions) {\n sTree.updateVal(instruction, 0, 1e5+6, 0);\n int left = sTree.query(0, instruction-1, 0, 1e5+6, 0);\n int right = sTree.query(instruction+1, 1e5+6, 0, 1e5+6, 0);\n cost = (cost % mod + min(left, right) % mod) % mod;\n }\n\n return cost;\n\n }\n};",
"memory": "218143"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 2 | {
"code": "class BIT{\n public:\n int N; \n vector<long long>bitArr; // Note: all arrays are 1-index\n vector<long long>nums;\n long long M = 1e9+7;\n\n void init(int N)\n {\n this->N = N;\n bitArr.resize(N+1);\n nums.resize(N+1); \n }\n\n // increase nums[i] by delta\n void updateDelta(int i, long long delta) {\n int idx = i;\n while (idx <= N)\n {\n bitArr[idx]+=delta;\n // bitArr[idx] %= M;\n idx+=idx&(-idx);\n }\n }\n\n // sum of a range nums[1:j] inclusively\n long long queryPreSum(int idx){\n long long result = 0;\n while (idx){\n result += bitArr[idx];\n // result %= M;\n idx-=idx&(-idx);\n }\n return result;\n }\n\n // sum of a range nums[i:j] inclusively\n long long sumRange(int i, int j) { \n return queryPreSum(j)-queryPreSum(i-1);\n } \n};\n\nclass Solution {\npublic:\n int createSortedArray(vector<int>& instructions) {\n int N = 100000;\n BIT bit;\n bit.init(N);\n\n long long res=0;\n long long mod=1e9+7;\n for(int i: instructions){\n int a = bit.sumRange(1, i-1);\n int b = bit.sumRange(i+1, N);\n res+=min(a, b);\n res%=mod;\n bit.updateDelta(i, 1);\n }\n return res;\n }\n};",
"memory": "222456"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 2 | {
"code": "class BIT{\n public:\n int N; \n vector<long long>bitArr; // Note: all arrays are 1-index\n vector<long long>nums;\n long long M = 1e9+7;\n\n void init(int N)\n {\n this->N = N;\n bitArr.resize(N+1);\n nums.resize(N+1); \n }\n\n // increase nums[i] by delta\n void updateDelta(int i, long long delta) {\n int idx = i;\n while (idx <= N)\n {\n bitArr[idx]+=delta;\n // bitArr[idx] %= M;\n idx+=idx&(-idx);\n }\n }\n\n // sum of a range nums[1:j] inclusively\n long long queryPreSum(int idx){\n long long result = 0;\n while (idx){\n result += bitArr[idx];\n // result %= M;\n idx-=idx&(-idx);\n }\n return result;\n }\n\n // sum of a range nums[i:j] inclusively\n long long sumRange(int i, int j) { \n return queryPreSum(j)-queryPreSum(i-1);\n } \n};\n\nclass Solution {\npublic:\n int createSortedArray(vector<int>& instructions) {\n int N = 100000;\n BIT bit;\n bit.init(N);\n\n long long res=0;\n long long mod=1e9+7;\n for(int i: instructions){\n int a = bit.sumRange(1, i-1);\n int b = bit.sumRange(i+1, N);\n res+=min(a, b);\n res%=mod;\n bit.updateDelta(i, 1);\n }\n return res;\n }\n};",
"memory": "226768"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 2 | {
"code": "class bst {\npublic:\n vector<vector<int>> nodes;\n int total;\n void construct(int index, int first, int last) {\n if (first == last) return;\n int mid = (first+last)/2;\n nodes[index][0] = mid;\n construct(2*index+1, first, mid);\n construct(2*index+2, mid+1, last);\n }\n bst(int maxnum) {\n int h = ceil(log2(maxnum)), maxsize = 2*pow(2, h)-1;\n maxsize = max(maxsize, maxnum);\n nodes.resize(maxsize, vector<int>(3, 0)); //[0]:val; [1]:left; [2]:equal\n construct(0, 0, maxnum);\n total = 0;\n }\n vector<long> addnode(int x, int index, long pre) {\n if (nodes[index][0] == x) {\n nodes[index][2]++;\n return {pre+nodes[index][1], nodes[index][2]};\n }\n if (nodes[index][0] > x) {\n nodes[index][1]++;\n return addnode(x, 2*index+1, pre);\n }\n else {\n return addnode(x, 2*index+2, nodes[index][1] + nodes[index][2] + pre);\n }\n }\n int insert(int x) {\n vector<long> loe = addnode(x, 0, 0);\n total++;\n return (int) min(loe[0], total-(loe[0]+loe[1]));\n } \n};\nclass Solution {\npublic:\n int createSortedArray(vector<int>& instructions) {\n const int mod = 1e9+7;\n int maxnum = *max_element(instructions.begin(), instructions.end());\n bst *tree = new bst(maxnum);\n long cost=0;\n for (auto i:instructions) {\n cost = (cost+tree->insert(i-1))%mod;\n }\n return (int)cost;\n }\n};",
"memory": "231081"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 2 | {
"code": "#define ll long long int\nclass SegmentTree{\n ll n ;\n vector < ll > nodes ;\n\n void update ( ll index , ll val , ll low , ll high , ll node){\n if ( low == high ){\n nodes [node] += val;\n return ;\n }\n ll mid = low + ( high - low ) / 2;\n if ( mid >= index ) update ( index , val , low , mid , 2 * node );\n else update ( index , val , mid + 1 , high , 2 * node + 1 );\n \n nodes [node] = nodes [2 * node] + nodes [2 * node + 1];\n }\n\n int query ( ll start , ll end , ll l , ll h , ll node ){\n if ( start > h || end < l ){\n return 0;\n }\n if ( start <= l && h <= end){\n return nodes [node];\n }\n\n ll mid = l + (h - l) / 2;\n ll left = query (start , end , l , mid , 2 * node);\n ll right = query (start , end , mid + 1 , h , 2 * node + 1);\n\n return left + right;\n }\npublic:\n SegmentTree ( ll n ){\n this -> n = n;\n nodes . resize (4 * n);\n }\n\n void update ( ll index , ll val ){\n update ( index , val , 0 , n - 1 , 1);\n }\n\n int query (ll i , bool left){\n if (left) return query ( 0 , i , 0 , n - 1 , 1) ;\n return query ( i , n - 1 , 0 , n - 1 , 1) ;\n }\n\n};\nclass Solution {\npublic:\n int MOD = int (1e9 + 7);\n int createSortedArray(vector<int>& instructions) {\n ll n = instructions.size ();\n\n set <ll> unique (instructions.begin () , instructions.end ());\n SegmentTree segment (unique.size ());\n\n map <ll , ll> element;\n for (auto x : unique) element [x] = element.size ();\n\n ll result = 0;\n for (int i = 0 ; i < instructions.size () ; i++){\n int index = element [instructions [i]];\n int left = segment.query (index - 1 , true);\n int right = segment.query (index + 1 , false);\n segment.update (index , 1); \n\n result += min (left , right) ;\n result %= MOD;\n }\n \n return result;\n }\n};",
"memory": "235393"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 2 | {
"code": "class Solution {\npublic:\n unordered_map<int, int> m;\n vector<int> tree;\n const int maxn = 1e5+3;\n void update(int id, int l, int r, int ql, int qr, int val) {\n if (qr < l || r < ql) return;\n if (ql <= l && r <= qr) {\n tree[id]++;\n return;\n }\n int mid = (l+r)/2;\n update(2*id, l, mid, ql, qr, val);\n update(2*id+1, mid+1, r, ql, qr, val);\n tree[id] = tree[2*id] + tree[2*id+1];\n }\n int query(int id, int l, int r, int ql, int qr) {\n if (qr < l || r < ql) return 0;\n if(ql <= l && r<=qr) {\n return tree[id];\n }\n int mid = (l+r)/2;\n return query(2*id, l, mid, ql, qr) + query(2*id+1, mid+1, r, ql, qr);\n }\n int createSortedArray(vector<int>& a) {\n int n = a.size();\n /* try index compression when a[i] >= 10^9*/\n vector<int> ins = a;\n sort(ins.begin(), ins.end());\n for (int i = 0; i < n; i++) {\n m[ins[i]] = i+1;\n }\n tree.resize(4*maxn, 0);\n int ans = 0;\n int mod = 1e9+7;\n for (int i =0; i <n; i++) {\n int l = query(1, 0, maxn-1, 0, m[a[i]] - 1)%mod;\n int r = query(1, 0, maxn-1, m[a[i]]+1, maxn-1)%mod;\n ans = ans%mod + min(l, r);\n update(1, 0, maxn-1, m[a[i]], m[a[i]], 1);\n }\n return ans;\n }\n};",
"memory": "239706"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 2 | {
"code": "class Solution {\npublic:\n long long cost[100005], sorted[100005], mod=1e9+7, count[100005];\n int createSortedArray(vector<int>& nums) {\n int n = nums.size();\n for(int i=0;i<n;i++){\n sorted[i] = nums[i];\n }\n\n helper(nums, 0, n-1);\n long long res=0;\n\n for(int i=0;i<n;i++){\n int smaller = cost[i];\n int larger = (i)-cost[i]-count[nums[i]];\n res += min(smaller, larger);\n res %= mod;\n count[nums[i]]++;\n }\n return res;\n }\n\n void helper(vector<int>& nums, int a, int b){\n if(a>=b){return;}\n int mid = a+(b-a)/2;\n helper(nums, a, mid);\n helper(nums, mid+1, b);\n\n for(int i=mid+1;i<=b;i++){\n auto iter = lower_bound(sorted+a, sorted+mid+1, nums[i]);\n cost[i] += iter-(sorted+a);\n }\n\n // int i = a, j = mid+1, p=a;\n\n // while(i<=mid&&j<=b){\n // if(sorted[i]<sorted[j]){\n // temp[p++] = sorted[i++];\n // }\n // else{\n // temp[p++] = sorted[j++];\n // }\n // }\n\n inplace_merge(sorted+a, sorted+mid+1, sorted+b+1);\n\n // while(i<=mid){\n // temp[p++] = sorted[i++];\n // }\n\n // while(j<=b){\n // temp[p++] = sorted[j++];\n // }\n\n // for(int i=a;i<=b;i++){\n // sorted[i] = temp[i];\n // }\n }\n};",
"memory": "244018"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 2 | {
"code": "class Solution {\npublic:\n long long cost[100005], sorted[100005], mod=1e9+7, count[100005];\n int createSortedArray(vector<int>& nums) {\n int n = nums.size();\n for(int i=0;i<n;i++){\n sorted[i] = nums[i];\n }\n\n helper(nums, 0, n-1);\n long long res=0;\n\n for(int i=0;i<n;i++){\n int smaller = cost[i];\n int larger = (i)-cost[i]-count[nums[i]];\n res += min(smaller, larger);\n res %= mod;\n count[nums[i]]++;\n }\n return res;\n }\n\n void helper(vector<int>& nums, int a, int b){\n if(a>=b){return;}\n int mid = a+(b-a)/2;\n helper(nums, a, mid);\n helper(nums, mid+1, b);\n\n for(int i=mid+1;i<=b;i++){\n auto iter = lower_bound(sorted+a, sorted+mid+1, nums[i]);\n cost[i] += iter-(sorted+a);\n }\n\n // int i = a, j = mid+1, p=a;\n\n // while(i<=mid&&j<=b){\n // if(sorted[i]<sorted[j]){\n // temp[p++] = sorted[i++];\n // }\n // else{\n // temp[p++] = sorted[j++];\n // }\n // }\n\n inplace_merge(sorted+a, sorted+mid+1, sorted+b+1);\n\n // while(i<=mid){\n // temp[p++] = sorted[i++];\n // }\n\n // while(j<=b){\n // temp[p++] = sorted[j++];\n // }\n\n // for(int i=a;i<=b;i++){\n // sorted[i] = temp[i];\n // }\n }\n};",
"memory": "244018"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 2 | {
"code": "#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\n#include <bits/stdc++.h>\n\nusing namespace __gnu_pbds;\nusing namespace std;\n\ntypedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;\nclass Solution {\npublic:\n int freq[100001]={0};\n long long int mod=pow(10,9)+7;\n int createSortedArray(vector<int>& nums) {\n int n=nums.size(),ans=0;\n pbds set;\n for(int i=0;i<n;i++)\n {\n set.insert(nums[i]);\n freq[nums[i]]++;\n int count=set.order_of_key(nums[i]);\n\n ans=(ans+min(count,i+1-count-freq[nums[i]])%mod)%mod;\n }\n return ans;\n }\n};",
"memory": "274206"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 2 | {
"code": "#include<ext/pb_ds/assoc_container.hpp>\n#include<ext/pb_ds/tree_policy.hpp>\nusing namespace __gnu_pbds;\ntypedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; \n// find_by_order, order_of_key\n\n#define mod 1000000007\nclass Solution {\npublic:\n int strictly_less_than(pbds &st, int val)\n {\n return st.order_of_key(val);\n }\n int strictly_greater_than(pbds &st, int val)\n {\n return st.size() - st.order_of_key(val+1);\n }\n int createSortedArray(vector<int>& instructions) {\n \n int n = instructions.size();\n int cost = 0;\n pbds st;\n\n for(auto &val: instructions)\n {\n cost = (cost + min(strictly_less_than(st, val), strictly_greater_than(st, val))) % mod;\n st.insert(val); \n }\n return cost;\n }\n};",
"memory": "278518"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 2 | {
"code": "#include<ext/pb_ds/assoc_container.hpp>\n#include<ext/pb_ds/tree_policy.hpp>\nusing namespace __gnu_pbds;\ntypedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; \n// find_by_order, order_of_key\n\n#define mod 1000000007\nclass Solution {\npublic:\n int strictly_less_than(pbds &st, int val)\n {\n return st.order_of_key(val);\n }\n int strictly_greater_than(pbds &st, int val)\n {\n return st.size() - st.order_of_key(val+1);\n }\n int createSortedArray(vector<int>& instructions) {\n \n int n = instructions.size();\n int cost = 0;\n pbds st;\n\n for(auto &val: instructions)\n {\n cost = (cost + min(strictly_less_than(st, val), strictly_greater_than(st, val))) % mod;\n st.insert(val); \n }\n return cost;\n }\n};",
"memory": "282831"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 2 | {
"code": "/* Segment Tree \nUse of each segment tree leaf node to represent a number in instructions, the info of these leaf nodes are the count of their appearence so far when iterating through the instructions array.\n\n### Implementation Walkthrough\n1. **Initialization and Preprocessing:**\n - Convert the `instructions` array into a sorted set `st` to remove duplicates and create a mapping from each element to its index in the sorted set (`num2idx`).\n - Initialize a segment tree (`SegTreeNode` class) to keep track of the counts of elements as they are inserted.\n2. **Segment Tree Node (`SegTreeNode`):**\n - Represents a node in the segment tree, with `start` and `end` indicating the range it covers.\n - `info` stores the count of elements within the range represented by the node.\n - Pointers to left and right child nodes.\n3. **Initialization (`init`):**\n - Recursively initialize the segment tree with `info` set to 0, representing no elements have been processed initially.\n4. **Update (`updateSingleBy`):**\n - Updates the segment tree by incrementing the count of the element at index `idx` by `val` (usually 1).\n - Recursively updates the counts in the segment tree nodes.\n5. **Query (`queryRange`):**\n - Queries the segment tree to count the number of elements within a range `[a, b]`.\n - This function is used to find the number of elements smaller or larger than a given element.\n6. **Main Logic (`createSortedArray`):**\n - For each element in `instructions`, calculate the cost of inserting it by querying the number of smaller and larger elements already processed.\n - Update the result with the minimum of these two counts, modulo `M` (1e9+7).\n - Update the segment tree to include the current element.\n\n### Key Ideas\n- **Segment Tree:** Used for efficiently counting the number of elements in a range.\n- **Discrete Mapping:** Maps elements to indices in a sorted set to handle potential large values and facilitate the use of the segment tree.\n- **Counting Inversions:** The problem boils down to counting inversions in the array, which is efficiently handled by the segment tree.\n\n### Time Complexity\n- **O(n log n):** The segment tree operations (update and query) each take O(log n) time, and they are performed for each of the `n` elements in the `instructions` array.\n\n### Space Complexity\n- **O(n):** The space complexity is dominated by the segment tree and the auxiliary data structures like `num2idx` map, both of which have sizes proportional to the number of unique elements in the `instructions` array.\n\n### Optimization\nThe current implementation is efficient for the problem constraints, leveraging the segment tree's capability to handle range queries and updates efficiently. However, the code could be further optimized or simplified in terms of structure, particularly by:\n\n- **Lazy Propagation:** If the problem included range updates, lazy propagation could be used to optimize update operations.\nGiven the specific problem requirements, the current solution is appropriately optimized and does not require further modifications.\n*/\nclass Solution {\npublic:\n int createSortedArray(vector<int>& instructions) {\n // Convert the instruction array to a sorted set, remove duplicate\n set<int> st(instructions.begin(), instructions.end());\n unordered_map<int,int>num2idx;\n int i = 0;\n for (auto x:st)\n {\n num2idx[x] = i;\n i++;\n }\n int n = num2idx.size();\n \n SegTreeNode* root = new SegTreeNode(0, n-1);\n init(root, 0, n-1);\n \n long ret = 0; \n for (auto x: instructions)\n {\n long a = queryRange(root, 0, num2idx[x]-1);\n long b = queryRange(root, num2idx[x]+1, n-1);\n ret += min(a,b);\n ret %= M;\n updateSingleBy(root, num2idx[x], 1);\n }\n return ret; \n }\n\nprivate:\n const long M = 1e9+7;\n class SegTreeNode {\n public:\n SegTreeNode* left;\n SegTreeNode* right;\n int start, end;\n // info represents the count of leaf node that has appeared so far\n int info; \n SegTreeNode(int a, int b):start(a),end(b),info(0),left(nullptr),right(nullptr){}\n };\n \n // Init info of all nodes to 0\n void init(SegTreeNode* node, int a, int b)\n { \n if (a == b) {\n // When there is no element, init node->info to be 0\n node->info = 0;\n return;\n }\n int mid = a + ((b - a) >> 1);\n if (node->left == nullptr) {\n node->left = new SegTreeNode(a, mid);\n node->right = new SegTreeNode(mid + 1, b);\n }\n init(node->left, a, mid);\n init(node->right, mid + 1, b);\n \n node->info = node->left->info + node->right->info;\n }\n \n // Update the info of each leaf node, id should be the index of the current iterated number in the sorted set\n void updateSingleBy(SegTreeNode* node, int idx, int val)\n { \n if (idx < node->start || idx > node->end) return;\n if (node->start == node->end) {\n node->info += val;\n return;\n }\n updateSingleBy(node->left, idx, val);\n updateSingleBy(node->right, idx, val);\n node->info = node->left->info + node->right->info;\n }\n \n // Based on the populated leaf node so far, calculate total smaller or larger node number\n int queryRange(SegTreeNode* node, int a, int b)\n {\n if (b < node->start || a > node->end) {\n return 0; \n }\n if (a <= node->start && b>=node->end) {\n return node->info; \n } \n return queryRange(node->left, a, b) + queryRange(node->right, a, b); \n } \n};",
"memory": "287143"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 2 | {
"code": "class Solution {\n class SegTreeNode\n {\n public:\n SegTreeNode* left;\n SegTreeNode* right;\n int start, end;\n int info; \n SegTreeNode(int a, int b):start(a),end(b),info(0),left(NULL),right(NULL){}\n };\n \n void init(SegTreeNode* node, int a, int b) // init for range [a,b]\n { \n if (a==b)\n {\n node->info = 0;\n return;\n }\n int mid = (a+b)/2;\n if (node->left==NULL)\n {\n node->left = new SegTreeNode(a, mid);\n node->right = new SegTreeNode(mid+1, b);\n }\n init(node->left, a, mid);\n init(node->right, mid+1, b);\n \n node->info = node->left->info + node->right->info; // write your own logic\n }\n \n void updateSingleBy(SegTreeNode* node, int id, int val)\n { \n if (id < node->start || id > node->end ) return;\n if (node->start == node->end)\n {\n node->info += val;\n return;\n }\n updateSingleBy(node->left, id, val);\n updateSingleBy(node->right, id, val);\n node->info = node->left->info + node->right->info; // write your own logic\n }\n \n int queryRange(SegTreeNode* node, int a, int b)\n {\n if (b < node->start || a > node->end )\n {\n return 0; // write your own logic\n }\n if (a <= node->start && b>=node->end)\n {\n return node->info; // write your own logic\n } \n return queryRange(node->left, a, b) + queryRange(node->right, a, b); // write your own logic\n } \n \n long M = 1e9+7;\n \npublic:\n int createSortedArray(vector<int>& instructions) \n {\n set<int>Set(instructions.begin(), instructions.end());\n unordered_map<int,int>num2idx;\n int i = 0;\n for (auto x:Set)\n {\n num2idx[x] = i;\n i++;\n }\n int n = num2idx.size();\n \n SegTreeNode* root = new SegTreeNode(0, n-1);\n init(root, 0, n-1);\n \n long ret = 0; \n for (auto x: instructions)\n {\n long a = queryRange(root, 0, num2idx[x]-1);\n long b = queryRange(root, num2idx[x]+1, n-1);\n ret += min(a,b);\n ret %= M;\n updateSingleBy(root, num2idx[x], 1);\n }\n return ret; \n }\n};",
"memory": "291456"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 2 | {
"code": "typedef long long LL;\nclass Solution {\n class SegTreeNode {\n public:\n SegTreeNode* left;\n SegTreeNode* right;\n int start, end;\n int info;\n SegTreeNode(int a, int b) : start(a), end(b), info(0), left(NULL), right(NULL) {}\n };\n void init(SegTreeNode* node, int a, int b) {\n if(a == b) {\n node->info = 0;\n return;\n }\n int mid = (a + b) / 2;\n if(node->left == NULL) {\n node->left = new SegTreeNode(a, mid);\n node->right = new SegTreeNode(mid+1, b);\n init(node->left, a, mid);\n init(node->right, mid+1, b);\n }\n node->info = node->left->info + node->right->info;\n }\n void updateSingle(SegTreeNode* node, int id, int val) {\n if(id < node->start || id > node->end) return;\n if(node->start == node->end) {\n node->info += val;\n return;\n }\n updateSingle(node->left, id, val);\n updateSingle(node->right, id, val);\n node->info = node->left->info + node->right->info;\n }\n int queryRange(SegTreeNode* node, int a, int b) {\n if(b < node->start || a > node->end)\n return 0;\n if(a <= node->start && b >= node->end)\n return node->info;\n return queryRange(node->left, a, b) + queryRange(node->right, a, b);\n }\n LL M = 1e9 + 7;\n\n\npublic:\n int createSortedArray(vector<int>& instructions) {\n set<int> Set(instructions.begin(), instructions.end());\n unordered_map<int, int> num2Idx;\n int i = 0;\n for(auto x : Set) {\n num2Idx[x] = i;\n i++;\n }\n int n = num2Idx.size();\n SegTreeNode* root = new SegTreeNode(0, n-1);\n init(root, 0, n-1);\n long ret = 0;\n for(auto x : instructions) {\n long a = queryRange(root, 0, num2Idx[x] - 1);\n long b = queryRange(root, num2Idx[x] + 1, n - 1);\n ret += min(a, b);\n ret %= M;\n updateSingle(root, num2Idx[x], 1);\n }\n return ret;\n }\n};",
"memory": "295768"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 2 | {
"code": "class Solution {\npublic:\n vector<int>seg;\n vector<int>v;\n int mod=1e9+7;\n int build(int node,int l,int r){\n if(l>r)\n return 0;\n if(l==r){\n return seg[node]=v[l];\n }\n int mid=(l+r)/2;\n return seg[node]=build(2*node+1,l,mid)+build(2*node+2,mid+1,r);\n }\n int query(int node,int l,int r,int sl,int sr){\n if(sl>r||sr<l)\n return 0;\n if(sl>=l&&sr<=r)\n return seg[node];\n int mid=(sl+sr)/2;\n return query(2*node+1,l,r,sl,mid)+query(2*node+2,l,r,mid+1,sr);\n }\n void update(int node,int l,int r,int index,int num){\n if(l==r)\n {\n seg[node]=num;\n return;\n }\n int mid=(l+r)/2;\n if(index<=mid){\n update(2*node+1,l,mid,index,num);\n }\n else{\n update(2*node+2,mid+1,r,index,num);\n }\n seg[node]=seg[2*node+1]+seg[2*node+2];\n }\n int createSortedArray(vector<int>& instructions) {\n seg.resize(400020);\n // v= instructions;\n while(v.size()<100001)\n v.push_back(0);\n long ans=0;\n int n=instructions.size();\n build(0,0,100000);\n for(int i=0;i<n;i++){\n int left=query(0,0,max(0,instructions[i]-1),0,100000);\n int right=query(0,instructions[i]+1,100000,0,100000);\n ans+=min(left,right);\n cout<<min(left,right)<<endl;\n ans%=mod;\n update(0,0,100000,instructions[i],++v[instructions[i]]);\n }\n return ans;\n }\n};",
"memory": "295768"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 2 | {
"code": "class Solution {\nprivate:\n vector<int> csel, cgel;\n int mod = 1e9+7;\n \n void merge(vector<int> &a, vector<int> &inx1, vector<int> &inx2, int left, int mid, int right) {\n vector<int> tmp(right-left+1);\n \n int i = left, j = mid+1, k = 0;\n while(i <= mid && j <= right) {\n if(a[inx1[i]] >= a[inx1[j]]) {\n tmp[k++] = inx1[i++];\n } else {\n csel[inx1[j]] += mid - i + 1;\n tmp[k++] = inx1[j++];\n }\n }\n\n while(i <= mid) \n tmp[k++] = inx1[i++];\n while(j <= right)\n tmp[k++] = inx1[j++];\n\n for(int l = left; l <= right; l++)\n inx1[l] = tmp[l-left]; \n \n i = left, j = mid+1, k = 0;\n while(i <= mid && j <= right) {\n if(a[inx2[i]] <= a[inx2[j]]) {\n tmp[k++] = inx2[i++];\n } else {\n cgel[inx2[j]] += mid - i + 1;\n tmp[k++] = inx2[j++];\n }\n }\n\n while(i <= mid) \n tmp[k++] = inx2[i++];\n while(j <= right)\n tmp[k++] = inx2[j++];\n\n for(int l = left; l <= right; l++)\n inx2[l] = tmp[l-left]; \n }\n \n void sort(vector<int> &a, vector<int> &inx1, vector<int> &inx2, int left, int right, int flag) {\n if(left >= right)\n return;\n int mid = (left + right) / 2;\n sort(a, inx1, inx2, left, mid, flag);\n sort(a, inx1, inx2, mid+1, right, flag);\n merge(a, inx1, inx2, left, mid, right);\n }\n \npublic:\n int createSortedArray(vector<int>& a) {\n int n = a.size();\n vector<int> inx1(n), inx2(n);\n csel.resize(n);\n cgel.resize(n);\n \n for(int i = 0; i < n; i++)\n inx1[i] = inx2[i] = i;\n \n sort(a, inx1, inx2, 0, n-1, 1);\n \n int ans = 0;\n for(int i = 0; i < n; i++) \n ans = (ans + min(csel[i], cgel[i])) % mod;\n \n return ans;\n }\n};",
"memory": "300081"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 2 | {
"code": "void update(vector<long long> &seg_tree, int ls, int rs, int l, int r, long long pos, int addV){\n\n if(ls<=l && r<=rs){\n if(l!=r){\n long long mid = ((long long)l+(long long)r)/2;\n\n update(seg_tree, ls, rs, l, mid, 2*pos+1, addV);\n update(seg_tree, ls, rs, mid+1, r, 2*pos+2, addV);\n seg_tree[pos] = seg_tree[2*pos+1] + seg_tree[2*pos+2];\n\n }\n else{\n seg_tree[pos] += addV;\n }\n }\n else if(ls<=r && rs>=l){\n long long mid = (l+r)/2;\n update(seg_tree, ls, rs, l, mid, 2*pos+1, addV);\n update(seg_tree, ls, rs, mid+1, r, 2*pos+2, addV);\n seg_tree[pos] = seg_tree[2*pos+1]+seg_tree[2*pos+2];\n }else{\n return;\n }\n}\nlong long query(vector<long long> &seg_tree, int ls, int rs, int l, int r, int pos){\n if(ls<=l && rs>=r){\n return seg_tree[pos];\n }\n else if(ls<=r && rs>=l){\n long long mid = (l+r)/2;\n long long a = query(seg_tree, ls, rs, l, mid, 2*pos+1);\n long long b = query(seg_tree, ls, rs, mid+1, r, 2*pos+2);\n return a+b;\n\n }\n else{\n return 0;\n }\n}\nclass Solution { \npublic:\n int createSortedArray(vector<int>& instructions) {\n const int N = 1e5; \n const long long mod = 1e9 + 7;\n vector<long long> seg_tree(4*N+10);\n long long ans = 0;\n unordered_map<int, int> mp;\n for(int i=0; i<instructions.size(); i++){\n mp[instructions[i]]++;\n update(seg_tree, instructions[i], instructions[i], 0, N, 0, 1);\n long long a = query(seg_tree, 0, instructions[i]-1, 0, N, 0);\n long long b = i+1-mp[instructions[i]]-a;\n ans+= min(a, b)%mod;\n ans = ans%mod;\n }\n return ans;\n \n }\n};",
"memory": "304393"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 2 | {
"code": "class Solution {\n vector<int> seg;\n int M =1e9+7;\npublic:\n int createSortedArray(vector<int>& instructions) {\n int n = *max_element(instructions.begin(),instructions.end());\n vector<int> freq(n+1,0);\n seg.resize(4*1e6);\n build(freq,0,0,n);\n int ans =0;\n for(int val: instructions){\n update(0,0,n,val,1);\n int q = min(query(0,0,n,val-1),seg[0]-query(0,0,n,val));\n ans = (ans+q)%M;\n }\n return ans;\n }\n void build(vector<int>&a,int idx, int low, int high){\n if(low==high){\n seg[idx]= a[low];\n return;\n }\n int mid = (low+high)>>1;\n build(a,2*idx+1,low,mid);\n build(a,2*idx+2,mid+1,high);\n seg[idx] = seg[2*idx+1] + seg[2*idx+2];\n }\n\n int query(int idx, int low, int high, int i){\n if(0<=low && high<=i) return seg[idx];\n if(high<0 || low>i) return 0;\n int mid = (low+high)>>1;\n int right = query(2*idx+2,mid+1,high,i);\n int left = query(2*idx+1,low,mid,i);\n return left + right;\n }\n\n void update(int idx, int low, int high, int i, int val){\n if(low==high){\n seg[idx]+=val;\n return;\n }\n int mid = (low+high)>>1;\n if(i>mid) update(2*idx+2,mid+1, high,i,val);\n else update(2*idx+1, low, mid,i, val);\n seg[idx] = seg[ 2*idx+1]+seg[2*idx+2];\n }\n};",
"memory": "308706"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 3 | {
"code": "#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\n#include <bits/stdc++.h>\n\nusing namespace __gnu_pbds;\nusing namespace std;\n\n \n#define ordered_set tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> \nclass Solution {\npublic:\n long long int mod=pow(10,9)+7;\n\n int createSortedArray(vector<int>& nums) {\n ordered_set st;\n int n= nums.size();\n int ans=0;\n vector<int>mp(100001, 0);\n for(int i=0; i<n; i++){\n \n st.insert(nums[i]);\n mp[nums[i]]++;\n int count=st.order_of_key(nums[i]);\n ans=(ans%mod+min(count,i+1-count-mp[nums[i]])%mod)%mod;\n }\n return ans%mod;\n }\n};\n\n",
"memory": "313018"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 3 | {
"code": "#include<ext/pb_ds/assoc_container.hpp>\n#include<ext/pb_ds/tree_policy.hpp>\nusing namespace __gnu_pbds;\n\ntypedef tree<pair<int,int>,null_type,less<pair<int,int>>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;\n\nclass Solution {\n int mod = 1e9 + 7;\npublic:\n int createSortedArray(vector<int>& instructions) {\n ordered_set st;\n unordered_map<int,int> mp;\n int ans=0;\n for(int i=0;i<instructions.size();i++){\n mp[instructions[i]]++;\n if(!st.empty() && instructions[i]<st.rbegin()->first && instructions[i]>st.begin()->first){\n int l=st.order_of_key({instructions[i],0});\n int r=st.size()-st.order_of_key({instructions[i],mp[instructions[i]]});\n ans=(ans+min(l,r))%mod;\n }\n st.insert({instructions[i],mp[instructions[i]]});\n }\n return ans;\n }\n};",
"memory": "317331"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 3 | {
"code": "#include <bits/stdc++.h>\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\n\nusing namespace std;\nusing namespace __gnu_pbds;\n\n\ntypedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update> mset;\n\n\nclass Solution {\npublic:\n int createSortedArray(vector<int>& ins) {\n\n mset ms;\n int ans = 0;\n int modu = 1e9;\n modu+=7;\n map<int,int> mp;\n for(auto &x : ins){\n ms.insert(x);\n mp[x]++;\n int lowerCnt = ms.order_of_key(x);\n int upperCnt = ms.size() - lowerCnt - mp[x];\n ans += min(lowerCnt,upperCnt);\n ans %= modu;\n }\n return ans;\n }\n};",
"memory": "321643"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 3 | {
"code": "#include<ext/pb_ds/assoc_container.hpp>\n#include<ext/pb_ds/tree_policy.hpp>\nusing namespace __gnu_pbds;\ntypedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> oset;\n\nclass Solution {\npublic:\n int createSortedArray(vector<int>& instructions) {\n int n = instructions.size();\n\n map<int,int> freq;\n oset o;\n o.insert(instructions[0]);\n freq[instructions[0]]++;\n\n int ans = 0;\n int mod = 1e9 + 7;\n for(int i=1; i<n; i++) {\n \n int smaller = o.order_of_key(instructions[i]);\n int bigger = i - smaller - freq[instructions[i]];\n\n int mn = min(smaller, bigger);\n\n ans = (ans%mod + mn%mod)%mod;\n freq[instructions[i]]++;\n o.insert(instructions[i]);\n\n }\n return ans;\n }\n};",
"memory": "325956"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 3 | {
"code": "#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\nusing namespace __gnu_pbds;\n\nclass Solution {\npublic:\n const int mod = 1e9 + 7;\n template <typename T, typename CMP = less<T>>\n using ordered_set = tree<T, null_type, CMP, rb_tree_tag, tree_order_statistics_node_update>;\n int createSortedArray(vector<int>& a) {\n ordered_set<pair<int, int>> set; set.insert({a[0], 0});\n map<int, int> cnt{{a[0], 1}};\n int cost = 0;\n for (int i = 1; i < a.size(); i++) { \n int l = set.order_of_key({a[i], INT_MIN});\n int r = set.size() - cnt[a[i]] - l;\n set.insert({a[i], i});\n cost = (cost + min(l, r)) % mod;\n cnt[a[i]]++;\n }\n return cost;\n }\n};",
"memory": "330268"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 3 | {
"code": "class Solution {\npublic:\n int MOD = 1e9 + 7;\n int createSortedArray(vector<int>& instructions) {\n int n = instructions.size();\n\n\n vector<long> cntlarger(n, 0);\n vector<int> sorted(n); // index in sorted order\n for (int i = 0; i < n; i++)\n sorted[i] = i;\n\n divcon(instructions, sorted, cntlarger, 0, n-1);\n\n long ret = 0;\n unordered_map<int, long> cnt;\n for (long i = 0; i < n; i++) {\n long smaller = i - cntlarger[i] - cnt[instructions[i]];\n long cost = min(smaller, cntlarger[i]);\n ret = (ret + cost) % MOD;\n cnt[instructions[i]]++;\n }\n return ret;\n\n }\n\n void divcon(const vector<int>& instructions, vector<int>& sorted, vector<long>& cntlarger, int l, int r) {\n if (l >= r) return;\n\n // divide\n int mid = (l + r) / 2;\n divcon(instructions, sorted, cntlarger, l, mid);\n divcon(instructions, sorted, cntlarger, mid+1, r);\n\n // conquer\n int i = l, j = mid+1;\n vector<int> tmp(r - l + 1);\n for (int k = 0; k <= (r - l); k++) {\n if (i > mid) {\n tmp[k] = sorted[j++];\n } else if (j > r) {\n tmp[k] = sorted[i++];\n } else if (instructions[sorted[i]] <= instructions[sorted[j]]) {\n tmp[k] = sorted[i++];\n } else {\n cntlarger[sorted[j]] += (mid + 1 - i);\n tmp[k] = sorted[j++];\n }\n }\n\n // update sorted\n for (int k = 0; k <= (r - l); k++) {\n sorted[l + k] = tmp[k];\n }\n }\n};\n\n/**\nlike count of smaller numbers after self,\n\nthe larger elements on the left of a number == numbers jump from left to right during stable sort\n(because the same value number will remain the same order)\n\nmerge A, B to C\n\nif A[i] <= B[j]\nplace A[i]\nif A[i] > B[j]\nplace B[j], (n-i) elements jump from left of B[j] to right of B[j]\n\n\n*/\n",
"memory": "334581"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 3 | {
"code": "class SegTreeNode\n{\npublic:\n SegTreeNode* left;\n SegTreeNode* right;\n int start;\n int end;\n int info;\n SegTreeNode(int a, int b)\n {\n left = nullptr;\n right = nullptr;\n start = a;\n end = b;\n info = 0;\n }\n};\nclass Solution {\npublic:\n\n void init(SegTreeNode* root, int a, int b)\n {\n if(a==b)\n {\n root->info = 0;\n return;\n }\n\n int mid = (a + b)/2;\n\n if(root->left == nullptr)\n {\n root->left = new SegTreeNode(a,mid);\n root->right = new SegTreeNode(mid+1, b);\n }\n\n init(root->left, a, mid);\n init(root->right, mid+1, b);\n root->info = 0;\n }\n\n void updateNode(SegTreeNode* root, int id, int val)\n {\n if(id < root->start || id > root->end)\n {\n return ;\n }\n if(root->start == root->end)\n {\n root->info += val;\n return;\n }\n updateNode(root->left, id, val);\n updateNode(root->right, id, val);\n\n root->info = root->left->info + root->right->info;\n return;\n }\n\n int query(SegTreeNode* root, int a, int b)\n {\n if(b < root->start || a > root->end)\n {\n return 0;\n }\n if(a <= root->start && root->end <= b)\n {\n return root->info;\n }\n\n return query(root->left, a, b) + query(root->right, a, b);\n }\n\n\n int createSortedArray(vector<int>& instructions) {\n int result = 0;\n\n int mod = 1e9 + 7;\n int n = instructions.size();\n set<int> s(instructions.begin(), instructions.end());\n unordered_map<int,int> num2id;\n int i = 0;\n for(int x : s)\n {\n num2id[x] = i;\n i++;\n }\n //int n = num2id.size();\n SegTreeNode* root = new SegTreeNode(0, n-1);\n init(root, 0, n-1);\n\n for(auto i :instructions)\n {\n int id = num2id[i];\n long count1 = query(root, 0, id - 1);\n long count2 = query(root, id+1, n-1);\n\n result+=min(count1, count2);\n result %= mod;\n\n updateNode(root, id, 1);\n\n }\n\n return result; \n }\n};",
"memory": "338893"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 3 | {
"code": "#include <ext/pb_ds/assoc_container.hpp> \n#include <ext/pb_ds/tree_policy.hpp> \nusing namespace __gnu_pbds; \n \n#define ordered_set tree<pair<int,int>, null_type,less<pair<int,int>>, rb_tree_tag,tree_order_statistics_node_update> \n \n\n\nclass Solution {\npublic:\n int mod=1e9+7;\n int createSortedArray(vector<int>& instructions) {\n\n int ans=0;\n multiset<int> m;\n m.insert(INT_MAX);\n ordered_set s; \n for(auto it : instructions){\n s.insert({it,s.size()});\n m.insert(it);\n\n int l=*m.lower_bound(it);\n int r=*m.upper_bound(it);\n\n // cout<<l<<\" \"<<r<<endl;\n\n l=s.order_of_key({l,0});\n l=max(0,l);\n\n if(r==INT_MAX) r=0;\n else r=s.size()-s.order_of_key({r,0});\n\n ans=1LL*(ans+min(l,r))%mod;\n // cout<<l<<\" \"<<r<<endl; \n }\n \n return ans;\n }\n};",
"memory": "343206"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 3 | {
"code": "#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\n#include <bits/stdc++.h>\n#define ll long long \nusing namespace __gnu_pbds;\nusing namespace std;\nll mod = 1e9+7;\ntypedef tree<pair<ll,ll> , null_type, less_equal<pair<ll,ll> >, rb_tree_tag,tree_order_statistics_node_update> pbds;\nclass Solution {\npublic:\n int createSortedArray(vector<int>& instructions) {\n vector<int>v = instructions;\n ll n = v.size();\n pbds mp;\n ll ans = 0;\n map<ll,ll>jp;\n for(ll i = 0;i<n;i++){\n ll a= mp.order_of_key({v[i],-1});\n ll b = (i)-a-jp[v[i]];\n ans = (ans+min(a,b))%mod;\n mp.insert({v[i],-1});\n jp[v[i]]++;\n }\n return ans;\n }\n};",
"memory": "347518"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.