id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
463
<p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents&nbsp;land and <code>grid[i][j] = 0</code> represents water.</p> <p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p> <p>The island doesn&#39;t have &quot;lakes&quot;, meaning the water inside isn&#39;t connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don&#39;t exceed 100. Determine the perimeter of the island.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" /> <pre> <strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]] <strong>Output:</strong> 16 <strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [[1]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> grid = [[1,0]] <strong>Output:</strong> 4 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>row == grid.length</code></li> <li><code>col == grid[i].length</code></li> <li><code>1 &lt;= row, col &lt;= 100</code></li> <li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li> <li>There is exactly one island in <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\nprivate:\n bool isValid(int row, int col, int n, int m) {\n return (row < n) && (row >= 0) && (col < m) && (col >= 0);\n }\n void dfs(int row, int col, int n, int m, vector<vector<int>>& grid,\n vector<vector<bool>>& vis, vector<vector<int>>& sides,\n vector<int>& dr, vector<int>& dc) {\n vis[row][col] = true;\n for (int i = 0; i < 4; i++) {\n int newr = row + dr[i];\n int newc = col + dc[i];\n if (isValid(newr, newc, n, m)) {\n if (!vis[newr][newc] && grid[newr][newc]){\n sides[row][col]--;\n dfs(newr,newc,n,m,grid,vis,sides,dr,dc);\n }\n else if(vis[newr][newc] && grid[newr][newc])sides[row][col]--;\n }\n }\n }\n\npublic:\n int islandPerimeter(vector<vector<int>>& grid) {\n int n = grid.size();\n int m = grid[0].size();\n vector<vector<bool>> vis(n, vector<bool>(m, false));\n vector<vector<int>> sides(n, vector<int>(m, 0));\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n if (grid[i][j])\n sides[i][j] = 4;\n }\n }\n vector<int> dr = {-1, 0, 1, 0};\n vector<int> dc = {0, 1, 0, -1};\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n if(!vis[i][j] && grid[i][j])\n dfs(i, j, n, m, grid, vis, sides, dr, dc);\n }\n }\n int sum = 0;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n sum += sides[i][j];\n }\n }\n return sum;\n }\n};", "memory": "111227" }
463
<p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents&nbsp;land and <code>grid[i][j] = 0</code> represents water.</p> <p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p> <p>The island doesn&#39;t have &quot;lakes&quot;, meaning the water inside isn&#39;t connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don&#39;t exceed 100. Determine the perimeter of the island.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" /> <pre> <strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]] <strong>Output:</strong> 16 <strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [[1]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> grid = [[1,0]] <strong>Output:</strong> 4 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>row == grid.length</code></li> <li><code>col == grid[i].length</code></li> <li><code>1 &lt;= row, col &lt;= 100</code></li> <li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li> <li>There is exactly one island in <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int islandPerimeter(vector<vector<int>>& grid) {\n int row= grid.size();\n int col= grid[0].size();\n int perimeter=0;\n\n for(int i=0; i<row; i++){\n for(int j=0; j<col; j++){\n if(grid[i][j]==1){\n\n vector<pair<int, int>>direction= {{-1,0}, {0,-1}, {1,0}, {0,1}};\n for(const auto& dir: direction){\n int newRow= i+dir.first;\n int newCol= j+ dir.second;\n\n if(newRow<0 || newRow>= row || newCol<0 || newCol>= col || grid[newRow][newCol]==0) {\n perimeter++;\n }\n }\n\n }\n }\n }\n return perimeter;\n \n }\n};", "memory": "111227" }
463
<p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents&nbsp;land and <code>grid[i][j] = 0</code> represents water.</p> <p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p> <p>The island doesn&#39;t have &quot;lakes&quot;, meaning the water inside isn&#39;t connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don&#39;t exceed 100. Determine the perimeter of the island.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" /> <pre> <strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]] <strong>Output:</strong> 16 <strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [[1]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> grid = [[1,0]] <strong>Output:</strong> 4 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>row == grid.length</code></li> <li><code>col == grid[i].length</code></li> <li><code>1 &lt;= row, col &lt;= 100</code></li> <li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li> <li>There is exactly one island in <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\nvoid dfs(vector<vector<int>>& grid, int x, int y,int &ans) {\n vector<pair<int, int>> directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\n // res.push_back({x, y});\n grid[x][y]=2;\n int count=0;\n for (auto& dir : directions) {\n int nx = x + dir.first;\n int ny = y + dir.second;\n if(nx == -1 || nx ==grid.size() || ny == -1 || ny == grid[0].size()){\n count++;\n }\n else if (nx >= 0 && nx <grid.size() && ny >= 0 && ny < grid[0].size() && grid[nx][ny] == 0) {\n count++;\n }\n }\n ans+=count;\n for (auto& dir : directions) {\n int nx = x + dir.first;\n int ny = y + dir.second;\n if (nx >= 0 && nx < grid.size() && ny >= 0 && ny < grid[0].size() && grid[nx][ny] == 1) {\n dfs(grid, nx, ny, ans);\n }\n }\n }\n int islandPerimeter(vector<vector<int>>& grid) {\n if (grid.empty()) return 0;\n int rows = grid.size();\n int cols = grid[0].size();\n // int count = 0;\n int ans=0;\n for (int i = 0; i < rows; i++) {\n for (int j = 0; j < cols; j++) {\n if ( grid[i][j] == 1) {\n // vector<pair<int, int>> tmp;\n dfs(grid, i, j,ans);\n // / count+=check(tmp);\n }\n }\n }\n\n return ans;\n }\n \n};", "memory": "112622" }
463
<p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents&nbsp;land and <code>grid[i][j] = 0</code> represents water.</p> <p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p> <p>The island doesn&#39;t have &quot;lakes&quot;, meaning the water inside isn&#39;t connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don&#39;t exceed 100. Determine the perimeter of the island.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" /> <pre> <strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]] <strong>Output:</strong> 16 <strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [[1]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> grid = [[1,0]] <strong>Output:</strong> 4 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>row == grid.length</code></li> <li><code>col == grid[i].length</code></li> <li><code>1 &lt;= row, col &lt;= 100</code></li> <li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li> <li>There is exactly one island in <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int bfs(int i,int j,vector<vector<int>>& v,vector<vector<int>> vis){\n int n=v.size(),m=v[0].size();\n queue<pair<int,int>> q;\n q.push({i,j});\n vis[i][j]=1;\n int ans=0;\n while(q.size()){\n int x=q.front().first;\n int y=q.front().second;\n q.pop();\n int dx[]={-1,0,1,0};\n int dy[]={0,1,0,-1};\n for(int k=0;k<4;k++){\n int r=x+dx[k];\n int c=y+dy[k];\n if(r>=0 and c>=0 and r<n and c<m and !vis[r][c] and v[r][c]==1){\n q.push({r,c});\n vis[r][c]=1;\n }\n if(r>=0 and c>=0 and r<n and c<m){\n if(v[r][c]==0)ans++;\n }\n else if(!(r>=0 and c>=0 and r<n and c<m)){\n ans++;\n }\n }\n }\n return ans;\n }\n int islandPerimeter(vector<vector<int>>& v) {\n int n=v.size(),m=v[0].size();\n vector<vector<int>> vis(n,vector<int>(m,0));\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n if(!vis[i][j] and v[i][j]==1){\n return bfs(i,j,v,vis);\n }\n }\n }\n return -1;\n }\n};", "memory": "112622" }
463
<p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents&nbsp;land and <code>grid[i][j] = 0</code> represents water.</p> <p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p> <p>The island doesn&#39;t have &quot;lakes&quot;, meaning the water inside isn&#39;t connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don&#39;t exceed 100. Determine the perimeter of the island.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" /> <pre> <strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]] <strong>Output:</strong> 16 <strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [[1]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> grid = [[1,0]] <strong>Output:</strong> 4 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>row == grid.length</code></li> <li><code>col == grid[i].length</code></li> <li><code>1 &lt;= row, col &lt;= 100</code></li> <li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li> <li>There is exactly one island in <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int islandPerimeter(vector<vector<int>>& grid) {\n std::set<std::pair<int, int>> visited;\n int p = 0;\n int rows = grid.size();\n int cols = grid[0].size();\n\n for (int i = 0; i < rows; ++i) {\n for (int j = 0; j < cols; ++j) {\n if (grid[i][j] == 1 && visited.find({i, j}) == visited.end()) {\n visited.insert({i, j});\n\n if (i - 1 < 0 || grid[i - 1][j] == 0) {\n p += 1;\n }\n\n if (j - 1 < 0 || grid[i][j - 1] == 0) {\n p += 1;\n }\n\n if (i + 1 >= rows || grid[i + 1][j] == 0) {\n p += 1;\n }\n\n if (j + 1 >= cols || grid[i][j + 1] == 0) {\n p += 1;\n }\n }\n }\n }\n\n return p;\n }\n};", "memory": "114017" }
463
<p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents&nbsp;land and <code>grid[i][j] = 0</code> represents water.</p> <p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p> <p>The island doesn&#39;t have &quot;lakes&quot;, meaning the water inside isn&#39;t connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don&#39;t exceed 100. Determine the perimeter of the island.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" /> <pre> <strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]] <strong>Output:</strong> 16 <strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [[1]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> grid = [[1,0]] <strong>Output:</strong> 4 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>row == grid.length</code></li> <li><code>col == grid[i].length</code></li> <li><code>1 &lt;= row, col &lt;= 100</code></li> <li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li> <li>There is exactly one island in <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\nprivate:\n void dfs(int r, int c, vector<vector<int>>& grid, vector<vector<int>> &vis,\n map<pair<int, int>, int> &sides, int rows, int cols) {\n vis[r][c] = 1;\n\n int delRow[] = {-1,0,1,0};\n int delCol[] = {0,-1,0,1};\n for(int i=0; i<4; i++) {\n int newR = r + delRow[i];\n int newC = c + delCol[i];\n\n if(newR>=0 && newR<rows && newC>=0 && newC<cols && grid[newR][newC]==1) {\n if(!vis[newR][newC]) {\n sides[{newR,newC}] = sides[{newR, newC}]-1;\n dfs(newR, newC, grid, vis, sides, rows, cols);\n }\n else {\n sides[{newR,newC}]--;\n }\n }\n }\n }\npublic:\n int islandPerimeter(vector<vector<int>>& grid) {\n map<pair<int, int>, int> sides;\n\n int rows = grid.size();\n int cols = grid[0].size();\n \n vector<vector<int>> vis(rows, vector<int>(cols, 0));\n int r,c;\n for(int i=0; i<rows; i++) {\n for(int j=0; j<cols; j++) {\n if(grid[i][j] == 1) {\n r = i;\n c = j;\n sides[{i,j}] =4;\n }\n }\n }\n dfs(r, c, grid, vis, sides, rows, cols);\n int peri = 0;\n\n for(auto itr: sides) {\n peri += itr.second;\n }\n\n return peri;\n }\n};", "memory": "118202" }
463
<p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents&nbsp;land and <code>grid[i][j] = 0</code> represents water.</p> <p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p> <p>The island doesn&#39;t have &quot;lakes&quot;, meaning the water inside isn&#39;t connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don&#39;t exceed 100. Determine the perimeter of the island.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" /> <pre> <strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]] <strong>Output:</strong> 16 <strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [[1]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> grid = [[1,0]] <strong>Output:</strong> 4 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>row == grid.length</code></li> <li><code>col == grid[i].length</code></li> <li><code>1 &lt;= row, col &lt;= 100</code></li> <li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li> <li>There is exactly one island in <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int rows;\n int cols;\n vector<pair<int,int>> directions = {{-1,0}, {1,0}, {0,-1}, {0,1}};\n\n bool valid( int row, int col ){\n return row >= 0 && row < rows && col >= 0 && col < cols;\n }\n\n int countSides( int row, int col, vector<vector<int>>& grid){\n int count = 4;\n \n for( auto& dir : directions ){\n \n int newRow = row + dir.first;\n int newCol = col + dir.second;\n\n if( valid(newRow, newCol) && grid[newRow][newCol] == 1 ){\n count--;\n }\n }\n\n return count;\n\n }\n\n int islandPerimeter(vector<vector<int>>& grid) {\n\n rows = grid.size();\n\n if( rows == 0){\n return 4;\n }\n\n cols = grid[0].size();\n\n queue<pair<int,int>> qu;\n set<pair<int,int>> seen;\n int out = 0;\n\n int row, col;\n bool found = false;\n for( row = 0; row < rows; ++row){\n for( col = 0; col < cols; ++col ){\n if( valid(row,col) && grid[row][col] == 1){\n found = true;\n break;\n }\n }\n if( found ){\n break;\n }\n }\n\n if( !found ){\n return 0;\n }\n\n qu.push({row,col});\n seen.insert({row,col});\n \n while( !qu.empty() ){\n\n auto [row, col] = qu.front();\n qu.pop();\n\n out += countSides( row, col, grid );\n\n for( auto& dir : directions ){\n \n int newRow = row + dir.first;\n int newCol = col + dir.second;\n\n if( valid(newRow, newCol) && seen.find({newRow, newCol}) == seen.end() && grid[newRow][newCol] == 1){\n seen.insert({newRow,newCol});\n qu.push( {newRow, newCol});\n }\n\n }\n\n\n }\n\n\n return out;\n }\n};", "memory": "118202" }
1,297
<p>Given a string <code>text</code>, you want to use the characters of <code>text</code> to form as many instances of the word <strong>&quot;balloon&quot;</strong> as possible.</p> <p>You can use each character in <code>text</code> <strong>at most once</strong>. Return the maximum number of instances that can be formed.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/05/1536_ex1_upd.JPG" style="width: 132px; height: 35px;" /></strong></p> <pre> <strong>Input:</strong> text = &quot;nlaebolko&quot; <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/05/1536_ex2_upd.JPG" style="width: 267px; height: 35px;" /></strong></p> <pre> <strong>Input:</strong> text = &quot;loonbalxballpoon&quot; <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> text = &quot;leetcode&quot; <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= text.length &lt;= 10<sup>4</sup></code></li> <li><code>text</code> consists of lower case English letters only.</li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as <a href="https://leetcode.com/problems/rearrange-characters-to-make-target-string/description/" target="_blank"> 2287: Rearrange Characters to Make Target String.</a></p>
0
{ "code": "class Solution {\npublic:\n/*\ncreate a vector that has the bins of each letter\n\niterate across this to find the minimum value\n\nhave an output that is the minimum value\n*/\n int maxNumberOfBalloons(string text) {\n int maxNumberOfBalloonss = (std::count(text.begin(), text.end(), 'b'));\n int a = (std::count(text.begin(), text.end(), 'a'));\n int l = (std::count(text.begin(), text.end(), 'l')/2);\n int o = (std::count(text.begin(), text.end(), 'o')/2);\n int n = (std::count(text.begin(), text.end(), 'n'));\n if (maxNumberOfBalloonss > a) maxNumberOfBalloonss = a; \n if (maxNumberOfBalloonss > l) maxNumberOfBalloonss = l; \n if (maxNumberOfBalloonss > o) maxNumberOfBalloonss = o; \n if (maxNumberOfBalloonss > n) maxNumberOfBalloonss = n; \n\n return maxNumberOfBalloonss;\n }\n};", "memory": "7700" }
1,297
<p>Given a string <code>text</code>, you want to use the characters of <code>text</code> to form as many instances of the word <strong>&quot;balloon&quot;</strong> as possible.</p> <p>You can use each character in <code>text</code> <strong>at most once</strong>. Return the maximum number of instances that can be formed.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/05/1536_ex1_upd.JPG" style="width: 132px; height: 35px;" /></strong></p> <pre> <strong>Input:</strong> text = &quot;nlaebolko&quot; <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/05/1536_ex2_upd.JPG" style="width: 267px; height: 35px;" /></strong></p> <pre> <strong>Input:</strong> text = &quot;loonbalxballpoon&quot; <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> text = &quot;leetcode&quot; <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= text.length &lt;= 10<sup>4</sup></code></li> <li><code>text</code> consists of lower case English letters only.</li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as <a href="https://leetcode.com/problems/rearrange-characters-to-make-target-string/description/" target="_blank"> 2287: Rearrange Characters to Make Target String.</a></p>
0
{ "code": "class Solution {\npublic:\n int maxNumberOfBalloons(string text) {\n int arr[26] = {};\n for (auto c : text) arr[c - 'a']++;\n auto & b = arr['b' - 'a'];\n auto & a = arr['a' - 'a'];\n auto & l = arr['l' - 'a'];\n auto & o = arr['o' - 'a'];\n auto & n = arr['n' - 'a'];\n return min( min(b,a) , min(min(l,o)/2, n) );\n }\n};", "memory": "7800" }
1,297
<p>Given a string <code>text</code>, you want to use the characters of <code>text</code> to form as many instances of the word <strong>&quot;balloon&quot;</strong> as possible.</p> <p>You can use each character in <code>text</code> <strong>at most once</strong>. Return the maximum number of instances that can be formed.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/05/1536_ex1_upd.JPG" style="width: 132px; height: 35px;" /></strong></p> <pre> <strong>Input:</strong> text = &quot;nlaebolko&quot; <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/05/1536_ex2_upd.JPG" style="width: 267px; height: 35px;" /></strong></p> <pre> <strong>Input:</strong> text = &quot;loonbalxballpoon&quot; <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> text = &quot;leetcode&quot; <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= text.length &lt;= 10<sup>4</sup></code></li> <li><code>text</code> consists of lower case English letters only.</li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as <a href="https://leetcode.com/problems/rearrange-characters-to-make-target-string/description/" target="_blank"> 2287: Rearrange Characters to Make Target String.</a></p>
0
{ "code": "class Solution {\n public:\n int maxNumberOfBalloons(string text) {\n int ans = INT_MAX;\n vector<int> count(26);\n\n for (char c : text)\n ++count[c - 'a'];\n\n for (char c : string(\"ban\"))\n ans = min(ans, count[c - 'a']);\n\n for (char c : string(\"lo\"))\n ans = min(ans, count[c - 'a'] / 2);\n\n return ans;\n }\n};", "memory": "7900" }
1,297
<p>Given a string <code>text</code>, you want to use the characters of <code>text</code> to form as many instances of the word <strong>&quot;balloon&quot;</strong> as possible.</p> <p>You can use each character in <code>text</code> <strong>at most once</strong>. Return the maximum number of instances that can be formed.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/05/1536_ex1_upd.JPG" style="width: 132px; height: 35px;" /></strong></p> <pre> <strong>Input:</strong> text = &quot;nlaebolko&quot; <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/05/1536_ex2_upd.JPG" style="width: 267px; height: 35px;" /></strong></p> <pre> <strong>Input:</strong> text = &quot;loonbalxballpoon&quot; <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> text = &quot;leetcode&quot; <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= text.length &lt;= 10<sup>4</sup></code></li> <li><code>text</code> consists of lower case English letters only.</li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as <a href="https://leetcode.com/problems/rearrange-characters-to-make-target-string/description/" target="_blank"> 2287: Rearrange Characters to Make Target String.</a></p>
0
{ "code": "class Solution {\npublic:\n int maxNumberOfBalloons(string text) {\n vector<int> count(26,0);\n for(auto x: text)\n {\n count[x - 'a']++;\n }\n\n count['l' - 'a'] /= 2;\n count['o' - 'a'] /= 2;\n string balloon = \"balon\";\n int min_balloon = INT_MAX;\n\n for(int i = 0; i < balloon.size(); i++)\n {\n min_balloon = min(min_balloon,count[balloon[i]-'a']);\n }\n return min_balloon;\n }\n};", "memory": "8000" }
1,297
<p>Given a string <code>text</code>, you want to use the characters of <code>text</code> to form as many instances of the word <strong>&quot;balloon&quot;</strong> as possible.</p> <p>You can use each character in <code>text</code> <strong>at most once</strong>. Return the maximum number of instances that can be formed.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/05/1536_ex1_upd.JPG" style="width: 132px; height: 35px;" /></strong></p> <pre> <strong>Input:</strong> text = &quot;nlaebolko&quot; <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/05/1536_ex2_upd.JPG" style="width: 267px; height: 35px;" /></strong></p> <pre> <strong>Input:</strong> text = &quot;loonbalxballpoon&quot; <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> text = &quot;leetcode&quot; <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= text.length &lt;= 10<sup>4</sup></code></li> <li><code>text</code> consists of lower case English letters only.</li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as <a href="https://leetcode.com/problems/rearrange-characters-to-make-target-string/description/" target="_blank"> 2287: Rearrange Characters to Make Target String.</a></p>
0
{ "code": "class Solution {\npublic:\n int maxNumberOfBalloons(string text) {\n int letters[27];\n int ans = 0;\n\n for (char c : text) {\n letters[c - 'a'] += 1;\n }\n\n while (true) {\n letters['b' - 'a'] -= 1;\n letters['a' - 'a'] -= 1;\n letters['l' - 'a'] -= 2;\n letters['o' - 'a'] -= 2;\n letters['n' - 'a'] -= 1;\n \n if (letters['b' - 'a'] < 0) {\n break;\n }\n if (letters['a' - 'a'] < 0) {\n break;\n };\n if (letters['l' - 'a'] < 0) {\n break;\n };\n if (letters['o' - 'a'] < 0) {\n break;\n };\n if (letters['n' - 'a'] < 0) {\n break;\n }\n ans += 1;\n }\n return ans;\n }\n};", "memory": "8000" }
1,297
<p>Given a string <code>text</code>, you want to use the characters of <code>text</code> to form as many instances of the word <strong>&quot;balloon&quot;</strong> as possible.</p> <p>You can use each character in <code>text</code> <strong>at most once</strong>. Return the maximum number of instances that can be formed.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/05/1536_ex1_upd.JPG" style="width: 132px; height: 35px;" /></strong></p> <pre> <strong>Input:</strong> text = &quot;nlaebolko&quot; <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/05/1536_ex2_upd.JPG" style="width: 267px; height: 35px;" /></strong></p> <pre> <strong>Input:</strong> text = &quot;loonbalxballpoon&quot; <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> text = &quot;leetcode&quot; <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= text.length &lt;= 10<sup>4</sup></code></li> <li><code>text</code> consists of lower case English letters only.</li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as <a href="https://leetcode.com/problems/rearrange-characters-to-make-target-string/description/" target="_blank"> 2287: Rearrange Characters to Make Target String.</a></p>
0
{ "code": "class Solution {\npublic:\n int maxNumberOfBalloons(string text) {\n string targetWord = \"balon\";\n vector<pair<char, size_t>> letters;\n for ( char letter : targetWord )\n letters.push_back(make_pair(letter, 0));\n\n for ( char letter : text ) {\n this -> updateCounter(letters, letter);\n }\n\n letters[2].second = letters[2].second / 2;\n letters[3].second = letters[3].second / 2;\n\n size_t minimal = 1000000000;\n for ( auto & p : letters ) {\n if ( p.second < minimal )\n minimal = p.second;\n }\n\n return minimal;\n\n }\n\n void updateCounter( vector<pair<char, size_t>> & letters, const char letter ) {\n for ( auto & p : letters ) {\n if ( letter == p.first )\n p.second++;\n }\n }\n};", "memory": "8100" }
1,297
<p>Given a string <code>text</code>, you want to use the characters of <code>text</code> to form as many instances of the word <strong>&quot;balloon&quot;</strong> as possible.</p> <p>You can use each character in <code>text</code> <strong>at most once</strong>. Return the maximum number of instances that can be formed.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/05/1536_ex1_upd.JPG" style="width: 132px; height: 35px;" /></strong></p> <pre> <strong>Input:</strong> text = &quot;nlaebolko&quot; <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/05/1536_ex2_upd.JPG" style="width: 267px; height: 35px;" /></strong></p> <pre> <strong>Input:</strong> text = &quot;loonbalxballpoon&quot; <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> text = &quot;leetcode&quot; <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= text.length &lt;= 10<sup>4</sup></code></li> <li><code>text</code> consists of lower case English letters only.</li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as <a href="https://leetcode.com/problems/rearrange-characters-to-make-target-string/description/" target="_blank"> 2287: Rearrange Characters to Make Target String.</a></p>
0
{ "code": "class Solution {\npublic:\n int maxNumberOfBalloons(string text) {\n// string s = {\"ablloon\"};\n// sort(begin(text), end(text));\n// int cnt = 0;\n// for(int i =0 ;i<size(text);i++){\n// if(s[i] == text[i]){\n// if(i == size(s)-1 && i!= size(text)-1){\n// cnt++;\n// s = s + s;\n// }else{\n// cnt++;\n// return cnt;\n// }\n// }\n// }\n\n// return 0;\n// }\n vector<int> v(26,0);\n for(int i=0;i<text.size();i++){\n v[text[i]-'a']++;\n }\n int mini=v[1];//for b\n mini=min(v[0],mini);//for a\n mini=min(v[11]/2,mini);//for l\n mini=min(mini,v[14]/2);// for o\n mini=min(mini,v[13]);\n return mini;\n }\n};", "memory": "8100" }
1,297
<p>Given a string <code>text</code>, you want to use the characters of <code>text</code> to form as many instances of the word <strong>&quot;balloon&quot;</strong> as possible.</p> <p>You can use each character in <code>text</code> <strong>at most once</strong>. Return the maximum number of instances that can be formed.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/05/1536_ex1_upd.JPG" style="width: 132px; height: 35px;" /></strong></p> <pre> <strong>Input:</strong> text = &quot;nlaebolko&quot; <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/05/1536_ex2_upd.JPG" style="width: 267px; height: 35px;" /></strong></p> <pre> <strong>Input:</strong> text = &quot;loonbalxballpoon&quot; <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> text = &quot;leetcode&quot; <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= text.length &lt;= 10<sup>4</sup></code></li> <li><code>text</code> consists of lower case English letters only.</li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as <a href="https://leetcode.com/problems/rearrange-characters-to-make-target-string/description/" target="_blank"> 2287: Rearrange Characters to Make Target String.</a></p>
1
{ "code": "class Solution {\npublic:\n int maxNumberOfBalloons(string s) \n {\n string target = \"balloon\";\n\n vector<int>v1(26,0);\n for(int i=0;i<target.length();i++)\n {\n char ch = target[i];\n v1[ch-'a']++;\n }\n vector<int>v2(26,0);\n for(int i=0;i<s.length();i++) \n {\n char ch = s[i];\n v2[ch-'a']++;\n }\n for(int i=0;i<26;i++)\n {\n if(v1[i]==0)\n {\n continue;\n }\n else\n {\n v2[i] = v2[i] / v1[i];\n }\n }\n int ans=INT_MAX;\n unordered_set<char>st(target.begin(),target.end());\n for(auto ch : st)\n {\n int val = ch - 'a';\n ans=min(ans,v2[val]);\n }\n return ans;\n }\n};", "memory": "8200" }
1,297
<p>Given a string <code>text</code>, you want to use the characters of <code>text</code> to form as many instances of the word <strong>&quot;balloon&quot;</strong> as possible.</p> <p>You can use each character in <code>text</code> <strong>at most once</strong>. Return the maximum number of instances that can be formed.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/05/1536_ex1_upd.JPG" style="width: 132px; height: 35px;" /></strong></p> <pre> <strong>Input:</strong> text = &quot;nlaebolko&quot; <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/05/1536_ex2_upd.JPG" style="width: 267px; height: 35px;" /></strong></p> <pre> <strong>Input:</strong> text = &quot;loonbalxballpoon&quot; <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> text = &quot;leetcode&quot; <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= text.length &lt;= 10<sup>4</sup></code></li> <li><code>text</code> consists of lower case English letters only.</li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as <a href="https://leetcode.com/problems/rearrange-characters-to-make-target-string/description/" target="_blank"> 2287: Rearrange Characters to Make Target String.</a></p>
1
{ "code": "class Solution {\npublic:\n int maxNumberOfBalloons(string text) {\n string balloon = \"balloon\";\n int count = 0;\n \n while (true) {\n string sample = \"\";\n for (int i = 0; i < balloon.size(); i++) {\n size_t pos = text.find(balloon[i]);\n if (pos < text.size()) { // Check if pos is a valid index\n sample += balloon[i];\n text.erase(pos, 1);\n } else {\n return count;\n }\n }\n if (sample == balloon) {\n count++;\n }\n }\n \n return count;\n }\n};\n\n\n\n\n", "memory": "8300" }
1,297
<p>Given a string <code>text</code>, you want to use the characters of <code>text</code> to form as many instances of the word <strong>&quot;balloon&quot;</strong> as possible.</p> <p>You can use each character in <code>text</code> <strong>at most once</strong>. Return the maximum number of instances that can be formed.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/05/1536_ex1_upd.JPG" style="width: 132px; height: 35px;" /></strong></p> <pre> <strong>Input:</strong> text = &quot;nlaebolko&quot; <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/05/1536_ex2_upd.JPG" style="width: 267px; height: 35px;" /></strong></p> <pre> <strong>Input:</strong> text = &quot;loonbalxballpoon&quot; <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> text = &quot;leetcode&quot; <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= text.length &lt;= 10<sup>4</sup></code></li> <li><code>text</code> consists of lower case English letters only.</li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as <a href="https://leetcode.com/problems/rearrange-characters-to-make-target-string/description/" target="_blank"> 2287: Rearrange Characters to Make Target String.</a></p>
1
{ "code": "class Solution {\npublic:\n int countt (string s, char c) {\n return count (s.begin(), s.end(), c);\n }\n int maxNumberOfBalloons(string s) {\n int b = countt (s, 'b');\n int a = countt (s, 'a');\n int l = countt (s, 'l') / 2;\n int o = countt (s, 'o') / 2;\n int n = countt (s, 'n');\n return min (b, min(a, min(l, min(o, n))));\n }\n};", "memory": "8400" }
1,297
<p>Given a string <code>text</code>, you want to use the characters of <code>text</code> to form as many instances of the word <strong>&quot;balloon&quot;</strong> as possible.</p> <p>You can use each character in <code>text</code> <strong>at most once</strong>. Return the maximum number of instances that can be formed.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/05/1536_ex1_upd.JPG" style="width: 132px; height: 35px;" /></strong></p> <pre> <strong>Input:</strong> text = &quot;nlaebolko&quot; <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/05/1536_ex2_upd.JPG" style="width: 267px; height: 35px;" /></strong></p> <pre> <strong>Input:</strong> text = &quot;loonbalxballpoon&quot; <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> text = &quot;leetcode&quot; <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= text.length &lt;= 10<sup>4</sup></code></li> <li><code>text</code> consists of lower case English letters only.</li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as <a href="https://leetcode.com/problems/rearrange-characters-to-make-target-string/description/" target="_blank"> 2287: Rearrange Characters to Make Target String.</a></p>
1
{ "code": "class Solution {\npublic:\n int maxNumberOfBalloons(string text) {\n unordered_map<char,int> counter;\n string balloon=\"balloon\";\n\n for(char ch:text){\n if(balloon.find(ch) != string :: npos){\n counter[ch]++;\n }\n }\n\n if(counter.find('b') == counter.end() ||\n counter.find('a') == counter.end()||\n counter.find('l') == counter.end()||\n counter.find('o') == counter.end()||\n counter.find('n') == counter.end()){\n return 0;\n }\n else{\n return min({counter['b'] ,counter['a'],counter['l']/2,counter['o']/2,counter['n']});\n }\n }\n};", "memory": "8900" }
1,297
<p>Given a string <code>text</code>, you want to use the characters of <code>text</code> to form as many instances of the word <strong>&quot;balloon&quot;</strong> as possible.</p> <p>You can use each character in <code>text</code> <strong>at most once</strong>. Return the maximum number of instances that can be formed.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/05/1536_ex1_upd.JPG" style="width: 132px; height: 35px;" /></strong></p> <pre> <strong>Input:</strong> text = &quot;nlaebolko&quot; <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/05/1536_ex2_upd.JPG" style="width: 267px; height: 35px;" /></strong></p> <pre> <strong>Input:</strong> text = &quot;loonbalxballpoon&quot; <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> text = &quot;leetcode&quot; <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= text.length &lt;= 10<sup>4</sup></code></li> <li><code>text</code> consists of lower case English letters only.</li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as <a href="https://leetcode.com/problems/rearrange-characters-to-make-target-string/description/" target="_blank"> 2287: Rearrange Characters to Make Target String.</a></p>
1
{ "code": "class Solution {\npublic:\n int maxNumberOfBalloons(string text) {\n unordered_map <char,int> mp;\n for(auto x:text){\n if(x=='a'||x=='b'||x=='l'||x=='o'||x=='n')\n mp[x]++;\n }\n return min({mp['b'], mp['a'], mp['l']/2, mp['o']/2, mp['n']});\n }\n \n \n};", "memory": "9000" }
1,297
<p>Given a string <code>text</code>, you want to use the characters of <code>text</code> to form as many instances of the word <strong>&quot;balloon&quot;</strong> as possible.</p> <p>You can use each character in <code>text</code> <strong>at most once</strong>. Return the maximum number of instances that can be formed.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/05/1536_ex1_upd.JPG" style="width: 132px; height: 35px;" /></strong></p> <pre> <strong>Input:</strong> text = &quot;nlaebolko&quot; <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/05/1536_ex2_upd.JPG" style="width: 267px; height: 35px;" /></strong></p> <pre> <strong>Input:</strong> text = &quot;loonbalxballpoon&quot; <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> text = &quot;leetcode&quot; <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= text.length &lt;= 10<sup>4</sup></code></li> <li><code>text</code> consists of lower case English letters only.</li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as <a href="https://leetcode.com/problems/rearrange-characters-to-make-target-string/description/" target="_blank"> 2287: Rearrange Characters to Make Target String.</a></p>
1
{ "code": "class Solution {\npublic:\n int maxNumberOfBalloons(string text) {\n std:map<char,int> occurs;\n occurs['b']=0;\n occurs['a']=0;\n occurs['l']=0;\n occurs['l']=0;\n occurs['o']=0;\n occurs['o']=0;\n occurs['n']=0;\n\n for(auto& ch:text)\n {\n if( ch == 'b' \n ||ch == 'a'\n ||ch == 'l' \n ||ch == 'l' \n ||ch == 'o' \n ||ch == 'o' \n ||ch == 'n' \n )\n {\n occurs[ch]++;\n }\n }\n \n int smallestVal = INT_MAX;\n for(auto [key,value]:occurs)\n {\n int tmpVal = 0;\n if( (key == 'l') || (key == 'o')){\n tmpVal = value /2;\n }\n else{\n tmpVal = value;\n }\n \n if(tmpVal < smallestVal){\n smallestVal = tmpVal;\n }\n }\n \n return smallestVal;\n }\n};", "memory": "9100" }
1,297
<p>Given a string <code>text</code>, you want to use the characters of <code>text</code> to form as many instances of the word <strong>&quot;balloon&quot;</strong> as possible.</p> <p>You can use each character in <code>text</code> <strong>at most once</strong>. Return the maximum number of instances that can be formed.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/05/1536_ex1_upd.JPG" style="width: 132px; height: 35px;" /></strong></p> <pre> <strong>Input:</strong> text = &quot;nlaebolko&quot; <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/05/1536_ex2_upd.JPG" style="width: 267px; height: 35px;" /></strong></p> <pre> <strong>Input:</strong> text = &quot;loonbalxballpoon&quot; <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> text = &quot;leetcode&quot; <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= text.length &lt;= 10<sup>4</sup></code></li> <li><code>text</code> consists of lower case English letters only.</li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as <a href="https://leetcode.com/problems/rearrange-characters-to-make-target-string/description/" target="_blank"> 2287: Rearrange Characters to Make Target String.</a></p>
1
{ "code": "class Solution {\npublic:\n int maxNumberOfBalloons(string text) {\n int n = text.size();\n unordered_map<char,int>mpp;\n for(auto c:text){\n mpp[c]++;\n }\n\n int mini = INT_MAX;\n //{ b-1, a-1 , l-2, o-2 , n-1 }\n mini = min(mpp['b'],min(mpp['a'],min(mpp['l']/2,min(mpp['o']/2,mpp['n']))));\n return mini;\n }\n};", "memory": "9100" }
1,297
<p>Given a string <code>text</code>, you want to use the characters of <code>text</code> to form as many instances of the word <strong>&quot;balloon&quot;</strong> as possible.</p> <p>You can use each character in <code>text</code> <strong>at most once</strong>. Return the maximum number of instances that can be formed.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/05/1536_ex1_upd.JPG" style="width: 132px; height: 35px;" /></strong></p> <pre> <strong>Input:</strong> text = &quot;nlaebolko&quot; <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/05/1536_ex2_upd.JPG" style="width: 267px; height: 35px;" /></strong></p> <pre> <strong>Input:</strong> text = &quot;loonbalxballpoon&quot; <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> text = &quot;leetcode&quot; <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= text.length &lt;= 10<sup>4</sup></code></li> <li><code>text</code> consists of lower case English letters only.</li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as <a href="https://leetcode.com/problems/rearrange-characters-to-make-target-string/description/" target="_blank"> 2287: Rearrange Characters to Make Target String.</a></p>
1
{ "code": "class Solution {\npublic:\n // TC = O(N)\n // SC = O(1)\n int maxNumberOfBalloons(string text) {\n\n unordered_map<char,int>freq;\n\n for(char ch : text){\n if(ch =='b' || ch =='a' || ch =='l' || ch =='o' || ch =='n'){\n freq[ch]++;\n }\n }\n\n int count_b = freq['b'];\n int count_a = freq['a'];\n int count_l = freq['l']/2;\n int count_o = freq['o']/2;\n int count_n = freq['n'];\n\n return min({count_b,count_a,count_l,count_o,count_n});\n }\n};", "memory": "9200" }
1,297
<p>Given a string <code>text</code>, you want to use the characters of <code>text</code> to form as many instances of the word <strong>&quot;balloon&quot;</strong> as possible.</p> <p>You can use each character in <code>text</code> <strong>at most once</strong>. Return the maximum number of instances that can be formed.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/05/1536_ex1_upd.JPG" style="width: 132px; height: 35px;" /></strong></p> <pre> <strong>Input:</strong> text = &quot;nlaebolko&quot; <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/05/1536_ex2_upd.JPG" style="width: 267px; height: 35px;" /></strong></p> <pre> <strong>Input:</strong> text = &quot;loonbalxballpoon&quot; <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> text = &quot;leetcode&quot; <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= text.length &lt;= 10<sup>4</sup></code></li> <li><code>text</code> consists of lower case English letters only.</li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as <a href="https://leetcode.com/problems/rearrange-characters-to-make-target-string/description/" target="_blank"> 2287: Rearrange Characters to Make Target String.</a></p>
2
{ "code": "class Solution {\npublic:\n int maxNumberOfBalloons(string text) {\n unordered_map<char,int>mp1;\n unordered_map<char,int>mp2;\n int count=INT_MAX;\n string ss=\"balloon\";\n for(int i=0;i<text.size();i++){\n mp1[text[i]]++;\n }\n for(int i=0;i<ss.size();i++){\n mp2[ss[i]]++;\n }\n\n for(auto it:mp2){\n int num=(mp1[it.first]/it.second);\n count=min(count,num);\n }\n \n\n return count;\n\n\n\n }\n};", "memory": "9300" }
1,297
<p>Given a string <code>text</code>, you want to use the characters of <code>text</code> to form as many instances of the word <strong>&quot;balloon&quot;</strong> as possible.</p> <p>You can use each character in <code>text</code> <strong>at most once</strong>. Return the maximum number of instances that can be formed.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/05/1536_ex1_upd.JPG" style="width: 132px; height: 35px;" /></strong></p> <pre> <strong>Input:</strong> text = &quot;nlaebolko&quot; <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/05/1536_ex2_upd.JPG" style="width: 267px; height: 35px;" /></strong></p> <pre> <strong>Input:</strong> text = &quot;loonbalxballpoon&quot; <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> text = &quot;leetcode&quot; <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= text.length &lt;= 10<sup>4</sup></code></li> <li><code>text</code> consists of lower case English letters only.</li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as <a href="https://leetcode.com/problems/rearrange-characters-to-make-target-string/description/" target="_blank"> 2287: Rearrange Characters to Make Target String.</a></p>
2
{ "code": "class Solution {\npublic:\n int maxNumberOfBalloons(string text) {\n std:map<char,int> occurs;\n occurs['b']=0;\n occurs['a']=0;\n occurs['l']=0;\n occurs['l']=0;\n occurs['o']=0;\n occurs['o']=0;\n occurs['n']=0;\n\n for(auto& ch:text)\n {\n if( ch == 'b' \n ||ch == 'a'\n ||ch == 'l' \n ||ch == 'l' \n ||ch == 'o' \n ||ch == 'o' \n ||ch == 'n' \n )\n {\n occurs[ch]++;\n }\n }\n \n int smallestVal = INT_MAX;\n for(auto [key,value]:occurs)\n {\n int tmpVal = 0;\n if( (key == 'l') || (key == 'o'))\n {\n tmpVal = value /2;\n }\n else\n {\n tmpVal = value;\n }\n \n if(tmpVal < smallestVal)\n {\n smallestVal = tmpVal;\n }\n }\n \n return smallestVal;\n }\n};", "memory": "9300" }
2,227
<p>You are given an integer array <code>nums</code>. The <strong>range</strong> of a subarray of <code>nums</code> is the difference between the largest and smallest element in the subarray.</p> <p>Return <em>the <strong>sum of all</strong> subarray ranges of </em><code>nums</code><em>.</em></p> <p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [2], range = 2 - 2 = 0 [3], range = 3 - 3 = 0 [1,2], range = 2 - 1 = 1 [2,3], range = 3 - 2 = 1 [1,2,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [3], range = 3 - 3 = 0 [3], range = 3 - 3 = 0 [1,3], range = 3 - 1 = 2 [3,3], range = 3 - 3 = 0 [1,3,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [4,-2,-3,4,1] <strong>Output:</strong> 59 <strong>Explanation:</strong> The sum of all subarray ranges of nums is 59. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 1000</code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow-up:</strong> Could you find a solution with <code>O(n)</code> time complexity?</p>
0
{ "code": "class Solution {\npublic:\n long long subArrayRanges(vector<int>& nums) {\n long long ans=0;\n for(int i=0;i<nums.size();i++)\n {long long maxi=INT_MIN;\n long long mini=INT_MAX;\n maxi=max(maxi,nums[i]+0LL);\n mini=min(mini,nums[i]+0LL);\n for(int j=i+1;j<nums.size();j++)\n {\n maxi=max(maxi,nums[j]+0LL);\n mini=min(mini,nums[j]+0LL); \n ans+=maxi-mini; }\n }\n return ans;\n \n }\n};", "memory": "12800" }
2,227
<p>You are given an integer array <code>nums</code>. The <strong>range</strong> of a subarray of <code>nums</code> is the difference between the largest and smallest element in the subarray.</p> <p>Return <em>the <strong>sum of all</strong> subarray ranges of </em><code>nums</code><em>.</em></p> <p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [2], range = 2 - 2 = 0 [3], range = 3 - 3 = 0 [1,2], range = 2 - 1 = 1 [2,3], range = 3 - 2 = 1 [1,2,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [3], range = 3 - 3 = 0 [3], range = 3 - 3 = 0 [1,3], range = 3 - 1 = 2 [3,3], range = 3 - 3 = 0 [1,3,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [4,-2,-3,4,1] <strong>Output:</strong> 59 <strong>Explanation:</strong> The sum of all subarray ranges of nums is 59. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 1000</code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow-up:</strong> Could you find a solution with <code>O(n)</code> time complexity?</p>
0
{ "code": "class Solution {\npublic:\n long long subArrayRanges(vector<int>& arr) {\n long long ans=0;\n int n=arr.size();\n\n for(int i=0;i<n;i++){\n int Min=arr[i];\n int Max=arr[i];\n for(int j=i+1;j<n;j++){\n Min=min(Min,arr[j]);\n Max=max(Max,arr[j]);\n ans+=(Max-Min);\n }\n }\n\n return ans;\n \n }\n};", "memory": "12900" }
2,227
<p>You are given an integer array <code>nums</code>. The <strong>range</strong> of a subarray of <code>nums</code> is the difference between the largest and smallest element in the subarray.</p> <p>Return <em>the <strong>sum of all</strong> subarray ranges of </em><code>nums</code><em>.</em></p> <p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [2], range = 2 - 2 = 0 [3], range = 3 - 3 = 0 [1,2], range = 2 - 1 = 1 [2,3], range = 3 - 2 = 1 [1,2,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [3], range = 3 - 3 = 0 [3], range = 3 - 3 = 0 [1,3], range = 3 - 1 = 2 [3,3], range = 3 - 3 = 0 [1,3,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [4,-2,-3,4,1] <strong>Output:</strong> 59 <strong>Explanation:</strong> The sum of all subarray ranges of nums is 59. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 1000</code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow-up:</strong> Could you find a solution with <code>O(n)</code> time complexity?</p>
0
{ "code": "class Solution {\npublic:\n long long subArrayRanges(vector<int>& nums) {\n int mini=-1;\n int maxi=-1;\n int n=nums.size();\n long long s=0;\n for(int i=0;i<n;i++){\n maxi=nums[i];\n mini=nums[i];\n for(int j=i+1;j<n;j++){\n mini=min(nums[j],mini);\n maxi=max(nums[j],maxi);\n s=s+(maxi-mini);\n }\n }\n return s;\n }\n};", "memory": "12900" }
2,227
<p>You are given an integer array <code>nums</code>. The <strong>range</strong> of a subarray of <code>nums</code> is the difference between the largest and smallest element in the subarray.</p> <p>Return <em>the <strong>sum of all</strong> subarray ranges of </em><code>nums</code><em>.</em></p> <p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [2], range = 2 - 2 = 0 [3], range = 3 - 3 = 0 [1,2], range = 2 - 1 = 1 [2,3], range = 3 - 2 = 1 [1,2,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [3], range = 3 - 3 = 0 [3], range = 3 - 3 = 0 [1,3], range = 3 - 1 = 2 [3,3], range = 3 - 3 = 0 [1,3,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [4,-2,-3,4,1] <strong>Output:</strong> 59 <strong>Explanation:</strong> The sum of all subarray ranges of nums is 59. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 1000</code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow-up:</strong> Could you find a solution with <code>O(n)</code> time complexity?</p>
0
{ "code": "class Solution {\npublic:\n long long subArrayRanges(vector<int>& nums) {\n long long res =0;\n int n=nums.size();\n for(int i=0;i<n;i++){\n long long mn = nums[i];\n long long mx = nums[i];\n for(int j=i+1;j<n;j++){\n // mn = min(mn,nums[j]);\n // mx = max(mx,nums[j]);\n if(mx<nums[j]) mx = nums[j];\n if(mn>nums[j]) mn = nums[j];\n res = res + mx-mn;\n \n \n }\n }\n return res;\n }\n};", "memory": "13000" }
2,227
<p>You are given an integer array <code>nums</code>. The <strong>range</strong> of a subarray of <code>nums</code> is the difference between the largest and smallest element in the subarray.</p> <p>Return <em>the <strong>sum of all</strong> subarray ranges of </em><code>nums</code><em>.</em></p> <p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [2], range = 2 - 2 = 0 [3], range = 3 - 3 = 0 [1,2], range = 2 - 1 = 1 [2,3], range = 3 - 2 = 1 [1,2,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [3], range = 3 - 3 = 0 [3], range = 3 - 3 = 0 [1,3], range = 3 - 1 = 2 [3,3], range = 3 - 3 = 0 [1,3,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [4,-2,-3,4,1] <strong>Output:</strong> 59 <strong>Explanation:</strong> The sum of all subarray ranges of nums is 59. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 1000</code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow-up:</strong> Could you find a solution with <code>O(n)</code> time complexity?</p>
0
{ "code": "class Solution {\npublic:\n long long subArrayRanges(vector<int>& nums) {\n long long int sum = 0;\n int n = nums.size();\n\n for(int i=0 ; i<n ; i++){\n int largest = nums[i];\n int smallest = nums[i];\n for(int j=i+1 ; j<n ; j++){\n largest = max(largest , nums[j]);\n smallest = min(smallest , nums[j]);\n sum += (largest - smallest);\n }\n }\n return sum;\n }\n};", "memory": "13000" }
2,227
<p>You are given an integer array <code>nums</code>. The <strong>range</strong> of a subarray of <code>nums</code> is the difference between the largest and smallest element in the subarray.</p> <p>Return <em>the <strong>sum of all</strong> subarray ranges of </em><code>nums</code><em>.</em></p> <p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [2], range = 2 - 2 = 0 [3], range = 3 - 3 = 0 [1,2], range = 2 - 1 = 1 [2,3], range = 3 - 2 = 1 [1,2,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [3], range = 3 - 3 = 0 [3], range = 3 - 3 = 0 [1,3], range = 3 - 1 = 2 [3,3], range = 3 - 3 = 0 [1,3,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [4,-2,-3,4,1] <strong>Output:</strong> 59 <strong>Explanation:</strong> The sum of all subarray ranges of nums is 59. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 1000</code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow-up:</strong> Could you find a solution with <code>O(n)</code> time complexity?</p>
0
{ "code": "class Solution {\npublic:\n long long subArrayRanges(vector<int>& nums) {\n int n=nums.size();\n long long ans=0;\n for(int i=0;i<n;i++){\n int maxi=nums[i];\n int mini=nums[i];\n for(int j=i;j<n;j++){\n maxi=max(maxi,nums[j]);\n mini=min(mini,nums[j]);\n ans+=(maxi-mini);\n }\n }\n return ans;\n }\n};", "memory": "13100" }
2,227
<p>You are given an integer array <code>nums</code>. The <strong>range</strong> of a subarray of <code>nums</code> is the difference between the largest and smallest element in the subarray.</p> <p>Return <em>the <strong>sum of all</strong> subarray ranges of </em><code>nums</code><em>.</em></p> <p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [2], range = 2 - 2 = 0 [3], range = 3 - 3 = 0 [1,2], range = 2 - 1 = 1 [2,3], range = 3 - 2 = 1 [1,2,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [3], range = 3 - 3 = 0 [3], range = 3 - 3 = 0 [1,3], range = 3 - 1 = 2 [3,3], range = 3 - 3 = 0 [1,3,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [4,-2,-3,4,1] <strong>Output:</strong> 59 <strong>Explanation:</strong> The sum of all subarray ranges of nums is 59. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 1000</code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow-up:</strong> Could you find a solution with <code>O(n)</code> time complexity?</p>
0
{ "code": "class Solution {\npublic:\n long long subArrayRanges(vector<int>& nums) {\n int n=nums.size();\n long ans=0;\n for(int i=0;i<n;i++)\n {\n int minV=nums[i];\n int maxV=nums[i];\n for(int j=i;j<n;j++)\n {\n maxV=max(maxV,nums[j]);\n minV=min(minV,nums[j]);\n ans+=maxV-minV;\n }\n }\n return ans;\n }\n};", "memory": "13100" }
2,227
<p>You are given an integer array <code>nums</code>. The <strong>range</strong> of a subarray of <code>nums</code> is the difference between the largest and smallest element in the subarray.</p> <p>Return <em>the <strong>sum of all</strong> subarray ranges of </em><code>nums</code><em>.</em></p> <p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [2], range = 2 - 2 = 0 [3], range = 3 - 3 = 0 [1,2], range = 2 - 1 = 1 [2,3], range = 3 - 2 = 1 [1,2,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [3], range = 3 - 3 = 0 [3], range = 3 - 3 = 0 [1,3], range = 3 - 1 = 2 [3,3], range = 3 - 3 = 0 [1,3,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [4,-2,-3,4,1] <strong>Output:</strong> 59 <strong>Explanation:</strong> The sum of all subarray ranges of nums is 59. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 1000</code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow-up:</strong> Could you find a solution with <code>O(n)</code> time complexity?</p>
0
{ "code": "class Solution {\npublic:\n long long subArrayRanges(vector<int>& nums) {\n int n = nums.size();\n long long res = 0;\n\n for(int i=0; i<n-1; i++)\n {\n int maxi = nums[i], mini = nums[i];\n\n for(int j=i+1; j<n; j++)\n {\n if(nums[j] > maxi)\n {\n maxi = nums[j];\n }\n else if(nums[j] < mini)\n {\n mini = nums[j];\n }\n\n res += maxi-mini;\n }\n }\n return res;\n }\n};", "memory": "13200" }
2,227
<p>You are given an integer array <code>nums</code>. The <strong>range</strong> of a subarray of <code>nums</code> is the difference between the largest and smallest element in the subarray.</p> <p>Return <em>the <strong>sum of all</strong> subarray ranges of </em><code>nums</code><em>.</em></p> <p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [2], range = 2 - 2 = 0 [3], range = 3 - 3 = 0 [1,2], range = 2 - 1 = 1 [2,3], range = 3 - 2 = 1 [1,2,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [3], range = 3 - 3 = 0 [3], range = 3 - 3 = 0 [1,3], range = 3 - 1 = 2 [3,3], range = 3 - 3 = 0 [1,3,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [4,-2,-3,4,1] <strong>Output:</strong> 59 <strong>Explanation:</strong> The sum of all subarray ranges of nums is 59. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 1000</code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow-up:</strong> Could you find a solution with <code>O(n)</code> time complexity?</p>
0
{ "code": "class Solution {\npublic:\n long long subArrayRanges(vector<int>& nums) {\n long long ans = 0;\n for(int i=0; i<nums.size(); i++){\n int mini = nums[i];\n int maxi = nums[i];\n for(int j=i+1; j<nums.size(); j++){\n mini = min(mini, nums[j]);\n maxi = max(maxi, nums[j]);\n ans += (maxi-mini);\n }\n }\n return ans;\n }\n\n};", "memory": "13200" }
2,227
<p>You are given an integer array <code>nums</code>. The <strong>range</strong> of a subarray of <code>nums</code> is the difference between the largest and smallest element in the subarray.</p> <p>Return <em>the <strong>sum of all</strong> subarray ranges of </em><code>nums</code><em>.</em></p> <p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [2], range = 2 - 2 = 0 [3], range = 3 - 3 = 0 [1,2], range = 2 - 1 = 1 [2,3], range = 3 - 2 = 1 [1,2,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [3], range = 3 - 3 = 0 [3], range = 3 - 3 = 0 [1,3], range = 3 - 1 = 2 [3,3], range = 3 - 3 = 0 [1,3,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [4,-2,-3,4,1] <strong>Output:</strong> 59 <strong>Explanation:</strong> The sum of all subarray ranges of nums is 59. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 1000</code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow-up:</strong> Could you find a solution with <code>O(n)</code> time complexity?</p>
1
{ "code": "class Solution {\npublic:\n\n void findNSE(vector<int>& arr, int dir, int *res){\n // dir == 0 for left and 1 for right;\n int n = arr.size();\n stack <int> st;\n if(dir==0){\n for(int i=0; i<n; i++){\n while(!st.empty() && arr[st.top()] > arr[i])\n st.pop();\n if(!st.empty()) res[i] = st.top();\n st.push(i);\n }\n }\n else{\n for(int i=n-1; i>=0; i--){\n while(!st.empty() && arr[st.top()] >= arr[i])\n st.pop();\n if(!st.empty()) res[i] = st.top();\n st.push(i);\n }\n\n }\n }\n void findNGE(vector<int>& arr, int dir, int *res){\n int n = arr.size();\n stack<int> st;\n if(dir==0){\n for(int i=0; i<n; i++){\n while(!st.empty() && arr[st.top()] < arr[i])\n st.pop();\n if(!st.empty()) res[i] = st.top();\n st.push(i);\n }\n }\n else{\n for(int i=n-1; i>=0; i--){\n while(!st.empty() && arr[st.top()] <= arr[i])\n st.pop();\n if(!st.empty()) res[i] = st.top();\n st.push(i);\n }\n\n }\n }\n\n long long findLargestSum(vector<int>& arr){\n int n= arr.size();\n int ngr[n], ngl[n];\n \n fill(ngr, ngr+n, n);\n fill(ngl, ngl+n, -1);\n\n findNGE(arr, 0, ngl);\n findNGE(arr, 1, ngr);\n\n long long sum = 0;\n for(int i=0; i<n; i++){\n sum += ((1LL* (i-ngl[i]) * (ngr[i]-i))*arr[i]);\n }\n\n return sum;\n\n }\n long long findSmallestSum(vector<int>& arr){\n int n = arr.size();\n int nsr[n], nsl[n];\n fill(nsr, nsr+n, n);\n fill(nsl, nsl+n, -1);\n\n findNSE(arr, 0, nsl);\n findNSE(arr, 1, nsr);\n long long sum = 0;\n for(int i=0; i<n; i++){\n sum += ((1LL* (i-nsl[i]) * (nsr[i]-i))*arr[i]);\n }\n\n return sum;\n\n }\n long long subArrayRanges(vector<int>& nums) {\n return findLargestSum(nums)-findSmallestSum(nums);\n }\n};", "memory": "13700" }
2,227
<p>You are given an integer array <code>nums</code>. The <strong>range</strong> of a subarray of <code>nums</code> is the difference between the largest and smallest element in the subarray.</p> <p>Return <em>the <strong>sum of all</strong> subarray ranges of </em><code>nums</code><em>.</em></p> <p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [2], range = 2 - 2 = 0 [3], range = 3 - 3 = 0 [1,2], range = 2 - 1 = 1 [2,3], range = 3 - 2 = 1 [1,2,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [3], range = 3 - 3 = 0 [3], range = 3 - 3 = 0 [1,3], range = 3 - 1 = 2 [3,3], range = 3 - 3 = 0 [1,3,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [4,-2,-3,4,1] <strong>Output:</strong> 59 <strong>Explanation:</strong> The sum of all subarray ranges of nums is 59. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 1000</code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow-up:</strong> Could you find a solution with <code>O(n)</code> time complexity?</p>
1
{ "code": "class Solution {\npublic:\n long long subarraymaxi(vector<int>& arr)\n {\n int n=arr.size();\n long long pge[n];\n long long nge[n];\n stack<long long> st;\n for(int i=0;i<n;i++)\n {\n while(!st.empty()&&arr[i]>arr[st.top()])\n {\n st.pop();\n }\n\n if(st.empty())\n pge[i]=-1;\n else\n pge[i]=st.top();\n\n st.push(i);\n }\n\n stack<long long>st1;\n for(int i=n-1;i>=0;i--)\n {\n while(!st1.empty()&&arr[i]>=arr[st1.top()])\n {\n st1.pop();\n }\n\n if(st1.empty())\n nge[i]=n;\n else\n nge[i]=st1.top();\n\n st1.push(i);\n }\n\n long long ans=0;\n for(int i=0;i<n;i++)\n {\n long long l=i-pge[i];\n long long r=nge[i]-i;\n\n ans+=(l*r*arr[i]);\n }\n\n return ans;\n }\n\n long long subarraymini(vector<int>& arr)\n {\n int n=arr.size();\n long long pse[n];\n long long nse[n];\n stack<long long> st;\n for(int i=0;i<n;i++)\n {\n while(!st.empty()&&arr[i]<arr[st.top()])\n {\n st.pop();\n }\n\n if(st.empty())\n pse[i]=-1;\n else\n pse[i]=st.top();\n\n st.push(i);\n }\n\n stack<long long>st1;\n for(int i=n-1;i>=0;i--)\n {\n while(!st1.empty()&&arr[i]<=arr[st1.top()])\n {\n st1.pop();\n }\n\n if(st1.empty())\n nse[i]=n;\n else\n nse[i]=st1.top();\n\n st1.push(i);\n }\n\n long long ans=0;\n for(int i=0;i<n;i++)\n {\n long long l=i-pse[i];\n long long r=nse[i]-i;\n\n ans+=(l*r*arr[i]);\n }\n\n return ans;\n }\n\n long long subArrayRanges(vector<int>& nums) {\n \n return (subarraymaxi(nums)-subarraymini(nums));\n\n }\n};", "memory": "13800" }
2,227
<p>You are given an integer array <code>nums</code>. The <strong>range</strong> of a subarray of <code>nums</code> is the difference between the largest and smallest element in the subarray.</p> <p>Return <em>the <strong>sum of all</strong> subarray ranges of </em><code>nums</code><em>.</em></p> <p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [2], range = 2 - 2 = 0 [3], range = 3 - 3 = 0 [1,2], range = 2 - 1 = 1 [2,3], range = 3 - 2 = 1 [1,2,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [3], range = 3 - 3 = 0 [3], range = 3 - 3 = 0 [1,3], range = 3 - 1 = 2 [3,3], range = 3 - 3 = 0 [1,3,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [4,-2,-3,4,1] <strong>Output:</strong> 59 <strong>Explanation:</strong> The sum of all subarray ranges of nums is 59. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 1000</code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow-up:</strong> Could you find a solution with <code>O(n)</code> time complexity?</p>
1
{ "code": "class Solution {\npublic:\n long long subArrayRanges(vector<int>& nums) {\n stack<int>maxi ;\n \n \n vector<int>nextsmaller(nums.size(),-1) ;\n for (int i = nextsmaller.size()-1 ; i >= 0 ; i-- ){\n while(!maxi.empty() && nums[maxi.top()] >= nums[i] ){\n maxi.pop();\n }\n if ( maxi.empty()){\n nextsmaller[i] = nums.size();\n }\n else{\n nextsmaller[i] = maxi.top();\n }\n maxi.push(i);\n } \n while(!maxi.empty()){\n maxi.pop();\n } \n vector<int>prevsmaller(nums.size(),-1) ;\n for (int i = 0 ; i < prevsmaller.size() ; i++ ){\n while(!maxi.empty() && nums[maxi.top()] > nums[i] ){\n maxi.pop();\n }\n if ( maxi.empty()){\n prevsmaller[i] = -1 ;\n }\n else{\n prevsmaller[i] = maxi.top();\n }\n maxi.push(i);\n } \n long long maxSum = 0 ;\n for ( int i = 0 ; i < nums.size() ; i++ ){\n maxSum += 1LL * nums[i] * (i - prevsmaller[i])*(nextsmaller[i] - i ) ;\n\n } \n\n while(!maxi.empty()){\n maxi.pop();\n } \n for ( int i = 0 ; i < nextsmaller.size() ; i++ ){\n nextsmaller[i] = 0 ;\n }\n for ( int i = 0 ; i < prevsmaller.size() ; i++ ){\n prevsmaller[i] = 0 ;\n }\n for (int i = nextsmaller.size()-1 ; i >= 0 ; i-- ){\n while(!maxi.empty() && nums[maxi.top()] <= nums[i] ){\n maxi.pop();\n }\n if ( maxi.empty()){\n nextsmaller[i] = nums.size();\n }\n else{\n nextsmaller[i] = maxi.top();\n }\n maxi.push(i);\n } \n while(!maxi.empty()){\n maxi.pop();\n } \n \n for (int i = 0 ; i < prevsmaller.size() ; i++ ){\n while(!maxi.empty() && nums[maxi.top()] < nums[i] ){\n maxi.pop();\n }\n if ( maxi.empty()){\n prevsmaller[i] = -1 ;\n }\n else{\n prevsmaller[i] = maxi.top();\n }\n maxi.push(i);\n } \n long long minSum = 0 ;\n for ( int i = 0 ; i < nums.size() ; i++ ){\n minSum += 1LL* nums[i] * (i - prevsmaller[i])*(nextsmaller[i] - i ) ;\n\n } \n // return (maxSum - minSum) ;\n return minSum - maxSum ;\n \n }\n};", "memory": "13900" }
2,227
<p>You are given an integer array <code>nums</code>. The <strong>range</strong> of a subarray of <code>nums</code> is the difference between the largest and smallest element in the subarray.</p> <p>Return <em>the <strong>sum of all</strong> subarray ranges of </em><code>nums</code><em>.</em></p> <p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [2], range = 2 - 2 = 0 [3], range = 3 - 3 = 0 [1,2], range = 2 - 1 = 1 [2,3], range = 3 - 2 = 1 [1,2,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [3], range = 3 - 3 = 0 [3], range = 3 - 3 = 0 [1,3], range = 3 - 1 = 2 [3,3], range = 3 - 3 = 0 [1,3,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [4,-2,-3,4,1] <strong>Output:</strong> 59 <strong>Explanation:</strong> The sum of all subarray ranges of nums is 59. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 1000</code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow-up:</strong> Could you find a solution with <code>O(n)</code> time complexity?</p>
1
{ "code": "class Solution {\npublic:\n long long subArrayRanges(vector<int>& nums) {\n int n = nums.size();\n vector<int>nle(n,n),nge(n,n);\n stack<int>st1,st2;\n\n long long dp1[n+1], dp2[n+1], ans=0;\n dp1[n]=dp2[n]=0;\n\n for(int i=n-1;i>=0;i--){\n while(!st1.empty() && nums[st1.top()]>=nums[i]) st1.pop();\n while(!st2.empty() && nums[st2.top()]<=nums[i]) st2.pop();\n if(!st1.empty()) nle[i]=st1.top();\n if(!st2.empty()) nge[i]=st2.top();\n st1.push(i);\n st2.push(i);\n\n dp1[i] = (dp1[nle[i]] + nums[i]*1LL*(nle[i]-i));\n dp2[i] = (dp2[nge[i]] + nums[i]*1LL*(nge[i]-i));\n ans += dp2[i]-dp1[i];\n }\n\n return ans;\n }\n};", "memory": "14000" }
2,227
<p>You are given an integer array <code>nums</code>. The <strong>range</strong> of a subarray of <code>nums</code> is the difference between the largest and smallest element in the subarray.</p> <p>Return <em>the <strong>sum of all</strong> subarray ranges of </em><code>nums</code><em>.</em></p> <p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [2], range = 2 - 2 = 0 [3], range = 3 - 3 = 0 [1,2], range = 2 - 1 = 1 [2,3], range = 3 - 2 = 1 [1,2,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [3], range = 3 - 3 = 0 [3], range = 3 - 3 = 0 [1,3], range = 3 - 1 = 2 [3,3], range = 3 - 3 = 0 [1,3,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [4,-2,-3,4,1] <strong>Output:</strong> 59 <strong>Explanation:</strong> The sum of all subarray ranges of nums is 59. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 1000</code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow-up:</strong> Could you find a solution with <code>O(n)</code> time complexity?</p>
1
{ "code": "class Solution {\npublic:\n long long subArrayRanges(vector<int>& arr) {\n\n int n = arr.size();\n\n // find next smaller on next and right side\n\n vector<int> left_smaller(n, -1);\n\n vector<int> right_smaller(n, n);\n\n stack<int> st;\n\n // fill left_smaller array\n\n // find next smaller element on left side\n\n for (int i = 0; i < n; i++) {\n // remove the index with greater element than arr[i]\n\n while (!st.empty() && arr[st.top()] >= arr[i]) {\n st.pop();\n }\n\n // if stack is empty\n\n if (st.empty()) {\n left_smaller[i] = -1;\n } else {\n left_smaller[i] = st.top();\n }\n\n // push the current index into stack\n\n st.push(i);\n }\n\n while (!st.empty()) {\n st.pop();\n }\n\n // fill the right_smaller array\n\n // find the next smaller element on right side\n\n for (int i = n - 1; i >= 0; i--) {\n // remove the index with greater element than arr[i]\n\n while (!st.empty() && arr[st.top()] > arr[i]) {\n st.pop();\n }\n\n // if stack is empty\n\n if (st.empty()) {\n right_smaller[i] = n;\n } else {\n right_smaller[i] = st.top();\n }\n\n // push the current index into stack\n\n st.push(i);\n }\n\n // find next greater on left and right side\n\n // find left_greater && right_greater\n\n vector<int> left_greater(n, -1);\n\n vector<int> right_greater(n, n);\n\n while (!st.empty()) {\n st.pop();\n }\n\n // fill left_greater array\n\n // find next greater element on left side\n\n for (int i = 0; i < n; i++) {\n // remove the index with smaller element than arr[i]\n\n while (!st.empty() && arr[st.top()] <= arr[i]) {\n st.pop();\n }\n\n // if stack is empty\n\n if (st.empty()) {\n left_greater[i] = -1;\n } else {\n left_greater[i] = st.top();\n }\n\n // push the current index into stack\n\n st.push(i);\n }\n\n while (!st.empty()) {\n st.pop();\n }\n\n // fill the right_greater array\n\n // find the next greater element on right side\n\n for (int i = n - 1; i >= 0; i--) {\n // remove the index with greater element than arr[i]\n\n while (!st.empty() && arr[st.top()] < arr[i]) {\n st.pop();\n }\n\n // if stack is empty\n\n if (st.empty()) {\n right_greater[i] = n;\n } else {\n right_greater[i] = st.top();\n }\n\n // push the current index into stack\n\n st.push(i);\n }\n\n // find the sum of diff of maximum and minimum\n\n long long sum = 0;\n\n for (int i = 0; i < n; i++) {\n long long min_sum = (long long)(right_smaller[i] - i) *\n (long long)(i - left_smaller[i]) *\n (long long)arr[i];\n\n long long max_sum = (long long)(right_greater[i] - i) *\n (long long)(i - left_greater[i]) *\n (long long)arr[i];\n\n sum += (max_sum - min_sum);\n }\n\n return sum;\n }\n};", "memory": "14100" }
2,227
<p>You are given an integer array <code>nums</code>. The <strong>range</strong> of a subarray of <code>nums</code> is the difference between the largest and smallest element in the subarray.</p> <p>Return <em>the <strong>sum of all</strong> subarray ranges of </em><code>nums</code><em>.</em></p> <p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [2], range = 2 - 2 = 0 [3], range = 3 - 3 = 0 [1,2], range = 2 - 1 = 1 [2,3], range = 3 - 2 = 1 [1,2,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [3], range = 3 - 3 = 0 [3], range = 3 - 3 = 0 [1,3], range = 3 - 1 = 2 [3,3], range = 3 - 3 = 0 [1,3,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [4,-2,-3,4,1] <strong>Output:</strong> 59 <strong>Explanation:</strong> The sum of all subarray ranges of nums is 59. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 1000</code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow-up:</strong> Could you find a solution with <code>O(n)</code> time complexity?</p>
1
{ "code": "class Solution {\npublic:\n \n long long subArrayRanges(vector<int>& nums) {\n int n=nums.size();\n long long sum=0;\n stack<int>st;\n vector<int> minPrev(n,-1),minNext(n,n),maxPrev(n,-1),maxNext(n,n);\n \n for(int i=0;i<n;i++)\n {\n while(!st.empty()&&nums[st.top()]>=nums[i]){st.pop();}\n if(!st.empty()){minPrev[i]=st.top();}\n st.push(i);\n }\n while(!st.empty()){st.pop();}\n for(int i=n-1;i>=0;i--)\n {\n while(!st.empty()&&nums[st.top()]>nums[i]){st.pop();}\n if(!st.empty()){minNext[i]=st.top();}\n st.push(i);\n }\n \n for(int i=0;i<n;i++)\n {\n while(!st.empty()&&nums[st.top()]<=nums[i]){st.pop();}\n if(!st.empty()){maxPrev[i]=st.top();}\n st.push(i);\n }\n while(!st.empty()){st.pop();}\n for(int i=n-1;i>=0;i--)\n {\n while(!st.empty()&&nums[st.top()]<nums[i]){st.pop();}\n if(!st.empty()){maxNext[i]=st.top();}\n st.push(i);\n }\n \n for(int i=0;i<n;i++)\n {\n long long leftMin=i-minPrev[i],rightMin=minNext[i]-i;\n long long leftMax=i-maxPrev[i],rightMax=maxNext[i]-i;\n sum+=(leftMax*rightMax-leftMin*rightMin)*nums[i];\n \n }\n return sum;\n }\n};", "memory": "14200" }
2,227
<p>You are given an integer array <code>nums</code>. The <strong>range</strong> of a subarray of <code>nums</code> is the difference between the largest and smallest element in the subarray.</p> <p>Return <em>the <strong>sum of all</strong> subarray ranges of </em><code>nums</code><em>.</em></p> <p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [2], range = 2 - 2 = 0 [3], range = 3 - 3 = 0 [1,2], range = 2 - 1 = 1 [2,3], range = 3 - 2 = 1 [1,2,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [3], range = 3 - 3 = 0 [3], range = 3 - 3 = 0 [1,3], range = 3 - 1 = 2 [3,3], range = 3 - 3 = 0 [1,3,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [4,-2,-3,4,1] <strong>Output:</strong> 59 <strong>Explanation:</strong> The sum of all subarray ranges of nums is 59. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 1000</code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow-up:</strong> Could you find a solution with <code>O(n)</code> time complexity?</p>
1
{ "code": "class Solution {\npublic:\n \n long long subArrayRanges(vector<int>& nums) {\n int n=nums.size();\n long long sum=0;\n stack<int>st;\n vector<int> minPrev(n,-1),minNext(n,n),maxPrev(n,-1),maxNext(n,n);\n \n for(int i=0;i<n;i++)\n {\n while(!st.empty()&&nums[st.top()]>=nums[i]){st.pop();}\n if(!st.empty()){minPrev[i]=st.top();}\n st.push(i);\n }\n while(!st.empty()){st.pop();}\n for(int i=n-1;i>=0;i--)\n {\n while(!st.empty()&&nums[st.top()]>nums[i]){st.pop();}\n if(!st.empty()){minNext[i]=st.top();}\n st.push(i);\n }\n \n for(int i=0;i<n;i++)\n {\n while(!st.empty()&&nums[st.top()]<=nums[i]){st.pop();}\n if(!st.empty()){maxPrev[i]=st.top();}\n st.push(i);\n }\n while(!st.empty()){st.pop();}\n for(int i=n-1;i>=0;i--)\n {\n while(!st.empty()&&nums[st.top()]<nums[i]){st.pop();}\n if(!st.empty()){maxNext[i]=st.top();}\n st.push(i);\n }\n \n for(int i=0;i<n;i++)\n {\n long long leftMin=i-minPrev[i],rightMin=minNext[i]-i;\n long long leftMax=i-maxPrev[i],rightMax=maxNext[i]-i;\n sum+=(leftMax*rightMax-leftMin*rightMin)*nums[i];\n \n }\n return sum;\n }\n};", "memory": "14300" }
2,227
<p>You are given an integer array <code>nums</code>. The <strong>range</strong> of a subarray of <code>nums</code> is the difference between the largest and smallest element in the subarray.</p> <p>Return <em>the <strong>sum of all</strong> subarray ranges of </em><code>nums</code><em>.</em></p> <p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [2], range = 2 - 2 = 0 [3], range = 3 - 3 = 0 [1,2], range = 2 - 1 = 1 [2,3], range = 3 - 2 = 1 [1,2,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [3], range = 3 - 3 = 0 [3], range = 3 - 3 = 0 [1,3], range = 3 - 1 = 2 [3,3], range = 3 - 3 = 0 [1,3,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [4,-2,-3,4,1] <strong>Output:</strong> 59 <strong>Explanation:</strong> The sum of all subarray ranges of nums is 59. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 1000</code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow-up:</strong> Could you find a solution with <code>O(n)</code> time complexity?</p>
1
{ "code": "class Solution {\npublic:\n \n long long subArrayRanges(vector<int>& nums) {\n int n=nums.size();\n long long sum=0;\n stack<int>st;\n vector<int> minPrev(n,-1),minNext(n,n),maxPrev(n,-1),maxNext(n,n);\n \n for(int i=0;i<n;i++)\n {\n while(!st.empty()&&nums[st.top()]>=nums[i]){st.pop();}\n if(!st.empty()){minPrev[i]=st.top();}\n st.push(i);\n }\n while(!st.empty()){st.pop();}\n for(int i=n-1;i>=0;i--)\n {\n while(!st.empty()&&nums[st.top()]>nums[i]){st.pop();}\n if(!st.empty()){minNext[i]=st.top();}\n st.push(i);\n }\n \n for(int i=0;i<n;i++)\n {\n while(!st.empty()&&nums[st.top()]<=nums[i]){st.pop();}\n if(!st.empty()){maxPrev[i]=st.top();}\n st.push(i);\n }\n while(!st.empty()){st.pop();}\n for(int i=n-1;i>=0;i--)\n {\n while(!st.empty()&&nums[st.top()]<nums[i]){st.pop();}\n if(!st.empty()){maxNext[i]=st.top();}\n st.push(i);\n }\n \n for(int i=0;i<n;i++)\n {\n long long leftMin=i-minPrev[i],rightMin=minNext[i]-i;\n long long leftMax=i-maxPrev[i],rightMax=maxNext[i]-i;\n sum+=(leftMax*rightMax-leftMin*rightMin)*nums[i];\n \n }\n return sum;\n }\n};", "memory": "14400" }
2,227
<p>You are given an integer array <code>nums</code>. The <strong>range</strong> of a subarray of <code>nums</code> is the difference between the largest and smallest element in the subarray.</p> <p>Return <em>the <strong>sum of all</strong> subarray ranges of </em><code>nums</code><em>.</em></p> <p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [2], range = 2 - 2 = 0 [3], range = 3 - 3 = 0 [1,2], range = 2 - 1 = 1 [2,3], range = 3 - 2 = 1 [1,2,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [3], range = 3 - 3 = 0 [3], range = 3 - 3 = 0 [1,3], range = 3 - 1 = 2 [3,3], range = 3 - 3 = 0 [1,3,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [4,-2,-3,4,1] <strong>Output:</strong> 59 <strong>Explanation:</strong> The sum of all subarray ranges of nums is 59. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 1000</code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow-up:</strong> Could you find a solution with <code>O(n)</code> time complexity?</p>
1
{ "code": "class Solution {\npublic:\n long long subArrayRanges(vector<int>& nums) {\n int n = nums.size();\n vector<int> next_max(n, n);\n vector<int> prev_max(n, -1);\n vector<int> next_min(n, n);\n vector<int> prev_min(n, -1);\n\n stack<int> s;\n\n for (int i = 0; i < n; i++) {\n while (!s.empty() && nums[s.top()] >= nums[i]) {\n s.pop();\n }\n if (!s.empty()) {\n prev_min[i] = s.top();\n }\n s.push(i);\n }\n\n while (!s.empty()) s.pop();\n\n for (int i = n - 1; i >= 0; i--) {\n while (!s.empty() && nums[s.top()] > nums[i]) {\n s.pop();\n }\n if (!s.empty()) {\n next_min[i] = s.top();\n }\n s.push(i);\n }\n\n while (!s.empty()) s.pop();\n\n for (int i = 0; i < n; i++) {\n while (!s.empty() && nums[s.top()] <= nums[i]) {\n s.pop();\n }\n if (!s.empty()) {\n prev_max[i] = s.top();\n }\n s.push(i);\n }\n\n while (!s.empty()) s.pop();\n\n for (int i = n - 1; i >= 0; i--) {\n while (!s.empty() && nums[s.top()] < nums[i]) {\n s.pop();\n }\n if (!s.empty()) {\n next_max[i] = s.top();\n }\n s.push(i);\n }\n\n long long sum = 0;\n\n for (int i = 0; i < n; i++) {\n int leftMin = i - prev_min[i];\n int rightMin = next_min[i] - i;\n int leftMax = i - prev_max[i];\n int rightMax = next_max[i] - i;\n\n sum += (long long)(leftMax * rightMax - leftMin * rightMin) * nums[i];\n }\n\n return sum;\n }\n};\n", "memory": "14400" }
2,227
<p>You are given an integer array <code>nums</code>. The <strong>range</strong> of a subarray of <code>nums</code> is the difference between the largest and smallest element in the subarray.</p> <p>Return <em>the <strong>sum of all</strong> subarray ranges of </em><code>nums</code><em>.</em></p> <p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [2], range = 2 - 2 = 0 [3], range = 3 - 3 = 0 [1,2], range = 2 - 1 = 1 [2,3], range = 3 - 2 = 1 [1,2,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [3], range = 3 - 3 = 0 [3], range = 3 - 3 = 0 [1,3], range = 3 - 1 = 2 [3,3], range = 3 - 3 = 0 [1,3,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [4,-2,-3,4,1] <strong>Output:</strong> 59 <strong>Explanation:</strong> The sum of all subarray ranges of nums is 59. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 1000</code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow-up:</strong> Could you find a solution with <code>O(n)</code> time complexity?</p>
2
{ "code": "class Solution {\n\nprivate:\n vector<int> computeNextSmaller(vector<int>& arr) {\n int n = arr.size();\n stack<int> st;\n vector<int> nse(n);\n for (int i=n-1; i>=0; i--) {\n while (!st.empty() && arr[st.top()]>=arr[i]) {\n st.pop();\n }\n\n if (st.empty()) {\n nse[i] = n;\n } else {\n nse[i] = st.top();\n }\n st.push(i);\n }\n return nse;\n }\n\n vector<int> computePrevSmallerEqual(vector<int>& arr) {\n int n = arr.size();\n stack<int> st;\n vector<int> pse(n);\n for (int i=0; i<n; i++) {\n while (!st.empty() && arr[st.top()]>arr[i]) {\n st.pop();\n }\n\n if (st.empty()) {\n pse[i] = -1;\n } else {\n pse[i] = st.top();\n }\n st.push(i);\n }\n return pse;\n }\n\n vector<int> computeNextGreater(vector<int>& arr) {\n int n = arr.size();\n stack<int> st;\n vector<int> nge(n);\n for (int i=n-1; i>=0; i--) {\n while (!st.empty() && arr[st.top()]<arr[i]) {\n st.pop();\n }\n\n if (st.empty()) {\n nge[i] = n;\n } else {\n nge[i] = st.top();\n }\n st.push(i);\n }\n return nge;\n }\n\n vector<int> computePrevGreaterEqual(vector<int>& arr) {\n int n = arr.size();\n stack<int> st;\n vector<int> pge(n);\n for (int i=0; i<n; i++) {\n while (!st.empty() && arr[st.top()]<=arr[i]) {\n st.pop();\n }\n\n if (st.empty()) {\n pge[i] = -1;\n } else {\n pge[i] = st.top();\n }\n st.push(i);\n }\n return pge;\n }\n\npublic:\n long long subArrayRanges(vector<int>& nums) {\n int n = nums.size();\n vector<int> nse = computeNextSmaller(nums);\n vector<int> pse = computePrevSmallerEqual(nums);\n long long smallerSum = 0;\n for (int i=0; i<n; i++) { \n long long left = i - pse[i];\n long long right = nse[i] - i;\n smallerSum = (smallerSum + left*right*nums[i]);\n }\n\n vector<int> nge = computeNextGreater(nums);\n vector<int> pge = computePrevGreaterEqual(nums);\n long long greaterSum = 0;\n for (int i=0; i<n; i++) { \n long long left = i - pge[i];\n long long right = nge[i] - i;\n greaterSum = (greaterSum + left*right*nums[i]);\n }\n return greaterSum - smallerSum;\n }\n};", "memory": "14500" }
2,227
<p>You are given an integer array <code>nums</code>. The <strong>range</strong> of a subarray of <code>nums</code> is the difference between the largest and smallest element in the subarray.</p> <p>Return <em>the <strong>sum of all</strong> subarray ranges of </em><code>nums</code><em>.</em></p> <p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [2], range = 2 - 2 = 0 [3], range = 3 - 3 = 0 [1,2], range = 2 - 1 = 1 [2,3], range = 3 - 2 = 1 [1,2,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [3], range = 3 - 3 = 0 [3], range = 3 - 3 = 0 [1,3], range = 3 - 1 = 2 [3,3], range = 3 - 3 = 0 [1,3,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [4,-2,-3,4,1] <strong>Output:</strong> 59 <strong>Explanation:</strong> The sum of all subarray ranges of nums is 59. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 1000</code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow-up:</strong> Could you find a solution with <code>O(n)</code> time complexity?</p>
2
{ "code": "class Solution {\npublic:\nvector<int> findNSE(vector<int>& nums) {\n int n = nums.size();\n vector<int> nse(n);\n stack<int> st;\n for(int i=n-1; i>=0; i--) {\n while(!st.empty() && nums[st.top()] >= nums[i])\n st.pop();\n nse[i] = st.empty() ? n : st.top();\n st.push(i);\n }\n return nse;\n }\n\n vector<int> findPSEE(vector<int>& nums) {\n int n = nums.size();\n vector<int> psee(n);\n stack<int> st;\n for(int i=0; i<n; i++) {\n while(!st.empty() && nums[st.top()] > nums[i])\n st.pop();\n psee[i] = st.empty() ? -1 : st.top();\n st.push(i);\n }\n return psee;\n }\n\n long long sumSubarrayMins(vector<int>& nums) {\n vector<int> nse = findNSE(nums);\n vector<int> psee = findPSEE(nums);\n\n long long total = 0;\n for(int i=0; i<nums.size(); i++) {\n int left = i - psee[i];\n int right = nse[i] - i;\n\n total += right * left * 1LL * nums[i];\n }\n return total;\n }\n\n vector<int> findNGEE(vector<int>& nums) {\n int n = nums.size();\n vector<int> ngee(n);\n stack<int> st;\n for (int i = n-1; i>=0; i--) {\n while (!st.empty() && nums[st.top()] < nums[i])\n st.pop();\n ngee[i] = st.empty() ? n : st.top();\n st.push(i);\n }\n return ngee;\n }\n\n vector<int> findPGE(vector<int>& nums) {\n int n = nums.size();\n vector<int> pge(n);\n stack<int> st;\n for (int i=0; i<n; i++) {\n while (!st.empty() && nums[st.top()] <= nums[i])\n st.pop();\n pge[i] = st.empty() ? -1 : st.top();\n st.push(i);\n }\n return pge;\n }\n long long sumSubarrayMax(vector<int>& nums) {\n vector<int> ngee = findNGEE(nums);\n vector<int> pge = findPGE(nums);\n\n long long total = 0;\n for (int i=0; i<nums.size(); i++) {\n int left = i - pge[i];\n int right = ngee[i] - i;\n \n total += right * left * 1LL * nums[i];\n }\n return total;\n }\n\n long long subArrayRanges(vector<int>& nums) {\n long long maxSum = sumSubarrayMax(nums);\n long long minSum = sumSubarrayMins(nums);\n return maxSum - minSum;\n }\n};", "memory": "14500" }
2,227
<p>You are given an integer array <code>nums</code>. The <strong>range</strong> of a subarray of <code>nums</code> is the difference between the largest and smallest element in the subarray.</p> <p>Return <em>the <strong>sum of all</strong> subarray ranges of </em><code>nums</code><em>.</em></p> <p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [2], range = 2 - 2 = 0 [3], range = 3 - 3 = 0 [1,2], range = 2 - 1 = 1 [2,3], range = 3 - 2 = 1 [1,2,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [3], range = 3 - 3 = 0 [3], range = 3 - 3 = 0 [1,3], range = 3 - 1 = 2 [3,3], range = 3 - 3 = 0 [1,3,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [4,-2,-3,4,1] <strong>Output:</strong> 59 <strong>Explanation:</strong> The sum of all subarray ranges of nums is 59. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 1000</code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow-up:</strong> Could you find a solution with <code>O(n)</code> time complexity?</p>
2
{ "code": "class Solution {\npublic:\n void findNSE(vector<int>& arr, vector<int>& nse, int n) {\n stack<int>st;\n for(int i = n - 1;i >= 0;i--) {\n while(!st.empty() && arr[st.top()] >= arr[i]) {\n st.pop();\n }\n nse[i] = st.empty() ? n : st.top();\n st.push(i);\n }\n }\n void findPSE(vector<int>& arr, vector<int>& pse, int n) {\n stack<int>st;\n for(int i = 0;i < n;i++) {\n while(!st.empty() && arr[st.top()] > arr[i]) {\n st.pop();\n }\n pse[i] = st.empty() ? -1 : st.top();\n st.push(i);\n }\n }\n void findNGE(vector<int>& arr, vector<int>& nge, int n) {\n stack<int>st;\n for(int i = n - 1;i >= 0;i--) {\n while(!st.empty() && arr[st.top()] <= arr[i]) {\n st.pop();\n }\n nge[i] = st.empty() ? n : st.top();\n st.push(i);\n }\n }\n void findPGE(vector<int>& arr, vector<int>& pge, int n) {\n stack<int>st;\n for(int i = 0;i < n;i++) {\n while(!st.empty() && arr[st.top()] < arr[i]) {\n st.pop();\n }\n pge[i] = st.empty() ? -1 : st.top();\n st.push(i);\n }\n }\n long long subArrayRanges(vector<int>& arr) {\n long long sum = 0;\n int n = arr.size();\n vector<int> nse(n);\n vector<int> pse(n);\n vector<int> nge(n);\n vector<int> pge(n);\n findNSE(arr, nse,n);\n findPSE(arr, pse, n);\n findNGE(arr, nge,n);\n findPGE(arr, pge, n);\n long long minSum = 0;\n long long maxSum = 0;\n for(int i = 0; i < n;i++) {\n //We need to find how many times each element is contributed to subarray as a minimum\n int leftSmaller = i - pse[i];\n int rightSmaller = nse[i] - i;\n int leftlarger = i - pge[i];\n int rightLarger = nge[i] - i;\n minSum = (minSum + (leftSmaller * rightSmaller * 1ll * arr[i]));\n maxSum = (maxSum + (leftlarger * rightLarger * 1ll * arr[i]));\n }\n sum = abs (maxSum - minSum);\n return sum;\n\n }\n};", "memory": "14600" }
2,227
<p>You are given an integer array <code>nums</code>. The <strong>range</strong> of a subarray of <code>nums</code> is the difference between the largest and smallest element in the subarray.</p> <p>Return <em>the <strong>sum of all</strong> subarray ranges of </em><code>nums</code><em>.</em></p> <p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [2], range = 2 - 2 = 0 [3], range = 3 - 3 = 0 [1,2], range = 2 - 1 = 1 [2,3], range = 3 - 2 = 1 [1,2,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [3], range = 3 - 3 = 0 [3], range = 3 - 3 = 0 [1,3], range = 3 - 1 = 2 [3,3], range = 3 - 3 = 0 [1,3,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [4,-2,-3,4,1] <strong>Output:</strong> 59 <strong>Explanation:</strong> The sum of all subarray ranges of nums is 59. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 1000</code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow-up:</strong> Could you find a solution with <code>O(n)</code> time complexity?</p>
2
{ "code": "class Solution {\npublic:\nvector<int> findNSE(vector<int>& nums) {\n int n = nums.size();\n vector<int> nse(n);\n stack<int> st;\n for(int i=n-1; i>=0; i--) {\n while(!st.empty() && nums[st.top()] >= nums[i])\n st.pop();\n nse[i] = st.empty() ? n : st.top();\n st.push(i);\n }\n return nse;\n }\n\n vector<int> findPSEE(vector<int>& nums) {\n int n = nums.size();\n vector<int> psee(n);\n stack<int> st;\n for(int i=0; i<n; i++) {\n while(!st.empty() && nums[st.top()] > nums[i])\n st.pop();\n psee[i] = st.empty() ? -1 : st.top();\n st.push(i);\n }\n return psee;\n }\n\n long long sumSubarrayMins(vector<int>& nums) {\n vector<int> nse = findNSE(nums);\n vector<int> psee = findPSEE(nums);\n\n long long total = 0;\n for(int i=0; i<nums.size(); i++) {\n int left = i - psee[i];\n int right = nse[i] - i;\n\n total += right * left * 1LL * nums[i];\n }\n return total;\n }\n\n vector<int> findNGEE(vector<int>& nums) {\n int n = nums.size();\n vector<int> ngee(n);\n stack<int> st;\n for (int i = n-1; i>=0; i--) {\n while (!st.empty() && nums[st.top()] < nums[i])\n st.pop();\n ngee[i] = st.empty() ? n : st.top();\n st.push(i);\n }\n return ngee;\n }\n\n vector<int> findPGE(vector<int>& nums) {\n int n = nums.size();\n vector<int> pge(n);\n stack<int> st;\n for (int i=0; i<n; i++) {\n while (!st.empty() && nums[st.top()] <= nums[i])\n st.pop();\n pge[i] = st.empty() ? -1 : st.top();\n st.push(i);\n }\n return pge;\n }\n long long sumSubarrayMax(vector<int>& nums) {\n vector<int> ngee = findNGEE(nums);\n vector<int> pge = findPGE(nums);\n\n long long total = 0;\n for (int i=0; i<nums.size(); i++) {\n int left = i - pge[i];\n int right = ngee[i] - i;\n \n total += right * left * 1LL * nums[i];\n }\n return total;\n }\n\n long long subArrayRanges(vector<int>& nums) {\n long long maxSum = sumSubarrayMax(nums);\n long long minSum = sumSubarrayMins(nums);\n return maxSum - minSum;\n }\n};", "memory": "14600" }
2,227
<p>You are given an integer array <code>nums</code>. The <strong>range</strong> of a subarray of <code>nums</code> is the difference between the largest and smallest element in the subarray.</p> <p>Return <em>the <strong>sum of all</strong> subarray ranges of </em><code>nums</code><em>.</em></p> <p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [2], range = 2 - 2 = 0 [3], range = 3 - 3 = 0 [1,2], range = 2 - 1 = 1 [2,3], range = 3 - 2 = 1 [1,2,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [3], range = 3 - 3 = 0 [3], range = 3 - 3 = 0 [1,3], range = 3 - 1 = 2 [3,3], range = 3 - 3 = 0 [1,3,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [4,-2,-3,4,1] <strong>Output:</strong> 59 <strong>Explanation:</strong> The sum of all subarray ranges of nums is 59. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 1000</code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow-up:</strong> Could you find a solution with <code>O(n)</code> time complexity?</p>
3
{ "code": "class Solution {\npublic:\n vector<int> findNSE(vector<int>& arr) {\n int n = arr.size();\n stack<int> st;\n vector<int> nse(n);\n for (int i = n - 1; i >= 0; i--) {\n while (!st.empty() && arr[st.top()] >= arr[i]) st.pop();\n nse[i] = st.empty() ? n : st.top();\n st.push(i);\n }\n return nse;\n }\n\n vector<int> findPSE(vector<int>& arr) {\n int n = arr.size();\n stack<int> st;\n vector<int> pse(n);\n for (int i = 0; i < n; i++) {\n while (!st.empty() && arr[st.top()] > arr[i]) st.pop();\n pse[i] = st.empty() ? -1 : st.top();\n st.push(i);\n }\n return pse;\n }\n\n vector<int> findNGE(vector<int>& arr) {\n int n = arr.size();\n stack<int> st;\n vector<int> nge(n);\n for (int i = n - 1; i >= 0; i--) {\n while (!st.empty() && arr[st.top()] <= arr[i]) st.pop();\n nge[i] = st.empty() ? n : st.top();\n st.push(i);\n }\n return nge;\n }\n\n vector<int> findPGE(vector<int>& arr) {\n int n = arr.size();\n stack<int> st;\n vector<int> pge(n);\n for (int i = 0; i < n; i++) {\n while (!st.empty() && arr[st.top()] < arr[i]) st.pop();\n pge[i] = st.empty() ? -1 : st.top();\n st.push(i);\n }\n return pge;\n }\n\n long long subarrayMin(vector<int>& nums) {\n int n = nums.size();\n long long total = 0;\n vector<int> nse = findNSE(nums);\n vector<int> pse = findPSE(nums);\n for (int i = 0; i < n; i++) {\n long long left = i - pse[i];\n long long right = nse[i] - i;\n total += left * right * nums[i];\n }\n return total;\n }\n\n long long subarrayMax(vector<int>& nums) {\n int n = nums.size();\n long long total = 0;\n vector<int> nge = findNGE(nums);\n vector<int> pge = findPGE(nums);\n for (int i = 0; i < n; i++) {\n long long left = i - pge[i];\n long long right = nge[i] - i;\n total += left * right * nums[i];\n }\n return total;\n }\n\n long long subArrayRanges(vector<int>& nums) {\n return subarrayMax(nums) - subarrayMin(nums);\n }\n};", "memory": "14700" }
2,227
<p>You are given an integer array <code>nums</code>. The <strong>range</strong> of a subarray of <code>nums</code> is the difference between the largest and smallest element in the subarray.</p> <p>Return <em>the <strong>sum of all</strong> subarray ranges of </em><code>nums</code><em>.</em></p> <p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [2], range = 2 - 2 = 0 [3], range = 3 - 3 = 0 [1,2], range = 2 - 1 = 1 [2,3], range = 3 - 2 = 1 [1,2,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [3], range = 3 - 3 = 0 [3], range = 3 - 3 = 0 [1,3], range = 3 - 1 = 2 [3,3], range = 3 - 3 = 0 [1,3,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [4,-2,-3,4,1] <strong>Output:</strong> 59 <strong>Explanation:</strong> The sum of all subarray ranges of nums is 59. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 1000</code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow-up:</strong> Could you find a solution with <code>O(n)</code> time complexity?</p>
3
{ "code": "class Solution {\npublic:\n long long subArrayRanges(vector<int>& arr) {\n //BRUTE FORCE TC=O(N^2) SC=O(1)\n //Going through all SubArrays\n /*\n int mini,maxi;\n int n=arr.size();\n long long ans=0;\n for(int i=0;i<n;i++)\n {\n mini = arr[i];\n maxi = arr[i];\n for(int j=i+1;j<n;j++)\n {\n mini = min(mini,arr[j]);\n maxi = max(maxi,arr[j]);\n ans = ans + maxi - mini;\n }\n }\n return ans;\n */\n\n\n //Using (NGE, PGEE) for summation of upperbounds and (NSE, PSEE) for summation of lower bounds\n vector<int> nse = find_NSE(arr);\n vector<int> pse = find_PSE(arr);\n vector<int> nge = find_NGE(arr);\n vector<int> pge = find_PGE(arr);\n long long maxi_sum=0,mini_sum=0;\n int n=arr.size();\n for(int i=0;i<n;i++)\n {\n maxi_sum += (long long)(nge[i]-i)*(i-pge[i])*arr[i];\n mini_sum += (long long)(nse[i]-i)*(i-pse[i])*arr[i];\n }\n return maxi_sum-mini_sum;\n }\n \n\n //Return Indices not values\n vector<int> find_NSE(vector<int>&arr)\n {\n int n=arr.size();\n vector<int> ans(n);\n stack<int> st;\n for(int i=n-1;i>=0;i--)\n {\n while(!st.empty() && arr[i]<=arr[st.top()]) st.pop();\n ans[i] = st.empty()?n:st.top();\n st.push(i);\n }\n return ans;\n }\n\n //Return Indices not values\n vector<int> find_PSE(vector<int>&arr)\n {\n int n=arr.size();\n vector<int> ans(n);\n stack<int> st;\n for(int i=0;i<n;i++)\n {\n while(!st.empty() && arr[i]<arr[st.top()]) st.pop();\n ans[i] = st.empty()?-1:st.top();\n st.push(i);\n }\n return ans;\n }\n\n //Return Indices not values\n vector<int> find_NGE(vector<int>&arr)\n {\n int n=arr.size();\n vector<int> ans(n);\n stack<int> st;\n for(int i=n-1;i>=0;i--)\n {\n while(!st.empty() && arr[i]>=arr[st.top()]) st.pop();\n ans[i] = st.empty()?n:st.top();\n st.push(i);\n }\n return ans;\n }\n\n //Return Indices not values\n vector<int> find_PGE(vector<int>&arr)\n {\n int n=arr.size();\n vector<int> ans(n);\n stack<int> st;\n for(int i=0;i<n;i++)\n {\n while(!st.empty() && arr[i]>arr[st.top()]) st.pop();\n ans[i] = st.empty()?-1:st.top();\n st.push(i);\n }\n return ans;\n }\n};", "memory": "14700" }
2,227
<p>You are given an integer array <code>nums</code>. The <strong>range</strong> of a subarray of <code>nums</code> is the difference between the largest and smallest element in the subarray.</p> <p>Return <em>the <strong>sum of all</strong> subarray ranges of </em><code>nums</code><em>.</em></p> <p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [2], range = 2 - 2 = 0 [3], range = 3 - 3 = 0 [1,2], range = 2 - 1 = 1 [2,3], range = 3 - 2 = 1 [1,2,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [3], range = 3 - 3 = 0 [3], range = 3 - 3 = 0 [1,3], range = 3 - 1 = 2 [3,3], range = 3 - 3 = 0 [1,3,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [4,-2,-3,4,1] <strong>Output:</strong> 59 <strong>Explanation:</strong> The sum of all subarray ranges of nums is 59. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 1000</code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow-up:</strong> Could you find a solution with <code>O(n)</code> time complexity?</p>
3
{ "code": "class Solution {\n vector<int> findNSE(vector<int>& nums) {\n int n = nums.size();\n vector<int> nse(n);\n stack<int> st;\n for(int i=n-1; i>=0; i--) {\n while(!st.empty() && nums[st.top()] >= nums[i])\n st.pop();\n nse[i] = st.empty() ? n : st.top();\n st.push(i);\n }\n return nse;\n }\n\n vector<int> findPSEE(vector<int>& nums) {\n int n = nums.size();\n vector<int> psee(n);\n stack<int> st;\n for(int i=0; i<n; i++) {\n while(!st.empty() && nums[st.top()] > nums[i])\n st.pop();\n psee[i] = st.empty() ? -1 : st.top();\n st.push(i);\n }\n return psee;\n }\n\n long long sumSubarrayMins(vector<int>& nums) {\n vector<int> nse = findNSE(nums);\n vector<int> psee = findPSEE(nums);\n\n long long total = 0;\n for(int i=0; i<nums.size(); i++) {\n int left = i - psee[i];\n int right = nse[i] - i;\n\n total += right * left * 1LL * nums[i];\n }\n return total;\n }\n\n vector<int> findNGEE(vector<int>& nums) {\n int n = nums.size();\n vector<int> ngee(n);\n stack<int> st;\n for (int i = n-1; i>=0; i--) {\n while (!st.empty() && nums[st.top()] < nums[i])\n st.pop();\n ngee[i] = st.empty() ? n : st.top();\n st.push(i);\n }\n return ngee;\n }\n\n vector<int> findPGE(vector<int>& nums) {\n int n = nums.size();\n vector<int> pge(n);\n stack<int> st;\n for (int i=0; i<n; i++) {\n while (!st.empty() && nums[st.top()] <= nums[i])\n st.pop();\n pge[i] = st.empty() ? -1 : st.top();\n st.push(i);\n }\n return pge;\n }\n\n long long sumSubarrayMax(vector<int>& nums) {\n vector<int> ngee = findNGEE(nums);\n vector<int> pge = findPGE(nums);\n\n long long total = 0;\n for (int i=0; i<nums.size(); i++) {\n int left = i - pge[i];\n int right = ngee[i] - i;\n \n total += right * left * 1LL * nums[i];\n }\n return total;\n }\n\npublic:\n long long subArrayRanges(vector<int>& nums) {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n /* int sum=0;\n for(int i=0;i<nums.size();i++){\n int largest=nums[i],minimum=nums[i];\n for(int j=i+1;j<nums.size();j++){\n largest=max(largest,nums[j]);\n minimum=min(minimum,nums[j]);\n sum+=(largest-minimum);\n }\n }\n return sum; */\n long long maxSum = sumSubarrayMax(nums);\n long long minSum = sumSubarrayMins(nums);\n return maxSum - minSum;\n }\n};", "memory": "14800" }
2,227
<p>You are given an integer array <code>nums</code>. The <strong>range</strong> of a subarray of <code>nums</code> is the difference between the largest and smallest element in the subarray.</p> <p>Return <em>the <strong>sum of all</strong> subarray ranges of </em><code>nums</code><em>.</em></p> <p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [2], range = 2 - 2 = 0 [3], range = 3 - 3 = 0 [1,2], range = 2 - 1 = 1 [2,3], range = 3 - 2 = 1 [1,2,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [3], range = 3 - 3 = 0 [3], range = 3 - 3 = 0 [1,3], range = 3 - 1 = 2 [3,3], range = 3 - 3 = 0 [1,3,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [4,-2,-3,4,1] <strong>Output:</strong> 59 <strong>Explanation:</strong> The sum of all subarray ranges of nums is 59. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 1000</code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow-up:</strong> Could you find a solution with <code>O(n)</code> time complexity?</p>
3
{ "code": "class Solution {\npublic:\n vector<int> nslCalc(vector<int>& arr) {\n vector<int> nsl(arr.size(), -1);\n stack<int> s;\n\n for(int i = 0; i < arr.size(); i++){\n while(!s.empty() && arr[s.top()] > arr[i]){\n s.pop();\n }\n\n nsl[i] = s.empty()? -1: s.top();\n\n s.push(i);\n }\n\n return nsl;\n }\n\n vector<int> nglCalc(vector<int>& arr) {\n vector<int> ngl(arr.size(), -1);\n stack<int> s;\n\n for(int i = 0; i < arr.size(); i++){\n while(!s.empty() && arr[s.top()] < arr[i]){\n s.pop();\n }\n\n ngl[i] = s.empty()? -1: s.top();\n\n s.push(i);\n }\n\n return ngl;\n }\n\n long long subArrayRanges(vector<int>& arr) {\n vector<int> nsl = nslCalc(arr);\n vector<int> ngl = nglCalc(arr);\n vector<long> sumSmall(arr.size(), 0);\n vector<long> sumGreater(arr.size(), 0);\n \n long long ans = 0;\n\n for(int i = 0; i < arr.size(); i++){\n if(nsl[i] == -1){\n sumSmall[i] += ((i+1) * (long)arr[i]);\n }else{\n sumSmall[i] += (i - nsl[i]) * (long)arr[i] + sumSmall[nsl[i]];\n }\n\n if(ngl[i] == -1){\n sumGreater[i] += ((i+1) * (long)arr[i]);\n }else{\n sumGreater[i] += (i - ngl[i]) * (long)arr[i] + sumGreater[ngl[i]];\n }\n\n ans += sumGreater[i] - sumSmall[i];\n }\n \n return ans;\n }\n};", "memory": "14900" }
2,227
<p>You are given an integer array <code>nums</code>. The <strong>range</strong> of a subarray of <code>nums</code> is the difference between the largest and smallest element in the subarray.</p> <p>Return <em>the <strong>sum of all</strong> subarray ranges of </em><code>nums</code><em>.</em></p> <p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [2], range = 2 - 2 = 0 [3], range = 3 - 3 = 0 [1,2], range = 2 - 1 = 1 [2,3], range = 3 - 2 = 1 [1,2,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [3], range = 3 - 3 = 0 [3], range = 3 - 3 = 0 [1,3], range = 3 - 1 = 2 [3,3], range = 3 - 3 = 0 [1,3,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [4,-2,-3,4,1] <strong>Output:</strong> 59 <strong>Explanation:</strong> The sum of all subarray ranges of nums is 59. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 1000</code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow-up:</strong> Could you find a solution with <code>O(n)</code> time complexity?</p>
3
{ "code": "class Solution {\npublic:\n long long sumSubarrayMins(vector<int>& arr) {\n int n = arr.size();\n long long res = 0;\n vector<long long> left(n,-1),right(n,n);\n stack<long long> st;\n for(int i=0;i<n;i++){\n while(!st.empty() && arr[st.top()] >= arr[i]) st.pop();\n if(!st.empty()) left[i] = st.top();\n st.push(i);\n }\n while(!st.empty()) st.pop();\n for(int i=n-1;i>=0;i--){\n while(!st.empty() && arr[st.top()] > arr[i]) st.pop();\n if(!st.empty()) right[i] = st.top();\n st.push(i);\n }\n for(int i=0;i<n;i++){\n res += (i - left[i]) * (right[i] - i) * arr[i];\n }\n return res;\n }\n\n long long sumSubarrayMaxs(vector<int>& arr) {\n int n = arr.size();\n long long res = 0;\n vector<long long> left(n,-1),right(n,n);\n stack<long long> st;\n for(int i=0;i<n;i++){\n while(!st.empty() && arr[st.top()] <= arr[i]) st.pop();\n if(!st.empty()) left[i] = st.top();\n st.push(i);\n }\n while(!st.empty()) st.pop();\n for(int i=n-1;i>=0;i--){\n while(!st.empty() && arr[st.top()] < arr[i]) st.pop();\n if(!st.empty()) right[i] = st.top();\n st.push(i);\n }\n for(int i=0;i<n;i++){\n res += (i - left[i]) * (right[i] - i) * arr[i];\n }\n return res;\n }\n long long subArrayRanges(vector<int>& nums) {\n return sumSubarrayMaxs(nums) - sumSubarrayMins(nums);\n }\n};", "memory": "15000" }
2,227
<p>You are given an integer array <code>nums</code>. The <strong>range</strong> of a subarray of <code>nums</code> is the difference between the largest and smallest element in the subarray.</p> <p>Return <em>the <strong>sum of all</strong> subarray ranges of </em><code>nums</code><em>.</em></p> <p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [2], range = 2 - 2 = 0 [3], range = 3 - 3 = 0 [1,2], range = 2 - 1 = 1 [2,3], range = 3 - 2 = 1 [1,2,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [3], range = 3 - 3 = 0 [3], range = 3 - 3 = 0 [1,3], range = 3 - 1 = 2 [3,3], range = 3 - 3 = 0 [1,3,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [4,-2,-3,4,1] <strong>Output:</strong> 59 <strong>Explanation:</strong> The sum of all subarray ranges of nums is 59. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 1000</code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow-up:</strong> Could you find a solution with <code>O(n)</code> time complexity?</p>
3
{ "code": "class Solution {\npublic:\n vector<int>PS(vector<int>arr){\n int n=arr.size();\n stack<int>st;\n st.push(-1);\n vector<int>ans(n);\n for(int i=0;i<n;i++){\n while(st.size()>1 && arr[st.top()]>arr[i]){\n st.pop();\n }\n ans[i]=st.top();\n st.push(i);\n }\n return ans;\n }\n vector<int>NS(vector<int>arr){\n int n=arr.size();\n stack<int>st;\n st.push(n);\n vector<int>ans(n);\n for(int i=n-1;i>=0;i--){\n while(st.size()>1 && arr[st.top()]>=arr[i]){\n st.pop();\n }\n ans[i]=st.top();\n st.push(i);\n }\n return ans;\n }\n int sumSubarrayMins(vector<int>& arr) {\n // Hard------->\n const int mod=1e9+7;\n vector<int> pg=PS(arr);\n vector<int> ng=NS(arr);\n int n=arr.size();\n long long ans=0;\n for(int i=0;i<n;i++){\n int l=i-pg[i];\n int r=ng[i]-i;\n ans=ans+( l*r*1LL*arr[i]);\n }\n return ans;\n }\n vector<int>PG(vector<int>arr){\n int n=arr.size();\n stack<int>st;\n st.push(-1);\n vector<int>ans(n);\n for(int i=0;i<n;i++){\n while(st.size()>1 && arr[st.top()]<arr[i]){\n st.pop();\n }\n ans[i]=st.top();\n st.push(i);\n }\n return ans;\n }\n vector<int>NG(vector<int>arr){\n int n=arr.size();\n stack<int>st;\n st.push(n);\n vector<int>ans(n);\n for(int i=n-1;i>=0;i--){\n while(st.size()>1 && arr[st.top()]<=arr[i]){\n st.pop();\n }\n ans[i]=st.top();\n st.push(i);\n }\n return ans;\n }\n int sumSubarrayMaxs(vector<int>& arr) {\n // Hard------->\n const int mod=1e9+7;\n vector<int> pg=PG(arr);\n vector<int> ng=NG(arr);\n int n=arr.size();\n long long ans=0;\n for(int i=0;i<n;i++){\n int l=i-pg[i];\n int r=ng[i]-i;\n ans=ans+ l*r*1LL*arr[i];\n }\n return ans;\n }\n long long subArrayRanges(vector<int>& arr) {\n const int mod=1e9+7;\n vector<int> pg=PG(arr);\n vector<int> ng=NG(arr);\n vector<int> ps=PS(arr);\n vector<int> ns=NS(arr);\n int n=arr.size();\n long long ans=0;\n for(int i=0;i<n;i++){\n long long l1=i-pg[i];\n long long r1=ng[i]-i;\n long long l2=i-ps[i];\n long long r2=ns[i]-i;\n ans=ans+((l1*r1)-(l2*r2))*arr[i]*1LL;\n }\n return ans;\n }\n};", "memory": "15100" }
2,227
<p>You are given an integer array <code>nums</code>. The <strong>range</strong> of a subarray of <code>nums</code> is the difference between the largest and smallest element in the subarray.</p> <p>Return <em>the <strong>sum of all</strong> subarray ranges of </em><code>nums</code><em>.</em></p> <p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [2], range = 2 - 2 = 0 [3], range = 3 - 3 = 0 [1,2], range = 2 - 1 = 1 [2,3], range = 3 - 2 = 1 [1,2,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [3], range = 3 - 3 = 0 [3], range = 3 - 3 = 0 [1,3], range = 3 - 1 = 2 [3,3], range = 3 - 3 = 0 [1,3,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [4,-2,-3,4,1] <strong>Output:</strong> 59 <strong>Explanation:</strong> The sum of all subarray ranges of nums is 59. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 1000</code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow-up:</strong> Could you find a solution with <code>O(n)</code> time complexity?</p>
3
{ "code": "class Solution {\npublic:\n ;\n long long solve2(vector<int>& arr) {\n vector<long long>v(arr.size(),0);\n stack<int>st;\n long long ans=0;\n int n=arr.size();\n v[n-1]=n;\n st.push(n-1);\n for(int i=n-2;i>=0;i--){\n while(!st.empty() && arr[st.top()]>arr[i])st.pop();\n if(st.empty())v[i]=n;\n else v[i]=st.top();\n st.push(i);\n \n }\n vector<long long>v2(arr.size(),0);\n while(!st.empty())st.pop();\n v2[0]=-1;\n st.push(0);\n for(int i=1;i<n;i++){\n while(!st.empty() && arr[st.top()]>=arr[i])st.pop();\n if(st.empty())v2[i]=-1;\n else v2[i]=st.top();st.push(i);\n \n }\n for(int i=0;i<n;i++){\n ans+=((long long)(i-v2[i])*(v[i]-i)*arr[i]);\n }\n return ans;\n\n\n\n \n }\n long long solve1(vector<int>& arr) {\n vector<long long>v(arr.size(),0);\n stack<int>st;\n long long ans=0;\n int n=arr.size();\n v[n-1]=n;\n st.push(n-1);\n for(int i=n-2;i>=0;i--){\n while(!st.empty() && arr[st.top()]<arr[i])st.pop();\n if(st.empty())v[i]=n;\n else v[i]=st.top();\n st.push(i);\n \n }\n vector<long long>v2(arr.size(),0);\n while(!st.empty())st.pop();\n v2[0]=-1;\n st.push(0);\n for(int i=1;i<n;i++){\n while(!st.empty() && arr[st.top()]<=arr[i])st.pop();\n if(st.empty())v2[i]=-1;\n else v2[i]=st.top();st.push(i);\n \n }\n for(int i=0;i<n;i++){\n ans+=((long long)(i-v2[i])*(v[i]-i)*arr[i]);\n }\n return ans;\n\n\n\n \n }\n long long subArrayRanges(vector<int>& nums) {\n return (long long)solve1(nums)-(long long)solve2(nums);\n \n }\n};", "memory": "15100" }
2,227
<p>You are given an integer array <code>nums</code>. The <strong>range</strong> of a subarray of <code>nums</code> is the difference between the largest and smallest element in the subarray.</p> <p>Return <em>the <strong>sum of all</strong> subarray ranges of </em><code>nums</code><em>.</em></p> <p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [2], range = 2 - 2 = 0 [3], range = 3 - 3 = 0 [1,2], range = 2 - 1 = 1 [2,3], range = 3 - 2 = 1 [1,2,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [3], range = 3 - 3 = 0 [3], range = 3 - 3 = 0 [1,3], range = 3 - 1 = 2 [3,3], range = 3 - 3 = 0 [1,3,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [4,-2,-3,4,1] <strong>Output:</strong> 59 <strong>Explanation:</strong> The sum of all subarray ranges of nums is 59. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 1000</code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow-up:</strong> Could you find a solution with <code>O(n)</code> time complexity?</p>
3
{ "code": "class Solution {\npublic:\n\nvector<int> PGE(vector<int> a)\n{\n int n=a.size();\n stack<int> s;\n vector<int> pge(n);\n for(int i=0;i<n;i++)\n {\n while(!s.empty() && a[s.top()]<=a[i])\n {\n s.pop();\n }\n pge[i]=s.empty()?-1:s.top();\n s.push(i);\n }\n return pge;\n}\nvector<int> NGE(vector<int> a)\n{\n int n=a.size();\n stack<int> s;\n vector<int> nge(n);\n for(int i=n-1;i>=0;i--)\n {\n while(!s.empty() && a[s.top()]<a[i])\n {\n s.pop();\n }\n nge[i]=s.empty()?n:s.top();\n s.push(i);\n }\n return nge;\n}\n\nvector<int> PSE(vector<int> a)\n{\n int n=a.size();\n stack<int> s;\n vector<int> pse(n);\n for(int i=0;i<n;i++)\n {\n while(!s.empty() && a[s.top()]>a[i])\n {\n s.pop();\n }\n pse[i]=s.empty()?-1:s.top();\n s.push(i);\n }\n return pse;\n}\n\nvector<int> NSE(vector<int> a)\n{\n int n=a.size();\n stack<int> s;\n vector<int> nse(n);\n for(int i=n-1;i>=0;i--)\n {\n while(!s.empty() && a[s.top()]>=a[i])\n {\n s.pop();\n }\n nse[i]=s.empty()?n:s.top();\n s.push(i);\n }\n return nse;\n}\n\n long long subArrayRanges(vector<int>& nums) {\n int n=nums.size();\n vector<int> pse=PSE(nums);\n vector<int> nse=NSE(nums);\n vector<int> pge=PGE(nums);\n vector<int> nge=NGE(nums);\n long long mini=0;\n long long maxi=0;\n for(int i=0;i<n;i++)\n {\n long long left=i-pse[i];\n long long right=nse[i]-i;\n mini+=(left*right)*nums[i];\n }\n for(int i=0;i<n;i++)\n {\n long long left=i-pge[i];\n long long right=nge[i]-i;\n maxi+=(left*right)*nums[i];\n }\n return maxi-mini;\n }\n};", "memory": "15200" }
2,227
<p>You are given an integer array <code>nums</code>. The <strong>range</strong> of a subarray of <code>nums</code> is the difference between the largest and smallest element in the subarray.</p> <p>Return <em>the <strong>sum of all</strong> subarray ranges of </em><code>nums</code><em>.</em></p> <p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [2], range = 2 - 2 = 0 [3], range = 3 - 3 = 0 [1,2], range = 2 - 1 = 1 [2,3], range = 3 - 2 = 1 [1,2,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [3], range = 3 - 3 = 0 [3], range = 3 - 3 = 0 [1,3], range = 3 - 1 = 2 [3,3], range = 3 - 3 = 0 [1,3,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [4,-2,-3,4,1] <strong>Output:</strong> 59 <strong>Explanation:</strong> The sum of all subarray ranges of nums is 59. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 1000</code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow-up:</strong> Could you find a solution with <code>O(n)</code> time complexity?</p>
3
{ "code": "class Solution {\npublic:\nvector<int>findnse(vector<int>&arr){\n int n=arr.size();\n vector<int>nse(n);\n stack<int>st;\n for(int i=n-1;i>=0;i--){\n while(!st.empty()&&arr[st.top()]>=arr[i]){\n st.pop();\n\n }\n nse[i]=st.empty()?n:st.top();\n st.push(i);\n }\n return nse;\n}\nvector<int>findpse(vector<int>&arr){\n int n=arr.size();\n vector<int>pse(n);\n stack<int>st;\n for(int i=0;i<n;i++){\n while(!st.empty()&&arr[st.top()]>arr[i]){\n st.pop();\n\n }\n pse[i]=st.empty()?-1:st.top();\n st.push(i);\n }\n return pse;\n}\nlong long sum_min(vector<int>&arr){\n long long total=0;\n int n=arr.size();\n vector<int>pse(n);\n vector<int>nse(n);\n nse=findnse(arr);\n pse=findpse(arr);\n for(int i=0;i<n;i++){\n \n long long left=i-pse[i];\n long long right=nse[i]-i;\n total=(total+(right*left*arr[i]));\n }\n return total;\n}\nvector<int>findnge(vector<int>&arr){\n int n=arr.size();\n vector<int>nge(n);\n stack<int>st;\n for(int i=n-1;i>=0;i--){\n while(!st.empty()&&arr[st.top()]<=arr[i]){\n st.pop();\n\n }\n nge[i]=st.empty()?n:st.top();\n st.push(i);\n }\n return nge;\n}\nvector<int>findpge(vector<int>&arr){\n int n=arr.size();\n vector<int>pge(n);\n stack<int>st;\n for(int i=0;i<n;i++){\n while(!st.empty()&&arr[st.top()]<arr[i]){\n st.pop();\n\n }\n pge[i]=st.empty()?-1:st.top();\n st.push(i);\n }\n return pge;\n}\nlong long sum_max(vector<int>&arr){\n long long total=0;\n int n=arr.size();\n vector<int>pge(n);\n vector<int>nge(n);\n nge=findnge(arr);\n pge=findpge(arr);\n for(int i=0;i<n;i++){\n \n long long left=i-pge[i];\n long long right=nge[i]-i;\n total=(total+(right*left*arr[i]));\n }\n return total;\n}\n long long subArrayRanges(vector<int>& nums) {\n return sum_max(nums)-sum_min(nums);\n }\n};", "memory": "15300" }
2,227
<p>You are given an integer array <code>nums</code>. The <strong>range</strong> of a subarray of <code>nums</code> is the difference between the largest and smallest element in the subarray.</p> <p>Return <em>the <strong>sum of all</strong> subarray ranges of </em><code>nums</code><em>.</em></p> <p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [2], range = 2 - 2 = 0 [3], range = 3 - 3 = 0 [1,2], range = 2 - 1 = 1 [2,3], range = 3 - 2 = 1 [1,2,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,3] <strong>Output:</strong> 4 <strong>Explanation:</strong> The 6 subarrays of nums are the following: [1], range = largest - smallest = 1 - 1 = 0 [3], range = 3 - 3 = 0 [3], range = 3 - 3 = 0 [1,3], range = 3 - 1 = 2 [3,3], range = 3 - 3 = 0 [1,3,3], range = 3 - 1 = 2 So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [4,-2,-3,4,1] <strong>Output:</strong> 59 <strong>Explanation:</strong> The sum of all subarray ranges of nums is 59. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 1000</code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow-up:</strong> Could you find a solution with <code>O(n)</code> time complexity?</p>
3
{ "code": "class Solution {\n long long amin(vector<int>& arr){\n int n = arr.size();\n stack<long long>s1,s2;\n vector<long long>left(n),right(n);\n long long ans=0;\n\n for(int i=0;i<n;i++){\n right[i]=n-i-1;\n left[i]=i;\n }\n\n for(int i=0;i<n;i++){\n while(!s1.empty() && arr[s1.top()]>arr[i]){ // why here equal to sign is not applicable\n right[s1.top()]=i-s1.top()-1; // it denotes lenght at which next min occur\n s1.pop();\n }\n s1.push(i);\n }\n\n for(int i=n-1;i>=0;i--){\n while(!s2.empty() && arr[s2.top()]>=arr[i]){ // why here equal to sign is applicable\n left[s2.top()]=s2.top()-i-1;\n s2.pop();\n }\n s2.push(i);\n }\n\n for(int i=0;i<n;i++){\n ans= (ans +(1ll*arr[i]*(left[i]+1)*(right[i]+1)));\n }\n return ans; \n }\n\n\n long long amax(vector<int>& arr){\n int n = arr.size();\n stack<long long>s1,s2;\n vector<long long>left(n),right(n);\n long long ans=0;\n\n for(int i=0;i<n;i++){\n right[i]=n-i-1;\n left[i]=i;\n }\n\n for(int i=0;i<n;i++){\n while(!s1.empty() && arr[s1.top()]<arr[i]){\n right[s1.top()]=i-s1.top()-1;\n s1.pop();\n }\n s1.push(i);\n }\n\n for(int i=n-1;i>=0;i--){\n while(!s2.empty() && arr[s2.top()]<=arr[i]){\n left[s2.top()]=s2.top()-i-1;\n s2.pop();\n }\n s2.push(i);\n }\n\n for(int i=0;i<n;i++){\n ans= (ans +(1ll*arr[i]*(left[i]+1)*(right[i]+1)));\n }\n return ans; \n }\npublic:\n long long subArrayRanges(vector<int>& nums) {\n \n long long ans1=amin(nums);\n long long ans2=amax(nums);\n\n return ans2-ans1;\n }\n};", "memory": "15300" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n string firstPalindrome(vector<string>& words) {\n for(auto& word : words){\n if(isPalin(word)) {\n return word;\n }\n }\n return \"\";\n }\n bool isPalin(string& word){\n int i = 0, j = word.size()-1;\n while(i < j){\n if(word[i++] != word[j--]) return false;\n }\n return true;\n }\n};", "memory": "23400" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n string firstPalindrome(vector<string>& words) {\n bool flag=true;\n string word=\"\";\n for(int i=0;i<words.size();i++){\n bool flag=true;\n for(int j=0;j<= words[i].size()/2;j++){\n if(words[i][j]!=words[i][words[i].size()-1-j]){\n flag=false;\n break;\n }\n }\n if(flag){\n word=words[i];\n break;\n }\n }\n return word;\n }\n};", "memory": "23500" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n string firstPalindrome(vector<string>& words) {\n for (string const& s : words) {\n if (is_palindrome(s)) {return s;}\n }\n return {};\n }\n\n bool is_palindrome(string_view v) {\n int i{0}, j{static_cast<int>(v.size()) - 1};\n while (i < j) {\n if (v[i++] != v[j--]) {return false;}\n }\n return true;\n }\n};", "memory": "23600" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n string firstPalindrome(vector<string>& words) {\n for (string const& s : words) {\n if (is_palindrome(s)) {return s;}\n }\n return {};\n }\n\n bool is_palindrome(string_view v) {\n int i{0}, j{static_cast<int>(v.size()) - 1};\n while (i < j) {\n if (v[i++] != v[j--]) {return false;}\n }\n return true;\n }\n};", "memory": "23600" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n string firstPalindrome(vector<string>& words) {\n for(int i=0;i<words.size();i++)\n {\n int l=0,h = words[i].length()-1;\n bool ch = false;\n while(l<h)\n {\n if(words[i][l] == words[i][h])\n {\n l++;\n h--;\n }\n else \n {\n ch = true;\n break;\n }\n }\n if(ch == false)return words[i];\n }\n return \"\";\n }\n};", "memory": "23700" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string firstPalindrome(vector<string>& words) {\n string t=\"\";\n for(int i=0; i<words.size(); i++){\n t=words[i];\n reverse(t.begin(), t.end());\n if(words[i][0]==words[i][words[i].size()-1]){\n if(t==words[i])\n return t;\n }\n }\n return \"\";\n }\n};", "memory": "23800" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string firstPalindrome(vector<string>& words) {\n for(int i = 0; i < words.size(); ++i){\n int l = 0, r = words[i].size() - 1;\n int checker = 1;\n while(l<=r){\n if(words[i][l] != words[i][r]){\n checker = 0;\n }\n l++; r--;\n }\n if(checker==1){\n return words[i];\n }\n }\n return \"\";\n }\n};", "memory": "23800" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string firstPalindrome(vector<string>& words) {\n string str=\"\";\n for(int i=0; i<words.size(); i++){\n str=words[i];\n bool flag=true;\n int m=0,n=str.length()-1;\n while(m<n){\n if(str[m]!= str[n]){\n flag=false;\n break;\n }\n m++; n--;\n }\n if(flag) return str;\n }\n return \"\";\n }\n};", "memory": "23900" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string firstPalindrome(vector<string>& words) {\n vector<string> ans;\n int n=words.size();\n string s=\"\";\n for(int i=0;i<n;i++){\n s=words[i];\n reverse(s.begin(),s.end());\n if(s==words[i]){\n return words[i];\n }\n\n }\n return \"\";\n }\n};", "memory": "24000" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string firstPalindrome(vector<string>& words) {\n for(int i=0;i<words.size();i++)\n {\n int size = words[i].size();\n if(words[i][0]==words[i][size-1])\n {\n string word = words[i];\n int k = 0;\n int l = word.size() - 1;\n int flag = 0;\n while(k<l)\n {\n if(word[k]!=word[l])\n {\n flag = 1;\n break;\n }\n k++;\n l--;\n }\n if(!flag)\n {\n return words[i];\n }\n }\n } \n return \"\"; \n }\n};", "memory": "25300" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool ispalindrome(string s)\n {\n int i=0;\n int j = s.size()-1;\n while(i<=j)\n {\n if(s[i]!=s[j])return false;\n i++;\n j--;\n }\n return true;\n }\n string firstPalindrome(vector<string>& words) {\n \n\n int n = words.size();\n\n for(int i=0;i<n;i++)\n {\n if(ispalindrome(words[i])==true)return words[i];\n }\n\n return \"\";\n }\n};", "memory": "26600" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool checker(string s){\n int l = 0;\n int r = s.size() - 1;\n\n while(l<=r){\n if(s[l] != s[r]){\n return false;\n }\n l++;\n r--;\n }\n return true;\n\n }\n \n string firstPalindrome(vector<string>& words) {\n for(int i = 0; i < words.size(); i++){\n if(checker(words[i])){\n return words[i];\n }\n\n }\n return \"\";\n }\n};", "memory": "26700" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string firstPalindrome(vector<string>& words) {\n for(string s : words){\n if(peli(0, s.size(), s)){\n return s;\n }\n }\n return \"\";\n }\n\nprivate:\n bool peli(int i, int n, const string& s){\n if(i>= n/2){\n return true;\n }\n if(s[i] != s[n-i-1]){\n return false;\n }\n return peli(i+1, n, s);\n }\n};\n\n", "memory": "26800" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string firstPalindrome(vector<string>& words) {\n for (string& word : words)\n {\n string reversed = word;\n reverse(reversed.begin(), reversed.end());\n if (reversed == word) return word;\n }\n return \"\";\n }\n};", "memory": "26800" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string firstPalindrome(vector<string>& words) {\n for(string s : words){\n if(peli(0, s.size(), s)){\n return s;\n }\n }\n return \"\";\n }\n\nprivate:\n bool peli(int i, int n, const string& s){\n if(i>= n/2){\n return true;\n }\n if(s[i] != s[n-i-1]){\n return false;\n }\n return peli(i+1, n, s);\n }\n};\n\n", "memory": "26900" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n\n bool isPalin(string &s){\n int st=0, en=s.size()-1;\n\n while(st<en){\n if(s[st]!=s[en]) return false;\n st++;\n en--;\n }\n\n return true;\n }\n\n string firstPalindrome(vector<string>& words) {\n for(auto word:words){\n if(isPalin(word)){\n return word;\n }\n }\n return \"\";\n }\n};", "memory": "26900" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string firstPalindrome(vector<string>& words) {\n \n for(int i = 0; i < words.size(); i++){\n string curr = words[i];\n reverse(curr.begin(), curr.end());\n if(words[i] == curr){\n return words[i];\n }\n }\n return \"\";\n }\n};", "memory": "27000" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string firstPalindrome(vector<string>& words) \n {\n for(int i=0;i<words.size();i++)\n {\n string res=words[i];\n reverse(res.begin(),res.end());\n if(res==words[i])\n {\n return res;\n break;\n }\n } \n return \"\";\n }\n};", "memory": "27000" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string firstPalindrome(vector<string>& words) {\n string s=\"\";\n for(auto it: words)\n {\n s=it;\n reverse(s.begin(),s.end());\n if(it==s)\n {\n return s;\n }\n }\n s=\"\";\n return s;\n }\n};", "memory": "27100" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string firstPalindrome(vector<string>& words) {\n string ans;\n string check;\n stack<int>s;\n\n\n for(int i=0;i<words.size();i++)\n {\n string temp=words[i];\n reverse(words[i].begin(),words[i].end());\n if(temp==words[i])\n {\n ans=words[i];\n break;\n }\n\n }\n\n return ans;\n }\n};", "memory": "27300" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string firstPalindrome(vector<string>& words) {\n for (const string& word : words) {\n if (word == string(word.rbegin(), word.rend())) {\n return word;\n }\n }\n return \"\";\n }\n};", "memory": "27800" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string firstPalindrome(vector<string>& words) {\n for(string &word : words)\n {\n if(word == string(rbegin(word), rend(word)))\n {\n return word;\n }\n }\n return \"\";\n }\n};", "memory": "27900" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string firstPalindrome(vector<string>& words) {\n for(int i=0; i<words.size(); i++){\n if(words[i]== string(words[i].rbegin(), words[i].rend())){\n return words[i];\n }\n }\n return \"\";\n }\n};", "memory": "28000" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string firstPalindrome(vector<string>& words) \n {\n vector<string> grid=words;\n\n for(int i=0;i<words.size();i++)\n {\n reverse(words[i].begin(),words[i].end());\n if(words[i]==grid[i])\n return words[i];\n } \n return \"\";\n }\n};", "memory": "28100" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n\n bool check_palindrome(string &strInput) {\n int halfway = (strInput.length() / 2);\n int full_length = strInput.length() - 1;\n int i = 0;\n bool isPalindrome = true;\n while (i <= halfway) {\n // cout<<\"i-th letter is \"<<strInput[i]<<\" and full-minus-i is \"<<strInput[full_length -i]<<endl;\n if (strInput[i] == strInput[full_length - i]) {\n isPalindrome = true;\n }\n else {\n isPalindrome = false;\n break;\n }\n i++;\n }\n return isPalindrome;\n }\n\n string firstPalindrome(vector<string> list_of_strings) {\n int length_of_list = list_of_strings.size();\n bool isPalindrome = false;\n for (int i=0; i<length_of_list; i++) {\n isPalindrome = check_palindrome(list_of_strings[i]);\n if (isPalindrome) return list_of_strings[i];\n }\n return \"\";\n }\n\n};", "memory": "28200" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n Solution() { ios_base::sync_with_stdio(false); cin.tie(NULL); }\n \n auto firstPalindrome(vector<string> words) -> string{\n auto const& result = find_if(cbegin(words), cend(words), [](auto const &word){\n return equal(cbegin(word), cbegin(word) + size(word) / 2, crbegin(word));\n });\n return result == cend(words) ? \"\" : *result;\n }\n};", "memory": "28200" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string firstPalindrome(vector<string> words) {\n for (const auto& word : words) {\n if (isPalindrome(word)) {\n return word;\n }\n }\n return \"\";\n }\n\nprivate:\n bool isPalindrome(const std::string& str) {\n int left = 0;\n int right = str.length() - 1;\n while (left < right) {\n if (str[left] != str[right]) {\n return false;\n }\n left++;\n right--;\n }\n return true;\n }\n};\n", "memory": "28300" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string firstPalindrome(vector<string>& words) {\n queue<string> st(words.begin(), words.end());\n \n string s = \"\";\n for(int i=0; i<words.size(); i++){\n reverse(words[i].begin(), words[i].end());\n if(words[i] == st.front()){\n \n s += words[i];\n break;\n \n }\n st.pop();\n }\n return s; \n }\n};\n ", "memory": "28400" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string firstPalindrome(vector<string>& words) {\n queue<string> st(words.begin(), words.end());\n \n string s = \"\";\n for(int i=0; i<words.size(); i++){\n reverse(words[i].begin(), words[i].end());\n if(words[i] == st.front()){\n \n s += words[i];\n break;\n \n }\n st.pop();\n }\n return s; \n }\n};\n ", "memory": "28500" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string firstPalindrome(vector<string>& words) {\n int n = words.size();\n for(int i=0; i<n; i++){\n string x = words[i];\n if(x[0]==x[x.length()-1]){\n string y = x;\n reverse(x.begin(),x.end());\n if(x==y){\n return y;\n break;\n }\n } \n }\n return \"\";\n }\n};", "memory": "28600" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string firstPalindrome(vector<string>& words) {\n int n = words.size();\n for(int i=0; i<n; i++){\n string x = words[i];\n if(x[0]==x[x.length()-1]){\n string y = x;\n reverse(x.begin(),x.end());\n if(x==y){\n return y;\n break;\n }\n } \n }\n return \"\";\n }\n};", "memory": "28600" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string firstPalindrome(vector<string>& words) {\n for(int i = 0; i < words.size(); i++){\n int l = 0, r = words[i].length() - 1;\n string str1, str2;\n while(l < r){\n str1.push_back(words[i][l++]);\n str2.push_back(words[i][r--]);\n }\n if(str1 == str2){\n return words[i];\n }\n }\n return \"\";\n }\n};", "memory": "29400" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string firstPalindrome(vector<string>& words) {\n vector<string> n;\n for(int i=0;i<words.size();i++){\n n.push_back(words[i]);\n }\n for(int i=0;i<words.size();i++){\n reverse(words[i].begin(),words[i].end());\n }\n int count=0;\n for(int i=0;i<words.size();i++){\n if(words[i]==n[i]){\n count = i+1;\n break;\n }\n \n }\n if(count == 0){\n return \"\";\n }\n return words[count-1];\n }\n};", "memory": "29500" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string firstPalindrome(vector<string>& words) {\n string ans;\n vector<string> s;\n for(int i =0;i<size(words);i++)\n {\n s.push_back(words[i]);\n reverse(words[i].begin(),words[i].end());\n \n } \n for(int i =0;i<size(words);i++)\n {\n if(s[i].compare(words[i]) == 0)\n { ans = s[i];\n break; } \n }\n return ans; \n } \n};", "memory": "29600" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string firstPalindrome(vector<string>& words) {\n\n int n=words.size();\n string s;\n string ans;\n vector<string> v;\n for(int i=0;i<n;i++){\n s=words[i];\n reverse(s.begin(),s.end());\n v.push_back(s);\n }\n\n for(int i=0;i<n;i++){\n if(v[i]==words[i]){\n ans+=v[i];\n break;\n }\n }\n\n return ans;;\n }\n};", "memory": "29700" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string firstPalindrome(vector<string>& words) {\n\n int n=words.size();\n string s;\n string ans;\n vector<string> v;\n for(int i=0;i<n;i++){\n s=words[i];\n reverse(s.begin(),s.end());\n v.push_back(s);\n }\n\n for(int i=0;i<n;i++){\n if(v[i]==words[i]){\n ans+=v[i];\n break;\n }\n }\n\n return ans;;\n }\n};", "memory": "29800" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\n bool ispalindrom(string s)\n {\n bool b = true;\n int n = s.size();\n for(int i = 0; i < n / 2; ++i)\n b &= (s[i] == s[n - i - 1]);\n return b;\n }\npublic:\n string firstPalindrome(vector<string>& words) {\n for(string s : words)\n if(ispalindrom(s))\n return s;\n return string(\"\");\n }\n};", "memory": "29900" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool isPalindrome(string s){\n int n = s.size();\n int i = 0;\n while(i<n/2){\n\n if(s[i] != s[n-i-1]) return false;\n i++;\n }\n\n return true;\n }\n string firstPalindrome(vector<string>& words) {\n \n for(string s : words){\n if(isPalindrome(s)) return s;\n }\n\n return \"\";\n }\n};", "memory": "30000" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool isPalindrome(string s){\n int l = 0, r = s.size()-1;\n while(r >= l){\n if (s[r] != s[l]){\n return false;\n }\n\n l++;\n r--;\n }\n\n return true;\n }\n string firstPalindrome(vector<string>& words) {\n for (auto s: words){\n if (isPalindrome(s)){\n return s;\n }\n }\n\n return \"\";\n }\n};", "memory": "30000" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\nbool ispalindromic(string s ){\n int left = 0 ; \n int right = s.size() - 1 ;\n while( left < right ){\n if(s[left] != s[right]){\n return false ;\n }\n left++;\n right--;\n }\n return true;\n}\n string firstPalindrome(vector<string>& words) {\n for(int i = 0 ; i < words.size() ; i++ ){\n string s = words[i];\n if(ispalindromic(s)) {\n return s;\n }\n }\n return \"\";\n }\n};", "memory": "30100" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string firstPalindrome(vector<string>& words) {\n for(auto ss : words){\n string answer = ss;\n int l = 0;\n int r = ss.size() - 1;\n bool isPalindrome = true;\n \n while(l <= r){\n if(ss[l] != ss[r]){\n isPalindrome = false;\n break;\n }\n l++;\n r--;\n }\n\n if(isPalindrome) {\n return answer;\n }\n }\n return \"\";\n}\n};", "memory": "30100" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool check(string word){\n int i=0,j=word.size()-1;\n while(i<j){\n if(word[i]!=word[j])\n return false;\n i++;\n j--;\n }\n return true;\n }\n string firstPalindrome(vector<string>& words) {\n for(auto x:words){\n if(check(x))\n return x;\n }\n return \"\";\n }\n};", "memory": "30200" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string firstPalindrome(vector<string>& words) {\n int idx =0; \n int n= words.size();\n for(auto i:words){\n string s =i;\n reverse(s.begin(), s.end());\n if(s==i){\n return s;\n }\n }\n return \"\";\n }\n};", "memory": "30200" }
2,231
<p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>&quot;&quot;</code>.</p> <p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;abc&quot;,&quot;car&quot;,&quot;ada&quot;,&quot;racecar&quot;,&quot;cool&quot;] <strong>Output:</strong> &quot;ada&quot; <strong>Explanation:</strong> The first string that is palindromic is &quot;ada&quot;. Note that &quot;racecar&quot; is also palindromic, but it is not the first. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;notapalindrome&quot;,&quot;racecar&quot;] <strong>Output:</strong> &quot;racecar&quot; <strong>Explanation:</strong> The first and only string that is palindromic is &quot;racecar&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;def&quot;,&quot;ghi&quot;] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\n\n bool isPalindrome(string word){\n int st = 0;\n int end = word.length() - 1;\n\n while(st<end){\n if(word[st] != word[end]){\n return 0;\n }\n st++;\n end--;\n }\n return 1;\n }\n\npublic:\n string firstPalindrome(vector<string>& words) {\n \n int n = words.size();\n string ans = \"\";\n\n for(int i=0;i<n;i++){\n string word = words[i];\n if(isPalindrome(word) == 1){\n ans = ans + word;\n return ans;\n }\n\n }\n\n return ans;\n\n\n }\n};", "memory": "30300" }