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/find-right-interval/discuss/2812012/Python-Sort-and-Two-Pointers-(Same-approach-as-826.-Most-Profit-Assigning-Work) | class Solution:
def findRightInterval(self, intervals: List[List[int]]) -> List[int]:
start = sorted([[intervals[i][0], i] for i in range(len(intervals))])
end = sorted([[intervals[i][1], i] for i in range(len(intervals))])
i = 0
res = []
for endVal, endIdx in end:
... | find-right-interval | [Python] Sort and Two Pointers (Same approach as # 826. Most Profit Assigning Work) | low_key_low_key | 0 | 4 | find right interval | 436 | 0.504 | Medium | 7,700 |
https://leetcode.com/problems/find-right-interval/discuss/2644967/Python-binary-search-solution-%2B-sorting-O(nlogn) | class Solution:
def findRightInterval(self, intervals: list[list[int]]) -> list[int]:
tmp, ans = sorted(intervals), []
indices = {tuple(j): i for i, j in enumerate(intervals)}
for i in intervals:
res = Solution().binary_search(tmp, i[1]) or ()
ans.append(indices.get(t... | find-right-interval | Python binary-search solution + sorting O(nlogn) | Mark_computer | 0 | 28 | find right interval | 436 | 0.504 | Medium | 7,701 |
https://leetcode.com/problems/find-right-interval/discuss/2606150/Python3-or-Solved-Using-Hashmap-Binary-Search-and-Sorting-greater-O(nlogn)-runtime! | class Solution:
#Let n = len(intervals)!
#Time-Complexity: O(n + nlogn + n*log(n)) -> O(nlogn)
#Space-Complexity: O(n + n) -> O(n)
def findRightInterval(self, intervals: List[List[int]]) -> List[int]:
#Brute-Force Approach: For every interval, linearly consider every other intervals
... | find-right-interval | Python3 | Solved Using Hashmap, Binary-Search, and Sorting -> O(nlogn) runtime! | JOON1234 | 0 | 12 | find right interval | 436 | 0.504 | Medium | 7,702 |
https://leetcode.com/problems/find-right-interval/discuss/2405586/Python-3-Binary-search | class Solution:
def findRightInterval(self, intervals: List[List[int]]) -> List[int]:
A=sorted([[intervals,i] for i,intervals in enumerate(intervals)])
def find(end):
l,r=0,len(A)-1
res=-1
while l<=r:
mid=l+(r-l)//2
if A[mid][0][0]>=end:
res=A[mid][1]
r=mid-1
else:
l=mid+1
re... | find-right-interval | [Python 3] Binary search | gabhay | 0 | 30 | find right interval | 436 | 0.504 | Medium | 7,703 |
https://leetcode.com/problems/find-right-interval/discuss/2078070/python-3-oror-simple-binary-search-solution-oror-O(nlogn)-O(n) | class Solution:
def findRightInterval(self, intervals: List[List[int]]) -> List[int]:
n = len(intervals)
starts = sorted((interval[0], i) for i, interval in enumerate(intervals))
res = []
for interval in intervals:
rightInterval = bisect.bisect_left(starts, interval[1], ... | find-right-interval | python 3 || simple binary search solution || O(nlogn) / O(n) | dereky4 | 0 | 82 | find right interval | 436 | 0.504 | Medium | 7,704 |
https://leetcode.com/problems/find-right-interval/discuss/1558998/Python-3-simple-Solution-beats-92.52-in-runtime-96.88-in-memory-using-Binary-Search | class Solution:
def findRightInterval(self, intervals: List[List[int]]) -> List[int]:
start = [ (interval[0], i) for i, interval in enumerate(intervals)]
start = sorted(start, key = lambda x: x[0])
start_search = [ i[0] for i in start]
rst = []
for interval in intervals:
... | find-right-interval | Python 3 simple Solution beats 92.52% in runtime; 96.88% in memory using Binary Search | kate_nhy | 0 | 94 | find right interval | 436 | 0.504 | Medium | 7,705 |
https://leetcode.com/problems/find-right-interval/discuss/1507078/99-faster-oror-Greedy-Approach-oror-Well-Explained-oror-For-Beginners | class Solution:
def findRightInterval(self, intervals: List[List[int]]) -> List[int]:
res = [-1 for _ in range(len(intervals))]
dic = dict()
for i,ele in enumerate(intervals):
dic[ele[0]] = i
keys = sorted(dic.keys())
i = 0
for s,e in intervals:
if e<=keys[-1]:
... | find-right-interval | 📌📌 99% faster || Greedy-Approach || Well-Explained || For Beginners 🐍 | abhi9Rai | 0 | 141 | find right interval | 436 | 0.504 | Medium | 7,706 |
https://leetcode.com/problems/find-right-interval/discuss/1453199/Simple-Python-O(nlogn)-sort%2Bbinary-search-solution | class Solution:
def findRightInterval(self, intervals: List[List[int]]) -> List[int]:
# add original indices to the intervals O(n)
for i in range(len(intervals)):
intervals[i].append(i)
# sort intervals by start O(nlogn)
intervals.sort()
ret = [-... | find-right-interval | Simple Python O(nlogn) sort+binary search solution | Charlesl0129 | 0 | 121 | find right interval | 436 | 0.504 | Medium | 7,707 |
https://leetcode.com/problems/find-right-interval/discuss/815413/Python-3-or-Binary-Search-or-Explanation | class Solution:
def findRightInterval(self, intervals: List[List[int]]) -> List[int]:
intervals = [interval + [i] for i, interval in enumerate(intervals)]
intervals.sort()
n = len(intervals)
ans = [-1] * n
for i, vals in enumerate(intervals):
(_, r, ori_idx) = val... | find-right-interval | Python 3 | Binary Search | Explanation | idontknoooo | 0 | 112 | find right interval | 436 | 0.504 | Medium | 7,708 |
https://leetcode.com/problems/find-right-interval/discuss/814990/Python3-binary-search-and-heap | class Solution:
def findRightInterval(self, intervals: List[List[int]]) -> List[int]:
ans = []
start, mp = zip(*sorted((x, i) for i, (x, _) in enumerate(intervals)))
for x, y in intervals:
i = bisect_left(start, y)
ans.append(mp[i] if 0 <= i < len(intervals) else -1)... | find-right-interval | [Python3] binary search & heap | ye15 | 0 | 47 | find right interval | 436 | 0.504 | Medium | 7,709 |
https://leetcode.com/problems/find-right-interval/discuss/814990/Python3-binary-search-and-heap | class Solution:
def findRightInterval(self, intervals: List[List[int]]) -> List[int]:
ans = [-1] * len(intervals)
hp = []
for i, (x, y) in sorted(enumerate(intervals), key=lambda x: x[1]):
while hp and hp[0][0] <= x:
_, k = heappop(hp)
ans[k] = i
... | find-right-interval | [Python3] binary search & heap | ye15 | 0 | 47 | find right interval | 436 | 0.504 | Medium | 7,710 |
https://leetcode.com/problems/find-right-interval/discuss/814615/Python-O(nlogn)-time-using-dictionary%2Bbinary-search-without-enumerate | class Solution:
def findRightInterval(self, intervals: List[List[int]]) -> List[int]:
if not intervals:
return []
if len(intervals) == 1:
return [-1]
ref = dict()
res = []
# fill ref with key = start point, value = index
for i in ran... | find-right-interval | Python O(nlogn) time using dictionary+binary search, without enumerate | Mowei | 0 | 30 | find right interval | 436 | 0.504 | Medium | 7,711 |
https://leetcode.com/problems/find-right-interval/discuss/406619/Python-3-(five-lines)-(beats-~93) | class Solution:
def findRightInterval(self, I: List[List[int]]) -> List[int]:
N, S, A, D, k = len(I), sorted(i[0] for i in I), [-1]*len(I), {j[0]:i for i,j in enumerate(I)}, 0
for L,R in sorted(I, key = lambda x: x[1]):
k = bisect.bisect_left(S,R,k)
if k < N: A[D[L]] = D[S[k]... | find-right-interval | Python 3 (five lines) (beats ~93%) | junaidmansuri | 0 | 318 | find right interval | 436 | 0.504 | Medium | 7,712 |
https://leetcode.com/problems/path-sum-iii/discuss/1049652/Python-Solution | class Solution:
def pathSum(self, root: TreeNode, sum: int) -> int:
global result
result = 0
def dfs(node, target):
if node is None: return
find_path_from_node(node, target)
dfs(node.left, target)
dfs(node.right, target)
... | path-sum-iii | Python Solution | dev-josh | 14 | 631 | path sum iii | 437 | 0.486 | Medium | 7,713 |
https://leetcode.com/problems/path-sum-iii/discuss/1850435/Python3-Prefix-Sum | class Solution(object):
def pathSum(self, root, targetSum):
"""
:type root: TreeNode
:type targetSum: int
:rtype: int
"""
self.targetSum=targetSum
self.hashmap={0:1}
self.prefix=0
self.result=0
self.helper(root)
... | path-sum-iii | Python3 Prefix Sum | muzhang90 | 2 | 109 | path sum iii | 437 | 0.486 | Medium | 7,714 |
https://leetcode.com/problems/path-sum-iii/discuss/843234/Python3-memoized-table-w-backtracking | class Solution:
def pathSum(self, root: TreeNode, sum: int) -> int:
seen = {0: 1}
def fn(node, prefix):
"""Return number of paths summing to target for tree rooted at node."""
if not node: return 0 #base condition
prefix += node.val # prefix sum up to no... | path-sum-iii | [Python3] memoized table w/ backtracking | ye15 | 2 | 107 | path sum iii | 437 | 0.486 | Medium | 7,715 |
https://leetcode.com/problems/path-sum-iii/discuss/2317497/Python-Simple-Idea-with-Explanation | class Solution:
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> int:
# Sum up from root to each depth and check if it's equal to targetSum
def dfs_calc_sum(root, path):
if not root:
return
path += root.val
if path == targetSum:
... | path-sum-iii | Python Simple Idea with Explanation | codeee5141 | 1 | 146 | path sum iii | 437 | 0.486 | Medium | 7,716 |
https://leetcode.com/problems/path-sum-iii/discuss/2697469/Python3-Recursive-easy-Solution | class Solution:
def __init__(self):
self.ans = 0
def helper(self,root: Optional[TreeNode], targetSum: int,summ: int) ->None:
if root is None:
return
if summ==targetSum:
self.ans+=1
if root.left:
self.helper(root.left, targetSum, summ + root.lef... | path-sum-iii | Python3 Recursive easy Solution | pranjalmishra334 | 0 | 16 | path sum iii | 437 | 0.486 | Medium | 7,717 |
https://leetcode.com/problems/path-sum-iii/discuss/2165595/Python-detailed-explination-runtime-57.71-memory-29.91 | class Solution:
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> int:
if not root:
return 0
return self.search(root, targetSum, 0) + self.pathSum(root.left, targetSum) + \
self.pathSum(root.right, targetSum)
def search(self, root, targetSum, prevSum):
i... | path-sum-iii | Python detailed explination, runtime 57.71%, memory 29.91% | tsai00150 | 0 | 67 | path sum iii | 437 | 0.486 | Medium | 7,718 |
https://leetcode.com/problems/path-sum-iii/discuss/2165595/Python-detailed-explination-runtime-57.71-memory-29.91 | class Solution:
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> int:
d = {}
return self.search(root, targetSum, d, 0)
def search(self, root, targetSum, d, curSum):
if not root:
return 0
curSum += root.val # update sum to this node
ways = d.... | path-sum-iii | Python detailed explination, runtime 57.71%, memory 29.91% | tsai00150 | 0 | 67 | path sum iii | 437 | 0.486 | Medium | 7,719 |
https://leetcode.com/problems/path-sum-iii/discuss/2157133/Python-oror-Python3-oror-BFS%2Bqueue | class Solution(object):
def pathSum(self, root, targetSum):
if root is None:
return 0
queue = [(root,[])]
res = 0
while queue:
node, ans = queue.pop(0)
curr_ans = []
if node.val == targetSum:
res += 1
for val... | path-sum-iii | Python || Python3 || BFS+queue | aul- | 0 | 40 | path sum iii | 437 | 0.486 | Medium | 7,720 |
https://leetcode.com/problems/path-sum-iii/discuss/1484104/Python-Clean-Recursive-DFS | class Solution:
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> int:
if not root:
return 0
def dfs(node = root, cumv = root.val, hashmap = {0:1}):
nonlocal count
if cumv-targetSum in hashmap:
count += hashmap[cumv-t... | path-sum-iii | [Python] Clean Recursive DFS | soma28 | 0 | 198 | path sum iii | 437 | 0.486 | Medium | 7,721 |
https://leetcode.com/problems/path-sum-iii/discuss/1386247/Simple-DFS-in-Python | class Solution:
def pathSum(self, root: TreeNode, targetSum: int) -> int:
cnt = 0
if not root:
return cnt
def counter(n: TreeNode, s=0):
nonlocal cnt
if not n:
return
s += n.val
if s == targetSum:
... | path-sum-iii | Simple DFS in Python | mousun224 | 0 | 222 | path sum iii | 437 | 0.486 | Medium | 7,722 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/1738367/Python-Sliding-Window(-)-algorithm-Detailed-Explanation-Concise-Soln-or-Faster-than-80 | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
# take counter of first n elements in s_dict with n = len(p) - 1
s_dict = collections.Counter(s[:len(p)-1])
# counter of p, this should not be changed
p_dict = collections.Counter(p)
start = 0
# fi... | find-all-anagrams-in-a-string | 🔎 Python - Sliding Window( 🚅 🪟) algorithm Detailed Explanation, Concise Soln | Faster than 80% | mystic_sd2001 | 16 | 1,400 | find all anagrams in a string | 438 | 0.49 | Medium | 7,723 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/392090/Solution-in-Python-3-(beats-~100)-(Hash) | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
LS, LP, S, P, A = len(s), len(p), 0, 0, []
if LP > LS: return []
for i in range(LP): S, P = S + hash(s[i]), P + hash(p[i])
if S == P: A.append(0)
for i in range(LP, LS):
S += hash(s[i]) - hash(s[i-LP])
if S == ... | find-all-anagrams-in-a-string | Solution in Python 3 (beats ~100%) (Hash) | junaidmansuri | 12 | 2,700 | find all anagrams in a string | 438 | 0.49 | Medium | 7,724 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/636879/Python-sliding-window-sol-sharing-w-Visualization | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
def helper( s: str, p: str):
signature, size_p = sum( map(hash,p) ), len(p)
size_s = len(s)
if size_s * size_p == 0 or size_p > size_s :
# Quick response:
# Reje... | find-all-anagrams-in-a-string | Python sliding-window sol sharing [w/ Visualization] | brianchiang_tw | 6 | 1,100 | find all anagrams in a string | 438 | 0.49 | Medium | 7,725 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/1738477/Python-3-(500ms)-or-Counter-Sliding-Window-O(n)-Approach-or-Easy-to-Understand | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
m = len(p)-1
res = []
pc = Counter(p)
sc = Counter(s[:m])
for i in range(m,len(s)):
sc[s[i]] += 1
if sc == pc:
res.append(i-len(p)+1)
sc[s[i-len(p)+1]] -= ... | find-all-anagrams-in-a-string | Python 3 (500ms) | Counter Sliding Window O(n) Approach | Easy to Understand | MrShobhit | 5 | 476 | find all anagrams in a string | 438 | 0.49 | Medium | 7,726 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/1411882/Two-Python-solutions-sliding-window-or-prefix-sum | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
target = [0]*26
for letter in p:
target[ord(letter)-ord('a')] += 1
count = [0]*26
left = right = 0
ret = []
while right < len(s):
count[ord(s[right])-ord('a')] +=... | find-all-anagrams-in-a-string | Two Python solutions, sliding window or prefix sum | Charlesl0129 | 4 | 381 | find all anagrams in a string | 438 | 0.49 | Medium | 7,727 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/2421680/Python3-Sliding-window-w-dicts-Explanation-9690 | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
# To solve this problem, looping through substrings of s, sorting them each time and comparing to a sorted p will
# work, but will take too long for some of the test cases. How else can we solve this?
# We can use ... | find-all-anagrams-in-a-string | [Python3] Sliding window w dicts - Explanation 96%/90% | connorthecrowe | 3 | 136 | find all anagrams in a string | 438 | 0.49 | Medium | 7,728 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/2375816/Python-or-Easy-solution-with-sliding-window-and-hashmap | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
result = list()
l, r = 0, len(p)-1
s_count, p_count = Counter(s[0:len(p)]), Counter(p)
while True:
# check if valid anagram, if so add leftmost index to result list.
if not len(p_coun... | find-all-anagrams-in-a-string | [Python] | Easy solution with sliding window and hashmap | lcerone | 3 | 236 | find all anagrams in a string | 438 | 0.49 | Medium | 7,729 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/638702/Common-solution-for-438-and-567 | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
slow,fast = 0,len(s1)
hmap = collections.Counter(s1)
hmap_temp = collections.Counter(s2[slow:fast])
while fast <= len(s2):
if hmap == hmap_temp:
return True
hmap_temp[s2[slow]]... | find-all-anagrams-in-a-string | Common solution for #438 and #567 | pratushah | 3 | 241 | find all anagrams in a string | 438 | 0.49 | Medium | 7,730 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/638702/Common-solution-for-438-and-567 | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
result = [] # just need to add this list in order to maintain the list of indexes where anagram of s starts.
slow,fast = 0,len(p)
hmap = collections.Counter(p)
hmap_temp = collections.Counter(s[slow:fast])
w... | find-all-anagrams-in-a-string | Common solution for #438 and #567 | pratushah | 3 | 241 | find all anagrams in a string | 438 | 0.49 | Medium | 7,731 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/2801531/Python-oror-Easy-oror-Sliding-Window-%2B-Dictionary-oror-O(N)-Solution | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
n1,n2=len(s),len(p)
d1=Counter(p)
d2=Counter(s[:n2-1])
ans=[]
j=0
for i in range(n2-1,n1):
d2[s[i]]+=1
if d1==d2:
ans.append(j)
d2[s[j]]-=1
... | find-all-anagrams-in-a-string | Python || Easy || Sliding Window + Dictionary || O(N) Solution | DareDevil_007 | 2 | 194 | find all anagrams in a string | 438 | 0.49 | Medium | 7,732 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/2702896/Real-O(n%2Bm)-solution-independent-of-vocabulary-size | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
p_count = defaultdict(int)
for c in p:
p_count[c] += 1
res = []
m = len(p)
missing = set(p)
window = defaultdict(int)
def update_missing(c):
if c in missing and win... | find-all-anagrams-in-a-string | Real O(n+m) solution independent of vocabulary size | tapka | 2 | 166 | find all anagrams in a string | 438 | 0.49 | Medium | 7,733 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/1738520/Find-All-Anagrams-in-a-String-or-Python-O(n)-Solution-or-Easy-To-Understand | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
if len(p) > len(s): return []
pCount, sCount = {}, {}
for i in range(len(p)):
pCount[p[i]] = 1 + pCount.get(p[i], 0)
sCount[s[i]] = 1 + sCount.get(s[i], 0)
res = [0] if sCou... | find-all-anagrams-in-a-string | Find All Anagrams in a String | Python O(n) Solution | Easy To Understand | pniraj657 | 2 | 268 | find all anagrams in a string | 438 | 0.49 | Medium | 7,734 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/1678272/Python3-oror-Sliding-window-oror-Dictionary | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
if len(p) > len(s):
return []
freq_p = {}
for char in p:
if char in freq_p:
freq_p[char] += 1
else:
freq_p[char] = 1
size = len(p)
... | find-all-anagrams-in-a-string | Python3 || Sliding window || Dictionary | s_m_d_29 | 2 | 201 | find all anagrams in a string | 438 | 0.49 | Medium | 7,735 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/1531863/Sliding-window-approach-Python | class Solution:
def findAnagrams(self, s: str, pattern: str):
start = 0
dic_pattern = collections.Counter(pattern)
dic_s = {}
result = []
for end in range(len(s)):
if s[end] not in dic_s:
dic_s[s[end]] = 1
else:
... | find-all-anagrams-in-a-string | Sliding window approach - Python | algoFun | 2 | 200 | find all anagrams in a string | 438 | 0.49 | Medium | 7,736 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/2345463/Python-efficient-solution | class Solution:
def countLetters(self, s: str) -> dict:
counts = {}
for char in s:
try:
counts[char] += 1
except KeyError:
counts[char] = 1
return counts
def isAnagram(self, sCounts: str, tCounts: str) -> bool:
for char... | find-all-anagrams-in-a-string | Python efficient solution | Woodie07 | 1 | 113 | find all anagrams in a string | 438 | 0.49 | Medium | 7,737 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/2123786/Simple-Python-Solution | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
n = len(s)
m = len(p)
if n < m:
return []
window = {}
pmap = {}
for i in range(m):
if p[i] not in pmap:
pmap[p[i]] = 1
... | find-all-anagrams-in-a-string | 📌 ✅ Simple Python Solution | reinkarnation | 1 | 179 | find all anagrams in a string | 438 | 0.49 | Medium | 7,738 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/1738936/Pyhton-oror-Easy-to-Understand-oror-Sliding-Window-and-Arrays-oror-99.43-faster | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
l1, l2 = len(s), len(p)
if l1 < l2:
return []
res = []
arr1, arr2 = [0] * 26, [0] * 26
for i in range(l2):
if i < l2 - 1:
arr1[ord(s[i]) - 97] += 1
arr2[o... | find-all-anagrams-in-a-string | Pyhton || Easy to Understand || Sliding Window and Arrays || 99.43% faster | cherrysri1997 | 1 | 83 | find all anagrams in a string | 438 | 0.49 | Medium | 7,739 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/1738402/Detail-Explained-Solution-(python3-slidingwindow) | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
sl, pl = len(s), len(p)
ans = []
# according to the question, p has to be shorter than s
# or there will be no valid anagrams --> return []
if sl < pl:
return ans
... | find-all-anagrams-in-a-string | Detail Explained Solution (python3, slidingwindow) | codingGeniusPP | 1 | 34 | find all anagrams in a string | 438 | 0.49 | Medium | 7,740 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/1722751/Python-Sliding-window-explained | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
res = list()
m = len(p)
p = Counter(p)
# start the window with the first
# m characters of s
window = Counter(s[:m])
# loop over s with len(p) to spare
# to be able ... | find-all-anagrams-in-a-string | [Python] Sliding window explained | buccatini | 1 | 156 | find all anagrams in a string | 438 | 0.49 | Medium | 7,741 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/1347407/Python-3-Simple-Sliding-Window-Solution | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
if len(s) < len(p):
return []
anagram_p = [0] * 26
anagram_curr = [0] * 26
output = []
for char in p:
anagram_p[ord(char)-97] += 1
for char in s[0:len(p)]:
... | find-all-anagrams-in-a-string | Python 3 Simple Sliding Window Solution | zhanz1 | 1 | 136 | find all anagrams in a string | 438 | 0.49 | Medium | 7,742 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/761487/Python3%3A-Sliding-window-with-elastic-window-length | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
if not p or len(p) > len(s):
return []
anagrams = []
len_s, len_p = len(s), len(p)
# distance to zeroize for each char in order to be the same as the pattern
counter = Counter(p)
# total num... | find-all-anagrams-in-a-string | Python3: Sliding window with elastic window length | alsvia | 1 | 109 | find all anagrams in a string | 438 | 0.49 | Medium | 7,743 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/2846809/Python3-Solution-Counter%2BSliding-Window | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
counter_p = collections.Counter(p)
len_window = len(p)
res = []
for i in range(len(s)-len_window+1):
counter_s = collections.Counter(s[i:i+len_window])
if counter_s == counter_p:
... | find-all-anagrams-in-a-string | Python3 Solution Counter+Sliding Window | osman74 | 0 | 1 | find all anagrams in a string | 438 | 0.49 | Medium | 7,744 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/2841942/BEATS-99-SUBMISSIONS-oror-FASTEST-AND-EASIEST-oror-TWO-POINTER-APPROACH | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
if len(p)>len(s):
return []
pcount,scount={},{}
for i in range(len(p)):
pcount[p[i]]=1 + pcount.get(p[i],0)
scount[s[i]]=1 + scount.get(s[i],0)
if pcount==scount :
res... | find-all-anagrams-in-a-string | BEATS 99% SUBMISSIONS || FASTEST AND EASIEST || TWO POINTER APPROACH | Pritz10 | 0 | 2 | find all anagrams in a string | 438 | 0.49 | Medium | 7,745 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/2838494/Hashtable-%2B-Sliding-Window | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
def _build_dict(w: str) -> Dict[str, int]:
"""Builds a dictionary."""
res: Dict[str, int] = {}
for w_ in w:
if w_ in res:
res[w_] += 1
else:
... | find-all-anagrams-in-a-string | Hashtable + Sliding Window | fengdi2020 | 0 | 2 | find all anagrams in a string | 438 | 0.49 | Medium | 7,746 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/2823266/Python-Easy-Understand-Solution | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
len_s = len(s)
len_p = len(p)
original = "".join(sorted(p))
anagram_index = []
for i in range(len_s - len_p + 1):
word = ''.join(sorted(s[i: i+len_p]))
if word == original:
... | find-all-anagrams-in-a-string | Python Easy Understand Solution | namashin | 0 | 3 | find all anagrams in a string | 438 | 0.49 | Medium | 7,747 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/2815062/Ultra-Streamlined-Sliding-Window-Python | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
res, matches, f = [], 0, Counter(p)
for i, old, new in ((j + 1, (s[j] if j >= 0 else None), c) for j, c in enumerate(s, -len(p))):
if old != new:
if old in f:
f[old] += 1
... | find-all-anagrams-in-a-string | Ultra-Streamlined Sliding Window [Python] | constantstranger | 0 | 3 | find all anagrams in a string | 438 | 0.49 | Medium | 7,748 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/2815062/Ultra-Streamlined-Sliding-Window-Python | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
if len(s) < len(p):
return []
res, matches, f = [], 0, defaultdict(int)
for c in p:
f[c] += 1
for i in range(len(p)):
if s[i] in f:
f[s[i]] -= 1
if... | find-all-anagrams-in-a-string | Ultra-Streamlined Sliding Window [Python] | constantstranger | 0 | 3 | find all anagrams in a string | 438 | 0.49 | Medium | 7,749 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/2784700/Easy-Python-solution-with-use-of-two-hashmapsSimilar-to-Problem-no.567 | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
dic1={}
for i in p:
if i in dic1:
dic1[i]+=1
else:
dic1[i]=1
i=0
j=len(p)-1
l=[]
dic2=Counter(s[i:j])
while(j<len(s)):
dic2... | find-all-anagrams-in-a-string | Easy Python solution with use of two hashmaps[Similar to Problem no.567] | liontech_123 | 0 | 6 | find all anagrams in a string | 438 | 0.49 | Medium | 7,750 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/2784402/Easy-to-understand-solution-using-a-Sliding-Window-and-Frequency-Lists-with-Explanation! | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
# Time complexity: O(s)
# Space complexity: O(1)
res = []
# sanity check
if len(p) > len(s):
return res
# init frequency lists for characters in alphabet
# we have foun... | find-all-anagrams-in-a-string | Easy to understand solution using a Sliding Window and Frequency Lists with Explanation! | FuriFuo | 0 | 4 | find all anagrams in a string | 438 | 0.49 | Medium | 7,751 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/2769520/My-python3-solution-Sliding-Window | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
res, shift, pCounter = [], len(p) - 1, Counter(p)
for i in range(len(s)):
pCounter[s[i]] -= 1
if i < shift:
continue
if set(pCounter.values()) == {0}:
res.append(i... | find-all-anagrams-in-a-string | My python3 solution - Sliding Window | toshiwu006 | 0 | 4 | find all anagrams in a string | 438 | 0.49 | Medium | 7,752 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/2755743/Python3-or-defaultdict-solution | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
ans = []
len_p = len(p)
len_s = len(s)
if len_p > len_s:
return ans
dict_s = collections.defaultdict(int)
dict_p = collections.defaultdict(int)
for i in range(len_p):
... | find-all-anagrams-in-a-string | Python3 | defaultdict solution | puppydog91111 | 0 | 3 | find all anagrams in a string | 438 | 0.49 | Medium | 7,753 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/2754863/Sliding-Window-%2B-Hash-Table-oror-Easy-To-Understand | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
if len(p) > len(s):
return []
s_count = {}
p_count = {}
for i in range(len(p)):
s_count[s[i]] = 1 + s_count.get(s[i],0)
p_count[p[i]] = 1 + p_count.get(p[i],0)
... | find-all-anagrams-in-a-string | Sliding Window + Hash Table || Easy To Understand | subbhashitmukherjee | 0 | 6 | find all anagrams in a string | 438 | 0.49 | Medium | 7,754 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/2710359/python-easy-solution | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
if len(p)>len(s):
return []
scount={}
pcount={}
anagram_index = []
for i in range(len(p)):
pcount[p[i]]=1+pcount.get(p[i],0)
scount[s[i]]=1+scount.get(s[i],0)
#pr... | find-all-anagrams-in-a-string | python easy solution | tush18 | 0 | 10 | find all anagrams in a string | 438 | 0.49 | Medium | 7,755 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/2702877/Python3-or-Sliding-Window-Hashmap-or-Beats-96-w-explanation | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
# edge case
if len(p) > len(s): return []
s_count, p_count = {}, {} # hashmap used to keep track of count of chars in current window in s (hence deciding if the phrase is an anagram of p)
anagram_index = []... | find-all-anagrams-in-a-string | Python3 | Sliding Window, Hashmap | Beats 96% w/ explanation | manasakal | 0 | 7 | find all anagrams in a string | 438 | 0.49 | Medium | 7,756 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/2697237/Python-solution | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
p_map = [0] * 26
window = [0] * 26
length = len(p)
ans = []
if length > len(s):
return
for i in range(length):
p_map[ord(p[i]) - ord('a')] += 1
... | find-all-anagrams-in-a-string | Python solution | maomao1010 | 0 | 8 | find all anagrams in a string | 438 | 0.49 | Medium | 7,757 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/2663155/Happy-to-share-my-python-solution-oror-NO-EXPLANATION-oror | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
ind = []
p_count = collections.Counter(p)
s_count = collections.Counter(s[:len(p)])
i = 0
while i<=len(s)-len(p):
if s_count==p_count:
ind.append(i)
if s_count[s[i]]>0... | find-all-anagrams-in-a-string | Happy to share my python solution 😊 || NO EXPLANATION || | daminrisho | 0 | 9 | find all anagrams in a string | 438 | 0.49 | Medium | 7,758 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/2625425/python-or-sliding-window-or-hash-table | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
left, right, valid, s_length, p_length = 0, 0, 0, len(s), len(p)
window, counter, result = defaultdict(lambda: 0), Counter(p), []
while right < s_length:
char = s[right]
right += 1
... | find-all-anagrams-in-a-string | python | sliding window | hash table | MichelleZou | 0 | 38 | find all anagrams in a string | 438 | 0.49 | Medium | 7,759 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/2621960/Python3-Faster-than-99.12 | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
d = defaultdict(lambda:0)
for i in p:
d[i] += 1
ans = []
slen, plen = len(s), len(p)
start, farthest = 0, plen
while farthest<=slen:
i = start
an... | find-all-anagrams-in-a-string | Python3 Faster than 99.12%✅ | Tensor08 | 0 | 28 | find all anagrams in a string | 438 | 0.49 | Medium | 7,760 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/2579455/Faster-than-98-and-imo-easier-to-follow-than-others-I've-seen-here | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
# dual pointers? fast and trailing
# increment fast whilst youre killing the dictionary.
f, tr = 0, 0
res = []
d = {}
for c in p:
if c not in d:
d[c] = 0
... | find-all-anagrams-in-a-string | Faster than 98% and imo easier to follow than others I've seen here | gabrielpetrov99 | 0 | 93 | find all anagrams in a string | 438 | 0.49 | Medium | 7,761 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/2539068/Python3-oror-Sliding-Window-oror-easy-to-understand | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
# lenght of window
k = len(p)
n = len(s)
pcounter = Counter(p)
res = []
c =len(pcounter)
i=j=0
while j<n:
#decrement the count of the element if it is present in the ... | find-all-anagrams-in-a-string | Python3 || Sliding Window || easy to understand | NitishKumarVerma | 0 | 30 | find all anagrams in a string | 438 | 0.49 | Medium | 7,762 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/2535326/Python-oror-Sliding-Window-Very-intuitive | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
a = [0] * 26
for letter in p:
a[ord(letter)-97] += 1
res = []
ls = len(s)
lp = len(p)
b = [0] * 26
currLength = 0
for i in range(ls):
if curr... | find-all-anagrams-in-a-string | Python || Sliding Window, Very intuitive | IamCookie | 0 | 54 | find all anagrams in a string | 438 | 0.49 | Medium | 7,763 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/2447356/Python3-oror-Sliding-Window-and-Hash-Maporor-Simple-and-fast | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
if len(p) > len(s): return []
pCount, sCount = {}, {}
for i in range(len(p)):
pCount[p[i]] = 1 + pCount.get(p[i], 0)
sCount[s[i]] = 1 + sCount.get(s[i], 0)
res = [0] if sCount == pCo... | find-all-anagrams-in-a-string | Python3 || Sliding Window and Hash Map|| Simple and fast | WhiteBeardPirate | 0 | 35 | find all anagrams in a string | 438 | 0.49 | Medium | 7,764 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/2445729/Sliding-Window-with-Dictionary-oror-With-Comments-oror-Simple-to-Understand | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
window = len(p)
p_dict = {}
cur_dict = {}
# Get original dictionary of p string
for l in p:
p_dict[l] = p_dict.get(l, 0) + 1
res = []
for i in range(len(s) -... | find-all-anagrams-in-a-string | Sliding Window with Dictionary || With Comments || Simple to Understand | wcnd27 | 0 | 46 | find all anagrams in a string | 438 | 0.49 | Medium | 7,765 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/2418999/Two-pointers(Sliding-window)-python3-solution | class Solution:
# O(n^2) time,
# O(n) space,
# Approach: two pointers, sliding window, hashmap
def findAnagrams(self, s: str, p: str) -> List[int]:
def areEqual(d1, d2) -> bool:
for key, value in d1.items():
if key not in d2.keys() or d2[key] != value: ... | find-all-anagrams-in-a-string | Two pointers(Sliding window) python3 solution | destifo | 0 | 12 | find all anagrams in a string | 438 | 0.49 | Medium | 7,766 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/2318451/Python-3-or-Sliding-Window-%2B-Hashmap-O(n%2Bm)-runtime | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
#Sliding window technique!
#Keep expanding sliding window until its size == len(p)
#Check if the characters you have accumulated have exact frequency count as that of p!
#If so, add Left index to answer array, shrin... | find-all-anagrams-in-a-string | Python 3 | Sliding Window + Hashmap O(n+m) runtime | JOON1234 | 0 | 22 | find all anagrams in a string | 438 | 0.49 | Medium | 7,767 |
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/2307180/Python-Sliding-Window | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
p_dic = {}
s_dic={}
answer=[]
for i in p:
if i not in p_dic:
p_dic[i]=0
p_dic[i]+=1
window_start = 0
p_length = sum(p_dic.values())
for window_end in range(len(s)):
right_char=s[window_end]
if right_char not in s_dic... | find-all-anagrams-in-a-string | Python Sliding Window | jashan1 | 0 | 23 | find all anagrams in a string | 438 | 0.49 | Medium | 7,768 |
https://leetcode.com/problems/k-th-smallest-in-lexicographical-order/discuss/1608540/Python3-traverse-denary-trie | class Solution:
def findKthNumber(self, n: int, k: int) -> int:
def fn(x):
"""Return node counts in denary trie."""
ans, diff = 0, 1
while x <= n:
ans += min(n - x + 1, diff)
x *= 10
diff *= 10
retur... | k-th-smallest-in-lexicographical-order | [Python3] traverse denary trie | ye15 | 3 | 435 | k th smallest in lexicographical order | 440 | 0.308 | Hard | 7,769 |
https://leetcode.com/problems/arranging-coins/discuss/2801813/Python-Simple-Binary-Search | class Solution:
def arrangeCoins(self, n: int) -> int:
first = 1
last = n
if n==1:
return 1
while first <= last:
mid = (first+last)//2
if mid*(mid+1) == 2*n:
return mid
elif mid*(mid+1) > 2*n:
last = mi... | arranging-coins | Python Simple Binary Search | BhavyaBusireddy | 2 | 69 | arranging coins | 441 | 0.462 | Easy | 7,770 |
https://leetcode.com/problems/arranging-coins/discuss/2325796/Python-Simple-Python-Solution | class Solution:
def arrangeCoins(self, n: int) -> int:
result = 0
stairs_number = 1
while n > 0:
n = n - stairs_number
stairs_number = stairs_number + 1
if n >= 0:
result = result + 1
return result | arranging-coins | [ Python ] ✅✅ Simple Python Solution 🥳✌👍 | ASHOK_KUMAR_MEGHVANSHI | 2 | 94 | arranging coins | 441 | 0.462 | Easy | 7,771 |
https://leetcode.com/problems/arranging-coins/discuss/1561360/Recursive-Python-Solution | class Solution:
def arrangeCoins(self, n: int, row=1) -> int:
if n < row:
return 0
return 1 + self.arrangeCoins(n - row, row + 1) | arranging-coins | Recursive Python Solution | ErikRodriguez-webdev | 2 | 95 | arranging coins | 441 | 0.462 | Easy | 7,772 |
https://leetcode.com/problems/arranging-coins/discuss/473189/1-Line-Python-3-Solution-AP-series-and-Quadratic-Equation-concept | class Solution:
def arrangeCoins(self, n: int) -> int:
return int((-1 + (1+(4*1*2*n))**0.5)//2) | arranging-coins | 1 Line Python 3 Solution [AP series & Quadratic Equation concept] | Mayuraksha | 2 | 214 | arranging coins | 441 | 0.462 | Easy | 7,773 |
https://leetcode.com/problems/arranging-coins/discuss/2097328/Python3-from-O(n)-to-O(1)-in-runtime-48ms-69.84 | class Solution:
def arrangeCoins(self, n: int) -> int:
return self.optimalOne(n)
return self.bruteForce(n)
# O(1) || O(1)
# runtime: 48ms 69.84%
# memory: 13.9mb 56.52%
# 1 + 2 + 3 + 4 ... n = n (n + 1) / 2 is a series
# where n is equal to the last term of our series and also represents the ... | arranging-coins | Python3 from O(n) to O(1) in runtime 48ms 69.84% | arshergon | 1 | 58 | arranging coins | 441 | 0.462 | Easy | 7,774 |
https://leetcode.com/problems/arranging-coins/discuss/1982248/Python-easy-to-read-and-understand | class Solution:
def arrangeCoins(self, n: int) -> int:
temp, total = 1, 1
rows = 1
while total <= n:
temp = temp+1
total += temp
if total > n:
break
rows += 1
return rows | arranging-coins | Python easy to read and understand | sanial2001 | 1 | 54 | arranging coins | 441 | 0.462 | Easy | 7,775 |
https://leetcode.com/problems/arranging-coins/discuss/1568923/Python-O(1)-time-faster-than-100 | class Solution:
def arrangeCoins(self, n: int) -> int:
x = math.floor(math.sqrt(n * 2))
return x if x * (x + 1) <= n * 2 else x - 1 | arranging-coins | Python O(1) time faster than 100% | dereky4 | 1 | 168 | arranging coins | 441 | 0.462 | Easy | 7,776 |
https://leetcode.com/problems/arranging-coins/discuss/373928/Python-1-line-99.57-math-with-total-explanation | class Solution:
def arrangeCoins(self, n: int) -> int:
return int((2*n + 1/4)**(1/2) - 1/2) | arranging-coins | Python 1-line 99.57% math with total explanation | DenysCoder | 1 | 262 | arranging coins | 441 | 0.462 | Easy | 7,777 |
https://leetcode.com/problems/arranging-coins/discuss/2829494/Arranging-Coin-or-Python | class Solution:
def arrangeCoins(self, n: int) -> int:
first = 1
last = n
if n == 1:
return 1
while first <= last:
mid = (first+last)//2
if mid*(mid+1) == 2*n:
return mid
elif mid*(mid+1) > 2*n:
last = mi... | arranging-coins | Arranging Coin | Python | jashii96 | 0 | 2 | arranging coins | 441 | 0.462 | Easy | 7,778 |
https://leetcode.com/problems/arranging-coins/discuss/2825684/Binary-Search-Approach-or-O(logn)-time-or-O(1)-space | class Solution:
def arrangeCoins(self, n: int) -> int:
left = 1
right = n
while left <= right:
mid = (left + right)//2
if (mid*(mid+1))//2 > n:
right = mid - 1
else:
left = mid + 1
return right | arranging-coins | Binary Search Approach | O(logn) time | O(1) space | advanced-bencoding | 0 | 8 | arranging coins | 441 | 0.462 | Easy | 7,779 |
https://leetcode.com/problems/arranging-coins/discuss/2793578/Brute-Force-Outline-and-Then-Mathematics-Explained-with-straightforward-steps. | class Solution:
def arrangeCoins(self, n: int) -> int:
'''
# Brute Force Method
coins_in_row = 1
coins_remaining = n
num_rows_built = 0
while coins_remaining > 0 :
coins_remaining -= coins_in_row
if coins_remaini... | arranging-coins | Brute Force Outline and Then Mathematics Explained with straightforward steps. | laichbr | 0 | 6 | arranging coins | 441 | 0.462 | Easy | 7,780 |
https://leetcode.com/problems/arranging-coins/discuss/2733042/Python-Solution-oror-Quadratic-Equation-Math-oror-TC-O(1)-oror-SC-O(1)-oror-Faster-than-97.55 | class Solution:
def arrangeCoins(self, n: int) -> int:
c=-2*n
ans1=int((-1+math.sqrt(1-4*c))//2)
ans2=int((-1-math.sqrt(1-4*c))//2)
return ans1 if ans1>0 else ans2 | arranging-coins | Python Solution || Quadratic Equation Math || TC O(1) || SC O(1) || Faster than 97.55% | RA2011050010037 | 0 | 4 | arranging coins | 441 | 0.462 | Easy | 7,781 |
https://leetcode.com/problems/arranging-coins/discuss/2729260/python-binary-search-solution | class Solution:
def arrangeCoins(self, n: int) -> int:
right = n
left = 0
while right >= left:
mid = (right+left)//2
guess = (mid * mid + mid)//2
if guess == n:
return mid
elif guess < n:
left = mid + 1
... | arranging-coins | python binary search solution | muge_zhang | 0 | 3 | arranging coins | 441 | 0.462 | Easy | 7,782 |
https://leetcode.com/problems/arranging-coins/discuss/2674614/Python-O(1)-O(1) | class Solution:
def arrangeCoins(self, n: int) -> int:
return int(math.floor((-1 + (4 * (2 * (n + 1))) ** 0.5) / 2)) | arranging-coins | Python - O(1), O(1) | Teecha13 | 0 | 5 | arranging coins | 441 | 0.462 | Easy | 7,783 |
https://leetcode.com/problems/arranging-coins/discuss/2546842/Python3-or-Utilizing-Arithmetic-Series-Math-Logic-%2B-Binary-Search%3A-TCO(logn)-S.C-O(1) | class Solution:
#Time-Complexity: O(log(n))
#Space-Complexity: O(1)
def arrangeCoins(self, n: int) -> int:
#Approach: First, notice that the minimum number of coins required
#to form each and every row is a number that is sum of
#arithmetic series(1+ 2+ 3 +... +n = (n * (n+1) / 2)
... | arranging-coins | Python3 | Utilizing Arithmetic Series Math Logic + Binary Search: TC=O(logn) S.C = O(1) | JOON1234 | 0 | 29 | arranging coins | 441 | 0.462 | Easy | 7,784 |
https://leetcode.com/problems/arranging-coins/discuss/2435939/C%2B%2BPython-Best-Optimized-Approach-with-Binary-Search | class Solution:
def arrangeCoins(self, n: int) -> int:
s = 1
e = n
while s<=e:
mid = s + (e-s)//2
temp = int(mid*(mid+1)/2)
if temp == n:
return mid
if temp < n:
s = mid + 1
else:
e = ... | arranging-coins | C++/Python Best Optimized Approach with Binary Search | arpit3043 | 0 | 24 | arranging coins | 441 | 0.462 | Easy | 7,785 |
https://leetcode.com/problems/arranging-coins/discuss/2370871/Python-Simple-Faster-Solutions-Binary-Search-and-Formula-oror-Documented | class Solution:
def arrangeCoins(self, n: int) -> int:
low, high = 0, n
# Repeat until the pointers low and high meet each other
while low <= high:
mid = (low + high) // 2 # middle point - pivot
if (mid * (mid+1)) >> 1 <= n:
low = ... | arranging-coins | [Python] Simple Faster Solutions - Binary Search and Formula || Documented | Buntynara | 0 | 16 | arranging coins | 441 | 0.462 | Easy | 7,786 |
https://leetcode.com/problems/arranging-coins/discuss/2369075/Python-Basic-Maths-Intuitive | class Solution:
def arrangeCoins(self, n: int) -> int:
for i in range(1,2**31):
val=i*(i+1)//2
if val>n:
a=i
break
elif val==n:
return i
return a-1 | arranging-coins | [Python] Basic Maths Intuitive | pheraram | 0 | 53 | arranging coins | 441 | 0.462 | Easy | 7,787 |
https://leetcode.com/problems/arranging-coins/discuss/2339621/easy-python-code-or-O(log-n)-or-binary-search | class Solution:
def sumofn(self,n):
return (n*(n+1))/2
def arrangeCoins(self, n: int) -> int:
l,r = 0,n
while(l<=r and l<n and r>=0):
m = (l+r)//2
m1 = self.sumofn(m)
if m1 > n:
r = m-1
elif m1 < n:
l = m+1
... | arranging-coins | easy python code | O(log n) | binary search | dakash682 | 0 | 63 | arranging coins | 441 | 0.462 | Easy | 7,788 |
https://leetcode.com/problems/arranging-coins/discuss/2329916/Arranging-Coins | class Solution:
def arrangeCoins(self, n: int) -> int:
x = n
count=0
for i in range(1,n+1):
if x >=i:
x=x-i
count+=1
else:
break
return count | arranging-coins | Arranging Coins | dhananjayaduttmishra | 0 | 20 | arranging coins | 441 | 0.462 | Easy | 7,789 |
https://leetcode.com/problems/arranging-coins/discuss/2329916/Arranging-Coins | class Solution:
def arrangeCoins(self, n: int) -> int:
l = 0
r = n
while l <=r :
m = (l+r)//2
curr = m*(m+1)//2
if curr==n:
return m
if n<curr:
r = m-1
else:
l=m+1
return r | arranging-coins | Arranging Coins | dhananjayaduttmishra | 0 | 20 | arranging coins | 441 | 0.462 | Easy | 7,790 |
https://leetcode.com/problems/arranging-coins/discuss/2170224/Python-simple-solution | class Solution:
def arrangeCoins(self, n: int) -> int:
ans = 0
for i in range(1,n+1):
n -= i
if n >= 0:
ans += 1
else:
break
return ans | arranging-coins | Python simple solution | StikS32 | 0 | 42 | arranging coins | 441 | 0.462 | Easy | 7,791 |
https://leetcode.com/problems/arranging-coins/discuss/2102773/Python-or-Simple-solution | class Solution:
def arrangeCoins(self, n: int) -> int:
levels = 0
while n > levels:
levels += 1
n -= levels
return levels | arranging-coins | Python | Simple solution | rahulsh31 | 0 | 74 | arranging coins | 441 | 0.462 | Easy | 7,792 |
https://leetcode.com/problems/arranging-coins/discuss/2099541/Python-Easy-solutions-with-complexities | class Solution:
def arrangeCoins(self, n: int) -> int:
stairs = []
temp = n
i=1
totalCoins = 0
while (n>=0):
stairs.append(i)
n = n-i
totalCoins =totalCoins + i
if totalCoins > temp:
... | arranging-coins | [Python] Easy solutions with complexities | mananiac | 0 | 27 | arranging coins | 441 | 0.462 | Easy | 7,793 |
https://leetcode.com/problems/arranging-coins/discuss/2098016/cleanandsimple-python-solution-O(logN) | class Solution:
def arrangeCoins(self, n: int) -> int:
i,j=1,n+1
while i<j:
mid=(i+j)>>1
t=(mid+1)*mid//2
if t<=n:
i=mid+1
else:
j=mid
return i-1 | arranging-coins | clean&simple python solution, O(logN) | xsank | 0 | 10 | arranging coins | 441 | 0.462 | Easy | 7,794 |
https://leetcode.com/problems/arranging-coins/discuss/1863689/Python-Easiest-Solution-With-Explanation-O(logn)-or-Binary-Search-or-Beg-to-Adv | class Solution:
def arrangeCoins(self, n: int) -> int:
def coins(n):
return (n * (1 + n)) // 2 # OR (mid / 2) * (mid + 1)
# above is the formula to calculate, number of coins required to fill the rows completely.
left, right = 1, n
while left <= right:
... | arranging-coins | Python Easiest Solution With Explanation O(logn) | Binary Search | Beg to Adv | rlakshay14 | 0 | 75 | arranging coins | 441 | 0.462 | Easy | 7,795 |
https://leetcode.com/problems/arranging-coins/discuss/1855252/Python-Easiest-Solution-With-Explanation-or-Maths-O(n)-or-Beg-to-Adv-or | class Solution:
def arrangeCoins(self, n: int) -> int:
count: int = 0 #took counter for counting the lines that are completely filled
i: int = 1 # taking variable as iterators for coins
k: int = 1 # taking variable as iterators for rows
while i <= n:
if i == k: # checkin... | arranging-coins | Python Easiest Solution With Explanation | Maths O(n) | Beg to Adv | | rlakshay14 | 0 | 44 | arranging coins | 441 | 0.462 | Easy | 7,796 |
https://leetcode.com/problems/arranging-coins/discuss/1851469/3-Lines-Python-Solution-oror-40-Faster-oror-Memory-less-than-70 | class Solution:
def arrangeCoins(self, n: int) -> int:
i=0
while n>i: i+=1 ; n-=i
return i | arranging-coins | 3-Lines Python Solution || 40% Faster || Memory less than 70% | Taha-C | 0 | 34 | arranging coins | 441 | 0.462 | Easy | 7,797 |
https://leetcode.com/problems/arranging-coins/discuss/1851469/3-Lines-Python-Solution-oror-40-Faster-oror-Memory-less-than-70 | class Solution:
def arrangeCoins(self, n: int) -> int:
l=0 ; r=n
while l<=r:
k=(r+l)//2
curr=k*(k+1)//2
if curr==n: return k
if n<curr: r=k-1
else: l=k+1
return r | arranging-coins | 3-Lines Python Solution || 40% Faster || Memory less than 70% | Taha-C | 0 | 34 | arranging coins | 441 | 0.462 | Easy | 7,798 |
https://leetcode.com/problems/arranging-coins/discuss/1851469/3-Lines-Python-Solution-oror-40-Faster-oror-Memory-less-than-70 | class Solution:
def arrangeCoins(self, n: int) -> int:
return int((sqrt(1+8*n)-1)/2) | arranging-coins | 3-Lines Python Solution || 40% Faster || Memory less than 70% | Taha-C | 0 | 34 | arranging coins | 441 | 0.462 | Easy | 7,799 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.