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/monotonic-array/discuss/1159003/Python3-Simple-And-Single-Line-Solution
class Solution: def isMonotonic(self, A: List[int]) -> bool: return(A == sorted(A) or A == sorted(A , reverse=True))
monotonic-array
[Python3] Simple And Single Line Solution
Lolopola
1
73
monotonic array
896
0.583
Easy
14,500
https://leetcode.com/problems/monotonic-array/discuss/401326/Python-Simple-Concise-Solution
class Solution: def isMonotonic(self, A: List[int]) -> bool: a = sorted(A) s = sorted(A, reverse =True) return (A==a) or (A==s)
monotonic-array
Python Simple Concise Solution
saffi
1
317
monotonic array
896
0.583
Easy
14,501
https://leetcode.com/problems/monotonic-array/discuss/2847907/one-liner-solution-using-sorting-in-python-**easy-to-understand**
class Solution: def isMonotonic(self, nums: List[int]) -> bool: return sorted(nums) == nums or sorted(nums,reverse=True) == nums
monotonic-array
one liner solution using sorting in python **easy to understand**
experimentallyf
0
1
monotonic array
896
0.583
Easy
14,502
https://leetcode.com/problems/monotonic-array/discuss/2838648/Super-Easy-python-solution-oror-Beats-77-oror-Double-array-iteration
class Solution: def isMonotonic(self, lista: List[int]) -> bool: if lista[0] <= lista [-1]: # if array is ascending for i in range(len(lista)-1): a = lista[i] b = lista[i+1] #iterate over list, and list[1: ] if a <= b: #if number is less than next, continue continue else: return False #else return false return True if lista[0] >= lista [-1]: # if array is descending for i in range(len(lista)-1): a = lista[i] b = lista[i+1] #iterate over list, and list[1: ] if a >= b: #if number is greater than next, continue continue else: return False #else return false return True
monotonic-array
Super Easy python solution || Beats 77% || Double array iteration
inusyd
0
1
monotonic array
896
0.583
Easy
14,503
https://leetcode.com/problems/monotonic-array/discuss/2805257/Simple-and-Clean-Solution-oror-Very-easy-to-Understand
class Solution: def isMonotonic(self, nums: List[int]) -> bool: increasing = False decreasing= False if(len(nums)==1): return True for idx in range(1,len(nums)): if(nums[idx]==nums[idx-1]): continue elif(nums[idx]>nums[idx-1]): increasing = True elif(nums[idx]<nums[idx-1]): decreasing = True if(increasing==decreasing): return False return True
monotonic-array
Simple and Clean Solution || Very easy to Understand
hasan2599
0
4
monotonic array
896
0.583
Easy
14,504
https://leetcode.com/problems/monotonic-array/discuss/2794866/N(logn)-oror-easiest-approach(100)
class Solution: def isMonotonic(self, nums: List[int]) -> bool: inc=[] dsc=[] inc=sorted(nums[:]) dsc=inc[::-1] if nums == inc or nums ==dsc: return True else: return False
monotonic-array
N(logn) || easiest approach(100%)
anandmishracs2023
0
4
monotonic array
896
0.583
Easy
14,505
https://leetcode.com/problems/monotonic-array/discuss/2789199/python3
class Solution: def isMonotonic(self, nums: List[int]) -> bool: k=1 m=1 for i in range(1,len(nums)): if(nums[i]>=nums[i-1]): k=k+1 if(nums[i]<=nums[i-1]): m=m+1 if(m==len(nums) or k==len(nums)): return True return False
monotonic-array
python3
fulla
0
2
monotonic array
896
0.583
Easy
14,506
https://leetcode.com/problems/monotonic-array/discuss/2784002/one-pass-python
class Solution: def isMonotonic(self, nums: List[int]) -> bool: # O(n), O(1) # one pass inc = dec = True for i in range(len(nums) - 1): if nums[i] > nums[i + 1]: inc = False if nums[i] < nums[i + 1]: dec = False return inc or dec
monotonic-array
one pass python
sahilkumar158
0
2
monotonic array
896
0.583
Easy
14,507
https://leetcode.com/problems/monotonic-array/discuss/2711267/Python-or-Simple-O(n)-solution
class Solution: def isMonotonic(self, nums: List[int]) -> bool: incr, decr = False, False for i in range(1, len(nums)): d = nums[i] - nums[i - 1] if (decr and d > 0) or (incr and d < 0): return False if d > 0: incr = True elif d < 0: decr = True return True
monotonic-array
Python | Simple O(n) solution
LordVader1
0
9
monotonic array
896
0.583
Easy
14,508
https://leetcode.com/problems/monotonic-array/discuss/2710503/PYTHON3-BEST
class Solution: def isMonotonic(self, nums: List[int]) -> bool: x = sorted(nums) if x == nums or x == nums[::-1]: return True return False
monotonic-array
PYTHON3 BEST
Gurugubelli_Anil
0
2
monotonic array
896
0.583
Easy
14,509
https://leetcode.com/problems/monotonic-array/discuss/2596503/python-1-liner
class Solution: def isMonotonic(self, nums: List[int]) -> bool: return (all(operator.le(*pair) for pair in itertools.pairwise(nums)) or all(operator.ge(*pair) for pair in itertools.pairwise(nums)))
monotonic-array
python 1-liner
Potentis
0
45
monotonic array
896
0.583
Easy
14,510
https://leetcode.com/problems/monotonic-array/discuss/2596119/Python-and-Go
class Solution: def isMonotonic(self, nums: List[int]) -> bool: increasing = True decreasing = True for i in range(len(nums) - 1): if nums[i] > nums[i + 1]: increasing = False if nums[i] < nums[i + 1]: decreasing = False return increasing or decreasing
monotonic-array
Python and Go答え
namashin
0
10
monotonic array
896
0.583
Easy
14,511
https://leetcode.com/problems/monotonic-array/discuss/2442388/Solution-(One-Liner)
class Solution: def isMonotonic(self, nums: List[int]) -> bool: return (nums == sorted(nums) or nums == sorted(nums,reverse = True))
monotonic-array
Solution (One-Liner)
fiqbal997
0
34
monotonic array
896
0.583
Easy
14,512
https://leetcode.com/problems/monotonic-array/discuss/2417379/Python-Beats-99
class Solution: def isMonotonic(self, nums: List[int]) -> bool: increasing = True decreasing = True for i in range(1, len(nums)): if nums[i] >= nums[i-1]: increasing = True else: increasing = False break for j in range(1, len(nums)): if nums[j] <= nums[j-1]: decreasing = True else: decreasing = False break return increasing or decreasing
monotonic-array
Python Beats 99%
theReal007
0
31
monotonic array
896
0.583
Easy
14,513
https://leetcode.com/problems/monotonic-array/discuss/2352274/Easy-python
class Solution: def isMonotonic(self, nums: List[int]) -> bool: l=len(nums) if l==1: return True direction = set() for i in range(l-1): if (nums[i]<nums[i+1]): if (-1 not in direction): direction.add(1) else: return False elif (nums[i]>nums[i+1]): if (1 not in direction): direction.add(-1) else: return False return True
monotonic-array
Easy python
sunakshi132
0
24
monotonic array
896
0.583
Easy
14,514
https://leetcode.com/problems/monotonic-array/discuss/2156705/Python-easy-understanding-solution-O(N)-Time
class Solution: def isMonotonic(self, nums: List[int]) -> bool: inc = dec = True for i in range(len(nums)-1): if nums[i] > nums[i+1]: inc = False elif nums[i] < nums[i+1]: dec = False return inc or dec
monotonic-array
Python easy understanding solution - O(N) Time
Jen0120
0
83
monotonic array
896
0.583
Easy
14,515
https://leetcode.com/problems/monotonic-array/discuss/2045722/Python-solution
class Solution: def isMonotonic(self, nums: List[int]) -> bool: return nums == sorted(nums) or nums == sorted(nums, reverse=True)
monotonic-array
Python solution
StikS32
0
56
monotonic array
896
0.583
Easy
14,516
https://leetcode.com/problems/monotonic-array/discuss/2020468/Naive-Approach-Python3-No-built-in-functions
class Solution: def isMonotonic(self, nums: List[int]) -> bool: # Naive approach: check each type of monotonic checkIncreasing = True checkDecreasing = True # Monotone increasing: for i in range(len(nums) -1): if nums[i] > nums[i+1]: checkIncreasing = False break for i in range(len(nums) -1): if nums[i] < nums[i+1]: checkDecreasing = False break if checkIncreasing or checkDecreasing: return True else: return False
monotonic-array
Naive Approach - Python3 - No built-in functions
huyanguyen3695
0
27
monotonic array
896
0.583
Easy
14,517
https://leetcode.com/problems/monotonic-array/discuss/1969568/One-line-with-list-slicing
class Solution: def isMonotonic(self, nums: List[int]) -> bool: return sorted(nums) == nums or sorted(nums)[::-1] == nums
monotonic-array
One line with list slicing
andrewnerdimo
0
42
monotonic array
896
0.583
Easy
14,518
https://leetcode.com/problems/monotonic-array/discuss/1930222/Python-1-line-code-oror-89-faster-than-others-oror-Python-3-oror-one-liner-using-sort
class Solution: def isMonotonic(self, nums: List[int]) -> bool: return nums==sorted(nums) or nums==sorted(nums,reverse=True)
monotonic-array
Python 1 line code || 89% faster than others || Python 3 || one liner using sort
nileshporwal
0
64
monotonic array
896
0.583
Easy
14,519
https://leetcode.com/problems/monotonic-array/discuss/1878782/Faster-then-ONE-PASS-(SIMPLE-VARIENT)
class Solution: def isMonotonic(self, nums: List[int]) -> bool: i = 0 j = len(nums)-1 if nums[i]<nums[j]: while i+1<=j: if nums[i]>nums[i+1]: return False else: i+=1 else: while i+1<=j: if nums[i]<nums[i+1]: return False else: i+=1 return True
monotonic-array
Faster then ONE PASS (SIMPLE VARIENT)
Brillianttyagi
0
28
monotonic array
896
0.583
Easy
14,520
https://leetcode.com/problems/monotonic-array/discuss/1836556/1-Line-Python-Solution-oror-94-Faster-oror-Memory-less-than-60
class Solution: def isMonotonic(self, nums: List[int]) -> bool: if all(nums[i]<=nums[i+1] for i in range(len(nums)-1)) or all(nums[i]>=nums[i+1] for i in range(len(nums)-1)): return True
monotonic-array
1-Line Python Solution || 94% Faster || Memory less than 60%
Taha-C
0
45
monotonic array
896
0.583
Easy
14,521
https://leetcode.com/problems/monotonic-array/discuss/1836556/1-Line-Python-Solution-oror-94-Faster-oror-Memory-less-than-60
class Solution: def isMonotonic(self, nums: List[int]) -> bool: return nums==sorted(nums) or nums==sorted(nums, reverse=True)
monotonic-array
1-Line Python Solution || 94% Faster || Memory less than 60%
Taha-C
0
45
monotonic array
896
0.583
Easy
14,522
https://leetcode.com/problems/monotonic-array/discuss/1772651/WEEB-DOES-PYTHONC%2B%2B
class Solution: def isMonotonic(self, nums: List[int]) -> bool: isDec = False isInc = False for i in range(len(nums)-1): if nums[i] < nums[i+1]: if isDec: return False isInc = True if nums[i] > nums[i+1]: if isInc: return False isDec = True return True
monotonic-array
WEEB DOES PYTHON/C++
Skywalker5423
0
33
monotonic array
896
0.583
Easy
14,523
https://leetcode.com/problems/monotonic-array/discuss/1659242/one-line-solution
class Solution: def isMonotonic(self, nums: List[int]) -> bool: # one line solution return nums== sorted(nums) or nums == sorted(nums)[::-1]
monotonic-array
one line solution
sc618445
0
43
monotonic array
896
0.583
Easy
14,524
https://leetcode.com/problems/monotonic-array/discuss/1652032/Python-O(n)-One-pass-solution-89-faster
class Solution: def isMonotonic(self, nums: List[int]) -> bool: n=len(nums) inc=True dec=True prev=nums[0] if n==1: return True for x in range(1,n): curr=nums[x] if prev>curr: inc=False if prev<curr: dec=False if not(inc or dec): return False prev=curr return inc or dec
monotonic-array
Python O(n) One pass solution 89% faster
wellimaj
0
64
monotonic array
896
0.583
Easy
14,525
https://leetcode.com/problems/monotonic-array/discuss/1148050/Python-O(n)-One-pass-Solution
class Solution: def isMonotonic(self, A: List[int]) -> bool: if(len(A)==1): return True inc = True dec = True for i in range(1, len(A)): if(A[i-1] < A[i]): dec = False elif(A[i-1] > A[i]): inc = False return inc or dec
monotonic-array
Python O(n) One-pass Solution
liamc628
0
97
monotonic array
896
0.583
Easy
14,526
https://leetcode.com/problems/monotonic-array/discuss/1144534/Python-One-line-using-sort
class Solution: def isMonotonic(self, A: List[int]) -> bool: return True if sorted(A) == A or sorted(A,reverse=True) == A else False
monotonic-array
Python One line using sort
keewook2
0
52
monotonic array
896
0.583
Easy
14,527
https://leetcode.com/problems/monotonic-array/discuss/1111675/Python3-simple-solution
class Solution: def isMonotonic(self, A: List[int]) -> bool: if sorted(A) == A or sorted(A,reverse = True) == A: return True else: return False
monotonic-array
Python3 simple solution
EklavyaJoshi
0
50
monotonic array
896
0.583
Easy
14,528
https://leetcode.com/problems/monotonic-array/discuss/951695/Python3-Easy-to-understand-and-Clear
class Solution: def isMonotonic(self, A: List[int]) -> bool: start = A[0] end = A[-1] if start <= end: for i in range(0, len(A)-1): if A[i] > A[i+1]: return False else: for i in range(0, len(A)-1): if A[i] < A[i+1]: return False return True
monotonic-array
[Python3] Easy to understand and Clear
vimoxshah
0
62
monotonic array
896
0.583
Easy
14,529
https://leetcode.com/problems/monotonic-array/discuss/924956/Python%3A-O(N)-Time-O(1)-Space-(No-Built-in-Functions)
class Solution: def isMonotonic(self, A: List[int]) -> bool: # if length <=2 output will automatically be monotone if len(A) <= 2: return True else: direction = 0 # 1 if increasing, -1 if decreasing, 0 temp (assume equal) # O(N) where N is length of A for i in range(0,len(A)-1): # skip if consecutive pair is equal # otherwise check difference and direction if A[i] != A[i+1]: # we haven't assigned a direction yet but we know # that the values are not equal to one another (i.e. != 0) if direction == 0: direction = -1 if A[i] > A[i+1] else 1 # pair shows sign of decrease but we're increasing elif A[i] > A[i+1] and direction == 1: return False # pair shows sign of increase but we're decreasing elif A[i] < A[i+1] and direction == -1: return False return True
monotonic-array
Python: O(N) Time, O(1) Space (No Built-in Functions)
JuanTheDoggo
0
85
monotonic array
896
0.583
Easy
14,530
https://leetcode.com/problems/monotonic-array/discuss/825854/Python3-or-Straight-forward-or-Time-Complexity-O(n)
class Solution: def isMonotonic(self, A: List[int]) -> bool: increase=True decrease=True for x in range(0,len(A)-1): if (A[x] > A[x+1]): increase=False if (A[x] < A[x+1]): decrease=False return increase or decrease
monotonic-array
Python3 | Straight forward | Time Complexity O(n)
donpauly
0
32
monotonic array
896
0.583
Easy
14,531
https://leetcode.com/problems/monotonic-array/discuss/801957/Python-3-One-Liner-beats-95
class Solution: def isMonotonic(self, A: List[int]) -> bool: return A == sorted(A) or A == sorted(A)[::-1]
monotonic-array
[Python 3] - One Liner - beats 95%
mb557x
0
57
monotonic array
896
0.583
Easy
14,532
https://leetcode.com/problems/monotonic-array/discuss/694684/Simple-Python-solution
class Solution: def isMonotonic(self, A: List[int]) -> bool: if len(A) == 1: return True increasing = False decreasing = False for i in range(len(A)-1): if A[i] < A[i+1]: increasing = True elif A[i] > A[i+1]: decreasing = True if increasing and decreasing: return False return True
monotonic-array
Simple Python solution
phoe6
0
42
monotonic array
896
0.583
Easy
14,533
https://leetcode.com/problems/monotonic-array/discuss/453871/Simple-and-Beats-94-in-runtime.
class Solution: def isMonotonic(self, A: List[int]) -> bool: incre_monotone = None last_monotone = None out = True for i in range(len(A) -1): if A[i] == A[i+1]:continue elif A[i] < A[i+1]:incre_monotone = True elif A[i] > A[i+1]:incre_monotone = False if last_monotone == None:last_monotone = incre_monotone elif last_monotone == incre_monotone:continue elif last_monotone != incre_monotone: out = False break return out
monotonic-array
Simple and Beats 94% in runtime.
sudhirkumarshahu80
0
173
monotonic array
896
0.583
Easy
14,534
https://leetcode.com/problems/monotonic-array/discuss/421978/Python-Solution%3A-Always-check-if-monotonically-increasing
class Solution: def isMonotonic(self, A: List[int]) -> bool: if len(A) < 2: return True # So that we can check for always monotonically increasing if A[0] >= A[-1]: A.reverse() prev = A[0] for idx in range(1, len(A)): curr = A[idx] if curr >= prev: prev = curr continue return False return True
monotonic-array
Python Solution: Always check if monotonically increasing
avpy
0
72
monotonic array
896
0.583
Easy
14,535
https://leetcode.com/problems/monotonic-array/discuss/357052/Python-O(n)-runtime-O(1)-space-complexity
class Solution: def is_increasing(self, items): start_value = items[0] for value in items: if value > start_value: return True if value < start_value: return False def get_comparison_function(self, is_increasing): def is_asscending(a, b): return a <= b def is_decending(a, b): return a >= b if is_increasing: return is_asscending return is_decending def isMonotonic(self, A: List[int]) -> bool: is_increasing = self.is_increasing(A) is_monotonic = self.get_comparison_function(is_increasing) for index in range(1, len(A)): previous_item = A[index - 1] item = A[index] if not is_monotonic(previous_item, item): return False return True
monotonic-array
Python O(n) runtime O(1) space complexity
agconti
0
156
monotonic array
896
0.583
Easy
14,536
https://leetcode.com/problems/increasing-order-search-tree/discuss/526258/Python-O(n)-sol-by-DFS-90%2B-w-Diagram
class Solution: def increasingBST(self, root: TreeNode) -> TreeNode: prev_node = None def helper( node: TreeNode): if node.right: helper( node.right ) # prev_novde always points to next larger element for current node nonlocal prev_node # update right link points to next larger element node.right = prev_node # break the left link of next larger element if prev_node: prev_node.left = None # update previous node as current node prev_node = node if node.left: helper( node.left) # --------------------------------------- helper( root ) return prev_node
increasing-order-search-tree
Python O(n) sol by DFS 90%+ [w/ Diagram ]
brianchiang_tw
4
690
increasing order search tree
897
0.785
Easy
14,537
https://leetcode.com/problems/increasing-order-search-tree/discuss/1955004/Python-3-or-Iterative-in-order-traversal-or-Explanation
class Solution: def increasingBST(self, root: TreeNode) -> TreeNode: node = root stack = [] prev = None lowest = None while stack or node: if node: stack.append(node) node = node.left else: node = stack.pop() if not lowest: lowest = node node.left = None if prev: prev.right = node prev = node node = node.right return lowest
increasing-order-search-tree
Python 3 | Iterative in-order traversal | Explanation
idontknoooo
3
346
increasing order search tree
897
0.785
Easy
14,538
https://leetcode.com/problems/increasing-order-search-tree/discuss/2681240/92-Accepted-Solution-or-Simple-and-Easy-Approach-or-Python
class Solution(object): def increasingBST(self, root): def dfs(root, ans): if not root: return dfs(root.left, ans) ans.append(root.val) dfs(root.right, ans) return ans ans = dfs(root, []) root = right = TreeNode(0) for i in range(len(ans)): right.right = TreeNode(ans[i]) right = right.right return root.right
increasing-order-search-tree
92% Accepted Solution | Simple and Easy Approach | Python
its_krish_here
1
295
increasing order search tree
897
0.785
Easy
14,539
https://leetcode.com/problems/increasing-order-search-tree/discuss/1956296/Python-Inorder-Recursive-Solution
class Solution: def inorder(self,root): if not root: return self.inorder(root.left) if self.result==None: self.result=TreeNode(root.val,None,None) self.curr=self.result else: self.curr.right=TreeNode(root.val,None,None) self.curr=self.curr.right #updating the current root of the new Tree self.inorder(root.right) return def increasingBST(self, root: TreeNode) -> TreeNode: self.result=None #root of the new Tree self.curr=None #current root of the new Tree self.inorder(root) return self.result
increasing-order-search-tree
Python Inorder Recursive Solution
HimanshuGupta_p1
1
38
increasing order search tree
897
0.785
Easy
14,540
https://leetcode.com/problems/increasing-order-search-tree/discuss/1351901/Iterative-inorder-traversal-in-python
class Solution: def increasingBST(self, root: TreeNode) -> TreeNode: if not root or not root.left: return root stack, node, prev = [], root, None while node.left: node = node.left new_root, node = node, root while stack or node: while node: stack.append(node) node = node.left node = stack.pop() node.left = None if prev: prev.right = node prev, node = node, node.right return new_root
increasing-order-search-tree
Iterative inorder traversal in python
mousun224
1
173
increasing order search tree
897
0.785
Easy
14,541
https://leetcode.com/problems/increasing-order-search-tree/discuss/1351901/Iterative-inorder-traversal-in-python
class Solution: def increasingBST(self, root: TreeNode) -> TreeNode: if not root or not root.left: return root def inorder_gen(n: TreeNode): if not n: return yield from inorder_gen(n.left) yield n yield from inorder_gen(n.right) node, prev, new_root = root, None, None for node in inorder_gen(root): node.left = None if prev: prev.right = node else: new_root = node prev, node = node, node.right return new_root
increasing-order-search-tree
Iterative inorder traversal in python
mousun224
1
173
increasing order search tree
897
0.785
Easy
14,542
https://leetcode.com/problems/increasing-order-search-tree/discuss/672968/Inorder-Solution-(-faster-than-100.00-)-Memory-Usage%3A-13.9-MB-less-than-39.01
class Solution: def inorder(self, root, last_node): if root.left is None and root.right is None: return (root, root) if root.left: main_root, last_node = self.inorder(root.left, last_node) root.left = None last_node.right = root last_node = last_node.right else: main_root, last_node = root, root if root.right: right_node, right_last = self.inorder(root.right, last_node) root.right = None last_node.right = right_node last_node = right_last return (main_root, last_node) def increasingBST(self, root: TreeNode) -> TreeNode: return None if root is None else self.inorder(root, root)[0]
increasing-order-search-tree
Inorder Solution ( faster than 100.00% ) Memory Usage: 13.9 MB, less than 39.01%
hasan08sust
1
93
increasing order search tree
897
0.785
Easy
14,543
https://leetcode.com/problems/increasing-order-search-tree/discuss/396894/Python3-inorder-traversal-O(N)
class Solution: def increasingBST(self, root: TreeNode) -> TreeNode: def inorder(node): if node: yield from inorder(node.right) yield node.val yield from inorder(node.left) head = None #head of list for val in inorder(root): node = TreeNode(val) node.right = head head = node return head
increasing-order-search-tree
[Python3] inorder traversal O(N)
ye15
1
154
increasing order search tree
897
0.785
Easy
14,544
https://leetcode.com/problems/increasing-order-search-tree/discuss/396894/Python3-inorder-traversal-O(N)
class Solution: def increasingBST(self, root: TreeNode) -> TreeNode: ans = temp = None stack = [] node = root while stack or node: if node: stack.append(node) node = node.left continue node = stack.pop() if not ans: ans = temp = node else: temp.right = temp = node node.left = None node = node.right return ans
increasing-order-search-tree
[Python3] inorder traversal O(N)
ye15
1
154
increasing order search tree
897
0.785
Easy
14,545
https://leetcode.com/problems/increasing-order-search-tree/discuss/2614330/Python-Readable-DFS
class Solution: def increasingBST(self, root: TreeNode) -> TreeNode: # we can do that using dfs # and a sentinel node sentinel = TreeNode() # make the dfs self.dfs(root, sentinel) # return the first node return sentinel.right def dfs(self, node, sentinel): # check if we are a lef node: if node is None: return sentinel # go left first sentinel = self.dfs(node.left, sentinel) # delete own left (to not have left children) node.left = None # attach self to the linked list sentinel.right = node sentinel.left = None sentinel = node # go right then sentinel = self.dfs(node.right, sentinel) return sentinel
increasing-order-search-tree
[Python] - Readable DFS
Lucew
0
22
increasing order search tree
897
0.785
Easy
14,546
https://leetcode.com/problems/increasing-order-search-tree/discuss/2481977/Python-simple-inorder-solution
class Solution: def __init__(self): self.inOrder = [] def traversal(self,root): if root is None: return [] if root: self.traversal(root.left) self.inOrder.append(root.val) self.traversal(root.right) return self.inOrder def increasingBST(self, root: TreeNode) -> TreeNode: arr = self.traversal(root) tree = TreeNode(val=arr[0]) node = tree for i in range(1,len(arr)): node.right = TreeNode(val=arr[i]) node = node.right return tree
increasing-order-search-tree
Python simple inorder solution
aruj900
0
43
increasing order search tree
897
0.785
Easy
14,547
https://leetcode.com/problems/increasing-order-search-tree/discuss/1958675/Using-Iteration-or-using-property-of-BST
class Solution: def increasingBST(self, root: TreeNode) -> TreeNode: stack = [] lst = [] while root or stack: if root: stack.append(root) root = root.left else: root = stack.pop() lst.append(root.val) root = root.right node = TreeNode(lst[0], None, None) curr = node for i in range(1, len(lst)): x = TreeNode(lst[i], None, None) curr.right = x curr = curr.right return node
increasing-order-search-tree
Using Iteration | using property of BST
prankurgupta18
0
8
increasing order search tree
897
0.785
Easy
14,548
https://leetcode.com/problems/increasing-order-search-tree/discuss/1958532/inorder-traverse-relinking-or-recursive-and-iterative-or-Python3
class Solution: def increasingBST(self, root: TreeNode) -> TreeNode: def inorder(node: Optional[TreeNode]): if node: inorder(node.left) node.left = None self.cur.right = node self.cur = node inorder(node.right) self.cur = TreeNode() head = self.cur inorder(root) return head.right
increasing-order-search-tree
inorder traverse relinking | recursive and iterative | Python3
qihangtian
0
16
increasing order search tree
897
0.785
Easy
14,549
https://leetcode.com/problems/increasing-order-search-tree/discuss/1958532/inorder-traverse-relinking-or-recursive-and-iterative-or-Python3
class Solution: def increasingBST(self, root: TreeNode) -> TreeNode: stack = collections.deque() node = root # linklist cur = TreeNode() new_root = cur while node or stack: if node: stack.append(node) node = node.left else: temp = stack.pop() # visit temp start temp.left = None cur.right = temp cur = cur.right # visit temp end node = temp.right return new_root.right
increasing-order-search-tree
inorder traverse relinking | recursive and iterative | Python3
qihangtian
0
16
increasing order search tree
897
0.785
Easy
14,550
https://leetcode.com/problems/increasing-order-search-tree/discuss/1957336/Python-or-Java-Very-Easy-Solutions
class Solution: def increasingBST(self, root: TreeNode) -> TreeNode: arr = [] # extract def traverse(root): if root is None: return root traverse(root.left) arr.append(root) traverse(root.right) traverse(root) # fill fakeRoot = dummy = TreeNode() for node in arr: node.left = None dummy.right = node dummy = dummy.right return fakeRoot.right
increasing-order-search-tree
✅ Python | Java Very Easy Solutions
dhananjay79
0
57
increasing order search tree
897
0.785
Easy
14,551
https://leetcode.com/problems/increasing-order-search-tree/discuss/1957336/Python-or-Java-Very-Easy-Solutions
class Solution: def increasingBST(self, root: TreeNode) -> TreeNode: self.fakeRoot = self.prev = TreeNode() def traverse(root): if root is None: return traverse(root.left) root.left = None self.prev.right = self.prev = root # append to the right traverse(root.right) traverse(root) return self.fakeRoot.right
increasing-order-search-tree
✅ Python | Java Very Easy Solutions
dhananjay79
0
57
increasing order search tree
897
0.785
Easy
14,552
https://leetcode.com/problems/increasing-order-search-tree/discuss/1957003/Python-SOlution-78-faster
class Solution: def increasingBST(self, root: TreeNode) -> TreeNode: arr = [] def traverse_inorder(node): if not node: return traverse_inorder(node.left) arr.append(node.val) traverse_inorder(node.right) # Get the values of nodes in increasing order by Inorder traversal traverse_inorder(root) # Recreate the BST using the values obtained in the earlier step head = TreeNode() cur_node = head for i in range(len(arr)): new_node = TreeNode(val=arr[i]) cur_node.right = new_node cur_node = new_node return head.right
increasing-order-search-tree
Python SOlution 78% faster
pradeep288
0
34
increasing order search tree
897
0.785
Easy
14,553
https://leetcode.com/problems/increasing-order-search-tree/discuss/1956820/Python-Solution-or-Recursive-Inorder-Based-or-Over-90-Faster
class Solution: def __init__(self): self.dummy = TreeNode(0) self.ptr = self.dummy def inorder(self,root): if root is None: return None self.inorder(root.left) self.ptr.right = TreeNode(root.val) self.ptr = self.ptr.right self.inorder(root.right) def increasingBST(self, root: TreeNode) -> TreeNode: self.inorder(root) return self.dummy.right
increasing-order-search-tree
Python Solution | Recursive Inorder Based | Over 90% Faster
Gautam_ProMax
0
41
increasing order search tree
897
0.785
Easy
14,554
https://leetcode.com/problems/increasing-order-search-tree/discuss/1956694/Python3-recursive-solution
class Solution: def increasingBST(self, root: TreeNode) -> TreeNode: new_tree = TreeNode() def helper(root, new_tree): if not root: return helper(root.right, new_tree) new_tree.right = TreeNode(root.val, right=new_tree.right) helper(root.left, new_tree) new_tree.left = TreeNode(root.val, right=new_tree.right) helper(root, new_tree) return new_tree.right
increasing-order-search-tree
Python3 recursive solution
alessiogatto
0
36
increasing order search tree
897
0.785
Easy
14,555
https://leetcode.com/problems/increasing-order-search-tree/discuss/1956673/java-python-rearrange-nodes-in-tree-(Time-On-space-Ohight_of_tree-)
class Solution: def increasingBST(self, root: TreeNode) -> TreeNode: falsehead = TreeNode() work = falsehead stack = [root] while len(stack) != 0 : current = stack[len(stack)-1] stack.pop() if current.right != None : stack.append(current.right) if current.left != None : stack.append(current) stack.append(current.left) current.left = current.right = None else : work.right = current work = work.right return falsehead.right ```
increasing-order-search-tree
java, python - rearrange nodes in tree (Time On, space Ohight_of_tree )
ZX007java
0
17
increasing order search tree
897
0.785
Easy
14,556
https://leetcode.com/problems/increasing-order-search-tree/discuss/1956294/Python3-Recursive-inorder-traversal-solution
class Solution: def increasingBST(self, root: TreeNode) -> TreeNode: self.new_root = self.head = TreeNode(val = None) def inorder(node): if not node: return inorder(node.left) self.head.right = node self.head.left = None self.head = self.head.right inorder(node.right) inorder(root) self.head.left = None return self.new_root.right ```
increasing-order-search-tree
[Python3] Recursive inorder traversal solution
nandhakiran366
0
9
increasing order search tree
897
0.785
Easy
14,557
https://leetcode.com/problems/increasing-order-search-tree/discuss/1955701/Python-Easy-Python-Solution-Using-Two-Approach
class Solution: def increasingBST(self, root: TreeNode) -> TreeNode: self.ans=[] def helper(node): if node: helper(node.left) self.ans.append(node.val) helper(node.right) helper(root) result=new_node=TreeNode(self.ans[0]) for i in self.ans[1:]: new_node.right=TreeNode(i) new_node=new_node.right return result
increasing-order-search-tree
[ Python ] ✅✅ Easy Python Solution Using Two Approach ✌🥳
ASHOK_KUMAR_MEGHVANSHI
0
66
increasing order search tree
897
0.785
Easy
14,558
https://leetcode.com/problems/increasing-order-search-tree/discuss/1955701/Python-Easy-Python-Solution-Using-Two-Approach
class Solution: def increasingBST(self, root: TreeNode) -> TreeNode: newnode = self.current = TreeNode(None) def CreateTree(value): if self.current.val == None: self.current.val = value else: self.current.right = TreeNode(value) self.current = self.current.right def InOrder(node): if node != None: InOrder(node.left) CreateTree(node.val) InOrder(node.right) InOrder(root) return newnode
increasing-order-search-tree
[ Python ] ✅✅ Easy Python Solution Using Two Approach ✌🥳
ASHOK_KUMAR_MEGHVANSHI
0
66
increasing order search tree
897
0.785
Easy
14,559
https://leetcode.com/problems/increasing-order-search-tree/discuss/1955109/Simple-recursive-Inorder-and-iterative-Morris
class Solution: def increasingBST(self, root: TreeNode, next_node = None) -> TreeNode: if not root: return next_node # pass the curr to left which will act as next for the left subtree left = self.increasingBST(root.left, root) # remove the left of curr root.left = None # build and set the right subtree for curr root.right = self.increasingBST(root.right, next_node) return left
increasing-order-search-tree
Simple recursive Inorder and iterative Morris
constantine786
0
44
increasing order search tree
897
0.785
Easy
14,560
https://leetcode.com/problems/increasing-order-search-tree/discuss/1955109/Simple-recursive-Inorder-and-iterative-Morris
class Solution: def increasingBST(self, root: TreeNode, next_node = None) -> TreeNode: dummy = TreeNode() curr = root node = dummy while curr: if not curr.left: node.right = curr node.left = None curr = curr.right node = node.right else: temp = curr.left while temp.right and temp.right is not curr: temp = temp.right # if we have already processed left subtree, move to right if temp.right is curr: # In morris Traversal, we would have broken this cycle to restore the tree. # But that's not needed here node.right = curr node.left = None curr = curr.right node = node.right else: # set inorder predecessor for curr temp.right = curr curr = curr.left # set the last node's left to None node.left = None return dummy.right
increasing-order-search-tree
Simple recursive Inorder and iterative Morris
constantine786
0
44
increasing order search tree
897
0.785
Easy
14,561
https://leetcode.com/problems/increasing-order-search-tree/discuss/1955093/Python3-recursive-solution-%2B-trick
class Solution: def increasingBST(self, root: TreeNode) -> TreeNode: return self._increasingBST(root)[0] # Just like increasingBST(), but also returns the "tail" (i.e. rightmost descedendent) def _increasingBST(self, root): right_tail = root if root.right: root.right, right_tail = self._increasingBST(root.right) if root.left: updated_left, updated_left_tail = self._increasingBST(root.left) updated_left_tail.right = root root.left = None return updated_left, right_tail return root, right_tail
increasing-order-search-tree
✅ Python3 recursive solution + trick
QuokkaQuokka
0
7
increasing order search tree
897
0.785
Easy
14,562
https://leetcode.com/problems/increasing-order-search-tree/discuss/1955073/Python3-solution-(98-time)
class Solution: def increasingBST(self, root: TreeNode) -> TreeNode: def dfs(node): if node: yield from dfs(node.left) yield node yield from dfs(node.right) cur, nxt = itertools.tee(dfs(root)) head = next(nxt) for c, n in itertools.zip_longest(cur, nxt): c.right = n c.left = None return head
increasing-order-search-tree
Python3 solution (98% time)
dalechoi
0
17
increasing order search tree
897
0.785
Easy
14,563
https://leetcode.com/problems/increasing-order-search-tree/discuss/1936233/Very-simple-python-solution-using-INORDER-traversal
class Solution: def increasingBST(self, root: TreeNode) -> TreeNode: ans = [] def inc(root): if not root: return inc(root.left) ans.append(root.val) inc(root.right) inc(root) h = TreeNode(ans.pop(0)) t = h while len(ans): t.right= TreeNode(ans.pop(0)) t = t.right return h
increasing-order-search-tree
Very simple python solution using INORDER traversal
aashnachib17
0
39
increasing order search tree
897
0.785
Easy
14,564
https://leetcode.com/problems/increasing-order-search-tree/discuss/1910176/Python3-Recursive-%2B-Iterative-Faster-Than-96.94
class Solution: def increasingBST(self, root: TreeNode) -> TreeNode: v = [] def walk(tree): if tree is None: return v.append(tree.val) walk(tree.left) walk(tree.right) walk(root) v = sorted(v) tree = TreeNode(val = v[0]) # Don't lose the basic tree, don't lose it's memroy location temp = tree for node in v[1:]: temp.right = TreeNode(val = node) temp = temp.right return tree
increasing-order-search-tree
Python3, Recursive + Iterative, Faster Than 96.94%
Hejita
0
22
increasing order search tree
897
0.785
Easy
14,565
https://leetcode.com/problems/increasing-order-search-tree/discuss/1812684/Simple-Python-Solution
class Solution: def increasingBST(self, root: TreeNode) -> TreeNode: def addChild(ans, val): if ans.right: addChild(ans.right, val) else: ans.right=TreeNode(val) return def in_order(root): elements=[] if root.left: elements+=in_order(root.left) elements.append(root.val) if root.right: elements+=in_order(root.right) return elements elements=in_order(root) elements.sort() ans=TreeNode(elements[0]) for element in elements[1:]: addChild(ans, element) return ans
increasing-order-search-tree
Simple Python Solution
Siddharth_singh
0
53
increasing order search tree
897
0.785
Easy
14,566
https://leetcode.com/problems/increasing-order-search-tree/discuss/1421816/Python3-Inorder-dfs-with-results
class Solution: def in_order_traversal(self, root): lhead, rtail = root, root if root.left: lhead, ltail = self.in_order_traversal(root.left) ltail.right = root root.left = None if root.right: rhead, rtail = self.in_order_traversal(root.right) root.right = rhead return lhead, rtail def increasingBST(self, root: TreeNode) -> TreeNode: head, tail = self.in_order_traversal(root) return head
increasing-order-search-tree
[Python3] Inorder dfs with results
maosipov11
0
22
increasing order search tree
897
0.785
Easy
14,567
https://leetcode.com/problems/increasing-order-search-tree/discuss/1397701/Python3-Recursive-Solution
class Solution: def __init__(self): self.nodes = [] def inorder(self, root): if root is None: return self.inorder(root.left) self.nodes.append(root) self.inorder(root.right) def increasingBST(self, root: TreeNode) -> TreeNode: self.inorder(root) for i in range(len(self.nodes)): self.nodes[i].left = None self.nodes[i].right = None for i in range(len(self.nodes)-1): self.nodes[i].right = self.nodes[i+1] return self.nodes[0]
increasing-order-search-tree
Python3 - Recursive Solution
harshitgupta323
0
62
increasing order search tree
897
0.785
Easy
14,568
https://leetcode.com/problems/increasing-order-search-tree/discuss/1030455/Python3-solution-beats-95
class Solution: def increasingBST(self, root: TreeNode) -> TreeNode: def inorder(root): if root == None: return inorder(root.left) self.node.right = TreeNode(root.val) self.node = self.node.right inorder(root.right) self.node = self.x = TreeNode() inorder(root) return self.x.right
increasing-order-search-tree
Python3 solution beats 95%
EklavyaJoshi
0
92
increasing order search tree
897
0.785
Easy
14,569
https://leetcode.com/problems/increasing-order-search-tree/discuss/958869/Python3-O(n)-iterative-with-explanation
class Solution(object): def increasingBST(self, root): """ :type root: TreeNode :rtype: TreeNode """ dummy = ListNode(0) cur = dummy stack = [] while root: stack.append(root) root = root.left while stack: node = stack.pop() node.left = None cur.right = node node = node.right cur = cur.right while node: stack.append(node) node = node.left return dummy.right
increasing-order-search-tree
Python3 O(n) iterative with explanation
ethuoaiesec
0
34
increasing order search tree
897
0.785
Easy
14,570
https://leetcode.com/problems/increasing-order-search-tree/discuss/549037/Python-simple-solution-16-ms-faster-than-97.98
class Solution(object): def increasingBST(self, root): """ :type root: TreeNode :rtype: TreeNode """ self.all_nodes = [] def find_all_nodes(root): if root: self.all_nodes.append(root.val) find_all_nodes(root.left) find_all_nodes(root.right) find_all_nodes(root) self.all_nodes.sort() tree = TreeNode(self.all_nodes.pop()) while len(self.all_nodes) > 0: t = TreeNode(self.all_nodes.pop()) t.left = None t.right = tree tree = t return tree
increasing-order-search-tree
Python simple solution 16 ms, faster than 97.98%
hemina
0
376
increasing order search tree
897
0.785
Easy
14,571
https://leetcode.com/problems/bitwise-ors-of-subarrays/discuss/1240982/python-code-with-comment-might-help-to-understand-or
class Solution: def subarrayBitwiseORs(self, arr: List[int]) -> int: ans=set(arr) # each element is a subarry one = set() # to get the ans for the subarray of size >1 # starting from 0th element to the ending element one.add(arr[0]) for i in range(1,len(arr)): two=set() for j in one: two.add(j | arr[i]) # subarray from the element in one set to the current ele(i th one) ans.add(j| arr[i]) two.add(arr[i]) # adding curr element to set two so that from next iteration we can take sub array starting from curr element one = two return len(ans)
bitwise-ors-of-subarrays
python code with comment , might help to understand |
chikushen99
3
423
bitwise ors of subarrays
898
0.369
Medium
14,572
https://leetcode.com/problems/bitwise-ors-of-subarrays/discuss/947105/Python3-set-O(N)
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: ans, vals = set(), set() for x in A: vals = {x | xx for xx in vals} | {x} ans |= vals return len(ans)
bitwise-ors-of-subarrays
[Python3] set O(N)
ye15
3
501
bitwise ors of subarrays
898
0.369
Medium
14,573
https://leetcode.com/problems/orderly-queue/discuss/2783233/Python-Simple-and-Easy-Way-to-Solve-with-Explanation-or-99-Faster
class Solution: def orderlyQueue(self, s: str, k: int) -> str: if k > 1: return "".join(sorted(s)) res = s for i in range(0,len(s)): s = s[1:] + s[0] res = min(res,s) return res
orderly-queue
✔️ Python Simple and Easy Way to Solve with Explanation | 99% Faster 🔥
pniraj657
12
577
orderly queue
899
0.665
Hard
14,574
https://leetcode.com/problems/orderly-queue/discuss/2784607/Easy-python-solution-using-queue-oror-TC%3A-O(K2%2BNlog(N))-SC%3A-O(N)
class Solution: def orderlyQueue(self, s: str, k: int) -> str: st="" lst=list(s) lst.sort() queue=list(s) flg=defaultdict(lambda :0) if k==1: pt=[z for z in range(len(lst)) if s[z]==lst[0]] mn=s[pt[0]:]+s[:pt[0]] for p in range(len(pt)): mn=min(mn,s[pt[p]:]+s[:pt[p]]) return mn ct=k if k==len(s): return "".join(lst) while k>0: if queue[0][0]==lst[0]: st+=queue.pop(0)[0] lst.pop(0) k-=1 for nm in flg: flg[nm]=0 else: mn=queue[0] ind=0 for i in range(1,min(ct,len(queue)-1)): if queue[i]<mn and queue[i]!=lst[0] and flg[queue[i]]!=1: ind=i x=queue.pop(ind) queue.append(x) flg[x]=1 if ct>1: queue.sort() return st+"".join(queue)
orderly-queue
Easy python solution using queue || TC: O(K^2+Nlog(N)), SC: O(N)
shubham_1307
6
135
orderly queue
899
0.665
Hard
14,575
https://leetcode.com/problems/orderly-queue/discuss/2786411/Python-Simple-Python-9-Line-Solution
class Solution: def orderlyQueue(self, s: str, k: int) -> str: if k==1: ans=s for i in range(len(s)): if ((s+s)[i:i+len(s)])<ans: ans=((s+s)[i:i+len(s)]) return ans else: return "".join(sorted(s))
orderly-queue
[ Python ] 🐍🐍 Simple Python 9 Line Solution ✅✅
sourav638
4
20
orderly queue
899
0.665
Hard
14,576
https://leetcode.com/problems/orderly-queue/discuss/2785561/Very-Simple-Approach-Beginner-Friendly!!!!!!
class Solution: def orderlyQueue(self, s: str, k: int) -> str: visited = set() if k == 1: arr = list(s) while "".join(arr) not in visited: visited.add("".join(arr)) ele = arr.pop(0) arr.append(ele) return min(visited) ans = sorted(s) return "".join(ans)
orderly-queue
Very Simple Approach, Beginner Friendly!!!!!!
varunshrivastava2706
2
29
orderly queue
899
0.665
Hard
14,577
https://leetcode.com/problems/orderly-queue/discuss/1445678/Python-Simple-Solution
class Solution: def orderlyQueue(self, s: str, k: int) -> str: if k == 1: return min([s[i:] + s[:i] for i in range(len(s))]) return ''.join(sorted(s))
orderly-queue
[Python] Simple Solution
kyttndr
2
258
orderly queue
899
0.665
Hard
14,578
https://leetcode.com/problems/orderly-queue/discuss/1350871/Python3-rotation-or-sorting
class Solution: def orderlyQueue(self, s: str, k: int) -> str: if k == 1: return min(s[i:] + s[:i] for i in range(len(s))) return "".join(sorted(s))
orderly-queue
[Python3] rotation or sorting
ye15
2
157
orderly queue
899
0.665
Hard
14,579
https://leetcode.com/problems/orderly-queue/discuss/2812777/Python-Easy-Solution-%3A-Expalined
class Solution: def orderlyQueue(self, s: str, k: int) -> str: if k>1: return ''.join(sorted(s)) n=len(s) t=s*2 # t=cbacba ans=s # ans=cba for i in range(1,n): s1=t[i:i+n] # 1st move : s1=t[1:1+3] = bac ans=min(ans,s1) # 2nd move : s1=t[2:2+3] = acb return ans
orderly-queue
Python Easy Solution : Expalined
DareDevil_007
1
4
orderly queue
899
0.665
Hard
14,580
https://leetcode.com/problems/orderly-queue/discuss/2784616/Simple-Solution-beats-97
class Solution: def orderlyQueue(self, s: str, k: int) -> str: if k > 1: return ''.join(sorted(s)) m = s for i in range(1, len(s)): # print(s[i: ]+s[: i]) m = min(m, s[i: ]+s[: i]) return m
orderly-queue
Simple Solution beats 97%
Mencibi
1
11
orderly queue
899
0.665
Hard
14,581
https://leetcode.com/problems/orderly-queue/discuss/2783754/Python-Simple-Python-Solution-Using-Sorting
class Solution: def orderlyQueue(self, s: str, k: int) -> str: new_s = s if k == 1: for i in range(len(s)): s = s[1:]+s[0] new_s = min(new_s, s) else: new_s = sorted(list(s)) result = ''.join(new_s) return result
orderly-queue
[ Python ] ✅✅ Simple Python Solution Using Sorting🥳✌👍
ASHOK_KUMAR_MEGHVANSHI
1
12
orderly queue
899
0.665
Hard
14,582
https://leetcode.com/problems/orderly-queue/discuss/2782847/This-is-not-a-hard-problem-no-ds-or-algorithm-applied
class Solution: def orderlyQueue(self, s: str, k: int) -> str: if k == 1: ans = s for _ in range(len(s)): s = s[1:] + s[0] ans = min(ans, s) return ans else: return ''.join(sorted(s))
orderly-queue
This is not a hard problem [no ds or algorithm applied]
kaichamp101
1
113
orderly queue
899
0.665
Hard
14,583
https://leetcode.com/problems/orderly-queue/discuss/2782758/Python-one-line
class Solution: def orderlyQueue(self, s: str, k: int) -> str: return ( "".join(sorted(s)) if k > 1 else min(s[i:] + s[:i] for i in range(len(s))) )
orderly-queue
Python one line
mjgallag
1
39
orderly queue
899
0.665
Hard
14,584
https://leetcode.com/problems/orderly-queue/discuss/2797743/Optimized-solution-or-Faster-than-91.38
class Solution: def orderlyQueue(self, s: str, k: int) -> str: start = min(s) if k == 1: mn = "z"*len(s) for i in range(len(s)): if s[i] == start: mn = min(s[i:] + s[:i], mn) return mn return ''.join(sorted(s))
orderly-queue
Optimized solution | Faster than 91.38%
HemantRana
0
4
orderly queue
899
0.665
Hard
14,585
https://leetcode.com/problems/orderly-queue/discuss/2786454/Python-one-liner-Proof-k-greater-1-implies-sorting
class Solution: def orderlyQueue(self, s: str, k: int) -> str: return min([s[x:] + s[:x] for x in range(len(s))]) if k == 1 else ''.join(sorted(s))
orderly-queue
Python one-liner, Proof k > 1 implies sorting
YuNoN8
0
10
orderly queue
899
0.665
Hard
14,586
https://leetcode.com/problems/orderly-queue/discuss/2786386/Simple-Two-Case-Solution
class Solution: def orderlyQueue(self, s: str, k: int) -> str: # special case result = s if k == 1: for i in range(len(s)): if s[i] > result[0]: continue option = s[i:] + s[:i] if option < result: result = option return result result = sorted(s) return ''.join(result)
orderly-queue
Simple Two Case Solution
theleastinterestingman
0
4
orderly queue
899
0.665
Hard
14,587
https://leetcode.com/problems/orderly-queue/discuss/2786299/Very-simple-Python-3-code-(Orderly-Queue)
class Solution: def orderlyQueue(self, s: str, k: int) -> str: if k > 1: return ''.join(sorted(s)) Min = s for i in range(len(s)): ss = s[i:]+s[:i] if ss < Min: Min = ss return Min
orderly-queue
Very simple Python 3 code, (Orderly Queue)
meysamalishahi
0
11
orderly queue
899
0.665
Hard
14,588
https://leetcode.com/problems/orderly-queue/discuss/2786287/Python3-Simplified-with-explanation-(97)
class Solution: def orderlyQueue(self, s: str, k: int) -> str: # k = 1 # If we can only move one character at a time, we can try moving each character # and compare to the lexicogtaphically best one we've seen yet # Notes: # - s = s[1:] + s[0] simply pushes the first character to the end # - In python we can compare strings alphabetically using < and >, but min() also works if k == 1: best = s for i in range(len(s)): s = s[1:] + s[0] best = min(best, s) return best # k > 1 # If k is 2 or more, then through some combination of operations we could arrive at any # possible permutation of characters. Therefore, the best option of the combinations will be # the alphabetical arrangement of the characters. We can simply sort the string to get this else: return ''.join(sorted(s))
orderly-queue
[Python3] Simplified with explanation (97%)
connorthecrowe
0
3
orderly queue
899
0.665
Hard
14,589
https://leetcode.com/problems/orderly-queue/discuss/2786219/Intuitional-math-solution
class Solution: def orderlyQueue(self, s: str, k: int) -> str: n = len(s) res = s if k == 1: for i in range(1, n): new = s[i:] + s[:i] if new < res: res = new elif k > 1: tmp = list(res) tmp.sort() res = "".join(tmp) return res
orderly-queue
Intuitional math solution
ilocmvs
0
4
orderly queue
899
0.665
Hard
14,590
https://leetcode.com/problems/orderly-queue/discuss/2786063/easy-solution-in-python
class Solution: def orderlyQueue(self, s: str, k: int) -> str: st=list(s) seen=set() if k==1: while ''.join(st) not in seen: seen.add(''.join(st)) dt=st.pop(0) st.append(dt) return min(seen) else: return ''.join(sorted(st))
orderly-queue
easy solution in python
immortal101
0
12
orderly queue
899
0.665
Hard
14,591
https://leetcode.com/problems/orderly-queue/discuss/2785870/Fast-solution-beats-97
class Solution: def orderlyQueue(self, s: str, k: int) -> str: if k==1: c0=min(s) return min(s[i:]+s[:i] for i, c in enumerate(s) if c==c0) else: return "".join(sorted(list(s)))
orderly-queue
Fast solution, beats 97%
mbeceanu
0
6
orderly queue
899
0.665
Hard
14,592
https://leetcode.com/problems/orderly-queue/discuss/2785841/Simple-Python-Solution-oror-Sorting-oror-Easy-Understanding
class Solution: def orderlyQueue(self, s: str, k: int) -> str: def K_is_1(s): n = len(s); final = s; for i in range(n): s = (s[1:n] + s[0]); final = min(s, final); return final if (k == 1): return K_is_1(s); s = [char for char in s]; s.sort(); return "".join(s);
orderly-queue
Simple Python Solution || Sorting || Easy Understanding
avinashdoddi2001
0
22
orderly queue
899
0.665
Hard
14,593
https://leetcode.com/problems/orderly-queue/discuss/2785798/Easy-to-understand-k-2-case
class Solution: def orderlyQueue(self, s: str, k: int) -> str: if k == 1: # rotate! return min([s[i:] + s[:i] for i in range(len(s))]) else: # sort! return "".join(sorted(s))
orderly-queue
Easy to understand k == 2 case
naubull2
0
8
orderly queue
899
0.665
Hard
14,594
https://leetcode.com/problems/orderly-queue/discuss/2785190/Python-EASY-based-on-sorting
class Solution: def orderlyQueue(self, s: str, k: int) -> str: if k == 1: ans = s[:] minc = s[0] for x in range(len(s)): if s[x] <= minc: minc = s[x] ans = min(ans, s[x:]+s[:x]) return ans return ''.join(sorted(list(s)))
orderly-queue
Python EASY based on sorting
nanorex
0
12
orderly queue
899
0.665
Hard
14,595
https://leetcode.com/problems/orderly-queue/discuss/2785143/899.-Orderly-Queue-or-Simple-Solution-or-Python-or-O(N)
class Solution: def orderlyQueue(self, s: str, k: int) -> str: if k > 1: return ''.join(sorted(s)) else: min_ch = min(s) candi = set() for i, ch in enumerate(s): if ch == min_ch: candi.add(s[i:] + s[:i]) return min(candi)
orderly-queue
899. Orderly Queue | Simple Solution | Python | O(N)
VaishnveeShinde
0
16
orderly queue
899
0.665
Hard
14,596
https://leetcode.com/problems/orderly-queue/discuss/2785104/Accepted-Easiest-Soln-in-Python-or-Best-Approch
class Solution: def orderlyQueue(self, s: str, k: int) -> str: if k==1: n=len(s) doublestr=s+s ans=s for i in range(1,n): s1=doublestr[i:n+i] print(s1) if s1<ans: ans=s1 return ans else: #if k>1 it will always get sorted res = ''.join(sorted(s)) return res
orderly-queue
Accepted Easiest Soln in Python | Best Approch 💯✅✅
adarshg04
0
20
orderly queue
899
0.665
Hard
14,597
https://leetcode.com/problems/orderly-queue/discuss/2784920/Simple-solution.-O(N)
class Solution: def orderlyQueue(self, s: str, k: int) -> str: if k > 1: return ''.join(sorted(s)) else: min_ch = min(s) candi = set() for i, ch in enumerate(s): if ch == min_ch: candi.add(s[i:] + s[:i]) return min(candi)
orderly-queue
Simple solution. O(N)
nonchalant-enthusiast
0
12
orderly queue
899
0.665
Hard
14,598
https://leetcode.com/problems/orderly-queue/discuss/2784900/Python3-Sliding-Window-Solution!-faster-98!
class Solution: def orderlyQueue(self, s: str, k: int) -> str: if k == 1: s_new = s + s # find min string of length len(s) min_so_far = 'z' * (len(s)) l, r = 0, len(s) - 1 # initalize left and right pointer making a window of size len(s) while r < len(s_new): # until the right pointer reaches end of new string if s_new[l:r+1] < min_so_far: min_so_far = s_new[l:r+1] l+=1 r+=1 return min_so_far else: return ''.join(sorted(s))
orderly-queue
✅✅ [Python3] Sliding Window Solution! faster 98%!
aboueleyes
0
14
orderly queue
899
0.665
Hard
14,599