post_href stringlengths 57 213 | python_solutions stringlengths 71 22.3k | slug stringlengths 3 77 | post_title stringlengths 1 100 | user stringlengths 3 29 | upvotes int64 -20 1.2k | views int64 0 60.9k | problem_title stringlengths 3 77 | number int64 1 2.48k | acceptance float64 0.14 0.91 | difficulty stringclasses 3 values | __index_level_0__ int64 0 34k |
|---|---|---|---|---|---|---|---|---|---|---|---|
https://leetcode.com/problems/find-the-most-competitive-subsequence/discuss/952823/Python-Time%3A-O(n)-Space%3A-O(n)-deque-sliding-windows | class Solution: # time limit
def mostCompetitive(self, nums: List[int], k: int) -> List[int]:
return min(combinations(nums, k)) | find-the-most-competitive-subsequence | Python, Time: O(n), Space: O(n), deque, sliding windows | Chumicat | 0 | 79 | find the most competitive subsequence | 1,673 | 0.493 | Medium | 24,200 |
https://leetcode.com/problems/minimum-moves-to-make-array-complementary/discuss/1650877/Sweep-Algorithm-or-Explained-Python | class Solution:
def minMoves(self, nums: List[int], limit: int) -> int:
n = len(nums)
overlay_arr = [0] * (2*limit+2)
for i in range(n//2):
left_boundary = min(nums[i], nums[n-1-i]) + 1
no_move_value = nums[i] + nums[n-1-i]
right_boundary = max(nums[i], nums[n-1-i]) + limit
overlay_arr[left_boundary] -= 1
overlay_arr[no_move_value] -= 1
overlay_arr[no_move_value+1] += 1
overlay_arr[right_boundary+1] += 1
curr_moves = n #initial assumption of two moves for each pair
res = float("inf")
# start Sweeping
for i in range(2, 2*limit+1):
curr_moves += overlay_arr[i]
res = min(res, curr_moves)
return res | minimum-moves-to-make-array-complementary | Sweep Algorithm | Explained [Python] | xyz76 | 5 | 397 | minimum moves to make array complementary | 1,674 | 0.386 | Medium | 24,201 |
https://leetcode.com/problems/minimum-moves-to-make-array-complementary/discuss/954060/Python3-difference-array | class Solution:
def minMoves(self, nums: List[int], limit: int) -> int:
diff = [0]*(2*limit+2) # difference array
for i in range(len(nums)//2):
m = min(nums[i], nums[~i]) + 1 # lower bound
diff[m] += -1
x = nums[i] + nums[~i]
diff[x] += -1
diff[x+1] += 1
M = max(nums[i], nums[~i]) + 1 + limit # upper bound
diff[M] += 1
for i in range(1, len(diff)): diff[i] += diff[i-1] # prefix sum
return len(nums) + min(diff) | minimum-moves-to-make-array-complementary | [Python3] difference array | ye15 | 0 | 302 | minimum moves to make array complementary | 1,674 | 0.386 | Medium | 24,202 |
https://leetcode.com/problems/minimum-moves-to-make-array-complementary/discuss/954060/Python3-difference-array | class Solution:
def minMoves(self, nums: List[int], limit: int) -> int:
freq = {} # frequency table
lower, upper = [], []
for i in range(len(nums)//2):
x = nums[i] + nums[~i]
freq[x] = 1 + freq.get(x, 0)
lower.append(min(nums[i], nums[~i]))
upper.append(max(nums[i], nums[~i]) + 1 + limit)
lower.sort()
upper.sort()
ans = inf
for x in freq:
k = len(lower) - bisect_left(lower, x)
kk = bisect_right(upper, x)
val = len(nums)//2 - freq[x] + k + kk
ans = min(ans, val)
return ans | minimum-moves-to-make-array-complementary | [Python3] difference array | ye15 | 0 | 302 | minimum moves to make array complementary | 1,674 | 0.386 | Medium | 24,203 |
https://leetcode.com/problems/minimize-deviation-in-array/discuss/1782626/Python-Simple-Python-Solution-By-SortedList | class Solution:
def minimumDeviation(self, nums: List[int]) -> int:
from sortedcontainers import SortedList
for i in range(len(nums)):
if nums[i]%2!=0:
nums[i]=nums[i]*2
nums = SortedList(nums)
result = 100000000000
while True:
min_value = nums[0]
max_value = nums[-1]
if max_value % 2 == 0:
nums.pop()
nums.add(max_value // 2)
max_value = nums[-1]
min_value = nums[0]
result = min(result , max_value - min_value)
else:
result = min(result , max_value - min_value)
break
return result | minimize-deviation-in-array | [ Python ] ββ Simple Python Solution By SortedList π₯β | ASHOK_KUMAR_MEGHVANSHI | 10 | 563 | minimize deviation in array | 1,675 | 0.52 | Hard | 24,204 |
https://leetcode.com/problems/minimize-deviation-in-array/discuss/954165/Python3-priority-queue | class Solution:
def minimumDeviation(self, nums: List[int]) -> int:
pq = [-2*x if x&1 else -x for x in nums]
heapify(pq)
most = max(pq)
ans = most - pq[0]
while not pq[0]&1:
x = heappop(pq)//2
heappush(pq, x)
most = max(most, x)
ans = min(ans, most - pq[0])
return ans | minimize-deviation-in-array | [Python3] priority queue | ye15 | 6 | 194 | minimize deviation in array | 1,675 | 0.52 | Hard | 24,205 |
https://leetcode.com/problems/minimize-deviation-in-array/discuss/1041971/Minimize-Deviation-in-Array%3A-Python-with-Priority-Queue-Heap | class Solution:
def minimumDeviation(self, nums: List[int]) -> int:
heap = [-n*2 if n%2 else -n for n in set(nums)]
heapify(heap)
min_n, max_n = -max(heap), -heap[0]
min_dev = max_n - min_n
while max_n % 2 == 0:
new_n = max_n // 2
while new_n&1 == 0 and new_n>>1 > min_n:
new_n >>= 1
heapreplace(heap, -new_n)
min_n, max_n = min(new_n, min_n), -heap[0]
min_dev = min(max_n - min_n, min_dev)
return min_dev | minimize-deviation-in-array | Minimize Deviation in Array: Python with Priority Queue Heap | imanon | 2 | 190 | minimize deviation in array | 1,675 | 0.52 | Hard | 24,206 |
https://leetcode.com/problems/minimize-deviation-in-array/discuss/2656229/Python3-or-Priority-Queue | class Solution:
def minimumDeviation(self, nums: List[int]) -> int:
for i in range(len(nums)):
if nums[i]%2!=0:
nums[i]*=2
minVal=min(nums)
nums=[-val for val in nums]
nums,ans=nums,float('inf')
heapify(nums)
while nums and abs(nums[0])%2==0:
maxVal=abs(heappop(nums))
ans=min(ans,abs(maxVal-minVal))
maxVal=maxVal//2
minVal=min(minVal,maxVal)
heappush(nums,-maxVal)
return min(ans,abs(min(nums))-abs(max(nums))) | minimize-deviation-in-array | [Python3] | Priority Queue | swapnilsingh421 | 0 | 6 | minimize deviation in array | 1,675 | 0.52 | Hard | 24,207 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/961441/Python-one-liner | class Solution:
def interpret(self, command: str) -> str:
return command.replace('()','o').replace('(al)','al') | goal-parser-interpretation | Python one-liner | lokeshsenthilkumar | 92 | 8,000 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,208 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/1056146/Simple-and-easy-python-if-else-solution-or-94-memory-86-time | class Solution:
def interpret(self, command: str) -> str:
s = ""
i = 0
while i < len(command):
if command[i] == "G":
s += "G"
i += 1
else:
if i < len(command) and command[i+1] == ")":
s += "o"
i += 2
else:
s += "al"
i += 4
return s | goal-parser-interpretation | Simple and easy python if-else solution | 94% memory, 86% time | vanigupta20024 | 6 | 745 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,209 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/1008632/Python-two-simple-solutions | class Solution:
def interpret(self, command: str) -> str:
for key, val in {"()":"o", "(al)":"al" }.items():
command = command.replace(key, val)
return command | goal-parser-interpretation | Python two simple solutions | denisrasulev | 6 | 510 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,210 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/1008632/Python-two-simple-solutions | class Solution:
def interpret(self, command: str) -> str:
return command.replace('()','o').replace('(al)','al') | goal-parser-interpretation | Python two simple solutions | denisrasulev | 6 | 510 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,211 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/2080447/Python-two-liner-solution-~-Easy | class Solution:
def interpret(self, command: str) -> str:
command=command.replace("()","o")
command=command.replace("(al)","al")
return command | goal-parser-interpretation | Python two liner solution ~ Easy | Shivam_Raj_Sharma | 5 | 118 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,212 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/1697217/**-Python-code%3A-one-linear-solution | class Solution:
def interpret(self, command: str) -> str:
return command.replace("()","o").replace("(al)","al") | goal-parser-interpretation | ** Python code: one linear solution | Anilchouhan181 | 5 | 146 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,213 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/2406043/simpleoror-python-ororeasy-to-understand | class Solution:
def interpret(self, command: str) -> str:
re=""
i=0
while i<len(command):
if command[i]=='('and command[i+1]==')':
re+='o'
i+=2
elif command[i]=='(' or command[i]==')':
i+=1
else:
re+=command[i]
i+=1
return re | goal-parser-interpretation | simple|| python ||easy to understand | Sneh713 | 4 | 54 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,214 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/2163911/Python-Short-Solution-with-inbuilt-function | class Solution:
def interpret(self, command: str) -> str:
command=command.replace('()','o')
command=command.replace('(al)','al')
return command | goal-parser-interpretation | Python Short Solution with inbuilt function | pruthashouche | 2 | 37 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,215 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/962469/Python-one-pass-O(N) | class Solution:
def interpret(self, command: str) -> str:
result = []
i = 0
while i < len(command):
if command[i] == 'G':
result.append('G')
i += 1
elif command[i] == '(' and command[i+1] == ')':
result.append('o')
i += 2
else:
result.append('al')
i += 4
return "".join(result) | goal-parser-interpretation | Python, one pass O(N) | blue_sky5 | 2 | 104 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,216 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/2421606/easiest-python-solution | class Solution:
def interpret(self, command: str) -> str:
res = ""
for i in range(len(command)):
if command[i] =="G":
res+=command[i]
elif command[i] == "(" and command[i+1] == ")":
res+="o"
elif command[i] == "(" and command[i+3] == ")":
res+="al"
return res | goal-parser-interpretation | easiest python solution | Abdulrahman_Ahmed | 1 | 48 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,217 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/2373195/Python-Best-Solution-for-Interview | class Solution:
def interpret(self, command: str) -> str:
i=0
n=len(command)
ans=""
while i<n:
if command[i]== "(" and command[i+1]==")":
ans+='o'
i+=2
elif command[i]=="(":
ans+="al"
i+=4
else:
ans+="G"
i+=1
return ans | goal-parser-interpretation | Python - Best Solution for Interview | aady_02 | 1 | 65 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,218 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/2264400/Python3-Runtime%3A-49ms-43.29-oror-Memory%3A-13.9mb-54.48 | class Solution:
# O(n) || O(n)
# Runtime: 49ms 43.29% || Memory: 13.9mb 54.48%
def interpret(self, command: str) -> str:
newString = []
for idx, val in enumerate(command):
if val == '(' and command[idx+1] == ')':
newString.append('o')
continue
elif val == '(' and command[idx+1] != ')' or val == ')':
continue
newString.append(val)
return ''.join(newString) | goal-parser-interpretation | Python3 # Runtime: 49ms 43.29% || Memory: 13.9mb 54.48% | arshergon | 1 | 35 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,219 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/2163901/Python-Simple-and-Easy-Solution | class Solution:
def interpret(self, command: str) -> str:
s=""
for i in range(len(command)):
if command[i]=='G':
s=s+"G"
elif command[i]=='(' and command[i+1]==')':
s=s+"o"
else:
if command[i]=='(' and command[i+1]=='a':
s=s+"al"
return s | goal-parser-interpretation | Python Simple and Easy Solution | pruthashouche | 1 | 26 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,220 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/1885393/Python-solution-using-a-dictionary | class Solution:
def interpret(self, command: str) -> str:
sep = 0
i = 0
string = ""
dic = {"G":"G", "()": "o", "(al)": "al"}
while i < len(command):
i +=1
if command[sep:i] in dic:
string += dic[command[sep:i]]
sep = i
return string | goal-parser-interpretation | Python solution using a dictionary | poliliu | 1 | 67 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,221 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/1526718/Simple-O(n)-Python-Solution | class Solution:
def interpret(self, command: str) -> str:
res = ""
for i in range(len(command)):
if command[i] == "G":
res += "G"
elif command[i] == "(":
if command[i+1] == ")":
res += "o"
else:
if command[i+1] == "a" and command[i+2] == "l":
res += "al"
return res | goal-parser-interpretation | Simple O(n) Python Solution | PythonicLava | 1 | 94 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,222 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/1284671/Python-Solution-or-99.99-Faster | class Solution:
def interpret(self, command: str) -> str:
command = command.replace("()","o")
command = command.replace("(al)","al")
return command | goal-parser-interpretation | Python Solution | 99.99% Faster | Gautam_ProMax | 1 | 96 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,223 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/2842955/python-very-easy-2-lines-greater | class Solution:
def interpret(self, command: str) -> str:
x = command.replace("()" , "o");
y = x.replace("(al)" , "al");
return y | goal-parser-interpretation | python very easy 2 lines --> | seifsoliman | 0 | 3 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,224 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/2840049/Python-2-liner-solution-with-O(n)-time-complexity-or-Space-greater-O(1) | class Solution:
def interpret(self, command: str) -> str:
x=command.replace("(al)", "al")
x=x.replace("()", "o")
return x | goal-parser-interpretation | Python 2 liner solution with O(n) time complexity | Space -> O(1) | definitely_not | 0 | 1 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,225 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/2837821/Fast-Python-Solution-using-While-Loop | class Solution:
def interpret(self, command: str) -> str:
x=0
ans=''
while x<len(command):
if command[x]=="G":
ans+="G"
elif command[x]=="(" and command[x+1]==")":
ans+="o"
x+=1
else:
ans+="al"
x+=3
x+=1
return ans | goal-parser-interpretation | Fast Python Solution using While Loop | dvdr1029 | 0 | 2 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,226 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/2824238/Single-Line-Solution(Python) | class Solution:
def interpret(self, command: str) -> str:
return command.replace("()","o").replace("(al)","al") | goal-parser-interpretation | Single Line Solution(Python) | durgaraopolamarasetti | 0 | 2 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,227 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/2821410/python-easy-solution-oror-using-replace-oror-one-liner | class Solution:
def interpret(self, command: str) -> str:
return (command.replace("()","o").replace("(al)","al")) | goal-parser-interpretation | [python] easy solution || using replace || one liner | user9516zM | 0 | 1 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,228 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/2819875/Python-simple-Solution-Goal-Parser | class Solution:
def interpret(self, command: str) -> str:
command=command.replace('()','o')
command=command.replace('(al)','al')
return command | goal-parser-interpretation | Python simple Solution Goal Parser | findakshaybhat | 0 | 1 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,229 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/2812079/Optimal-and-Clean-2-ways%3A-O(n)-time-and-O(n)-space | class Solution:
# O(n) time : O(n) space
def interpret(self, command: str) -> str:
res = ""
i = 0
while i < len(command):
if command[i] == 'G':
res += 'G'
i += 1
else:
if command[i+1] == ')':
res += 'o'
i += 2
else:
res += 'al'
i += 4
return res
# O(n) time : O(n) space
def interpret(self, command: str) -> str:
return command.replace('()', 'o').replace('(al)', 'al') | goal-parser-interpretation | Optimal and Clean - 2 ways: O(n) time and O(n) space | topswe | 0 | 3 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,230 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/2807535/Goal-Parser-Interpretation-or-Python-Using-Replace-method | class Solution:
def interpret(self, command: str) -> str:
command = command.replace("()","o")
command = command.replace("(al)","al")
return command | goal-parser-interpretation | Goal Parser Interpretation | Python Using Replace method | jashii96 | 0 | 2 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,231 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/2797424/Python-simple-solution-using-while-loops | class Solution:
def interpret(self, command: str) -> str:
s=""
i=0
while i<len(command):
if command[i]=="G":
s+="G"
else:
if command[i]=="(" and command[i+1]==")":
s+="o"
i+=1
else:
s+="al"
i+=3
i+=1
return s | goal-parser-interpretation | Python simple solution using while loops | sbhupender68 | 0 | 2 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,232 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/2788183/Simple-Python3-Solution | class Solution:
def interpret(self, command: str) -> str:
output = ''
for i in range(len(command)):
if command[i].isalpha():
output += command[i]
elif (command[i] == '(') and command[i+1] == ')':
output += 'o'
return output | goal-parser-interpretation | Simple Python3 Solution | vivekrajyaguru | 0 | 3 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,233 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/2782152/python-easy-solution | class Solution:
def interpret(self, command: str) -> str:
for i in range(len(command)):
if "()":
command=command.replace("()","o")
if "(al)":
command=command.replace("(al)","al")
return command | goal-parser-interpretation | python easy solution | beingab329 | 0 | 4 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,234 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/2744429/Python-Easy-Stack-Solution | class Solution:
def interpret(self, command: str) -> str:
# return command.replace('()', 'o').replace('(al)', 'al')
stack = []
for i in command:
if i ==')':
if stack[-1]=='(':
stack.pop()
stack.append('o')
else:
stack[-3]=''
else:
stack.append(i)
# print(stack)
return ''.join(stack) | goal-parser-interpretation | Python Easy Stack Solution | ben_wei | 0 | 2 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,235 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/2738423/Python-Solution-94.89-faster-O(n)-Time-Complexity | class Solution:
def interpret(self, command: str) -> str:
ans = ''
i = 0
while i<len(command):
if command[i]=='G':
ans+='G'
elif command[i]=='(':
if command[i+1]==')':
ans+='o'
elif command[i+1]=='a':
ans+='al'
i+=1
return ans | goal-parser-interpretation | Python Solution 94.89% faster O(n) Time Complexity | nidhi_nishad26 | 0 | 10 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,236 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/2725819/Python3-(Not-Fastest-But-works)-Solution | class Solution:
def interpret(self, command: str) -> str:
output = ""
temp = ""
for e in command:
if e == "G":
output += ('G')
elif e == "(":
temp += ('(')
elif temp == '(' and e == ')':
output += ('o')
temp = ""
elif e == "a":
output += ('al')
else:
temp = ""
return output
''' | goal-parser-interpretation | Python3 (Not Fastest But works) Solution | xzvg | 0 | 1 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,237 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/2718273/Python-Solution | class Solution:
def interpret(self, command: str) -> str:
idx, jdx, s = 0, 1, ""
while jdx <= len(command):
if command[idx] == "G":
s = s + "G"
idx += 1
jdx += 1
elif command[idx] == "(":
if command[jdx] == ")":
s += "o"
idx += 2
jdx += 2
elif command[jdx] == "a":
s += "al"
idx += 4
jdx += 4
return s | goal-parser-interpretation | Python Solution | anandanshul001 | 0 | 2 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,238 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/2712026/Python-solution | class Solution:
def interpret(self, command: str) -> str:
res = ''
for i in range(len(command)):
if command[i] == 'G':
res += 'G'
elif command[i] == ')':
if command[i - 1] == '(':
res += 'o'
else:
res += 'al'
return res | goal-parser-interpretation | Python solution | michaelniki | 0 | 3 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,239 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/2700178/Python-Improved-Linear-Solution-94-Faster | class Solution:
def interpret(self, command: str) -> str:
new = ""
idx = 0;
while idx < len(command):
if command[idx]=="G":
new+="G"
elif command[idx:idx+2]=="()":
new+="o"
idx+=1
elif command[idx:idx+2]=="(a":
new+="al"
idx+=2
idx+=1
return new | goal-parser-interpretation | Python Improved Linear Solution 94% Faster | keioon | 0 | 2 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,240 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/2673690/Python-one-string-solution | class Solution:
def interpret(self, command: str) -> str:
return command.replace("()", "o").replace("(al)", "al") | goal-parser-interpretation | Python - one string solution | Anavatis | 0 | 19 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,241 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/2673661/Python-recursion-example | class Solution:
_mapping = {
"G": "G",
"()": "o",
"(al)": "al"
}
def interpret(self, command: str) -> str:
if not command:
return ""
result = ""
if symb := self._mapping.get(command[0]):
return symb + self.interpret(command[1:])
elif symb := self._mapping.get(command[0: 2]):
return symb + self.interpret(command[2:])
elif symb := self._mapping.get(command[0: 4]):
return symb + self.interpret(command[4:])
return result | goal-parser-interpretation | Python - recursion example | Anavatis | 0 | 11 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,242 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/2673627/Python-clear-solution | class Solution:
_mapping = {
"G": "G",
"()": "o",
"(al)": "al"
}
def interpret(self, command: str) -> str:
result = ""
i = 0
while i < len(command):
if symb := self._mapping.get(command[i]):
result += symb
i += 1
elif symb := self._mapping.get(command[i: i+2]):
result += symb
i += 2
else:
result += self._mapping.get("(al)")
i += 4
return result | goal-parser-interpretation | Python - clear solution | Anavatis | 0 | 13 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,243 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/2671605/Python-simple | class Solution:
def interpret(self, command: str) -> str:
i = 0
ans = ""
while i < len(command):
if command[i] == "G":
ans += "G"
i += 1
elif command[i] == '(':
if command[i + 1] == ")":
ans += "o"
i += 2
elif command[i + 1] == "a":
ans += "al"
i += 4
return ans | goal-parser-interpretation | Python simple | phantran197 | 0 | 2 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,244 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/2660281/Python-3-without-dictionary.....-Faster-than-99 | class Solution:
def interpret(self, command: str) -> str:
i=0
ans=""
while i!=len(command):
if command[i]=="G":
ans+=command[i]+""
i+=1
else:
if command[i+1]==")":
ans+="o"+""
i+=2
else:
ans+="al"+""
i+=4
return ans | goal-parser-interpretation | Python 3 without dictionary..... Faster than 99% | guneet100 | 0 | 20 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,245 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/2644856/Sliding-window-approach-to-solve-1678.-Goal-Parser-Interpretation | class Solution:
def interpret(self, command: str) -> str:
if len(command) == 1:
return command
interpretation_rules = {
"G": "G",
"()": "o",
"(al)": "al"
}
start, end = 0, 1
ans = ""
while end <= len(command):
current_substring = command[start: end]
if current_substring in interpretation_rules:
ans += interpretation_rules[current_substring]
start = end
end += 1
return ans | goal-parser-interpretation | Sliding window approach to solve 1678. Goal Parser Interpretation | akylzhanov_r | 0 | 1 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,246 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/2581683/Easy-Python-Solution | class Solution:
def interpret(self, command: str) -> str:
ans = ""
index = 0
while index<=len(command)-1:
if command[index] == "(":
if command[index+1] == "a":
ans+="al"
index+=4
continue
else:
ans+="o"
index+=2
continue
else:
ans+="G"
index+=1
continue
return ans | goal-parser-interpretation | Easy Python Solution | aniketbhamani | 0 | 33 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,247 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/2572411/python3-oror-Best-Solution | class Solution:
def interpret(self, command: str) -> str:
res = ""
i = 0
while i <len(command):
if command[i]=='G':
res+="G"
i+=1
elif command[i:i+2]=="()":
res+="o"
i+=2
else:
res+="al"
i+=4
return res | goal-parser-interpretation | python3 || Best Solution | shacid | 0 | 29 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,248 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/2524459/PYTHON-Simple-Clean-approach-beat-95-Easy-T%3A-O(N) | class Solution:
def interpret(self, command: str) -> str:
res = ""
prev= ''
for val in command:
if val=='(' or (val==')' and prev!='('):
prev=val
continue
if val==')' and prev=='(':
res+='o'
prev=val
continue
res+=val
prev = val
return res | goal-parser-interpretation | β
[PYTHON] Simple, Clean approach beat 95% Easy T: O(N) | girraj_14581 | 0 | 55 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,249 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/2490638/Simple-python-code-with-explanation | class Solution:
def interpret(self, command: str) -> str:
#create an empty string --> k
k = ""
#iterate over the indices of the string
for i in range(len(command)):
#if the curr val is "G"
if command[i] == "G":
#then add "G" to the string k
k = k + "G"
#if curr val is "(" and next val is ")"
elif command[i]== "(" and command[i+1] == ")":
#then add "o" to sting k
k = k + "o"
#if curr val is "(" and next val is "a" and third val is "l" and last val is ")"
elif command[i] == "(" and command[i+1] =="a" and command[i+2] == "l" and command[i+3] == ")":
#then add "al" to string k
k = k + "al"
#return the string k
return k | goal-parser-interpretation | Simple python code with explanation | thomanani | 0 | 34 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,250 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/2472622/python3-simple-solution-(rfind-replace) | class Solution:
def interpret(self, command: str) -> str:
if command.rfind('()') or command.rfind(' ()') :
command = command.replace('()', 'o')
else:
command = command
if command.rfind('(al)') or command.rfind(' (al)'):
command = command.replace('(al)', 'al')
else:
command = command
return command
``` | goal-parser-interpretation | python3 simple solution (rfind, replace) | katyasobol | 0 | 10 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,251 |
https://leetcode.com/problems/goal-parser-interpretation/discuss/2470338/Python-easy-to-understand-oror-90 | class Solution:
def interpret(self, command: str) -> str:
res = ""
i = 0
while i < len(command):
if command[i] == "G":
res += "G"
i += 1
elif command[i] == "(" and command[i+1] == ")":
res += "o"
i += 2
else:
res += "al"
i += 4
return res | goal-parser-interpretation | Python easy to understand || 90% | aruj900 | 0 | 32 | goal parser interpretation | 1,678 | 0.861 | Easy | 24,252 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/2005867/Python-Simple-One-Pass | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
counter = defaultdict(int)
count = 0
for x in nums:
comp = k - x
if counter[comp]>0:
counter[comp]-=1
count+=1
else:
counter[x] +=1
return count | max-number-of-k-sum-pairs | Python Simple One Pass | constantine786 | 2 | 181 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,253 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/2799298/Python3-Solution-or-Two-Pointers | class Solution:
def maxOperations(self, A, K):
A.sort()
ans, l, r = 0, 0, len(A) - 1
while l < r:
val = A[l] + A[r]
if val <= K: l += 1
if val >= K: r -= 1
if val == K: ans += 1
return ans | max-number-of-k-sum-pairs | Python3 Solution | Two Pointers | satyam2001 | 1 | 31 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,254 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/966022/Python-Hashmap-beats-97-speed-with-explanation | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
sol = {}
cnt = 0
for el in nums:
if el < k:
if el in sol:
sol[el] -= 1
cnt += 1
if sol[el] == 0:
sol.pop(el)
else:
sol[k-el] = sol.get(k-el, 0) + 1
return cnt | max-number-of-k-sum-pairs | [Python] Hashmap, beats 97% speed with explanation | modusV | 1 | 96 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,255 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/961612/Python-Simplest-Solution | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
nums.sort()
i=0;j=len(nums)-1
ans=0
while i<j:
Sum=nums[i]+nums[j]
if Sum==k:
i+=1
j-=1
ans+=1
elif Sum<k:
i+=1
else:
j-=1
return ans | max-number-of-k-sum-pairs | Python Simplest Solution | lokeshsenthilkumar | 1 | 209 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,256 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/961402/Python3-frequency-table | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
freq = {}
for x in nums: freq[x] = 1 + freq.get(x, 0)
ans = 0
for x, v in freq.items():
if k - x in freq:
if x == k - x: ans += freq[x]//2
elif x < k - x: ans += min(freq[x], freq[k-x])
return ans | max-number-of-k-sum-pairs | [Python3] frequency table | ye15 | 1 | 99 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,257 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/961402/Python3-frequency-table | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
nums.sort()
lo, hi = 0, len(nums)-1
ans = 0
while lo < hi:
if nums[lo] + nums[hi] < k: lo += 1
elif nums[lo] + nums[hi] > k: hi -= 1
else:
ans += 1
lo += 1
hi -= 1
return ans | max-number-of-k-sum-pairs | [Python3] frequency table | ye15 | 1 | 99 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,258 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/2802453/Python-solution-or-counter | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
res = 0
count = Counter(nums)
for key in count.keys():
pairs = k - key
if pairs in count:
if key == pairs:
product = count[key] // 2
count[key] = 0
else:
product = min(count[key], count[pairs])
count[key] = 0
count[pairs] = 0
res += product
return res | max-number-of-k-sum-pairs | Python solution | counter | maomao1010 | 0 | 1 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,259 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/2722336/python3-or-easy-hash-solution | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
freqDict = defaultdict(int)
for number in nums:
freqDict[number]+=1
ans = 0
for key in freqDict.keys():
if(k-key in freqDict):
ans+=min(freqDict[key],freqDict[k-key])
return int(ans//2) | max-number-of-k-sum-pairs | python3 | easy hash solution | ty2134029 | 0 | 4 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,260 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/2669599/Python-Solution-Using-Two-Pointers | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
nums.sort()
i=0
j=len(nums)-1
count=0
sum1=0
while(i<j):
sum1=nums[i]+nums[j]
if(sum1==k):
count+=1
i+=1
j-=1
elif(sum1<k):
i+=1
else:
j-=1
return(count) | max-number-of-k-sum-pairs | Python Solution Using Two Pointers | ankitr8055 | 0 | 2 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,261 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/2516927/Python-runtime-86.58-memory-92.16 | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
nums.sort()
ans = 0
lp = 0
rp = len(nums)-1
while lp < rp:
added = nums[lp] + nums[rp]
if added == k:
ans += 1
lp += 1
rp -= 1
elif added < k:
lp += 1
else:
rp -= 1
return ans | max-number-of-k-sum-pairs | Python, runtime 86.58%, memory 92.16% | tsai00150 | 0 | 29 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,262 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/2075157/Python-easy-to-read-and-understand-or-sorting-hashmap | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
nums.sort()
ans = 0
i, j = 0, len(nums)-1
while i < j:
sums = nums[i] + nums[j]
if sums == k:
ans += 1
i, j = i+1, j-1
elif sums > k:
j = j-1
else:
i = i+1
return ans | max-number-of-k-sum-pairs | Python easy to read and understand | sorting-hashmap | sanial2001 | 0 | 38 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,263 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/2075157/Python-easy-to-read-and-understand-or-sorting-hashmap | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
d = {}
ans = 0
for num in nums:
if k-num in d and d[k-num] > 0:
ans += 1
d[k-num] -= 1
elif num not in d:
d[num] = 1
else:
d[num] += 1
return ans | max-number-of-k-sum-pairs | Python easy to read and understand | sorting-hashmap | sanial2001 | 0 | 38 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,264 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/2008380/O(N)-Easy-python-solution-using-HashMap | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
d = {}
count = 0
for i in nums:
if d.get(k-i):
count += 1
d[k-i]-=1
elif i not in d:
d[i] = 1
else:
d[i] += 1
return count | max-number-of-k-sum-pairs | O(N) Easy python solution using HashMap | aashnachib17 | 0 | 20 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,265 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/2008357/Dumb-solution-but-high-efficiency-or-Beats-98-or-Python | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
hashmap={}
result=0
for i in nums:
if i in hashmap:
hashmap[i]+=1
else:
hashmap[i]=1
visited=set()
for i in hashmap.keys():
if i not in visited:
if i*2==k:
if hashmap[i]>1:
result+=hashmap[i]//2
visited.add(i)
elif (k-i)>-1 and k-i in hashmap:
result+=min(hashmap[i],hashmap[k-i])
visited.add(i)
visited.add(k-i)
return result | max-number-of-k-sum-pairs | Dumb solution but high efficiency | Beats 98% | Python | RickSanchez101 | 0 | 12 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,266 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/2008253/Python-or-using-dict-or-Time-O(N)-or-Space-O(N) | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
seen = {}
pairs = 0
for num in nums:
remainer = k - num
if seen.get(remainer, 0):
pairs += 1
seen[remainer] -= 1
else:
num_count = seen.get(num, 0)
seen[num] = num_count + 1
return pairs | max-number-of-k-sum-pairs | Python | using dict | Time O(N) | Space O(N) | user3694B | 0 | 10 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,267 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/2008209/Python-Counter-Clean-and-Simple! | class Solution:
def maxOperations(self, nums, k):
c = Counter(nums)
ops = 0
for num in nums:
target = k-num
if num != target and c[num] > 0 and c.get(target, 0) > 0:
ops += 1
c[target] -= 1
c[num] -= 1
elif num == target and c[num] >= 2:
ops += 1
c[num] -= 2
return ops | max-number-of-k-sum-pairs | Python - Counter - Clean and Simple! | domthedeveloper | 0 | 22 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,268 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/2008209/Python-Counter-Clean-and-Simple! | class Solution:
def maxOperations(self, nums, k):
ops = 0
c = Counter()
for num in nums:
target = k - num
if c.get(target, 0) > 0:
c[target] -= 1
ops += 1
else:
c[num] += 1
return ops | max-number-of-k-sum-pairs | Python - Counter - Clean and Simple! | domthedeveloper | 0 | 22 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,269 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/2008129/Python-or-2-Simple-Solutions-or-TC%3A-O(N)-SC%3A-O(N)-or-TC%3A-(N-Log-N)-SC%3A-O(1) | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
_map = defaultdict(lambda: 0)
count = 0
for i in range(len(nums)):
target = k - nums[i]
if _map[target] > 0:
count += 1
_map[target] -= 1
else:
_map[nums[i]] += 1
return count | max-number-of-k-sum-pairs | Python | 2 Simple Solutions | TC: O(N), SC: O(N) | TC: (N Log N) SC: O(1) | Crimsoncad3 | 0 | 14 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,270 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/2008129/Python-or-2-Simple-Solutions-or-TC%3A-O(N)-SC%3A-O(N)-or-TC%3A-(N-Log-N)-SC%3A-O(1) | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
nums.sort()
left = 0
right = len(nums) - 1
count = 0
while left < right:
val = nums[left] + nums[right]
if val == k:
count += 1
left += 1
right -= 1
elif val < k:
left += 1
else:
right -= 1
return count | max-number-of-k-sum-pairs | Python | 2 Simple Solutions | TC: O(N), SC: O(N) | TC: (N Log N) SC: O(1) | Crimsoncad3 | 0 | 14 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,271 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/2008024/Simple-Python-Solution-Using-Counter()-oror-Explained | class Solution(object):
def maxOperations(self, nums, k):
d = Counter(nums)
val = 0
for ele in nums:
if k-ele in d:
if k-ele != ele and d[k-ele]>=1 and d[ele]>=1:
d[k-ele]-=1
d[ele]-=1
val+=1
elif d[k-ele]>1 and d[ele]>1:
d[k-ele]-=1
d[ele]-=1
val+=1
return val | max-number-of-k-sum-pairs | Simple Python Solution Using Counter() || Explained | NathanPaceydev | 0 | 11 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,272 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/2007915/Python-easy-solution-without-hashmaps | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
nums.sort()
left = 0
right = len(nums) - 1
res = 0
while left < right:
if nums[left] + nums[right] == k:
res += 1
left += 1
right -= 1
elif nums[left] + nums[right] < k:
left += 1
else:
right -= 1
return res | max-number-of-k-sum-pairs | Python easy solution without hashmaps | alishak1999 | 0 | 12 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,273 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/2007452/Python3-Solution-with-using-hashmap | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
d = collections.defaultdict(int)
res = 0
for num in nums:
if d[k - num] == 0:
d[num] += 1
else:
d[k - num] -= 1
res += 1
return res | max-number-of-k-sum-pairs | [Python3] Solution with using hashmap | maosipov11 | 0 | 7 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,274 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/2007370/Python3-twoPointer-and-hash-faster-than-99.78 | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
numFreq=dict(collections.Counter(nums))
cnt=0
for x, freq in numFreq.items():
if x*2==k:
cnt+=numFreq[x]//2
else:
if x*2 <k and (k-x) in numFreq:
cnt += min(freq, numFreq[k-x])
return cnt | max-number-of-k-sum-pairs | Python3 twoPointer & hash, faster than 99.78% | zhaoran | 0 | 9 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,275 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/2007370/Python3-twoPointer-and-hash-faster-than-99.78 | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
n=len(nums)
nums.sort()
l, r = 0, n-1
cnt=0
while l<r:
if nums[l]+nums[r]==k:
cnt+=1
l+=1
r-=1
elif nums[l]+nums[r]<k:
l+=1
else:
r-=1
return cnt | max-number-of-k-sum-pairs | Python3 twoPointer & hash, faster than 99.78% | zhaoran | 0 | 9 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,276 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/2007125/Python-Solution-using-Hashmap-or-Easy-to-understand-with-comments | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
ans = 0
# We sort and filter all elements < k - since to to form k you need numbers less than k
# Also min(nums) = 1 is given in Constraint
nums.sort()
nums = [c for c in nums if c < k]
# Creating a counter for nums
f = defaultdict(int)
for c in nums:
f[c] += 1
"""
As we are removing elements we decrease the count in the hashmap
Note: Removing elements from the array is very expensive, and will result in TLE.
"""
for c in nums:
diff = k - c
# Consider finding the pair only if the element hasn't been already paired-up
if f[c] > 0:
# (c, diff) is the pair we want to remove, and we consider the 2 cases possible
if c == diff:
if f[diff] >= 2:
f[diff] -= 2
ans += 1
elif c != diff:
if f[diff] > 0:
f[diff] -= 1
f[c] -= 1
ans += 1
return ans | max-number-of-k-sum-pairs | Python Solution using Hashmap | Easy to understand with comments | cppygod | 0 | 5 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,277 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/2006932/python-3-oror-hash-map-solution-oror-O(n)O(n) | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
counter = collections.defaultdict(lambda: 0)
res = 0
for num in nums:
if counter[k - num]:
counter[k - num] -= 1
res += 1
else:
counter[num] += 1
return res | max-number-of-k-sum-pairs | python 3 || hash map solution || O(n)/O(n) | dereky4 | 0 | 24 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,278 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/2006740/python-java-easy-and-fast-hash-solution-(Time-On-space-On) | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
if k == 1 : return 0
numbers = dict()
for n in nums :
if n < k :
if n in numbers : numbers[n] = numbers[n] + 1
else : numbers[n] = 1
lim = (k+1)>>1
answer = 0
for n in numbers :
if n < lim and (k - n) in numbers :
answer += min(numbers[n], numbers[k - n])
if (k&1) == 0 and lim in numbers :
answer += numbers[lim]>>1
return answer | max-number-of-k-sum-pairs | python, java - easy & fast hash solution (Time On, space On) | ZX007java | 0 | 9 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,279 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/2006615/Python3-or-Easy-or-One-Pass-or-Clean | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
count = defaultdict(lambda: 0)
answer = 0
for num in nums:
other = k-num
if count[other] > 0:
count[other] -= 1
answer += 1
else:
count[num] += 1
return answer | max-number-of-k-sum-pairs | Python3 | Easy | One Pass | Clean | suhrid | 0 | 20 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,280 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/2006527/Python-dictionary-O(n)-fast-solution | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
"""
Time: O(n)
Space: O(n)
"""
d = {}
for x in nums:
if x in d:
d[x] += 1
else:
d[x] = 1
res = 0
for num, f in d.items():
if k - num in d and d[num] and d[k-num]:
if num == k - num:
res += d[num]//2
else:
res += min(d[num], d[k-num])
d[num] = d[k-num] = 0
d[num] = 0
return res | max-number-of-k-sum-pairs | Python dictionary O(n) fast solution | belal_bh | 0 | 19 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,281 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/2006514/Python-oror-Efficient-oror-short-oror-clean-oror-Easy-oror-solution-oror | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
c=Counter(nums)
ans=0
seen=set()
for x in c:
if x not in seen and (k-x) in c:
if x==(k-x):
ans+=c[x]//2
else:
ans+=min(c[x],c[k-x])
seen.add(x)
seen.add(k-x)
return ans | max-number-of-k-sum-pairs | Python || Efficient || short || clean || Easy || solution || | Debajyoti-Shit | 0 | 15 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,282 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/2006468/easy-python-code-used-binary-search | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
nums.sort()
def binary_search(arr, x):
low = 0
high = len(arr) - 1
mid = 0
while low <= high:
mid = (high + low) // 2
if arr[mid] < x:
low = mid + 1
elif arr[mid] > x:
high = mid - 1
else:
return mid
return -1
count = 0
while(len(nums)>1):
n = nums[0]
nums.pop(0)
m = binary_search(nums, k-n)
if m != -1:
nums.pop(m)
count += 1
return count | max-number-of-k-sum-pairs | easy python code, used binary search | dakash682 | 0 | 14 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,283 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/2006423/Simple-python-solution-O(n) | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
count = Counter(nums)
ans = 0
for i in count:
ans += min(count[i], count[k - i])
return ans//2 | max-number-of-k-sum-pairs | Simple python solution - O(n) | saladino | 0 | 6 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,284 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/2006386/Clean-Python-HashmapCounter-Set-based-While-Loop | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
from collections import Counter
count = Counter(nums)
numbers = set(count.keys()) # make a copy of keys that we can manipulate
ans = 0
while numbers:
v = numbers.pop()
if k-v == v:
ans += count[v] // 2
elif k-v in numbers:
numbers.remove(k-v) # we don't need to visit k-v anymore
ans += min(count[v], count[k - v] )
return ans | max-number-of-k-sum-pairs | Clean Python, Hashmap/Counter, Set-based While Loop | boris17 | 0 | 9 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,285 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/2006350/Python3-Easy-Dictionary-Solution | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
ans = 0
m = defaultdict(int) # Initializes new keys with value = 0
for i in nums:
if k-i in m:
ans += 1
m[k-i] -= 1
if m[k-i] == 0:
m.pop(k-i)
else:
m[i] += 1
return ans | max-number-of-k-sum-pairs | Python3 Easy Dictionary Solution | kewinMalone | 0 | 7 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,286 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/2006318/O(n)-solution-in-Python-3-inspired-by-classic-Two-sum | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
from collections import defaultdict
ans = 0
cnt = defaultdict(int)
for i in nums:
if cnt[k - i]:
# t is the number of currently available pair (i, k - i)
t = min(cnt[i] + 1, cnt[k - i])
ans += t
# in case i apears again since nums is not sorted,
# we need to update the number of available k - i
cnt[k - i] -= t
else:
# otherwise record i if k - i has not yet appeared
cnt[i] += 1
return ans | max-number-of-k-sum-pairs | O(n) solution in Python 3, inspired by classic Two-sum | mousun224 | 0 | 8 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,287 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/2006020/Python3-solution | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
c = collections.Counter()
ans = 0
for n in nums:
if c[k-n] > 0:
c[k-n] -= 1
ans += 1
else:
c[n] += 1
return ans | max-number-of-k-sum-pairs | Python3 solution | dalechoi | 0 | 15 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,288 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/2006019/Python-Two-Solution-oror-HashMap-O(n)-oror-Two-Pointers-O(nlogn) | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
dict = {}
for val in nums:
if val in dict:
dict[val] += 1
else:
dict[val] = 1
ans = 0
for val in nums:
diff = k-val
if diff in dict and val in dict:
if diff == val and dict[diff] > 1:
ans += 1
dict[diff] -= 2
elif diff != val:
ans += 1
dict[diff] -= 1
dict[val] -= 1
if dict[diff] == 0:
del dict[diff]
if dict[val] == 0:
del dict[val]
return ans | max-number-of-k-sum-pairs | Python - Two Solution || HashMap O(n) || Two Pointers O(nlogn) | gamitejpratapsingh998 | 0 | 13 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,289 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/2006019/Python-Two-Solution-oror-HashMap-O(n)-oror-Two-Pointers-O(nlogn) | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
nums.sort()
i,j = 0,len(nums)-1
ans = 0
while i < j:
target = nums[i]+nums[j]
if k == target:
ans += 1
i += 1
j -= 1
elif target > k:
j -= 1
else:
i += 1
return ans | max-number-of-k-sum-pairs | Python - Two Solution || HashMap O(n) || Two Pointers O(nlogn) | gamitejpratapsingh998 | 0 | 13 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,290 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/2006013/Python-with-explanation-O(n)-time | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
pair_map, operations = defaultdict(int), 0
for num in nums:
#If pair found increment operations
if pair_map[num]:
operations += 1
pair_map[num] -= 1
else:
pair_map[k- num] += 1
return operations | max-number-of-k-sum-pairs | Python with explanation - O(n) time | firefist07 | 0 | 15 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,291 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/2005942/Python-using-defaultdict(int)-single-pass | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
count = defaultdict(int)
result = 0
for n in nums:
if count.get(k - n):
count[k - n] -= 1
result += 1
else:
count[n] += 1
return result | max-number-of-k-sum-pairs | Python, using defaultdict(int), single pass | blue_sky5 | 0 | 9 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,292 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/1703868/Python-O(n)-time-O(n)-space-solution-O(nlogn)-time-O(1)-space-solution-(timespace-tradeoff) | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
n = len(nums)
counts = collections.Counter(nums)
res = 0
for i in range(n):
num = nums[i]
if counts[num] == 0:
pass
elif num in counts:
if k == 2*num:
if counts[num] >= 2:
counts[num] -= 2
res += 1
if counts[num] == 0:
del counts[num]
else:
pass
else:
if k-num in counts:
counts[num] -= 1
counts[k-num] -= 1
if counts[num] == 0:
del counts[num]
if counts[k-num] == 0:
del counts[k-num]
res += 1
return res | max-number-of-k-sum-pairs | Python O(n) time, O(n) space solution / O(nlogn) time, O(1) space solution (time/space tradeoff) | byuns9334 | 0 | 96 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,293 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/1703868/Python-O(n)-time-O(n)-space-solution-O(nlogn)-time-O(1)-space-solution-(timespace-tradeoff) | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
n = len(nums)
nums.sort()
res = 0
i, j = 0, n-1
while i < j:
if nums[i] + nums[j] == k:
res += 1
i += 1
j -= 1
elif nums[i] + nums[j] > k:
j -= 1
else: # nums[i] + nums[j] < k
i +=1
return res | max-number-of-k-sum-pairs | Python O(n) time, O(n) space solution / O(nlogn) time, O(1) space solution (time/space tradeoff) | byuns9334 | 0 | 96 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,294 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/1397485/hashmap-two-sum-problem-with-discard-elements-case | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
res = 0
seen_fre = Counter()
for x in nums:
if seen_fre[k-x]:
res += 1
seen_fre[k-x] -= 1
else: # THIS `ELSE` coz if match, BOTH are discarded!!
seen_fre[x] += 1
return res | max-number-of-k-sum-pairs | hashmap, two sum problem, with discard elements case | yozaam | 0 | 67 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,295 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/961437/Python-Super-easy-to-understand-100-fast | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
d=collections.Counter(nums)
c=0
for i in nums:
if(d[i]>0):
d[i]-=1
if(d[k-i]>0):
d[k-i]-=1
c+=1
return c | max-number-of-k-sum-pairs | Python Super easy to understand 100% fast | jatindscl | 0 | 102 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,296 |
https://leetcode.com/problems/max-number-of-k-sum-pairs/discuss/961419/Python-two-pointer-solution | class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
ans = 0
if not nums or len(nums) == 1:
return ans
nums.sort()
l = 0
r = len(nums) - 1
while l < r:
s = nums[l] + nums[r]
if s == k:
ans += 1
l +=1
r -=1
elif s > k:
r -= 1
else:
l += 1
return ans | max-number-of-k-sum-pairs | Python two pointer solution | kimchi_boy | 0 | 66 | max number of k sum pairs | 1,679 | 0.573 | Medium | 24,297 |
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/2612717/python-one-line-solution-94-beats | class Solution:
def concatenatedBinary(self, n: int) -> int:
return int("".join([bin(i)[2:] for i in range(1,n+1)]),2)%(10**9+7) | concatenation-of-consecutive-binary-numbers | python one line solution 94% beats | benon | 5 | 677 | concatenation of consecutive binary numbers | 1,680 | 0.57 | Medium | 24,298 |
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/2613843/Python-Simple-Python-Solution-Using-Bin-Function | class Solution:
def concatenatedBinary(self, n: int) -> int:
binary = ''
for num in range(1, n+1):
binary = binary + bin(num)[2:]
result = int(binary, 2)
return result % (10**9 + 7) | concatenation-of-consecutive-binary-numbers | [ Python ] β
β
Simple Python Solution Using Bin Function π₯³βπ | ASHOK_KUMAR_MEGHVANSHI | 3 | 167 | concatenation of consecutive binary numbers | 1,680 | 0.57 | Medium | 24,299 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.