post_href
stringlengths
57
213
python_solutions
stringlengths
71
22.3k
slug
stringlengths
3
77
post_title
stringlengths
1
100
user
stringlengths
3
29
upvotes
int64
-20
1.2k
views
int64
0
60.9k
problem_title
stringlengths
3
77
number
int64
1
2.48k
acceptance
float64
0.14
0.91
difficulty
stringclasses
3 values
__index_level_0__
int64
0
34k
https://leetcode.com/problems/number-of-provinces/discuss/2543862/Python-Elegant-and-Short-or-DSU
class Solution: """ Time: O(n^2) Memory: O(n) """ def findCircleNum(self, isConnected: List[List[int]]) -> int: n = len(isConnected) dsu = DSU(n) for i in range(n): for j in range(n): if isConnected[i][j]: dsu.union(i, j) return len({dsu.find(i) for i in range(n)})
number-of-provinces
Python Elegant & Short | DSU
Kyrylo-Ktl
2
392
number of provinces
547
0.634
Medium
9,600
https://leetcode.com/problems/number-of-provinces/discuss/2159710/Number-of-Provinces-or-Python-or-Union-Find-or-Count-of-connected-components
class Solution: def findCircleNum(self, isConnected: List[List[int]]) -> int: # set all parents value to 1 that is node is a parent of itself par=[i for i in range(len(isConnected))] # rank is the length of the particular component at that index, initially all are disjoint components so default to 1 ...
number-of-provinces
Number of Provinces | Python | Union Find | Count of connected components
glimloop
2
160
number of provinces
547
0.634
Medium
9,601
https://leetcode.com/problems/number-of-provinces/discuss/1781252/Python-Union-by-rank-and-path-compression-O(N)
class Solution: def findCircleNum(self, isConnected: List[List[int]]) -> int: class UnionFind: # Constructor of Union-find. The size is the length of the root array. def __init__(self, size): self.root = [i for i in range(size)] self.rank = [1]*size ...
number-of-provinces
[Python] Union by rank and path compression O(N)
haydarevren
2
121
number of provinces
547
0.634
Medium
9,602
https://leetcode.com/problems/number-of-provinces/discuss/1726574/Python-3-DFS-with-meaningful-variable-names
class Solution: def findCircleNum(self, isConnected: List[List[int]]) -> int: n = len(isConnected) nb_provinces = 0 def dfs(city_a: int) -> None: for city_b in range(n): if isConnected[city_a][city_b] == 1: isConnected[city_a][city_b] ...
number-of-provinces
Python 3 DFS with meaningful variable names
thomasthiebaud
2
76
number of provinces
547
0.634
Medium
9,603
https://leetcode.com/problems/number-of-provinces/discuss/2435895/Number-of-provinces-oror-Python3-oror-Union-Find
class Solution: def findCircleNum(self, isConnected: List[List[int]]) -> int: n = len(isConnected) parent = [i for i in range(0, n)] rank = [0] * n # Initially there are n components components = n # Only traversing half of 2d array, since edges are bidirect...
number-of-provinces
Number of provinces || Python3 || Union-Find
vanshika_2507
1
27
number of provinces
547
0.634
Medium
9,604
https://leetcode.com/problems/number-of-provinces/discuss/1615407/Python-Solution-Faster-than-99
class Solution: def findCircleNum(self, isConnected: List[List[int]]) -> int: def dfsHelper(v,visited,isConnected): # dfs search for all the connected components visited[v]=True for neigh in range(len(isConnected[v])): if visited[neigh]==False and...
number-of-provinces
Python Solution Faster than 99%
Zach0787
1
245
number of provinces
547
0.634
Medium
9,605
https://leetcode.com/problems/number-of-provinces/discuss/1586277/Python-or-DFS-or-Simple-Solution-or-Easy-To-Understand
class Solution: def findCircleNum(self, isConnected: List[List[int]]) -> int: def find_connected_cities(row): for column in range(len(isConnected[0])): if isConnected[row][column] == 1 and self.visited[column] != True: self.visited[column] = True find_connected_cities(column) self.vi...
number-of-provinces
Python | DFS | Simple Solution | Easy To Understand
Call-Me-AJ
1
207
number of provinces
547
0.634
Medium
9,606
https://leetcode.com/problems/number-of-provinces/discuss/874019/Python3-dfs-(99.75)
class Solution: def findCircleNum(self, M: List[List[int]]) -> int: # graph as adjacency matrix n = len(M) # number of people in total def fn(i): """Group (direct & indirect) friends.""" seen[i] = True # mark visited for ii in range(n): ...
number-of-provinces
[Python3] dfs (99.75%)
ye15
1
255
number of provinces
547
0.634
Medium
9,607
https://leetcode.com/problems/number-of-provinces/discuss/874019/Python3-dfs-(99.75)
class Solution: def findCircleNum(self, isConnected: List[List[int]]) -> int: n = len(isConnected) ans = 0 visited = [False]*n for x in range(n): if not visited[x]: ans += 1 stack = [x] visited[x] = True whi...
number-of-provinces
[Python3] dfs (99.75%)
ye15
1
255
number of provinces
547
0.634
Medium
9,608
https://leetcode.com/problems/number-of-provinces/discuss/2833400/Python-BFS-approach
class Solution: def findCircleNum(self, isConnected: List[List[int]]) -> int: visited = set() queue = [] num = 0 for i in range(len(isConnected)): if isConnected[i][i] == 1 and i not in visited: queue.append(i) visited.add(i) ...
number-of-provinces
Python BFS approach
paul1202
0
2
number of provinces
547
0.634
Medium
9,609
https://leetcode.com/problems/number-of-provinces/discuss/2831177/BFSDFSUnion-Find-for-number-of-Components
class Solution: "BFS" def bfs(self, isConnected): n = len(isConnected) visited = set() ans = 0 for i in range(n): if i in visited: continue q = deque([i]) while q: n = q.popleft() visited.add(n) ...
number-of-provinces
BFS/DFS/Union-Find for number of Components
MACHMichael
0
4
number of provinces
547
0.634
Medium
9,610
https://leetcode.com/problems/number-of-provinces/discuss/2824984/Python-BFS-Solution
class Solution: def findCircleNum(self, isConnected: List[List[int]]) -> int: graph = {i+1:[] for i in range(len(isConnected))} for i in range(1, len(isConnected)+1): for j in range(1, len(isConnected[i-1])+1): if i == j: continue if is...
number-of-provinces
Python BFS Solution
vijay_2022
0
2
number of provinces
547
0.634
Medium
9,611
https://leetcode.com/problems/number-of-provinces/discuss/2818533/Python-DFS
class Solution: def findCircleNum(self, isConnected: List[List[int]]) -> int: ROW, COL = len(isConnected), len(isConnected[0]) count = 0 visited = set() def dfs(i): visited.add(i) for j in range(ROW): if isConnected[i][j] and j not in visited: ...
number-of-provinces
Python DFS
zananpech9
0
3
number of provinces
547
0.634
Medium
9,612
https://leetcode.com/problems/number-of-provinces/discuss/2812508/simple-Dfs-solution
class Solution: def dfs(grid,visited,i): for j in range(len(grid[i])): if grid[i][j]==1 and visited[j]==0: visited[j]=1 visited=Solution.dfs(grid,visited,j) # print(visited) return visited def findCircleNum(self, isConnected: List[List...
number-of-provinces
simple Dfs solution
althrun
0
2
number of provinces
547
0.634
Medium
9,613
https://leetcode.com/problems/number-of-provinces/discuss/2812499/python-solution-with-DFS
class Solution: def dfs(grid,visited,i): for j in range(len(grid[i])): if grid[i][j]==1 and visited[j]==0: visited[j]=1 Solution.dfs(grid,visited,j) def findCircleNum(self, isConnected: List[List[int]]) -> int: grid=isConnected q=[] m=...
number-of-provinces
python solution with DFS
althrun
0
1
number of provinces
547
0.634
Medium
9,614
https://leetcode.com/problems/number-of-provinces/discuss/2811710/DFS-Python
class Solution: def findCircleNum(self, isConnected: List[List[int]]) -> int: n = len(isConnected) adjMatrix = [ [] for i in range(n)] for i in range(n): for j in range(n): if i != j and isConnected[i][j]: adjMatrix[i].append(j) #pr...
number-of-provinces
DFS - Python
ysreddy
0
1
number of provinces
547
0.634
Medium
9,615
https://leetcode.com/problems/number-of-provinces/discuss/2739539/Simple-Python-BFS
class Solution: def bfs(self, isConnected, index_source, visited): if index_source in visited: return visited.add(index_source) for i in range(len(isConnected[index_source])): if isConnected[index_source][i] == 1: self.bfs(isConnected, i, visited) def findCircleNum(self, is...
number-of-provinces
Simple Python BFS
LuraMisner
0
9
number of provinces
547
0.634
Medium
9,616
https://leetcode.com/problems/number-of-provinces/discuss/2735841/Python-BFS-easy-understanding
class Solution: def findCircleNum(self, isConnected: List[List[int]]) -> int: n = len(isConnected) adjList = {i:[] for i in range(1, n+1)} for i in range(len(isConnected)): for j in range(len(isConnected)): if (isConnected[i][j] == 1 and (i != j)...
number-of-provinces
Python BFS easy understanding
logeshsrinivasans
0
13
number of provinces
547
0.634
Medium
9,617
https://leetcode.com/problems/number-of-provinces/discuss/2700460/Python-Solution-or-DFS
class Solution: def findCircleNum(self, isConnected: List[List[int]]) -> int: row = len(isConnected) col = len(isConnected[0]) ans = 0 seen = set() def dfs(i): for j in range(col): if isConnected[i][j] == 1 and i != j and j not in seen: ...
number-of-provinces
Python Solution | DFS
maomao1010
0
12
number of provinces
547
0.634
Medium
9,618
https://leetcode.com/problems/number-of-provinces/discuss/2662696/Python3-Disjoint-Set-with-Path-Compression-and-Union-By-Rank
class Solution: def find(self, x): if x == self.root[x]: return x self.root[x] = self.find(self.root[x]) return self.root[x] def union(self, x, y): rootX = self.find(x) rootY = self.find(y) if rootX != rootY: if self.rank[rootX] > se...
number-of-provinces
Python3 Disjoint Set with Path Compression and Union By Rank
PartialButton5
0
3
number of provinces
547
0.634
Medium
9,619
https://leetcode.com/problems/number-of-provinces/discuss/2615744/Simple-O(Nlog(N))-Union-Find-Python-Solution
class Solution(object): def findCircleNum(self, isConnected): N = len(isConnected) parents = list(range(N)) rank = [1] * N def find(x): while x != parents[x]: x = parents[x] return x def union(x, y): x = fi...
number-of-provinces
Simple O(Nlog(N)) Union-Find Python Solution
tomascf
0
57
number of provinces
547
0.634
Medium
9,620
https://leetcode.com/problems/number-of-provinces/discuss/2481080/Runtime%3A-194-ms-faster-than-95.28-Memory-Usage%3A-14.8-MB-less-than-21.29
class Solution: def findCircleNum(self, isConnected: List[List[int]]) -> int: n = len(isConnected) visited = set() def getStart(): i = 0 while i < n: if i not in visited: return i ...
number-of-provinces
Runtime: 194 ms, faster than 95.28%; Memory Usage: 14.8 MB, less than 21.29%
GizDave
0
18
number of provinces
547
0.634
Medium
9,621
https://leetcode.com/problems/number-of-provinces/discuss/2406406/Readable-broken-into-smaller-sub-logics-with-complexity-analysis-python3
class Solution: # O(n^2) time, # O(n) space, # Approach: DFS, hashset def findCircleNum(self, isConnected: List[List[int]]) -> int: n = len(isConnected) unexplored = set([i for i in range(1, n+1)]) def findNeighbours(root): row = isConnected[root-1] ...
number-of-provinces
Readable, broken into smaller sub logics with complexity analysis, python3
destifo
0
9
number of provinces
547
0.634
Medium
9,622
https://leetcode.com/problems/number-of-provinces/discuss/2402866/python-very-slow-solution-13..need-help-to-optimize
class Solution: def findCircleNum(self, isConnected: List[List[int]]) -> int: d={} #here is making dictionary holds city as key and nabours as a value for i in range(len(isConnected)): d[i]=[] for j in range(len(isConnected)): if i==j: con...
number-of-provinces
python very slow solution 13%😔..need help to optimize
benon
0
27
number of provinces
547
0.634
Medium
9,623
https://leetcode.com/problems/number-of-provinces/discuss/2360269/Python3-oror-2-Approaches-oror-BFS-oror-Union-Find
class Solution: def findCircleNum(self, isConnected: List[List[int]]) -> int: n = len(isConnected) adj_list = collections.defaultdict(list) for i in range(n): for j in range(n): if i != j: if isConnected[i][j] == 1: adj_...
number-of-provinces
Python3 || 2 Approaches || BFS || Union Find
s_m_d_29
0
104
number of provinces
547
0.634
Medium
9,624
https://leetcode.com/problems/number-of-provinces/discuss/2354556/Python-DFS-Beats-98-with-full-working-solution
class Solution: def findCircleNum(self, isConnected: List[List[int]]) -> int: # Time: O(n*m) and Space: O(n) seen = set() def dfs(node): # isConnected[node][nei]: nei is the index at node 0 index of the list, and adj is the value at that index for nei, adj in enumerate(isConnected...
number-of-provinces
Python [DFS / Beats 98%] with full working solution
DanishKhanbx
0
79
number of provinces
547
0.634
Medium
9,625
https://leetcode.com/problems/number-of-provinces/discuss/2350013/547.-My-Python-Solution-with-comments
class Solution: def findCircleNum(self, isConnected: List[List[int]]) -> int: # typical union find algo n = len(isConnected) # initialize disjoint set, with parent of x is x itself parent = {x:x for x in range(n)} for i in range(n): for j in range(i+1,n): ...
number-of-provinces
547. My Python Solution with comments
JunyiLin
0
18
number of provinces
547
0.634
Medium
9,626
https://leetcode.com/problems/number-of-provinces/discuss/2297949/python-dfs-solution
class Solution: def findCircleNum(self, isConnected: List[List[int]]) -> int: n = len(isConnected[0]) d={i:[] for i in range(n)} for i in range(n): for j in range(n): if isConnected[i][j]!=0 and j!=i: d[i].append(j) visit=set()...
number-of-provinces
python dfs solution
sprasu21
0
35
number of provinces
547
0.634
Medium
9,627
https://leetcode.com/problems/number-of-provinces/discuss/2297281/Java-Python-Optimized-Space-O(n)-Time-Easy-to-Understand
class Solution: def findCircleNum(self, mat: List[List[int]]) -> int: parent = [x for x in range(len(mat) + 1)] def find(x: int) -> int: while parent[x] != x: #Wire x to point to its grandparent, which may #be identical to parent. Then move x up g...
number-of-provinces
[Java, Python] Optimized Space, O(n) Time, Easy to Understand
arceusx
0
22
number of provinces
547
0.634
Medium
9,628
https://leetcode.com/problems/number-of-provinces/discuss/2171734/Solved-using-Connected-Components-oror-DFS
class Solution: def dfs(self, comp, i, graph, visited): visited[i] = True comp.append(i) for neighbour in graph[i]: if visited[neighbour] == False: comp = self.dfs(comp, neighbour, graph, visited) return comp def findCircleNum(self, isCon...
number-of-provinces
Solved using Connected Components || DFS
Vaibhav7860
0
38
number of provinces
547
0.634
Medium
9,629
https://leetcode.com/problems/number-of-provinces/discuss/2136147/Python-DFS-modifying-the-array
class Solution: def findCircleNum(self, isConnected: List[List[int]]) -> int: def dfs(city): isConnected[city][city] = 0 for c in range(n): if isConnected[city][c]: isConnected[city][c] = 0 ...
number-of-provinces
Python, DFS modifying the array
blue_sky5
0
50
number of provinces
547
0.634
Medium
9,630
https://leetcode.com/problems/number-of-provinces/discuss/2015486/Easy-DFS-Solution(90-faster-and-90-less-memory)
class Solution: def dfs(self, isConnected, i): self.visited[i] = True # Visit all the neighbours of city i and there neighbours for j in range(self.n): if isConnected[i][j] == 1 and not self.visited[j]: self.dfs(isConnected, j) def findCircl...
number-of-provinces
Easy DFS Solution(90% faster and 90% less memory)
dbansal18
0
35
number of provinces
547
0.634
Medium
9,631
https://leetcode.com/problems/number-of-provinces/discuss/2006811/Union-Find-or-DSU-or-Easy-code
class Solution: def findCircleNum(self, isConnected: List[List[int]]) -> int: f = {} def find(x): f.setdefault(x, x) if f[x] != x: f[x] = find(f[x]) return f[x] def union(x, y): f[find(x)] = find(y) for i in ran...
number-of-provinces
Union Find | DSU | Easy code
divyanshugairola
0
47
number of provinces
547
0.634
Medium
9,632
https://leetcode.com/problems/number-of-provinces/discuss/1963382/dfs-and-union-find-python-solution
class Solution: def findCircleNum(self, isConnected: List[List[int]]) -> int: # In this questions first take count=len(isConnected) i.e. No. of different components. #Than keeps on decrementing it if they have not same parents bcoz we have to find no. of #different componenets. # Method ...
number-of-provinces
dfs and union find , python solution
Aniket_liar07
0
79
number of provinces
547
0.634
Medium
9,633
https://leetcode.com/problems/number-of-provinces/discuss/1815856/python3-DFS-defeat-80-with-explanation
class Solution: def findCircleNum(self, isConnected: List[List[int]]) -> int: cities = len(isConnected) ans = 0 white, grey, black = set(range(cities)), set(), set() def dfs(city, root = False): nonlocal white, grey, black, ans related = isConnected[city] ...
number-of-provinces
python3 DFS defeat 80% with explanation
752937603
0
73
number of provinces
547
0.634
Medium
9,634
https://leetcode.com/problems/number-of-provinces/discuss/1769241/Python-DFS-Solution
class Solution: def findCircleNum(self, isConnected: List[List[int]]) -> int: def dfs(node): for i in range(0,len(isConnected[node])): if i != node and isConnected[node][i] == 1 and i not in s: s.add(i) dfs(i) prov...
number-of-provinces
Python DFS Solution
DietCoke777
0
82
number of provinces
547
0.634
Medium
9,635
https://leetcode.com/problems/number-of-provinces/discuss/1611936/This-Question-is-eerily-Similar-to-323-and-261-with-Solution
class Solution: def findCircleNum(self, isConnected: List[List[int]]) -> int: # Graph initialization graph = {x:[] for x in range(len(isConnected))} # Here graph is given in Adjacency matrix form, we would build accordingly. for i in range(len(isConnected)): for j in range(len(isCon...
number-of-provinces
This Question is eerily Similar to 323 and 261 [with Solution]
pratushah
0
66
number of provinces
547
0.634
Medium
9,636
https://leetcode.com/problems/number-of-provinces/discuss/1481461/Python3-DFS-solution
class Solution: def __init__(self): self.seen = set() def dfs(self, isConnected, i): self.seen.add(i) for j in range(len(isConnected[i])): if isConnected[i][j] == 1 and j not in self.seen: self.dfs(isConnected, j) def ...
number-of-provinces
[Python3] DFS solution
maosipov11
0
66
number of provinces
547
0.634
Medium
9,637
https://leetcode.com/problems/number-of-provinces/discuss/1460323/Python-using-Union-Find-base-methods
class Solution: ''' cities belong to one province if they're drectly or indirectly connected => we need to count only distinct provincies. And due to construction of Disjoint Set, `self.root` will contain roots of provincies. Ex: [1, 2, 3, 4] with [[1,1,0,0], [1,1,0,1], [0,0,1,0], [...
number-of-provinces
Python using Union Find base methods
SleeplessChallenger
0
97
number of provinces
547
0.634
Medium
9,638
https://leetcode.com/problems/number-of-provinces/discuss/1422691/Short-Python-DFS-beats-99.24
class Solution: def findCircleNum(self, isConnected: List[List[int]]) -> int: def dfs(node): self.visited[node] = True for i in range(n): if isConnected[node][i] and not self.visited[i]: dfs(i) ret = 0 n = len(isConnected) s...
number-of-provinces
Short Python DFS beats 99.24%
Charlesl0129
0
246
number of provinces
547
0.634
Medium
9,639
https://leetcode.com/problems/number-of-provinces/discuss/1418547/Just-use-DFS.-That's-it.-Simple-readable-solution.
class Solution: def dfs(self, edges: List[set], is_visited: set, node: int): if node not in is_visited: is_visited.add(node) for adj_node in edges[node]: self.dfs(edges, is_visited, adj_node) return def makeGraph(self, isConnected: List[List[int]...
number-of-provinces
Just use DFS. That's it. Simple readable solution.
ssshukla26
0
81
number of provinces
547
0.634
Medium
9,640
https://leetcode.com/problems/number-of-provinces/discuss/1414849/Python3-dfs-solution
class Solution: def findCircleNum(self, isConnected: List[List[int]]) -> int: def dfs(i,isConnected,visited): st = [] for j,k in enumerate(isConnected[i]): if j != i and j not in visited and k == 1: visited.append(j) st.append(j...
number-of-provinces
Python3 dfs solution
EklavyaJoshi
0
29
number of provinces
547
0.634
Medium
9,641
https://leetcode.com/problems/number-of-provinces/discuss/1394460/Python-or-DFS
class Solution(object): def findCircleNum(self, isConnected): """ :type isConnected: List[List[int]] :rtype: int """ g={} for i in range(len(isConnected)): for j in range(len(isConnected[i])): if isConnected[i][j]==1 and i!=j: ...
number-of-provinces
Python | DFS
nmk0462
0
161
number of provinces
547
0.634
Medium
9,642
https://leetcode.com/problems/number-of-provinces/discuss/1049167/Python-Find-union-with-path-compression-and-union-by-rank
class Solution: def find(self, x: int) -> int: """ C = cities Time: O(1) amortized time """ if self.parents[x] != x: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x: int, y: int) -> bool: """...
number-of-provinces
Python, Find union with path compression and union by rank
arauter
0
96
number of provinces
547
0.634
Medium
9,643
https://leetcode.com/problems/student-attendance-record-i/discuss/356636/Solution-in-Python-3-(one-line)
class Solution: def checkRecord(self, s: str) -> bool: return (s.count('A') < 2) and ('LLL' not in s) - Junaid Mansuri (LeetCode ID)@hotmail.com
student-attendance-record-i
Solution in Python 3 (one line)
junaidmansuri
16
940
student attendance record i
551
0.481
Easy
9,644
https://leetcode.com/problems/student-attendance-record-i/discuss/1407236/Python-or-Faster-Than-100-or-Runtime-%3A-12-ms
class Solution: def checkRecord(self, s: str) -> bool: d=0 for i in range(len(s)): if s[i]=='A': d+=1 if d==2: return False if i>=2 and s[i]==s[i-1]==s[i-2]=='L': return False return True
student-attendance-record-i
Python | Faster Than 100% | Runtime : 12 ms
satyamshrma
4
224
student attendance record i
551
0.481
Easy
9,645
https://leetcode.com/problems/student-attendance-record-i/discuss/1290958/Python3-greedy
class Solution: def checkRecord(self, s: str) -> bool: absent = late = 0 for i, ch in enumerate(s): if ch == "A": absent += 1 elif ch == "L": if i == 0 or s[i-1] != "L": cnt = 0 cnt += 1 late = max(late, cnt) return ...
student-attendance-record-i
[Python3] greedy
ye15
1
95
student attendance record i
551
0.481
Easy
9,646
https://leetcode.com/problems/student-attendance-record-i/discuss/1290958/Python3-greedy
class Solution: def checkRecord(self, s: str) -> bool: return s.count("A") < 2 and "LLL" not in s
student-attendance-record-i
[Python3] greedy
ye15
1
95
student attendance record i
551
0.481
Easy
9,647
https://leetcode.com/problems/student-attendance-record-i/discuss/1236515/Python3-simple-solution-%22One-liner%22-beats-95-users
class Solution: def checkRecord(self, s: str) -> bool: return s.count('A') < 2 and 'LLL' not in s
student-attendance-record-i
Python3 simple solution "One-liner" beats 95% users
EklavyaJoshi
1
37
student attendance record i
551
0.481
Easy
9,648
https://leetcode.com/problems/student-attendance-record-i/discuss/1017572/Simple-Solution-faster-than-99.83
class Solution: def checkRecord(self, s: str) -> bool: if s.count("A")>1: return False for i in range(len(s)-2): if s[i]==s[i+1]==s[i+2]=="L": return False return True
student-attendance-record-i
Simple Solution- faster than 99.83%
thisisakshat
1
107
student attendance record i
551
0.481
Easy
9,649
https://leetcode.com/problems/student-attendance-record-i/discuss/429080/Python-one-line
class Solution: def checkRecord(self, s): return not ('LLL' in s or s.count('A') > 1)
student-attendance-record-i
Python one-line
domthedeveloper
1
86
student attendance record i
551
0.481
Easy
9,650
https://leetcode.com/problems/student-attendance-record-i/discuss/2798011/2-lines-O(n)
class Solution: def checkRecord(self, s: str) -> bool: if s.count('A')>=2 or "LLL" in s: return False return True
student-attendance-record-i
2 lines, O(n)
albararamli
0
2
student attendance record i
551
0.481
Easy
9,651
https://leetcode.com/problems/student-attendance-record-i/discuss/2696561/Python-One-Pass-Solution
class Solution: def checkRecord(self, s: str) -> bool: lates = absences = 0 for status in s: if status == 'L': lates += 1 if lates >= 3: return False continue if status == 'A': absences += 1 ...
student-attendance-record-i
Python One Pass Solution
kcstar
0
6
student attendance record i
551
0.481
Easy
9,652
https://leetcode.com/problems/student-attendance-record-i/discuss/2647251/python-solution
class Solution: def checkRecord(self, s: str) -> bool: p=s.count("A") r="LLL" if p>=2 or r in s: return False return True
student-attendance-record-i
python solution
kissi616
0
6
student attendance record i
551
0.481
Easy
9,653
https://leetcode.com/problems/student-attendance-record-i/discuss/2584184/Python-3-One-liner-%2B-descriptive-Solution
class Solution: def checkRecord(self, s: str) -> bool: #return s.count("A") < 2 and s.count("LLL") < 1 absent = late = 0 for i in s: if i == "L": late+=1 elif i== "A": ...
student-attendance-record-i
Python 3 One liner + descriptive Solution
abhisheksanwal745
0
16
student attendance record i
551
0.481
Easy
9,654
https://leetcode.com/problems/student-attendance-record-i/discuss/2491612/Python3-faster-than-88
class Solution(object): def checkRecord(self, s): """ :type s: str :rtype: bool """ count_abscence = 0 previous_pos = -1 count_late = 0 for pos, i in enumerate(s): if i == 'A': count_abscence += 1 if count_a...
student-attendance-record-i
Python3 faster than 88%
gourav14051992
0
18
student attendance record i
551
0.481
Easy
9,655
https://leetcode.com/problems/student-attendance-record-i/discuss/2410191/Python3-easy-solution
class Solution: def checkRecord(self, s: str) -> bool: absent = 0 for i in range(len(s)): if i+2<len(s) and s[i]=='L' and s[i+1]=="L" and s[i+2]=="L": return False if s[i]=='A': absent+=1 if absent >=2: return False ...
student-attendance-record-i
Python3 easy solution
shacid
0
19
student attendance record i
551
0.481
Easy
9,656
https://leetcode.com/problems/student-attendance-record-i/discuss/2081200/Python-2-solutions%3A-Basic-and-Oneliner
class Solution: def checkRecord(self, s: str) -> bool: A,L = 0,0 for i in s: if i =='A': A+=1 elif i=='L': L+=1 if i!='L': L=0 if A>=2 or L>=3: return False return True
student-attendance-record-i
[Python] 2 solutions: Basic and Oneliner
rtyagi1
0
59
student attendance record i
551
0.481
Easy
9,657
https://leetcode.com/problems/student-attendance-record-i/discuss/2081200/Python-2-solutions%3A-Basic-and-Oneliner
class Solution: def checkRecord2(self, s:str) ->bool: return s.count('A') <= 1 and s.count('LLL') == 0
student-attendance-record-i
[Python] 2 solutions: Basic and Oneliner
rtyagi1
0
59
student attendance record i
551
0.481
Easy
9,658
https://leetcode.com/problems/student-attendance-record-i/discuss/2042359/Python-oneliner
class Solution: def checkRecord(self, s: str) -> bool: return False if 'LLL' in s or s.count('A') >= 2 else True
student-attendance-record-i
Python oneliner
StikS32
0
44
student attendance record i
551
0.481
Easy
9,659
https://leetcode.com/problems/student-attendance-record-i/discuss/1903039/Easiest-and-Simplest-Python3-Solution-oror-Beginner-Friendly-Code-oror-Very-Easy
class Solution: def checkRecord(self, s: str) -> bool: cL=0 cA=0 l=0 a=0 for i in s: if i=='L': cL=cL+1 l=cL elif i=='A': cA=cA+1 l=cL a=cA cL=0 ...
student-attendance-record-i
Easiest & Simplest Python3 Solution || Beginner Friendly Code || Very Easy
RatnaPriya
0
32
student attendance record i
551
0.481
Easy
9,660
https://leetcode.com/problems/student-attendance-record-i/discuss/1719206/Python3-accepted-solution
class Solution: def checkRecord(self, s: str) -> bool: return True if(s.count("A")<2 and s.count("LLL")==0)else False
student-attendance-record-i
Python3 accepted solution
sreeleetcode19
0
39
student attendance record i
551
0.481
Easy
9,661
https://leetcode.com/problems/student-attendance-record-i/discuss/1687314/Python3-100-faster-with-explanation
class Solution: def checkRecord(self, s: str) -> bool: absent, late = 0,0 for x in s: if x == 'A': absent += 1 if absent >= 2: return False if x == 'L': late += 1 if late >= 3: ...
student-attendance-record-i
Python3, 100% faster with explanation
cvelazquez322
0
90
student attendance record i
551
0.481
Easy
9,662
https://leetcode.com/problems/student-attendance-record-i/discuss/1411326/Python-Solution-oror-Easy-to-understand
class Solution: def checkRecord(self, s: str) -> bool: x = s.count('A') for i in range (len(s)-2): if (s[i]==s[i+1]==s[i+2] == 'L'): return False if x<2: return True return False
student-attendance-record-i
Python Solution || Easy to understand
adityarichhariya7879
0
102
student attendance record i
551
0.481
Easy
9,663
https://leetcode.com/problems/student-attendance-record-i/discuss/1273534/Easy-Python-Solution(28ms)
class Solution: def checkRecord(self, s: str) -> bool: c=Counter(s) if(c['A']>=2): return False for i in range(len(s)): if(s[i]=='L' and i<len(s)-2): if(s[i+1]=='L' and i<len(s)-1): if(s[i+2]=='L' and i<len(s)): ...
student-attendance-record-i
Easy Python Solution(28ms)
Sneh17029
0
80
student attendance record i
551
0.481
Easy
9,664
https://leetcode.com/problems/student-attendance-record-i/discuss/1238245/Python-oror-99.24-faster
class Solution: def checkRecord(self, s: str) -> bool: if s.count('A')<2 and s.count('L')<3: return True elif s.count('A')<2 and s.count('L')>2: for c in range(3,s.count('L')+1): temp ='L'*c if temp in s: return False ...
student-attendance-record-i
Python || 99.24% faster
dhrumilg699
0
61
student attendance record i
551
0.481
Easy
9,665
https://leetcode.com/problems/student-attendance-record-i/discuss/1230393/PYTHON-3-FASTER-THAN-99.8-OF-CODE
class Solution: def checkRecord(self, s: str) -> bool: if s.count('A') < 2 and s.count('LLL') < 1: return True return False
student-attendance-record-i
PYTHON 3 FASTER THAN 99.8% OF CODE
ehtesham22
0
27
student attendance record i
551
0.481
Easy
9,666
https://leetcode.com/problems/student-attendance-record-i/discuss/1133191/Easy-and-simple-python
class Solution: def checkRecord(self, s: str) -> bool: if s.count("A") > 1 or "LLL" in s: return False return True
student-attendance-record-i
Easy and simple python
pheobhe
0
34
student attendance record i
551
0.481
Easy
9,667
https://leetcode.com/problems/student-attendance-record-i/discuss/794955/Python3-98-faster
class Solution: def checkRecord(self, s: str) -> bool: if 'LLL' not in s: c=0 for i in s: if i=='A': c+=1 if c==2: return False return True return False
student-attendance-record-i
Python3 98% faster
SurajJadhav7
0
68
student attendance record i
551
0.481
Easy
9,668
https://leetcode.com/problems/student-attendance-record-i/discuss/645907/Python3-simple-solution-using-counter
class Solution: def checkRecord(self, s: str) -> bool: checks = Counter(s) if 'LLL' in s or checks['A'] > 1: return False return True
student-attendance-record-i
Python3 simple solution using counter
pythongo
0
77
student attendance record i
551
0.481
Easy
9,669
https://leetcode.com/problems/student-attendance-record-i/discuss/583978/Simple-Python-Solution
class Solution: def checkRecord(self, s: str) -> bool: if s.count('A')>1: return False l=[] for i in range(len(s)): if s[i]=='L': l.append(i) for i in range(len(l)-2): if l[i]+1==l[i+1] an...
student-attendance-record-i
Simple Python Solution
Ayu-99
0
51
student attendance record i
551
0.481
Easy
9,670
https://leetcode.com/problems/student-attendance-record-i/discuss/528631/Python-One-Line-(-Ternary-)
class Solution: def checkRecord(self, s: str) -> bool: return False if s.count('A')>=2 or 'LLL' in s else True
student-attendance-record-i
Python One Line ( Ternary )
bachana3435
0
61
student attendance record i
551
0.481
Easy
9,671
https://leetcode.com/problems/student-attendance-record-i/discuss/395804/Python%3A-32-ms-faster-than-92.22-of-submissions
class Solution: def __init__(self): self.absent = False self.late = 0 def checkRecord(self, s: str) -> bool: for c in s: if c == 'P': self.late = 0 elif c == 'A': if self.absent: return False ...
student-attendance-record-i
Python: 32 ms, faster than 92.22% of submissions
btjd
0
107
student attendance record i
551
0.481
Easy
9,672
https://leetcode.com/problems/student-attendance-record-i/discuss/1299074/Python3-dollarolution
class Solution: def checkRecord(self, s: str) -> bool: a, l = 0, 0 for i in range(len(s)): if s[i] == 'A': a += 1 if l < 3: l = 0 elif s[i] == 'L': if l == 0: l += 1 elif l...
student-attendance-record-i
Python3 $olution
AakRay
-1
70
student attendance record i
551
0.481
Easy
9,673
https://leetcode.com/problems/student-attendance-record-ii/discuss/356750/Solution-in-Python-3-(five-lines)-(with-explanation)
class Solution: def checkRecord(self, n: int) -> int: C, m = [1,1,0,1,0,0], 10**9 + 7 for i in range(n-1): a, b = sum(C[:3]) % m, sum(C[3:]) % m C = [a, C[0], C[1], a + b, C[3], C[4]] return (sum(C) % m)
student-attendance-record-ii
Solution in Python 3 (five lines) (with explanation)
junaidmansuri
12
1,900
student attendance record ii
552
0.412
Hard
9,674
https://leetcode.com/problems/student-attendance-record-ii/discuss/827561/Python-solution-with-explanation
class Solution: def checkRecord(self, n: int) -> int: """ Suppose dp[i] is the number of all the rewarded sequences without 'A' having their length equals to i, then we have: 1. Number of sequence ends with 'P': dp[i - 1] 2. Number of sequence ends with 'L': ...
student-attendance-record-ii
Python solution with explanation
eroneko
9
458
student attendance record ii
552
0.412
Hard
9,675
https://leetcode.com/problems/student-attendance-record-ii/discuss/1290973/Python3-top-down-dp
class Solution: def checkRecord(self, n: int) -> int: @lru_cache(100) def fn(i, absent, late): """Return number of attendance eligible for award.""" if i == n: return 1 ans = fn(i+1, absent, 0) # present if absent == 0: ans += fn(i+1, 1, 0) ...
student-attendance-record-ii
[Python3] top-down dp
ye15
5
357
student attendance record ii
552
0.412
Hard
9,676
https://leetcode.com/problems/student-attendance-record-ii/discuss/1290973/Python3-top-down-dp
class Solution: def checkRecord(self, n: int) -> int: dp = [1, 2, 4] for i in range(3, n+1): dp.append((dp[i-3] + dp[i-2] + dp[i-1]) % 1_000_000_007) ans = dp[n] for i in range(n): ans = (ans + dp[i] * dp[n-1-i]) % 1_000_000_007 return ans
student-attendance-record-ii
[Python3] top-down dp
ye15
5
357
student attendance record ii
552
0.412
Hard
9,677
https://leetcode.com/problems/student-attendance-record-ii/discuss/1290973/Python3-top-down-dp
class Solution: def checkRecord(self, n: int) -> int: f0, f1, f2 = 1, 1, 0 g0, g1, g2 = 1, 0, 0 for _ in range(n-1): f0, f1, f2, g0, g1, g2 = (f0+f1+f2) % 1_000_000_007, f0, f1, (f0+f1+f2+g0+g1+g2) % 1_000_000_007, g0, g1 return (f0+f1+f2+g0+g1+g2) % 1_000_000_007
student-attendance-record-ii
[Python3] top-down dp
ye15
5
357
student attendance record ii
552
0.412
Hard
9,678
https://leetcode.com/problems/student-attendance-record-ii/discuss/1962121/Python-or-DFS-%2B-Memo-(TLE)-or-DP-(success)-or-Simple-comments
class Solution: def checkRecord(self, n: int) -> int: hsh = {} if n==1: return 3 md = (10**9)+7 def helper(rem,countA,endL): if countA>1 or endL>=3: return 0 if (rem,countA,endL) in hsh: return hsh[(rem,...
student-attendance-record-ii
Python | DFS + Memo (TLE) | DP (success) | Simple comments
vishyarjun1991
3
304
student attendance record ii
552
0.412
Hard
9,679
https://leetcode.com/problems/student-attendance-record-ii/discuss/1962121/Python-or-DFS-%2B-Memo-(TLE)-or-DP-(success)-or-Simple-comments
class Solution: def checkRecord(self, n: int) -> int: dp = [1,1,0,1,0,0] md = (10**9)+7 def calculate_dp(): nonlocal dp # carefully look at how case 1 to 5 is handled here and try to justify the reason, pls comment incase if this is not clear # make sure to % dp at every st...
student-attendance-record-ii
Python | DFS + Memo (TLE) | DP (success) | Simple comments
vishyarjun1991
3
304
student attendance record ii
552
0.412
Hard
9,680
https://leetcode.com/problems/student-attendance-record-ii/discuss/1091774/Python-DP-solution-O(n)-time-with-detailed-comments
class Solution: def checkRecord(self, n: int) -> int: # dptotal[i] the number of rewardable records without A whose lenghth is i dptotal = [0] * (n + 1) dp1,dp2,dp3 = 1,1,0 # dp1: the number of rewardable records that end with one L and without A # dp2: the number of reward...
student-attendance-record-ii
Python DP solution O(n) time with detailed comments
yuhaogogo123
2
467
student attendance record ii
552
0.412
Hard
9,681
https://leetcode.com/problems/student-attendance-record-ii/discuss/1914899/Python-Clean-and-Simple!-(DP)
class Solution: def checkRecord(self, n): return sum(map(self.check,product(["A","L","P"],repeat=n))) def check(self, s): return not ('LLL' in s or s.count('A') > 1)
student-attendance-record-ii
Python - Clean and Simple! (DP)
domthedeveloper
1
255
student attendance record ii
552
0.412
Hard
9,682
https://leetcode.com/problems/student-attendance-record-ii/discuss/1914899/Python-Clean-and-Simple!-(DP)
class Solution: def checkRecord(self, n): mod = lambda x : x % (10**9+7) dp = [1, 1, 0, 1, 0, 0] for i in range(2, n+1): dp = [mod(sum(dp[:3])), dp[0], dp[1], mod(sum(dp)), dp[3], dp[4]] return mod(sum(dp))
student-attendance-record-ii
Python - Clean and Simple! (DP)
domthedeveloper
1
255
student attendance record ii
552
0.412
Hard
9,683
https://leetcode.com/problems/student-attendance-record-ii/discuss/1914899/Python-Clean-and-Simple!-(DP)
class Solution: def checkRecord(self, n): mod = lambda x : x % (10**9+7) absentZero = [1, 1, 0] absentOnce = [1, 0, 0] for i in range(2, n+1): absentZero, absentOnce = \ [mod(sum(absentZero)), absentZero[0], absentZero[1]], \ [mod(sum(absentZero+...
student-attendance-record-ii
Python - Clean and Simple! (DP)
domthedeveloper
1
255
student attendance record ii
552
0.412
Hard
9,684
https://leetcode.com/problems/student-attendance-record-ii/discuss/2792165/Python-oror-Recursion-%2B-Memoization-oror-DP
class Solution: mod = 1000000007 dp = {} def checkRecord(self, n: int) -> int: def helper(curr_n, leave_taken, late_count): if (curr_n, leave_taken, late_count) in self.dp: return self.dp[(curr_n, leave_taken, late_count)] #Student cannot take more than 2 leav...
student-attendance-record-ii
Python || Recursion + Memoization || DP
buggybot
0
10
student attendance record ii
552
0.412
Hard
9,685
https://leetcode.com/problems/student-attendance-record-ii/discuss/2721748/Top-Down-DP-Python-Memoization-O(n)
class Solution: def checkRecord(self, n: int) -> int: self.dp = defaultdict(int) self.dp[0] = 1 self.dp[1] = 2 self.dp[2] = 4 self.dp[3] = 7 self.helper(n) total = self.dp[n] for i in range(1, n + 1): total += self.dp[i - 1] * self.dp[n ...
student-attendance-record-ii
Top-Down DP - Python - Memoization - O(n)
sehi05
0
18
student attendance record ii
552
0.412
Hard
9,686
https://leetcode.com/problems/optimal-division/discuss/1265206/Python3-string-concatenation
class Solution: def optimalDivision(self, nums: List[int]) -> str: if len(nums) <= 2: return "/".join(map(str, nums)) return f'{nums[0]}/({"/".join(map(str, nums[1:]))})'
optimal-division
[Python3] string concatenation
ye15
3
86
optimal division
553
0.597
Medium
9,687
https://leetcode.com/problems/optimal-division/discuss/1265206/Python3-string-concatenation
class Solution: def optimalDivision(self, nums: List[int]) -> str: return "/".join(map(str, nums)) if len(nums) <= 2 else f'{nums[0]}/({"/".join(map(str, nums[1:]))})'
optimal-division
[Python3] string concatenation
ye15
3
86
optimal division
553
0.597
Medium
9,688
https://leetcode.com/problems/optimal-division/discuss/1265206/Python3-string-concatenation
class Solution: def optimalDivision(self, nums: List[int]) -> str: @cache def fn(lo, hi): """Return max division of nums[lo:hi].""" if lo + 1 == hi: return str(nums[lo]) ans = "-inf" for mid in range(lo+1, hi): cand = fn(lo,...
optimal-division
[Python3] string concatenation
ye15
3
86
optimal division
553
0.597
Medium
9,689
https://leetcode.com/problems/optimal-division/discuss/362944/Solution-in-Python-3-(beats-~93)-(one-line)
class Solution: def optimalDivision(self, n: List[int]) -> str: return f'{n[0]}/({"/".join(map(str,n[1:]))})' if len(n)>2 else "/".join(map(str,n)) - Junaid Mansuri (LeetCode ID)@hotmail.com
optimal-division
Solution in Python 3 (beats ~93%) (one line)
junaidmansuri
2
243
optimal division
553
0.597
Medium
9,690
https://leetcode.com/problems/optimal-division/discuss/1347507/oror-python-oror-clean-and-short-solution-oror-explained
class Solution(object): def optimalDivision(self, nums): A = list(map(str, nums)) if len(A) <= 2: return '/'.join(A) return A[0] + '/(' + '/'.join(A[1:]) + ')'
optimal-division
✅ || python || clean and short solution || explained
chikushen99
1
162
optimal division
553
0.597
Medium
9,691
https://leetcode.com/problems/optimal-division/discuss/404017/Python-3-.-Make-a-division-of-1st-element-by-2nd-element-to-the-last-one-.-.-Return
class Solution(object): def optimalDivision(self, nums): """ :type nums: List[int] :rtype: str """ list_length=len(nums) if list_length==1: return str(nums[0]) elif list_length==2: return str(nums[0])+'/'+str(nums[1]) else: ...
optimal-division
Python 3 . Make a division of 1st element by 2nd element to the last one . . Return
mathewjose09
1
182
optimal division
553
0.597
Medium
9,692
https://leetcode.com/problems/optimal-division/discuss/2830571/Python-(Simple-Maths)
class Solution: def optimalDivision(self, nums): n, k = len(nums), str(nums[0]) + "/(" if n == 1: return str(nums[0]) elif n == 2: return str(nums[0]) + "/" + str(nums[1]) else: for i in nums[1:]: k += str(i) + "/" return ...
optimal-division
Python (Simple Maths)
rnotappl
0
2
optimal division
553
0.597
Medium
9,693
https://leetcode.com/problems/optimal-division/discuss/1223726/Simple-8-lines-solution-97-fast
class Solution: def optimalDivision(self, nums: List[int]) -> str: s1,s=str(nums[0]),'' if(len(nums)==2): return str(nums[0])+'/'+str(nums[1]) for i in range(1,len(nums)): s=s+str(nums[i])+'/' if(len(s)>0): s1=s1+'/'+'('+s[:-1]+')' ...
optimal-division
Simple 8 lines solution 97% fast
Rajashekar_Booreddy
0
90
optimal division
553
0.597
Medium
9,694
https://leetcode.com/problems/optimal-division/discuss/1201982/slow-sol-but-dp-approach
class Solution: def optimalDivision(self, nums: List[int]) -> str: n=len(nums) new=[[(0,"") for i in range(0,n)] for i in range(0,n)] for i in range(0,n): new[i][i]=(nums[i],str(nums[i])) for i in range(0,n-1): for j in range(i+1,n): new[i][j]=...
optimal-division
slow sol but dp approach
heisenbarg
0
63
optimal division
553
0.597
Medium
9,695
https://leetcode.com/problems/brick-wall/discuss/1736767/python-easy-hashmap-solution
class Solution: def leastBricks(self, wall: List[List[int]]) -> int: count = defaultdict(int) tot = len(wall) if tot == 1 and len(wall[0]) > 1: return 0 elif tot == 1 and len(wall[0]) == 1: return 1 for w in wall: s = 0 ...
brick-wall
python easy hashmap solution
byuns9334
1
62
brick wall
554
0.532
Medium
9,696
https://leetcode.com/problems/brick-wall/discuss/1172325/python-easy-understanding-omn-solution
class Solution: def leastBricks(self, wall: List[List[int]]) -> int: table = dict() for row in wall: tmp_sum = 0 for item in row: tmp_sum += item if tmp_sum not in table: table[tmp_sum] = 1 else: ...
brick-wall
python easy understanding omn solution
yingziqing123
1
68
brick wall
554
0.532
Medium
9,697
https://leetcode.com/problems/brick-wall/discuss/393969/Solution-in-Python-3-(three-lines)
class Solution: def leastBricks(self, W: List[List[int]]) -> int: L, C = len(W), collections.defaultdict(int) for w in W: s = 0 for b in w: s += b; C[s] += 1 C[s] = 0 return L - max(C.values())
brick-wall
Solution in Python 3 (three lines)
junaidmansuri
1
581
brick wall
554
0.532
Medium
9,698
https://leetcode.com/problems/brick-wall/discuss/393969/Solution-in-Python-3-(three-lines)
class Solution: def leastBricks(self, W: List[List[int]]) -> int: L, C = len(W), collections.Counter(sum([list(itertools.accumulate(w)) for w in W],[])) C[sum(W[0])] = 0 return L - max(C.values()) - Junaid Mansuri (LeetCode ID)@hotmail.com
brick-wall
Solution in Python 3 (three lines)
junaidmansuri
1
581
brick wall
554
0.532
Medium
9,699