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/keyboard-row/discuss/1682705/Python3-84-faster-with-explanation | class Solution:
def findWords(self, words: List[str]) -> List[str]:
first, second, third, rlist = 'qwertyuiopQWERTYUIOP', "asdfghjklASDFGHJKL", "zxcvbnmZXCVBNM", []
for word in words:
one, two, three = 1, 1, 1
for letter in word:
#print(letter, word)
... | keyboard-row | Python3, 84% faster with explanation | cvelazquez322 | 0 | 111 | keyboard row | 500 | 0.692 | Easy | 8,800 |
https://leetcode.com/problems/keyboard-row/discuss/1582035/sb-question's-python-solution | class Solution:
def findWords(self, words: List[str]) -> List[str]:
row1,row2,row3 = set("qwertyuiop"),set("asdfghjkl"),set("zxcvbnm")
keyboard = [row1,row2,row3]
output = []
for word in words:
can_add = True
for row in keyboard:
if word[0].low... | keyboard-row | sb question's python solution | yingziqing123 | 0 | 42 | keyboard row | 500 | 0.692 | Easy | 8,801 |
https://leetcode.com/problems/keyboard-row/discuss/1569057/Python-faster-than-95 | class Solution:
def findWords(self, words: List[str]) -> List[str]:
rows = [{'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p',
'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P'},
{'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l',
'A', 'S', 'D', 'F', 'G', 'H', 'J',... | keyboard-row | Python faster than 95% | dereky4 | 0 | 214 | keyboard row | 500 | 0.692 | Easy | 8,802 |
https://leetcode.com/problems/keyboard-row/discuss/1542770/Python-easy-solution-32-ms | class Solution:
def findWords(self, words: List[str]) -> List[str]:
row1 = set('qwertyuiop')
row2 = set('asdfghjkl')
row3 = set('zxcvbnm')
output = []
for word in words:
tempWord = word.lower()
if tempWord[0] in row1:
alter = False
... | keyboard-row | Python easy solution 32 ms | akshaykumar19002 | 0 | 72 | keyboard row | 500 | 0.692 | Easy | 8,803 |
https://leetcode.com/problems/keyboard-row/discuss/1150253/Python3-very-easy-to-understand-(Bonus-annotation)28-ms-faster-than-78.08 | class Solution:
def findWords(self, words: List[str]) -> List[str]:
a={"qwertyuiop":1,"asdfghjkl":2,"zxcvbnm":3}
res=[]
for word in words:#word= Hello Alaska Dad Peace
c=""
for wor in word:#h e l l o
for aa in a:#qwe asd zxc 1,2,3
i... | keyboard-row | Python3 very easy to understand (Bonus annotation)28 ms, faster than 78.08% | lin11116459 | 0 | 93 | keyboard row | 500 | 0.692 | Easy | 8,804 |
https://leetcode.com/problems/keyboard-row/discuss/1013055/Simple-solution-with-Explanation%3A-Faster-than-98.66 | class Solution:
def findWords(self, words: List[str]) -> List[str]:
l=["qwertyuiop","asdfghjkl","zxcvbnm"] #This list contains alphabets which are in one row of keyboard , total three rows, hence three strings
l1=[]
'''
i.)We now check if all the letter in word is present in a row or not
... | keyboard-row | Simple solution with Explanation: Faster than 98.66% | thisisakshat | 0 | 82 | keyboard row | 500 | 0.692 | Easy | 8,805 |
https://leetcode.com/problems/keyboard-row/discuss/932874/Python3%3A-Simple-Efficient-Solution | class Solution:
def findWords(self, words: List[str]) -> List[str]:
top = "qwertyuiop"
mid = "asdfghjkl"
low = "zxcvbnm"
sol = []
for word in words:
startingChar = word[0]
if startingChar.lower() in top:
if self.validator(word, top):
... | keyboard-row | Python3: Simple Efficient Solution | MakeTeaNotWar | 0 | 83 | keyboard row | 500 | 0.692 | Easy | 8,806 |
https://leetcode.com/problems/keyboard-row/discuss/686668/Python-solution-using-Any-and-All | class Solution:
def possible(self,word):
return any( all(i in x for i in word ) for x in [ 'qwertyuiop', 'asdfghjkl', 'zxcvbnm'] )
def findWords(self, words: List[str]) -> List[str]:
return [ word for word in words if self.possible(word.lower()) ] | keyboard-row | Python solution using Any and All | Trevahok | 0 | 44 | keyboard row | 500 | 0.692 | Easy | 8,807 |
https://leetcode.com/problems/keyboard-row/discuss/447055/Python3-simple-solution-using-a-dictionary-runtime%3A-16-ms-faster-than-99.92 | class Solution:
def findWords(self, words: List[str]) -> List[str]:
keyboard = {
"q":1,
"w":1,
"e":1,
"r":1,
"t":1,
"y":1,
"u":1,
"i":1,
"o":1,
"p":1,
"a":2,
"s":2,
"d":2,
"f":2,
"g":2,
"h":2,
"j":2,
"k":2,
"l":2,
"z":3,
"x":3,
"c":3,
"v":3,
"b"... | keyboard-row | Python3 simple solution using a dictionary, runtime: 16 ms, faster than 99.92% | jb07 | 0 | 95 | keyboard row | 500 | 0.692 | Easy | 8,808 |
https://leetcode.com/problems/keyboard-row/discuss/299373/Easy-understand-solution-with-Python3-set-method | class Solution:
def findWords(self, words: List[str]) -> List[str]:
res=[]
dic1={'q','w','e','r','t','y','u','i','o','p','Q','W','E','R','T','Y','U','I','O','P'}
dic2={'a','s','d','f','g','h','j','k','l','A','S','D','F','G','H','J','K','L'}
dic3={'z','x','c','v','b','n','m','Z','X','C','V','B','N','M'}... | keyboard-row | Easy understand solution with Python3 set method | JasperZhou | 0 | 72 | keyboard row | 500 | 0.692 | Easy | 8,809 |
https://leetcode.com/problems/keyboard-row/discuss/380302/Solution-in-Python-3-(beats-~99)-(one-line) | class Solution:
def findWords(self, w: List[str]) -> List[str]:
return [i for i in w if any([set(i.lower()).issubset(set(j)) for j in ['qwertyuiop','asdfghjkl','zxcvbnm']])] | keyboard-row | Solution in Python 3 (beats ~99%) (one line) | junaidmansuri | -2 | 325 | keyboard row | 500 | 0.692 | Easy | 8,810 |
https://leetcode.com/problems/keyboard-row/discuss/380302/Solution-in-Python-3-(beats-~99)-(one-line) | class Solution:
def findWords(self, w: List[str]) -> List[str]:
L = [set('qwertyuiop'),set('asdfghjkl'),set('zxcvbnm')]
D, A = {j:i for i in range(3) for j in L[i]}, []
for i,j in enumerate([i.lower() for i in w]):
if set(j).issubset(L[D[j[0]]]): A.append(w[i])
return A
- Junaid Mans... | keyboard-row | Solution in Python 3 (beats ~99%) (one line) | junaidmansuri | -2 | 325 | keyboard row | 500 | 0.692 | Easy | 8,811 |
https://leetcode.com/problems/find-mode-in-binary-search-tree/discuss/1722526/Python3-Inorder-traversal-beats-99 | class Solution:
def findMode(self, root: Optional[TreeNode]) -> List[int]:
def traverse(root: TreeNode) -> None:
if not root:
return
nonlocal maxcount, count, prevval, modes
traverse(root.left)
... | find-mode-in-binary-search-tree | [Python3] Inorder traversal; beats 99% | cwkirby | 2 | 90 | find mode in binary search tree | 501 | 0.487 | Easy | 8,812 |
https://leetcode.com/problems/find-mode-in-binary-search-tree/discuss/2800423/Python-beats-82(Simple-iterative-solution) | class Solution:
def findMode(self, root: Optional[TreeNode]) -> List[int]:
if root.val == 0: return [0]
nums, stack = {}, []
while True:
while root:
if root.val in nums.keys(): nums[root.val] += 1
else: nums[root.val] = 1
... | find-mode-in-binary-search-tree | Python beats 82%(Simple iterative solution) | farruhzokirov00 | 1 | 166 | find mode in binary search tree | 501 | 0.487 | Easy | 8,813 |
https://leetcode.com/problems/find-mode-in-binary-search-tree/discuss/2389747/Easy-Python-Solution-TC-greater-99-oror-SC-O(N) | class Solution:
def __init__(self):
self.d = {}
self.m = 1
self.ans = []
def dfs(self, x):
if not x:
return
if x.val not in self.d:
self.d[x.val] = 1
else:
self.d[x.val] += 1
self.m = max(self.m, self.d[... | find-mode-in-binary-search-tree | Easy Python Solution TC > 99% || SC O(N) | Racernigga | 1 | 116 | find mode in binary search tree | 501 | 0.487 | Easy | 8,814 |
https://leetcode.com/problems/find-mode-in-binary-search-tree/discuss/2235776/Python3-Easy-to-Understand-or-Fast | class Solution:
def findMode(self, root: Optional[TreeNode]) -> List[int]:
modes = {}
def dfs(node):
if not node: return node
modes[node.val] = modes.get(node.val, 0) + 1
dfs(node.left)
dfs(node.right)
... | find-mode-in-binary-search-tree | ✅Python3 - Easy to Understand | Fast | thesauravs | 1 | 52 | find mode in binary search tree | 501 | 0.487 | Easy | 8,815 |
https://leetcode.com/problems/find-mode-in-binary-search-tree/discuss/2794722/easy-python-solution | class Solution:
def findMode(self, root: Optional[TreeNode]) -> List[int]:
numCount = {}
self.dfs(root,numCount)
maxCount = max(numCount.values())
return [k for k,v in numCount.items() if v == maxCount]
def dfs(self,root,numCount):
if root is None:
return
... | find-mode-in-binary-search-tree | easy python solution | betaal | 0 | 4 | find mode in binary search tree | 501 | 0.487 | Easy | 8,816 |
https://leetcode.com/problems/find-mode-in-binary-search-tree/discuss/2762474/Python-and-Golang-Solution | class Solution:
def findMode(self, root: Optional[TreeNode]) -> List[int]:
result = []
mode = {}
for val in self.inorder(root):
mode[val] = mode.get(val, 0) + 1
most_frequent_count = max(mode.values())
for key, val in mode.items():
if val == most_frequent_count:
result.append(key)
re... | find-mode-in-binary-search-tree | Python and Golang Solution | namashin | 0 | 6 | find mode in binary search tree | 501 | 0.487 | Easy | 8,817 |
https://leetcode.com/problems/find-mode-in-binary-search-tree/discuss/2709576/Simple-dictionary-list-and-dfs-solution-using-Python | class Solution:
def findMode(self, root: Optional[TreeNode]) -> List[int]:
b = []
def dfs(n):
if n:
b.append(n.val)
if n.left:
dfs(n.left)
if n.right:
dfs(n.right)
dfs(root)
dic = {}
for i in ... | find-mode-in-binary-search-tree | Simple dictionary, list and dfs solution using Python | spraj_123 | 0 | 2 | find mode in binary search tree | 501 | 0.487 | Easy | 8,818 |
https://leetcode.com/problems/find-mode-in-binary-search-tree/discuss/2701113/Python3-oror-Easy-Understanding-oror-Simple-Solution-oror-O(N) | class Solution:
def findMode(self, root: Optional[TreeNode]) -> List[int]:
dict = {}
def inordertraverse(root):
if root:
inordertraverse(root.left)
if(root.val in dict):
dict[root.val] += 1
else:
dict... | find-mode-in-binary-search-tree | Python3 || Easy Understanding || Simple Solution || O(N) | ahamedmusadiq_-12 | 0 | 1 | find mode in binary search tree | 501 | 0.487 | Easy | 8,819 |
https://leetcode.com/problems/find-mode-in-binary-search-tree/discuss/2480380/faster-than-98.95-of-Python3-online-submissions | class Solution:
def findMode(self, root: Optional[TreeNode]) -> List[int]:
def chk_none(node):
if node.left is None and node.right is None:
return
def chk_mode(root):
if root.val not in dic:
dic[root.val]=1
else:
dic... | find-mode-in-binary-search-tree | faster than 98.95% of Python3 online submissions | Abhiishekk | 0 | 29 | find mode in binary search tree | 501 | 0.487 | Easy | 8,820 |
https://leetcode.com/problems/find-mode-in-binary-search-tree/discuss/2338034/Python-3-easy-resursive-solution | class Solution:
def findMode(self, root: Optional[TreeNode]) -> List[int]:
dct = {}
def fm(rt):
nonlocal dct
if rt==None:
return
dct[rt.val] = dct.get(rt.val,0)
dct[rt.val]+=1
fm(rt.left)
fm(rt.right)
fm(... | find-mode-in-binary-search-tree | Python 3 easy resursive solution | sanzid | 0 | 37 | find mode in binary search tree | 501 | 0.487 | Easy | 8,821 |
https://leetcode.com/problems/find-mode-in-binary-search-tree/discuss/2313263/Recursive-Python-Solution | class Solution:
def findMode(self, root: Optional[TreeNode]) -> List[int]:
final_mode, max_freq = root.val, 1
dict_nums = {}
def check_tree(node, dict_nums):
if not node: return dict_nums
if node.val in dict_nums: dict_nums[node.val] += 1
... | find-mode-in-binary-search-tree | Recursive Python Solution | zip_demons | 0 | 76 | find mode in binary search tree | 501 | 0.487 | Easy | 8,822 |
https://leetcode.com/problems/find-mode-in-binary-search-tree/discuss/2064658/Python-or-dfs-or-S(N)-is-O(n)-but-easy-to-follow | class Solution:
def findMode(self, root: Optional[TreeNode]) -> List[int]:
self.h = defaultdict(dict)
def dfs(node):
if not node: return
self.h[node.val] = self.h.get(node.val, 0) + 1
dfs(node.left)
dfs(node.right)
return
... | find-mode-in-binary-search-tree | Python | dfs | S(N) is O(n) but easy to follow | Ploypaphat | 0 | 113 | find mode in binary search tree | 501 | 0.487 | Easy | 8,823 |
https://leetcode.com/problems/find-mode-in-binary-search-tree/discuss/1923092/Python-DFS-%2B-Dictionary | class Solution:
def findMode(self, root: Optional[TreeNode]) -> List[int]:
m = {}
def dfs(tree):
if not tree:
return
if tree.val in m:
m[tree.val] += 1
else:
m[tree.val] = 1
... | find-mode-in-binary-search-tree | Python DFS + Dictionary | Hejita | 0 | 103 | find mode in binary search tree | 501 | 0.487 | Easy | 8,824 |
https://leetcode.com/problems/find-mode-in-binary-search-tree/discuss/1866383/91.93-faster-easy-python-solution | class Solution:
def findMode(self, root: Optional[TreeNode]) -> List[int]:
q = [root]
level = []
dd = {root.val: 1}
while q and root:
for i in q:
if i.left:
level.append(i.left)
dd[i.left.val] = 1 + dd.get(i.left.val... | find-mode-in-binary-search-tree | 91.93% faster easy python solution | ankurbhambri | 0 | 152 | find mode in binary search tree | 501 | 0.487 | Easy | 8,825 |
https://leetcode.com/problems/find-mode-in-binary-search-tree/discuss/1522026/Python3-solution | class Solution:
def findMode(self, root: Optional[TreeNode]) -> List[int]:
self.modes = []
self.val = None
self.mode = 0
self.count = 0
def find(root):
if not root:
return
find(root.left)
if self.val == None:
... | find-mode-in-binary-search-tree | Python3 solution | EklavyaJoshi | 0 | 106 | find mode in binary search tree | 501 | 0.487 | Easy | 8,826 |
https://leetcode.com/problems/find-mode-in-binary-search-tree/discuss/792998/Python3-simple-solution | class Solution:
def findMode(self, root: TreeNode) -> List[int]:
if not root:
return None
queue = deque([root])
ans=[]
ans.append(root.val)
while queue:
node = queue.pop()
if node.left:
ans.app... | find-mode-in-binary-search-tree | Python3 simple solution | Geeky-star | 0 | 166 | find mode in binary search tree | 501 | 0.487 | Easy | 8,827 |
https://leetcode.com/problems/find-mode-in-binary-search-tree/discuss/710958/Python3-Extendedsimple-solution | class Solution:
def findMode(self, root: TreeNode) -> List[int]:
arr = self.traverseInOrder(root,[])
if arr==None:
return
else:
#forming hashmap/dictionary to get frequencies of node values
obj = {}
res = []
for i in range(len(arr)):
... | find-mode-in-binary-search-tree | Python3 Extended,simple solution | mn0960 | 0 | 153 | find mode in binary search tree | 501 | 0.487 | Easy | 8,828 |
https://leetcode.com/problems/find-mode-in-binary-search-tree/discuss/638513/Python-O(-n-)-by-in-order-DFS-w-Comment | class Solution:
def findMode(self, root: TreeNode) -> List[int]:
# record for previous node's value, record for accumulated occurrence
self.prev_val, self.occurrence = None, 1
# list to collect mode values, record for mode value's occurrence
self.mode, self.mode_occ = [], 0... | find-mode-in-binary-search-tree | Python O( n ) by in-order DFS [w/ Comment] | brianchiang_tw | 0 | 470 | find mode in binary search tree | 501 | 0.487 | Easy | 8,829 |
https://leetcode.com/problems/find-mode-in-binary-search-tree/discuss/546595/Python-simple-solution-44-ms-faster-than-91.67-O(n)-complexity | class Solution(object):
def findMode(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
if root is None:
return []
def get_value(values_dict, root):
if root is not None:
value = root.val
if value not in... | find-mode-in-binary-search-tree | Python simple solution 44 ms, faster than 91.67%, O(n) complexity | hemina | 0 | 463 | find mode in binary search tree | 501 | 0.487 | Easy | 8,830 |
https://leetcode.com/problems/find-mode-in-binary-search-tree/discuss/972326/Python3-Solution-using-Traversal-and-Heap-95-Faster | class Solution:
def findMode(self, root: TreeNode) -> List[int]:
dic = dict()
if root is None:
return []
def inorder(root,dic):
if root is None:
return
else:
left = inorder(root.left,dic)
if root.val in dic:
... | find-mode-in-binary-search-tree | Python3 Solution using Traversal and Heap 95% Faster | tgoel219 | -1 | 240 | find mode in binary search tree | 501 | 0.487 | Easy | 8,831 |
https://leetcode.com/problems/ipo/discuss/1492025/Python3-greedy | class Solution:
def findMaximizedCapital(self, k: int, w: int, profits: List[int], capital: List[int]) -> int:
capital, profits = zip(*sorted(zip(capital, profits)))
i = 0
pq = []
for _ in range(k):
while i < len(capital) and capital[i] <= w:
heappush(p... | ipo | [Python3] greedy | ye15 | 1 | 144 | ipo | 502 | 0.45 | Hard | 8,832 |
https://leetcode.com/problems/ipo/discuss/2747429/Two-Heaps-Fast-and-Easy-Solution | class Solution:
def findMaximizedCapital(self, k: int, w: int, profits: List[int], capital: List[int]) -> int:
max_profit = []
min_capital = []
for a, b in zip(capital, profits):
if a <= w:
heappush(max_profit, -b)
else:
heappu... | ipo | Two Heaps - Fast and Easy Solution | user6770yv | 0 | 9 | ipo | 502 | 0.45 | Hard | 8,833 |
https://leetcode.com/problems/ipo/discuss/1405696/Dictionary-of-sorted-profits-96-speed | class Solution:
def findMaximizedCapital(self, k: int, w: int, profits: List[int], capital: List[int]) -> int:
capital_profit = defaultdict(list)
for p, c in zip(profits, capital):
insort_left(capital_profit[c], p)
sorted_capital = sorted(capital_profit.keys())
if sorted_... | ipo | Dictionary of sorted profits, 96% speed | EvgenySH | 0 | 133 | ipo | 502 | 0.45 | Hard | 8,834 |
https://leetcode.com/problems/ipo/discuss/2624891/Clean-Simple-Python3-or-Sorting-and-Heap-or-O(n-log(n)) | class Solution:
def findMaximizedCapital(self, k: int, w: int, profits: List[int], capital: List[int]) -> int:
projects = sorted([(c, p) for p, c in zip(profits, capital)])
i, N = 0, len(projects)
profits_maxheap = []
cur_capital = w
while k > 0:
while i < N and c... | ipo | Clean, Simple Python3 | Sorting & Heap | O(n log(n)) | ryangrayson | -1 | 14 | ipo | 502 | 0.45 | Hard | 8,835 |
https://leetcode.com/problems/next-greater-element-ii/discuss/2520585/Python-Stack-98.78-faster-or-Simplest-solution-with-explanation-or-Beg-to-Adv-or-Monotonic-Stack | class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
stack, res = [], [-1] * len(nums) # taking an empty stack for storing index, a list with the lenght same as of nums so that we wont add unnecessary elements.
# [-1] * len(nums) = this will produce a list with len of nums an... | next-greater-element-ii | Python Stack 98.78% faster | Simplest solution with explanation | Beg to Adv | Monotonic Stack | rlakshay14 | 3 | 279 | next greater element ii | 503 | 0.631 | Medium | 8,836 |
https://leetcode.com/problems/next-greater-element-ii/discuss/2182797/Python3-OnePass-O(n)-oror-O(n)-Runtime%3A-323ms-48.44-Memory%3A-15.7mb-65.00 | class Solution:
# O(n) || O(n)
# Runtime: 323ms 48.44% Memory: 15.7mb 65.00%
def nextGreaterElements(self, nums: List[int]) -> List[int]:
result = [-1] * len(nums)
stack = list()
for i in range(2*len(nums)):
circularIdx = i % len(nums)
while len(stack) > 0 and... | next-greater-element-ii | Python3 OnePass O(n) || O(n) Runtime: 323ms 48.44% Memory: 15.7mb 65.00% | arshergon | 2 | 125 | next greater element ii | 503 | 0.631 | Medium | 8,837 |
https://leetcode.com/problems/next-greater-element-ii/discuss/2400483/Python-Solution-with-Explanation | class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
n = len(nums)
ret = [-1] * n
stack = nums[::-1]
for i in range(n - 1, -1, -1):
while stack and stack[-1] <= nums[i]:
stack.pop()
if stack:
ret[i] = stack[-1]
stack.append(nums[i])... | next-greater-element-ii | Python Solution with Explanation | prateekgoel7248 | 1 | 78 | next greater element ii | 503 | 0.631 | Medium | 8,838 |
https://leetcode.com/problems/next-greater-element-ii/discuss/2081108/Python-using-monotonic-stack | class Solution(object):
def nextGreaterElements(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
n = len(nums)
res = [-1 for _ in range(n)]
nums = nums + nums # Add another nums to simulate the circulate situation
stack = []
for i i... | next-greater-element-ii | Python using monotonic stack | Kennyyhhu | 1 | 119 | next greater element ii | 503 | 0.631 | Medium | 8,839 |
https://leetcode.com/problems/next-greater-element-ii/discuss/1833330/Python-solution-or-91.45-lesser-memory | class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
def nextgreater(num,arr):
for i in arr:
if i>num:
return i
else:
return -1
ans = []
for i in range(len(nums)):
ans.append(... | next-greater-element-ii | ✔Python solution | 91.45% lesser memory | Coding_Tan3 | 1 | 104 | next greater element ii | 503 | 0.631 | Medium | 8,840 |
https://leetcode.com/problems/next-greater-element-ii/discuss/1573343/WEEB-DOES-PYTHON-MONOTONIC-STACK | class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
newNums = nums + nums
result = [-1] * len(nums)
stack = [] # stores index
# decreasing stack implementation
for i in range(len(newNums)):
while stack and newNums[stack[-1]] < newNums[i]:
idx = stack.pop()
if idx >= len(n... | next-greater-element-ii | WEEB DOES PYTHON MONOTONIC STACK | Skywalker5423 | 1 | 173 | next greater element ii | 503 | 0.631 | Medium | 8,841 |
https://leetcode.com/problems/next-greater-element-ii/discuss/1347804/python3-%2B-monotonic-stack-%2B-dictionary-or-time-complexity%3A-O(n) | class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]: #follow the idea in Next Greater Element I, loop twice
stack = []#value,index
m1 = {}
for i in range(len(nums)):
if len(stack) == 0 or nums[i] <= stack[-1][0]:
stack.append((nums[i],i))
... | next-greater-element-ii | python3 + monotonic stack + dictionary | time complexity: O(n) | Francis98Liu | 1 | 73 | next greater element ii | 503 | 0.631 | Medium | 8,842 |
https://leetcode.com/problems/next-greater-element-ii/discuss/1144212/Python-2-pass-solution-O(n) | class Solution:
def nextGreaterElements(self, A: List[int]) -> List[int]:
result = [-1] * len(A)
stack = []
for pos, val in enumerate(A):
while stack and val > A[stack[-1]]:
result[stack.pop()] = val
stack.append(pos)
... | next-greater-element-ii | Python - 2 pass solution - O(n) | piyushagg19 | 1 | 266 | next greater element ii | 503 | 0.631 | Medium | 8,843 |
https://leetcode.com/problems/next-greater-element-ii/discuss/2836911/Python-stack | class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
n = len(nums)
stack = []
result = [-1] * n
for i in range(n):
while stack and stack[-1][0] < nums[i]:
num, j = stack.pop()
result[j] = nums[i]
stack... | next-greater-element-ii | Python, stack | swepln | 0 | 4 | next greater element ii | 503 | 0.631 | Medium | 8,844 |
https://leetcode.com/problems/next-greater-element-ii/discuss/2789835/Stack-oror-python | class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
st,ans = [] , []
j=len(nums)-1
while j>=0:
if st!=[] and st[-1]<=nums[j]:
while st!=[] and st[-1]<=nums[j]:
st.pop()
elif st!= [] and st[-1]>nums[j]:
... | next-greater-element-ii | Stack || python | cheems_ds_side | 0 | 4 | next greater element ii | 503 | 0.631 | Medium | 8,845 |
https://leetcode.com/problems/next-greater-element-ii/discuss/2766436/Simple-queue-solution | class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
n=len(nums)
nxt=[-1]*n
max_ind=0
for i in range(1,n):
if nums[i]>nums[max_ind]:
max_ind=i
queue=[nums[max_ind]]
k=max_ind-1
for _ in range(n):
... | next-greater-element-ii | Simple queue solution | beneath_ocean | 0 | 9 | next greater element ii | 503 | 0.631 | Medium | 8,846 |
https://leetcode.com/problems/next-greater-element-ii/discuss/2743448/PYTHON3 | class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
st = []
n = len(nums)
ans = [-1] * n
for i in range(2*n-1, -1, -1):
while st and st[-1] <= nums[i%n]:
st.pop()
if st and i < n:
ans[i] = st[-1]
... | next-greater-element-ii | PYTHON3 | Gurugubelli_Anil | 0 | 2 | next greater element ii | 503 | 0.631 | Medium | 8,847 |
https://leetcode.com/problems/next-greater-element-ii/discuss/2678782/Python-or-Stack-%2B-Two-Loops | class Solution:
def nextGreaterElements(self, xs: List[int]) -> List[int]:
n = len (xs)
stack = []
ans = [-1] * n
for i, x in enumerate(xs):
while stack and stack[-1][0] < x:
ans [stack[-1][1]] = x
stack.pop()
stack.append((x,... | next-greater-element-ii | Python | Stack + Two Loops | on_danse_encore_on_rit_encore | 0 | 9 | next greater element ii | 503 | 0.631 | Medium | 8,848 |
https://leetcode.com/problems/next-greater-element-ii/discuss/2654650/Easy-solution | class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
n=len(nums)
rge=[-1]*n
st=[]
st.append(nums[-1])
for i in range((2*n)-1,-1,-1):
while st and nums[i%n]>=st[-1]:
st.pop()
if st==[]:
rge[i%n]=-1... | next-greater-element-ii | Easy solution | kaushik555 | 0 | 4 | next greater element ii | 503 | 0.631 | Medium | 8,849 |
https://leetcode.com/problems/next-greater-element-ii/discuss/2654236/Next-Greater-Element-2-(easy-understanding) | class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
n=len(nums)
rge=[float("-inf")]*n
st=[]
st.append(nums[-1])
for i in range(n-2,-1,-1):
while st and nums[i]>=st[-1]:
st.pop()
if st==[]:
rge[i]... | next-greater-element-ii | Next Greater Element 2 (easy understanding) | kaushik555 | 0 | 4 | next greater element ii | 503 | 0.631 | Medium | 8,850 |
https://leetcode.com/problems/next-greater-element-ii/discuss/2640192/Python-Stack-(Optimized-Solution) | class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
stack=[]
for i in range(len(nums)-2,-1,-1):
var=nums[i]
while len(stack)!=0 and stack[-1]<=var:
stack.pop()
i-=1
stack.append(var)
print (stack)
... | next-greater-element-ii | Python - Stack (Optimized Solution) | utsa_gupta | 0 | 26 | next greater element ii | 503 | 0.631 | Medium | 8,851 |
https://leetcode.com/problems/next-greater-element-ii/discuss/2605656/Python-Solution-or-Monotonic-Stack | class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
n=len(nums)
ans=[-1]*n
stack=[]
for i in range(2*n-1, -1, -1):
while len(stack) and stack[-1]<=nums[i%n]:
stack.pop()
if i<n:
if len(stack):
... | next-greater-element-ii | Python Solution | Monotonic Stack | Siddharth_singh | 0 | 67 | next greater element ii | 503 | 0.631 | Medium | 8,852 |
https://leetcode.com/problems/next-greater-element-ii/discuss/2559934/I-don't-know-why-people-like-from-len(nums)-1-to-0.-it-is-more-clear-from-0-to-len(nums) | class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
l = len(nums)
stack = []
res = [-1] * l
for i in range(2 * l):
while stack and nums[stack[-1]] < nums[i % l]:
a = stack.pop()
res[a] = nums[i % l]
st... | next-greater-element-ii | I don't know why people like from len(nums) - 1 to 0. it is more clear from 0 to len(nums) | yij793 | 0 | 19 | next greater element ii | 503 | 0.631 | Medium | 8,853 |
https://leetcode.com/problems/next-greater-element-ii/discuss/2411338/Python-using-stack-faster-91.55 | class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
s = []
res = [-1]*len(nums)
for i in range(len(nums)-1, -1, -1):
while s and s[-1] <= nums[i]:
s.pop()
if s and s[-1] > nums[i]:
res[... | next-greater-element-ii | Python using stack faster 91.55% | Mohan01234 | 0 | 87 | next greater element ii | 503 | 0.631 | Medium | 8,854 |
https://leetcode.com/problems/next-greater-element-ii/discuss/2196501/Stack-Approach-oror-Simplest-and-Easiest-Solution | class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
stack = []
doubleNums = nums + nums
n = len(doubleNums)
m = len(nums)
nextGreater = [0]*n
for i in range(n - 1, -1, -1):
while stack and stack[-1] <= doubleNums[i]:
... | next-greater-element-ii | Stack Approach || Simplest and Easiest Solution | Vaibhav7860 | 0 | 119 | next greater element ii | 503 | 0.631 | Medium | 8,855 |
https://leetcode.com/problems/next-greater-element-ii/discuss/2156676/Python-two-pass-stack-solution | class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
result = [-1] * len(nums)
stack = []
for i, n in enumerate(nums):
while stack and stack[-1][1] < n:
si, _ = stack.pop()
result[si] = n
stack.a... | next-greater-element-ii | Python, two-pass stack solution | blue_sky5 | 0 | 31 | next greater element ii | 503 | 0.631 | Medium | 8,856 |
https://leetcode.com/problems/next-greater-element-ii/discuss/1978474/Python-Easy-Solution-oror-Stack-oror-O(N) | class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
stack = []
i = 0
ans = [-1]*len(nums)
while i < len(nums)*2:
idx = i % len(nums)
if stack and stack[-1][0] < nums[idx]:
val,idx1 = stack.pop()
... | next-greater-element-ii | Python Easy Solution || Stack || O(N) | gamitejpratapsingh998 | 0 | 112 | next greater element ii | 503 | 0.631 | Medium | 8,857 |
https://leetcode.com/problems/next-greater-element-ii/discuss/1885842/Python-Monotonic-Stack-one-pass-with-maximum-of-elements | class Solution:
def nextGreaterElements(self, nums):
return self.monotonic_stack(nums)
def monotonic_stack(self, nums):
M_idx = nums.index(max(nums))
# shift cycle so that max is end, and break it
nums = nums[M_idx + 1:] + nums[:M_idx + 1]
next_greater, stack = [], []
... | next-greater-element-ii | Python Monotonic Stack, one pass with maximum of elements | steve-jokes | 0 | 57 | next greater element ii | 503 | 0.631 | Medium | 8,858 |
https://leetcode.com/problems/next-greater-element-ii/discuss/1876571/Using-Stack-oror-Full-Explanation-oror-Python | class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
n = len(nums)
stack = []
"""ans initalizes with -1 because if any element is not bigger
than current element it simply return -1 instead of returning -1
i am initializes my answer array with -1 """
ans = [-1... | next-greater-element-ii | ✅Using Stack || Full Explanation || Python | Dev_Kesarwani | 0 | 81 | next greater element ii | 503 | 0.631 | Medium | 8,859 |
https://leetcode.com/problems/next-greater-element-ii/discuss/1847709/Easy-Simple-Clean-stack-solution | class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
s = []
size = len(nums)
res = [-1 for i in range(size)]
for i in range(2 * size):
i = i % size
while len(s) != 0 and nums[s[-1]] < nums[i]:
item = s.pop()
... | next-greater-element-ii | Easy Simple, Clean stack solution | rishabhjindal4 | 0 | 55 | next greater element ii | 503 | 0.631 | Medium | 8,860 |
https://leetcode.com/problems/next-greater-element-ii/discuss/1781585/python3-Monotonic-stack | class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
n = len(nums)
res = [0]*n
stack = []
for i in range(2*n-1, -1, -1):
while stack and stack[-1] <= nums[i%n]:
stack.pop()
res[i%n] = -1 if not stac... | next-greater-element-ii | python3 Monotonic stack | alexxu666 | 0 | 40 | next greater element ii | 503 | 0.631 | Medium | 8,861 |
https://leetcode.com/problems/next-greater-element-ii/discuss/1778080/Python3%3A-beats-98.25 | class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
stack = nums.copy()
stack.reverse()
n = len(nums)
ans = [0]*n
for i in range(1, n+1):
i = -i
if not stack:
ans[i] = -1
else:
while ... | next-greater-element-ii | Python3: beats 98.25% | Jeff871025 | 0 | 41 | next greater element ii | 503 | 0.631 | Medium | 8,862 |
https://leetcode.com/problems/next-greater-element-ii/discuss/1758916/Python-3-stack-solution | class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
stack = []
res = [-1] * len(nums)
for i, num in enumerate(nums):
while stack and num > nums[stack[-1]]:
res[stack.pop()] = num
stack.append(i)
for i in range(... | next-greater-element-ii | Python 3, stack solution | dereky4 | 0 | 103 | next greater element ii | 503 | 0.631 | Medium | 8,863 |
https://leetcode.com/problems/next-greater-element-ii/discuss/1728220/Simple-Python-single-traversal-%3A | class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
stack=[]
n=len(nums)
d=[-1]*n
for i in range(n*2):
while stack and nums[stack[-1]]<nums[i%n]: #i%n is used to find correct index
x=stack.pop()
if x... | next-greater-element-ii | Simple Python single traversal : | goxy_coder | 0 | 81 | next greater element ii | 503 | 0.631 | Medium | 8,864 |
https://leetcode.com/problems/next-greater-element-ii/discuss/1710176/2-passes-or-Monotonic-Stack-or-Python | class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
stack = []
res = [-1] * len(nums)
for i in range(len(nums)):
if not stack:
stack.append(i)
while stack and nums[stack[-1]] < nums[i... | next-greater-element-ii | 2 passes | Monotonic Stack | Python | iamskd03 | 0 | 30 | next greater element ii | 503 | 0.631 | Medium | 8,865 |
https://leetcode.com/problems/next-greater-element-ii/discuss/1661564/Python3-Maintain-a-monotonic-increasing-stack | class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
# Its like maintain a monotonic increasing stack with [index,val]
# if current val greater than stack[-1], then index, val = stack.pop() and assign to res[index] = val
# n = len(nums) at must find within n + n-1 tim... | next-greater-element-ii | [Python3] Maintain a monotonic increasing stack | JackYeh17 | 0 | 95 | next greater element ii | 503 | 0.631 | Medium | 8,866 |
https://leetcode.com/problems/next-greater-element-ii/discuss/1636306/Easy-python3-solution | class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
stack=nums[::-1]
res=[]
for val in reversed(nums):
while stack and val>=stack[-1]:
stack.pop()
if stack:
res.append(stack[-1])
else:
... | next-greater-element-ii | Easy python3 solution | Karna61814 | 0 | 60 | next greater element ii | 503 | 0.631 | Medium | 8,867 |
https://leetcode.com/problems/next-greater-element-ii/discuss/1619817/Python-Easy-Solution-or-Optimal-Stack-Approach | class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
n = len(nums)
stck = []
for i in range(n-2, -1, -1):
while len(stck) != 0 and nums[i] >= stck[-1]:
stck.pop()
stck.append(nums[i])
res = []
for i in range(n-1, -1, -1):
while len(stck) != 0 and nums[i] >= stck[-1]:
... | next-greater-element-ii | Python Easy Solution | Optimal Stack Approach ✔ | leet_satyam | 0 | 105 | next greater element ii | 503 | 0.631 | Medium | 8,868 |
https://leetcode.com/problems/next-greater-element-ii/discuss/1607283/Python3-Simple-Solution-with-Full-Comment-using-Monotonic-Stack-or-O(N)-or-Construction-or-Intuition | class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
# monotonic stack
# store the index!!
mStack = []
res = []
# construct a circle by copy it the end
circle = copy.deepcopy(nums)
circle.extend(nums)
for i in range(len(circle) ... | next-greater-element-ii | Python3 Simple Solution with Full Comment using Monotonic Stack | O(N) | Construction | Intuition | Zhoueeer | 0 | 88 | next greater element ii | 503 | 0.631 | Medium | 8,869 |
https://leetcode.com/problems/next-greater-element-ii/discuss/1594936/Super-simple-Python-solution%3A-Stack-%3A) | class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
n = len(nums)
answer = [-1 for _ in range(len(nums))]
stack = [0]
for i in range(1, 2*len(nums)):
while stack and nums[i%n] > nums[stack[-1]]: answer[stack.pop()] = nums[i%n]
stack.ap... | next-greater-element-ii | Super simple Python solution: Stack :) | iknoor | 0 | 95 | next greater element ii | 503 | 0.631 | Medium | 8,870 |
https://leetcode.com/problems/next-greater-element-ii/discuss/1594893/Monotonic-Stack | class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
n = len(nums)
stack = [] #monotonous stack
ans = [0]*n
#unroll the circular list
nums.extend(nums[:-1])
for i in range(len(nums)-1, -1, -1):
while stack!=[] and nums[i]>=stack[-1... | next-greater-element-ii | Monotonic Stack | steven0821 | 0 | 52 | next greater element ii | 503 | 0.631 | Medium | 8,871 |
https://leetcode.com/problems/next-greater-element-ii/discuss/1506263/Python-simple-O(n)-time-O(n)-space-solution-with-decreasing-stack | class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
n = len(nums)
arr = nums + nums
stack = []
ans = [-1 for _ in range(n)]
for i, val in enumerate(arr):
while stack and arr[stack[-1]] < val :
c = stack.pop()
... | next-greater-element-ii | Python simple O(n) time, O(n) space solution with decreasing stack | byuns9334 | 0 | 103 | next greater element ii | 503 | 0.631 | Medium | 8,872 |
https://leetcode.com/problems/next-greater-element-ii/discuss/1255620/Python-Easy-solution-using-stack-with-explanation-time-complexity-O(n) | class Solution:
def nextGreaterElements(self, arr: List[int]) -> List[int]:
d={}
stack=[]
#check for greater element
for i , n in enumerate(arr):
while stack and arr[stack[-1]]<n:
curr=stack.pop()
d[curr] = n
st... | next-greater-element-ii | Python Easy solution using stack with explanation , time complexity-O(n) | ritesh98 | 0 | 78 | next greater element ii | 503 | 0.631 | Medium | 8,873 |
https://leetcode.com/problems/next-greater-element-ii/discuss/1157661/faster-than-11.91-of-Python3 | class Solution:
def nextGreaterElements(self, nums1: List[int]) -> List[int]:
num=[-1]*len(nums1)
for i in range(len(nums1)):
found=0
for n in range(i+1,len(nums1)):
if nums1[n]>nums1[i]:
num[i]=nums1[n]
found=1
... | next-greater-element-ii | faster than 11.91% of Python3 | janhaviborde23 | 0 | 73 | next greater element ii | 503 | 0.631 | Medium | 8,874 |
https://leetcode.com/problems/next-greater-element-ii/discuss/1104089/Python-or-Short-and-Simple-or-Using-circular-traversal-and-a-stack | class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
if not nums: return nums
maxElem, n, startIdx = max(nums), len(nums), 0
for idx in reversed(range(n)):
if nums[idx] == maxElem:
startIdx = idx
break
def prevIdx(... | next-greater-element-ii | Python | Short and Simple | Using circular traversal and a stack | wind_pawan | 0 | 161 | next greater element ii | 503 | 0.631 | Medium | 8,875 |
https://leetcode.com/problems/next-greater-element-ii/discuss/876423/Simple-Python-Solution-Stack-O(n) | class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
stack=[]
for i in range(len(nums)-1,-1,-1):
stack.append(nums[i])
ans=[]
for i in range(len(nums)-1,-1,-1):
if len(stack)=... | next-greater-element-ii | Simple Python Solution - Stack O(n) | Ayu-99 | 0 | 73 | next greater element ii | 503 | 0.631 | Medium | 8,876 |
https://leetcode.com/problems/next-greater-element-ii/discuss/862999/Python3-two-approaches-forward-and-backward | class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
ans, stack = [], [] # mono-stack (decreasing)
for x in reversed(nums + nums):
while stack and stack[-1] <= x: stack.pop()
ans.append(stack[-1] if stack else -1)
stack.append(x)
a... | next-greater-element-ii | [Python3] two approaches - forward & backward | ye15 | 0 | 92 | next greater element ii | 503 | 0.631 | Medium | 8,877 |
https://leetcode.com/problems/next-greater-element-ii/discuss/862999/Python3-two-approaches-forward-and-backward | class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
ans = [-1]*len(nums)
stack = []
for i, x in enumerate(nums + nums):
while stack and stack[-1][1] < x: ans[stack.pop()[0]] = x
stack.append((i%len(nums), x))
return ans | next-greater-element-ii | [Python3] two approaches - forward & backward | ye15 | 0 | 92 | next greater element ii | 503 | 0.631 | Medium | 8,878 |
https://leetcode.com/problems/next-greater-element-ii/discuss/352741/Solution-in-Python-3-(beats-~96)-(single-pass) | class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
L = len(nums)
N, H, I = [-1]*L, [], nums.index(max(nums)) if L > 0 else 0
for i in range(I,I-L,-1):
while len(H) != 0 and nums[H[-1]] <= nums[i]: del H[-1]
if len(H) > 0: N[i] = nums[H[-1]]
H.append(i)
... | next-greater-element-ii | Solution in Python 3 (beats ~96%) (single pass) | junaidmansuri | 0 | 344 | next greater element ii | 503 | 0.631 | Medium | 8,879 |
https://leetcode.com/problems/next-greater-element-ii/discuss/295380/Python-Solution | class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
st = []
nlen = len(nums)
res = [-1] * nlen
for times in range(2):
for idx in range(nlen - 1, -1, -1):
while st and st[-1] <= nums[idx]:
st.pop()
... | next-greater-element-ii | Python Solution | ajrator123 | 0 | 318 | next greater element ii | 503 | 0.631 | Medium | 8,880 |
https://leetcode.com/problems/next-greater-element-ii/discuss/1500431/Python3-O(2-*-n)-solution | class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
stack = []
old_len = len(nums)
nums += nums
res = [-1] * len(nums)
for idx in range(len(nums) -1, -1, -1):
while stack and nums[idx] >= stack[-1]:
stack.... | next-greater-element-ii | [Python3] O(2 * n) solution | maosipov11 | -1 | 39 | next greater element ii | 503 | 0.631 | Medium | 8,881 |
https://leetcode.com/problems/base-7/discuss/1014922/Simple-and-easy-faster-than-99.31 | class Solution:
def convertToBase7(self, num: int) -> str:
if not num:
return "0"
l=[]
x=num
if num<0:
num=-num
while num>0:
r=num%7
l.append(str(r))
num//=7
return "".join(l[::-1]) if x>=0 else "-"+ "".join(... | base-7 | Simple and easy - faster than 99.31% | thisisakshat | 4 | 607 | base 7 | 504 | 0.48 | Easy | 8,882 |
https://leetcode.com/problems/base-7/discuss/1356779/Easy-Fast-Python-Solutions-(2-Approaches-24ms-32ms-Faster-than-95) | class Solution:
def convertToBase7(self, num: int) -> str:
answer = []
sign = num
if not num:
return "0"
elif num < 0:
num *= -1
while num:
a = str(num % 7)
answer.insert(0, a)
num = num // 7
if sign < 0:
... | base-7 | Easy, Fast Python Solutions (2 Approaches - 24ms, 32ms; Faster than 95%) | the_sky_high | 2 | 227 | base 7 | 504 | 0.48 | Easy | 8,883 |
https://leetcode.com/problems/base-7/discuss/1356779/Easy-Fast-Python-Solutions-(2-Approaches-24ms-32ms-Faster-than-95) | class Solution:
def convertToBase7(self, num: int) -> str:
answer = ""
sign = num
if not num:
return "0"
elif num < 0:
num *= -1
while num:
a = str(num % 7)
answer = a + answer
num = num // 7
if sign < 0:
... | base-7 | Easy, Fast Python Solutions (2 Approaches - 24ms, 32ms; Faster than 95%) | the_sky_high | 2 | 227 | base 7 | 504 | 0.48 | Easy | 8,884 |
https://leetcode.com/problems/base-7/discuss/2761308/Python-Recursive-DivMod-Solution-(3-Lines) | class Solution:
def convertToBase7(self, num: int) -> str:
if num < 0: return "-" + self.convertToBase7(-num)
if num < 7: return str(num)
return self.convertToBase7(num//7) + str(num%7) | base-7 | [Python] Recursive DivMod Solution (3 Lines) | keioon | 0 | 7 | base 7 | 504 | 0.48 | Easy | 8,885 |
https://leetcode.com/problems/base-7/discuss/2031091/Easy-Simple-Solution-for-Problem-Noob-way | class Solution(object):
def convertToBase7(self, num):
"""
:type num: int
:rtype: str
"""
r =""
if num == 0:
return "0"
num1 = abs(num)
while(num1 >0):
prevnum = num1
num1 =num1/7
l = num1 * 7
... | base-7 | Easy Simple Solution for Problem Noob way | Toshnav_Khatke | 0 | 94 | base 7 | 504 | 0.48 | Easy | 8,886 |
https://leetcode.com/problems/base-7/discuss/1872462/Simple-Python-Solution-easy-to-understand | class Solution:
def convertToBase7(self, n: int) -> str:
ans=""
neg=False
#if number is negative then neg=True
if n<0:
neg=True
while n>=7:
q=n//7
r=n%7
ans+=str(r)
n=q
ans+=str(n)
#If number is negative then add... | base-7 | Simple Python Solution easy-to understand | imjenit | 0 | 94 | base 7 | 504 | 0.48 | Easy | 8,887 |
https://leetcode.com/problems/base-7/discuss/1839396/3-Lines-Python-Solution-oror-90-Faster-(31ms)-oror-Memory-less-than-99 | class Solution:
def convertToBase7(self, num: int) -> str:
ans='' ; n=abs(num)
while n>0: ans=str(n%7)+ans ; n//=7
return '-'*(num<0)+ans or '0' | base-7 | 3-Lines Python Solution || 90% Faster (31ms) || Memory less than 99% | Taha-C | 0 | 95 | base 7 | 504 | 0.48 | Easy | 8,888 |
https://leetcode.com/problems/base-7/discuss/1838003/Python-Simple-and-Elegant-Multiple-solutions! | class Solution:
def convertToBase7(self, num: int) -> str:
ans, sign = "", ""
if num == 0: return "0"
if num < 0: num, sign = abs(num), "-"
while num:
ans = str(num%7) + ans
num //= 7
return sign + ans | base-7 | Python - Simple and Elegant - Multiple solutions! | domthedeveloper | 0 | 58 | base 7 | 504 | 0.48 | Easy | 8,889 |
https://leetcode.com/problems/base-7/discuss/1838003/Python-Simple-and-Elegant-Multiple-solutions! | class Solution:
def convertToBase7(self, num: int) -> str:
ans, sign = deque(), ""
if num == 0: return "0"
if num < 0: num, sign = abs(num), "-"
while num:
ans.appendleft(num%7)
num //= 7
return sign + reduce(add, map(str, ans)) | base-7 | Python - Simple and Elegant - Multiple solutions! | domthedeveloper | 0 | 58 | base 7 | 504 | 0.48 | Easy | 8,890 |
https://leetcode.com/problems/base-7/discuss/1827248/Python-Math-base-Solution | class Solution:
def convertToBase7(self, num: int) -> str:
if num < 0:
return '-' + self.convertToBase7(-num)
m = ''
if num >= 7:
num, m = divmod(num, 7)
num = self.convertToBase7(num)
return str(num)+str(m) | base-7 | [Python] Math base Solution | crazypuppy | 0 | 70 | base 7 | 504 | 0.48 | Easy | 8,891 |
https://leetcode.com/problems/base-7/discuss/1778051/WEEB-DOES-PYTHONC%2B%2B | class Solution:
def convertToBase7(self, num: int) -> str:
n, result = num, ""
if num == 0: return "0"
if num<0:
num = -num
while num:
result += str(num%7)
num = num // 7
return result[::-1] if n >= 0 else "-" + result[::-1] | base-7 | WEEB DOES PYTHON/C++ | Skywalker5423 | 0 | 53 | base 7 | 504 | 0.48 | Easy | 8,892 |
https://leetcode.com/problems/base-7/discuss/1372639/Python-Maths-Faster-than-95.4 | class Solution:
def convertToBase7(self, num: int) -> str:
temp = num
num = abs(num)
result = num % 7
power = 1
while num//7 != 0:
num = num//7
result += ((num % 7) * 10**power)
power += 1
return str(result) if temp >= 0 else '-' + ... | base-7 | Python - Maths - Faster than 95.4% | _Mansiii_ | 0 | 258 | base 7 | 504 | 0.48 | Easy | 8,893 |
https://leetcode.com/problems/base-7/discuss/1317352/Easy-Solution_python3 | class Solution:
def convertToBase7(self, num: int) -> str:
abs_num = abs(num)
val = ''
ans = ''
while abs_num >= 7:# we want to add up every remainder until the dividend(num) equals to 7 or less than 7.
val += str(abs_num % 7)
abs_num = abs_num // 7
va... | base-7 | Easy Solution_python3 | An_222 | 0 | 101 | base 7 | 504 | 0.48 | Easy | 8,894 |
https://leetcode.com/problems/base-7/discuss/1234417/Python3-simple-solution-beats-95-users | class Solution:
def convertToBase7(self, num: int) -> str:
if num == 0:
return str(num)
res = ''
x = abs(num)
while x > 0:
res = str(x%7) + res
x //= 7
return '-' + res if num < 0 else res | base-7 | Python3 simple solution beats 95% users | EklavyaJoshi | 0 | 95 | base 7 | 504 | 0.48 | Easy | 8,895 |
https://leetcode.com/problems/relative-ranks/discuss/1705542/Python-Simple-Solution-using-Max-Heap | class Solution:
def findRelativeRanks(self, score: List[int]) -> List[str]:
rankings = []
for i, val in enumerate(score):
heappush(rankings, (-val, i))
ans = [''] * len(score)
r = 1
rank = ["Gold Medal", "Silver Medal", "Bronze Medal"]
while len(rankings) ... | relative-ranks | [Python] Simple Solution using Max-Heap | anCoderr | 8 | 573 | relative ranks | 506 | 0.592 | Easy | 8,896 |
https://leetcode.com/problems/relative-ranks/discuss/1013255/Simple-Solution-with-explanation-faster-than-99.58 | class Solution:
def findRelativeRanks(self, nums: List[int]) -> List[str]:
if len(nums)==1:
return ["Gold Medal"]
elif len(nums)==2:
if nums[0]>nums[1]:
nums[0],nums[1]="Gold Medal","Silver Medal"
else:
nums[1],nums[0]="Gold Medal",... | relative-ranks | Simple Solution with explanation- faster than 99.58% | thisisakshat | 6 | 690 | relative ranks | 506 | 0.592 | Easy | 8,897 |
https://leetcode.com/problems/relative-ranks/discuss/2415058/Python-Elegant-and-Short-or-O(n*log(n))-time-or-Sorting | class Solution:
"""
Time: O(n*log(n))
Memory: O(n)
"""
MEDALS = {
1: 'Gold Medal',
2: 'Silver Medal',
3: 'Bronze Medal',
}
def findRelativeRanks(self, nums: List[int]) -> List[str]:
ranks = {num: ind for ind, num in enumerate(sorted(nums, reverse=True), start=1)}
return [self._get_place(ranks[num])... | relative-ranks | Python Elegant & Short | O(n*log(n)) time | Sorting | Kyrylo-Ktl | 4 | 259 | relative ranks | 506 | 0.592 | Easy | 8,898 |
https://leetcode.com/problems/relative-ranks/discuss/1207201/Python3-simple-solution-using-dictionary-beats-95-users | class Solution:
def findRelativeRanks(self, score: List[int]) -> List[str]:
x = sorted(score, reverse=True)
d = {}
for i,j in enumerate(x):
if i == 0:
d[j] = "Gold Medal"
elif i == 1:
d[j] = "Silver Medal"
elif i == 2:
... | relative-ranks | Python3 simple solution using dictionary beats 95% users | EklavyaJoshi | 3 | 197 | relative ranks | 506 | 0.592 | Easy | 8,899 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.