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/largest-multiple-of-three/discuss/564606/Python-Clean-code-using-remainder-array | class Solution:
def largestMultipleOfThree(self, digits: List[int]) -> str:
digits.sort()
rem = [d % 3 for d in digits]
s = sum(rem) % 3
def remove_smallest_with_rem(x):
idx = rem.index(x)
del digits[idx]
del rem[idx]
if s != 0:
try:
remove_smallest_with_rem(s)
... | largest-multiple-of-three | [Python] Clean code using remainder array | rajkar86 | 1 | 290 | largest multiple of three | 1,363 | 0.333 | Hard | 20,500 |
https://leetcode.com/problems/largest-multiple-of-three/discuss/517725/Python3-super-simple-long-solution | class Solution:
def largestMultipleOfThree(self, digits: List[int]) -> str:
digits.sort()
q0,q1,q2=[],[],[]
sums=0
for i in range(len(digits)):
sums+=digits[i]
remain=digits[i]%3
if remain==0:
q0.append(digits[i])
elif remain==1:
q1.append(digits[i])
else:
q2.append(digits[i])
q1.s... | largest-multiple-of-three | Python3 super simple long solution | jb07 | 1 | 125 | largest multiple of three | 1,363 | 0.333 | Hard | 20,501 |
https://leetcode.com/problems/largest-multiple-of-three/discuss/517688/Python3-A-sorting-based-solution | class Solution:
def largestMultipleOfThree(self, digits: List[int]) -> str:
digits.sort(reverse=True)
category = dict()
for d in digits: category.setdefault(d%3, []).append(d)
parity = sum(digits) % 3
if parity != 0:
if len(category.get(parity, [])) > 0:... | largest-multiple-of-three | [Python3] A sorting-based solution | ye15 | 1 | 61 | largest multiple of three | 1,363 | 0.333 | Hard | 20,502 |
https://leetcode.com/problems/largest-multiple-of-three/discuss/2329746/python-3-or-readable-solution-or-O(n)O(1) | class Solution:
def largestMultipleOfThree(self, digits: List[int]) -> str:
count = collections.Counter(digits)
mod1 = count[1] + count[4] + count[7]
zeroMod1 = not mod1
mod1 %= 3
mod2 = count[2] + count[5] + count[8]
zeroMod2 = not mod2
mod2... | largest-multiple-of-three | python 3 | readable solution | O(n)/O(1) | dereky4 | 0 | 60 | largest multiple of three | 1,363 | 0.333 | Hard | 20,503 |
https://leetcode.com/problems/largest-multiple-of-three/discuss/2277498/Python-99-faster-easy-solution | class Solution:
def largestMultipleOfThree(self, nums: List[int]) -> str:
nums.sort(reverse = True)
if sum(nums)%3 == 1:
# we need to remove one minimum or two with remainder == 2
flag = False
for i in reversed(range(len(nums))):
if nums[i]%... | largest-multiple-of-three | Python 99% faster easy solution | Abhi_009 | 0 | 53 | largest multiple of three | 1,363 | 0.333 | Hard | 20,504 |
https://leetcode.com/problems/largest-multiple-of-three/discuss/517620/Python3-super-simple-solution | class Solution:
def largestMultipleOfThree(self, digits: List[int]) -> str:
digits.sort()
q0,q1,q2=[],[],[]
sums=0
for i in range(len(digits)):
sums+=digits[i]
remain=digits[i]%3
if remain==0:
q0.append(digits[i])
elif remain==1:
q1.append(digits[i])
else:
q2.append(digits[i])
q1.s... | largest-multiple-of-three | Python3 super simple solution | jb07 | 0 | 103 | largest multiple of three | 1,363 | 0.333 | Hard | 20,505 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/613343/Simple-Python-Solution-72ms-and-13.8-MB-EXPLAINED | class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
a=[]
numi = sorted(nums)
for i in range(0,len(nums)):
a.append(numi.index(nums[i]))
return a | how-many-numbers-are-smaller-than-the-current-number | Simple Python Solution [72ms and 13.8 MB] EXPLAINED | code_zero | 10 | 849 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,506 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/1863223/Python-solution-faster-than-86 | class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
sort_nums = sorted(nums)
res = []
for i in nums:
res.append(sort_nums.index(i))
return res | how-many-numbers-are-smaller-than-the-current-number | Python solution faster than 86% | alishak1999 | 5 | 288 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,507 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/2119776/Python-Easy-solution-with-complexities | class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
nums1 = sorted(nums) #time O(nlogn)
dic = {}
answer = []
for i in range(0,len(nums1)): #time O(n)
if nums1[i] in dic: #time O(1)
conti... | how-many-numbers-are-smaller-than-the-current-number | [Python] Easy solution with complexities | mananiac | 3 | 206 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,508 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/2119776/Python-Easy-solution-with-complexities | class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
nums1 = sorted(nums) #time O(nlogn)
dic = {}
answer = []
for index,value in enumerate(nums1): #time O(n)
dic.setdefault(value,index)
... | how-many-numbers-are-smaller-than-the-current-number | [Python] Easy solution with complexities | mananiac | 3 | 206 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,509 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/2119776/Python-Easy-solution-with-complexities | class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
nums1 = sorted(nums) #time O(nlogn)
dic = {}
answer = []
for index,value in enumerate(nums1): #time O(n)
dic.setdefault(value,index)
... | how-many-numbers-are-smaller-than-the-current-number | [Python] Easy solution with complexities | mananiac | 3 | 206 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,510 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/2119776/Python-Easy-solution-with-complexities | class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
count = [0] * 102
answer = []
for num in nums:
count[num+1] = count[num+1] + 1
for i in range(1, 102):
count[i] = count[i] + count[i-1]
... | how-many-numbers-are-smaller-than-the-current-number | [Python] Easy solution with complexities | mananiac | 3 | 206 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,511 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/1492937/WEEB-DOES-PYTHON(FASTER-THAN-96.67) | class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
arr, result = sorted(nums), []
mapper = self.helper(arr, {})
for i in range(len(nums)):
result.append(mapper[nums[i]])
return result
def helper(self, arr, mapper):
count, cur_max = 0, -float("inf")
for i in range(0... | how-many-numbers-are-smaller-than-the-current-number | WEEB DOES PYTHON(FASTER THAN 96.67%) | Skywalker5423 | 3 | 373 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,512 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/1882814/Python-easy-to-read-and-understand-or-sorting | class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
d = collections.defaultdict(int)
sorted_nums = sorted(nums)
for i, val in enumerate(sorted_nums):
d[val] = d.get(val, i)
for i in range(len(nums)):
nums[i] = d... | how-many-numbers-are-smaller-than-the-current-number | Python easy to read and understand | sorting | sanial2001 | 2 | 183 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,513 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/1739142/1365-how-many-lesser | class Solution(object):
def smallerNumbersThanCurrent(self, nums):
rArray = []
for i in range(len(nums)):
lesser = 0
for j in range(len(nums)):
if i!=j and nums[j] < nums[i]:
lesser += 1
rArray.append(lesser)
... | how-many-numbers-are-smaller-than-the-current-number | 1365 - how many lesser | ankit61d | 2 | 48 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,514 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/1427607/Python-two-approaches%3A | class Solution(object):
def smallerNumbersThanCurrent(self, nums):
sortedNums = sorted(nums)
dict_count = {}
for i in range(len(nums)):
if sortedNums[i] not in dict_count:
dict_count[sortedNums[i]] = i
return [dict_count[num] for... | how-many-numbers-are-smaller-than-the-current-number | Python two approaches: | Aroy88 | 2 | 312 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,515 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/1427607/Python-two-approaches%3A | class Solution(object):
def smallerNumbersThanCurrent(self, nums):
sortedNums = sorted(nums)
result = []
for i in nums:
result.append(sortedNums.index(i))
return result | how-many-numbers-are-smaller-than-the-current-number | Python two approaches: | Aroy88 | 2 | 312 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,516 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/1427607/Python-two-approaches%3A | class Solution(object):
def smallerNumbersThanCurrent(self, nums):
sortedNums = sorted(nums)
return [sortedNums.index(num) for num in nums] | how-many-numbers-are-smaller-than-the-current-number | Python two approaches: | Aroy88 | 2 | 312 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,517 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/2393882/Python-solution-short-code-.. | class Solution(object):
def smallerNumbersThanCurrent(self, nums):
arr=[]
num = sorted(nums)
for i in range(0,len(nums)):
arr.append(num.index(nums[i]))
return arr | how-many-numbers-are-smaller-than-the-current-number | Python solution short code ..🐍 | RohanRob | 1 | 79 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,518 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/1742299/Short-Efficient-and-Easy-Solution-Python | class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
# sort in descending order
sorted_nums = sorted(nums, reverse=True)
small_nums = dict()
n = len(sorted_nums)
for i, num in enumerate(sorted_nums, 1):
small_nums[num] = n - ... | how-many-numbers-are-smaller-than-the-current-number | Short, Efficient and Easy Solution {Python} | ashudva | 1 | 124 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,519 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/1670663/Python-Solution-using-Sorting | class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
n=sorted(nums)
n2=[]
for i in range(len(n)):
n2.append(n.index(nums[i]))
return (n2) | how-many-numbers-are-smaller-than-the-current-number | Python Solution using Sorting | shreerajbhamare | 1 | 75 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,520 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/1379093/Python-solution-using-filter | class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
ans = []
for n in nums:
temp = list(filter(lambda x:x<n,nums))
ans.append(len(temp))
return ans | how-many-numbers-are-smaller-than-the-current-number | Python solution using filter | shraddhapp | 1 | 97 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,521 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/1182226/Binary-Search-Lower-Bound-Faster-than-80-Python-3-Solution | class Solution:
def find(self,arr,target):
low = 0
high = len(arr)
close = 0
while low<=high:
mid = low+(high-low)//2
if arr[mid] == target:
close = mid
high = mid-1
elif arr[mid]>target:
high = mid-1... | how-many-numbers-are-smaller-than-the-current-number | Binary Search - Lower Bound - Faster than 80% Python 3 Solution | tgoel219 | 1 | 58 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,522 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/788481/Python-Solution-with-complexity-analysis | class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
n, counts = len(nums), []
numMap = {} # O(n) space
sortedNums = sorted(nums) # O(n log n) time, O(n) space
# O(n)
for i in range(n-1, -1, -1):
numMap[sortedNums[i]] = i
... | how-many-numbers-are-smaller-than-the-current-number | Python Solution with complexity analysis | zuu | 1 | 132 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,523 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/530333/Simple-and-Easy-to-understand-Python3-solution-using-sort()-and-dictionary | class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
my_dict = {}
n = nums[:]
nums.sort()
for i in range(len(nums)):
if i == 0 or nums[i-1] < nums[i]:
my_dict[nums[i]] = i
return [my_dict[nu... | how-many-numbers-are-smaller-than-the-current-number | Simple and Easy to understand Python3 solution [using sort() and dictionary] | aravind5010 | 1 | 281 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,524 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/2838593/Python3-~96-faster | class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
# counters will have a sorted dict of nums and its count
counters = collections.Counter(sorted(nums))
# Update the dict values with sum of nums[j]s for each nums[i]
sum = 0
for k in counters.keys():
... | how-many-numbers-are-smaller-than-the-current-number | Python3 ~96% faster | jubinmehta369 | 0 | 3 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,525 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/2826703/Python-or-Fenwick-Tree-O(n-log-n)-to-Array-based-O(n) | class Solution:
def smallerNumbersThanCurrent(self, xs: List[int]) -> List[int]:
tree = FenwickTree(101)
for x in xs:
# Add 1 to the count of occurrences of x. The offset
# of 1 is due to the fact that the Fenwick tree uses
# the indices 1 through n−1.
... | how-many-numbers-are-smaller-than-the-current-number | Python | Fenwick Tree O(n log n) to Array-based O(n) | on_danse_encore_on_rit_encore | 0 | 1 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,526 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/2825858/Simple-Python-Solution | class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
output=[]
for i in nums:
count=0
for j in nums:
if i>j:
count=count+1
output.append(count)
return output | how-many-numbers-are-smaller-than-the-current-number | Simple Python Solution | durgaraopolamarasetti | 0 | 2 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,527 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/2821580/2-line-easy-peasy | class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
x = sorted(nums)
return [x.index(i) for i in nums] | how-many-numbers-are-smaller-than-the-current-number | 2 line easy peasy | almazgimaev | 0 | 2 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,528 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/2807075/Python-Solution | class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
arr = sorted(nums)
return [arr.index(i) for i in nums] | how-many-numbers-are-smaller-than-the-current-number | Python Solution | Divyanshu_Kumar | 0 | 3 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,529 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/2805672/python-code | class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
l=[]
for i in range(len(nums)):
c=0
for j in range(len(nums)):
if(nums[i]>nums[j]):
c=c+1
l.append(c)
return l; | how-many-numbers-are-smaller-than-the-current-number | python code | Manjeet_Malik | 0 | 2 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,530 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/2805661/here's-the-soln | class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
res=[]
for i in range(len (nums)):
count=0
for j in range(len(nums)):
if(nums[i]>nums[j] and i!=j):
count=count+1
res.append(count)
retur... | how-many-numbers-are-smaller-than-the-current-number | here's the soln | Prabhleen_17 | 0 | 1 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,531 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/2711783/Two-Solutions-including-One-Liner-or-Time%3A-O(n2)-or-Python-3 | class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
return [len([other_num for other_num in nums if other_num < num]) for num in nums] | how-many-numbers-are-smaller-than-the-current-number | Two Solutions including One-Liner | Time: O(n^2) | Python / 3 | keioon | 0 | 7 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,532 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/2674996/Python-O(nlogn)-solution-with-O(n)-extra-space | class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
map = {}
temp = sorted(nums)
for i in range(len(temp)):
if temp[i] not in map:
map[temp[i]] = i
res = []
for number in nums:
res.append(map[number])
... | how-many-numbers-are-smaller-than-the-current-number | Python O(nlogn) solution with O(n) extra space | cat_woman | 0 | 5 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,533 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/2669801/easy-using-loops-python | class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
m=[]
res=0
for i in range(len(nums)):
for j in range(len(nums)):
if nums[i]>nums[j]:
res+=1
m.append(res)
res=0
return(m) | how-many-numbers-are-smaller-than-the-current-number | easy using loops python | AMBER_FATIMA | 0 | 1 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,534 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/2663302/Simple-python-code-with-explanation | class Solution:
def smallerNumbersThanCurrent(self, nums):
#create an empty list(k)
k = []
#iterate over the elements in list(nums)
for i in range(len(nums)):
#create a variable(summ) and assign 0 to it
summ = 0
#iterate over the elements in lis... | how-many-numbers-are-smaller-than-the-current-number | Simple python code with explanation | thomanani | 0 | 40 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,535 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/2653089/Python-Simple-Solution | class Solution(object):
def smallerNumbersThanCurrent(self, nums):
sorted_nums=sorted(nums)
op=[sorted_nums.index(n) for n in nums]
return op | how-many-numbers-are-smaller-than-the-current-number | Python Simple Solution | shandilayasujay | 0 | 46 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,536 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/2611813/Python-Basic-Solution | class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
n = len(nums)
result = []
for i in range(n):
j_counter = 0
for j in range(0, n):
if nums[j] < nums[i]:
j_counter += 1
result.append(j_cou... | how-many-numbers-are-smaller-than-the-current-number | Python Basic Solution | dk_talks | 0 | 31 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,537 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/2587322/optimized-solution-(python3) | class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
sorted_array = sorted(nums)
h_map = {}
result =[]
n = len(nums)
for i in range(n):
if sorted_array[i] not in h_map:
h_map[sorted_array[i]] = i
... | how-many-numbers-are-smaller-than-the-current-number | optimized solution (python3) | kingshukmaity60 | 0 | 74 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,538 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/2564478/using-hashmap | class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
# the size of nums, n, is only 500
# create an array to store the smaller nums for each index
# create a hashmap to store the numbers in nums with numbers smaller as values
# if a number is in the hash... | how-many-numbers-are-smaller-than-the-current-number | using hashmap | andrewnerdimo | 0 | 52 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,539 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/2564478/using-hashmap | class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
# optimized solution in place
d = dict()
arr = sorted(nums)
n = len(nums)
for i in range(n):
val = arr[i]
if val not in d:
d[val] = i
for i in range(n):
num = nums[i]
nums[i] = d[num]
return n... | how-many-numbers-are-smaller-than-the-current-number | using hashmap | andrewnerdimo | 0 | 52 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,540 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/2554556/Python3-Solution-oror-Easy-oror-O(n2) | class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
new = []
for i in nums:
count = 0
for j in nums:
if i > j :
count += 1
new.append(count)
return new | how-many-numbers-are-smaller-than-the-current-number | Python3 Solution || Easy || O(n^2) | shashank_shashi | 0 | 16 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,541 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/2541034/Python-easy-solution | class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
sorted_nums = nums[:]
sorted_nums.sort()
for index, value in enumerate(nums):
nums[index] = sorted_nums.index(value)
return nums | how-many-numbers-are-smaller-than-the-current-number | Python easy solution | anton_python | 0 | 45 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,542 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/2529340/Easy-solution-in-python3-(2-solutions) | class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
return [sorted(nums).index(i) for i in nums]
count = 0
result = []
for i in range(len(nums)):
for j in range(len(nums)):
if nums[j] < nums[i]:
c... | how-many-numbers-are-smaller-than-the-current-number | Easy solution in python3 (2 solutions) | khushie45 | 0 | 48 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,543 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/2529340/Easy-solution-in-python3-(2-solutions) | class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
return [sorted(nums).index(i) for i in nums] | how-many-numbers-are-smaller-than-the-current-number | Easy solution in python3 (2 solutions) | khushie45 | 0 | 48 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,544 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/2417458/easy-python-solution | class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
i = 0 ;c = 0 ; res = []
while i<len(nums):
for j in range(len(nums)):
if nums[j] < nums[i]:
c+=1
res.append(c)
c = 0
i+=1
ret... | how-many-numbers-are-smaller-than-the-current-number | easy python solution | Abdulrahman_Ahmed | 0 | 81 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,545 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/2363685/How-Many-Numbers-Are-Smaller-Than-the-Current-Number | class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
y = [i for i in nums ]
for i in range(len(y)):
minIndex=i
for j in range(i+1,len(y)):
if y[minIndex]>y[j]:
minIndex = j
x = y[minIndex]
... | how-many-numbers-are-smaller-than-the-current-number | How Many Numbers Are Smaller Than the Current Number | dhananjayaduttmishra | 0 | 17 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,546 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/2280454/How-Many-Numbers-Are-Smaller-Than-the-Current-Number | class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
temp = sorted(nums)
for i in range(len(nums)):
nums[i] = temp.index(nums[i])
return nums | how-many-numbers-are-smaller-than-the-current-number | How Many Numbers Are Smaller Than the Current Number | tancoder24 | 0 | 45 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,547 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/2280454/How-Many-Numbers-Are-Smaller-Than-the-Current-Number | class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
temp = sorted(nums)
return [ temp.index(nums[i]) for i in range(len(nums)) ] | how-many-numbers-are-smaller-than-the-current-number | How Many Numbers Are Smaller Than the Current Number | tancoder24 | 0 | 45 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,548 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/2158591/Using-dictionary-and-sort-function-Python | class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
temp = nums.copy()
temp.sort()
lookup = {}
res = []
for i, n in enumerate(temp):
if n not in lookup:
lookup[n] = i
for j in nums:
res.append(look... | how-many-numbers-are-smaller-than-the-current-number | Using dictionary and sort function Python | ankurbhambri | 0 | 96 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,549 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/1982139/Python-Simplest-Solution-With-Explanation-or-92.32-Faster-or-Sorting-or-Beg-to-adv | class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
sorted_nums = sorted(nums) # sorting the list
# In this way, all the elements are sorted and we dont need to separately count the smaller elements separately.
res = [] # taking empty list to save the result
... | how-many-numbers-are-smaller-than-the-current-number | Python Simplest Solution With Explanation | 92.32% Faster | Sorting | Beg to adv | rlakshay14 | 0 | 164 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,550 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/1982025/Python-Easiest-Solution-With-Explanation. | class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
res = [] # took a list to store the result
count = 0 # took a counter
for i in range(len(nums)): # loop one to traverse all the elements in the list.
for j in range(len(nums)): # loop two... | how-many-numbers-are-smaller-than-the-current-number | Python Easiest Solution With Explanation. | rlakshay14 | 0 | 75 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,551 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/1930538/Python-Simple-and-Clean! | class Solution:
def smallerNumbersThanCurrent(self, nums):
d = {}
for i,n in enumerate(sorted(nums)):
if n not in d: d[n]=i
return [d[i] for i in nums] | how-many-numbers-are-smaller-than-the-current-number | Python - Simple and Clean! | domthedeveloper | 0 | 85 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,552 |
https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/discuss/1930538/Python-Simple-and-Clean! | class Solution:
def smallerNumbersThanCurrent(self, nums):
count = [0 for i in range(102)]
for num in nums: count[num+1] += 1
for i in range(1, len(count)): count[i] += count[i-1]
return [count[num] for num in nums] | how-many-numbers-are-smaller-than-the-current-number | Python - Simple and Clean! | domthedeveloper | 0 | 85 | how many numbers are smaller than the current number | 1,365 | 0.867 | Easy | 20,553 |
https://leetcode.com/problems/rank-teams-by-votes/discuss/2129031/python-3-oror-simple-O(n)O(1)-solution | class Solution:
def rankTeams(self, votes: List[str]) -> str:
teamVotes = collections.defaultdict(lambda: [0] * 26)
for vote in votes:
for pos, team in enumerate(vote):
teamVotes[team][pos] += 1
return ''.join(sorted(teamVotes.keys(), reverse=True,
... | rank-teams-by-votes | python 3 || simple O(n)/O(1) solution | dereky4 | 5 | 465 | rank teams by votes | 1,366 | 0.586 | Medium | 20,554 |
https://leetcode.com/problems/rank-teams-by-votes/discuss/524981/Python3-organize-votes-in-a-frequency-table | class Solution:
def rankTeams(self, votes: List[str]) -> str:
ranking = dict()
for vote in votes:
for i, x in enumerate(vote):
ranking.setdefault(x, [0]*len(vote))[i] += 1
return "".join(sorted(sorted(vote), key=ranking.get, reverse=True)) | rank-teams-by-votes | [Python3] organize votes in a frequency table | ye15 | 4 | 631 | rank teams by votes | 1,366 | 0.586 | Medium | 20,555 |
https://leetcode.com/problems/rank-teams-by-votes/discuss/1200413/Python-Following-Hints | class Solution:
ALPHABET_LENGTH = 26
ASCII_OFFSET = 97
def rankTeams(self, votes: List[str]) -> str:
"""
1. Build array rank where rank[i][j] is the number of votes for team i to be the j-th rank.
2. Sort the teams by rank array. if rank array is the same for two or more te... | rank-teams-by-votes | Python Following Hints | doubleimpostor | 1 | 386 | rank teams by votes | 1,366 | 0.586 | Medium | 20,556 |
https://leetcode.com/problems/rank-teams-by-votes/discuss/676468/Python3-2-liner-Rank-Teams-by-Votes | class Solution:
def rankTeams(self, votes: List[str]) -> str:
counters = [Counter(v) for v in zip(*votes)]
return ''.join(sorted(votes[0], key=lambda x:(*(-c[x] for c in counters), x))) | rank-teams-by-votes | Python3 2 liner - Rank Teams by Votes | r0bertz | 1 | 320 | rank teams by votes | 1,366 | 0.586 | Medium | 20,557 |
https://leetcode.com/problems/rank-teams-by-votes/discuss/542452/Python-Straightforward | class Solution(object):
def rankTeams(self, votes):
"""
:type votes: List[str]
:rtype: str
"""
counts = collections.defaultdict(list)
for vote in zip(*votes):
cntr = collections.Counter(vote)
for ch in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
... | rank-teams-by-votes | [Python] Straightforward | fallenranger | 1 | 494 | rank teams by votes | 1,366 | 0.586 | Medium | 20,558 |
https://leetcode.com/problems/rank-teams-by-votes/discuss/2842233/Explained-solution-optimal | class Solution:
def rankTeams(self, votes: List[str]) -> str:
# map teams to their ranking votes in a map
# once the teams are mapped get a list of the teams sorted by their character
# sort by character first because next, the teams will be sorted by their ranking votes
# that way, ... | rank-teams-by-votes | Explained solution optimal | andrewnerdimo | 0 | 3 | rank teams by votes | 1,366 | 0.586 | Medium | 20,559 |
https://leetcode.com/problems/rank-teams-by-votes/discuss/2729727/Python-98-speed-6-lines | class Solution:
def rankTeams(self, votes: List[str]) -> str:
n_team = len(votes[0])
vote_count = dict(zip(votes[0], ([0 for _ in range(n_team)] for i in range(n_team))))
for vote in votes:
for i, team in enumerate(vote):
vote_count[team][i] += 1
... | rank-teams-by-votes | Python 98 % speed, 6 lines | very_drole | 0 | 37 | rank teams by votes | 1,366 | 0.586 | Medium | 20,560 |
https://leetcode.com/problems/rank-teams-by-votes/discuss/2393968/Python-sorting | class Solution:
def rankTeams(self, votes: List[str]) -> str:
n = len(votes[0])
places = defaultdict(lambda: [0]*n)
for vote in votes:
for idx, team in enumerate(vote):
places[team][idx] -= 1 # for correct sorting
places = sorted([p + [te... | rank-teams-by-votes | Python, sorting | blue_sky5 | 0 | 142 | rank teams by votes | 1,366 | 0.586 | Medium | 20,561 |
https://leetcode.com/problems/rank-teams-by-votes/discuss/2159059/Python-solution | class Solution:
def rankTeams(self, votes: List[str]) -> str:
c = {}
for w in votes:
val = 0
for char in w:
if char in c:
c[char][val] += 1
else:
c[char] = [0] * len(w)
c[char... | rank-teams-by-votes | Python solution | user6397p | 0 | 126 | rank teams by votes | 1,366 | 0.586 | Medium | 20,562 |
https://leetcode.com/problems/rank-teams-by-votes/discuss/1487092/Unique-and-Easy-oror-Sorting-of-dictionary-oror-93-faster | class Solution:
def rankTeams(self, votes: List[str]) -> str:
dic = defaultdict(lambda : [0]*len(votes[0]))
for vote in votes:
for p,t in enumerate(vote): # p-> postion(rank), t->teamName
dic[t][p]-=1
res = dict(sorted(dic.items(),key=lambda x: (x[1],x[0])))
ret... | rank-teams-by-votes | 📌📌 Unique & Easy || Sorting of dictionary || 93% faster 🐍 | abhi9Rai | 0 | 350 | rank teams by votes | 1,366 | 0.586 | Medium | 20,563 |
https://leetcode.com/problems/rank-teams-by-votes/discuss/1468421/Python3-solution-using-dictionary | class Solution:
def rankTeams(self, votes: List[str]) -> str:
d = {}
for z in range(len(votes[0])):
for i in votes:
d[i[z]] = d.get(i[z],'') + chr(97 + z)
d = dict((i,j) for i,j in sorted(d.items(),key=lambda x:x[0]))
return ''.join([i for i,j in sorted(d.... | rank-teams-by-votes | Python3 solution using dictionary | EklavyaJoshi | 0 | 189 | rank teams by votes | 1,366 | 0.586 | Medium | 20,564 |
https://leetcode.com/problems/linked-list-in-binary-tree/discuss/525814/Python3-A-naive-dp-approach | class Solution:
def isSubPath(self, head: ListNode, root: TreeNode) -> bool:
#build longest prefix-suffix array
pattern, lps = [head.val], [0] #longest prefix-suffix array
j = 0
while head.next:
head = head.next
pattern.append(head.val)
... | linked-list-in-binary-tree | [Python3] A naive dp approach | ye15 | 1 | 96 | linked list in binary tree | 1,367 | 0.435 | Medium | 20,565 |
https://leetcode.com/problems/linked-list-in-binary-tree/discuss/2633911/python3-easy-peasy-solution-using-combo-of-recursion-and-iteration | class Solution:
def isSubPath(self, head: Optional[ListNode], root: Optional[TreeNode]) -> bool:
def dfs(hd,node):
if not hd.next :
return hd.val==node.val
if hd.val==node.val:
if node.left and node.right:
return dfs(hd.next,node.le... | linked-list-in-binary-tree | python3 easy-peasy solution using combo of recursion and iteration | benon | 0 | 15 | linked list in binary tree | 1,367 | 0.435 | Medium | 20,566 |
https://leetcode.com/problems/linked-list-in-binary-tree/discuss/1856648/Backtrack-with-help-of-string-express | class Solution:
def isSubPath(self, head: ListNode, root: TreeNode) -> bool:
return self.backtrack(head, root)
def backtrack(self, head: ListNode, root: TreeNode) -> bool:
target_path = ''
while head:
target_path += f'|{head.val}'
head = head.next
path =... | linked-list-in-binary-tree | Backtrack with help of string express | steve-jokes | 0 | 28 | linked list in binary tree | 1,367 | 0.435 | Medium | 20,567 |
https://leetcode.com/problems/linked-list-in-binary-tree/discuss/1810646/Python3-oror-2-Method-BFS-BFS%2BDFS-oror-recursion-sucks-I-love-recursion | class Solution:
def isSubPath(self, head: Optional[ListNode], root: Optional[TreeNode]) -> bool:
def listcheck(root, head):
if not head:
res[0] = True
return
elif not root: return
if root.val ==... | linked-list-in-binary-tree | Python3 || 2 Method - BFS, BFS+DFS || recursion sucks / I love recursion ❤ | Dewang_Patil | 0 | 100 | linked list in binary tree | 1,367 | 0.435 | Medium | 20,568 |
https://leetcode.com/problems/linked-list-in-binary-tree/discuss/1810646/Python3-oror-2-Method-BFS-BFS%2BDFS-oror-recursion-sucks-I-love-recursion | class Solution:
def isSubPath(self, head: Optional[ListNode], root: Optional[TreeNode]) -> bool:
def listchk(head, root):
if not head:
return True
if root and head.val == root.val:
return (listchk(head.next, root.left) or
listchk(head... | linked-list-in-binary-tree | Python3 || 2 Method - BFS, BFS+DFS || recursion sucks / I love recursion ❤ | Dewang_Patil | 0 | 100 | linked list in binary tree | 1,367 | 0.435 | Medium | 20,569 |
https://leetcode.com/problems/linked-list-in-binary-tree/discuss/1440965/Python3-Recursive-solution | class Solution:
def traversal(self, head, node):
if not head:
return True
if not node:
return False
return head.val == node.val and (
self.traversal(head.next, node.left) or self.traversal(head.next, node.right))
def isSubPath(self,... | linked-list-in-binary-tree | [Python3] Recursive solution | maosipov11 | 0 | 89 | linked list in binary tree | 1,367 | 0.435 | Medium | 20,570 |
https://leetcode.com/problems/linked-list-in-binary-tree/discuss/1393707/Python3-Preorder-Traversal-%2B-KMP-matching.-greater99-runtime-70%2Bms-.-O(N)-Space-O(N)-RunTime | class Solution:
def isSubPath(self, head: Optional[ListNode], root: Optional[TreeNode]) -> bool:
# Utilize KMP String Pattern Matching Algorithm with the preorder traversal
# O(N) space where N is max( depth of Tree, size of linked llist)
# O(N) Runtime.
def LPS(stack, size):
... | linked-list-in-binary-tree | [Python3] Preorder Traversal + KMP matching. >99% runtime, 70+ms . O(N) Space O(N) RunTime | whitehatbuds | 0 | 115 | linked list in binary tree | 1,367 | 0.435 | Medium | 20,571 |
https://leetcode.com/problems/linked-list-in-binary-tree/discuss/531241/Python3-108ms-solution | class Solution:
def isSubPath(self, head: ListNode, root: TreeNode) -> bool:
if root is None:
return False
if self.helper(head, root):
return True
if self.isSubPath(head, root.left):
return True
return self.isSubPath(head, root.right)
def help... | linked-list-in-binary-tree | Python3 108ms solution | tjucoder | 0 | 55 | linked list in binary tree | 1,367 | 0.435 | Medium | 20,572 |
https://leetcode.com/problems/minimum-cost-to-make-at-least-one-valid-path-in-a-grid/discuss/1504913/Python-or-Template-or-0-1-BFS-vs-Dijkstra-or-Explanation | class Solution:
def minCost(self, grid: List[List[int]]) -> int:
graph = {}
m, n = len(grid), len(grid[0])
def addEdges(i, j):
graph[(i, j)] = {}
neighbors = [(i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1)]
for each in neighbors:
x... | minimum-cost-to-make-at-least-one-valid-path-in-a-grid | Python | Template | 0-1 BFS vs Dijkstra | Explanation | detective_dp | 3 | 290 | minimum cost to make at least one valid path in a grid | 1,368 | 0.613 | Hard | 20,573 |
https://leetcode.com/problems/minimum-cost-to-make-at-least-one-valid-path-in-a-grid/discuss/527871/Two-kinds-of-BFS%3A-deque-BFS-and-layer-by-layer-BFS-(python) | class Solution:
def minCost(self, grid: List[List[int]]) -> int:
m = len(grid)
n = len(grid[0])
i_curr, j_curr = 0, 0
cost = 0
frontier = []
visited = [[False for _ in range(n)] for _ in range(m)]
dirs = [(0, 1), (0, -1), (1, 0), (-1, 0)]
while 0 <= i_... | minimum-cost-to-make-at-least-one-valid-path-in-a-grid | Two kinds of BFS: deque BFS and layer by layer BFS (python) | lechen999 | 2 | 156 | minimum cost to make at least one valid path in a grid | 1,368 | 0.613 | Hard | 20,574 |
https://leetcode.com/problems/minimum-cost-to-make-at-least-one-valid-path-in-a-grid/discuss/1735980/Python-Djikstras-vs-01-BFS | class Solution:
def minCost(self, grid) -> int:
m, n = len(grid), len(grid[0])
pq = [(0, 0, 0)]
cost = 0
visited = [[False for _ in range(n)] for _ in range(m)]
while pq:
cost, x, y = heapq.heappop(pq)
# print(cost, x, y)
visited[x][y] = T... | minimum-cost-to-make-at-least-one-valid-path-in-a-grid | [Python] Djikstras vs 01 BFS | sanial2001 | 1 | 108 | minimum cost to make at least one valid path in a grid | 1,368 | 0.613 | Hard | 20,575 |
https://leetcode.com/problems/minimum-cost-to-make-at-least-one-valid-path-in-a-grid/discuss/1735980/Python-Djikstras-vs-01-BFS | class Solution:
def minCost(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
pq = [(0, 0, 0)]
cost = 0
visited = [[False for _ in range(n)] for _ in range(m)]
while pq:
cost, x, y = pq.pop(0)
#print(cost, x, y)
visited[x... | minimum-cost-to-make-at-least-one-valid-path-in-a-grid | [Python] Djikstras vs 01 BFS | sanial2001 | 1 | 108 | minimum cost to make at least one valid path in a grid | 1,368 | 0.613 | Hard | 20,576 |
https://leetcode.com/problems/minimum-cost-to-make-at-least-one-valid-path-in-a-grid/discuss/526344/Python3-BFS | class Solution:
def minCost(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
direct = [(0, 1), (0, -1), (1, 0), (-1, 0)]
seen = set()
queue = [(0, 0)]
cost = 0
while queue: #breadth-first search
temp = set()
... | minimum-cost-to-make-at-least-one-valid-path-in-a-grid | [Python3] BFS | ye15 | 1 | 96 | minimum cost to make at least one valid path in a grid | 1,368 | 0.613 | Hard | 20,577 |
https://leetcode.com/problems/minimum-cost-to-make-at-least-one-valid-path-in-a-grid/discuss/2848360/Python3-BFS-with-PriorityQueue-soln | class Solution:
def minCost(self, grid: List[List[int]]) -> int:
dir=[0,[0,1],[0,-1],[1,0],[-1,0]]
n=len(grid)
m=len(grid[0])
q=[[0,0,0]]
visit=set()
while q:
val,i,j=heapq.heappop(q)
if i==n-1 and j==m-1:
... | minimum-cost-to-make-at-least-one-valid-path-in-a-grid | Python3 BFS with PriorityQueue soln | DhruvBagrecha | 0 | 1 | minimum cost to make at least one valid path in a grid | 1,368 | 0.613 | Hard | 20,578 |
https://leetcode.com/problems/minimum-cost-to-make-at-least-one-valid-path-in-a-grid/discuss/2831318/Python-Min-Heap%3A-12-time-28-space | class Solution:
def minCost(self, grid: List[List[int]]) -> int:
m = len(grid)
n = len(grid[0])
next_path = {
1: [0, 1],
2: [0, -1],
3: [1, 0],
4: [-1, 0],
}
heap = [(0, 0, 0)]
visited = set()
while(len(heap)):
... | minimum-cost-to-make-at-least-one-valid-path-in-a-grid | Python Min Heap: 12% time, 28% space | hqz3 | 0 | 2 | minimum cost to make at least one valid path in a grid | 1,368 | 0.613 | Hard | 20,579 |
https://leetcode.com/problems/minimum-cost-to-make-at-least-one-valid-path-in-a-grid/discuss/2222005/Python3-solution-Build-directed-weighted-graph-%2B-heap | class Solution:
def minCost(self, grid: List[List[int]]) -> int:
adj_list = collections.defaultdict(list)
m, n = len(grid), len(grid[0])
dirs = [(0, -1, 2), (0, 1, 1), (1, 0, 3), (-1, 0, 4)]
for i in range(m):
for j in range(n):
for d in dirs:
... | minimum-cost-to-make-at-least-one-valid-path-in-a-grid | Python3 solution - Build directed, weighted graph + heap | myvanillaexistence | 0 | 11 | minimum cost to make at least one valid path in a grid | 1,368 | 0.613 | Hard | 20,580 |
https://leetcode.com/problems/minimum-cost-to-make-at-least-one-valid-path-in-a-grid/discuss/526126/Python3-dfs-bfs-and-dijkstra | class Solution:
def dfs(self, grid: List[List[int]]) -> int:
# timeout
size_y = len(grid)
size_x = len(grid[0])
src = (0, 0)
des = (size_y - 1, size_x - 1)
stack = [(src, 0)]
costs = {
src: 0
}
while stack:
... | minimum-cost-to-make-at-least-one-valid-path-in-a-grid | [Python3] dfs, bfs and dijkstra | iameugenejo | 0 | 152 | minimum cost to make at least one valid path in a grid | 1,368 | 0.613 | Hard | 20,581 |
https://leetcode.com/problems/increasing-decreasing-string/discuss/543172/Python-3-Using-Set-and-Sort-with-commentary | class Solution:
def sortString(self, s: str) -> str:
s = list(s)
# Big S: O(n)
result = []
# Logic is capture distinct char with set
# Remove found char from initial string
# Big O: O(n)
while len(s) > 0:
# Big O: O(n log n) Spac... | increasing-decreasing-string | [Python 3] Using Set and Sort with commentary | dentedghost | 11 | 966 | increasing decreasing string | 1,370 | 0.774 | Easy | 20,582 |
https://leetcode.com/problems/increasing-decreasing-string/discuss/1073396/Python-3-Simple-Clean-code-beats-greater98-speed. | class Solution:
def sortString(self, s: str) -> str:
res = ''
counter = dict(collections.Counter(s))
chars = sorted(list(set(s)))
while(counter):
for char in chars:
if char in counter:
res += char
c... | increasing-decreasing-string | [Python 3] Simple Clean code, beats >98% speed. | rohitmujumdar | 5 | 516 | increasing decreasing string | 1,370 | 0.774 | Easy | 20,583 |
https://leetcode.com/problems/increasing-decreasing-string/discuss/2167833/PythonPython-3-hashmap-easy-understand-solution!!!-Beats-97 | class Solution(object):
def sortString(self, s):
dict = {}
for s1 in s:
dict[s1] = dict.get(s1, 0)+1
list1 = sorted(list(set(s)))
result = ''
while len(result) < len(s):
for l in list1:
if l in dict and dict[l] != 0:
... | increasing-decreasing-string | [Python/Python 3] hashmap easy-understand solution!!! Beats 97% | sophiel625 | 1 | 101 | increasing decreasing string | 1,370 | 0.774 | Easy | 20,584 |
https://leetcode.com/problems/increasing-decreasing-string/discuss/1481728/Python3-faster-than-95-Consistently-*Commented* | class Solution:
def sortString(self, s: str) -> str:
dict = {
'a' : 0, 'b' : 0, 'c' : 0, 'd' : 0, 'e' : 0, 'f' : 0,
'g' : 0, 'h' : 0, 'i' : 0, 'j' : 0, 'k' : 0, 'l' : 0,
'm' : 0, 'n' : 0, 'o' : 0, 'p' : 0, 'q' : 0, 'r' : 0,
's' : 0, 't' : 0, 'u' : 0, 'v' : 0, ... | increasing-decreasing-string | Python3 faster than 95% Consistently *Commented* | manuel_lemos | 1 | 200 | increasing decreasing string | 1,370 | 0.774 | Easy | 20,585 |
https://leetcode.com/problems/increasing-decreasing-string/discuss/1403851/WEEB-DOES-PYTHON | class Solution:
def sortString(self, s: str) -> str:
string, memo = sorted(set(s)), Counter(s)
result = ""
count = 0
while len(result) < len(s):
if count == 0:
for char in string:
if memo[char] == 0: continue
result += char
memo[char] -= 1
count += 1
if count == 1:
for char ... | increasing-decreasing-string | WEEB DOES PYTHON | Skywalker5423 | 1 | 158 | increasing decreasing string | 1,370 | 0.774 | Easy | 20,586 |
https://leetcode.com/problems/increasing-decreasing-string/discuss/2805741/Easy-to-understand-or-BEATS-90 | class Solution:
def sortString(self, s: str) -> str:
count = len(s)
freq = [0]*26 #conatins frequency(no. of occurance) - a-z
res=[]
#incrementing frequency of characters that are found in string ...kinda like dict / hashmap
for i in s:
freq[ord(i)-ord('a')] += 1... | increasing-decreasing-string | Easy to understand | BEATS 90% | Nick_23 | 0 | 6 | increasing decreasing string | 1,370 | 0.774 | Easy | 20,587 |
https://leetcode.com/problems/increasing-decreasing-string/discuss/2723284/Python3-Solution-with-using-counting | class Solution:
def sortString(self, s: str) -> str:
c = [0] * 26
cur_symb_cnt = len(s)
for char in s:
c[ord(char) - 97] += 1
res = []
while cur_symb_cnt > 0:
for i in range(len(c)):
if c[i] != 0:
... | increasing-decreasing-string | [Python3] Solution with using counting | maosipov11 | 0 | 7 | increasing decreasing string | 1,370 | 0.774 | Easy | 20,588 |
https://leetcode.com/problems/increasing-decreasing-string/discuss/2714240/Python3-KeySort-left-to-right | class Solution:
def sortString(self, s: str) -> str:
# count the elements and have an order
chars = [0]*26
for char in s:
# 97 == ord('a')
chars[ord(char) - 97] += 1
# end if we have no chars lef
leftover = len(s)
update = 1
... | increasing-decreasing-string | [Python3] - KeySort left to right | Lucew | 0 | 3 | increasing decreasing string | 1,370 | 0.774 | Easy | 20,589 |
https://leetcode.com/problems/increasing-decreasing-string/discuss/2587026/SIMPLE-PYTHON3-SOLUTION-using-hashmap | class Solution:
def sort_hm(self, s1,hm, keys):
for k in keys:
if hm[k] >0:
s1 += k
hm[k] -=1
return s1, hm
def sortString(self, s: str) -> str:
# Logic is based on counts
# Create hash map first
hm = {}
for i in s:... | increasing-decreasing-string | ✅✔ SIMPLE PYTHON3 SOLUTION ✅✔using hashmap | rajukommula | 0 | 37 | increasing decreasing string | 1,370 | 0.774 | Easy | 20,590 |
https://leetcode.com/problems/increasing-decreasing-string/discuss/2579652/solution-in-python3 | class Solution:
def sortString(self, s: str) -> str:
l=len(s)
res=''
f={}
for i in s:
if i in f:
f[i]+=1
else:
f[i]=1
s_k=sorted(f.keys())
r_k=list(reversed(s_k))
while l!=0:
for x in s_k:
... | increasing-decreasing-string | solution in python3 | RITIK_30454 | 0 | 39 | increasing decreasing string | 1,370 | 0.774 | Easy | 20,591 |
https://leetcode.com/problems/increasing-decreasing-string/discuss/2550616/Python3-sort-the-key-one-time-instead-of-the-string. | class Solution:
def sortString(self, s: str) -> str:
counter = collections.Counter(s)
sorted_keys = sorted(counter.keys())
result = ''
while sum(counter.values()):
for key in sorted_keys:
if counter[key]!=0:
result+=key
... | increasing-decreasing-string | [Python3] sort the key one time instead of the string. | JustinL | 0 | 33 | increasing decreasing string | 1,370 | 0.774 | Easy | 20,592 |
https://leetcode.com/problems/increasing-decreasing-string/discuss/2129242/python-3-oror-simple-hash-map-solution | class Solution:
def sortString(self, s: str) -> str:
count = collections.Counter(s)
res = []
chrs = [''] * 52
for i in range(26):
chrs[i] = chrs[~i] = chr(i + 97)
remaining = True
while remaining:
remaining = False
... | increasing-decreasing-string | python 3 || simple hash map solution | dereky4 | 0 | 133 | increasing decreasing string | 1,370 | 0.774 | Easy | 20,593 |
https://leetcode.com/problems/increasing-decreasing-string/discuss/2072763/python-solution-using-dictionary-and-sets | class Solution:
def sortString(self, s: str) -> str:
size = len(s)
d = {k: s.count(k) for k in s}
s = sorted(list(set(s)), key=ord)
res = ""
for i in range(size):
for c in s:
if d.get(c):
res += c
... | increasing-decreasing-string | python solution using dictionary and sets | andrewnerdimo | 0 | 83 | increasing decreasing string | 1,370 | 0.774 | Easy | 20,594 |
https://leetcode.com/problems/increasing-decreasing-string/discuss/2047646/Increasing-Decreasing-String | class Solution(object):
def sortString(self, s):
"""
:type s: str
:rtype: str
"""
data = sorted([c, n] for c, n in Counter(s).items())
result = []
while len(result) < len(s):
for i in range(len(data)):
if data[i][1]:
... | increasing-decreasing-string | Increasing Decreasing String | Muggles102 | 0 | 55 | increasing decreasing string | 1,370 | 0.774 | Easy | 20,595 |
https://leetcode.com/problems/increasing-decreasing-string/discuss/2010686/Python-easy-solution-faster-than-97 | class Solution:
def sortString(self, s: str) -> str:
freq = dict(Counter(s))
chars = sorted(list(set(s)))
res = ""
while freq:
for i in chars:
if i in freq:
res += i
freq[i] -= 1
if freq[i] == 0:
... | increasing-decreasing-string | Python easy solution faster than 97% | alishak1999 | 0 | 116 | increasing decreasing string | 1,370 | 0.774 | Easy | 20,596 |
https://leetcode.com/problems/increasing-decreasing-string/discuss/1980562/Python-(Simple-Approach-and-Beginner-Friendly) | class Solution:
def sortString(self, s: str) -> str:
dict = {}
alphabets = "abcdefghijklmnopqrstuvwxyz"
output = ""
for i in s:
if i not in dict:
dict[i] = 1
else:
dict[i]+=1
while len(output) < len(s):
for i... | increasing-decreasing-string | Python (Simple Approach and Beginner-Friendly) | vishvavariya | 0 | 90 | increasing decreasing string | 1,370 | 0.774 | Easy | 20,597 |
https://leetcode.com/problems/increasing-decreasing-string/discuss/1851544/5-Lines-Python-Solution-oror-93-Faster-oror-Memory-less-than-63 | class Solution:
def sortString(self, s: str) -> str:
C=sorted(Counter(s).items()) ; ans=''
while len(C)>0:
ans+=''.join([x for x,y in C]) ; C=[(x,y-1) for x,y in C if y>1]
ans+=''.join([x for x,y in C])[::-1] ; C=[(x,y-1) for x,y in C if y>1]
return ans | increasing-decreasing-string | 5-Lines Python Solution || 93% Faster || Memory less than 63% | Taha-C | 0 | 101 | increasing decreasing string | 1,370 | 0.774 | Easy | 20,598 |
https://leetcode.com/problems/increasing-decreasing-string/discuss/1851544/5-Lines-Python-Solution-oror-93-Faster-oror-Memory-less-than-63 | class Solution:
def sortString(self, s: str) -> str:
s=list(s) ; ans=[]
while len(s)>0:
smallest=sorted(set(s))
for small in smallest: ans.append(small) ; s.remove(small)
largest=sorted(set(s),reverse=True)
for large in largest: ans.append(large) ; s.r... | increasing-decreasing-string | 5-Lines Python Solution || 93% Faster || Memory less than 63% | Taha-C | 0 | 101 | increasing decreasing string | 1,370 | 0.774 | Easy | 20,599 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.