post_href
stringlengths
57
213
python_solutions
stringlengths
71
22.3k
slug
stringlengths
3
77
post_title
stringlengths
1
100
user
stringlengths
3
29
upvotes
int64
-20
1.2k
views
int64
0
60.9k
problem_title
stringlengths
3
77
number
int64
1
2.48k
acceptance
float64
0.14
0.91
difficulty
stringclasses
3 values
__index_level_0__
int64
0
34k
https://leetcode.com/problems/find-the-difference-of-two-arrays/discuss/2462302/Python-for-beginners-3solutions-brute-to-optimized-solutions
class Solution: def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: #Runtime:311ms lis1,lis2=set(nums1),set(nums2) return [list(lis1-lis2),list(lis2-lis1)] """ #Runtime:370ms lis1=list(set(list(nums1))-set(list(nums2))) lis2=list(set(list(nums2))-set(list(nums1))) return [lis1,lis2] """ """ #Runtime:1252ms lis1,lis2=[],[] for i in nums1: if i not in nums2: lis1.append(i) for i in nums2: if i not in nums1: lis2.append(i) lis1,lis2=list(set(lis1)),list(set(lis2)) return [lis1,lis2] """
find-the-difference-of-two-arrays
Python for beginners 3solutions brute to optimized solutions
mehtay037
0
36
find the difference of two arrays
2,215
0.693
Easy
30,700
https://leetcode.com/problems/find-the-difference-of-two-arrays/discuss/2427921/92.85-faster-in-python
class Solution: def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: n = set(nums1) m = set(nums2) return [list(n - m), list(m - n)]
find-the-difference-of-two-arrays
92.85 % faster in python
ankurbhambri
0
36
find the difference of two arrays
2,215
0.693
Easy
30,701
https://leetcode.com/problems/find-the-difference-of-two-arrays/discuss/2395198/using-set-intersection
class Solution: def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: # have the result be a 2D array where the first element is unique in nums1 and the second is # unique in nums2 # redefine nums1 & nums2 of a set to find the intersection between them # iterate nums1 set and every number that is not in the intersection add to 1st element in res # iterate nums2 set and every number that is not in the intersection add to 2nd element in res # Time O(M + N) Space: O(M + N) nums1, nums2 = set(nums1), set(nums2) common = nums1.intersection(nums2) res = [[], []] for val in nums1: if val not in common: res[0].append(val) for val in nums2: if val not in common: res[1].append(val) return res
find-the-difference-of-two-arrays
using set intersection
andrewnerdimo
0
26
find the difference of two arrays
2,215
0.693
Easy
30,702
https://leetcode.com/problems/find-the-difference-of-two-arrays/discuss/2357574/easy-python-solution
class Solution: def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: part_one, part_two = [], [] for itr1 in range(len(nums1)) : if nums1[itr1] not in nums2 : if nums1[itr1] not in part_one : part_one.append(nums1[itr1]) for itr2 in range(len(nums2)) : if nums2[itr2] not in nums1 : if nums2[itr2] not in part_two : part_two.append(nums2[itr2]) return [part_one, part_two]
find-the-difference-of-two-arrays
easy python solution
sghorai
0
25
find the difference of two arrays
2,215
0.693
Easy
30,703
https://leetcode.com/problems/find-the-difference-of-two-arrays/discuss/2337257/Python-Solution-Using-Sets
class Solution: def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: return [list(set(nums1).difference(set(nums2))),list(set(nums2).difference(set(nums1)))]
find-the-difference-of-two-arrays
Python Solution Using Sets
tq326
0
17
find the difference of two arrays
2,215
0.693
Easy
30,704
https://leetcode.com/problems/find-the-difference-of-two-arrays/discuss/2147931/Python-Solution-Using-Map
class Solution: def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: nums1_map = Counter(nums1) nums2_map = Counter(nums2) left_arr = [] right_arr = [] for num in nums1_map: if num not in nums2_map: left_arr.append(num) for num in nums2_map: if num not in nums1_map: right_arr.append(num) return [left_arr, right_arr]
find-the-difference-of-two-arrays
Python Solution Using Map
yash921
0
41
find the difference of two arrays
2,215
0.693
Easy
30,705
https://leetcode.com/problems/find-the-difference-of-two-arrays/discuss/2083359/Beginner-Level-Python-With-O(n)-Time
class Solution: from collections import Counter def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: nums1,nums2 = set(nums1),set(nums2) t = Counter(nums1)+Counter(nums2) t = {x:y for x,y in t.items() if y == 1} res = [[],[]] for i in t.keys(): if i in nums1: res[0].append(i) if i in nums2: res[1].append(i) return res
find-the-difference-of-two-arrays
Beginner Level Python With O(n) Time
itsmeparag14
0
61
find the difference of two arrays
2,215
0.693
Easy
30,706
https://leetcode.com/problems/find-the-difference-of-two-arrays/discuss/2060239/Python-or-Beginner-or-Easy-using-hashmap-and-brute-force
class Solution: def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: d1 = {} for n in nums1: d1[n] = 1 + d1.get(n,0) d2 = {} for n in nums2: d2[n] = 1 + d2.get(n,0) res = [] ans1 = [] for k1 in d1.keys(): if k1 not in d2.keys(): ans1.append(k1) res.append(ans1) ans2 = [] for k2 in d2.keys(): if k2 not in d1.keys(): ans2.append(k2) res.append(ans2) return res
find-the-difference-of-two-arrays
Python | Beginner | Easy using hashmap and brute force
__Asrar
0
61
find the difference of two arrays
2,215
0.693
Easy
30,707
https://leetcode.com/problems/find-the-difference-of-two-arrays/discuss/2043539/Python3-One-Line-Solution
class Solution: def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: return [list(set(nums1) - set(nums2)), list(set(nums2) - set(nums1))]
find-the-difference-of-two-arrays
[Python3] One-Line Solution
terrencetang
0
44
find the difference of two arrays
2,215
0.693
Easy
30,708
https://leetcode.com/problems/find-the-difference-of-two-arrays/discuss/2013118/Python-oneliner
class Solution: def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: return [set(nums1)-set(nums2), set(nums2)-set(nums1)]
find-the-difference-of-two-arrays
Python oneliner
StikS32
0
70
find the difference of two arrays
2,215
0.693
Easy
30,709
https://leetcode.com/problems/find-the-difference-of-two-arrays/discuss/1976187/Python-solution-easy-to-understand-better-than-90
class Solution: def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: arr1 = [] arr2 = [] for i in nums1: if i not in nums2: if i not in arr1: arr1.append(i) for i in nums2: if i not in nums1: if i not in arr2: arr2.append(i) output = [arr1, arr2] return output ```
find-the-difference-of-two-arrays
Python solution easy to understand better than 90%
KirillMcQ
0
64
find the difference of two arrays
2,215
0.693
Easy
30,710
https://leetcode.com/problems/find-the-difference-of-two-arrays/discuss/1968708/Python-Easy-Soluction-or-Beats-99-submits
class Solution: def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: set1 = set(nums1) set2 = set(nums2) return [list(set1 - set2), list(set2 - set1)]
find-the-difference-of-two-arrays
[ Python ] Easy Soluction | Beats 99% submits
crazypuppy
0
54
find the difference of two arrays
2,215
0.693
Easy
30,711
https://leetcode.com/problems/find-the-difference-of-two-arrays/discuss/1965961/easy-python-code
class Solution: def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: output = [] temp1 = [] temp2 = [] nums1 = list(set(nums1)) nums2 = list(set(nums2)) for i in nums1: if i not in nums2: temp1.append(i) for i in nums2: if i not in nums1: temp2.append(i) output.append(temp1) output.append(temp2) return output
find-the-difference-of-two-arrays
easy python code
dakash682
0
25
find the difference of two arrays
2,215
0.693
Easy
30,712
https://leetcode.com/problems/find-the-difference-of-two-arrays/discuss/1957941/Python-3-Solution-Simple
class Solution: def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: result = [[], []] for i in range(len(nums1)): if nums1[i] not in nums2: if nums1[i] not in result[0]: result[0].append(nums1[i]) for j in range(len(nums2)): if nums2[j] not in nums1: if nums2[j] not in result[1]: result[1].append(nums2[j]) return result
find-the-difference-of-two-arrays
Python 3 Solution Simple
AprDev2011
0
35
find the difference of two arrays
2,215
0.693
Easy
30,713
https://leetcode.com/problems/find-the-difference-of-two-arrays/discuss/1954227/Python3-simple-solution
class Solution: def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: ans = [[],[]] for i in nums1: if i not in nums2 and i not in ans[0]: ans[0].append(i) for i in nums2: if i not in nums1 and i not in ans[1]: ans[1].append(i) return ans
find-the-difference-of-two-arrays
Python3 simple solution
EklavyaJoshi
0
25
find the difference of two arrays
2,215
0.693
Easy
30,714
https://leetcode.com/problems/find-the-difference-of-two-arrays/discuss/1942551/1-Lines-Python-Solution-oror-95-Faster
class Solution: def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: return [set(nums1)-set(nums2),set(nums2)-set(nums1)]
find-the-difference-of-two-arrays
1-Lines Python Solution || 95% Faster
Taha-C
0
50
find the difference of two arrays
2,215
0.693
Easy
30,715
https://leetcode.com/problems/find-the-difference-of-two-arrays/discuss/1937004/Python-(Simple-Approach-and-Beginner-Friendly)
class Solution: def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: output = [] a = [] b = [] for i in nums1: if i not in nums2: a.append(i) output.append(set(a)) for j in nums2: if j not in nums1: b.append(j) output.append(set(b)) return output
find-the-difference-of-two-arrays
Python (Simple Approach and Beginner-Friendly)
vishvavariya
0
40
find the difference of two arrays
2,215
0.693
Easy
30,716
https://leetcode.com/problems/find-the-difference-of-two-arrays/discuss/1924153/Python-One-Line!
class Solution: def findDifference(self, a, b): return [set(a)-set(b), set(b)-set(a)]
find-the-difference-of-two-arrays
Python - One Line!
domthedeveloper
0
48
find the difference of two arrays
2,215
0.693
Easy
30,717
https://leetcode.com/problems/find-the-difference-of-two-arrays/discuss/1922673/Python-Solution-One-Liner-or-Set-Difference-Based-or-Over-95-Faster
class Solution: def findDifference(self, n1: List[int], n2: List[int]) -> List[List[int]]: # one liner return [list(set(n1) - set(n2)),list(set(n2) - set(n1))]
find-the-difference-of-two-arrays
Python Solution - One Liner | Set Difference Based | Over 95% Faster
Gautam_ProMax
0
22
find the difference of two arrays
2,215
0.693
Easy
30,718
https://leetcode.com/problems/find-the-difference-of-two-arrays/discuss/1922207/Python-easy-to-read-and-understand-or-hashmap
class Solution: def solve(self, n1, n2): d = {} for val in n1: d[val] = d.get(val, 1) for val in n2: if val in d: del d[val] res = [] for key in d: res.append(key) return res def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: d = {} ans = [] ans.append(self.solve(nums1, nums2)) ans.append(self.solve(nums2, nums1)) return ans
find-the-difference-of-two-arrays
Python easy to read and understand | hashmap
sanial2001
0
42
find the difference of two arrays
2,215
0.693
Easy
30,719
https://leetcode.com/problems/find-the-difference-of-two-arrays/discuss/1915284/Python-Step-By-Step-Easy-To-Understand
class Solution: def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: m1, m2 = {}, {} res1, res2 = set(), set() for element in nums1: m1[element] = 1 for element in nums2: m2[element] = 1 for i in nums1: if m2.get(i) is None: res1.add(i) for i in nums2: if m1.get(i) is None: res2.add(i) return [list(res1), list(res2)]
find-the-difference-of-two-arrays
Python, Step By Step Easy To Understand
Hejita
0
31
find the difference of two arrays
2,215
0.693
Easy
30,720
https://leetcode.com/problems/find-the-difference-of-two-arrays/discuss/1897776/Python-Just-Set-difference
class Solution: def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: s1, s2 = set(nums1), set(nums2) a1 = [n for n in s1 - s2] a2 = [n for n in s2 - s1] return [a1, a2]
find-the-difference-of-two-arrays
[Python] Just Set difference
casshsu
0
14
find the difference of two arrays
2,215
0.693
Easy
30,721
https://leetcode.com/problems/find-the-difference-of-two-arrays/discuss/1892001/python3-oror-set-difference-oror-O(n)O(n)
class Solution: def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: a, b = set(nums1), set(nums2) return [list(a - b), list(b - a)]
find-the-difference-of-two-arrays
python3 || set difference || O(n)/O(n)
dereky4
0
34
find the difference of two arrays
2,215
0.693
Easy
30,722
https://leetcode.com/problems/find-the-difference-of-two-arrays/discuss/1891608/PYTHON-or-EASY-SOLUTION
class Solution: def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: n1,n2 = set(nums1),set(nums2) return [list(n1.difference(n2)),list(n2.difference(n1))]
find-the-difference-of-two-arrays
PYTHON | EASY SOLUTION
rohitkhairnar
0
29
find the difference of two arrays
2,215
0.693
Easy
30,723
https://leetcode.com/problems/find-the-difference-of-two-arrays/discuss/1890956/easy-and-efficient-python3-solution-184ms-and-faster-than-100
class Solution: def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: ans1=[] ans2=[] ans3=[] nums1=set(nums1) nums2=set(nums2) for i in nums1: if i not in nums2: ans1.append(i) for i in nums2: if i not in nums1: ans2.append(i) ans3.append(ans1) ans3.append(ans2) return ans3
find-the-difference-of-two-arrays
easy and efficient python3 solution 184ms and faster than 100%
sangeethbatman
0
17
find the difference of two arrays
2,215
0.693
Easy
30,724
https://leetcode.com/problems/find-the-difference-of-two-arrays/discuss/1890249/Python3-set-diff
class Solution: def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: s1, s2 = set(nums1), set(nums2) return [s1-s2, s2-s1]
find-the-difference-of-two-arrays
[Python3] set diff
ye15
0
15
find the difference of two arrays
2,215
0.693
Easy
30,725
https://leetcode.com/problems/find-the-difference-of-two-arrays/discuss/1888750/Python-using-set-difference-directly
class Solution: def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: return [set(nums1) - set(nums2), set(nums2) - set(nums1)]
find-the-difference-of-two-arrays
Python, using set difference directly
blue_sky5
0
13
find the difference of two arrays
2,215
0.693
Easy
30,726
https://leetcode.com/problems/find-the-difference-of-two-arrays/discuss/1888750/Python-using-set-difference-directly
class Solution: def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: s1 = set(nums1) s2 = set() common = set() r2 = [] for n2 in nums2: if n2 in s1: s1.remove(n2) common.add(n2) elif n2 in common: pass elif n2 not in s2: s2.add(n2) r2.append(n2) return [list(s1), r2]
find-the-difference-of-two-arrays
Python, using set difference directly
blue_sky5
0
13
find the difference of two arrays
2,215
0.693
Easy
30,727
https://leetcode.com/problems/find-the-difference-of-two-arrays/discuss/1887179/Python3-or-Using-sets
class Solution: def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: set1 = set(nums1) set2 = set(nums2) ans = [] temp = [] for num in set1: if num not in set2: temp.append(num) ans.append(temp) temp = [] for num in set2: if num not in set1: temp.append(num) ans.append(temp) return ans
find-the-difference-of-two-arrays
Python3 | Using sets
goyaljatin9856
0
6
find the difference of two arrays
2,215
0.693
Easy
30,728
https://leetcode.com/problems/find-the-difference-of-two-arrays/discuss/1887075/Python-or-Counter-or-Easy
class Solution: def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: counter1 = Counter(nums1) counter2 = Counter(nums2) answer = [[],[]] for key in counter1.keys(): if key not in counter2: answer[0].append(key) for key in counter2.keys(): if key not in counter1: answer[1].append(key) return answer
find-the-difference-of-two-arrays
Python | Counter | Easy
Mikey98
0
22
find the difference of two arrays
2,215
0.693
Easy
30,729
https://leetcode.com/problems/find-the-difference-of-two-arrays/discuss/1886980/Python-3-Solution-or-One-Liner-or-Easy-Solution
class Solution: def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: return [list(set(nums1)-set(nums2)), list(set(nums2)-set(nums1))]
find-the-difference-of-two-arrays
Python 3 Solution | One Liner | Easy Solution
parthpatel9414
0
24
find the difference of two arrays
2,215
0.693
Easy
30,730
https://leetcode.com/problems/find-the-difference-of-two-arrays/discuss/1886913/Simple-Solution-oror-Set
class Solution: def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: x = set(nums1) y = set(nums2) one = [int(i) for i in x if i not in y] two = [int(i) for i in y if i not in x] res = [one,two] return res;
find-the-difference-of-two-arrays
Simple Solution || Set
abhijeetmallick29
0
14
find the difference of two arrays
2,215
0.693
Easy
30,731
https://leetcode.com/problems/find-the-difference-of-two-arrays/discuss/1886907/Python-3-Easy-solution-Using-Set
class Solution: def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: nums1, nums2 = list(set(nums1)), list(set(nums2)) # Converting to set to ensure only unique values remain new1, new2 = [], [] # New list we'll be adding to for x in nums1: # Compares first list if x not in nums2: new1.append(x) for y in nums2: # Compares second list if y not in nums1: new2.append(y) return [new1, new2] # Combines the two
find-the-difference-of-two-arrays
[Python 3] Easy solution Using Set
IvanTsukei
0
60
find the difference of two arrays
2,215
0.693
Easy
30,732
https://leetcode.com/problems/minimum-deletions-to-make-array-beautiful/discuss/1886918/Python-or-Greedy
class Solution: def minDeletion(self, nums: List[int]) -> int: # Greedy ! # we first only consider requirement 2: nums[i] != nums[i + 1] for all i % 2 == 0 # at the begining, we consider the num on the even index # when we delete a num, we need consider the num on the odd index # then repeat this process # at the end we check the requirement 1: nums.length is even or not n = len(nums) count = 0 # flag is true then check the even index # flag is false then check the odd index flag = True for i in range(n): # check the even index if flag: if i % 2 == 0 and i != n -1 and nums[i] == nums[i + 1]: count += 1 flag = False # check the odd index elif not flag: if i % 2 == 1 and i != n -1 and nums[i] == nums[i + 1]: count += 1 flag = True curLength = n - count return count if curLength % 2 == 0 else count + 1
minimum-deletions-to-make-array-beautiful
Python | Greedy
Mikey98
10
397
minimum deletions to make array beautiful
2,216
0.463
Medium
30,733
https://leetcode.com/problems/minimum-deletions-to-make-array-beautiful/discuss/1887080/Python-Simple-Solution-O(n)-Time-and-O(1)-Space
class Solution: def minDeletion(self, nums: List[int]) -> int: n = len(nums) dels = cnt = 0 for i in range(n - 1): if (cnt % 2) == 0 and (nums[i] == nums[i + 1]): dels += 1 else: cnt += 1 cnt += 1 # Take the last element as it is. dels += cnt & 1 # If final count is odd, delete last element. return dels
minimum-deletions-to-make-array-beautiful
Python Simple Solution O(n) Time and O(1) Space
mrunankmistry52
6
143
minimum deletions to make array beautiful
2,216
0.463
Medium
30,734
https://leetcode.com/problems/minimum-deletions-to-make-array-beautiful/discuss/1903628/Python-3-Two-Pointer-Greedy-Solution
class Solution: def minDeletion(self, nums: List[int]) -> int: i = index = steps = 0 while i < len(nums): if index%2 != 0: index += 1 else: if i == len(nums)-1: index += 1 break if nums[i] == nums[i+1]: steps += 1 else: index += 1 i += 1 return steps if index%2 == 0 else steps+1
minimum-deletions-to-make-array-beautiful
[Python 3] Two Pointer Greedy Solution
hari19041
2
49
minimum deletions to make array beautiful
2,216
0.463
Medium
30,735
https://leetcode.com/problems/minimum-deletions-to-make-array-beautiful/discuss/1895077/Python-Easy
class Solution: def minDeletion(self, nums: List[int]) -> int: res = 0 i = 0 n = len(nums) while i < n-1: if nums[i] == nums[i+1]: res += 1 i += 1 else: i += 2 if nums[n-1] == nums[n-2]: res += 1 if (n - res) % 2: res += 1 return res
minimum-deletions-to-make-array-beautiful
Python - Easy
lokeshsenthilkumar
1
48
minimum deletions to make array beautiful
2,216
0.463
Medium
30,736
https://leetcode.com/problems/minimum-deletions-to-make-array-beautiful/discuss/2285968/Easy-Solution-Stack
class Solution: def minDeletion(self, nums: List[int]) -> int: stack = [] count = 0 for ch in nums: if len(stack)%2 == 0: stack.append(ch) else: if ch == stack[-1]: count+=1 else: stack.append(ch) if len(stack)%2 != 0: count+=1 return count
minimum-deletions-to-make-array-beautiful
Easy Solution Stack
Abhi_009
0
24
minimum deletions to make array beautiful
2,216
0.463
Medium
30,737
https://leetcode.com/problems/minimum-deletions-to-make-array-beautiful/discuss/2179747/python-3-or-simple-O(1)-space-solution
class Solution: def minDeletion(self, nums: List[int]) -> int: i, n, deletions = 1, len(nums), 0 while i < n: if nums[i] == nums[i - 1]: deletions += 1 i += 1 else: i += 2 return deletions + (n - deletions) % 2
minimum-deletions-to-make-array-beautiful
python 3 | simple O(1) space solution
dereky4
0
28
minimum deletions to make array beautiful
2,216
0.463
Medium
30,738
https://leetcode.com/problems/minimum-deletions-to-make-array-beautiful/discuss/2003855/Do-what-hints-say.-Python-O(n)
class Solution: def minDeletion(self, nums: List[int]) -> int: N = len(nums) i = 0 virtual_index = 0 while i < N - 1: if virtual_index % 2 == 0: if nums[i] == nums[i + 1]: # virtually remove this elem which is equal to next nums[i] = -1 i += 1 # don't increment virtual_index, because elements are shifted to the left # filling emptied cells continue i += 1 virtual_index += 1 res = sum(n < 0 for n in nums) if (N - res) % 2 != 0: res += 1 return res
minimum-deletions-to-make-array-beautiful
Do what hints say. Python O(n)
canwe
0
63
minimum deletions to make array beautiful
2,216
0.463
Medium
30,739
https://leetcode.com/problems/minimum-deletions-to-make-array-beautiful/discuss/1987257/Python3-Greedy-O(n)-Solution
class Solution: def minDeletion(self, nums: List[int]) -> int: res = 0 ind = 0 size_t = len(nums) while ind < size_t-1: if nums[ind] == nums[ind + 1]: res += 1 ind += 1 else: ind += 2 return res + (size_t - res) % 2
minimum-deletions-to-make-array-beautiful
Python3 Greedy O(n) Solution
xxHRxx
0
52
minimum deletions to make array beautiful
2,216
0.463
Medium
30,740
https://leetcode.com/problems/minimum-deletions-to-make-array-beautiful/discuss/1943143/2-Python-Solutions
class Solution: def minDeletion(self, nums: List[int]) -> int: cnt=0 ; ans=0 for i in range(len(nums)-1): if nums[i]==nums[i+1] and cnt%2==0: ans+=1 else: cnt+=1 return ans if cnt%2 else ans+1
minimum-deletions-to-make-array-beautiful
2 Python Solutions
Taha-C
0
34
minimum deletions to make array beautiful
2,216
0.463
Medium
30,741
https://leetcode.com/problems/minimum-deletions-to-make-array-beautiful/discuss/1943143/2-Python-Solutions
class Solution: def minDeletion(self, nums: List[int]) -> int: ans=[] for num in nums: if len(ans)%2==0 or num!=ans[-1]: ans.append(num) return len(nums)-len(ans)+len(ans)%2
minimum-deletions-to-make-array-beautiful
2 Python Solutions
Taha-C
0
34
minimum deletions to make array beautiful
2,216
0.463
Medium
30,742
https://leetcode.com/problems/minimum-deletions-to-make-array-beautiful/discuss/1940918/Most-Easy-Solution-To-Understand-With-Step-By-Step-Walkthrough
class Solution: def minDeletion(self, nums: List[int]) -> int: n = len(nums) # find the length of arr i = 0 # we start at index 0 count = 0 # keep track of the number of elements that is removed while i < n - 1: # we break when reached the last element of the arr since we have nothing to the right of the arr to compare with (nums[i] == nums[i + 1]) if i % 2 == 0 and nums[i] == nums[i + 1]: # check beautiful arr condition nums.pop(i) # pop whenever the beautiful arr condition is satisfy count += 1 #increment the number of elements that is removed n -= 1 # decrement the length of the arr since we popped one element from the arr else: i += 1 #if everything to the current index is beautiful, we increase the index by one # We have completed the first step of making sure that `nums[i]` != `nums[i + 1]` for all `i % 2 == 0`. Now we need to make sure that the arr is even. while i == n - 1 and n % 2: # after making sure the arr is beautiful, we need to check that the len(arr) is even. Once the arr is even and beautiful, we break. nums.pop(-1) # we pop from the end to prevent messing up the beautiful arr count += 1 # increase count to keep track of the number of elements that we popped n -= 1 # decrease n which is used to keep track of the length of the arr. return count # return count (the minimum number of elements that needs to be popped to make the arr beautiful)
minimum-deletions-to-make-array-beautiful
Most Easy Solution To Understand With Step-By-Step Walkthrough
danielkua
0
43
minimum deletions to make array beautiful
2,216
0.463
Medium
30,743
https://leetcode.com/problems/minimum-deletions-to-make-array-beautiful/discuss/1902417/Python3-Solution-for-reference
class Solution: def minDeletion(self, nums: List[int]) -> int: N = len(nums) deletes = 0 idx = 0 while idx < N - 1: if (idx - deletes) % 2 == 0 and nums[idx] == nums[idx+1]: tidx = idx + 1 while tidx < N and nums[idx] == nums[tidx]: tidx += 1 deletes += 1 idx = tidx else: idx += 1 return deletes + (N-deletes)%2
minimum-deletions-to-make-array-beautiful
[Python3] Solution for reference
vadhri_venkat
0
16
minimum deletions to make array beautiful
2,216
0.463
Medium
30,744
https://leetcode.com/problems/minimum-deletions-to-make-array-beautiful/discuss/1896507/Python-Easy-Solution-or-99-faster-or-Greedy-Approach
class Solution: def minDeletion(self, nums: List[int]) -> int: delt=0 for i in range(len(nums)-1): if nums[i]==nums[i+1] and (i-delt)%2==0: delt+=1 if (len(nums)-delt)%2==0: return delt else: return delt+1
minimum-deletions-to-make-array-beautiful
Python Easy Solution | 99% faster | Greedy Approach
harshitb93
0
33
minimum deletions to make array beautiful
2,216
0.463
Medium
30,745
https://leetcode.com/problems/minimum-deletions-to-make-array-beautiful/discuss/1890252/Python3-greedy
class Solution: def minDeletion(self, nums: List[int]) -> int: ans = 0 for i in range(len(nums)-1): if nums[i] == nums[i+1] and (i-ans) % 2 == 0: ans += 1 return ans + (len(nums)-ans) % 2
minimum-deletions-to-make-array-beautiful
[Python3] greedy
ye15
0
11
minimum deletions to make array beautiful
2,216
0.463
Medium
30,746
https://leetcode.com/problems/minimum-deletions-to-make-array-beautiful/discuss/1887436/Python3-or-Simple-Approach
class Solution: def minDeletion(self, nums: List[int]) -> int: n = len(nums) if n == 0: return 0 ans = 0 i = 0 while i < n: if i < n and i+1 < n and nums[i] == nums[i+1]: ans += 1 i += 1 else: i += 2 if (n - ans)%2 != 0: ans += 1 return ans
minimum-deletions-to-make-array-beautiful
Python3 | Simple Approach
goyaljatin9856
0
8
minimum deletions to make array beautiful
2,216
0.463
Medium
30,747
https://leetcode.com/problems/minimum-deletions-to-make-array-beautiful/discuss/1887349/Easy-Solution-O(n)-Time
class Solution: def minDeletion(self, nums: List[int]) -> int: ans = 0 for i in range(len(nums) - 1): if (i - ans) % 2 == 0 and nums[i] == nums[i + 1]: ans += 1 return ans if (len(nums) - ans) % 2 == 0 else ans + 1
minimum-deletions-to-make-array-beautiful
Easy Solution - O(n) Time
EdwinJagger
0
8
minimum deletions to make array beautiful
2,216
0.463
Medium
30,748
https://leetcode.com/problems/minimum-deletions-to-make-array-beautiful/discuss/1887065/Python-or-Short-and-simple-or-Just-Counting
class Solution: def minDeletion(self, nums: List[int]) -> int: res = 0 for i in range(len(nums)): # if res % 2 == 0, check even indexes # if res % 2 == 1, check odd indexes if (i + (res % 2)) % 2 == 0 and i + 1 < len(nums) and nums[i] == nums[i + 1]: res += 1 return res + (len(nums) - res) % 2
minimum-deletions-to-make-array-beautiful
Python | Short and simple | Just Counting
celestez
0
23
minimum deletions to make array beautiful
2,216
0.463
Medium
30,749
https://leetcode.com/problems/find-palindrome-with-fixed-length/discuss/1886956/Python-or-simple-and-straightforward
class Solution: def kthPalindrome(self, queries: List[int], intLength: int) -> List[int]: # think the palindromes in half # e.g. len = 4 we only consider the first 2 digits # half: 10, 11, 12, 13, 14, ..., 19, 20, # full: 1001, 1111, 1221, 1331, ... # e.g. len = 5 we consider the first 3 digits # half: 100, 101, 102, ... # full: 10001, 10101, 10201, ... result = [] for i in queries: result.append(self.generatePalindrome(intLength, i)) return result def generatePalindrome(self, length, num): # index start from 0 # e.g. num =1 means we want to find the most smallest palindrome, then its index is 0 # e.g. num =2 means we want to find the second most smallest palindrome, then its index is 1 index = num -1 # if the length is even # we only think about the fisrt half of digits if length % 2 == 0: cur = int('1' + '0' * (length // 2 -1)) maxLength = len(str(cur)) cur += index if len(str(cur)) > maxLength: return -1 else: cur = str(cur) cur = cur + cur[::-1] cur = int(cur) return cur # if the length is odd # we consider first (length // 2 + 1) digits else: cur = int('1' + '0' * (length // 2)) maxLength = len(str(cur)) cur += index if len(str(cur)) > maxLength: return -1 else: cur = str(cur) temp = str(cur)[:-1] cur = cur + temp[::-1] cur = int(cur) return cur
find-palindrome-with-fixed-length
Python | simple and straightforward
Mikey98
6
474
find palindrome with fixed length
2,217
0.343
Medium
30,750
https://leetcode.com/problems/find-palindrome-with-fixed-length/discuss/2178890/Python3-Math-solution-with-explanation
class Solution: def kthPalindrome(self, queries: List[int], intLength: int) -> List[int]: ans = [] # number of palindromic numbers in base k with n digits can be found by the formulas: # k, if n == 1 and (k - 1) * (k ** floor((n - 1) / 2)), if n > 1 # we accumulate numbers for each possible length, starting with 1. # for example: [10, 19, 109, 199, 1099...] limit = [10, 19] for i in range(3, intLength + 1): limit.append(limit[-1] + 9 * (10 ** int((i - 1) / 2))) def helper(num): left = str(num) if intLength % 2 == 0: right = str(num)[::-1] else: right = str(num)[:-1][::-1] return int(left + right) # now we form our palindrome if the serial number is less or equal to the limit, # else we append -1 to our answer. # left part of the desired palindrome can be computed by following: # 10 ** digits + serial_number - 1 if intLength % 2 == 0: digits = intLength // 2 - 1 else: digits = intLength // 2 for i in queries: if i <= limit[intLength - 1]: half = 10 ** digits + i - 1 tmp = helper(half) if len(str(tmp)) <= intLength: ans.append(tmp) else: ans.append(-1) else: ans.append(-1) return ans
find-palindrome-with-fixed-length
Python3 Math solution with explanation
frolovdmn
1
96
find palindrome with fixed length
2,217
0.343
Medium
30,751
https://leetcode.com/problems/find-palindrome-with-fixed-length/discuss/1943697/7-Lines-Python-Solution-oror-93-Faster-oror-Memory-less-than-70
class Solution: def kthPalindrome(self, Q: List[int], k: int) -> List[int]: ans=[] ; s='' ; n=ceil(k/2)-1 for i in Q: x=str(10**n+i-1) if k%2==0: s=x+x[::-1] else: s=x+x[::-1][1:] ans.append(s if len(s)==k else -1) return ans
find-palindrome-with-fixed-length
7-Lines Python Solution || 93% Faster || Memory less than 70%
Taha-C
1
168
find palindrome with fixed length
2,217
0.343
Medium
30,752
https://leetcode.com/problems/find-palindrome-with-fixed-length/discuss/1913402/Explained-Solution-In-easy-ways-step-by-step
class Solution: def kthPalindrome(self, queries: List[int], L: int) -> List[int]: l1=[] st="" # if we have 4 then break it 2 or we have 5 then also to 2 if L%2==0: n=L//2-1 else: n=L//2 # starting from that length like if we have 2 then 10 find the power if we have 3 then 100 continue from that string start=pow(10,n) for i in queries: print(start) # add that queries-1 in the start ans=str(start+i-1) # reverse that string rev=ans[::-1] # if the length is the even simple ad the reverse string if L%2==0: st=ans+rev # other wise add from the 1 index like k=3 string 10 then we have to just add the 1 to make the final string 101 else: st=ans+rev[1:] # if the string length matches the given length then add in the result otherwise -1 if len(st)==L: l1.append(st) else: l1.append(-1) # print(st,l1) return l1
find-palindrome-with-fixed-length
Explained Solution In easy ways step by step
DaRk_hEaRt
1
122
find palindrome with fixed length
2,217
0.343
Medium
30,753
https://leetcode.com/problems/find-palindrome-with-fixed-length/discuss/1887102/Python-easy-solution-or-Beats-100
class Solution: def kthPalindrome(self, queries: List[int], intLength: int) -> List[int]: if intLength == 1: return [ i if i < 10 else -1 for i in queries ] else: start = 10**(intLength//2-1) end = 10**(intLength) res = [] for q in queries: q -= 1 if intLength%2: temp = str(start+q//10 ) + str(q%10) + str(start+q//10 )[::-1] else: temp = str(start+q) +str(start+q )[::-1] temp = int(temp) if int(temp) < end else -1 res.append(temp) return res
find-palindrome-with-fixed-length
Python easy solution | Beats 100%
mshanker
1
83
find palindrome with fixed length
2,217
0.343
Medium
30,754
https://leetcode.com/problems/find-palindrome-with-fixed-length/discuss/2731212/Simple-and-Easy-Solution
class Solution: def kthPalindrome(self, queries: List[int], intLength: int) -> List[int]: rawnum = 10 ** int((intLength-1)/2) ans = [] for x in queries : firsthalf = rawnum + x - 1 stri = str(firsthalf) stri += stri[::-1] if intLength%2 == 0 else stri[:len(stri)-1][::-1] if len(stri) == intLength: ans.append(stri) else: ans.append(-1) return ans
find-palindrome-with-fixed-length
Simple & Easy Solution
vaibhav0077
0
2
find palindrome with fixed length
2,217
0.343
Medium
30,755
https://leetcode.com/problems/find-palindrome-with-fixed-length/discuss/2662790/Fast-python3-solution
class Solution: def create_palindrome(self, base, query, intLength): start = f"{base + query - 1}" addIndex = intLength - len(start) palindrome = f"{start}{start[:addIndex][::-1]}" return int(palindrome) def kthPalindrome(self, queries: List[int], intLength: int) -> List[int]: palindromes = [] base = 10**((intLength-1)//2) for query in queries: no_palins = base*10 - base if no_palins < query: palindromes.append(-1) else: palindrome = self.create_palindrome(base, query, intLength) palindromes.append(palindrome) return palindromes
find-palindrome-with-fixed-length
Fast python3 solution
marbad1994
0
6
find palindrome with fixed length
2,217
0.343
Medium
30,756
https://leetcode.com/problems/find-palindrome-with-fixed-length/discuss/1897455/One-pass-98-speed
class Solution: def kthPalindrome(self, queries: List[int], intLength: int) -> List[int]: half = intLength // 2 odd = intLength % 2 half += odd max_num = pow(10, half) base = max_num // 10 - 1 def query(i: int) -> int: n = base + i if n < max_num: s = str(n) s += s[::-1][odd::] return int(s) else: return -1 return [query(q) for q in queries]
find-palindrome-with-fixed-length
One pass, 98% speed
EvgenySH
0
63
find palindrome with fixed length
2,217
0.343
Medium
30,757
https://leetcode.com/problems/find-palindrome-with-fixed-length/discuss/1896729/95-faster-oror-Easy-Python3-Solution-ororFind-Palindrome-With-Fixed-Length
class Solution: def kthPalindrome(self, q: List[int], l: int) -> List[int]: ans = [] if l%2==0: x = (l//2)-1 else: x = l//2 #print(x) for i in q: a = str(10**x+i-1) b = a[::-1] if l%2==0: a = a+b else: a = a+b[1:] if len(a)==l: ans.append(a) else: ans.append(-1) return ans
find-palindrome-with-fixed-length
95% faster || Easy Python3 Solution ||Find Palindrome With Fixed Length
user8744WJ
0
40
find palindrome with fixed length
2,217
0.343
Medium
30,758
https://leetcode.com/problems/find-palindrome-with-fixed-length/discuss/1896517/Python-Easy-Solution-or-Time-O(n)-or-Fastest-Solution
class Solution: def kthPalindrome(self, queries: List[int], intLength: int) -> List[int]: res=[] if intLength%2==0: pow = (intLength//2)-1 else: pow = intLength//2 for i in queries: ans = str(10**pow + i-1) rev=ans[::-1] if intLength%2==0: ans+=rev else: ans+=rev[1:] if len(ans)==intLength: res.append(int(ans)) else: res.append(-1) return res
find-palindrome-with-fixed-length
Python Easy Solution | Time - O(n) | Fastest Solution
harshitb93
0
42
find palindrome with fixed length
2,217
0.343
Medium
30,759
https://leetcode.com/problems/find-palindrome-with-fixed-length/discuss/1890251/Python3-math
class Solution: def kthPalindrome(self, queries: List[int], intLength: int) -> List[int]: ans = [] end = -1 if intLength&amp;1 else None lo, hi = 10**((intLength-1)//2), 10**((intLength+1)//2) for q in queries: if lo + q - 1 < hi: x = lo + q - 1 ans.append(int(str(x) + str(x)[:end][::-1])) else: ans.append(-1) return ans
find-palindrome-with-fixed-length
[Python3] math
ye15
0
16
find palindrome with fixed length
2,217
0.343
Medium
30,760
https://leetcode.com/problems/find-palindrome-with-fixed-length/discuss/1888235/Python-Solution-or-Beats-memory-100-or-Half-palindrome-length
class Solution: def kthPalindrome(self, queries: List[int], intLength: int) -> List[int]: ans=[] if intLength%2==0: il=intLength//2 else: il=intLength//2+1 start=(10**(il-1)) end=(10**(il))-1 for i in queries: val=start+i-1 if val>end: ans.append(-1) else: ans.append(val) finalans=[] if intLength%2==0: for i in ans: if i==-1: finalans.append(-1) continue val=str(i)+str(i)[::-1] if len(val)>intLength: finalans.append(-1) else: finalans.append(int(val)) else: for i in ans: if i==-1: finalans.append(-1) continue val=str(i)+str(i)[:-1][::-1] if len(val)>intLength: finalans.append(-1) else: finalans.append(int(val)) return finalans
find-palindrome-with-fixed-length
Python Solution | Beats memory 100% | Half palindrome length
RickSanchez101
0
29
find palindrome with fixed length
2,217
0.343
Medium
30,761
https://leetcode.com/problems/find-palindrome-with-fixed-length/discuss/1887732/Python-or-Easy-Approach
class Solution: def solve(self, q, n): toAdd = q-1 leng = n//2 if n % 2 == 0: half = int(('1'+'0'*(leng-1))) hafL = len(str(half)) half += toAdd if len(str(half)) > hafL: return -1 full = str(half) full += full[::-1] return int(full) else: half = int(('1'+'0'*(leng))) hafL = len(str(half)) half += toAdd if len(str(half)) > hafL: return -1 half = str(half) full = str(half)[:-1] half += full[::-1] return int(half) def kthPalindrome(self, queries: List[int], intLength: int) -> List[int]: res = [] for q in queries: res.append(self.solve(q, intLength)) return res
find-palindrome-with-fixed-length
Python | Easy Approach ✅
leet_satyam
0
40
find palindrome with fixed length
2,217
0.343
Medium
30,762
https://leetcode.com/problems/find-palindrome-with-fixed-length/discuss/1887599/Python3-Clear-logic-compact-in-9-lines
class Solution: def kthPalindrome(self, queries: List[int], intLength: int) -> List[int]: def nthPalindrome(n, k): half_len = k // 2 - 1 if k % 2 == 0 else k // 2 left = str(10 ** half_len + n - 1) right = (left if k % 2 == 0 else left[:-1])[::-1] res_str = left + right return int(res_str) if len(res_str) == intLength else -1 return [nthPalindrome(query,intLength) for query in queries]
find-palindrome-with-fixed-length
[Python3] Clear logic compact in 9 lines
Rainyforest
0
23
find palindrome with fixed length
2,217
0.343
Medium
30,763
https://leetcode.com/problems/maximum-value-of-k-coins-from-piles/discuss/1889647/Python-Bottom-up-DP-solution
class Solution: def maxValueOfCoins(self, piles: List[List[int]], k: int) -> int: n, m = len(piles), 0 prefixSum = [] for i in range(n): temp = [0] for j in range(len(piles[i])): temp.append(temp[-1] + piles[i][j]) m += 1 prefixSum.append(temp) if m == k: return sum(temp[-1] for temp in prefixSum) dp = [[0] * (k + 1) for _ in range(n)] for j in range(1, k + 1): if j < len(prefixSum[0]): dp[0][j] = prefixSum[0][j] for i in range(1, n): for j in range(1, k + 1): for l in range(len(prefixSum[i])): if l > j: break dp[i][j] = max(dp[i][j], prefixSum[i][l] + dp[i - 1][j - l]) return dp[n - 1][k]
maximum-value-of-k-coins-from-piles
[Python] Bottom-up DP solution
xil899
9
407
maximum value of k coins from piles
2,218
0.48
Hard
30,764
https://leetcode.com/problems/maximum-value-of-k-coins-from-piles/discuss/2313787/HELP-NEEDED
class Solution: def maxValueOfCoins(self, piles: List[List[int]], k: int) -> int: n = len(piles) cache = {} # <--------------- Caching using dictionary def dp(index, remain_k): if remain_k == 0: return 0 if index == n: return 0 if (index, remain_k) in cache: return cache[(index, remain_k)] ans = 0 ans = max(ans, dp(index+1, remain_k)) running_prefix = 0 for i in range(1, min(remain_k, len(piles[index]))+1): running_prefix += piles[index][i-1] ans = max(ans, running_prefix+dp(index+1, remain_k-i)) cache[(index, remain_k)] = ans return ans return dp(0, k)
maximum-value-of-k-coins-from-piles
HELP NEEDED
gabhinav001
0
18
maximum value of k coins from piles
2,218
0.48
Hard
30,765
https://leetcode.com/problems/maximum-value-of-k-coins-from-piles/discuss/2313787/HELP-NEEDED
class Solution: def maxValueOfCoins(self, piles: List[List[int]], k: int) -> int: n = len(piles) cache = [[-1 for _ in range(k+1)] for _ in range(n)] # <--------------- Caching using list def dp(index, remain_k): if remain_k == 0: return 0 if index == n: return 0 if cache[index][remain_k] != -1: return cache[index][remain_k] ans = 0 ans = max(ans, dp(index+1, remain_k)) running_prefix = 0 for i in range(1, min(remain_k, len(piles[index]))+1): running_prefix += piles[index][i-1] ans = max(ans, running_prefix+dp(index+1, remain_k-i)) cache[index][remain_k] = ans return ans return dp(0, k)
maximum-value-of-k-coins-from-piles
HELP NEEDED
gabhinav001
0
18
maximum value of k coins from piles
2,218
0.48
Hard
30,766
https://leetcode.com/problems/maximum-value-of-k-coins-from-piles/discuss/1890250/Python3-dp
class Solution: def maxValueOfCoins(self, piles: List[List[int]], k: int) -> int: @cache def fn(i, k): """Return """ if i == len(piles) or k == 0: return 0 ans = fn(i+1, k) prefix = 0 for j in range(min(k, len(piles[i]))): prefix += piles[i][j] ans = max(ans, prefix + fn(i+1, k-j-1)) return ans return fn(0, k)
maximum-value-of-k-coins-from-piles
[Python3] dp
ye15
0
16
maximum value of k coins from piles
2,218
0.48
Hard
30,767
https://leetcode.com/problems/maximum-value-of-k-coins-from-piles/discuss/1888912/Python-3-DP-%2B-Prefix-Sum-(optimization)
class Solution: def maxValueOfCoins(self, piles: List[List[int]], k: int) -> int: # sort by the maximum prefix sum p = sorted([list(accumulate(x[:k], initial=0)) for x in piles], key=lambda x: -x[-1]) @lru_cache(None) def dp(i, k): if i == len(p): return 0 ans = 0 for j in range(min(len(p[i]), k+1)): ans = max(ans, p[i][j] + dp(i+1, k-j)) return ans return dp(0, k)
maximum-value-of-k-coins-from-piles
[Python 3] DP + Prefix Sum (optimization?)
chestnut890123
0
41
maximum value of k coins from piles
2,218
0.48
Hard
30,768
https://leetcode.com/problems/minimum-bit-flips-to-convert-number/discuss/2775126/Python-Solution-without-XOR
class Solution: def minBitFlips(self, s: int, g: int) -> int: count = 0 while s or g: if s%2 != g%2: count+=1 s, g = s//2, g//2 return count
minimum-bit-flips-to-convert-number
Python Solution without XOR
keioon
4
134
minimum bit flips to convert number
2,220
0.821
Easy
30,769
https://leetcode.com/problems/minimum-bit-flips-to-convert-number/discuss/1984124/Python-Solution-or-Hamming-Distance-Based-or-One-Liner
class Solution: def minBitFlips(self, start: int, goal: int) -> int: return bin(start ^ goal).count("1")
minimum-bit-flips-to-convert-number
Python Solution | Hamming Distance Based | One Liner
Gautam_ProMax
2
138
minimum bit flips to convert number
2,220
0.821
Easy
30,770
https://leetcode.com/problems/minimum-bit-flips-to-convert-number/discuss/1915306/python-3-O(n)-time-easy-solution
class Solution: def minBitFlips(self, start: int, goal: int) -> int: def get( i): s="" while i!=0: s+="0" i-=1 return s s=bin(start).replace("0b",'') g=bin(goal).replace('0b','') if len(s)<len(g): s=get(len(g)-len(s))+s if len(g)<len(s): g=get(len(s)-len(g))+g c=0 for i in range(len(s)): if s[i]!=g[i]: c+=1 return c
minimum-bit-flips-to-convert-number
python 3 O(n) time easy solution
nileshporwal
1
108
minimum bit flips to convert number
2,220
0.821
Easy
30,771
https://leetcode.com/problems/minimum-bit-flips-to-convert-number/discuss/2848283/Easy-to-understand-Python-using-1-for-loop
class Solution: def minBitFlips(self, start: int, goal: int) -> int: a = bin(start)[2:] c = bin(goal)[2:] new_a = '0' * (32 - len(a)) + a new_c = '0' * (32 - len(c)) + c c = 0 for i in range(len(new_a)): if new_a[i] != new_c[i]: c = c +1 return c
minimum-bit-flips-to-convert-number
Easy to understand - Python using 1 for loop
spraj_123
0
1
minimum bit flips to convert number
2,220
0.821
Easy
30,772
https://leetcode.com/problems/minimum-bit-flips-to-convert-number/discuss/2842417/Solution-using-string-beats-99
class Solution: def minBitFlips(self, start: int, goal: int) -> int: b_start = str(bin(start))[2:] b_goal = str(bin(goal))[2:] ans = 0 if len(b_start) < len(b_goal): b_start = '0' * (len(b_goal) - len(b_start)) + b_start elif len(b_start) > len(b_goal): b_goal = '0' * (len(b_start) - len(b_goal)) + b_goal for i in range(len(b_goal)): if b_goal[i] != b_start[i]: ans += 1 return ans
minimum-bit-flips-to-convert-number
Solution using string, beats 99%
l-e-q
0
2
minimum bit flips to convert number
2,220
0.821
Easy
30,773
https://leetcode.com/problems/minimum-bit-flips-to-convert-number/discuss/2775127/Python-Divmod-Solution
class Solution: def minBitFlips(self, s: int, g: int) -> int: count = 0 while s or g: if s%2 != g%2: count+=1 s, g = s//2, g//2 return count
minimum-bit-flips-to-convert-number
Python Divmod Solution
keioon
0
6
minimum bit flips to convert number
2,220
0.821
Easy
30,774
https://leetcode.com/problems/minimum-bit-flips-to-convert-number/discuss/2723578/Python3-Solution
class Solution: def minBitFlips(self, start: int, goal: int) -> int: bstart = bin(start)[2:] bgoal = bin(goal)[2:] diff = len(bstart) - len(bgoal) if diff < 0: bstart = abs(diff) * '0' + bstart elif diff > 0: bgoal = diff * '0' + bgoal return sum([abs(int(bstart[i])-int(bgoal[i])) for i in range(0,len(bstart))])
minimum-bit-flips-to-convert-number
Python3 Solution
sipi09
0
5
minimum bit flips to convert number
2,220
0.821
Easy
30,775
https://leetcode.com/problems/minimum-bit-flips-to-convert-number/discuss/2722014/Simple-python-code-with-explanation
class Solution: def minBitFlips(self, start, goal): #create a variable to keep count of bits that are different #assign value 0 to the variable (count) count = 0 #untill start or goal == 0 #this while loop will not break while start != 0 or goal != 0 : #store the value of (start&amp;1) in x #x store the value at last bit of start x = (start&amp;1) #store thr value of (goal&amp;1) in y #y store the value at last bit of goal y = (goal&amp;1) #if the both bits (x,y) are equal then xor(x,y) == 0 #if both bits(x,y) are different then xor(x,y) == 1 #if both are different if x ^ y == 1: #then we should increase the count value by1 count +=1 #right shift the bits in start by 1 start >>=1 #right shift the bits in goal by 1 goal >>=1 #return the value of count return count
minimum-bit-flips-to-convert-number
Simple python code with explanation
thomanani
0
7
minimum bit flips to convert number
2,220
0.821
Easy
30,776
https://leetcode.com/problems/minimum-bit-flips-to-convert-number/discuss/2718490/python3-one-line-easy-solution
class Solution: def minBitFlips(self, start: int, goal: int) -> int: return (start ^ goal).bit_count()
minimum-bit-flips-to-convert-number
[python3] one line easy solution
huangweijing
0
4
minimum bit flips to convert number
2,220
0.821
Easy
30,777
https://leetcode.com/problems/minimum-bit-flips-to-convert-number/discuss/2679849/O(k)-complexity-solution-where-k-number-of-bit-that-are-opposite-in-two-input-numbers
class Solution: def minBitFlips(self, start: int, goal: int) -> int: xor_result = start^goal count = 0 while xor_result: xor_result = xor_result &amp; (xor_result-1) count += 1 return count
minimum-bit-flips-to-convert-number
O(k) complexity solution, where k = number of bit that are opposite in two input numbers
cat_woman
0
3
minimum bit flips to convert number
2,220
0.821
Easy
30,778
https://leetcode.com/problems/minimum-bit-flips-to-convert-number/discuss/2614148/Python-Simple-Python-Solution-Using-BIN-Function
class Solution: def minBitFlips(self, start: int, goal: int) -> int: binary_start = bin(start)[2:] binary_goal = bin(goal)[2:] ls = len(binary_start) lg = len(binary_goal) if ls > lg: binary_goal = '0' * (ls - lg) + binary_goal else: binary_start = '0' * (lg - ls) + binary_start result = 0 for index in range(max(ls, lg)): if binary_goal[index] != binary_start[index]: result = result + 1 return result
minimum-bit-flips-to-convert-number
[ Python ] ✅✅ Simple Python Solution Using BIN Function 🥳✌👍
ASHOK_KUMAR_MEGHVANSHI
0
20
minimum bit flips to convert number
2,220
0.821
Easy
30,779
https://leetcode.com/problems/minimum-bit-flips-to-convert-number/discuss/2488470/Python-for-beginners
class Solution: def minBitFlips(self, start: int, goal: int) -> int: #Runtime:45ms a,b=str(bin(start)[2:]),str(bin(goal)[2:]) minimum_len=abs(len(a)-len(b)) count=0 if(len(a)>len(b)): b="0"*minimum_len+b else: a="0"*minimum_len+a for i in range(len(a)): if(a[i]!=b[i]): count+=1 return count
minimum-bit-flips-to-convert-number
Python for beginners
mehtay037
0
35
minimum bit flips to convert number
2,220
0.821
Easy
30,780
https://leetcode.com/problems/minimum-bit-flips-to-convert-number/discuss/2488470/Python-for-beginners
class Solution: def minBitFlips(self, start: int, goal: int) -> int: return (bin(start^goal).count("1"))
minimum-bit-flips-to-convert-number
Python for beginners
mehtay037
0
35
minimum bit flips to convert number
2,220
0.821
Easy
30,781
https://leetcode.com/problems/minimum-bit-flips-to-convert-number/discuss/2244004/Python3-1-line
class Solution: def minBitFlips(self, start: int, goal: int) -> int: return bin(start^goal).count('1')
minimum-bit-flips-to-convert-number
[Python3] 1-line
ye15
0
19
minimum bit flips to convert number
2,220
0.821
Easy
30,782
https://leetcode.com/problems/minimum-bit-flips-to-convert-number/discuss/2143094/Just-bit-operations.-You-don't-need-any-additional-memory-and-type-translation-to-count-the-bits
class Solution: def minBitFlips(self, start: int, goal: int) -> int: d = start ^ goal c = 0 while d: c += d &amp; 1 d >>= 1 return c
minimum-bit-flips-to-convert-number
Just bit operations. You don't need any additional memory and type translation to count the bits
dima62
0
44
minimum bit flips to convert number
2,220
0.821
Easy
30,783
https://leetcode.com/problems/minimum-bit-flips-to-convert-number/discuss/2120152/Python-oror-Easy-Approach
class Solution: def minBitFlips(self, start: int, goal: int) -> int: start_binary = "" goal_binary = "" start_binary = format(start, "b") goal_binary = format(goal, "b") len_start = len(start_binary) len_goal = len(goal_binary) len_check = max(len_start, len_goal) if len_start < len_check: start_binary = "0" *(len_check - len_start) + start_binary if len_goal < len_check: goal_binary = "0" *(len_check - len_goal) + goal_binary n = 0 for i in range(0, len_check, 1): if start_binary[i] != goal_binary[i]: n = n + 1 return n
minimum-bit-flips-to-convert-number
✅Python || Easy Approach
chuhonghao01
0
36
minimum bit flips to convert number
2,220
0.821
Easy
30,784
https://leetcode.com/problems/minimum-bit-flips-to-convert-number/discuss/2117593/faster-than-80.05-of-Python3
class Solution: def minBitFlips(self, start: int, goal: int) -> int: return bin(start ^ goal).count('1')
minimum-bit-flips-to-convert-number
faster than 80.05% of Python3
writemeom
0
33
minimum bit flips to convert number
2,220
0.821
Easy
30,785
https://leetcode.com/problems/minimum-bit-flips-to-convert-number/discuss/1967656/PYTHON-Easy-Solution-oror-Bit-Manipulation-oror-99-Faster.
class Solution: def minBitFlips(self, start: int, goal: int) -> int: a=start^goal l=1 c=0 while(l<=a): if(a &amp; l == l): c+=1 l=l<<1 return(c)
minimum-bit-flips-to-convert-number
PYTHON Easy Solution || Bit-Manipulation || 99% Faster.
saibackinaction1
0
39
minimum bit flips to convert number
2,220
0.821
Easy
30,786
https://leetcode.com/problems/minimum-bit-flips-to-convert-number/discuss/1958782/Python-dollarolution-(99-Faster)
class Solution: def minBitFlips(self, start: int, goal: int) -> int: start, goal = bin(start)[2:], bin(goal)[2:] x, y = len(start),len(goal) if y>x: start = '0'*(y-x) + start elif y<x: goal = '0'*(x-y) + goal count = 0 for i in range(len(start)): if start[i] != goal[i]: count += 1 return count
minimum-bit-flips-to-convert-number
Python $olution (99% Faster)
AakRay
0
36
minimum bit flips to convert number
2,220
0.821
Easy
30,787
https://leetcode.com/problems/minimum-bit-flips-to-convert-number/discuss/1942251/1-Line-Python-Solution-oror-90-Faster-oror-Memory-less-than-98
class Solution: def minBitFlips(self, start: int, goal: int) -> int: return str(bin(start^goal)).count('1')
minimum-bit-flips-to-convert-number
1-Line Python Solution || 90% Faster || Memory less than 98%
Taha-C
0
35
minimum bit flips to convert number
2,220
0.821
Easy
30,788
https://leetcode.com/problems/minimum-bit-flips-to-convert-number/discuss/1935551/Python3-Simple-Solution
class Solution: def minBitFlips(self, start: int, goal: int) -> int: xor = start ^ goal flips = 0 while xor: flips += xor % 2 xor //= 2 return flips
minimum-bit-flips-to-convert-number
[Python3] Simple Solution
terrencetang
0
23
minimum bit flips to convert number
2,220
0.821
Easy
30,789
https://leetcode.com/problems/minimum-bit-flips-to-convert-number/discuss/1926015/Python3-simple-solution
class Solution: def minBitFlips(self, start: int, goal: int) -> int: x = bin(start)[2:] y = bin(goal)[2:] if len(x) < len(y): x = x.zfill(len(y)) elif len(y) < len(x): y = y.zfill(len(x)) count = 0 for i,j in zip(x,y): if i != j: count += 1 return count
minimum-bit-flips-to-convert-number
Python3 simple solution
EklavyaJoshi
0
21
minimum bit flips to convert number
2,220
0.821
Easy
30,790
https://leetcode.com/problems/minimum-bit-flips-to-convert-number/discuss/1924083/Python-Multiple-Solutions-%2B-One-Liner!-Clean-and-Simple
class Solution: def minBitFlips(self, start, goal): flips = 0 while start or goal: start_bit = start &amp; 1 goal_bit = goal &amp; 1 flip = start_bit != goal_bit flips += flip start >>= 1 goal >>= 1 return flips
minimum-bit-flips-to-convert-number
Python - Multiple Solutions + One Liner! Clean and Simple
domthedeveloper
0
33
minimum bit flips to convert number
2,220
0.821
Easy
30,791
https://leetcode.com/problems/minimum-bit-flips-to-convert-number/discuss/1924083/Python-Multiple-Solutions-%2B-One-Liner!-Clean-and-Simple
class Solution: def minBitFlips(self, start, goal): flips = 0 x = start ^ goal while x: flip = x &amp; 1 flips += flip x >>= 1 return flips
minimum-bit-flips-to-convert-number
Python - Multiple Solutions + One Liner! Clean and Simple
domthedeveloper
0
33
minimum bit flips to convert number
2,220
0.821
Easy
30,792
https://leetcode.com/problems/minimum-bit-flips-to-convert-number/discuss/1924083/Python-Multiple-Solutions-%2B-One-Liner!-Clean-and-Simple
class Solution: def minBitFlips(self, start, goal): return (start ^ goal).bit_count()
minimum-bit-flips-to-convert-number
Python - Multiple Solutions + One Liner! Clean and Simple
domthedeveloper
0
33
minimum bit flips to convert number
2,220
0.821
Easy
30,793
https://leetcode.com/problems/minimum-bit-flips-to-convert-number/discuss/1912369/Python-easy-and-beginner-friendly-solution-43-ms-runtime
class Solution: def minBitFlips(self, start: int, goal: int) -> int: start_bin = bin(start)[2:] goal_bin = bin(goal)[2:] if len(start_bin) < len(goal_bin): start_bin = "0" * (len(goal_bin) - len(start_bin)) + start_bin elif len(goal_bin) < len(start_bin): goal_bin = "0" * (len(start_bin) - len(goal_bin)) + goal_bin count = 0 for i in range(len(start_bin)): if start_bin[i] != goal_bin[i]: count += 1 return count
minimum-bit-flips-to-convert-number
Python easy and beginner friendly solution, 43 ms runtime
alishak1999
0
25
minimum bit flips to convert number
2,220
0.821
Easy
30,794
https://leetcode.com/problems/minimum-bit-flips-to-convert-number/discuss/1910697/Python3-Fastest-Solution-On-Board-Faster-Than-100-Using-String-Manpulation
class Solution: def minBitFlips(self, start: int, goal: int) -> int: c, start, goal = 0, bin(start)[2:], bin(goal)[2:] if len(start) > len(goal): goal = (len(start) - len(goal)) * '0' + goal elif len(start) < len(goal): start = (len(goal) - len(start)) * '0' + start for i in range(len(start) - 1, -1, -1): if start[i] != goal[i]: c += 1 return c
minimum-bit-flips-to-convert-number
Python3 Fastest Solution On Board, Faster Than 100% Using String Manpulation
Hejita
0
9
minimum bit flips to convert number
2,220
0.821
Easy
30,795
https://leetcode.com/problems/minimum-bit-flips-to-convert-number/discuss/1907403/Python-Easy-XOR-Solution
class Solution: def minBitFlips(self, start: int, goal: int) -> int: # xor values difference_bits = start ^ goal count = 0 # count 1s while difference_bits != 0: if difference_bits &amp; 1 == 1: count += 1 difference_bits >>= 1 return count
minimum-bit-flips-to-convert-number
[Python] Easy XOR Solution
andrenbrandao
0
34
minimum bit flips to convert number
2,220
0.821
Easy
30,796
https://leetcode.com/problems/minimum-bit-flips-to-convert-number/discuss/1907215/Easy-to-understand-Python-3-Solution
class Solution: def minBitFlips(self, start: int, goal: int) -> int: bstart=bin(start).replace("0b","") bgoal=bin(goal).replace("0b","") diff=len(str(bstart))-len(str(bgoal)) for i in range(abs(diff)): if diff<0: bstart='0'+bstart elif diff>0: bgoal='0'+bgoal counter=0 for i in range(len(bstart)): if bstart[i]!=bgoal[i]: counter+=1 return counter
minimum-bit-flips-to-convert-number
Easy to understand Python 3 Solution
user8795ay
0
18
minimum bit flips to convert number
2,220
0.821
Easy
30,797
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/1909302/Pascal-Triangle
class Solution: def triangularSum(self, nums: List[int]) -> int: return sum(n * comb(len(nums) - 1, i) for i, n in enumerate(nums)) % 10
find-triangular-sum-of-an-array
Pascal Triangle
votrubac
28
3,800
find triangular sum of an array
2,221
0.79
Medium
30,798
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/1909302/Pascal-Triangle
class Solution: def triangularSum(self, nums: List[int]) -> int: res, nCr, n = 0, 1, len(nums) - 1 for r, num in enumerate(nums): res = (res + num * nCr) % 10 nCr = nCr * (n - r) // (r + 1) return res
find-triangular-sum-of-an-array
Pascal Triangle
votrubac
28
3,800
find triangular sum of an array
2,221
0.79
Medium
30,799