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/count-good-nodes-in-binary-tree/discuss/2511667/Easy-python-solution | class Solution:
def goodNodes(self, root: TreeNode) -> int:
def pre(root,mx):
if root is None:
return 0
z=0
if root.val>=mx:
z=1
mx=root.val
x=pre(root.left,mx)
y=pre(root.right,mx)
return x+y+z
return pre(root,root.val) | count-good-nodes-in-binary-tree | Easy python solution | shubham_1307 | 0 | 4 | count good nodes in binary tree | 1,448 | 0.746 | Medium | 21,600 |
https://leetcode.com/problems/count-good-nodes-in-binary-tree/discuss/2511403/Python-or-Recursive-DFS | class Solution:
def goodNodes(self, root: TreeNode) -> int:
def dfs(node: TreeNode = root, maxVal: int = root.val):
nonlocal goodCount
if node is None:
return
if node.val >= maxVal:
goodCount += 1
maxVal = max(maxVal, node.val)
for child in [node.left, node.right]:
dfs(child, maxVal)
goodCount = 0; dfs()
return goodCount | count-good-nodes-in-binary-tree | Python | Recursive DFS | sr_vrd | 0 | 8 | count good nodes in binary tree | 1,448 | 0.746 | Medium | 21,601 |
https://leetcode.com/problems/count-good-nodes-in-binary-tree/discuss/2465958/Python-Recursive-and-Iterative-DFS-Solutions | class Solution:
def goodNodes(self, root: TreeNode) -> int:
self.res = 0
def dfs(root, max_val):
if root is None:
return 0
if root.val >= max_val:
self.res += 1
max_val = root.val
dfs(root.left, max_val)
dfs(root.right, max_val)
dfs(root, float("-inf"))
return self.res | count-good-nodes-in-binary-tree | Python Recursive and Iterative DFS Solutions | PythonicLava | 0 | 45 | count good nodes in binary tree | 1,448 | 0.746 | Medium | 21,602 |
https://leetcode.com/problems/count-good-nodes-in-binary-tree/discuss/2465958/Python-Recursive-and-Iterative-DFS-Solutions | class Solution:
def goodNodes(self, root: TreeNode) -> int:
if root is None:
return 0
stack = []
stack.append((root, float("-inf")))
res = 0
while stack:
node, max_val = stack.pop()
if node.val >= max_val:
res += 1
max_val = node.val
if node.left:
stack.append((node.left, max_val))
if node.right:
stack.append((node.right, max_val))
return res | count-good-nodes-in-binary-tree | Python Recursive and Iterative DFS Solutions | PythonicLava | 0 | 45 | count good nodes in binary tree | 1,448 | 0.746 | Medium | 21,603 |
https://leetcode.com/problems/count-good-nodes-in-binary-tree/discuss/2295635/python-DFS-Recursive | class Solution:
def goodNodes(self, root: TreeNode) -> int:
def dfs(node, maxVal):
if not node:
return 0
res = 1 if node.val >= maxVal else 0
maxVal = max(node.val, maxVal)
res += dfs(node.left, maxVal)
res += dfs(node.right, maxVal)
return res
return dfs(root, root.val) | count-good-nodes-in-binary-tree | python DFS Recursive | soumyadexter7 | 0 | 28 | count good nodes in binary tree | 1,448 | 0.746 | Medium | 21,604 |
https://leetcode.com/problems/count-good-nodes-in-binary-tree/discuss/2262765/Easy-DFS-solution | class Solution:
def goodNodes(self, root: TreeNode) -> int:
def dfs(node,maxval):
if not node:
return 0
res = 1 if node.val>=maxval else 0
maxval = max(node.val, maxval)
res+= dfs(node.left,maxval)
res+=dfs(node.right, maxval)
return res
return dfs(root, root.val) | count-good-nodes-in-binary-tree | Easy DFS solution | Abhi_009 | 0 | 24 | count good nodes in binary tree | 1,448 | 0.746 | Medium | 21,605 |
https://leetcode.com/problems/form-largest-integer-with-digits-that-add-up-to-target/discuss/1113027/Python3-top-down-dp | class Solution:
def largestNumber(self, cost: List[int], target: int) -> str:
@cache
def fn(x):
"""Return max integer given target x."""
if x == 0: return 0
if x < 0: return -inf
return max(fn(x - c) * 10 + i + 1 for i, c in enumerate(cost))
return str(max(0, fn(target))) | form-largest-integer-with-digits-that-add-up-to-target | [Python3] top-down dp | ye15 | 1 | 117 | form largest integer with digits that add up to target | 1,449 | 0.472 | Hard | 21,606 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/1817462/Python-Simple-Solution-or-Zip-and-Iterate-86-37ms | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
count = 0 # If a value meets the criteria, one will be added here.
for x, y in zip(startTime, endTime): # Zipping the two lists to allow us to iterate over them using x,y as our variables.
if x <= queryTime <= y: # Checking if the queryTime number is between startTime and endTime, adding one to count if it is.
count += 1
return count # Returning the value in count | number-of-students-doing-homework-at-a-given-time | Python Simple Solution | Zip & Iterate - 86% 37ms | IvanTsukei | 4 | 110 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,607 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/1070363/Python-91-faster | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
ans = 0
for i,j in enumerate(range(len(startTime))):
if queryTime in range(startTime[i], endTime[j]+1):
ans += 1
return ans | number-of-students-doing-homework-at-a-given-time | Python 91% faster | brimarcosano | 2 | 98 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,608 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/2514100/Simple-python-solution | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
count = 0
for i in range(len(startTime)):
if queryTime <= endTime[i] and queryTime >= startTime[i]:
count += 1
return count | number-of-students-doing-homework-at-a-given-time | Simple python solution | aruj900 | 1 | 74 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,609 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/2259847/Python-simple-solution | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
ans = 0
for i in range(len(startTime)):
if startTime[i] <= queryTime <= endTime[i]:
ans += 1
return ans | number-of-students-doing-homework-at-a-given-time | Python simple solution | StikS32 | 1 | 28 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,610 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/1243270/Fastest-97.67-Python3-one-liner-solution | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
return sum(s<=queryTime<=e for s,e in zip(startTime, endTime)) | number-of-students-doing-homework-at-a-given-time | Fastest 97.67% Python3 one liner solution | dhrumilg699 | 1 | 78 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,611 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/1027246/Python3-easy-to-understand-solution | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
count = 0
for i in range(len(startTime)):
if startTime[i] <= queryTime and endTime[i] >= queryTime:
count += 1
return count | number-of-students-doing-homework-at-a-given-time | Python3 easy to understand solution | EklavyaJoshi | 1 | 109 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,612 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/636323/Python3-one-line | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
return sum(s <= queryTime <= e for s, e in zip(startTime, endTime)) | number-of-students-doing-homework-at-a-given-time | [Python3] one line | ye15 | 1 | 35 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,613 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/2843279/Python-One-liner | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
return sum((1 for s, e in zip(startTime, endTime) if s <= queryTime <= e)) | number-of-students-doing-homework-at-a-given-time | [Python] One-liner | nonchalant-enthusiast | 0 | 1 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,614 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/2825956/Easy-solution | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
count=0
for i in range (0,len(startTime)):
if startTime[i]<=queryTime and queryTime<=endTime[i]:
count+=1
return count | number-of-students-doing-homework-at-a-given-time | Easy solution | nishithakonuganti | 0 | 1 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,615 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/2814663/python-super-easy-using-time-line-%2B-binary-search | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
time_line = {}
for s in startTime:
if s not in time_line:
time_line[s] = 1
else:
time_line[s] += 1
for e in endTime:
if e+1 not in time_line:
time_line[e+1] = -1
else:
time_line[e+1] -=1
time_line = sorted(map(lambda x:list(x), time_line.items()), key = lambda x: x[0])
for i in range(1, len(time_line)):
time_line[i][1] += time_line[i-1][1]
index = bisect.bisect_right(time_line, queryTime, key = lambda x:x[0])
if index > 0:
return time_line[index-1][1]
return 0 | number-of-students-doing-homework-at-a-given-time | python super easy using time line + binary search | harrychen1995 | 0 | 2 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,616 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/2804134/Python-or-LeetCode-or-1450.-Number-of-Students-Doing-Homework-at-a-Given-Time-(Easy) | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
count = 0
for i in range(len(startTime)):
if startTime[i] <= queryTime and queryTime <= endTime[i]:
count += 1
return count | number-of-students-doing-homework-at-a-given-time | Python | LeetCode | 1450. Number of Students Doing Homework at a Given Time (Easy) | UzbekDasturchisiman | 0 | 3 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,617 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/2715417/Python-Simple-Solution-or-Time-Complexity%3A-O(N)-Space-Complexity%3A-O(1) | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
noOfStudents = 0
for i in range(len(startTime)): # len(endTime) could have been used as well
if startTime[i] <= queryTime <= endTime[i]:
noOfStudents += 1
return noOfStudents | number-of-students-doing-homework-at-a-given-time | Python Simple Solution | Time Complexity: O(N), Space Complexity: O(1) | Haste | 0 | 4 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,618 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/2612924/Optimized-to-minimize-number-of-evaluations | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
count = 0
for i in range(len(endTime)):
if endTime[i] < queryTime:
continue
if startTime[i] <= queryTime:
count += 1
return count | number-of-students-doing-homework-at-a-given-time | Optimized to minimize number of evaluations | kcstar | 0 | 8 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,619 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/2555603/Python-82.9-Faster | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
#keep a count of students
cstudent = 0
#zip lists and check ranges
for st,et in zip(startTime,endTime):
if queryTime >= st and queryTime <= et:
cstudent = cstudent + 1
return(cstudent) | number-of-students-doing-homework-at-a-given-time | Python 82.9% Faster | ovidaure | 0 | 20 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,620 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/2462954/Python-Solution-easyv-oror-short-and-clean-code | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
c=0
for i in range(len(startTime)):
if startTime[i]<=queryTime and queryTime<=endTime[i]:
c+=1
return c | number-of-students-doing-homework-at-a-given-time | Python Solution - easyv || short and clean code | T1n1_B0x1 | 0 | 13 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,621 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/2461209/Less-memory-usage-13.8-MB | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
count = 0
for i in range(len(startTime)):
if startTime[i] <= queryTime <= endTime[i]:
count += 1
return count | number-of-students-doing-homework-at-a-given-time | Less memory usage 13.8 MB | samanehghafouri | 0 | 2 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,622 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/2446513/c%2B%2B-andand-python-code | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
hash_map = {}
for i in range(1 , 1001):
hash_map[i] = 0
for i in range(0 , len(startTime)):
for j in range(startTime[i] , endTime[i] + 1):
hash_map[j] += 1
return hash_map[queryTime] | number-of-students-doing-homework-at-a-given-time | c++ && python code | akashp2001 | 0 | 16 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,623 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/2433232/Python3-Solution-with-using-array-traverse | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
res = 0
for i in range(len(startTime)):
if startTime[i] <= queryTime <= endTime[i]:
res += 1
return res | number-of-students-doing-homework-at-a-given-time | [Python3] Solution with using array traverse | maosipov11 | 0 | 8 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,624 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/2405859/Python3-Easy-One-Liner | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
return sum([startTime[i] <= queryTime <= endTime[i] for i in range(len(startTime))]) | number-of-students-doing-homework-at-a-given-time | [Python3] Easy One-Liner | ivnvalex | 0 | 10 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,625 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/2321015/Simple-python | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
c=0
l=len(startTime)
for i in range(l):
if (queryTime>=startTime[i]) & (queryTime<=endTime[i]):
c+=1
return c | number-of-students-doing-homework-at-a-given-time | Simple python | sunakshi132 | 0 | 23 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,626 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/2095036/PYTHON-or-Simple-python-solution | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
res = 0
for i in range(len(startTime)):
if queryTime >= startTime[i] and queryTime <= endTime[i]:
res += 1
return res | number-of-students-doing-homework-at-a-given-time | PYTHON | Simple python solution | shreeruparel | 0 | 57 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,627 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/2029108/Beginner-Python-3-Line-Code-O(N)-Time | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
count = 0
for i , j in zip(startTime,endTime):
if i <= queryTime <= j:
count += 1
return count | number-of-students-doing-homework-at-a-given-time | Beginner Python 3 Line Code O(N) Time | itsmeparag14 | 0 | 23 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,628 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/1927918/easy-python-code | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
count = 0
for i in range(len(startTime)):
if startTime[i]<=queryTime and endTime[i]>=queryTime:
count += 1
return count | number-of-students-doing-homework-at-a-given-time | easy python code | dakash682 | 0 | 20 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,629 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/1890721/Python-Simple-and-Clean!-One-Liner | class Solution:
def busyStudent(self, s, e, x):
c = 0
for a, b in zip(s, e):
c += int(a <= x <= b)
return c | number-of-students-doing-homework-at-a-given-time | Python - Simple and Clean! One-Liner | domthedeveloper | 0 | 26 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,630 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/1890721/Python-Simple-and-Clean!-One-Liner | class Solution:
def busyStudent(self, s, e, x):
return sum(t[0] <= x <= t[1] for t in zip(s, e)) | number-of-students-doing-homework-at-a-given-time | Python - Simple and Clean! One-Liner | domthedeveloper | 0 | 26 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,631 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/1884287/Python-simple-solution-using-if | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
count = 0
for i in range(len(startTime)):
if queryTime >= startTime[i] and queryTime <= endTime[i]:
count += 1
return count | number-of-students-doing-homework-at-a-given-time | Python simple solution using if | alishak1999 | 0 | 14 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,632 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/1833567/1-Line-Python-Solution-oror-60-Faster-oror-Memory-less-than-85 | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
return sum([x<=queryTime<=y for x,y in list(zip(startTime,endTime))]) | number-of-students-doing-homework-at-a-given-time | 1-Line Python Solution || 60% Faster || Memory less than 85% | Taha-C | 0 | 20 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,633 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/1802180/Python-O(n)-Solution | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
result = 0
for i in range(len(startTime)):
if endTime[i] <= queryTime:
if endTime[i] < queryTime:
pass
else:
result += 1
elif startTime[i] <= queryTime <= endTime[i]:
result += 1
elif startTime[i] >= queryTime:
if startTime[i] > queryTime:
pass
else:
result += 1
return result | number-of-students-doing-homework-at-a-given-time | Python O(n) Solution | White_Frost1984 | 0 | 20 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,634 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/1753664/Python-dollarolution | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
count = 0
for i in range(len(startTime)):
if queryTime in range(startTime[i],endTime[i]+1):
count += 1
return count | number-of-students-doing-homework-at-a-given-time | Python $olution | AakRay | 0 | 39 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,635 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/1696509/Simple-and-Easy-Python-Solution | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
counter=0
for i in range(int(len(startTime))):
for j in range(int(len(endTime))):
if startTime[i]<= queryTime and endTime[j]>=queryTime and i==j:
counter+=1
return counter | number-of-students-doing-homework-at-a-given-time | Simple and Easy Python Solution | Buyanjargal | 0 | 41 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,636 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/1596541/Python-3-one-liner | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
return sum(startTime[i] <= queryTime <= endTime[i]
for i in range(len(startTime))) | number-of-students-doing-homework-at-a-given-time | Python 3 one liner | dereky4 | 0 | 51 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,637 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/1223441/python-solution-or-easy-one | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
ans=0
for j in range(len(startTime)):
if startTime[j]<=queryTime<=endTime[j]:
ans+=1
return ans | number-of-students-doing-homework-at-a-given-time | python solution | easy one | chikushen99 | 0 | 36 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,638 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/1144808/Python-97.67-pythonic | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
count = 0
for i, j in zip(startTime, endTime):
if i <= queryTime <= j:
count += 1
return count | number-of-students-doing-homework-at-a-given-time | [Python] 97.67, pythonic | cruim | 0 | 46 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,639 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/1128042/Simple-Python-Solution | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
c = 0
for i in range(len(startTime)):
if startTime[i]<=queryTime and endTime[i]>=queryTime:
c += 1
return c | number-of-students-doing-homework-at-a-given-time | Simple Python Solution | Annushams | 0 | 92 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,640 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/1116743/Python-faster-than-90 | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
indexes = [index for index, start in enumerate(startTime)
if start <= queryTime]
if len(indexes) == 0:
return 0
return len([1 for index in indexes
if endTime[index] >= queryTime]) | number-of-students-doing-homework-at-a-given-time | Python faster than 90% | peatear-anthony | 0 | 35 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,641 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/1019422/Runtime%3A-36-ms-faster-than-73.43-of-Python3-online-submissions-or-Easy-Debuggable | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
total = 0
for s, e in zip(startTime, endTime):
if s <= queryTime <= e:
total += 1
return total | number-of-students-doing-homework-at-a-given-time | Runtime: 36 ms, faster than 73.43% of Python3 online submissions | Easy - Debuggable | user3912v | 0 | 44 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,642 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/707932/simple-of-simple | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
l = len(startTime)
count = 0
for i in range(l):
if startTime[i] <= queryTime and \
queryTime <= endTime[i]:
count += 1
return count | number-of-students-doing-homework-at-a-given-time | simple of simple | seunggabi | 0 | 33 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,643 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/700375/Python3C-%3A-Runtime%3A-0-ms-Simple-Code-O(n) | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
count = 0
for i,j in zip(startTime,endTime):
if(queryTime in range(i,j+1)):
count += 1
return count | number-of-students-doing-homework-at-a-given-time | [Python3/C : Runtime: 0 ms] Simple Code O(n) | abhijeetmallick29 | 0 | 41 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,644 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/683323/Python-Very-Easy-One-Liner. | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
return len([x for x in zip(startTime, endTime) if queryTime <= x[1] and queryTime >= x[0]]) | number-of-students-doing-homework-at-a-given-time | Python, Very Easy One-Liner. | Cavalier_Poet | 0 | 51 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,645 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/652572/Intuitive-approach-by-zip-filter-to-find-time-periods-cover-the-query-time | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
tp_list = []
''' list of tuple(start time, end time) '''
# 0) Build time period list
for s, e in zip(startTime, endTime):
tp_list.append((s, e))
# 1) Return the number of time period which cover the query time
return len(list(filter(lambda t: t[0] <= queryTime and t[1] >= queryTime, tp_list))) | number-of-students-doing-homework-at-a-given-time | Intuitive approach by zip, filter to find time periods cover the query time | puremonkey2001 | 0 | 27 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,646 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/652572/Intuitive-approach-by-zip-filter-to-find-time-periods-cover-the-query-time | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
return len(list(filter(lambda t: t[0] <= queryTime and t[1] >= queryTime, list(zip(*[startTime, endTime]))))) | number-of-students-doing-homework-at-a-given-time | Intuitive approach by zip, filter to find time periods cover the query time | puremonkey2001 | 0 | 27 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,647 |
https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/discuss/637156/Pythonic-sol-by-zip-and-sum-w-Comment | class Solution:
def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
# make a pair with start-time and end-time
homework_interval = zip(startTime, endTime)
# compute the number of busy students by condition judgement
return sum( ( start <= queryTime <= end ) for start, end in homework_interval ) | number-of-students-doing-homework-at-a-given-time | Pythonic sol by zip and sum [w/ Comment] | brianchiang_tw | 0 | 51 | number of students doing homework at a given time | 1,450 | 0.759 | Easy | 21,648 |
https://leetcode.com/problems/rearrange-words-in-a-sentence/discuss/636348/Python3-one-line | class Solution:
def arrangeWords(self, text: str) -> str:
return " ".join(sorted(text.split(), key=len)).capitalize() | rearrange-words-in-a-sentence | [Python3] one line | ye15 | 40 | 2,600 | rearrange words in a sentence | 1,451 | 0.626 | Medium | 21,649 |
https://leetcode.com/problems/rearrange-words-in-a-sentence/discuss/2828860/Python-3-Simple-or-Easy-To-Understand-or-Fast | class Solution:
def arrangeWords(self, text: str) -> str:
a = []
for x in text.split(" "):
a.append(x.lower())
return " ".join(sorted(a, key=len)).capitalize() | rearrange-words-in-a-sentence | [Python 3] Simple | Easy To Understand | Fast | omkarxpatel | 2 | 6 | rearrange words in a sentence | 1,451 | 0.626 | Medium | 21,650 |
https://leetcode.com/problems/rearrange-words-in-a-sentence/discuss/2828860/Python-3-Simple-or-Easy-To-Understand-or-Fast | class Solution:
def arrangeWords(self, text: str) -> str:
return " ".join(sorted(text.split(" "), key=len)).capitalize() | rearrange-words-in-a-sentence | [Python 3] Simple | Easy To Understand | Fast | omkarxpatel | 2 | 6 | rearrange words in a sentence | 1,451 | 0.626 | Medium | 21,651 |
https://leetcode.com/problems/rearrange-words-in-a-sentence/discuss/2822109/Python-Easy-Solution-Using-Dictionary-and-Sorting | class Solution:
def arrangeWords(self, text: str) -> str:
a=text.split()
d,t={},''
for i in a:
l=len(i)
if l in d:
d[l]+=" "+i
else:
d[l]=i
for i in sorted(d):
t+=" "+d[i]
t=t.lstrip().capitalize()
return t | rearrange-words-in-a-sentence | Python Easy Solution Using Dictionary and Sorting | DareDevil_007 | 1 | 16 | rearrange words in a sentence | 1,451 | 0.626 | Medium | 21,652 |
https://leetcode.com/problems/rearrange-words-in-a-sentence/discuss/1686925/Single-line-python-solution | class Solution:
def arrangeWords(self, text: str) -> str:
return " ".join(sorted(text.split(),key=lambda x:len(x))).capitalize() | rearrange-words-in-a-sentence | Single line python solution | amannarayansingh10 | 1 | 59 | rearrange words in a sentence | 1,451 | 0.626 | Medium | 21,653 |
https://leetcode.com/problems/rearrange-words-in-a-sentence/discuss/1617816/Python%3A-Easy-one-line-solution | class Solution:
def arrangeWords(self, text: str) -> str:
return ' '.join(sorted(text.split(), key=len)).capitalize() | rearrange-words-in-a-sentence | Python: Easy one line solution | y-arjun-y | 1 | 92 | rearrange words in a sentence | 1,451 | 0.626 | Medium | 21,654 |
https://leetcode.com/problems/rearrange-words-in-a-sentence/discuss/790716/Python3-Solution-Easy | class Solution:
def arrangeWords(self, text: str) -> str:
text = text.split(' ')
text.sort(key=len)
text = " ".join(text)
return text.capitalize()
Runtime: 32 ms, faster than 97.07% of Python3 online submissions for Rearrange Words in a Sentence.
Memory Usage: 15.5 MB, less than 70.53% of Python3 online submissions for Rearrange Words in a Sentence. | rearrange-words-in-a-sentence | Python3 Solution Easy | faizulhai | 1 | 97 | rearrange words in a sentence | 1,451 | 0.626 | Medium | 21,655 |
https://leetcode.com/problems/rearrange-words-in-a-sentence/discuss/637271/Pythonic-sol-by-split-and-join-w-Comment | class Solution:
def arrangeWords(self, text: str) -> str:
# Split each word by white space, store them in list
words = [ *text.split() ]
# Convert first word to lower case
words[0] = words[0].lower()
# Sort words by character length
words.sort( key = len )
# Convert back to string, separated by space
# Then capitalize the result
return ( ' '.join( words ) ).capitalize() | rearrange-words-in-a-sentence | Pythonic sol by split and join [w/ Comment] | brianchiang_tw | 1 | 111 | rearrange words in a sentence | 1,451 | 0.626 | Medium | 21,656 |
https://leetcode.com/problems/rearrange-words-in-a-sentence/discuss/2844698/Python3-easy-to-understand | class Solution:
def arrangeWords(self, text: str) -> str:
d = defaultdict(int)
for i, word in enumerate(text.split()):
d[i] = word
res = ''
for K, V in sorted(d.items(), key = lambda x: (len(x[1]), x[0])):
if res == '':
res += V[0].upper() + V[1:] + ' '
else:
res = res + V.lower() + ' '
return res.strip() | rearrange-words-in-a-sentence | Python3 - easy to understand | mediocre-coder | 0 | 1 | rearrange words in a sentence | 1,451 | 0.626 | Medium | 21,657 |
https://leetcode.com/problems/rearrange-words-in-a-sentence/discuss/2838154/Python3-2-liner-beats-95.3491.91 | class Solution:
def arrangeWords(self, text):
x = sorted(text.lower().split(" "), key = lambda s: len(s)) # sort the sentence by length of each word
return ' '.join([x[0].capitalize()] + x[1:]) # join together the final sentence (after capitalizing the first word) | rearrange-words-in-a-sentence | [Python3] 2-liner, beats 95.34/91.91 | U753L | 0 | 1 | rearrange words in a sentence | 1,451 | 0.626 | Medium | 21,658 |
https://leetcode.com/problems/rearrange-words-in-a-sentence/discuss/2823223/Python-oror-EASY-oror-Using-split-and-sort | class Solution:
def arrangeWords(self, text: str) -> str:
words= text.split()
words.sort(key=len)
temp=""
for ch in range(0, len(words)):
temp+=words[ch] + " "
res=temp.strip().capitalize()
return res | rearrange-words-in-a-sentence | Python || EASY || Using split and sort | apoorvapriya7733 | 0 | 3 | rearrange words in a sentence | 1,451 | 0.626 | Medium | 21,659 |
https://leetcode.com/problems/rearrange-words-in-a-sentence/discuss/2821474/Short-and-efficient-in-python | class Solution:
def arrangeWords(self, text: str) -> str:
return ' '.join(x for x in sorted(text.lower().split(), key=len)).capitalize() | rearrange-words-in-a-sentence | Short and efficient in python | user3687fV | 0 | 2 | rearrange words in a sentence | 1,451 | 0.626 | Medium | 21,660 |
https://leetcode.com/problems/rearrange-words-in-a-sentence/discuss/2809261/python-solution-using-sorting | class Solution:
def arrangeWords(self, text: str) -> str:
text = text.lower()
arr = text.split()
arr.sort(key = lambda x : (len(x)))
ans = ' '.join(arr)
ans = ans[0].upper() + ans[1:]
return ans | rearrange-words-in-a-sentence | python solution using sorting | akashp2001 | 0 | 5 | rearrange words in a sentence | 1,451 | 0.626 | Medium | 21,661 |
https://leetcode.com/problems/rearrange-words-in-a-sentence/discuss/2493750/easy-python-solution | class Solution:
def arrangeWords(self, text: str) -> str:
descript_list = []
split_text = text.split()
for i in range(len(split_text)) :
descript_list.append([split_text[i], len(split_text[i]), i])
new_li = sorted(descript_list, key = lambda x: (x[1], x[2]))
ans = ""
for i in range(len(new_li)) :
if i == 0 :
ans += new_li[i][0].capitalize() + ' '
else :
ans += new_li[i][0].lower() + ' '
return ans[:-1] | rearrange-words-in-a-sentence | easy python solution | sghorai | 0 | 25 | rearrange words in a sentence | 1,451 | 0.626 | Medium | 21,662 |
https://leetcode.com/problems/rearrange-words-in-a-sentence/discuss/2223872/Python-or-Two-easy-solutions-using-intuitive-approach-and-lambda-method | class Solution:
def arrangeWords(self, text: str) -> str:
# /// Solution 2 intutive approach ///
res = []
text = text.split()
for word in text:
res.append([word.lower(),len(word)])
res.sort(key = lambda x : x[1])
res = [word[0] for word in res]
res = ' '.join(res)
return res[0].capitalize() + res[1:]
# /// Solution 1 using lambda method ///
return ' '.join(sorted(text.split(),key = lambda x : len(x))).capitalize() | rearrange-words-in-a-sentence | Python | Two easy solutions using intuitive approach and lambda method | __Asrar | 0 | 26 | rearrange words in a sentence | 1,451 | 0.626 | Medium | 21,663 |
https://leetcode.com/problems/rearrange-words-in-a-sentence/discuss/1888689/Python-simplest-7-Lines-sorting-approach | class Solution:
def arrangeWords(self, text: str) -> str:
lis1=text.split(" ")
lis1.sort(key=len)
lis1[0]=lis1[0].title()
for i in range(1,len(lis1)):
lis1[i]=lis1[i].lower()
str2=" ".join(lis1)
return str2 | rearrange-words-in-a-sentence | Python simplest 7 Lines sorting approach | Coder-117 | 0 | 70 | rearrange words in a sentence | 1,451 | 0.626 | Medium | 21,664 |
https://leetcode.com/problems/rearrange-words-in-a-sentence/discuss/1727147/Python-One-liner-or-Hash-map-Solution-or-Simple-Readable-and-Fast | class Solution:
def arrangeWords(self, text: str) -> str:
return((" ".join(sorted(text.split(), key=len))).capitalize()) | rearrange-words-in-a-sentence | Python One liner | Hash map Solution | Simple Readable and Fast | shandilayasujay | 0 | 109 | rearrange words in a sentence | 1,451 | 0.626 | Medium | 21,665 |
https://leetcode.com/problems/rearrange-words-in-a-sentence/discuss/1727147/Python-One-liner-or-Hash-map-Solution-or-Simple-Readable-and-Fast | class Solution:
def arrangeWords(self, text: str) -> str:
len_dict=defaultdict(list)
string=[]
for word in text.split():
len_dict[len(word)].append(word)
for length in sorted(len_dict.keys()):
string+=len_dict[length]
return(" ".join(string).capitalize())
``` | rearrange-words-in-a-sentence | Python One liner | Hash map Solution | Simple Readable and Fast | shandilayasujay | 0 | 109 | rearrange words in a sentence | 1,451 | 0.626 | Medium | 21,666 |
https://leetcode.com/problems/rearrange-words-in-a-sentence/discuss/1702202/python-simple-hashmap-%2B-sorting-solution | class Solution:
def arrangeWords(self, text: str) -> str:
lengths = defaultdict(list)
words = text.split(' ')
for word in words:
lengths[len(word)].append(word)
res = []
for k in sorted(lengths.keys()):
for w in lengths[k]:
res.append(w.lower())
def convert(w):
s = [x for x in w]
s[0] = s[0].upper()
return "".join(s)
res[0] = convert(res[0])
return " ".join(res) | rearrange-words-in-a-sentence | python simple hashmap + sorting solution | byuns9334 | 0 | 69 | rearrange words in a sentence | 1,451 | 0.626 | Medium | 21,667 |
https://leetcode.com/problems/rearrange-words-in-a-sentence/discuss/1586830/Python3-Solution-with-using-sorting | class Solution:
def arrangeWords(self, text: str) -> str:
words = text.split(' ')
d = collections.defaultdict(list)
for word in words:
d[len(word)].append(word)
res = []
for key in sorted(d):
for word in d[key]:
res.append(word)
return ' '.join(res).capitalize() | rearrange-words-in-a-sentence | [Python3] Solution with using sorting | maosipov11 | 0 | 48 | rearrange words in a sentence | 1,451 | 0.626 | Medium | 21,668 |
https://leetcode.com/problems/rearrange-words-in-a-sentence/discuss/1401485/Python3-simple-one-liner-solution | class Solution:
def arrangeWords(self, text: str) -> str:
return ' '.join(sorted(text.lower().split(),key=lambda x : len(x))).capitalize() | rearrange-words-in-a-sentence | Python3 simple one-liner solution | EklavyaJoshi | 0 | 63 | rearrange words in a sentence | 1,451 | 0.626 | Medium | 21,669 |
https://leetcode.com/problems/rearrange-words-in-a-sentence/discuss/1262796/Python3-Simple-Solution | class Solution:
def arrangeWords(self, text: str) -> str:
text = text.lower()
text = text.split()
text.sort(key=len)
return(" ".join([text[0].capitalize()] + text[1:])) | rearrange-words-in-a-sentence | [Python3] Simple Solution | VoidCupboard | 0 | 51 | rearrange words in a sentence | 1,451 | 0.626 | Medium | 21,670 |
https://leetcode.com/problems/rearrange-words-in-a-sentence/discuss/1089239/Python-solution-using-dictionary | class Solution:
def arrangeWords(self, text: str) -> str:
print(text)
text_arr = text.lower().split(' ')
print(text_arr)
text_dict = {}
for val in text_arr:
#text_dict[len(val)].append(val)
text_dict.setdefault(len(val),[]).append(val)
print(sorted(text_dict))
reordered_text = ''
for val in sorted(text_dict):
for val1 in text_dict[val]:
if reordered_text != '' : reordered_text+=' '
reordered_text+=val1
# reordered_text+=' '
# reordered_text = reordered_text.capitalize()
print(reordered_text.capitalize())
return reordered_text.capitalize() | rearrange-words-in-a-sentence | Python solution using dictionary | souvikd_coder | 0 | 51 | rearrange words in a sentence | 1,451 | 0.626 | Medium | 21,671 |
https://leetcode.com/problems/rearrange-words-in-a-sentence/discuss/636366/Python-1-line | class Solution:
def arrangeWords(self, text):
return ' '.join(sorted(text.split(), key=len)).capitalize() | rearrange-words-in-a-sentence | [Python] - 1 line | _xavier_ | 0 | 48 | rearrange words in a sentence | 1,451 | 0.626 | Medium | 21,672 |
https://leetcode.com/problems/rearrange-words-in-a-sentence/discuss/1341429/Python-3-oror-One-line-Solution | class Solution:
def arrangeWords(self, text: str) -> str:
return " ".join(sorted(str(text[0].lower()+text[1:]).split(),key=len)).capitalize() | rearrange-words-in-a-sentence | Python 3 || One line Solution | Suryanandhu | -1 | 89 | rearrange words in a sentence | 1,451 | 0.626 | Medium | 21,673 |
https://leetcode.com/problems/people-whose-list-of-favorite-companies-is-not-a-subset-of-another-list/discuss/2827639/Python-or-Dictionary-%2B-Bitwise-operation | class Solution:
def peopleIndexes(self, favoriteCompanies: List[List[str]]) -> List[int]:
n = len(favoriteCompanies)
comp = []
for f in favoriteCompanies:
comp += f
comp = list(set(comp))
dictBit = {comp[i] : 1 << i for i in range(len(comp))}
def getBit(cList):
output = 0
for c in cList:
output |= dictBit[c]
return output
bitFav = [getBit(favoriteCompanies[i]) for i in range(n)]
output = []
for i in range(n):
isGood = True
for j in range(n):
if(i != j and bitFav[i] & bitFav[j] == bitFav[i]):
isGood = False
break
if(isGood):
output.append(i)
return output | people-whose-list-of-favorite-companies-is-not-a-subset-of-another-list | Python | Dictionary + Bitwise operation | CosmosYu | 0 | 3 | people whose list of favorite companies is not a subset of another list | 1,452 | 0.568 | Medium | 21,674 |
https://leetcode.com/problems/people-whose-list-of-favorite-companies-is-not-a-subset-of-another-list/discuss/2534777/Python-Sets-but-Sorted-Faster | class Solution:
def peopleIndexes(self, favoriteCompanies: List[List[str]]) -> List[int]:
# convert to list of sets and keep track of the index
favoriteCompanies = [(idx, set(ele)) for idx, ele in enumerate(favoriteCompanies)]
# we sort the lists by length for early stopping
favoriteCompanies = sorted(favoriteCompanies, key=lambda x: len(x[1]), reverse=True)
# go through all subsets
result = []
for index, (idx, companies) in enumerate(favoriteCompanies):
# get the length
companies_length = len(companies)
add_to_list = True
for other_index, (other_idx, other_companies) in enumerate(favoriteCompanies):
# check whether we look at the same set
if index == other_index:
continue
# if we are subset we set add_to_list to false and break
if companies.issubset(other_companies):
add_to_list = False
break
# once we hit sets that are shorter we can break
if len(other_companies) < companies_length:
break
# add if we want to
if add_to_list:
result.append(idx)
return sorted(result) | people-whose-list-of-favorite-companies-is-not-a-subset-of-another-list | [Python] - Sets but Sorted - Faster? | Lucew | 0 | 9 | people whose list of favorite companies is not a subset of another list | 1,452 | 0.568 | Medium | 21,675 |
https://leetcode.com/problems/people-whose-list-of-favorite-companies-is-not-a-subset-of-another-list/discuss/2534777/Python-Sets-but-Sorted-Faster | class Solution:
def peopleIndexes(self, favoriteCompanies: List[List[str]]) -> List[int]:
# convert to list of sets
favoriteCompanies = [set(ele) for idx, ele in enumerate(favoriteCompanies)]
# go through all subsets
result = []
for index, companies in enumerate(favoriteCompanies):
# a bool whether we will add this set
add_to_list = True
for other_index, other_companies in enumerate(favoriteCompanies):
# check whether we look at the same set
if index == other_index:
continue
# if we are subset we set add_to_list to false and break
if companies.issubset(other_companies):
add_to_list = False
break
# add if we want to
if add_to_list:
result.append(index)
return result | people-whose-list-of-favorite-companies-is-not-a-subset-of-another-list | [Python] - Sets but Sorted - Faster? | Lucew | 0 | 9 | people whose list of favorite companies is not a subset of another list | 1,452 | 0.568 | Medium | 21,676 |
https://leetcode.com/problems/maximum-number-of-darts-inside-of-a-circular-dartboard/discuss/636439/Python3-angular-sweep-O(N2-logN) | class Solution:
def numPoints(self, points: List[List[int]], r: int) -> int:
ans = 1
for x, y in points:
angles = []
for x1, y1 in points:
if (x1 != x or y1 != y) and (d:=sqrt((x1-x)**2 + (y1-y)**2)) <= 2*r:
angle = atan2(y1-y, x1-x)
delta = acos(d/(2*r))
angles.append((angle-delta, +1)) #entry
angles.append((angle+delta, -1)) #exit
angles.sort(key=lambda x: (x[0], -x[1]))
val = 1
for _, entry in angles:
ans = max(ans, val := val+entry)
return ans | maximum-number-of-darts-inside-of-a-circular-dartboard | [Python3] angular sweep O(N^2 logN) | ye15 | 38 | 1,400 | maximum number of darts inside of a circular dartboard | 1,453 | 0.369 | Hard | 21,677 |
https://leetcode.com/problems/maximum-number-of-darts-inside-of-a-circular-dartboard/discuss/2373750/python3-or-math-or-faster-than-100 | class Solution:
def numPoints(self, points: List[List[int]], r: int) -> int:
def getPointsInside(i, r, n):
# This vector stores alpha and beta and flag
# is marked true for alpha and false for beta
angles = []
for j in range(n):
if i != j and distance[i][j] <= 2 * r:
# acos returns the arc cosine of the complex
# used for cosine inverse
B = math.acos(distance[i][j] / (2 * r))
# arg returns the phase angle of the complex
x1, y1 = points[i]
x2, y2 = points[j]
A = math.atan2(y1 - y2, x1 - x2)
alpha = A - B
beta = A + B
angles.append((alpha, False))
angles.append((beta, True))
# angles vector is sorted and traversed
angles.sort()
# count maintains the number of points inside
# the circle at certain value of theta
# res maintains the maximum of all count
cnt, res = 1, 1
for angle in angles:
# entry angle
if angle[1] == False:
cnt += 1
# exit angle
else:
cnt -= 1
res = max(cnt, res)
return res
# Returns count of maximum points that can lie
# in a circle of radius r.
#a dis array stores the distance between every
# pair of points
n = len(points)
max_pts = n
distance = [[0 for _ in range(max_pts)] for _ in range(max_pts)]
for i in range(n - 1):
for j in range(i + 1, n):
# abs gives the magnitude of the complex
# number and hence the distance between
# i and j
x1, y1 = points[i]
x2, y2 = points[j]
distance[i][j] = distance[j][i] = sqrt((x1 - x2)**2 + (y1 - y2)**2)
# This loop picks a point p
ans = 0
# maximum number of points for point arr[i]
for i in range(n):
ans = max(ans, getPointsInside(i, r, n))
return ans | maximum-number-of-darts-inside-of-a-circular-dartboard | python3 | math | faster than 100% | vimla_kushwaha | 1 | 61 | maximum number of darts inside of a circular dartboard | 1,453 | 0.369 | Hard | 21,678 |
https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/discuss/1170901/Python3-Simple-And-Readable-Solution-With-Explanation | class Solution:
def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:
for i , j in enumerate(sentence.split()):
if(j.startswith(searchWord)):
return i + 1
return -1 | check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence | [Python3] Simple And Readable Solution With Explanation | VoidCupboard | 3 | 67 | check if a word occurs as a prefix of any word in a sentence | 1,455 | 0.642 | Easy | 21,679 |
https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/discuss/961604/Python-Simple-Solution | class Solution:
def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:
l=sentence.split()
for i in range(len(l)):
if l[i].startswith(searchWord):
return i+1
return -1 | check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence | Python Simple Solution | lokeshsenthilkumar | 2 | 224 | check if a word occurs as a prefix of any word in a sentence | 1,455 | 0.642 | Easy | 21,680 |
https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/discuss/1129090/Simple-Python-Solution-using-startswith-method-or-Faster-than-85 | class Solution:
def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:
l = sentence.split()
for i in range(len(l)):
if l[i].startswith(searchWord):
return (i+1)
return -1 | check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence | Simple Python Solution using startswith method | Faster than 85% | Annushams | 1 | 189 | check if a word occurs as a prefix of any word in a sentence | 1,455 | 0.642 | Easy | 21,681 |
https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/discuss/2838665/Simple-Python-solution | class Solution:
def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:
words = sentence.split(" ")
n = len(searchWord)
for i in range(len(words)):
if words[i][:n] == searchWord:
return i+1
return -1 | check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence | Simple Python solution | aruj900 | 0 | 2 | check if a word occurs as a prefix of any word in a sentence | 1,455 | 0.642 | Easy | 21,682 |
https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/discuss/2831159/Simple-Python-Solution | class Solution:
def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:
word_list = sentence.split(' ')
for i in range(len(word_list)):
if len(word_list[i]) >= len(searchWord):
if word_list[i][0:len(searchWord)] == searchWord:
return i + 1
continue
return -1 | check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence | Simple Python Solution | lornedot | 0 | 1 | check if a word occurs as a prefix of any word in a sentence | 1,455 | 0.642 | Easy | 21,683 |
https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/discuss/2790834/Python-Solution | class Solution:
def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:
e = sentence.split(" ")
print(e)
for i in e:
if searchWord in i:
if i.split(searchWord)[0]== "":
return e.index(i)+1
return -1 | check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence | Python Solution | nav_debug | 0 | 7 | check if a word occurs as a prefix of any word in a sentence | 1,455 | 0.642 | Easy | 21,684 |
https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/discuss/2776309/Easy-Python-Solution | class Solution:
def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:
lst = list(sentence.split(" "))
l = len(searchWord)
for index in range(len(lst)):
if lst[index][:l] == searchWord:
return index + 1
return -1 | check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence | Easy Python Solution | danishs | 0 | 6 | check if a word occurs as a prefix of any word in a sentence | 1,455 | 0.642 | Easy | 21,685 |
https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/discuss/2738825/Python-or-Easy-Soltution | class Solution:
def isPrefixOfWord(self, s: str, searchWord: str) -> int:
s = s.split()
for i in range(len(s)):
if (s[i].find(searchWord)==0):
return i+1
return -1 | check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence | Python | Easy Soltution | atharva77 | 0 | 2 | check if a word occurs as a prefix of any word in a sentence | 1,455 | 0.642 | Easy | 21,686 |
https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/discuss/2514678/Python3-Straightforward-with-explanation | class Solution:
def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:
# Split the sentence into words, then loop through each word comparing
# the searchWord to the first len(searchWord) characters
words = sentence.split(' ')
for i in range(len(words)):
if searchWord == words[i][:len(searchWord)]: return i+1
return -1 | check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence | [Python3] Straightforward with explanation | connorthecrowe | 0 | 22 | check if a word occurs as a prefix of any word in a sentence | 1,455 | 0.642 | Easy | 21,687 |
https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/discuss/2440300/Python-Simple-Solution | class Solution:
def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:
s,sr = sentence , searchWord
x = s.split()
print(x)
for i in range(0,len(x)):
if x[i].startswith(sr):
return x.index(x[i]) + 1
return -1 | check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence | Python Simple Solution | SouravSingh49 | 0 | 34 | check if a word occurs as a prefix of any word in a sentence | 1,455 | 0.642 | Easy | 21,688 |
https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/discuss/2309198/easy-python-solution | class Solution:
def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:
words = sentence.split(' ')
for i in range(len(words)) :
if words[i][:len(searchWord)] == searchWord :
return i+1
return -1 | check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence | easy python solution | sghorai | 0 | 25 | check if a word occurs as a prefix of any word in a sentence | 1,455 | 0.642 | Easy | 21,689 |
https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/discuss/2088602/Super-simple-python-solution | class Solution:
def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:
arr = sentence.split(' ')
for i in range(len(arr)):
if arr[i][:len(searchWord)] == searchWord:
return i + 1
return -1 | check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence | Super simple python solution | shreeruparel | 0 | 28 | check if a word occurs as a prefix of any word in a sentence | 1,455 | 0.642 | Easy | 21,690 |
https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/discuss/2033152/Python-simple-solution | class Solution:
def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:
for ind, i in enumerate(sentence.split()):
if i.startswith(searchWord):
return ind+1
else:
return -1 | check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence | Python simple solution | StikS32 | 0 | 41 | check if a word occurs as a prefix of any word in a sentence | 1,455 | 0.642 | Easy | 21,691 |
https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/discuss/1951761/Python-(Simple-Approach-and-Beginner-Friendly) | class Solution:
def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:
a = []
list1 = sentence.split(' ')
for i,n in enumerate(list1):
if searchWord in n:
if n[:len(searchWord)] == searchWord:
a.append(i)
return min(a)+1 if a else -1 | check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence | Python (Simple Approach and Beginner-Friendly) | vishvavariya | 0 | 20 | check if a word occurs as a prefix of any word in a sentence | 1,455 | 0.642 | Easy | 21,692 |
https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/discuss/1893673/Python-solution-using-built-in-functions | class Solution:
def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:
pos = 1
for i in sentence.split():
if i.startswith(searchWord):
return pos
pos += 1
return -1 | check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence | Python solution using built-in functions | alishak1999 | 0 | 21 | check if a word occurs as a prefix of any word in a sentence | 1,455 | 0.642 | Easy | 21,693 |
https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/discuss/1753679/Python-dollarolution | class Solution:
def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:
sentence = sentence.split()
for i in range(len(sentence)):
if searchWord == sentence[i][0:len(searchWord)]:
return i+1
return -1 | check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence | Python $olution | AakRay | 0 | 98 | check if a word occurs as a prefix of any word in a sentence | 1,455 | 0.642 | Easy | 21,694 |
https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/discuss/1709269/Python-readable | class Solution:
def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:
extract = []
j = 0
m = len(searchWord)
#extracting words in an array/list, we can also use .split()
for i in range(len(sentence)):
if sentence[i] == " ":
extract.append(sentence[j:i])
j = i + 1
if i == len(sentence)-1:
extract.append(sentence[j:i+1])
#comparison
for k in range(len(extract)):
x = 0
if m <= len(extract[k]):
for i in extract[k]:
if i != searchWord[x]:
break
if i == searchWord[x]:
x += 1
if x == m:
return (k+1)
return -1 | check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence | Python readable | Parashar_py | 0 | 41 | check if a word occurs as a prefix of any word in a sentence | 1,455 | 0.642 | Easy | 21,695 |
https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/discuss/1498784/Move-from-Incomplete-to-Complete-Approach-oror-Thought-Process-Explained | class Solution:
def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:
#This clears 34/40 test cases and then gives a wrong answer
#But has one major flaw : This doesn't guarantee that we are indeed checking the prefixes
#So, it is an incomplete solution because I just checked if searchWord exists in a particular word or not
#This will return answer even if searchWord is present and doesn't do anything to check
#if it is a prefix or not
#O(N^2) Time and O(1) Space
s = sentence.split()
for i in range(0, len(s)):
if searchWord in s[i]:
return i + 1
break
else:
return -1
#Let us try to think from more basics
#Note the definition of prefix given in question : A prefix of a string s is any leading contiguous substring of s
#So, substring/subarray idea comes into mind, we can generate all substrings and keep checking if it is == searchWord
#will not be an efficient one, but lets try
#O(N^3) Time and O(N) space
#Accepted Code
s = sentence.split()
answer = 0
for i in s:
answer += 1 #answer will keep a track of indices of words being considered
#lets make substrings now
for j in range(0, len(i)):
sub_string = ""
for k in range(j, len(i)):
sub_string += i[k]
if sub_string == searchWord:
return answer
else:
break
return -1
#Lets try to do better now - because Time consumed is too much in previous approach
#Now, I took the help from Discussions Section
#This is rather a very simple approach - should always think from most fundamental approach
#And slicing of strings, lists etc is a very critical concept
#O(N) Time and O(1) Space
#Accepted Code
sentence = sentence.split() #this basically converts sentence to an array of characters
for index,word in enumerate(sentence):
if searchWord == word[0 : len(searchWord)]: #this basically checks if searchWord is a prefix or not
return index+1
return -1 | check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence | Move from Incomplete to Complete Approach || Thought Process Explained | aarushsharmaa | 0 | 27 | check if a word occurs as a prefix of any word in a sentence | 1,455 | 0.642 | Easy | 21,696 |
https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/discuss/1434636/New-to-coding-Please-suggest-any-better-algorithm. | class Solution:
def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:
sent_list = sentence.split(" ")
ind_list = []
for i in range(len(sent_list)):
if len(sent_list[i])<len(searchWord):
ind_list
elif sent_list[i][0:len(searchWord)] == searchWord or sent_list[i] == searchWord:
ind_list.append(i+1)
else:
ind_list
if not ind_list:
return -1
else:
return min(ind_list) | check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence | New to coding, Please suggest any better algorithm. | rishabh3198 | 0 | 27 | check if a word occurs as a prefix of any word in a sentence | 1,455 | 0.642 | Easy | 21,697 |
https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/discuss/1407128/Python3-Faster-Than-94.68-Memory-Less-Than-74.79 | class Solution:
def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:
sentence = sentence.split()
for i in sentence:
if searchWord in i:
if i.find(searchWord) == 0:
return sentence.index(i) + 1
return -1 | check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence | Python3 Faster Than 94.68%, Memory Less Than 74.79% | Hejita | 0 | 43 | check if a word occurs as a prefix of any word in a sentence | 1,455 | 0.642 | Easy | 21,698 |
https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/discuss/1099621/python3-80 | class Solution:
def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:
search_index = 0
search_length = len(searchWord)
count = 1
for i in sentence:
if i == ' ':
count += 1
search_index = 0
continue
elif search_index == -1:
continue
if i == searchWord[search_index]:
search_index += 1
else:
search_index = -1
if search_index == search_length:
return count
if search_index == search_length:
return count
return -1
``` | check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence | python3 80% | Hoke_luo | 0 | 134 | check if a word occurs as a prefix of any word in a sentence | 1,455 | 0.642 | Easy | 21,699 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.