question_slug
stringlengths
3
77
title
stringlengths
1
183
slug
stringlengths
12
45
summary
stringlengths
1
160
author
stringlengths
2
30
certification
stringclasses
2 values
created_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
updated_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
hit_count
int64
0
10.6M
has_video
bool
2 classes
content
stringlengths
4
576k
upvotes
int64
0
11.5k
downvotes
int64
0
358
tags
stringlengths
2
193
comments
int64
0
2.56k
surrounded-regions
[Python] DFS - Clean & Concise
python-dfs-clean-concise-by-hiepit-ajvi
\u2714\uFE0F Solution 1: DFS\npython\nclass Solution:\n def solve(self, board: List[List[str]]) -> None:\n m, n = len(board), len(board[0])\n v
hiepit
NORMAL
2021-09-28T15:32:52.148795+00:00
2024-04-05T22:33:58.284285+00:00
227
false
**\u2714\uFE0F Solution 1: DFS**\n```python\nclass Solution:\n def solve(self, board: List[List[str]]) -> None:\n m, n = len(board), len(board[0])\n visited = [[False] * n for _ in range(m)]\n DIR = [0, 1, 0, -1, 0]\n\n def dfs(r, c, shouldMarkO = False):\n if r < 0 or r == m o...
10
0
['Depth-First Search']
1
surrounded-regions
Python beats 98% easy to understand DFS solution
python-beats-98-easy-to-understand-dfs-s-f9kw
Use "N" to mark all "O"s linked to boundary "O"s.\n\nclass Solution(object):\n def solve(self, board):\n """\n :type board: List[List[str]]\n
xuan__yu
NORMAL
2018-11-21T02:45:59.417125+00:00
2018-11-21T02:45:59.417163+00:00
834
false
Use "N" to mark all "O"s linked to boundary "O"s.\n```\nclass Solution(object):\n def solve(self, board):\n """\n :type board: List[List[str]]\n :rtype: void Do not return anything, modify board in-place instead.\n """\n if not board: return\n m, n = len(board), len(board[0]...
10
0
[]
2
surrounded-regions
[20-ms C++][recommend for beginners]clean C++ implementation with detailed explanation
20-ms-crecommend-for-beginnersclean-c-im-vhqe
As far as I am concerned, after knowing about the BFS, it is all about details to use deque to do the BFS.\n\nthe idea is simple and the implementation is conci
rainbowsecret
NORMAL
2016-01-26T06:55:51+00:00
2016-01-26T06:55:51+00:00
954
false
As far as I am concerned, after knowing about the BFS, it is all about details to use deque to do the BFS.\n\nthe idea is simple and the implementation is concise \n\n\n class Solution {\n public:\n void solve(vector<vector<char>>& board) {\n if(board.size()<=1 || board[0].size()<=1) return;\n ...
10
0
[]
0
surrounded-regions
Java || 1 ms || faster than 99.93%
java-1-ms-faster-than-9993-by-shafiqul-rt0b
\n public void solve(char[][] board) {\n int m = board.length;\n int n = board[0].length;\n\n // traverse 1st and last col\n for
shafiqul
NORMAL
2022-10-16T01:33:02.038414+00:00
2022-10-16T01:33:02.038452+00:00
1,196
false
```\n public void solve(char[][] board) {\n int m = board.length;\n int n = board[0].length;\n\n // traverse 1st and last col\n for (int i = 0; i < m; i++){\n dfs(board, i, 0);\n dfs(board, i, n - 1);\n }\n\n // traverse 1st and last row\n for (i...
9
0
['Depth-First Search', 'Java']
2
surrounded-regions
Javascript - Runtime - Faster than 95.54% - dfs
javascript-runtime-faster-than-9554-dfs-bncsw
Runtime - Faster than 95.54%\nMemory usage - Used less memory than 85.40%\n\n\nvar solve = function(board) {\n for(let i = 0; i < board.length; i++) {\n
ShashwatBangar
NORMAL
2021-10-30T07:10:08.785047+00:00
2021-10-30T07:11:00.216280+00:00
1,347
false
Runtime - Faster than 95.54%\nMemory usage - Used less memory than 85.40%\n\n```\nvar solve = function(board) {\n for(let i = 0; i < board.length; i++) {\n for(let j = 0; j < board[0].length; j++) {\n if(board[i][j] === \'O\' && (i === 0 || j === 0 || i === board.length - 1 || j === board[0].length...
9
1
['Depth-First Search', 'JavaScript']
0
surrounded-regions
Simple & Easy Java Solution Using DFS (1ms, 99% Beats)
simple-easy-java-solution-using-dfs-1ms-ciymz
class Solution {\n public void solve(char[][] board) {\n \n if(board.length==0)\n return;\n \n // this for loop handle
pankaj846
NORMAL
2020-08-26T10:08:33.493089+00:00
2020-08-26T10:08:33.493125+00:00
1,164
false
class Solution {\n public void solve(char[][] board) {\n \n if(board.length==0)\n return;\n \n // this for loop handles all boundary condition in 1st & last row.\n for(int i=0; i<board[0].length ;i++){\n if(board[0][i]==\'O\')\n DFS(board, 0, i)...
9
0
['Depth-First Search', 'Java']
2
surrounded-regions
Python recursion
python-recursion-by-gsan-6ku7
First mark the not surrounded ones as \'\'. These will be assigned to O. The rest become X\n\n\nclass Solution:\n def mark_border(self, i, j, board):\n
gsan
NORMAL
2020-06-17T07:29:41.315943+00:00
2020-06-17T07:29:41.315993+00:00
663
false
First mark the `not surrounded` ones as `\'\'`. These will be assigned to `O`. The rest become `X`\n\n```\nclass Solution:\n def mark_border(self, i, j, board):\n if i==-1 or i==len(board):\n return\n if j==-1 or j==len(board[0]):\n return\n if board[i][j]==\'O\':\n ...
9
0
[]
0
surrounded-regions
Efficient Java Solution using recursion
efficient-java-solution-using-recursion-2uujm
public class Solution {\n public void solve(char[][] board) {\n if(board==null || board.length<=1 ||board[0].length<=1)\n retur
chasethebug
NORMAL
2015-11-29T09:52:57+00:00
2015-11-29T09:52:57+00:00
1,934
false
public class Solution {\n public void solve(char[][] board) {\n if(board==null || board.length<=1 ||board[0].length<=1)\n return;\n int rows = board.length;\n int cols = board[0].length;\n for(int i=0; i<rows; i++){\n if(board[i][0]=='...
9
0
[]
1
surrounded-regions
5ms Simple DFS Java Solution
5ms-simple-dfs-java-solution-by-arjunsun-0eil
This problem can be dealt with in a very simple way. A 'O' will not be surrounded by all sides only if it is linked (directly or through another 'O') to a 'O' t
arjunsunbuffaloedu
NORMAL
2016-11-11T20:56:33.696000+00:00
2018-10-01T23:44:42.964668+00:00
1,260
false
This problem can be dealt with in a very simple way. A 'O' will not be surrounded by all sides only if it is linked (directly or through another 'O') to a 'O' that is on the boundary row or column.\n\nThis means that if all 4 boundaries have only 'X' then all the characters can be switched to 'X' \n\nFor example if you...
9
0
[]
2
surrounded-regions
Why this code has runtime error?
why-this-code-has-runtime-error-by-clubm-saju
Hi,\n\nThe first version of my submission has runtime error, and I debugged it on local machine, it seems to be stack overflow. But if I add the if with comment
clubmaster
NORMAL
2015-11-08T13:33:51+00:00
2015-11-08T13:33:51+00:00
1,953
false
Hi,\n\nThe first version of my submission has runtime error, and I debugged it on local machine, it seems to be stack overflow. But if I add the if with comment "modified version", then it can pass. I don't understand. Is the case kind of corner? Or the test case is not large enough to make the modified version fail?\n...
9
0
[]
3
surrounded-regions
DFS Approach | 100% Runtime | Easy to Understand
dfs-approach-100-runtime-easy-to-underst-22pn
Basically, all the boundary \'O\'s need not to be changed.\nUse DFS to visit the boundary \'O\', turn it into \'V\' and explore the node.\nIn this way, we chang
niro11
NORMAL
2023-01-01T12:01:59.548277+00:00
2023-01-01T12:01:59.548318+00:00
1,187
false
Basically, all the boundary \'O\'s need not to be changed.\nUse DFS to visit the boundary \'O\', turn it into \'V\' and explore the node.\nIn this way, we change the remaining \'O\'s to \'X\'s.\nConvert the \'V\'s to \'O\'s.\n\n# Code\n```\nclass Solution {\n public void solve(char[][] board) {\n int rows = b...
8
0
['Java']
1
surrounded-regions
✅ [Solution] Swift: Surrounded Regions (+ test cases)
solution-swift-surrounded-regions-test-c-r52w
swift\nclass Solution {\n func solve(_ board: inout [[Character]]) {\n for r in board.indices {\n for c in board[r].indices where board[r][
AsahiOcean
NORMAL
2022-01-03T05:47:37.258364+00:00
2022-01-03T05:47:37.258416+00:00
960
false
```swift\nclass Solution {\n func solve(_ board: inout [[Character]]) {\n for r in board.indices {\n for c in board[r].indices where board[r][c] == "O" {\n var curr = board\n if dfs(&curr, r, c) { board = curr }\n }\n }\n }\n \n // valid regi...
8
0
['Depth-First Search', 'Swift']
1
surrounded-regions
Not Best but Intuitive solution
not-best-but-intuitive-solution-by-rieme-7vo4
Okay, so this is my first ever post ever, any feedback is appreciated. Thanks for reading in advance.\n\nNow, my intuition is simple as you will also realise fr
riemeltm
NORMAL
2021-11-01T12:01:03.642440+00:00
2021-11-01T12:01:03.642486+00:00
113
false
Okay, so this is my first ever post ever, any feedback is appreciated. Thanks for reading in advance.\n\nNow, my intuition is simple as you will also realise from the following steps:\n\n**Note:** Observe that, any component of "O" that will not be converted to "X" wil have atleast one cell that will lie on the boundar...
8
0
[]
2
surrounded-regions
C++ Simple and Easy-to-Understand Clean DFS Solution
c-simple-and-easy-to-understand-clean-df-2o7j
\nclass Solution {\npublic:\n void DFS(vector<vector<char>>& board, int x, int y, char c) {\n if (x < 0 || x >= board.size() || y < 0 || y >= board[0]
yehudisk
NORMAL
2021-07-13T07:46:11.676908+00:00
2021-07-13T07:46:11.676952+00:00
298
false
```\nclass Solution {\npublic:\n void DFS(vector<vector<char>>& board, int x, int y, char c) {\n if (x < 0 || x >= board.size() || y < 0 || y >= board[0].size() || board[x][y] != \'O\') return;\n \n board[x][y] = c;\n \n DFS(board, x + 1, y, c);\n DFS(board, x - 1, y, c)...
8
0
['C']
0
surrounded-regions
Java clean code
java-clean-code-by-user6227l-wa31
\nclass Solution {\n public void solve(char[][] board) {\n if(board.length == 0)\n return;\n for(int i=0;i<board.length;i++){\n
user6227l
NORMAL
2021-01-30T05:44:41.758843+00:00
2021-01-30T05:44:41.758890+00:00
558
false
```\nclass Solution {\n public void solve(char[][] board) {\n if(board.length == 0)\n return;\n for(int i=0;i<board.length;i++){\n for(int j=0;j<board[0].length;j++){\n if(i==0 || j==0 || i==board.length-1 || j==board[0].length-1){\n dfs(i,j,boa...
8
2
['Depth-First Search', 'Java']
0
surrounded-regions
C# DFS
c-dfs-by-bacon-2k6w
\npublic class Solution {\n public void Solve(char[][] board) {\n var n = board.Length;\n\n if (n == 0) return;\n var m = board[0].Lengt
bacon
NORMAL
2019-05-06T01:47:19.981894+00:00
2019-05-06T01:47:19.981959+00:00
540
false
```\npublic class Solution {\n public void Solve(char[][] board) {\n var n = board.Length;\n\n if (n == 0) return;\n var m = board[0].Length;\n\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n if ((i == 0 || j == 0 || i == n - 1 || j == m - 1) &&...
8
0
[]
0
surrounded-regions
Java 6ms DFS solution, easy to understand and relatively short
java-6ms-dfs-solution-easy-to-understand-7d7c
Idea is simple: the only 'O's that will NOT change to 'X's are those at the edges and those horizontally or vertically connected to the 'O's at the edges. So, f
davidsunsbk
NORMAL
2016-03-29T21:33:05+00:00
2016-03-29T21:33:05+00:00
2,577
false
Idea is simple: the only 'O's that will NOT change to 'X's are those at the edges and those horizontally or vertically connected to the 'O's at the edges. So, first find out all the 'O's at the edges, mark them as 'M', and keep checking their surrounding 'O's and mark them as 'M'. Then loop the board again, change 'O's...
8
1
['Depth-First Search']
8
surrounded-regions
Simple DFS solution || JAVA || Beats 100%
simple-dfs-solution-java-beats-100-by-sa-ds6b
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
saad_hussain_
NORMAL
2024-05-12T19:46:53.236919+00:00
2024-05-12T19:46:53.236948+00:00
1,249
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
7
0
['Java']
1
surrounded-regions
Python || 97.64% Faster || BFS || DFS || Two Approaches
python-9764-faster-bfs-dfs-two-approache-d0p4
BFS Approach:\n\ndef solve(self, board: List[List[str]]) -> None:\n """\n Do not return anything, modify board in-place instead.\n """\n
pulkit_uppal
NORMAL
2023-03-19T11:49:18.095414+00:00
2023-03-19T11:50:14.759248+00:00
1,360
false
**BFS Approach:**\n```\ndef solve(self, board: List[List[str]]) -> None:\n """\n Do not return anything, modify board in-place instead.\n """\n m=len(board)\n n=len(board[0])\n q=deque()\n for i in range(m):\n for j in range(n):\n if i==0 or i==...
7
0
['Depth-First Search', 'Breadth-First Search', 'Graph', 'Python', 'Python3']
0
surrounded-regions
Default code compile error
default-code-compile-error-by-suitier-y4s9
When I submit c++ default code, it gives compile error.\nIs there anyone in the same situation?\n\n- Code\n\nclass Solution {\npublic:\n void solve(vector<ve
suitier
NORMAL
2020-04-28T22:50:49.787533+00:00
2020-04-29T00:07:29.344219+00:00
227
false
When I submit c++ default code, it gives compile error.\nIs there anyone in the same situation?\n\n- Code\n```\nclass Solution {\npublic:\n void solve(vector<vector<char>>& board) {\n\n }\n};\n```\n\n- Error message\n```\nCompile Error:\n\nLine 30: Char 17: fatal error: no matching member function for call to \'r...
7
0
[]
5
surrounded-regions
Java - Union Find
java-union-find-by-wilsoncursino-kyaz
\nclass Solution {\n \n // Union Find \n // Intuition: \n // - We need to separate \'O\' in the borders from the other \'O\'\n // - S
wilsoncursino
NORMAL
2020-04-11T21:18:38.311630+00:00
2020-04-11T21:18:38.311691+00:00
267
false
```\nclass Solution {\n \n // Union Find \n // Intuition: \n // - We need to separate \'O\' in the borders from the other \'O\'\n // - So, if a node \'O\' is on the border connect it to the virtual node.\n // - All the others \'O\' connect to their \'O\' neighbors. (eventually, if th...
7
0
[]
2
surrounded-regions
Java | DFS, Union-Find, Union-Find Rank
java-dfs-union-find-union-find-rank-by-a-v71v
```\n/\n * Approach1 Logical Thinking We aim to set all O\'s which doesn\'t locate at\n * borders or connect to O at borders to X. We mark all O\'s at borders a
aurbatao
NORMAL
2020-03-22T20:04:31.445385+00:00
2020-03-22T20:07:31.665716+00:00
640
false
```\n/*\n * Approach1 Logical Thinking We aim to set all O\'s which doesn\'t locate at\n * borders or connect to O at borders to X. We mark all O\'s at borders and apply\n * DFS at each O at boarders to mark all O\'s connected to it. The un-marked O\'s\n * ought to be set X.\n * \n * Trick We search for invalid candida...
7
0
[]
1
surrounded-regions
Python straight forward solution
python-straight-forward-solution-by-giri-izta
Inspired from [caikehe's][1] solution. Start from the edge and mark all the 'O' that are accessible from the 'O' in the edge using BFS. The marked 'O' should st
girikuncoro
NORMAL
2016-01-06T08:26:49+00:00
2016-01-06T08:26:49+00:00
1,559
false
Inspired from [caikehe's][1] solution. Start from the edge and mark all the 'O' that are accessible from the 'O' in the edge using BFS. The marked 'O' should stay 'O', and the 'untouchable' 'O' would be converted to 'X'\n\n def solve(board):\n queue = []\n for r in range(len(board)):\n for c...
7
0
['Python']
0
surrounded-regions
BFS-based solution in Java
bfs-based-solution-in-java-by-mach7-on16
/*\n dfs, bfs, union-find\u90fd\u53ef\u4ee5\u505a, \u57fa\u672c\u601d\u8def\u662f\n \u4ece\u5728\u56db\u4e2a\u8fb9\u7684\u5404\u4e2a'O'\u5f00\u59c
mach7
NORMAL
2016-02-26T10:11:29+00:00
2016-02-26T10:11:29+00:00
1,130
false
/*\n dfs, bfs, union-find\u90fd\u53ef\u4ee5\u505a, \u57fa\u672c\u601d\u8def\u662f\n \u4ece\u5728\u56db\u4e2a\u8fb9\u7684\u5404\u4e2a'O'\u5f00\u59cb\u641c\u7d22, \u8fde\u5728\u4e00\u8d77\u7684'O'\u5c31\u662f\u4e0d\u80fd\u88ab\u5305\u56f4\u7684, \u5176\u4f59\u7684\u70b9\u90fd\u5e94\u8be5\u8bbe\u4e3a'X'....
7
1
[]
0
surrounded-regions
Surrounded Regions [C++][Easy]
surrounded-regions-ceasy-by-moveeeax-95rm
IntuitionThe problem asks to capture all the surrounded regions (i.e., 'O's that are surrounded by 'X's) in a 2D grid and replace them with 'X'. The cells that
moveeeax
NORMAL
2025-02-03T02:35:06.794130+00:00
2025-02-03T02:35:06.794130+00:00
886
false
# Intuition The problem asks to capture all the surrounded regions (i.e., 'O's that are surrounded by 'X's) in a 2D grid and replace them with 'X'. The cells that are connected to the boundary or the outer regions should not be surrounded, so these 'O's should remain unchanged. Our goal is to identify all the 'O's that...
6
0
['C++']
0
surrounded-regions
simple DFS solution without extra space
simple-dfs-solution-without-extra-space-ap0w3
IntuitionAs the problem states, all the cells ('O') that are connected to the edges cannot be surrounded by 'X'. Therefore, we will visit all four edges and che
vipin_rana8
NORMAL
2025-01-06T10:17:26.659801+00:00
2025-01-06T10:17:26.659801+00:00
975
false
# Intuition As the problem states, all the cells ('O') that are connected to the edges cannot be surrounded by 'X'. Therefore, we will visit all four edges and check the connected regions to any 'O' on the edge. # Approach I will follow the DFS approach to visit every 'O' cell connected to the edges and mark them as ...
6
0
['Depth-First Search', 'Java']
1
surrounded-regions
83.1 (Approach 1) | ( O(m * n) )✅ | Python & C++(Step by step explanation)✅
831-approach-1-om-n-python-cstep-by-step-uzlv
Intuition\nThe problem requires capturing surrounded regions on a board, where surrounded regions are defined as regions that are not connected to the border. W
monster0Freason
NORMAL
2024-02-13T20:36:28.666522+00:00
2024-02-13T20:36:28.666541+00:00
894
false
# Intuition\nThe problem requires capturing surrounded regions on a board, where surrounded regions are defined as regions that are not connected to the border. We need to capture (convert to \'X\') surrounded regions while leaving unsurrounded regions intact.\n\n# Approach\n![image.png](https://assets.leetcode.com/use...
6
0
['Depth-First Search', 'Graph', 'Matrix', 'C++', 'Python3']
2
surrounded-regions
Java Solution for Surrounded Regions Problem
java-solution-for-surrounded-regions-pro-hm2b
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem is to find all the "surrounded regions" in a 2D board. A surrounded region
Aman_Raj_Sinha
NORMAL
2023-05-01T04:44:15.954991+00:00
2023-05-01T04:44:15.956409+00:00
834
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem is to find all the "surrounded regions" in a 2D board. A surrounded region is defined as a region of \'O\'s (capital letter O) that is surrounded by \'X\'s (capital letter X) in all directions (left, right, top, bottom). The g...
6
0
['Java']
0
surrounded-regions
Surrounded Regions, Explained | DFS in Python
surrounded-regions-explained-dfs-in-pyth-x7mu
Intuition\nIn order to solve this problem, we first need to distinguish between two types of "O" cells within the matrix: those that are connected 4-directional
BeefCode
NORMAL
2023-03-04T02:36:58.517185+00:00
2023-05-03T21:12:38.443030+00:00
899
false
# Intuition\nIn order to solve this problem, we first need to distinguish between two types of `"O"` cells within the matrix: those that are connected 4-directionally to `"O"` cells touching the border, and those that aren\'t. In other words, all `"O"` cells along the bounds of the matrix are **safe**, where "safe" mea...
6
0
['Depth-First Search', 'Graph', 'Matrix', 'Python', 'Python3']
1
surrounded-regions
130: Solution with step by step explanation
130-solution-with-step-by-step-explanati-ygvs
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nThe algorithm works as follows:\n\n1. We first handle the edge cases by c
Marlen09
NORMAL
2023-02-18T07:02:08.144407+00:00
2023-02-18T07:02:08.144453+00:00
1,525
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThe algorithm works as follows:\n\n1. We first handle the edge cases by checking if the board is empty. If it is, we return without doing anything.\n\n2. We define a helper function dfs that performs a depth-first search to ...
6
0
['Array', 'Depth-First Search', 'Breadth-First Search', 'Python', 'Python3']
1
surrounded-regions
Fastest Solution (100% - 0ms) with Explanation for beginners.
fastest-solution-100-0ms-with-explanatio-ebku
Intuition\nDFS\n\n# Approach\n1. Apply DFS from border of the grid so that all the element which do not have any path leading to broder of the gird will be unaf
shashwat1712
NORMAL
2023-02-06T10:03:47.027446+00:00
2023-02-07T08:21:52.558837+00:00
1,585
false
# Intuition\nDFS\n\n# Approach\n1. Apply DFS from border of the grid so that all the element which do not have any path leading to broder of the gird will be unaffected. \n2. Change value of the traversed path to anything for example \'p\'.\n3. After completion of DFS change all the value in grid from \'O\' to \'X\'\n4...
6
0
['Depth-First Search', 'Graph', 'Java']
0
surrounded-regions
✅Accepted | | ✅Easy solution || ✅Short & Simple || ✅Best Method
accepted-easy-solution-short-simple-best-1xou
\n# Code\n\nclass Solution {\npublic:\n void solve(vector<vector<char>>& board) {\n queue<pair<int, int>> q;\n int m=board.size();\n int
sanjaydwk8
NORMAL
2023-01-23T13:51:04.569944+00:00
2023-01-23T13:51:04.569990+00:00
2,541
false
\n# Code\n```\nclass Solution {\npublic:\n void solve(vector<vector<char>>& board) {\n queue<pair<int, int>> q;\n int m=board.size();\n int n=board[0].size();\n for(int i=0;i<m;i++)\n {\n for(int j=0;j<n;j++)\n if(i==0 || j==0 || i==m-1 || j==n-1)\n ...
6
0
['C++']
1
surrounded-regions
Check for the 'O' which lie on the perimeter cells of the board || C++
check-for-the-o-which-lie-on-the-perimet-4xze
At first, I thought that was a very hard question as we need to check all cells that surround a particular group of \'O\'s. But, it turned out to be a rather si
pranjal_gaur
NORMAL
2022-07-07T15:50:40.928322+00:00
2022-07-07T15:51:17.480187+00:00
319
false
At first, I thought that was a very hard question as we need to check all cells that surround a particular group of \'O\'s. But, it turned out to be a rather simple question.\nI solved this problem in 2 iterataions of the board. In the first iteration, all you need to do is to traverse the board and look for the O\'s w...
6
0
['Depth-First Search', 'C']
0
surrounded-regions
Simple C++ DFS Solution with Explanations and Comments
simple-c-dfs-solution-with-explanations-g5c4a
Steps to Solve :\n 1. Move over the boundary of board, and find O\'s \n 2. Every time we find an O, perform DFS from it\'s position\n 3. In DFS c
rishabh_devbanshi
NORMAL
2021-11-01T14:36:43.991602+00:00
2021-11-01T14:40:23.683988+00:00
257
false
Steps to Solve :\n 1. Move over the boundary of board, and find O\'s \n 2. Every time we find an O, perform DFS from it\'s position\n 3. In DFS convert all \'O\' to \'#\' (why?? so that we can differentiate which \'O\' can be flipped and which cannot be) \n 4. After all DFSs have been perform...
6
0
['Backtracking', 'Depth-First Search', 'Recursion', 'C']
2
surrounded-regions
📌📌 Question Explanation is very Bad || Well-Explained Question and Solution || Easy 🐍
question-explanation-is-very-bad-well-ex-q86h
Question : \nMeaning of Question is to convert all "O" in matrix to "X" which are not connected to any "O" at the border of matrix. \nApproach:\n Pick all O\'s
abhi9Rai
NORMAL
2021-11-01T06:23:00.530182+00:00
2021-11-01T06:23:00.530209+00:00
380
false
## Question : \n**Meaning of Question is to convert all "O" in matrix to "X" which are not connected to any "O" at the border of matrix.** \n**Approach:**\n* Pick all O\'s from boundary (Top/Bottom row, Leftmost/Rightmost column)\n* Make all connected O\'s to some intermediate value (* in my case).\n* Now remaining all...
6
0
['Depth-First Search', 'Python', 'Python3']
0
surrounded-regions
Simple Python BFS solution beats 90%
simple-python-bfs-solution-beats-90-by-c-3xxo
The key of this problem is to realize that the only way for a "O" cell to escape is through the boundaries. Instead of starting from each "O" cell we can expand
charlesl0129
NORMAL
2021-08-23T02:43:16.256640+00:00
2021-08-23T02:43:42.990883+00:00
451
false
The key of this problem is to realize that the only way for a "O" cell to escape is through the boundaries. Instead of starting from each "O" cell we can expand "O" cells at the four boundaries using BFS and see which cell is reachable form the boundaries (or in other words which cells can reach/escape the boundary).\n...
6
0
['Breadth-First Search', 'Python', 'Python3']
0
surrounded-regions
Simple understandable dfs solution C++
simple-understandable-dfs-solution-c-by-qwdt5
\nclass Solution {\npublic:\n void dfs(vector<vector<char>>&board,int i,int j,int m,int n){\n if(i<0||i==m||j<0||j==n||board[i][j]==\'X\'||board[i][j]
gaurav2k20
NORMAL
2021-07-22T20:21:09.700505+00:00
2021-12-24T05:36:20.826376+00:00
116
false
```\nclass Solution {\npublic:\n void dfs(vector<vector<char>>&board,int i,int j,int m,int n){\n if(i<0||i==m||j<0||j==n||board[i][j]==\'X\'||board[i][j]==\'$\'){\n return ;\n }\n board[i][j]=\'$\';\n dfs(board,i-1,j,m,n);\n dfs(board,i,j-1,m,n);\n dfs(board,i+1,j...
6
0
['Depth-First Search']
0
surrounded-regions
C++ || Simple BFS to understand || Faster than 99.45%
c-simple-bfs-to-understand-faster-than-9-xzkn
Main logic is that to be totally surrounded by X they must be on four sides.\nSo if there is a O on the outer edge then it can never be surrounded , and all the
Kilua__
NORMAL
2021-03-14T17:44:28.848345+00:00
2021-03-14T17:44:28.848384+00:00
368
false
Main logic is that to be totally surrounded by X they must be on four sides.\nSo if there is a O on the outer edge then it can never be surrounded , and all the O\'s attached to it are also cannot be surrounded!! \n\nSo we start storing all the boundary O\'s and start securing O\'s in BFS manner.\n\nTo Optimise the mem...
6
0
['Breadth-First Search', 'C']
2
surrounded-regions
Easy to understand Python Solution (beats 98% of runtimes, 50% of memory)
easy-to-understand-python-solution-beats-jgsk
\nclass Solution:\n def solve(self, board: List[List[str]]) -> None:\n """\n Do not return anything, modify board in-place instead.\n ""
user3971c
NORMAL
2020-06-17T20:31:58.508369+00:00
2020-06-17T20:31:58.508412+00:00
746
false
```\nclass Solution:\n def solve(self, board: List[List[str]]) -> None:\n """\n Do not return anything, modify board in-place instead.\n """\n #check for edge cases\n if not board:\n return\n if len(board[0]) == 1 or len(board) == 1:\n return\n \...
6
0
['Python', 'Python3']
0
surrounded-regions
Union Find algorithm
union-find-algorithm-by-harris_ceod-95e8
The problem asks us to flip inner "O"s, which does not connect to any border "O"s. If we connect adjacent "O"s and join them in the same set(i.e. union adjacent
harris_ceod
NORMAL
2019-05-07T10:21:02.895699+00:00
2019-05-07T10:21:02.895766+00:00
520
false
The problem asks us to flip inner `"O"`s, which does not connect to any border `"O"`s. If we connect adjacent `"O"`s and join them in the same set(i.e. **union** adjacent `"O"`s), we can **find** if a `"O"` is in the same set with any border `"O"`. This solution can be splited into three steps:\n```\nX X X X\nO O X X\...
6
0
[]
3
surrounded-regions
JavaScript Solution
javascript-solution-by-jay-shi-h563
\n/**\n * @param {character[][]} board\n * @return {void} Do not return anything, modify board in-place instead.\n */\nvar solve = function(board) {\n if (boar
jay-shi
NORMAL
2019-01-11T02:18:15.345284+00:00
2019-01-11T02:18:15.345360+00:00
378
false
```\n/**\n * @param {character[][]} board\n * @return {void} Do not return anything, modify board in-place instead.\n */\nvar solve = function(board) {\n if (board === null || board.length === 0 || board[0].length === 0) return;\n for (let i = 0; i< board.length; i++) {\n for (let j = 0; j < board[0].length; j++) ...
6
0
[]
0
surrounded-regions
It's important to master all 3 methods: DFS, BFS, Union Find
its-important-to-master-all-3-methods-df-ljwj
\nclass Solution_DFS:\n def solve(self, board):\n """\n :type board: List[List[str]]\n :rtype: void Do not return anything, modify board
joeleetcode2018
NORMAL
2018-09-18T01:19:37.269416+00:00
2018-10-10T06:47:52.574671+00:00
925
false
```\nclass Solution_DFS:\n def solve(self, board):\n """\n :type board: List[List[str]]\n :rtype: void Do not return anything, modify board in-place instead.\n """\n alive,v = set(),set()\n for r in range(len(board)):\n for c in range(len(board[r])):\n ...
6
0
[]
2
surrounded-regions
Java, concise Union Find
java-concise-union-find-by-kevincongcc-t0r4
I have tried my best to make my code clean and readable. ``` public class Solution { private int m, n; private class UnionFind{ private int[] parent;
kevincongcc
NORMAL
2018-03-05T06:55:25.736738+00:00
2018-10-10T01:25:58.408823+00:00
745
false
I have tried my best to make my code clean and readable. ``` public class Solution { private int m, n; private class UnionFind{ private int[] parent; private int[] rank; public UnionFind(int n){ parent = new int[n]; rank = new int[n]; ...
6
0
[]
5
surrounded-regions
Accepted 14ms DFS c++ solution and 16ms BFS c++ solution.
accepted-14ms-dfs-c-solution-and-16ms-bf-1vr7
Please pay special attention to the comment in the solutions\uff01\n\nDFS, 14ms:\n\n class Solution {\n public:\n void solve(std::vector > &board)
prime_tang
NORMAL
2015-05-17T07:18:23+00:00
2015-05-17T07:18:23+00:00
2,768
false
**Please pay special attention to the comment in the solutions\uff01**\n\nDFS, 14ms:\n\n class Solution {\n public:\n void solve(std::vector<std::vector<char> > &board) {\n if (board.empty())\n return;\n rows = static_cast<int>(board.size());\n cols = static_...
6
0
['Depth-First Search', 'Breadth-First Search']
3
surrounded-regions
Very easy to understand | Well explained | Beats 100.00%
very-easy-to-understand-well-explained-b-012f
IntuitionThe problem involves capturing regions of 'O' surrounded by 'X' on a 2D board. The main idea is that only the 'O' regions connected to the borders rema
ayush_pratap27
NORMAL
2025-01-16T11:28:39.887152+00:00
2025-01-16T11:28:39.887152+00:00
2,349
false
![Screenshot 2025-01-16 at 4.42.13 PM.png](https://assets.leetcode.com/users/images/c7dad670-7c56-4eb6-9014-32341e404825_1737026386.0916784.png) # Intuition <!-- Describe your first thoughts on how to solve this problem. --> The problem involves capturing regions of 'O' surrounded by 'X' on a 2D board. The main idea i...
5
0
['Array', 'Depth-First Search', 'Graph', 'C++', 'Java', 'Python3', 'JavaScript']
0
surrounded-regions
💢☠💫Easiest👾Faster✅💯 Lesser🧠 🎯 C++✅Python3🐍✅Java✅C✅Python🐍✅C#✅💥🔥💫Explained☠💥🔥 Beats 100
easiestfaster-lesser-cpython3javacpython-w05l
\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n\n Describe your first thoughts on how to solve this problem. \n- J
Edwards310
NORMAL
2024-12-07T05:58:18.485213+00:00
2024-12-07T05:58:18.485253+00:00
1,133
false
![0ehh83fsnh811.jpg](https://assets.leetcode.com/users/images/9fc46acb-7ba4-42e1-864c-3b0e7e0e82b6_1730795144.4340796.jpeg)\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n\n<!-- Describe your first thoughts on how to solve this problem. -->\n- ***JavaScript Code -->**...
5
0
['Array', 'Depth-First Search', 'Breadth-First Search', 'C', 'Python', 'C++', 'Java', 'Go', 'Python3', 'JavaScript']
0
surrounded-regions
Easy DFS || C++ || Beats 92% || Beginner friendly approach
easy-dfs-c-beats-92-beginner-friendly-ap-b43u
\n\n# Approach\n Describe your approach to solving the problem. \nDFS\n\n# Complexity\n- Time complexity: O(m * n)\n Add your time complexity here, e.g. O(n) \n
DecafCoder0312
NORMAL
2024-06-25T08:25:51.626354+00:00
2024-06-25T08:25:51.626396+00:00
1,250
false
![image.png](https://assets.leetcode.com/users/images/6482dbce-e410-4052-b409-842edf1dc7ad_1719303891.9397962.png)\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nDFS\n\n# Complexity\n- Time complexity: O(m * n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(m * ...
5
0
['Array', 'Depth-First Search', 'Recursion', 'Matrix', 'C++']
4
surrounded-regions
96% Beats |C++|Memory & Space Optimized| Boundary DFS |
96-beats-cmemory-space-optimized-boundar-njth
Approach\nWe can start by identifying \'O\' cells at the border of the board because these cells cannot be surrounded by \'X\'. All other \'O\' cells within the
Shivam_Sikotra
NORMAL
2023-09-06T18:46:40.596558+00:00
2023-09-06T18:46:40.596577+00:00
374
false
# Approach\nWe can start by identifying \'O\' cells at the border of the board because these cells cannot be surrounded by \'X\'. All other \'O\' cells within the border can potentially be surrounded and should be marked with \'N\'.\n\n# Algorithm\n+ Initialize variables to store the number of rows (row) and columns (...
5
0
['Array', 'Depth-First Search', 'Matrix', 'C++']
0
surrounded-regions
C++ || Graph || DFS || BFS || Simple Approach
c-graph-dfs-bfs-simple-approach-by-inam_-rtun
METHOD 1 -DFS\nTime complexity- O(rows * cols)\nSpace complexity- O(rows * cols)\n\nclass Solution {\n void dfs(int i, int j, vector<vector<char>>& board) {\
Inam_28_06
NORMAL
2023-08-11T17:42:57.745934+00:00
2023-08-11T17:45:25.643367+00:00
191
false
METHOD 1 -DFS\nTime complexity- O(rows * cols)\nSpace complexity- O(rows * cols)\n```\nclass Solution {\n void dfs(int i, int j, vector<vector<char>>& board) {\n // Check if the indices are within the boundaries and the cell contains \'O\'\n if (i >= 0 && j >= 0 && i < board.size() && j < board[0].size...
5
0
['Depth-First Search', 'Breadth-First Search', 'Graph', 'C']
0
surrounded-regions
Easy solution in Java using Union Find with a simple trick.
easy-solution-in-java-using-union-find-w-55cs
Intuition\n Describe your first thoughts on how to solve this problem. \nReplace O with X only if it\'s surrounded by Xs -> we can use union find to group conne
mega01
NORMAL
2023-08-09T11:23:02.596225+00:00
2023-08-09T11:23:54.823973+00:00
357
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nReplace O with X only if it\'s surrounded by Xs -> we can use union find to group connected Os together, but how to know whether to replace this O or not? \nwe can simply make extra dummy node if the O is in the first, last row or col and...
5
0
['Union Find', 'Java']
0
surrounded-regions
My Solution in Java
my-solution-in-java-by-puranjan_nitr-2lya
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nIdea: first we traverse all the boundaries, if there is a \'O\' present w
puranjan_nitr
NORMAL
2023-06-09T18:39:36.157571+00:00
2023-06-09T18:42:30.412944+00:00
1,375
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nIdea: first we traverse all the boundaries, if there is a \'O\' present we mark that aa \'P\'\nafter that again we traverse through the board, if there is any \'O\', present than we replace that with \'X\'. else if there is ...
5
0
['Depth-First Search', 'Java']
3
surrounded-regions
Easy & Clear Solution Python 3 Beat 99.8%
easy-clear-solution-python-3-beat-998-by-d2ez
\n# Code\n\nclass Solution:\n def solve(self, b: List[List[str]]) -> None:\n m=len(b)\n n=len(b[0])\n def dfs(i,j):\n if b[i]
moazmar
NORMAL
2023-03-30T02:59:19.590825+00:00
2023-03-30T02:59:19.590861+00:00
681
false
\n# Code\n```\nclass Solution:\n def solve(self, b: List[List[str]]) -> None:\n m=len(b)\n n=len(b[0])\n def dfs(i,j):\n if b[i][j]=="O":\n b[i][j]="P"\n if i<m-1:\n dfs(i+1,j)\n if i>0:\n dfs(i-1,j)\n ...
5
0
['Depth-First Search', 'Python3']
0
surrounded-regions
C++ | Boundary DFS with Explanation | No-Extra Space | Easy understanding
c-boundary-dfs-with-explanation-no-extra-i9co
Approach\n Steps to Solve :\n 1. Move over the boundary of board, and find O\'s \n 2. Every time we find an O, perform DFS from it\'s position\n 3. In
imSoumyadeep
NORMAL
2022-10-30T06:25:01.831012+00:00
2022-10-30T06:31:00.228166+00:00
615
false
# Approach\n Steps to Solve :\n 1. Move over the boundary of board, and find O\'s \n 2. Every time we find an O, perform DFS from it\'s position\n 3. In DFS convert all \'O\' to \'C\' (why?? so that we can differentiate which \'O\' can be flipped and which cannot be)\n 4. After all DFSs have been performed,...
5
0
['Depth-First Search', 'Graph', 'Recursion', 'C++']
1
surrounded-regions
Easy DFS intuitive Java solution beats 99.98%
easy-dfs-intuitive-java-solution-beats-9-sl4h
\nclass Solution {\n public void solve(char[][] a) {\n int n = a.length, m = a[0].length;\n for(int i = 0; i < n; i++){ // Boundary calls o
lagaHuaHuBro
NORMAL
2021-11-01T10:27:29.394239+00:00
2021-11-01T12:47:56.109262+00:00
47
false
```\nclass Solution {\n public void solve(char[][] a) {\n int n = a.length, m = a[0].length;\n for(int i = 0; i < n; i++){ // Boundary calls on 0th and a[0].length - 1 th coll\n if(a[i][0] == \'O\'){\n dfs(a, i, 0);\n }\n if(a[i][m - 1] == \'O\'){\n ...
5
0
[]
0
surrounded-regions
Simple - Best Solution
simple-best-solution-by-kukreti-59ay
class Solution {\npublic:\n \n void change(vector>& board , int i , int j){\n board[i][j] = \'\';\n int dx[] = {0 ,0 ,-1,1};\n int dy
kukreti____________
NORMAL
2021-11-01T02:57:07.823657+00:00
2021-11-01T05:38:18.378335+00:00
160
false
class Solution {\npublic:\n \n void change(vector<vector<char>>& board , int i , int j){\n board[i][j] = \'*\';\n int dx[] = {0 ,0 ,-1,1};\n int dy[] = {1,-1 ,0,0};\n for(int k=0;k<4;k++){\n int cx = i +dx[k];\n int cy = j+ dy[k];\n if(cx>=0 && cx<board...
5
1
[]
0
surrounded-regions
[Python] Interview Solution Explained (DFS)
python-interview-solution-explained-dfs-bu5yw
```\nclass Solution:\n \n # Helper function to determine if a postiion is on the border of the matrix\n def is_border(self, row, col, m, n):\n ret
hasanaltaf2001
NORMAL
2021-08-08T20:10:33.850348+00:00
2021-08-08T20:10:33.850377+00:00
348
false
```\nclass Solution:\n \n # Helper function to determine if a postiion is on the border of the matrix\n def is_border(self, row, col, m, n):\n return row == 0 or row == m - 1 or col == 0 or col == n-1\n \n # Helper function to get valid neighbours\n def get_valid_neighbours(self, row, col, board)...
5
0
['Depth-First Search', 'Recursion', 'Python']
2
surrounded-regions
Simple Detailed C++ DFS approach faster than 100%
simple-detailed-c-dfs-approach-faster-th-yxeu
Travel on the boundary of the given board and call dfs everytime you encounter \'O\'.\n Now when you call dfs at a cell which is \'O\', make it \'S\' (or any ot
persistentBeast
NORMAL
2021-04-07T20:35:43.024259+00:00
2021-04-09T15:40:04.327530+00:00
129
false
* Travel on the boundary of the given board and call dfs everytime you encounter \'O\'.\n* Now when you call dfs at a cell which is \'O\', make it \'S\' (or any other recognisable marker) and call dfs to its neighbours \n* So all \'O\' reachable from the boundary \'O\' will get marker \'S\' along with the one at the bo...
5
0
[]
0
surrounded-regions
[Python 3] DFS & backtracking - with explanation (128ms runtime, O(1) extra space)
python-3-dfs-backtracking-with-explanati-x7xg
Approach:\nUse dfs from every "O" that is on the boarders and convert all those "O" that are connected into "A" which temporarily means they are explored, but w
vikktour
NORMAL
2021-01-01T21:03:49.200833+00:00
2024-09-28T02:32:07.086996+00:00
593
false
Approach:\nUse dfs from every "O" that is on the boarders and convert all those "O" that are connected into "A" which temporarily means they are explored, but will turn back to "O" at the end of the dfs. After finishing dfs on all boarder "O", we can then iterate through the entire board and convert all "O" into "X", a...
5
0
['Depth-First Search', 'Python', 'Python3']
0
valid-triangle-number
A similar O(n^2) solution to 3-Sum
a-similar-on2-solution-to-3-sum-by-jeant-17n9
This problem is very similar to 3-Sum, in 3-Sum, we can use three pointers (i, j, k and i < j < k) to solve the problem in O(n^2) time for a sorted array, the w
jeantimex
NORMAL
2018-05-02T07:02:05.505940+00:00
2018-10-18T01:40:18.963350+00:00
21,758
false
This problem is very similar to 3-Sum, in 3-Sum, we can use three pointers (i, j, k and i < j < k) to solve the problem in O(n^2) time for a sorted array, the way we do in 3-Sum is that we first lock pointer i and then scan j and k, if nums[j] + nums[k] is too large, k--, otherwise j++, once we complete the scan, incre...
489
0
[]
19
valid-triangle-number
[C++/Java/Python] Two Pointers - Picture Explain - Clean & Concise - O(N^2)
cjavapython-two-pointers-picture-explain-2245
\u2714\uFE0F Solution 1: Two Pointer\n\nTheorem: In a triangle, the length of any side is less than the sum of the other two sides.\n\n- So 3 side lengths a, b
hiepit
NORMAL
2021-07-15T10:19:10.984057+00:00
2023-08-28T16:33:19.166340+00:00
11,808
false
**\u2714\uFE0F Solution 1: Two Pointer**\n\n**Theorem**: In a triangle, the length of any side is less than the sum of the other two sides.\n![image](https://assets.leetcode.com/users/images/3a8ebfd1-b8e4-4931-a525-29f8b7a08a54_1626365313.7172236.png)\n- So 3 side lengths `a`, `b`, `c` can form a Triangle if and only ...
385
105
['Two Pointers']
15
valid-triangle-number
Java O(n^2) Time O(1) Space
java-on2-time-o1-space-by-compton_scatte-vis2
\npublic static int triangleNumber(int[] A) {\n Arrays.sort(A);\n int count = 0, n = A.length;\n for (int i=n-1;i>=2;i--) {\n int l = 0, r = i-1
compton_scatter
NORMAL
2017-06-11T03:15:33.169000+00:00
2019-11-07T07:55:30.966578+00:00
36,439
false
```\npublic static int triangleNumber(int[] A) {\n Arrays.sort(A);\n int count = 0, n = A.length;\n for (int i=n-1;i>=2;i--) {\n int l = 0, r = i-1;\n while (l < r) {\n if (A[l] + A[r] > A[i]) {\n count += r-l;\n r--;\n }\n else l++;\...
357
11
[]
34
valid-triangle-number
[Python] sort + 2 pointers solution, explained
python-sort-2-pointers-solution-explaine-lhaj
Let n be number of our numbers. Then bruteforce solution is O(n^3). Another approach is to sort numbers and for each pair a_i and a_j, where i<j we need to find
dbabichev
NORMAL
2021-07-15T09:12:33.783836+00:00
2021-07-15T09:12:33.783866+00:00
5,088
false
Let `n` be number of our numbers. Then bruteforce solution is `O(n^3)`. Another approach is to sort numbers and for each pair `a_i` and `a_j`, where `i<j` we need to find the biggest index `k`, such that `a_k < a_i + a_j`. It can be done with binary search with overall complexity `O(n^2 * log n)`.\n\nThere is even bett...
99
0
['Two Pointers']
8
valid-triangle-number
Java Solution, 3 pointers
java-solution-3-pointers-by-shawngao-ctn4
Same as https://leetcode.com/problems/3sum-closest\n\nAssume a is the longest edge, b and c are shorter ones, to form a triangle, they need to satisfy len(b) +
shawngao
NORMAL
2017-06-11T03:18:58.194000+00:00
2018-09-16T08:07:09.015489+00:00
12,311
false
Same as https://leetcode.com/problems/3sum-closest\n\nAssume ```a``` is the longest edge, ```b``` and ```c``` are shorter ones, to form a triangle, they need to satisfy ```len(b) + len(c) > len(a)```.\n\n```\npublic class Solution {\n public int triangleNumber(int[] nums) {\n int result = 0;\n if (nums...
81
2
[]
6
valid-triangle-number
C++ Simple and Clean Solution
c-simple-and-clean-solution-by-yehudisk-hnwh
\nclass Solution {\npublic:\n int triangleNumber(vector<int>& nums) {\n int res = 0, n = nums.size();\n \n sort(nums.begin(), nums.end()
yehudisk
NORMAL
2021-07-15T07:33:42.070534+00:00
2021-07-15T07:33:42.070579+00:00
5,035
false
```\nclass Solution {\npublic:\n int triangleNumber(vector<int>& nums) {\n int res = 0, n = nums.size();\n \n sort(nums.begin(), nums.end());\n \n for (int i = n-1; i >= 0; i--) {\n int lo = 0, hi = i-1;\n \n while (lo < hi) {\n if (n...
76
2
['C']
3
valid-triangle-number
Python3 O(n^2) pointer solution
python3-on2-pointer-solution-by-skekre98-0412
\nclass Solution:\n def triangleNumber(self, nums):\n """\n :type nums: List[int]\n :rtype: int\n """\n \n c = 0\n
skekre98
NORMAL
2018-11-29T02:37:54.558361+00:00
2018-11-29T02:37:54.558402+00:00
4,933
false
```\nclass Solution:\n def triangleNumber(self, nums):\n """\n :type nums: List[int]\n :rtype: int\n """\n \n c = 0\n n = len(nums)\n nums.sort()\n for i in range(n-1,1,-1):\n lo = 0\n hi = i - 1\n while lo < hi:\n ...
51
0
[]
3
valid-triangle-number
O(N^2) solution for C++ & Python
on2-solution-for-c-python-by-zqfan-ilia
c++\n\nclass Solution {\npublic:\n int triangleNumber(vector<int>& nums) {\n vector<int> snums(nums);\n sort(snums.begin(), snums.end());\n
zqfan
NORMAL
2017-06-11T09:14:14.015000+00:00
2018-09-09T21:40:28.333919+00:00
6,111
false
c++\n```\nclass Solution {\npublic:\n int triangleNumber(vector<int>& nums) {\n vector<int> snums(nums);\n sort(snums.begin(), snums.end());\n int count = 0;\n for ( int n = nums.size(), k = n - 1; k > 1; --k ) {\n int i = 0, j = k - 1;\n while ( i < j ) {\n ...
36
3
[]
4
valid-triangle-number
C++ || Detailed explanation || Two-pointer || Clear Intuitions
c-detailed-explanation-two-pointer-clear-4yue
*INTUITIONS:\n## We will understand with an example [1 , 2 , 3 , 5 , 6 , 7, 9]\n## After sorting , Take pointer left as 0 , take right as n-1 (intially as loop
KR_SK_01_In
NORMAL
2022-04-09T15:26:09.066606+00:00
2022-04-09T15:34:29.052232+00:00
2,145
false
# ****INTUITIONS:\n## We will understand with an example [1 , 2 , 3 , 5 , 6 , 7, 9]\n## After sorting , Take pointer left as 0 , take right as n-1 (intially as loop will be going for it from n-1 to 2) . Take mid as right-1 intially . \n\n## while(left<mid) , now check the nums[left] + nums[mid] > nums[right] , if this ...
23
0
['C', 'Sorting', 'C++']
3
valid-triangle-number
Python3 solution in O(n^2 log(n)) using bisect_left
python3-solution-in-on2-logn-using-bisec-n9ps
\nfrom bisect import bisect_left\n\nclass Solution:\n def triangleNumber(self, nums: List[int]) -> int:\n nums.sort()\n n = len(nums)\n
de_mexico
NORMAL
2019-05-01T18:06:44.316486+00:00
2019-05-01T18:06:44.316538+00:00
1,516
false
```\nfrom bisect import bisect_left\n\nclass Solution:\n def triangleNumber(self, nums: List[int]) -> int:\n nums.sort()\n n = len(nums)\n ans = 0\n for i in range(n):\n for j in range(i+1, n):\n k = bisect_left(nums, nums[i] + nums[j])\n ans += ma...
23
1
[]
2
valid-triangle-number
Python O( n^2 ) sol. based on sliding window and sorting. 85%+ [ With explanation ]
python-o-n2-sol-based-on-sliding-window-odl23
Python O( n^2 ) sol. based on sliding window and sorting.\n\nMain idea:\n1. [ Pre-processing ] Use in-place nums.sort() to keep numbers in ascending order\n\n2.
brianchiang_tw
NORMAL
2020-01-22T14:59:42.798892+00:00
2020-01-22T15:15:14.908861+00:00
2,083
false
Python O( n^2 ) sol. based on sliding window and sorting.\n\nMain idea:\n1. [ Pre-processing ] Use in-place nums.**sort()** to **keep numbers in ascending order**\n\n2. First **fix third edge with current largest one**, then **maintain a sliding window** to **compute all valid pairs** of **first edge** and **second edg...
22
0
['Sliding Window', 'Sorting', 'Python']
5
valid-triangle-number
C++ || Brut-force to optimal || Faster then 100.00%
c-brut-force-to-optimal-faster-then-1000-serz
Theorem: In a triangle, the length of any side is less than the sum of the other two sides.\n\n\nBrut-Force solutions:\nit give TLE\n\nclass Solution {\npublic
nitin23rathod
NORMAL
2022-06-30T11:20:59.653160+00:00
2022-06-30T11:26:03.548618+00:00
886
false
**Theorem**: In a triangle, the length of any side is less than the sum of the other two sides.\n![image](https://assets.leetcode.com/users/images/3e1c1958-53ca-4f57-8058-23fbe524cf97_1656587820.0885897.png)\n\n**Brut-Force solutions:**\nit give **TLE**\n```\nclass Solution {\npublic:\n// If the sum of any two side le...
19
0
['C']
0
valid-triangle-number
Solution Similar to Leetcode 259. 3Sum Smaller
solution-similar-to-leetcode-259-3sum-sm-gyx4
/** we need to find 3 number, i < j < k, and a[i] + a[j] > a[k];\n\t * if we sort the array, then we can easily use two pointer to find all the pairs we need.
helloworldzt
NORMAL
2017-06-11T03:19:02.722000+00:00
2017-06-11T03:19:02.722000+00:00
3,809
false
/** we need to find 3 number, i < j < k, and a[i] + a[j] > a[k];\n\t * if we sort the array, then we can easily use two pointer to find all the pairs we need.\n\t * if at some point a[left] + a[right] > a[i], all the elements from left to right-1 are valid.\n\t * because they are all greater then a[left];\n\t * s...
15
2
[]
3
valid-triangle-number
java binary search(log(n) * n^2) and two pointer (O(n^2))
java-binary-searchlogn-n2-and-two-pointe-6ue0
````\nBinary search: \nloop all the combination of the first two edges, and then binary search the position of third edge. It should be the last index that smal
seanshen20
NORMAL
2019-06-27T12:58:36.521314+00:00
2019-06-27T13:00:27.524015+00:00
1,058
false
````\nBinary search: \nloop all the combination of the first two edges, and then binary search the position of third edge. It should be the last index that smaller than sum(edge1 + edge2). \nIf there is no edge meet the a+b> c, then return -1. Count will not include return value of -1\n\nclass Solution {\n public i...
13
0
[]
0
valid-triangle-number
C++ O(n^2) | Solution with explanation | Easy to understand
c-on2-solution-with-explanation-easy-to-04gdz
So we have to give the count of all the 3 numbers from the array such that they form a triangle;\nFor 3 sides to form a triangle there are 3 main conditions:\n1
Job-lagwado
NORMAL
2022-02-06T21:08:45.370126+00:00
2022-02-06T21:16:41.587246+00:00
759
false
So we have to give the count of all the 3 numbers from the array such that they form a triangle;\nFor 3 sides to form a triangle there are 3 main conditions:\n1) **s1+s2>s3**\n2) **s2+s3>s1**\n3) **s3+s1>s2** (where s1, s2, s3 are the sides of triangle).\n\nIf you haven\'t tried the bruteforce method just take 3 "for" ...
12
0
['Two Pointers', 'C']
0
valid-triangle-number
C++ Optimal Solution || Easy to understand || Beginner's Friendly
c-optimal-solution-easy-to-understand-be-agvt
Please Upvote the solution if it helped you because it motivate the creators like us to produce more such content...........\n\nHappy Coding :-)\nCode->\n\nclas
lakshgaur
NORMAL
2022-06-02T08:21:31.060770+00:00
2022-06-02T08:21:31.060804+00:00
1,089
false
**Please Upvote the solution if it helped you because it motivate the creators like us to produce more such content...........**\n\n**Happy Coding :-)**\nCode->\n```\nclass Solution {\npublic:\n int triangleNumber(vector<int>& nums) {\n int n=nums.size();\n if(n<3) return 0;\n sort(nums.begin()...
10
0
['Two Pointers', 'C', 'Binary Tree', 'C++']
1
valid-triangle-number
💥[EXPLAINED] Runtime beats 95.00%
explained-runtime-beats-9500-by-r9n-cc5a
Intuition\nSort the array to simplify checking triangle validity. For each potential largest side, use two pointers to count valid triangles efficiently.\n\n# A
r9n
NORMAL
2024-08-23T01:33:30.354524+00:00
2024-08-26T19:51:46.147509+00:00
178
false
# Intuition\nSort the array to simplify checking triangle validity. For each potential largest side, use two pointers to count valid triangles efficiently.\n\n# Approach\n1 - Sort the array.\n\n2 - For each element as the largest side, use two pointers to find all pairs that form valid triangles.\n\n3 - Count valid tri...
8
0
['TypeScript']
0
valid-triangle-number
611: Solution with step by step explanation
611-solution-with-step-by-step-explanati-cns7
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n1. Sort the input array "nums" in ascending order.\n2. Initialize a varia
Marlen09
NORMAL
2023-03-17T17:15:40.754337+00:00
2023-03-17T17:16:42.769112+00:00
2,081
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Sort the input array "nums" in ascending order.\n2. Initialize a variable "count" to 0, which will keep track of the number of valid triangles.\n3. Loop through all possible triplets in the array, using two pointers "i" a...
8
0
['Array', 'Two Pointers', 'Binary Search', 'Python', 'Python3']
0
valid-triangle-number
[Python] O(N^2) Explanation with Diagram
python-on2-explanation-with-diagram-by-w-mid8
Let\'s assume that a triplet for-loop (i, j, k) is used. Now, consider the following case.\n\n\n\n2. What should we do next? We can either\n\n\t Advance j, then
wei_lun
NORMAL
2020-07-13T13:42:33.390360+00:00
2020-07-13T13:42:33.390394+00:00
691
false
1. Let\'s assume that a triplet for-loop (i, j, k) is used. Now, consider the following case.\n\n![image](https://assets.leetcode.com/users/images/cdcf7fab-b679-4bc8-bcec-d2490386f2f1_1594647564.0569782.png)\n\n2. What should we do next? We can either\n\n\t* Advance j, then bring k to j + 1, or - O(N)\n\t* Advance j, t...
8
0
[]
1
valid-triangle-number
✅💯🔥Simple Code🚀📌|| 🔥✔️Easy to understand🎯 || 🎓🧠Beats 97%🔥|| Beginner friendly💀💯
simple-code-easy-to-understand-beats-97-h1b21
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
atishayj4in
NORMAL
2024-07-27T20:55:09.474332+00:00
2024-08-01T19:50:07.966775+00:00
1,021
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
7
0
['Array', 'Two Pointers', 'Binary Search', 'Greedy', 'C', 'Sorting', 'Python', 'C++', 'Java']
0
valid-triangle-number
Easiest Python Solution to understand using two pointers
easiest-python-solution-to-understand-us-pqzj
\n# Code\n\nclass Solution:\n def triangleNumber(self, nums: List[int]) -> int:\n nums.sort()\n count = 0\n for k in range(len(nums) - 1
virendrapatil24
NORMAL
2024-05-18T12:39:40.970298+00:00
2024-05-18T12:39:40.970326+00:00
749
false
\n# Code\n```\nclass Solution:\n def triangleNumber(self, nums: List[int]) -> int:\n nums.sort()\n count = 0\n for k in range(len(nums) - 1, -1, -1):\n i = 0\n j = k - 1\n while i < j:\n if nums[i] + nums[j] > nums[k]:\n count +=...
7
0
['Two Pointers', 'Python3']
0
valid-triangle-number
Easy peasy python solution O(n^2) time, O(1) space with explanation, very similar to 3sum smaller
easy-peasy-python-solution-on2-time-o1-s-ua3f
\tdef triangleNumber(self, nums: List[int]) -> int:\n ln = len(nums)\n if ln < 3:\n return 0\n res = 0\n nums.sort()\n
lostworld21
NORMAL
2019-08-14T04:40:15.224165+00:00
2019-08-14T05:05:03.565151+00:00
777
false
\tdef triangleNumber(self, nums: List[int]) -> int:\n ln = len(nums)\n if ln < 3:\n return 0\n res = 0\n nums.sort()\n \n # target(nums[i]) is till index 2, because start and end should be different\n # for i == 2, start 0 and end = 1\n for i in range(l...
7
0
[]
2
valid-triangle-number
C++ Clean Code
c-clean-code-by-alexander-rhff
Binary Search O(N2lgN)\n\nclass Solution {\npublic:\n int triangleNumber(vector<int>& a) {\n int n = a.size();\n sort(a.begin(), a.end());\n
alexander
NORMAL
2017-06-11T03:18:53.286000+00:00
2017-06-11T03:18:53.286000+00:00
2,132
false
**Binary Search O(N<sup>2</sup>lgN)**\n```\nclass Solution {\npublic:\n int triangleNumber(vector<int>& a) {\n int n = a.size();\n sort(a.begin(), a.end());\n int res = 0;\n for (int i = 0; i < n - 2; i++) {\n for (int j = i + 1; j < n - 1; j++) {\n int sum = a[i...
7
1
[]
4
valid-triangle-number
Two-pointers approach explained || Beats 96.12%🔥✌️|| JAVA
two-pointers-approach-explained-beats-96-vbof
Intuition\n The problem requires us to find the number of valid triangles that can be formed using elements from the input array.\n We can use a two-pointer app
prateekrjt14
NORMAL
2024-02-15T04:43:19.325767+00:00
2024-02-15T04:43:19.325797+00:00
984
false
# Intuition\n* *The problem requires us to find the number of valid triangles that can be formed using elements from the input array.*\n* *We can use a two-pointer approach to efficiently find these triangles.*\n# Approach\n1. Sort the input array in non-decreasing order.\n2. Initialize a variable count to keep track o...
6
0
['Array', 'Two Pointers', 'Greedy', 'Sorting', 'Java']
2
valid-triangle-number
Fast & Easy Solution
fast-easy-solution-by-purandhar999-vziu
\n# Code\n\nclass Solution {\n public int triangleNumber(int[] a) {\n Arrays.sort(a);\n int n=a.length;\n int count=0;\n for(int
Purandhar999
NORMAL
2023-12-10T09:31:03.990553+00:00
2023-12-10T09:31:03.990577+00:00
690
false
\n# Code\n```\nclass Solution {\n public int triangleNumber(int[] a) {\n Arrays.sort(a);\n int n=a.length;\n int count=0;\n for(int i=n-1;i>=1;i--){\n int left=0,right=i-1;\n while(left<right){\n if(a[left]+a[right]>a[i]){\n count+=r...
6
0
['Java']
0
valid-triangle-number
Python - O(N^3) ➜ O(N^2 * LogN) ➜ O(N^2)
python-on3-on2-logn-on2-by-itsarvindhere-6hxb
1. BRUTE FORCE - O(N^3)\n\nThe most straightforward way is to have three nested loops and try to find all the combinations of sides a,b and c such that - \n\t\t
itsarvindhere
NORMAL
2022-10-20T09:05:14.536382+00:00
2022-10-20T10:30:05.365122+00:00
825
false
## **1. BRUTE FORCE - O(N^3)**\n\nThe most straightforward way is to have three nested loops and try to find all the combinations of sides a,b and c such that - \n\t\t\n\t\t\ta + b > c\n\t\t\ta + c > b\n\t\t\tb + c > a\n\t\t\t\nWill give TLE for large inputs.\n\n def triangleNumber(self, nums: List[int]) -> int:\n ...
6
0
['Binary Search', 'Binary Tree', 'Python']
1
valid-triangle-number
C++ Basic Simple Solution || Without Upper/Lower Bound
c-basic-simple-solution-without-upperlow-trlb
\nclass Solution {\npublic:\n int triangleNumber(vector<int>& nums) {\n sort(nums.begin() , nums.end()) ;\n int ans = 0 ;\n int n = nums
Maango16
NORMAL
2021-07-16T05:37:29.459117+00:00
2021-07-16T05:37:29.459159+00:00
181
false
```\nclass Solution {\npublic:\n int triangleNumber(vector<int>& nums) {\n sort(nums.begin() , nums.end()) ;\n int ans = 0 ;\n int n = nums.size() ;\n for(int k = 2 ; k < n ; k++)\n {\n int i = 0 , j = k - 1 ;\n while(i < j)\n {\n if(...
6
0
[]
0
valid-triangle-number
C++ Binary Search O(n^2logn)
c-binary-search-on2logn-by-shtanriverdi-jvc7
\nclass Solution {\npublic:\n int triangleNumber(vector<int>& nums) {\n sort(begin(nums), end(nums));\n int count = 0, len = nums.size(), wante
shtanriverdi
NORMAL
2021-07-15T20:28:17.686559+00:00
2021-07-15T20:28:17.686602+00:00
664
false
```\nclass Solution {\npublic:\n int triangleNumber(vector<int>& nums) {\n sort(begin(nums), end(nums));\n int count = 0, len = nums.size(), wanted;\n for (int i = 0; i < len - 2; i++) {\n for (int j = i + 1; j < len - 1; j++) {\n wanted = nums[i] + nums[j];\n ...
6
0
['C', 'Binary Tree']
0
valid-triangle-number
[Python3] Clean | Using bisect_left() | Binary Search
python3-clean-using-bisect_left-binary-s-h98d
This is the binary search solution using python module bisect_left()\n\nimport bisect as bs\nclass Solution:\n def triangleNumber(self, nums: List[int]) -> i
yadvendra
NORMAL
2021-07-15T18:59:34.013746+00:00
2021-07-16T05:02:30.264926+00:00
1,039
false
This is the binary search solution using python module [bisect_left()](https://docs.python.org/3/library/bisect.html)\n```\nimport bisect as bs\nclass Solution:\n def triangleNumber(self, nums: List[int]) -> int:\n n = len(nums)\n a= sorted(nums)\n count = 0\n for i in range(n):\n ...
6
1
['Binary Tree', 'Python', 'Python3']
0
valid-triangle-number
[Python] Two solutions with explanations
python-two-solutions-with-explanations-b-i66s
Sol 1. Binary Search\nPrior: If we know a < b < c , we know if a + b > c , then a, b, c can compose a valid triangle.\nExplanations: If we know the first two nu
codingasiangirll
NORMAL
2020-10-23T04:06:08.065955+00:00
2020-10-23T04:06:08.065994+00:00
681
false
Sol 1. Binary Search\n**Prior**: If we know `a < b < c` , we know if `a + b > c` , then `a, b, c` can compose a valid triangle.\n**Explanations**: If we know the first two numbers (`a, b`), then `c < a + b`. Since `nums` is sorted, we can find the left most upper bound and calculate the number of `c` that can compose v...
6
0
['Two Pointers', 'Binary Tree']
1
valid-triangle-number
[Python3] O(N^2) time solution
python3-on2-time-solution-by-ye15-zwrt
O(N^2) 94.94%\n\nclass Solution:\n def triangleNumber(self, nums: List[int]) -> int:\n nums.sort()\n ans = 0\n for i in range(len(nums))
ye15
NORMAL
2020-10-08T03:36:44.342776+00:00
2021-07-15T19:50:43.681211+00:00
792
false
`O(N^2)` 94.94%\n```\nclass Solution:\n def triangleNumber(self, nums: List[int]) -> int:\n nums.sort()\n ans = 0\n for i in range(len(nums)): \n lo, hi = 0, i-1\n while lo < hi: \n if nums[lo] + nums[hi] > nums[i]:\n ans += hi - lo \n ...
6
0
['Python3']
5
valid-triangle-number
C++ CODE || Binary Search
c-code-binary-search-by-chiikuu-1h0u
Complexity\n- Time complexity: O(n*n(logn))\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(1)\n Add your space complexity here, e.g. O(n)
CHIIKUU
NORMAL
2023-02-13T13:59:16.693437+00:00
2023-02-13T13:59:16.693473+00:00
1,297
false
# Complexity\n- Time complexity: **O(n*n(logn))**\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: **O(1)**\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n int triangleNumber(vector<int>& nums) {\n sort(nums.begin(),nums.end());\...
5
0
['Math', 'Binary Search', 'C++']
0
valid-triangle-number
C++ || Two Pointer || Easy Understanding || Fast
c-two-pointer-easy-understanding-fast-by-nbsi
Intuition\n Describe your first thoughts on how to solve this problem. \nTWO POINTER APPROACH ON DESCENDING ORDER ARRAY\n\n# Approach\n Describe your approach t
Asad9113
NORMAL
2022-12-07T17:02:21.711324+00:00
2022-12-07T17:02:21.711368+00:00
1,457
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTWO POINTER APPROACH ON DESCENDING ORDER ARRAY\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe all know that two pointer approach works easily on sorted array, but here is a trick we have to sort in descending o...
5
0
['Two Pointers', 'C++']
1
valid-triangle-number
Java using two pointer | Easy and Clean code
java-using-two-pointer-easy-and-clean-co-xsr9
\nclass Solution {\n public int triangleNumber(int[] a) {\n Arrays.sort(a);\n int n=a.length;\n int count=0;\n for(int i=n-1;i>=1
rohitkumarsingh369
NORMAL
2021-07-16T06:21:29.467606+00:00
2021-07-16T06:59:21.354543+00:00
820
false
```\nclass Solution {\n public int triangleNumber(int[] a) {\n Arrays.sort(a);\n int n=a.length;\n int count=0;\n for(int i=n-1;i>=1;i--){\n int left=0,right=i-1;\n while(left<right){\n if(a[left]+a[right]>a[i]){\n count+=right-left;...
5
0
['Java']
0
valid-triangle-number
[C++] Easy, Clean Solution in O(n^2)
c-easy-clean-solution-in-on2-by-rsgt24-4hg2
Solution:\n\nclass Solution {\npublic:\n int triangleNumber(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n int ans = 0;\n for(
rsgt24
NORMAL
2021-07-15T07:39:03.507141+00:00
2021-07-15T07:45:18.692635+00:00
597
false
**Solution:**\n```\nclass Solution {\npublic:\n int triangleNumber(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n int ans = 0;\n for(int i = 2; i < nums.size(); i++){\n int l = 0, r = i - 1;\n while(l < r){\n if(nums[l] + nums[r] > nums[i]){\n ...
5
0
['C', 'Sorting']
0
valid-triangle-number
c++, O(n^2)
c-on2-by-dayao-mw4p
\nclass Solution {\npublic:\n int triangleNumber(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n int res = 0;\n for (int i = nu
dayao
NORMAL
2019-10-06T08:49:24.094906+00:00
2019-10-06T08:49:24.094943+00:00
596
false
```\nclass Solution {\npublic:\n int triangleNumber(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n int res = 0;\n for (int i = nums.size() - 1; i > 1; i--) {\n int l = 0;\n int r = i - 1;\n while (l < r) {\n if (nums[l] + nums[r] > nums[i...
5
0
[]
1
valid-triangle-number
Python, Straightforward with Explanation
python-straightforward-with-explanation-6t4cu
Sort the array. For every pair of sticks u, v with stick u occuring before v (u <= v), we want to know how many w occuring after v have w < u + v.\n\nFor every
awice
NORMAL
2017-06-11T19:12:51.916000+00:00
2017-06-11T19:12:51.916000+00:00
2,730
false
Sort the array. For every pair of sticks u, v with stick u occuring before v (u <= v), we want to know how many w occuring after v have w < u + v.\n\nFor every middle stick B[j] = v, we can use two pointers: one pointer i going down from j to 0, and one pointer k going from the end to j. This is because if we have al...
5
2
[]
1
valid-triangle-number
Brute-Better-Optimal || Beats 99.13% 🔥 || Java, C++ || Explanation & Code || Two-Pointers
brute-better-optimal-beats-9913-java-c-e-1y5o
Intuition\n- This problem asks to find the number of valid triangles which can be formed using array elements as length of sides of triangles.\n- We can solve t
girish13
NORMAL
2024-02-18T11:19:08.312809+00:00
2024-02-18T11:32:36.484893+00:00
208
false
# Intuition\n- This problem asks to find the number of valid triangles which can be formed using array elements as length of sides of triangles.\n- We can solve this problem using three approaches, each approach giving us a better time complexity.\n\n\n# Brute Force Approach\n\n1. Sort the input array `nums` in ascendi...
4
0
['Two Pointers', 'Binary Search', 'Greedy', 'Sorting', 'C++', 'Java']
0
valid-triangle-number
Full Explanation + Python 2 line
full-explanation-python-2-line-by-speedy-yx22
Solution 1 :\n## What Q asked to do :\n\nnums = [0,0,10,15,17,19,20,22,25,26,30,31,37] (sorted version)\n\nIf nums[i] is not 0 as with 0 length a triangle is no
speedyy
NORMAL
2023-05-25T15:06:33.518247+00:00
2023-05-25T19:06:13.261082+00:00
737
false
## Solution 1 :\n## What Q asked to do :\n```\nnums = [0,0,10,15,17,19,20,22,25,26,30,31,37] (sorted version)\n\nIf nums[i] is not 0 as with 0 length a triangle is not possible :\n_________________________________________________________________\n\nfor a triangle we need 3 length and if a+b>c and a+c>b and b+c>a BUT si...
4
0
['C++', 'Python3']
0
valid-triangle-number
C++ Solution
c-solution-by-pranto1209-056c
Code\n\nclass Solution {\npublic:\n int triangleNumber(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n int ans = 0;\n for(int k
pranto1209
NORMAL
2023-04-30T15:23:48.829663+00:00
2023-04-30T15:23:48.829708+00:00
267
false
# Code\n```\nclass Solution {\npublic:\n int triangleNumber(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n int ans = 0;\n for(int k = nums.size() - 1; k > 1; k--) {\n int i = 0, j = k-1;\n while(i < j) {\n if(nums[i] + nums[j] > nums[k]) {\n ...
4
0
['C++']
0
valid-triangle-number
Simple C++ Solution with comments
simple-c-solution-with-comments-by-divya-23dz
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
Divyanshu_singh_cs
NORMAL
2023-02-19T14:42:02.383185+00:00
2023-02-19T14:42:02.383228+00:00
1,457
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
4
0
['C++']
1
valid-triangle-number
C++ || Binary search || Easy approach
c-binary-search-easy-approach-by-mrigank-pqnw
Here is my c++ code for this problem.\n\n# Complexity\n- Time complexity:O(NNlogN)\n\n- Space complexity:O(1)\n\n# Code\n\nclass Solution {\npublic:\n int tr
mrigank_2003
NORMAL
2022-12-17T06:30:44.057320+00:00
2022-12-17T06:30:44.057357+00:00
991
false
Here is my c++ code for this problem.\n\n# Complexity\n- Time complexity:O(N*N*logN)\n\n- Space complexity:O(1)\n\n# Code\n```\nclass Solution {\npublic:\n int triangleNumber(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n int ans=0;\n for(int i=0; i<nums.size(); i++){\n for(...
4
0
['Binary Search', 'Greedy', 'C++']
0