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/convert-the-temperature/discuss/2815504/python-solution-no-need-for-10*-5 | class Solution:
def convertTemperature(self, celsius: float) -> List[float]:
kelvin = celsius + 273.15
fahrenheit = (celsius * 1.80) + 32.00
return [kelvin, fahrenheit] | convert-the-temperature | python solution no need for 10*-5 | samanehghafouri | 0 | 2 | convert the temperature | 2,469 | 0.909 | Easy | 33,900 |
https://leetcode.com/problems/convert-the-temperature/discuss/2815390/Fastest-and-Simplest-Solution-Python-(One-Liner-Code) | class Solution:
def convertTemperature(self, celsius: float) -> List[float]:
return [celsius + 273.15, celsius * 1.8 + 32.0] | convert-the-temperature | Fastest and Simplest Solution - Python (One-Liner Code) | PranavBhatt | 0 | 2 | convert the temperature | 2,469 | 0.909 | Easy | 33,901 |
https://leetcode.com/problems/convert-the-temperature/discuss/2815231/Hardest-Problem-EVER! | class Solution:
def convertTemperature(self, celsius: float) -> List[float]:
return [celsius + 273.15, celsius * 1.80 + 32.00] | convert-the-temperature | Hardest Problem EVER! | elbasrouisama | 0 | 6 | convert the temperature | 2,469 | 0.909 | Easy | 33,902 |
https://leetcode.com/problems/convert-the-temperature/discuss/2814881/Python3 | class Solution:
def convertTemperature(self, celsius: float) -> List[float]:
Kelvin = celsius + 273.15
Fahrenheit = celsius * 1.80 + 32
return [Kelvin, Fahrenheit] | convert-the-temperature | Python3 | anashaupt | 0 | 2 | convert the temperature | 2,469 | 0.909 | Easy | 33,903 |
https://leetcode.com/problems/convert-the-temperature/discuss/2814833/Beats-100-Python | class Solution:
def convertTemperature(self, celsius: float) -> List[float]:
ans=[]
Kelvin=celsius + 273.15
Fahrenheit=celsius * 1.80 + 32.00
ans.append(Kelvin)
ans.append(Fahrenheit)
return ans | convert-the-temperature | Beats 100%, Python | prakritig2 | 0 | 2 | convert the temperature | 2,469 | 0.909 | Easy | 33,904 |
https://leetcode.com/problems/convert-the-temperature/discuss/2814701/Python-Simple-Python-Solution | class Solution:
def convertTemperature(self, celsius: float) -> List[float]:
Kelvin = celsius + 273.15
Fahrenheit = celsius * 1.80 + 32.00
return [Kelvin,Fahrenheit] | convert-the-temperature | [ Python ] ✅✅ Simple Python Solution🥳✌👍 | ASHOK_KUMAR_MEGHVANSHI | 0 | 6 | convert the temperature | 2,469 | 0.909 | Easy | 33,905 |
https://leetcode.com/problems/convert-the-temperature/discuss/2814432/Super-easy-and-fast-Python3-Answer | class Solution:
def convertTemperature(self, c: float) -> List[float]:
return [c + 273.15, c*1.80+32.00] | convert-the-temperature | Super easy and fast Python3 Answer | hyamtx | 0 | 1 | convert the temperature | 2,469 | 0.909 | Easy | 33,906 |
https://leetcode.com/problems/convert-the-temperature/discuss/2813960/One-line-100-speed | class Solution:
def convertTemperature(self, celsius: float) -> List[float]:
return [celsius + 273.15, celsius * 1.80 + 32.00] | convert-the-temperature | One line, 100% speed | EvgenySH | 0 | 5 | convert the temperature | 2,469 | 0.909 | Easy | 33,907 |
https://leetcode.com/problems/convert-the-temperature/discuss/2810375/Python-Solution-or-One-Liner-or-100-Faster | class Solution:
def convertTemperature(self, celsius: float) -> List[float]:
return [
celsius + 273.15,
(celsius * 1.80) + 32.00
] | convert-the-temperature | Python Solution | One Liner | 100% Faster | Gautam_ProMax | 0 | 12 | convert the temperature | 2,469 | 0.909 | Easy | 33,908 |
https://leetcode.com/problems/convert-the-temperature/discuss/2810108/Python-Solution | class Solution:
def convertTemperature(self, celsius: float) -> List[float]:
return [celsius+273.15,celsius*1.80+32.00] | convert-the-temperature | Python Solution | CEOSRICHARAN | 0 | 3 | convert the temperature | 2,469 | 0.909 | Easy | 33,909 |
https://leetcode.com/problems/convert-the-temperature/discuss/2809679/Easiest-Python-Solution-oror-Full-Explanation | class Solution:
def convertTemperature(self, celsius: float) -> List[float]:
return [celsius+273.15,celsius*1.80+32.00] | convert-the-temperature | ✔💖Easiest Python Solution || Full Explanation | AdityaTrivedi88 | 0 | 4 | convert the temperature | 2,469 | 0.909 | Easy | 33,910 |
https://leetcode.com/problems/convert-the-temperature/discuss/2809644/Python-or-Easy-or-Faster-than-100 | class Solution:
def convertTemperature(self, celsius: float) -> List[float]:
return [celsius + 273.15, (celsius * 1.80) + 32.00] | convert-the-temperature | Python | Easy | Faster than 100% | pawangupta | 0 | 6 | convert the temperature | 2,469 | 0.909 | Easy | 33,911 |
https://leetcode.com/problems/convert-the-temperature/discuss/2809252/Easy-Python-Solution | class Solution:
def convertTemperature(self, celsius: float) -> List[float]:
return [celsius+ 273.15, (celsius * 1.80) + 32] | convert-the-temperature | Easy Python Solution | imash_9 | 0 | 4 | convert the temperature | 2,469 | 0.909 | Easy | 33,912 |
https://leetcode.com/problems/convert-the-temperature/discuss/2809215/Python3JavaScript-Self-Explanatory | class Solution:
def convertTemperature(self, celsius: float) -> List[float]:
kelvin = celsius + 273.15
fahrenheit = celsius * 1.80 + 32.00
return [kelvin, fahrenheit] | convert-the-temperature | ✅[Python3/JavaScript] Self Explanatory | MusfiqDehan | 0 | 10 | convert the temperature | 2,469 | 0.909 | Easy | 33,913 |
https://leetcode.com/problems/convert-the-temperature/discuss/2809047/Easy-python-beats-100.00-just-convert | class Solution:
def convertTemperature(self, c):
return [c+273.15,c*1.8+32] | convert-the-temperature | Easy python beats 100.00% [just convert] | U753L | 0 | 9 | convert the temperature | 2,469 | 0.909 | Easy | 33,914 |
https://leetcode.com/problems/convert-the-temperature/discuss/2808910/One-liner-Python3-O(1)-Solution-Easiest-to-understand | class Solution:
def convertTemperature(self, celsius: float) -> List[float]:
return [(celsius + 273.15), (celsius * 1.80 + 32.00)] | convert-the-temperature | One liner Python3 O(1) Solution Easiest to understand | jubinsoni | 0 | 8 | convert the temperature | 2,469 | 0.909 | Easy | 33,915 |
https://leetcode.com/problems/convert-the-temperature/discuss/2808873/Python-1-liner-simple-solution | class Solution:
def convertTemperature(self, celsius: float) -> List[float]:
return [celsius + 273.15, celsius * 1.80 + 32.00] | convert-the-temperature | ✅ Python 1 liner simple solution | sezanhaque | 0 | 8 | convert the temperature | 2,469 | 0.909 | Easy | 33,916 |
https://leetcode.com/problems/convert-the-temperature/discuss/2808832/return-celsius-%2B-273.15-celsius-*-1.80-%2B-32.00 | class Solution:
def convertTemperature(self, celsius: float) -> List[float]:
return celsius + 273.15, celsius * 1.80 + 32.00 | convert-the-temperature | return celsius + 273.15, celsius * 1.80 + 32.00 | Silvia42 | 0 | 14 | convert the temperature | 2,469 | 0.909 | Easy | 33,917 |
https://leetcode.com/problems/convert-the-temperature/discuss/2808794/python-solution | class Solution:
def convertTemperature(self, celsius: float) -> List[float]:
return [celsius + 273.15, celsius * 1.80 + 32.00] | convert-the-temperature | python solution | akaghosting | 0 | 27 | convert the temperature | 2,469 | 0.909 | Easy | 33,918 |
https://leetcode.com/problems/number-of-subarrays-with-lcm-equal-to-k/discuss/2808943/Beginners-Approach-%3A | class Solution:
def subarrayLCM(self, nums: List[int], k: int) -> int:
def find_lcm(num1, num2):
if(num1>num2):
num = num1
den = num2
else:
num = num2
den = num1
rem = num % den
while(rem != 0):
... | number-of-subarrays-with-lcm-equal-to-k | Beginners Approach : | goxy_coder | 1 | 57 | number of subarrays with lcm equal to k | 2,470 | 0.388 | Medium | 33,919 |
https://leetcode.com/problems/number-of-subarrays-with-lcm-equal-to-k/discuss/2835262/Python-(Simple-Maths) | class Solution:
def subarrayLCM(self, nums, k):
n, count = len(nums), 0
for i in range(n):
temp = nums[i]
for j in range(i,n):
temp = math.lcm(temp,nums[j])
if temp == k:
count += 1
elif temp > k:
... | number-of-subarrays-with-lcm-equal-to-k | Python (Simple Maths) | rnotappl | 0 | 5 | number of subarrays with lcm equal to k | 2,470 | 0.388 | Medium | 33,920 |
https://leetcode.com/problems/number-of-subarrays-with-lcm-equal-to-k/discuss/2828569/More-Efficient-GCD-based-Solution-The-Best | class Solution:
def subarrayLCM(self, nums: list[int], k: int) -> int:
coprime_count = 0
for subarray_end, n in enumerate(nums):
nums[subarray_end] = k // n if k % n == 0 else None
running_gcd = 0
for subarray_start in reversed(range(subarray_end + 1)):
... | number-of-subarrays-with-lcm-equal-to-k | More Efficient GCD-based Solution, The Best | Triquetra | 0 | 19 | number of subarrays with lcm equal to k | 2,470 | 0.388 | Medium | 33,921 |
https://leetcode.com/problems/number-of-subarrays-with-lcm-equal-to-k/discuss/2824249/Python-A-DP-approach | class Solution:
def subarrayLCM(self, nums: List[int], k: int) -> int:
n = len(nums)
dp = [[0] * n for _ in range(n)]
result = 0
for i in range(n-1,-1,-1):
for j in range(i, n):
temp = math.lcm(nums[i], nums[j])
... | number-of-subarrays-with-lcm-equal-to-k | [Python] A DP approach | hengsoo | 0 | 7 | number of subarrays with lcm equal to k | 2,470 | 0.388 | Medium | 33,922 |
https://leetcode.com/problems/number-of-subarrays-with-lcm-equal-to-k/discuss/2809141/python-solution | class Solution:
def subarrayLCM(self, nums: List[int], k: int) -> int:
res = 0
for i in range(len(nums)):
curLcm = nums[i]
if nums[i] <= k:
for j in range(i, len(nums)):
if nums[j] <= k:
curLcm = lcm(curLcm, nums[j])... | number-of-subarrays-with-lcm-equal-to-k | python solution | akaghosting | 0 | 19 | number of subarrays with lcm equal to k | 2,470 | 0.388 | Medium | 33,923 |
https://leetcode.com/problems/number-of-subarrays-with-lcm-equal-to-k/discuss/2809139/Python-Answer-Simple-Two-Loops-(start-and-end-index) | class Solution:
def subarrayLCM(self, nums: List[int], k: int) -> int:
res = 0
for i in range(len(nums)):
l = nums[i]
if l == k:
res+=1
for j in range(i+1,len(nums)):
... | number-of-subarrays-with-lcm-equal-to-k | [Python Answer🤫🐍🐍🐍] Simple Two Loops (start and end index) | xmky | 0 | 16 | number of subarrays with lcm equal to k | 2,470 | 0.388 | Medium | 33,924 |
https://leetcode.com/problems/number-of-subarrays-with-lcm-equal-to-k/discuss/2809111/Python3-running-LCM-beats-100.00 | class Solution:
def subarrayLCM(self, nums, k):
cnt=0 # number of working subsets
for i in range(len(nums)): # left bound
runningLCM = nums[i]
for j in range(i, len(nums)):
runningLCM = lcm(runningLCM, nums[j]) # generate new gcd of given subset
... | number-of-subarrays-with-lcm-equal-to-k | [Python3] running LCM, beats 100.00% | U753L | 0 | 17 | number of subarrays with lcm equal to k | 2,470 | 0.388 | Medium | 33,925 |
https://leetcode.com/problems/number-of-subarrays-with-lcm-equal-to-k/discuss/2808985/Python3-brute-force | class Solution:
def subarrayLCM(self, nums: List[int], k: int) -> int:
ans = 0
for i in range(len(nums)):
val = 1
for j in range(i, len(nums)):
val = lcm(val, nums[j])
if val == k: ans += 1
return ans | number-of-subarrays-with-lcm-equal-to-k | [Python3] brute-force | ye15 | 0 | 15 | number of subarrays with lcm equal to k | 2,470 | 0.388 | Medium | 33,926 |
https://leetcode.com/problems/number-of-subarrays-with-lcm-equal-to-k/discuss/2808978/Python-Simple-Solution | class Solution:
def subarrayLCM(self, nums: List[int], k: int) -> int:
res = 0
for left in range(len(nums)):
curr = nums[left]
right = left
while right < len(nums) and k % nums[right] == 0:
curr = math.lcm(curr, nums[right])
if cu... | number-of-subarrays-with-lcm-equal-to-k | ✅ Python Simple Solution | sezanhaque | 0 | 56 | number of subarrays with lcm equal to k | 2,470 | 0.388 | Medium | 33,927 |
https://leetcode.com/problems/number-of-subarrays-with-lcm-equal-to-k/discuss/2808921/Python-Brute-Force | class Solution:
def subarrayLCM(self, nums: List[int], k: int) -> int:
n = len(nums)
ans = 0
for i in range(n):
lcm = nums[i]
for j in range(i, n):
lcm = lcm * nums[j] // math.gcd(lcm, nums[j])
if lcm == k: ans += 1
elif lcm >k: ... | number-of-subarrays-with-lcm-equal-to-k | Python Brute Force | rbhandu | 0 | 17 | number of subarrays with lcm equal to k | 2,470 | 0.388 | Medium | 33,928 |
https://leetcode.com/problems/number-of-subarrays-with-lcm-equal-to-k/discuss/2808840/Optimized-Bruteforce-or-Short-and-simple-to-understand-or-with-explanation | class Solution:
def subarrayLCM(self, nums: List[int], k: int) -> int:
num_subarray = 0
n = len(nums)
for i in range(n):
lcm = nums[i]
for j in range(i, n):
lcm = math.lcm(lcm, nums[j])
if lcm > k:
break
... | number-of-subarrays-with-lcm-equal-to-k | Optimized Bruteforce | Short and simple to understand | with explanation | jubinsoni | 0 | 40 | number of subarrays with lcm equal to k | 2,470 | 0.388 | Medium | 33,929 |
https://leetcode.com/problems/minimum-number-of-operations-to-sort-a-binary-tree-by-level/discuss/2808960/Python3-BFS | class Solution:
def minimumOperations(self, root: Optional[TreeNode]) -> int:
ans = 0
queue = deque([root])
while queue:
vals = []
for _ in range(len(queue)):
node = queue.popleft()
vals.append(node.val)
if node.left:... | minimum-number-of-operations-to-sort-a-binary-tree-by-level | [Python3] BFS | ye15 | 6 | 567 | minimum number of operations to sort a binary tree by level | 2,471 | 0.629 | Medium | 33,930 |
https://leetcode.com/problems/minimum-number-of-operations-to-sort-a-binary-tree-by-level/discuss/2808987/Easy-Level-order-traversal-with-comments-oror-Python | class Solution:
def cntSwap(self,arr):
n = len(arr)
cnt = 0
a = sorted(arr)
index = {}
# store index of elements in arr
for ind in range(n):
index[arr[ind]] = ind
for ind in range(n):
# If element in arr is out of position we ne... | minimum-number-of-operations-to-sort-a-binary-tree-by-level | Easy Level order traversal with comments || Python | CoderIsCodin | 4 | 203 | minimum number of operations to sort a binary tree by level | 2,471 | 0.629 | Medium | 33,931 |
https://leetcode.com/problems/minimum-number-of-operations-to-sort-a-binary-tree-by-level/discuss/2836036/Python-(Simple-DFS-%2BSorting) | class Solution:
def func_(self,nums):
dict2 = {n:j for j,n in enumerate(sorted(nums))}
count, i = 0, 0
while i < len(nums):
j = dict2[nums[i]]
if i != j:
nums[i], nums[j] = nums[j], nums[i]
count += 1
else:
... | minimum-number-of-operations-to-sort-a-binary-tree-by-level | Python (Simple DFS +Sorting) | rnotappl | 0 | 5 | minimum number of operations to sort a binary tree by level | 2,471 | 0.629 | Medium | 33,932 |
https://leetcode.com/problems/minimum-number-of-operations-to-sort-a-binary-tree-by-level/discuss/2831314/Optimal-and-Without-Actually-Swapping-Values-The-Best | class Solution:
def minimumOperations(self, root: Optional[TreeNode]) -> int:
return sum([self.minimum_swaps(level) for level in self.bfs_levels(root)])
def bfs_levels(self, root: Optional[TreeNode]) -> list[list[int]]:
nodes, levels, index = [root], [0], 0
while index != len(nodes):
... | minimum-number-of-operations-to-sort-a-binary-tree-by-level | Optimal and Without Actually Swapping Values, The Best | Triquetra | 0 | 5 | minimum number of operations to sort a binary tree by level | 2,471 | 0.629 | Medium | 33,933 |
https://leetcode.com/problems/minimum-number-of-operations-to-sort-a-binary-tree-by-level/discuss/2823256/Python-solution | class Solution:
def minimumOperations(self, root: Optional[TreeNode]) -> int:
levels = []
res = 0
def levelorder(node, level):
if level >= len(levels):
levels.append([])
if node:
levels[level].append(node.val)
... | minimum-number-of-operations-to-sort-a-binary-tree-by-level | Python solution | maomao1010 | 0 | 8 | minimum number of operations to sort a binary tree by level | 2,471 | 0.629 | Medium | 33,934 |
https://leetcode.com/problems/minimum-number-of-operations-to-sort-a-binary-tree-by-level/discuss/2812540/Python3-Level-BFS-and-manipulate-vals-index | class Solution:
def minimumOperations(self, root: Optional[TreeNode]) -> int:
cur, ans = [root], 0
while cur:
nxt, nums, numidx = [], [], dict()
for node in cur:
numidx[node.val] = len(nums)
nums.append(node.val)
if node.left: n... | minimum-number-of-operations-to-sort-a-binary-tree-by-level | [Python3] Level BFS and manipulate vals index | cava | 0 | 4 | minimum number of operations to sort a binary tree by level | 2,471 | 0.629 | Medium | 33,935 |
https://leetcode.com/problems/minimum-number-of-operations-to-sort-a-binary-tree-by-level/discuss/2809954/Python-BFS-%2B-count-swaps-required-simple-solution | class Solution:
def countswaps(self, arr):
count = 0
temp = sorted(arr)
h = {}
for i in range(len(arr)):
h[arr[i]] = i
for i in range(len(arr)):
if arr[i] != temp[i]:
count += 1
# swap ... | minimum-number-of-operations-to-sort-a-binary-tree-by-level | Python BFS + count swaps required simple solution | ishaan06 | 0 | 19 | minimum number of operations to sort a binary tree by level | 2,471 | 0.629 | Medium | 33,936 |
https://leetcode.com/problems/maximum-number-of-non-overlapping-palindrome-substrings/discuss/2808845/Python3-DP-with-Explanations-or-Only-Check-Substrings-of-Length-k-and-k-%2B-1 | class Solution:
def maxPalindromes(self, s: str, k: int) -> int:
n = len(s)
dp = [0] * (n + 1)
for i in range(k, n + 1):
dp[i] = dp[i - 1]
for length in range(k, k + 2):
j = i - length
if j < 0:
break
... | maximum-number-of-non-overlapping-palindrome-substrings | [Python3] DP with Explanations | Only Check Substrings of Length k and k + 1 | xil899 | 11 | 542 | maximum number of non overlapping palindrome substrings | 2,472 | 0.366 | Hard | 33,937 |
https://leetcode.com/problems/maximum-number-of-non-overlapping-palindrome-substrings/discuss/2832974/Python3-or-Memoization | class Solution:
def maxPalindromes(self, s: str, k: int) -> int:
n=len(s)
def checkIfPalindrome(i,j):
while i<j:
if s[i]!=s[j]:
return False
i+=1
j-=1
return True
@lru_cache(2000)
def dp(i,j):... | maximum-number-of-non-overlapping-palindrome-substrings | Python3 | Memoization | Dr_Negative | 1 | 9 | maximum number of non overlapping palindrome substrings | 2,472 | 0.366 | Hard | 33,938 |
https://leetcode.com/problems/maximum-number-of-non-overlapping-palindrome-substrings/discuss/2814448/Simplest-Greedy-Python-Solution-or-O(1)-Space-or-O(n*k)-Time | class Solution:
def maxPalindromes(self, s: str, k: int) -> int:
n = len(s)
# function to check whether substring is a palindrome
def valid(i, j):
# if end index is greater then length of string
if j > len(s):
return False
if s[i : j] ... | maximum-number-of-non-overlapping-palindrome-substrings | Simplest Greedy Python Solution | O(1) Space | O(n*k) Time | deepaklaksman | 1 | 18 | maximum number of non overlapping palindrome substrings | 2,472 | 0.366 | Hard | 33,939 |
https://leetcode.com/problems/maximum-number-of-non-overlapping-palindrome-substrings/discuss/2811830/Python-Very-simple-solution-O(n)-oror-no-DP | class Solution:
def maxPalindromes(self, s: str, k: int) -> int:
res = 0
lastp = -1
def helper(l, r):
nonlocal lastp, res
while l >= 0 and r < len(s) and s[l] == s[r] and l > lastp:
if r-l+1 >= k:
res += 1
lastp... | maximum-number-of-non-overlapping-palindrome-substrings | ✅ [Python] Very simple solution - O(n) || no DP | Meerka_ | 1 | 22 | maximum number of non overlapping palindrome substrings | 2,472 | 0.366 | Hard | 33,940 |
https://leetcode.com/problems/maximum-number-of-non-overlapping-palindrome-substrings/discuss/2809266/Python3-greedy-only-check-length-k-and-k-%2B-1 | class Solution:
def maxPalindromes(self, s: str, k: int) -> int:
ans = 0
l = 0
while l<len(s):
cdd1 = s[l:l+k]
if len(cdd1)>=k and cdd1 == cdd1[::-1]:
ans += 1
l = l+k
continue
cdd2 = s[l:l+k+1]
i... | maximum-number-of-non-overlapping-palindrome-substrings | [Python3] greedy - only check length k and k + 1 | nxiayu | 1 | 39 | maximum number of non overlapping palindrome substrings | 2,472 | 0.366 | Hard | 33,941 |
https://leetcode.com/problems/maximum-number-of-non-overlapping-palindrome-substrings/discuss/2816429/Python-Two-DP-O(NK) | class Solution:
def maxPalindromes(self, s: str, k: int) -> int:
n = len(s)
if k == 1: return n
ispalindromes, palidxs = [[True] * n for _ in range(n)], [[] for _ in range(n + 1)]
for step in range(1, k + 2):
for i in range(n - step):
ispalindromes[i][i + ... | maximum-number-of-non-overlapping-palindrome-substrings | [Python] Two DP, O(NK) | cava | 0 | 6 | maximum number of non overlapping palindrome substrings | 2,472 | 0.366 | Hard | 33,942 |
https://leetcode.com/problems/maximum-number-of-non-overlapping-palindrome-substrings/discuss/2814368/Python3-or-Memoization | class Solution:
def maxPalindromes(self, s: str, k: int) -> int:
@cache
def dp(ind):
if ind >= n-k+1:
return 0
val = 0
for i in range(k,k+2):
if s[ind:ind+i] == s[ind:ind+i][::-1]:
val = max(val,1 + dp(ind+i))
... | maximum-number-of-non-overlapping-palindrome-substrings | [Python3] | Memoization | swapnilsingh421 | 0 | 11 | maximum number of non overlapping palindrome substrings | 2,472 | 0.366 | Hard | 33,943 |
https://leetcode.com/problems/maximum-number-of-non-overlapping-palindrome-substrings/discuss/2811654/Non-Overlapping-Intervals-or-Greedy-or-Python3 | class Solution:
def maxPalindromes(self, s: str, k: int) -> int:
intervals = []
n = len(s)
for i in range(n):
j = 0
while i - j > -1 and i + j < n and s[i - j] == s[i + j]:
st, e = i - j, i + j
if e - st + 1 >= k:
in... | maximum-number-of-non-overlapping-palindrome-substrings | Non Overlapping Intervals | Greedy | Python3 | DheerajGadwala | 0 | 15 | maximum number of non overlapping palindrome substrings | 2,472 | 0.366 | Hard | 33,944 |
https://leetcode.com/problems/maximum-number-of-non-overlapping-palindrome-substrings/discuss/2811186/Python-3Greedy-cutting | class Solution:
def maxPalindromes(self, s: str, k: int) -> int:
n = len(s)
if k == 1: return n
# palindrome length with center at x without overalpping with previous string end at index "prev"
def helper(x, prev):
i, j = x - 1, x + 1
while i > prev a... | maximum-number-of-non-overlapping-palindrome-substrings | [Python 3]Greedy cutting | chestnut890123 | 0 | 15 | maximum number of non overlapping palindrome substrings | 2,472 | 0.366 | Hard | 33,945 |
https://leetcode.com/problems/maximum-number-of-non-overlapping-palindrome-substrings/discuss/2810522/Python-O(nk)-without-DP-or-recursion.-Simple-iterative-solution-with-explanation | class Solution:
def checkPalindromeAndReturnShortestGreaterThanK(self, s, i, j, beg, k):
while i > beg and j < len(s) and s[i]==s[j]:
if j-i-1 >= k:
return j-1
i-=1
j+=1
if j-i-1 >= k:
return j-1
return -1
... | maximum-number-of-non-overlapping-palindrome-substrings | Python O(nk) without DP or recursion. Simple iterative solution with explanation | AkshayDagar | 0 | 3 | maximum number of non overlapping palindrome substrings | 2,472 | 0.366 | Hard | 33,946 |
https://leetcode.com/problems/maximum-number-of-non-overlapping-palindrome-substrings/discuss/2809409/Python-Time-O(NK)-or-Space-O(1) | class Solution:
def maxPalindromes(self, s: str, k: int) -> int:
def expand(i, j):
# j - i <= k means we have to expand only until
# len(palindrome) >= k, and len(palindrome) is as small as possible
while i > left_boundary and j < len(s) and j - i <= k and s[i] == s[j]:
... | maximum-number-of-non-overlapping-palindrome-substrings | [Python] Time O(NK) | Space O(1) | miguel_v | 0 | 11 | maximum number of non overlapping palindrome substrings | 2,472 | 0.366 | Hard | 33,947 |
https://leetcode.com/problems/maximum-number-of-non-overlapping-palindrome-substrings/discuss/2809306/Python-solution-with-explanation | class Solution:
def maxPalindromes(self, s: str, k: int) -> int:
def getPalindrome(idx, single):
i, j = idx - 1, idx + 1
length = 1
if not single:
length += 1
j += 1
while i >= 0 and j < len(s) and ... | maximum-number-of-non-overlapping-palindrome-substrings | Python solution with explanation | nawrazi | 0 | 17 | maximum number of non overlapping palindrome substrings | 2,472 | 0.366 | Hard | 33,948 |
https://leetcode.com/problems/maximum-number-of-non-overlapping-palindrome-substrings/discuss/2809175/Python3-DP%2BGreedy | class Solution:
def maxPalindromes(self, s: str, k: int) -> int:
n = len(s)
palEnds = -1 # previous palindrome ending pos
res = 0
for i in range(n):
for j in range(i + k - 1, n):
if i <= palEnds and j >= palEnds:
break
... | maximum-number-of-non-overlapping-palindrome-substrings | Python3 DP+Greedy | fengqifeng | 0 | 10 | maximum number of non overlapping palindrome substrings | 2,472 | 0.366 | Hard | 33,949 |
https://leetcode.com/problems/maximum-number-of-non-overlapping-palindrome-substrings/discuss/2809175/Python3-DP%2BGreedy | class Solution:
def maxPalindromes(self, s: str, k: int) -> int:
def isPalindrome(i: int, j: int) -> bool:
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
n, i, res = len(s), 0, 0
... | maximum-number-of-non-overlapping-palindrome-substrings | Python3 DP+Greedy | fengqifeng | 0 | 10 | maximum number of non overlapping palindrome substrings | 2,472 | 0.366 | Hard | 33,950 |
https://leetcode.com/problems/maximum-number-of-non-overlapping-palindrome-substrings/discuss/2809114/Python-Answer-Until-K-%2B-2-FAST-Beats-75 | class Solution:
def maxPalindromes(self, s: str, k: int) -> int:
@cache
def checkPall(ss):
return ss == ss[::-1]
@cache
def checkPal(i,j):
ss = s[i:j]
return checkPall(ss)
dp = [0] * (len(s) + 1)
for i in range(len(s... | maximum-number-of-non-overlapping-palindrome-substrings | [Python Answer🤫🐍🐍🐍] Until K + 2 - FAST Beats 75% | xmky | 0 | 7 | maximum number of non overlapping palindrome substrings | 2,472 | 0.366 | Hard | 33,951 |
https://leetcode.com/problems/maximum-number-of-non-overlapping-palindrome-substrings/discuss/2809071/Python-Greedy-Approach-O(nk)O(1) | class Solution:
def find_first_palindrome(self, string, i, k):
N = len(string)
L = N - i
M = L + L - 1
for j in range(M):
if j % 2 == 0:
ll = rr = i + j // 2
else:
ll = i + j // 2
rr = ll + 1
while ll... | maximum-number-of-non-overlapping-palindrome-substrings | [Python] Greedy Approach O(nk)/O(1) | ryanmeow | 0 | 11 | maximum number of non overlapping palindrome substrings | 2,472 | 0.366 | Hard | 33,952 |
https://leetcode.com/problems/maximum-number-of-non-overlapping-palindrome-substrings/discuss/2809042/Very-easy-and-short-O(n)-solution-or-python | class Solution:
def maxPalindromes(self, s: str, k: int) -> int:
n = len(s)
if k == 1:
return n
ans = 0
i = 0
while i + k <= n:
if s[i:i + k] == s[i:i + k][::-1]:
ans += 1
i = i + k
elif s[i:i + k + ... | maximum-number-of-non-overlapping-palindrome-substrings | Very easy and short O(n) solution | python | jackie890621 | 0 | 19 | maximum number of non overlapping palindrome substrings | 2,472 | 0.366 | Hard | 33,953 |
https://leetcode.com/problems/maximum-number-of-non-overlapping-palindrome-substrings/discuss/2809025/Enumerate-and-greedy-O(n*k) | class Solution:
def maxPalindromes(self, s: str, k: int) -> int:
n = len(s)
self.ans = 0
self.start = 0
i = 0
while i < n:
r1 = self.is_pal(i, i, s, k)
r2 = self.is_pal(i, i+1, s, k)
if r1 < n or r2 < n:
i = min(r1, r2) + 1
... | maximum-number-of-non-overlapping-palindrome-substrings | Enumerate and greedy, O(n*k) | goodgoodwish | 0 | 26 | maximum number of non overlapping palindrome substrings | 2,472 | 0.366 | Hard | 33,954 |
https://leetcode.com/problems/maximum-number-of-non-overlapping-palindrome-substrings/discuss/2808975/Python3-dp | class Solution:
def maxPalindromes(self, s: str, k: int) -> int:
n = len(s)
vals = [1]*n
for i in range(2*n-1):
lo, hi = i//2, (i+1)//2
while 0 <= lo <= hi < n and s[lo] == s[hi]:
if vals[lo] < k: vals[lo] = max(vals[lo], hi-lo+1)
lo ... | maximum-number-of-non-overlapping-palindrome-substrings | [Python3] dp | ye15 | 0 | 21 | maximum number of non overlapping palindrome substrings | 2,472 | 0.366 | Hard | 33,955 |
https://leetcode.com/problems/number-of-unequal-triplets-in-array/discuss/2834169/Python-Hashmap-O(n)-with-diagrams | class Solution:
def unequalTriplets(self, nums: List[int]) -> int:
c = Counter(nums)
res = 0
left = 0
right = len(nums)
for _, freq in c.items():
right -= freq
res += left * freq * right
left += freq
retur... | number-of-unequal-triplets-in-array | Python Hashmap O(n) with diagrams | cheatcode-ninja | 10 | 242 | number of unequal triplets in array | 2,475 | 0.699 | Easy | 33,956 |
https://leetcode.com/problems/number-of-unequal-triplets-in-array/discuss/2832078/PythonC%2B%2BJavaScript-O(n)-beats-100 | class Solution:
def unequalTriplets(self, nums: List[int]) -> int:
count = 0
prev, nxt = 0, len(nums)
for _, frequency in Counter(nums).items():
nxt -= frequency
count += prev * frequency * nxt
prev += frequency
return count | number-of-unequal-triplets-in-array | ✅ [Python/C++/JavaScript] O(n) beats 100% | sezanhaque | 8 | 533 | number of unequal triplets in array | 2,475 | 0.699 | Easy | 33,957 |
https://leetcode.com/problems/number-of-unequal-triplets-in-array/discuss/2831993/Easy-Brute-Force-solution-or-Python-or-TC-O(n3) | class Solution:
def unequalTriplets(self, nums: List[int]) -> int:
c = 0
for i in range(len(nums)):
for j in range(i+1, len(nums)):
for k in range(j+1, len(nums)):
if nums[i]!=nums[j] and nums[i]!=nums[k] and nums[j] != nums[k]:
... | number-of-unequal-triplets-in-array | Easy Brute Force solution | Python | TC = O(n^3) | jayvardhanrathore111 | 2 | 118 | number of unequal triplets in array | 2,475 | 0.699 | Easy | 33,958 |
https://leetcode.com/problems/number-of-unequal-triplets-in-array/discuss/2834376/Python-One-Pass-O(n)-Count-invalid-triplets | class Solution:
def unequalTriplets(self, nums: List[int]) -> int:
from collections import Counter
num_count = Counter()
n_invalid_pairs = 0
n_invalid_triplets = 0
for (i, num) in enumerate(nums):
n_invalid_triplets += (i-num_count.get(num, 0)) * num_count.get(num... | number-of-unequal-triplets-in-array | Python One Pass O(n) Count invalid triplets | Rinnegan_Sep | 1 | 52 | number of unequal triplets in array | 2,475 | 0.699 | Easy | 33,959 |
https://leetcode.com/problems/number-of-unequal-triplets-in-array/discuss/2831749/Python-Simple-Python-Solution-Using-Brute-Force | class Solution:
def unequalTriplets(self, nums: List[int]) -> int:
result = 0
for i in range(len(nums)-2):
for j in range(i+1,len(nums)-1):
for k in range(j+1,len(nums)):
if nums[i] != nums[j] and nums[j] != nums[k] and nums[i] != nums[k]:
result = result + 1
return result | number-of-unequal-triplets-in-array | [ Python ] ✅✅ Simple Python Solution Using Brute Force🥳✌👍 | ASHOK_KUMAR_MEGHVANSHI | 1 | 80 | number of unequal triplets in array | 2,475 | 0.699 | Easy | 33,960 |
https://leetcode.com/problems/number-of-unequal-triplets-in-array/discuss/2849851/Python-brute-force-solution | class Solution:
def unequalTriplets(self, nums: List[int]) -> int:
ans = 0
for i in range(len(nums)):
for j in range(i+1,len(nums)):
for k in range(j+1, len(nums)):
if nums[i] != nums[j] and nums[i] != nums[k] and nums[j] != nums[k]:
... | number-of-unequal-triplets-in-array | Python brute force solution | StikS32 | 0 | 3 | number of unequal triplets in array | 2,475 | 0.699 | Easy | 33,961 |
https://leetcode.com/problems/number-of-unequal-triplets-in-array/discuss/2843209/O(n)-Simple-one-pass-Math-solution-with-detailed-explanation | class Solution:
def unequalTriplets(self, nums: List[int]) -> int:
n = len(nums)
combinations = math.comb(n, 3)
for num, frq in Counter(nums).items():
if frq >= 3:
combinations -= math.comb(frq, 3)
if frq >= 2:
combinations -= math.com... | number-of-unequal-triplets-in-array | O(n) Simple one-pass Math solution with detailed explanation | ckush | 0 | 10 | number of unequal triplets in array | 2,475 | 0.699 | Easy | 33,962 |
https://leetcode.com/problems/number-of-unequal-triplets-in-array/discuss/2839719/Three-loops-and-set-92-speed | class Solution:
def unequalTriplets(self, nums: List[int]) -> int:
ans = 0
len_nums = len(nums)
for i in range(len_nums - 2):
s = {nums[i]}
for j in range(i + 1, len_nums - 1):
if nums[j] in s:
continue
s.add(nums[j]... | number-of-unequal-triplets-in-array | Three loops and set, 92% speed | EvgenySH | 0 | 9 | number of unequal triplets in array | 2,475 | 0.699 | Easy | 33,963 |
https://leetcode.com/problems/number-of-unequal-triplets-in-array/discuss/2836490/Python-Solution | class Solution:
def unequalTriplets(self, nums: List[int]) -> int:
c=0
for i in range(len(nums)):
for j in range(i+1,len(nums)):
for k in range(j+1,len(nums)):
if(nums[i]!=nums[j] and nums[i]!=nums[k] and nums[j]!=nums[k]):
c+=1... | number-of-unequal-triplets-in-array | Python Solution | CEOSRICHARAN | 0 | 9 | number of unequal triplets in array | 2,475 | 0.699 | Easy | 33,964 |
https://leetcode.com/problems/number-of-unequal-triplets-in-array/discuss/2835397/Simple-Solution-by-python | class Solution:
def unequalTriplets(self, nums: List[int]) -> int:
count = 0
for i in range(0,len(nums)-2):
for j in range(i+1,len(nums)-1):
for k in range(j+1,len(nums)):
if (nums[i]!=nums[j]) and (nums[j]!=nums[k]) and (nums[i]!=nums[k]):
... | number-of-unequal-triplets-in-array | Simple Solution by python | pypypymi | 0 | 9 | number of unequal triplets in array | 2,475 | 0.699 | Easy | 33,965 |
https://leetcode.com/problems/number-of-unequal-triplets-in-array/discuss/2834252/Python3-freq-table | class Solution:
def unequalTriplets(self, nums: List[int]) -> int:
freq = Counter(nums)
ans = prefix = 0
suffix = len(nums)
for k, v in freq.items():
suffix -= v
ans += prefix * v * suffix
prefix += v
return ans | number-of-unequal-triplets-in-array | [Python3] freq table | ye15 | 0 | 17 | number of unequal triplets in array | 2,475 | 0.699 | Easy | 33,966 |
https://leetcode.com/problems/number-of-unequal-triplets-in-array/discuss/2833997/Easiest-python-solution-using-Combinations-(5-liner) | class Solution:
def unequalTriplets(self, nums: List[int]) -> int:
z=itertools.combinations(nums, 3)
count=0
for i in z:
if len(set(i))==3:
count+=1
return count | number-of-unequal-triplets-in-array | Easiest python solution using Combinations, (5 liner) | fahim_ash | 0 | 13 | number of unequal triplets in array | 2,475 | 0.699 | Easy | 33,967 |
https://leetcode.com/problems/number-of-unequal-triplets-in-array/discuss/2833704/Python-brute-force | class Solution:
def unequalTriplets(self, nums: List[int]) -> int:
result = 0
for i in range(len(nums)-2):
for j in range(i+1, len(nums)-1):
for k in range(j+1, len(nums)):
if nums[i] != nums[j] and nums[j] != nums[k] and nums[i] != nums[k]:
... | number-of-unequal-triplets-in-array | Python, brute force | blue_sky5 | 0 | 6 | number of unequal triplets in array | 2,475 | 0.699 | Easy | 33,968 |
https://leetcode.com/problems/number-of-unequal-triplets-in-array/discuss/2832446/Python-one-line-brute-force | class Solution:
def unequalTriplets(self, nums: List[int]) -> int:
return len([[i,j,k] for i in range(len(nums)) for j in range(i,len(nums)) for k in range(j,len(nums)) if nums[i]!=nums[j] and nums[i]!=nums[k] and nums[j]!=nums[k]]) | number-of-unequal-triplets-in-array | Python, one line brute force | Leox2022 | 0 | 14 | number of unequal triplets in array | 2,475 | 0.699 | Easy | 33,969 |
https://leetcode.com/problems/number-of-unequal-triplets-in-array/discuss/2832414/Python-easy-Solution-using-brute-force | class Solution:
def unequalTriplets(self, nums: List[int]) -> int:
count=0
for i in range(len(nums)):
for j in range(i+1,len(nums)):
if nums[i]!=nums[j]:
for k in range(j+1,len(nums)):
if nums[i]!=nums[k] and nums[j]!=nums[k]:
... | number-of-unequal-triplets-in-array | Python easy Solution using brute force | Motaharozzaman1996 | 0 | 2 | number of unequal triplets in array | 2,475 | 0.699 | Easy | 33,970 |
https://leetcode.com/problems/number-of-unequal-triplets-in-array/discuss/2832190/python-simple-iteration | class Solution:
def unequalTriplets(self, nums: List[int]) -> int:
c=0
n=len(nums)
for i in range(n-2):
for j in range(i+1,n-1):
for k in range(j+1,n):
if nums[i]!=nums[j] and nums[i]!=nums[k] and nums[j]!=nums[k]:
c+=1
... | number-of-unequal-triplets-in-array | python simple iteration | insane_me12 | 0 | 11 | number of unequal triplets in array | 2,475 | 0.699 | Easy | 33,971 |
https://leetcode.com/problems/number-of-unequal-triplets-in-array/discuss/2832020/Python-Brute-Force-Solution | class Solution:
def unequalTriplets(self, nums: List[int]) -> int:
l = len(nums)
count = 0
for i in range(l-2):
for j in range(i+1,l-1):
for k in range(j+1,l):
if nums[i] != nums[j] and nums[j] != nums[k] and nums[k] != nums[i]:
... | number-of-unequal-triplets-in-array | Python Brute Force Solution | saivamshi0103 | 0 | 4 | number of unequal triplets in array | 2,475 | 0.699 | Easy | 33,972 |
https://leetcode.com/problems/number-of-unequal-triplets-in-array/discuss/2831882/Python-Easy-solution | class Solution:
def unequalTriplets(self, nums: List[int]) -> int:
count=0
for i in range(len(nums)):
for j in range(i+1,len(nums)):
for k in range(j+1,len(nums)):
if nums[i]!=nums[j] and nums[j]!=nums[k] and nums[k]!=nums[i]:
c... | number-of-unequal-triplets-in-array | Python Easy solution | Vimalshetty07 | 0 | 25 | number of unequal triplets in array | 2,475 | 0.699 | Easy | 33,973 |
https://leetcode.com/problems/number-of-unequal-triplets-in-array/discuss/2831772/Python-Bruteforce-or-Easy-to-understand | class Solution:
def unequalTriplets(self, nums: List[int]) -> int:
n = len(nums)
trip = 0
for i in range(n):
for j in range(i+1, n):
for k in range(j+1, n):
if (nums[i] != nums[j] and nums[i] != nums[k] and nums[j] != nums[k]):
... | number-of-unequal-triplets-in-array | Python Bruteforce | Easy to understand | jubinsoni | 0 | 9 | number of unequal triplets in array | 2,475 | 0.699 | Easy | 33,974 |
https://leetcode.com/problems/number-of-unequal-triplets-in-array/discuss/2831689/Python3-Simple | class Solution:
def unequalTriplets(self, nums: List[int]) -> int:
n=len(nums)
answ=0
for i in range(n):
for j in range(i+1,n):
for k in range(j+1,n):
answ+= len({nums[i],nums[j],nums[k]})==3
return answ | number-of-unequal-triplets-in-array | Python3, Simple | Silvia42 | 0 | 36 | number of unequal triplets in array | 2,475 | 0.699 | Easy | 33,975 |
https://leetcode.com/problems/number-of-unequal-triplets-in-array/discuss/2831665/pythonjava-brute-force | class Solution:
def unequalTriplets(self, nums: List[int]) -> int:
n = len(nums)
res = 0
for i in range(n - 2):
for j in range(i + 1, n - 1):
for k in range(j + 1, n):
if nums[i] != nums[j] and nums[i] != nums[k] and nums[j] != nums[k]:
... | number-of-unequal-triplets-in-array | python/java brute force | akaghosting | 0 | 23 | number of unequal triplets in array | 2,475 | 0.699 | Easy | 33,976 |
https://leetcode.com/problems/closest-nodes-queries-in-a-binary-search-tree/discuss/2831726/Binary-Search-Approach-or-Python | class Solution(object):
def closestNodes(self, root, queries):
def dfs(root, arr):
if not root: return
dfs(root.left, arr)
arr.append(root.val)
dfs(root.right, arr)
arr = []
dfs(root, arr)
ans = []
n = len(arr)
for key i... | closest-nodes-queries-in-a-binary-search-tree | Binary Search Approach | Python | its_krish_here | 11 | 975 | closest nodes queries in a binary search tree | 2,476 | 0.404 | Medium | 33,977 |
https://leetcode.com/problems/closest-nodes-queries-in-a-binary-search-tree/discuss/2838464/Python-solution | class Solution:
def closestNodes(self, root: Optional[TreeNode], queries: List[int]) -> List[List[int]]:
nums = []
def dfs(node):
if node:
nums.append(node.val)
if node.left:
dfs(node.left)
if node.right:
... | closest-nodes-queries-in-a-binary-search-tree | Python solution | maomao1010 | 0 | 2 | closest nodes queries in a binary search tree | 2,476 | 0.404 | Medium | 33,978 |
https://leetcode.com/problems/closest-nodes-queries-in-a-binary-search-tree/discuss/2836153/Python-(Simple-DFS) | class Solution:
def closestNodes(self, root, queries):
ans = []
def dfs(node):
if not node:
return
dfs(node.left)
ans.append(node.val)
dfs(node.right)
dfs(root)
res = []
n = len(ans)
for i in queri... | closest-nodes-queries-in-a-binary-search-tree | Python (Simple DFS) | rnotappl | 0 | 4 | closest nodes queries in a binary search tree | 2,476 | 0.404 | Medium | 33,979 |
https://leetcode.com/problems/closest-nodes-queries-in-a-binary-search-tree/discuss/2834272/Python3-in-order-traversal | class Solution:
def closestNodes(self, root: Optional[TreeNode], queries: List[int]) -> List[List[int]]:
vals = []
node = root
stack = []
while node or stack:
if node:
stack.append(node)
node = node.left
else:
... | closest-nodes-queries-in-a-binary-search-tree | [Python3] in-order traversal | ye15 | 0 | 4 | closest nodes queries in a binary search tree | 2,476 | 0.404 | Medium | 33,980 |
https://leetcode.com/problems/closest-nodes-queries-in-a-binary-search-tree/discuss/2832423/Inorder-%2B-Binary-search-or-Python-or-easy-method | class Solution:
def closestNodes(self, root: Optional[TreeNode], queries: List[int]) -> List[List[int]]:
dic = {}
inorder_array = []
res = []
def inorder(node):
if(not node):
return
inorder(node.left)
inorder_array.append(node.val)
inorder(node.right)
inorder(root)
def find(target):
... | closest-nodes-queries-in-a-binary-search-tree | Inorder + Binary search | Python | easy method | 55337123kk3 | 0 | 16 | closest nodes queries in a binary search tree | 2,476 | 0.404 | Medium | 33,981 |
https://leetcode.com/problems/closest-nodes-queries-in-a-binary-search-tree/discuss/2831885/Inorder-Traversal | class Solution:
def closestNodes(self, root: Optional[TreeNode], queries: List[int]) -> List[List[int]]:
nums = []
def in_order(node):
nonlocal nums
if not node:
return
in_order(node.left)
nums.append(node.val)
i... | closest-nodes-queries-in-a-binary-search-tree | Inorder Traversal | tiaotiao5762 | 0 | 3 | closest nodes queries in a binary search tree | 2,476 | 0.404 | Medium | 33,982 |
https://leetcode.com/problems/closest-nodes-queries-in-a-binary-search-tree/discuss/2831856/Python-Simple-Python-Solution-Using-Binary-Search | class Solution:
def closestNodes(self, root: Optional[TreeNode], queries: List[int]) -> List[List[int]]:
array = []
def PrintTree(node):
if node != None:
PrintTree(node.left)
array.append(node.val)
PrintTree(node.right)
PrintTree(root)
result = []
for query in queries:
left, right = 0,... | closest-nodes-queries-in-a-binary-search-tree | [ Python ] ✅✅ Simple Python Solution Using Binary Search🥳✌👍 | ASHOK_KUMAR_MEGHVANSHI | 0 | 27 | closest nodes queries in a binary search tree | 2,476 | 0.404 | Medium | 33,983 |
https://leetcode.com/problems/minimum-fuel-cost-to-report-to-the-capital/discuss/2834516/Python-DFS-Picture-explanation-O(N) | class Solution:
def minimumFuelCost(self, roads: List[List[int]], seats: int) -> int:
n = len(roads) + 1
graph = defaultdict(list)
for a, b in roads:
graph[a].append(b)
graph[b].append(a)
def dfs(u, p):
cnt = 1
for v in graph[u... | minimum-fuel-cost-to-report-to-the-capital | [Python] DFS - Picture explanation - O(N) | hiepit | 33 | 528 | minimum fuel cost to report to the capital | 2,477 | 0.53 | Medium | 33,984 |
https://leetcode.com/problems/minimum-fuel-cost-to-report-to-the-capital/discuss/2831713/Python-DFS | class Solution:
def minimumFuelCost(self, roads: List[List[int]], seats: int) -> int:
graph = defaultdict(list)
for e in roads:
graph[e[0]].append(e[1])
graph[e[1]].append(e[0])
visited = set()
fuel = [0]
def dfs(node):
visited.add(node)
... | minimum-fuel-cost-to-report-to-the-capital | Python DFS | JSTM2022 | 7 | 220 | minimum fuel cost to report to the capital | 2,477 | 0.53 | Medium | 33,985 |
https://leetcode.com/problems/minimum-fuel-cost-to-report-to-the-capital/discuss/2839984/Python-DFS-Picture-explanation-Video-Solution-O(n) | class Solution:
def minimumFuelCost(self, roads: List[List[int]], seats: int) -> int:
m = defaultdict(list)
for a,b in roads:
m[a].append(b)
m[b].append(a)
ans = [0]
def dfs(node, prev):
people = 1
... | minimum-fuel-cost-to-report-to-the-capital | [Python] DFS - Picture explanation - Video Solution - O(n) | cheatcode-ninja | 2 | 17 | minimum fuel cost to report to the capital | 2,477 | 0.53 | Medium | 33,986 |
https://leetcode.com/problems/minimum-fuel-cost-to-report-to-the-capital/discuss/2832516/Python-3-Easy-to-understand-DFS | class Solution:
def minimumFuelCost(self, roads, seats):
graph = defaultdict(set)
# find the adjacency list representation
for i,j in roads:
graph[i].add(j)
graph[j].add(i)
n = len(graph)
if n==0: return 0
visited = [False]*n
self.ans =... | minimum-fuel-cost-to-report-to-the-capital | Python 3, Easy to understand DFS | cuongmng | 1 | 24 | minimum fuel cost to report to the capital | 2,477 | 0.53 | Medium | 33,987 |
https://leetcode.com/problems/minimum-fuel-cost-to-report-to-the-capital/discuss/2839206/Python3-Concise-and-Commented-DFS-approach | class Solution:
def minimumFuelCost(self, roads: List[List[int]], seats: int) -> int:
# we can do dfs into the leaf nodes and on return calculate the
# amount of people that travel together. We will travel to the
# leaves first and count the collected representatives on the
# way ba... | minimum-fuel-cost-to-report-to-the-capital | [Python3] - Concise and Commented DFS approach | Lucew | 0 | 3 | minimum fuel cost to report to the capital | 2,477 | 0.53 | Medium | 33,988 |
https://leetcode.com/problems/minimum-fuel-cost-to-report-to-the-capital/discuss/2839206/Python3-Concise-and-Commented-DFS-approach | class Solution:
def minimumFuelCost(self, roads: List[List[int]], seats: int) -> int:
graph = collections.defaultdict(list)
for start, end in roads:
graph[start].append(end)
graph[end].append(start)
self.fuel = 0
self.dfs(0, -1, graph, seats)
return... | minimum-fuel-cost-to-report-to-the-capital | [Python3] - Concise and Commented DFS approach | Lucew | 0 | 3 | minimum fuel cost to report to the capital | 2,477 | 0.53 | Medium | 33,989 |
https://leetcode.com/problems/minimum-fuel-cost-to-report-to-the-capital/discuss/2835335/My-Python-solution | class Solution:
def minimumFuelCost(self, roads: List[List[int]], seats: int) -> int:
graph = defaultdict(list)
for x, y in roads:
graph[x].append(y)
graph[y].append(x)
self.ans = 0
def dfs(i, prev, people = 1):
for x in graph[i]:
... | minimum-fuel-cost-to-report-to-the-capital | My Python solution | Chiki1601 | 0 | 5 | minimum fuel cost to report to the capital | 2,477 | 0.53 | Medium | 33,990 |
https://leetcode.com/problems/minimum-fuel-cost-to-report-to-the-capital/discuss/2834288/Python3-post-order-dfs | class Solution:
def minimumFuelCost(self, roads: List[List[int]], seats: int) -> int:
graph = [[] for _ in range(len(roads)+1)]
for u, v in roads:
graph[u].append(v)
graph[v].append(u)
ans = 0
def dfs(u, p):
"""Return number of people g... | minimum-fuel-cost-to-report-to-the-capital | [Python3] post-order dfs | ye15 | 0 | 12 | minimum fuel cost to report to the capital | 2,477 | 0.53 | Medium | 33,991 |
https://leetcode.com/problems/minimum-fuel-cost-to-report-to-the-capital/discuss/2832330/Naive-BFS-(bottom-up)-with-degree | class Solution:
def minimumFuelCost(self, roads: List[List[int]], seats: int) -> int:
import collections
node = collections.defaultdict(list)
n = len(roads)+1
degree = [0] * n
people = [1] * n
for i, j in roads:
degree[i] +=1
degree[j] +=1
... | minimum-fuel-cost-to-report-to-the-capital | Naive BFS (bottom up) with degree | her0e1c1 | 0 | 19 | minimum fuel cost to report to the capital | 2,477 | 0.53 | Medium | 33,992 |
https://leetcode.com/problems/minimum-fuel-cost-to-report-to-the-capital/discuss/2831941/Easiest-Python-Solution | class Solution:
def minimumFuelCost(self, roads: List[List[int]], seats: int) -> int:
n = len(roads)
op =[0]
graph = defaultdict(list)
for i,j in roads:
graph[i].append(j)
graph[j].append(i)
vis=[0 for _ in range(n+1)]
def dfs(root,pr):
vis[root]=1
for nei in graph[root]:
if nei!=pr:
d... | minimum-fuel-cost-to-report-to-the-capital | Easiest Python Solution | prateekgoel7248 | 0 | 49 | minimum fuel cost to report to the capital | 2,477 | 0.53 | Medium | 33,993 |
https://leetcode.com/problems/minimum-fuel-cost-to-report-to-the-capital/discuss/2831886/DFS-divide-and-merge-sub-problems-O(n) | class Solution:
def minimumFuelCost(self, roads: List[List[int]], seats: int) -> int:
g = defaultdict(list)
for u, v in roads:
g[u].append(v)
g[v].append(u)
# print(f"g {g}")
def dp(u, pre):
liters = 0
people = 0
if... | minimum-fuel-cost-to-report-to-the-capital | DFS, divide and merge sub problems, O(n) | goodgoodwish | 0 | 8 | minimum fuel cost to report to the capital | 2,477 | 0.53 | Medium | 33,994 |
https://leetcode.com/problems/minimum-fuel-cost-to-report-to-the-capital/discuss/2831710/Python-or-Topological-Sort | class Solution:
def minimumFuelCost(self, roads: List[List[int]], seats: int) -> int:
n = len(roads) + 1
deg = [0] * n
reps = [1] * n
g = defaultdict(list)
for u, v in roads:
g[u].append(v)
g[v].append(u)
deg[u] += 1
deg[v] += 1... | minimum-fuel-cost-to-report-to-the-capital | Python | Topological Sort | bachelorwang | 0 | 55 | minimum fuel cost to report to the capital | 2,477 | 0.53 | Medium | 33,995 |
https://leetcode.com/problems/minimum-fuel-cost-to-report-to-the-capital/discuss/2831694/Python3-BFS-Graph | class Solution:
def minimumFuelCost(self, roads: List[List[int]], seats: int) -> int:
d = defaultdict(list)
q = deque([])
c = Counter()
c_acc = Counter()
for x, y in roads:
d[x].append(y)
d[y].append(x)
c[x] += 1
c[y] +... | minimum-fuel-cost-to-report-to-the-capital | Python3, BFS, Graph | mike840609 | 0 | 74 | minimum fuel cost to report to the capital | 2,477 | 0.53 | Medium | 33,996 |
https://leetcode.com/problems/number-of-beautiful-partitions/discuss/2833244/Python-Top-down-DP-Clean-and-Concise-O(N-*-K) | class Solution:
def beautifulPartitions(self, s: str, k: int, minLength: int) -> int:
n = len(s)
MOD = 10**9 + 7
def isPrime(c):
return c in ['2', '3', '5', '7']
@lru_cache(None)
def dp(i, k):
if k == 0 and i <= n:
return 1
... | number-of-beautiful-partitions | [Python] Top down DP - Clean & Concise - O(N * K) | hiepit | 21 | 382 | number of beautiful partitions | 2,478 | 0.294 | Hard | 33,997 |
https://leetcode.com/problems/number-of-beautiful-partitions/discuss/2831907/Python-simple-DP-with-optimization | class Solution:
def beautifulPartitions(self, s: str, k: int, minLength: int) -> int:
n = len(s)
primes = ['2', '3', '5', '7']
# pruning
if k * minLength > n or s[0] not in primes or s[-1] in primes:
return 0
# posible starting indexes of a new p... | number-of-beautiful-partitions | Python simple DP with optimization | zsk99881 | 7 | 353 | number of beautiful partitions | 2,478 | 0.294 | Hard | 33,998 |
https://leetcode.com/problems/number-of-beautiful-partitions/discuss/2832142/Python3-Solution-or-DP-or-100-Faster-or-Clean-and-Concise | class Solution:
def beautifulPartitions(self, S, K, M):
N = len(S)
P = '2357'
help = lambda a, b: a not in P and b in P
if not help(S[-1], S[0]) or K * M > N: return 0
dp = [1] * (N - M)
mod = 10 ** 9 + 7
for j in range(1, K):
dp2 = [0] * (N - M)
... | number-of-beautiful-partitions | ✔ Python3 Solution | DP | 100% Faster | Clean & Concise | satyam2001 | 1 | 89 | number of beautiful partitions | 2,478 | 0.294 | Hard | 33,999 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.