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-distance-value-between-two-arrays/discuss/2791041/Python-Solution-(Both-Brute-Force-and-Binary-Search) | class Solution:
def findTheDistanceValue(self, arr1: List[int], arr2: List[int], d: int) -> int:
# Brute force O(N**2) complexity
# result = 0
# for x in arr1:
# for y in arr2:
# if abs(x-y) <= d:
# result += 1
# break
# return len(arr1) - result
arr2.sort()
count = 0
for x in arr1:
first, last = 0, len(arr2)-1
while first <= last:
mid = (first+last)//2
if abs(x-arr2[mid]) <= d:
count +=1
break
elif arr2[mid] > x:
last = mid - 1
else:
first = mid + 1
return len(arr1)- count | find-the-distance-value-between-two-arrays | Python Solution (Both Brute Force and Binary Search) | BhavyaBusireddy | 0 | 2 | find the distance value between two arrays | 1,385 | 0.654 | Easy | 20,800 |
https://leetcode.com/problems/find-the-distance-value-between-two-arrays/discuss/2631015/Python3-Straightforward-with-comments | class Solution:
def findTheDistanceValue(self, arr1: List[int], arr2: List[int], d: int) -> int:
# Set output to the total numer of elements in arr1, and we will decremet it when we find
# values that do not work.
output = len(arr1)
# For each element of arr1, loop through arr2
# if any element in arr2 is within the distance number,
# decrement one from the output, and break the inner loop since we do not want to decrement again if another arr2 value is within distance
for a1 in arr1:
for a2 in arr2:
if abs(a1-a2) <= d:
output -= 1
break
return output | find-the-distance-value-between-two-arrays | [Python3] Straightforward with comments | connorthecrowe | 0 | 6 | find the distance value between two arrays | 1,385 | 0.654 | Easy | 20,801 |
https://leetcode.com/problems/find-the-distance-value-between-two-arrays/discuss/2597107/Python-solution-Brute-Force-Approach | class Solution:
def findTheDistanceValue(self, arr1: List[int], arr2: List[int], d: int) -> int:
count=0
for i in arr1:
if all(abs(i-j)>d for j in arr2):
count+=1
return count | find-the-distance-value-between-two-arrays | Python solution Brute Force Approach | utsa_gupta | 0 | 13 | find the distance value between two arrays | 1,385 | 0.654 | Easy | 20,802 |
https://leetcode.com/problems/find-the-distance-value-between-two-arrays/discuss/2465844/Python-(Simple-and-Beginner-Friendly-Solution) | class Solution:
def findTheDistanceValue(self, arr1: List[int], arr2: List[int], d: int) -> int:
output = []
op = 0
for i in arr1:
count = 0
for j in arr2:
if abs(i-j) <= d:
continue
else:
count+=1
if count == len(arr2):
count = 0
op+=1
return op | find-the-distance-value-between-two-arrays | Python (Simple and Beginner-Friendly Solution) | vishvavariya | 0 | 73 | find the distance value between two arrays | 1,385 | 0.654 | Easy | 20,803 |
https://leetcode.com/problems/find-the-distance-value-between-two-arrays/discuss/2463123/Python-Simple-Solution | class Solution:
def findTheDistanceValue(self, arr1: List[int], arr2: List[int], d: int) -> int:
length1 = len(arr1)
length2 = len(arr2)
count = 0
for i in range(0, length1):
constant = arr1[i]
for j in range(0, length2):
number = arr2[j]
difference = abs(constant - number)
if difference <= d:
count += 1
break
return length1 - count | find-the-distance-value-between-two-arrays | Python Simple Solution | Daksh_7 | 0 | 49 | find the distance value between two arrays | 1,385 | 0.654 | Easy | 20,804 |
https://leetcode.com/problems/find-the-distance-value-between-two-arrays/discuss/2445831/Easy-to-implement | class Solution:
def findTheDistanceValue(self, arr1: List[int], arr2: List[int], d: int) -> int:
ans = len(arr1) # assume all are satifify the distance value
for val1 in arr1: # loop over arr1
for val2 in arr2: # loop over arr2
if abs(val1 - val2) <= d: # Compare val1 and val2
ans -= 1 # if smaller and equal to distance "d", result minues 1
break # Break as already found the invalid one.
return ans | find-the-distance-value-between-two-arrays | Easy to implement | JackalQi | 0 | 24 | find the distance value between two arrays | 1,385 | 0.654 | Easy | 20,805 |
https://leetcode.com/problems/find-the-distance-value-between-two-arrays/discuss/2324514/CPP-or-Python-or-Java-or-O(n-logn) | class Solution:
def findTheDistanceValue(self, arr1: List[int], arr2: List[int], d: int) -> int:
arr2.sort()
count = len(arr1)
for i in arr1:
start, end = 0, len(arr2)-1
while(start <= end):
mid = start + (end-start)//2
if(d >= abs(-i + arr2[mid])):
count -= 1
break
else:
if arr2[mid] > i: end = mid - 1
else: start = mid + 1
return count | find-the-distance-value-between-two-arrays | CPP | Python | Java | O(n logn) | devilmind116 | 0 | 61 | find the distance value between two arrays | 1,385 | 0.654 | Easy | 20,806 |
https://leetcode.com/problems/find-the-distance-value-between-two-arrays/discuss/2037073/Python3-simple-code | class Solution:
def findTheDistanceValue(self, arr1: List[int], arr2: List[int], d: int) -> int:
length = len(arr1)
for n in arr1:
for n2 in arr2:
if not abs(n - n2) > d:
length -= 1
break
return length | find-the-distance-value-between-two-arrays | [Python3] simple code | Shiyinq | 0 | 93 | find the distance value between two arrays | 1,385 | 0.654 | Easy | 20,807 |
https://leetcode.com/problems/find-the-distance-value-between-two-arrays/discuss/2009893/Python-Low-Efficiency-but-Easy-Understand | class Solution:
def findTheDistanceValue(self, arr1: List[int], arr2: List[int], d: int) -> int:
res = 0
for a1 in arr1 :
if all(map(lambda a2:abs(a1-a2)>d, arr2)) :
res += 1
return res | find-the-distance-value-between-two-arrays | [ Python ] Low Efficiency but Easy Understand | crazypuppy | 0 | 68 | find the distance value between two arrays | 1,385 | 0.654 | Easy | 20,808 |
https://leetcode.com/problems/find-the-distance-value-between-two-arrays/discuss/2002027/Python-3-Solution-binary-search | class Solution:
def binary_search(self, nums: List[int], check: int, test: int) -> bool:
low, high = 0, len(nums) - 1
while low <= high:
mid = (low + high) // 2
if abs(check - nums[mid]) <= test:
return False
elif check > nums[mid]:
low = mid + 1
else:
high = mid - 1
return True
def findTheDistanceValue(self, arr1: List[int], arr2: List[int], d: int) -> int:
arr2.sort()
distance = 0
for i in range(len(arr1)):
search_res = self.binary_search(arr2, arr1[i], d)
if search_res:
distance += 1
return distance | find-the-distance-value-between-two-arrays | Python 3 Solution, binary search | AprDev2011 | 0 | 90 | find the distance value between two arrays | 1,385 | 0.654 | Easy | 20,809 |
https://leetcode.com/problems/find-the-distance-value-between-two-arrays/discuss/1909083/Easy-Python-SolutionFind-the-Distance-Value-Between-Two-Arrays | class Solution:
def findTheDistanceValue(self, arr1: List[int], arr2: List[int], d: int) -> int:
res = 0
for i in arr1:
for j in arr2:
if(d >= abs(j-i)):
break
else:
res += 1
return res | find-the-distance-value-between-two-arrays | Easy Python Solution[Find the Distance Value Between Two Arrays] | demonKing_253 | 0 | 103 | find the distance value between two arrays | 1,385 | 0.654 | Easy | 20,810 |
https://leetcode.com/problems/find-the-distance-value-between-two-arrays/discuss/1807795/Simple-Python-Solution-oror-80-Faster-oror-Memory-less-than-90 | class Solution:
def findTheDistanceValue(self, arr1: List[int], arr2: List[int], d: int) -> int:
out = 0
for a1 in arr1:
for a2 in arr2:
if abs(a1-a2) <= d: break
else: out += 1
return out | find-the-distance-value-between-two-arrays | Simple Python Solution || 80% Faster || Memory less than 90% | Taha-C | 0 | 99 | find the distance value between two arrays | 1,385 | 0.654 | Easy | 20,811 |
https://leetcode.com/problems/find-the-distance-value-between-two-arrays/discuss/1742986/Python-dollarolution | class Solution:
def findTheDistanceValue(self, arr1: List[int], arr2: List[int], d: int) -> int:
k, count = 0, 0
for i in arr1:
count += 1
for j in arr2:
if abs(i-j) < d+1:
count -= 1
break
return count | find-the-distance-value-between-two-arrays | Python $olution | AakRay | 0 | 130 | find the distance value between two arrays | 1,385 | 0.654 | Easy | 20,812 |
https://leetcode.com/problems/find-the-distance-value-between-two-arrays/discuss/1672079/Python-O(m%2Bn)-time-O(nd)-space-using-set-no-sort | class Solution:
def findTheDistanceValue(self, arr1: List[int], arr2: List[int], d: int) -> int:
unaccepted = set()
for num in arr2:
unaccepted.update(set(range(num-d, num+d+1)))
count = 0
for num in arr1:
if num not in unaccepted:
count += 1
return count | find-the-distance-value-between-two-arrays | Python, O(m+n) time, O(nd) space, using set, no sort | yaok09 | 0 | 128 | find the distance value between two arrays | 1,385 | 0.654 | Easy | 20,813 |
https://leetcode.com/problems/find-the-distance-value-between-two-arrays/discuss/1502724/Python-linear-time-solution | class Solution:
def findTheDistanceValue(self, arr1: List[int], arr2: List[int], d: int) -> int:
minv = min(arr2) - d
maxv = max(arr2) + d
# table for boolean of integers in range [minv, maxv]
table = [False for _ in range(maxv-minv+1)]
for num in arr2:
# num-d ~ num+d should be covered
for i in range(num-d-minv, num-d-minv+2*d+1):
table[i] = True
res = 0
for m in arr1:
if m > maxv or m < minv or not table[m-minv]:
res += 1
return res | find-the-distance-value-between-two-arrays | Python linear time solution | byuns9334 | 0 | 291 | find the distance value between two arrays | 1,385 | 0.654 | Easy | 20,814 |
https://leetcode.com/problems/find-the-distance-value-between-two-arrays/discuss/1114667/(nlogn)binary-search-using-python-80-faster | class Solution:
def findTheDistanceValue(self, arr1: List[int], arr2: List[int], d: int) -> int:
def bs(arr,cmp):
l,r = 0,len(arr)-1
m = (l + r)//2
while l<=r:
if arr[m] in cmp:
return 0
elif arr[m]< cmp[0]:
l = m+1
elif arr[m]>cmp[len(cmp)-1]:
r = m-1
m = (l+r)//2
return 1
res = 0
arr2.sort()
cnt = 0
for i in range(len(arr1)):
x = range(arr1[i]-d, arr1[i]+d+1)
cnt += 1 if bs(arr2, x) else 0
return cnt | find-the-distance-value-between-two-arrays | (nlogn)binary search using python 80% faster | deleted_user | 0 | 288 | find the distance value between two arrays | 1,385 | 0.654 | Easy | 20,815 |
https://leetcode.com/problems/find-the-distance-value-between-two-arrays/discuss/1093136/Python3-Solution-using-'break'-command-in-loop | class Solution:
def findTheDistanceValue(self, arr1: List[int], arr2: List[int], d: int) -> int:
count = 0
for i in range(len(arr1)):
for j in range(len(arr2)):
if abs(arr1[i] - arr2[j]) <= d:
break # abandon this round of loop, continue to next value in arr1
else:
if j == len(arr2) - 1:
count += 1
return count
``` | find-the-distance-value-between-two-arrays | [Python3] Solution using 'break' command in loop | bsnsus0205 | 0 | 113 | find the distance value between two arrays | 1,385 | 0.654 | Easy | 20,816 |
https://leetcode.com/problems/find-the-distance-value-between-two-arrays/discuss/547360/Python-Nested-loop | class Solution:
def findTheDistanceValue(self, arr1: List[int], arr2: List[int], d: int) -> int:
count, total = 0, len(arr1)
for out in arr1:
for inner in arr2:
if abs(out - inner) <= d:
count += 1
brea
return total - count | find-the-distance-value-between-two-arrays | [Python] Nested loop | dentedghost | 0 | 158 | find the distance value between two arrays | 1,385 | 0.654 | Easy | 20,817 |
https://leetcode.com/problems/cinema-seat-allocation/discuss/1124736/Python3-bitmask | class Solution:
def maxNumberOfFamilies(self, n: int, reservedSeats: List[List[int]]) -> int:
seats = {}
for i, j in reservedSeats:
if i not in seats: seats[i] = 0
seats[i] |= 1 << j-1
ans = 2 * (n - len(seats))
for v in seats.values():
if not int("0111111110", 2) & v: ans += 2
elif not int("0111100000", 2) & v: ans += 1
elif not int("0001111000", 2) & v: ans += 1
elif not int("0000011110", 2) & v: ans += 1
return ans | cinema-seat-allocation | [Python3] bitmask | ye15 | 3 | 256 | cinema seat allocation | 1,386 | 0.409 | Medium | 20,818 |
https://leetcode.com/problems/cinema-seat-allocation/discuss/1574196/Python-Simple-beats-99.5-memory-99-speed | class Solution:
def maxNumberOfFamilies(self, n: int, reservedSeats: List[List[int]]) -> int:
alloc = collections.defaultdict(set)
ct = n*2
while reservedSeats:
x = reservedSeats.pop()
if 1 < x[1] < 6:
alloc[x[0]].add(1)
elif 5 < x[1] < 10:
alloc[x[0]].add(3)
if 3 < x[1] < 8:
alloc[x[0]].add(2)
ct = 2*n
for key, val in alloc.items():
if val=={1,2,3}:
ct-=2
else:
ct-=1
return ct | cinema-seat-allocation | [Python] Simple, beats 99.5% memory, 99% speed | IsaacQ135 | 1 | 747 | cinema seat allocation | 1,386 | 0.409 | Medium | 20,819 |
https://leetcode.com/problems/cinema-seat-allocation/discuss/643042/Python3-save-bitmap-for-the-middle-seats-only-Cinema-Seat-Allocation | class Solution:
def maxNumberOfFamilies(self, n: int, reserved: List[List[int]]) -> int:
r = defaultdict(int)
for row, seat in reserved:
if 2 <= seat <= 9:
r[row] |= 1 << (seat-2)
ans = 0
for _, v in r.items():
ans += int(any(not v & mask for mask in [0xf, 0xf0, 0x3c]))
return ans + (n - len(r)) * 2 | cinema-seat-allocation | Python3 save bitmap for the middle seats only - Cinema Seat Allocation | r0bertz | 1 | 267 | cinema seat allocation | 1,386 | 0.409 | Medium | 20,820 |
https://leetcode.com/problems/cinema-seat-allocation/discuss/2660693/Python-solution-w-comments | class Solution:
def maxNumberOfFamilies(self, n: int, reservedSeats: List[List[int]]) -> int:
res = 0
reserved = set()
rowsTaken = {}
# loop through reserved seats and add any reserved seats and add row to rowsTaken
for r,c in reservedSeats:
reserved.add((r, c))
rowsTaken[r] = None
# helper function to check if these 4 seats are available
def valid(r, seats):
nonlocal res
for seat in seats:
if seat in reserved:
break
if seat == seats[-1]:
reserved.add(seat)
res += 1
# only loop through rows that have reserves seats, cause empty rows will always contain 2
for row in rowsTaken.keys():
valid(row, [(row,2), (row,3), (row,4), (row,5)])
valid(row, [(row,4), (row,5), (row,6), (row,7)])
valid(row, [(row,6), (row, 7), (row,8), (row,9)])
# return our res + the (total rows - rowsTaken) which equals our empty rows, then multiply by two because 2 families can always fit in those rows
return res + ((n - len(rowsTaken)) * 2) | cinema-seat-allocation | Python solution w/ comments | Mark5013 | 0 | 36 | cinema seat allocation | 1,386 | 0.409 | Medium | 20,821 |
https://leetcode.com/problems/cinema-seat-allocation/discuss/2545560/Python3-or-Using-map-to-Solve | class Solution:
def maxNumberOfFamilies(self, n: int, reservedSeats: List[List[int]]) -> int:
seat=0
rs=defaultdict(set)
for i,j in reservedSeats:
rs[i].add(j)
fr=n-len(rs)
for i in rs.keys():
fseat,lseat,mseat=1,1,1
for j in range(2,6):
if j in rs[i]:
fseat=0
break
for k in range(9,5,-1):
if k in rs[i]:
lseat=0
break
if not fseat and not lseat:
for l in range(4,8):
if l in rs[i]:
mseat=0
break
else:
mseat=0
seat+=fseat+lseat+mseat
return seat+(2*fr) | cinema-seat-allocation | [Python3] | Using map to Solve | swapnilsingh421 | 0 | 90 | cinema seat allocation | 1,386 | 0.409 | Medium | 20,822 |
https://leetcode.com/problems/cinema-seat-allocation/discuss/1886718/Bit-operation-python-solution | class Solution:
def maxNumberOfFamilies(self, n: int, reservedSeats: List[List[int]]) -> int:
records = defaultdict(list)
for [a,b] in reservedSeats:
records[a].append(b)
state1 = (1<<1) | (1<<2) | (1<<3) | (1<<4)
state2 = (1<<3) | (1<<4) | (1<<5) | (1<<6)
state3 = (1<<5) | (1<<6) | (1<<7) | (1<<8)
count = 0
for i in records.keys():
state = 0
for k in records[i]:
state |= (1 << k-1)
if state & state1 == 0 and state & state3 == 0:
count += 2
elif state & state1 == 0 or state & state2 == 0 or state & state3 == 0 :
count += 1
count = (n-len(records)) *2 + count
return count | cinema-seat-allocation | Bit operation python solution | cruisekkk | 0 | 70 | cinema seat allocation | 1,386 | 0.409 | Medium | 20,823 |
https://leetcode.com/problems/cinema-seat-allocation/discuss/547368/Python-using-sorted-array | class Solution:
def maxNumberOfFamilies(self, n: int, reservedSeats: List[List[int]]) -> int:
count, reservedSeats, available_rows = 0, sorted(reservedSeats), n
row = reservedSeats[0][0]
available_rows -= 1
row_result = [0] * 10
for seats in reservedSeats:
sit = seats[1]
if row == seats[0]:
row_result[sit-1] = 1
else:
# Calculate
spaces = 0
for idx, chair in enumerate(row_result):
if chair == 0:
spaces += 1
else:
spaces = 0
if spaces >= 4 and idx in (4,6,8):
count += 1
spaces = 0
row = seats[0]
row_result = [0] * 10
row_result[sit-1] = 1
available_rows -= 1
spaces = 0
for idx, chair in enumerate(row_result):
if chair == 0:
spaces += 1
else:
spaces = 0
if spaces >= 4 and idx in (4,6,8):
count += 1
spaces = 0
count = count + (available_rows * 2)
return count | cinema-seat-allocation | [Python] using sorted array | dentedghost | 0 | 127 | cinema seat allocation | 1,386 | 0.409 | Medium | 20,824 |
https://leetcode.com/problems/sort-integers-by-the-power-value/discuss/1597631/Python-using-sorted-function | class Solution:
def getpower(self,num):
p=0
while(num!=1):
if num%2==0:
num=num//2
else:
num=(3*num)+1
p+=1
return p
def getKth(self, lo: int, hi: int, k: int) -> int:
temp=sorted(range(lo,hi+1),key=lambda x:self.getpower(x))
return temp[k-1]
``` | sort-integers-by-the-power-value | Python using sorted function | PrimeOp | 1 | 118 | sort integers by the power value | 1,387 | 0.7 | Medium | 20,825 |
https://leetcode.com/problems/sort-integers-by-the-power-value/discuss/2833166/easy-python | class Solution:
def po(self,x):
if(x==1):
return(0)
a=0
if(x%2==0):
a=self.po(x//2)
else:
a=self.po(x*3+1)
return(a+1)
def getKth(self, lo: int, hi: int, k: int) -> int:
ot=[]
for i in range(lo,hi+1):
ot.append([i,self.po(i)])
newara=sorted(ot, key = lambda x:x[1])
return(newara[k-1][0]) | sort-integers-by-the-power-value | easy python | droj | 0 | 2 | sort integers by the power value | 1,387 | 0.7 | Medium | 20,826 |
https://leetcode.com/problems/sort-integers-by-the-power-value/discuss/2805453/Python-(Simple-Dynamic-Programming) | class Solution:
def dfs(self,n,dict1):
if n in dict1:
return dict1[n]
if n%2 == 0:
dict1[n] = 1 + self.dfs(n//2,dict1)
else:
dict1[n] = 1 + self.dfs(3*n+1,dict1)
return dict1[n]
def getKth(self, lo, hi, k):
ans = [i for i in range(lo,hi+1)]
ans.sort(key = lambda x: self.dfs(x,{1:0}))
return ans[k-1] | sort-integers-by-the-power-value | Python (Simple Dynamic Programming) | rnotappl | 0 | 5 | sort integers by the power value | 1,387 | 0.7 | Medium | 20,827 |
https://leetcode.com/problems/sort-integers-by-the-power-value/discuss/2795680/Python-(Simple-Maths) | class Solution:
@lru_cache(None)
def dfs(self,x):
total = 0
while x > 1:
total += 1
if x%2 == 0:
x = x//2
else:
x = 3*x + 1
return total
def getKth(self, lo, hi, k):
ans = [i for i in range(lo,hi+1)]
ans.sort(key = lambda x: self.dfs(x))
return ans[k-1] | sort-integers-by-the-power-value | Python (Simple Maths) | rnotappl | 0 | 2 | sort integers by the power value | 1,387 | 0.7 | Medium | 20,828 |
https://leetcode.com/problems/sort-integers-by-the-power-value/discuss/2686050/Basic-Python-Solution | class Solution:
def getKth(self, lo: int, hi: int, k: int) -> int:
d={}
def getPower(num):
c=0
while num != 1:
if num % 2 == 0:
num=num//2
c+=1
else:
num=num*3 + 1
c+=1
return c
for i in range(lo,hi+1):
#Function to get the power
a=getPower(i)
d[i]=a
di=sorted(d.items(),key = lambda x:x[1])
ans=list(di)
return ans[k-1][0] | sort-integers-by-the-power-value | Basic Python Solution | beingab329 | 0 | 17 | sort integers by the power value | 1,387 | 0.7 | Medium | 20,829 |
https://leetcode.com/problems/sort-integers-by-the-power-value/discuss/2674092/Python-recursion-%2B-memoization | class Solution:
def getKth(self, lo: int, hi: int, k: int) -> int:
def getPowerValue(n):
if n not in memo:
memo[n] = 1 + (getPowerValue(3*n + 1) if n % 2 else getPowerValue(n//2))
return memo[n]
memo = {1: 0}
values = [(getPowerValue(n), n) for n in range(lo, hi+1)]
return sorted(values)[k-1][1] | sort-integers-by-the-power-value | Python, recursion + memoization | blue_sky5 | 0 | 24 | sort integers by the power value | 1,387 | 0.7 | Medium | 20,830 |
https://leetcode.com/problems/sort-integers-by-the-power-value/discuss/2635564/Python3-Fast-and-Easy-Solution-oror-Memoization-oror-DP-oror-Dynamic-Programming | class Solution:
def getKth(self, lo: int, hi: int, k: int) -> int:
def solve(x):
if x in dp:
return dp[x]
if x == 1:
return 0
if x & 1:
x = 3 * x + 1
else:
x//=2
return 1+solve(x)
dp = {}
for i in range(lo,hi+1):
dp[i] = solve(i)
dp = sorted(dp.items(), key=lambda item: item[1])
return dp[k-1][0]
#Please Upvote if you like the Solution | sort-integers-by-the-power-value | Python3 Fast and Easy Solution || Memoization || DP || Dynamic Programming | hoo__mann | 0 | 25 | sort integers by the power value | 1,387 | 0.7 | Medium | 20,831 |
https://leetcode.com/problems/sort-integers-by-the-power-value/discuss/2544468/Python-HashmapDynamic-Programming | class Solution:
def getKth(self, lo: int, hi: int, k: int) -> int:
#Using Hashmap
#Runtime:886ms
dic,temp={},1
for i in range(lo,hi+1):
number=i
count=0
while number>1:
if(number%2==0):
number=number//2
else:
number=3*number+1
count+=1
dic[i]=count
for k,v in sorted(dic.items(),key=lambda x:x[1]):
if(temp==j):
return k
temp+=1 | sort-integers-by-the-power-value | Python {Hashmap,Dynamic Programming} | mehtay037 | 0 | 58 | sort integers by the power value | 1,387 | 0.7 | Medium | 20,832 |
https://leetcode.com/problems/sort-integers-by-the-power-value/discuss/2544468/Python-HashmapDynamic-Programming | class Solution:
def getKth(self, lo: int, hi: int, k: int) -> int:
#Dynamic Programming
#Runtime:316ms
cache={1:1}
def fn(n):
if n not in cache:
if n%2==0:
cache[n]=1+fn(n//2)
else:
cache[n]=1+fn(3*n+1)
return cache[n]
return sorted((fn(i),i) for i in range(lo,hi+1))[k-1][1] | sort-integers-by-the-power-value | Python {Hashmap,Dynamic Programming} | mehtay037 | 0 | 58 | sort integers by the power value | 1,387 | 0.7 | Medium | 20,833 |
https://leetcode.com/problems/sort-integers-by-the-power-value/discuss/2386845/Python3-Memoization-search-%2B-sorting-approach | class Solution:
def getKth(self, lo: int, hi: int, k: int) -> int:
self.store = {1:0}
items = []
for x in range(lo, hi+1):
self.compute(x)
items.append((self.store[x], x))
items.sort()
return items[k-1][1]
def compute(self, x):
if x in self.store:
return self.store[x]
y = x // 2 if x % 2 == 0 else 3 * x + 1
self.store[x] = 1 + self.compute(y)
return self.store[x] | sort-integers-by-the-power-value | [Python3] Memoization search + sorting approach | BestAC | 0 | 53 | sort integers by the power value | 1,387 | 0.7 | Medium | 20,834 |
https://leetcode.com/problems/sort-integers-by-the-power-value/discuss/2272122/Python3-Recursion-%2B-memoization-to-get-all-powers-sort-and-fetch-kth-value | class Solution:
def getKth(self, lo: int, hi: int, k: int) -> int:
def get_steps(number):
nonlocal memo
if number in memo:
return memo[number]
ans = 0
if number % 2 == 0:
ans += 1 + get_steps(number // 2)
else:
ans += 1 + get_steps((number * 3 + 1))
memo[number] = ans
return ans
memo = {1:0}
all_powers = []
for i in range(lo, hi + 1):
if i not in memo:
get_steps(i)
all_powers.append((memo[i], i))
all_powers.sort(key = lambda x:(x[0], x[1]))
return all_powers[k - 1][1] | sort-integers-by-the-power-value | Python3 - Recursion + memoization to get all powers, sort and fetch kth value | myvanillaexistence | 0 | 12 | sort integers by the power value | 1,387 | 0.7 | Medium | 20,835 |
https://leetcode.com/problems/sort-integers-by-the-power-value/discuss/2271854/Simple-Python-with-Cache-for-Memoization-in-Recursive-Power-Calculation | class Solution:
def getKth(self, lo: int, hi: int, k: int) -> int:
@cache
def power(val):
if val == 1:
return 0
else:
if val % 2 == 0:
return 1 + power(val / 2)
else:
return 1 + power(3 * val + 1)
vals = sorted([(power(val),val) for val in range(lo, hi+1)])
return vals[k-1][1] | sort-integers-by-the-power-value | Simple Python with Cache for Memoization in Recursive Power Calculation | boris17 | 0 | 13 | sort integers by the power value | 1,387 | 0.7 | Medium | 20,836 |
https://leetcode.com/problems/sort-integers-by-the-power-value/discuss/1873234/Python-Solution-or-Dynamic-Programming-or-Store-Powers-and-Sort | class Solution:
def calcPower(self,nums: List[int]):
ans = []
for ele in nums:
count = 0
while ele != 1:
if ele % 2 == 0:
ele //= 2
else:
ele = 3 * ele + 1
count += 1
ans.append(count)
return ans
def getKth(self, lo: int, hi: int, k: int) -> int:
nums = [i for i in range(lo,hi+1)]
powers = self.calcPower(nums)
ans = [x for _,x in sorted(zip(powers,nums))]
return ans[k-1] | sort-integers-by-the-power-value | Python Solution | Dynamic Programming | Store Powers and Sort | Gautam_ProMax | 0 | 48 | sort integers by the power value | 1,387 | 0.7 | Medium | 20,837 |
https://leetcode.com/problems/sort-integers-by-the-power-value/discuss/1839860/WEEB-DOES-PYTHONC%2B%2B-TOP-DOWN-DP(MEMOIZATION) | class Solution:
def getKth(self, lo: int, hi: int, k: int) -> int:
dp = defaultdict(int)
result = []
for num in range(lo, hi+1):
count =0
temp = []
curNum = num
while curNum != 1:
temp.append(curNum)
if curNum in dp:
count += dp[curNum]
break
if curNum % 2 == 0:
curNum = curNum / 2
else:
curNum = curNum * 3 + 1
count += 1
result.append((count, num))
for val in temp:
dp[val] = count
count -=1
result.sort()
return result[k-1][1] | sort-integers-by-the-power-value | WEEB DOES PYTHON/C++ TOP-DOWN DP(MEMOIZATION) | Skywalker5423 | 0 | 51 | sort integers by the power value | 1,387 | 0.7 | Medium | 20,838 |
https://leetcode.com/problems/sort-integers-by-the-power-value/discuss/1558404/Easy-to-understand-python-solution-to-this-problem! | class Solution:
def getKth(self, lo: int, hi: int, k: int) -> int:
ans = dict()
for i in range(lo, hi+1):
step = 0
cur = i
while i > 1:
if i % 2 == 0:
i = i/2
step += 1
else:
i = 3*i+1
step += 1
ans[cur] = step
ans = sorted(ans.items(), key=lambda x: x[1])
return ans[k-1][0] | sort-integers-by-the-power-value | Easy to understand python solution to this problem! | minhson3421 | 0 | 83 | sort integers by the power value | 1,387 | 0.7 | Medium | 20,839 |
https://leetcode.com/problems/sort-integers-by-the-power-value/discuss/1475330/Solution-for-noobs-in-python | class Solution:
def getKth(self, lo, hi, k):
def func(n):
if n==1:
return 0
else:
if n%2==0:
return 1+func(n//2)
if n%2!=0:
return 1+func(3*n+1)
power = []
for i in range(lo,hi+1):
power.append((func(i),i))
return sorted(power)[k-1][1] | sort-integers-by-the-power-value | Solution for noobs in python | Ridzzz | 0 | 56 | sort integers by the power value | 1,387 | 0.7 | Medium | 20,840 |
https://leetcode.com/problems/sort-integers-by-the-power-value/discuss/1399781/Python3-solution | class Solution:
def getKth(self, lo: int, hi: int, k: int) -> int:
dictionary = {}
for i in range(lo, hi + 1):
static = i
power = 0
while i > 1:
if i % 2 != 0:
i = (3 * i) + 1
else:
i /= 2
power += 1
dictionary[static] = power
# got the power, now we have to sort the i values in a list by their ascending corresponding power values
sorted_numbers = sorted(dictionary, key = dictionary.get)
return sorted_numbers[k - 1] | sort-integers-by-the-power-value | Python3 solution | RobertObrochta | 0 | 25 | sort integers by the power value | 1,387 | 0.7 | Medium | 20,841 |
https://leetcode.com/problems/sort-integers-by-the-power-value/discuss/1196916/Python3-simple-solution-using-dictionary | class Solution:
def getKth(self, lo: int, hi: int, k: int) -> int:
z = {16:4,10:6,8:3,5:5,4:2,3:7,2:1,1:1}
d = dict()
for i in range(lo,hi+1):
power = 0
x = i
while x not in z:
if x % 2 == 0:
x = x /2
else:
x = 3 * x + 1
power += 1
power += z[x]
d[i] = power
z[i] = power
return([i for i,j in sorted(d.items(), key= lambda x : x[1])][k-1]) | sort-integers-by-the-power-value | Python3 simple solution using dictionary | EklavyaJoshi | 0 | 43 | sort integers by the power value | 1,387 | 0.7 | Medium | 20,842 |
https://leetcode.com/problems/sort-integers-by-the-power-value/discuss/1187910/Python-clean-solution | class Solution:
def sort(self, x):
count = 0
while x > 1:
if x % 2:
x = 3 * x +1
else:
x = x // 2
count += 1
return count
def getKth(self, lo: int, hi: int, k: int) -> int:
return sorted((i for i in range(lo, hi+1)), key=self.sort)[k-1] | sort-integers-by-the-power-value | Python clean solution | saydi | 0 | 68 | sort integers by the power value | 1,387 | 0.7 | Medium | 20,843 |
https://leetcode.com/problems/sort-integers-by-the-power-value/discuss/1107487/sorted-dictionary-solution-%2B-memo-recurison(python) | class Solution:
def isEven(self, x):
return x // 2
def isOdd(self, x):
return 3 * x + 1
def getKth(self, lo: int, hi: int, k: int) -> int:
powers = {}
for num in range(lo, hi+1):
key = num # num gets altered per iteration so need the original to add as key in dict
steps = 0
while num != 1:
if num % 2 == 0:
num = self.isEven(num)
else:
num = self.isOdd(num)
steps += 1
powers[key] = steps
return sorted(powers, key=powers.get)[k-1] | sort-integers-by-the-power-value | sorted dictionary solution + memo recurison(python) | uzumaki01 | 0 | 54 | sort integers by the power value | 1,387 | 0.7 | Medium | 20,844 |
https://leetcode.com/problems/sort-integers-by-the-power-value/discuss/1079836/Python3-O(NlogN)-or-O(NlogK)-or-O(N) | class Solution:
def getKth(self, lo: int, hi: int, k: int) -> int:
def fn(x):
"""Return number of steps to transform x into 1."""
ans = 0
while x > 1:
if x&1: x = 3*x + 1
else: x //= 2
ans += 1
return ans
return sorted(range(lo, hi+1), key=fn)[k-1] | sort-integers-by-the-power-value | [Python3] O(NlogN) | O(NlogK) | O(N) | ye15 | 0 | 56 | sort integers by the power value | 1,387 | 0.7 | Medium | 20,845 |
https://leetcode.com/problems/sort-integers-by-the-power-value/discuss/1079836/Python3-O(NlogN)-or-O(NlogK)-or-O(N) | class Solution:
def getKth(self, lo: int, hi: int, k: int) -> int:
pq = [] # min heap (size hi - lo - k + 2)
for x in range(lo, hi+1):
cnt, xx = 0, x
while xx > 1:
xx = 3*xx + 1 if xx&1 else xx//2
cnt += 1
heappush(pq, (cnt, x))
if len(pq) > hi - lo - k + 2: heappop(pq)
return pq[0][1] | sort-integers-by-the-power-value | [Python3] O(NlogN) | O(NlogK) | O(N) | ye15 | 0 | 56 | sort integers by the power value | 1,387 | 0.7 | Medium | 20,846 |
https://leetcode.com/problems/sort-integers-by-the-power-value/discuss/1079836/Python3-O(NlogN)-or-O(NlogK)-or-O(N) | class Solution:
def getKth(self, lo: int, hi: int, k: int) -> int:
nums = []
for x in range(lo, hi+1):
xx = x
cnt = 0
while xx > 1:
xx = 3*xx + 1 if xx&1 else xx//2
cnt += 1
nums.append((cnt, x))
shuffle(nums) # random shuffling
# quick select
def part(lo, hi):
"""Partition nums[lo:hi]"""
i, j = lo+1, hi-1
while i <= j:
if nums[i] < nums[lo]: i += 1
elif nums[lo] < nums[j]: j -= 1
else:
nums[i], nums[j] = nums[j], nums[i]
i += 1
j -= 1
nums[lo], nums[j] = nums[j], nums[lo]
return j
lo, hi = 0, len(nums)
while lo < hi:
mid = part(lo, hi)
if mid + 1 == k: break
elif mid + 1 < k: lo = mid + 1
else: hi = mid
return nums[mid][1] | sort-integers-by-the-power-value | [Python3] O(NlogN) | O(NlogK) | O(N) | ye15 | 0 | 56 | sort integers by the power value | 1,387 | 0.7 | Medium | 20,847 |
https://leetcode.com/problems/sort-integers-by-the-power-value/discuss/931143/Python3-Hash-Table-%2B-Sorted-Solution | class Solution:
def getKth(self, lo: int, hi: int, k: int) -> int:
d = dict()
for i in range(lo, hi+1):
d[i] = self.power(i)
sorted_d = sorted(d.items(), key=lambda kv: kv[1])
return sorted_d[k-1][0]
def power(self, x):
step = 0
while x != 1:
if x % 2 == 0:
x /= 2
else:
x = 3 * x + 1
step += 1
return step | sort-integers-by-the-power-value | Python3 Hash Table + Sorted Solution | yanshengjia | 0 | 55 | sort integers by the power value | 1,387 | 0.7 | Medium | 20,848 |
https://leetcode.com/problems/sort-integers-by-the-power-value/discuss/671018/python3-solution-using-insertion-sort | class Solution:
def getKth(self, lo: int, hi: int, k: int) -> int:
freq = []
x = lo
while x <= hi :
count = 0
while x != 1:
if x%2 == 0:
x = x//2
elif x%2 == 1:
x = (3*x) + 1
count += 1
freq.append([lo,count])
lo += 1
x = lo
for i in range(1,len(freq)):
end = i
start = end
temp = freq[end]
for j in range(end,-1,-1):
if freq[i][1] < freq[j][1]:
start = j
for h in range(end,start,-1):
freq[h] = freq[h-1]
freq[start] = temp
print(freq)
return freq[k-1][0] | sort-integers-by-the-power-value | python3 solution, using insertion sort | zharfanf | 0 | 29 | sort integers by the power value | 1,387 | 0.7 | Medium | 20,849 |
https://leetcode.com/problems/sort-integers-by-the-power-value/discuss/639963/Intuitive-approach-by-memorizing-the-paths-to-save-time | class Solution:
def __init__(self):
self.n2k_dict = {1:0, 2:1}
def tk(self, v, depth=0):
if v == 1:
return depth
elif v in self.n2k_dict:
return self.n2k_dict[v] + depth
elif v % 2 == 1:
d = self.tk(v * 3 + 1, depth+1)
self.n2k_dict[v] = d - depth
return d
else:
d = self.tk(v // 2, depth+1)
self.n2k_dict[v] = d - depth
return d
def getKth(self, lo: int, hi: int, k: int) -> int:
return sorted(list(range(lo, hi+1)), key=lambda v: self.tk(v))[k-1] | sort-integers-by-the-power-value | Intuitive approach by memorizing the paths to save time | puremonkey2001 | 0 | 48 | sort integers by the power value | 1,387 | 0.7 | Medium | 20,850 |
https://leetcode.com/problems/sort-integers-by-the-power-value/discuss/555115/Python-sol-by-dict-and-look-up-table | class Solution:
def getKth(self, lo: int, hi: int, k: int) -> int:
# to store the power value of n, and avoid repeated computation
loop_up_table = {}
def power_val( n ):
step, origin_input = 0, n
while n != 1:
if n in loop_up_table:
# speed up by look-up table
loop_up_table[origin_input] = loop_up_table[n] + step
return loop_up_table[n]+step
if n & 1 == 1:
n = 3*n + 1
else:
n = n // 2
step += 1
loop_up_table[origin_input] = step
return step
# ------------------------------------------
ranked_value = sorted( range(lo, hi+1), key = lambda n: (power_val(n), n) )
return ranked_value[k-1] | sort-integers-by-the-power-value | Python sol by dict and look-up table | brianchiang_tw | 0 | 133 | sort integers by the power value | 1,387 | 0.7 | Medium | 20,851 |
https://leetcode.com/problems/pizza-with-3n-slices/discuss/1124752/Python3-top-down-dp | class Solution:
def maxSizeSlices(self, slices: List[int]) -> int:
@cache
def fn(i, k, first):
"""Return max sum of k pieces from slices[i:]."""
if k == 0: return 0
if i >= len(slices) or first and i == len(slices)-1: return -inf
if i == 0: return max(fn(i+1, k, False), slices[i] + fn(i+2, k-1, True))
return max(fn(i+1, k, first), slices[i] + fn(i+2, k-1, first))
return fn(0, len(slices)//3, None) | pizza-with-3n-slices | [Python3] top-down dp | ye15 | 2 | 236 | pizza with 3n slices | 1,388 | 0.501 | Hard | 20,852 |
https://leetcode.com/problems/pizza-with-3n-slices/discuss/2639637/Python3-or-Both-Top-Bottom-and-Bottom-up-Solution | class Solution:
def maxSizeSlices(self, slices: List[int]) -> int:
m=len(slices)//3
def solve(slices,m):
n=len(slices)
dp=[[0 for i in range(m+1)] for j in range(n+1)]
for i in range(1,n+1):
for j in range(1,m+1):
if i==1:
dp[i][j]=slices[0]
else:
dp[i][j]=max(dp[i-1][j],dp[i-2][j-1]+slices[i-1])
return dp[n][m]
return max(solve(slices[:-1],m),solve(slices[1:],m)) | pizza-with-3n-slices | [Python3] | Both Top-Bottom and Bottom-up Solution | swapnilsingh421 | 0 | 12 | pizza with 3n slices | 1,388 | 0.501 | Hard | 20,853 |
https://leetcode.com/problems/pizza-with-3n-slices/discuss/2639637/Python3-or-Both-Top-Bottom-and-Bottom-up-Solution | class Solution:
def maxSizeSlices(self, slices: List[int]) -> int:
n=len(slices)
@lru_cache(None)
def dfs(ind,pick,start):
if ind<start or pick==0:return 0
return max(dfs(ind-1,pick,start),dfs(ind-2,pick-1,start)+slices[ind])
m=n//3
return max(dfs(n-1,m,1),dfs(n-2,m,0)) | pizza-with-3n-slices | [Python3] | Both Top-Bottom and Bottom-up Solution | swapnilsingh421 | 0 | 12 | pizza with 3n slices | 1,388 | 0.501 | Hard | 20,854 |
https://leetcode.com/problems/pizza-with-3n-slices/discuss/2628721/10-line-Python-solution-with-memoization-with-explanation | class Solution:
@cache
def dp(self, l, r, c):
if c == 0 or l >= r:
return 0
return max(self.slices[l] + self.dp(l+2, r, c-1), self.dp(l+1, r, c)) # To select or not to select the current slice
def maxSizeSlices(self, slices: List[int]) -> int:
self.slices = slices + slices
return max(self.slices[i] + self.dp(i + 2, i - 1 + len(slices), len(slices) // 3 - 1) for i in range(-3, 3)) # Handling the circular condition | pizza-with-3n-slices | 10 line Python solution with memoization with explanation | metaphysicalist | 0 | 13 | pizza with 3n slices | 1,388 | 0.501 | Hard | 20,855 |
https://leetcode.com/problems/pizza-with-3n-slices/discuss/2436861/python-recursive-dp | class Solution:
def maxSizeSlices(self, slices: List[int]) -> int:
def solve(index,end,slices,n,dic):
key=(index,n)
if key in dic:
return dic[key]
else:
if n==0 or index>end:
return 0
include=slices[index]+solve(index+2,end,slices,n-1,dic)
exclude=solve(index+1,end,slices,n,dic)
dic[key]=max(include,exclude)
return dic[key]
l=len(slices)
dic1={}
dic2={}
c1=solve(0,l-2,slices,l//3,dic1)
c2=solve(1,l-1,slices,l//3,dic2)
return max(c1,c2) | pizza-with-3n-slices | python recursive dp | shivamkedia31 | 0 | 35 | pizza with 3n slices | 1,388 | 0.501 | Hard | 20,856 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/1163965/Python3-Simple-Solution | class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
ans = []
for i in range(len(nums)):
ans.insert(index[i] , nums[i])
return ans | create-target-array-in-the-given-order | [Python3] Simple Solution | VoidCupboard | 5 | 252 | create target array in the given order | 1,389 | 0.859 | Easy | 20,857 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/1367633/WEEB-DOES-PYTHON-(BEATS-94.02) | class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
target = []
for num, idx in zip(nums, index):
target.insert(idx, num)
return target | create-target-array-in-the-given-order | WEEB DOES PYTHON (BEATS 94.02%) | Skywalker5423 | 4 | 443 | create target array in the given order | 1,389 | 0.859 | Easy | 20,858 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/1151407/Python3-Simple-And-Readable-Solution | class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
arr = []
for i in range(len(nums)):
arr.insert(index[i] , nums[i])
return arr | create-target-array-in-the-given-order | [Python3] Simple And Readable Solution | Lolopola | 3 | 148 | create target array in the given order | 1,389 | 0.859 | Easy | 20,859 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/772103/Python-3-easy-and-simple-logic-99faster | class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
res=[]
for i in range(len(nums)):
res.insert(index[i],nums[i]) | create-target-array-in-the-given-order | Python 3 easy and simple logic 99%faster | Geeky-star | 3 | 371 | create target array in the given order | 1,389 | 0.859 | Easy | 20,860 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/564981/Python-Straightforward-Solution-with-3-Lines | class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
list = []
[list.insert(ind, num) for num, ind in zip(nums, index)]
return list | create-target-array-in-the-given-order | Python Straightforward Solution with 3 Lines | aniulis | 3 | 380 | create target array in the given order | 1,389 | 0.859 | Easy | 20,861 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/547852/Two-Pythonic-O(n2)-sol-sharing. | class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
output = []
for k in range(len(nums)):
output.insert( index[k], nums[k] )
return output | create-target-array-in-the-given-order | Two Pythonic O(n^2) sol sharing. | brianchiang_tw | 3 | 275 | create target array in the given order | 1,389 | 0.859 | Easy | 20,862 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/547852/Two-Pythonic-O(n2)-sol-sharing. | class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
output = []
for idx, number in zip( index, nums):
output.insert( idx, number)
return output | create-target-array-in-the-given-order | Two Pythonic O(n^2) sol sharing. | brianchiang_tw | 3 | 275 | create target array in the given order | 1,389 | 0.859 | Easy | 20,863 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/1777680/python-3-or-simple-solution-or-4-lines-of-code | class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
lst = []
for i,j in zip(nums,index):
lst.insert(j,i)
return lst | create-target-array-in-the-given-order | ✔python 3 | simple solution | 4 lines of code | Coding_Tan3 | 1 | 91 | create target array in the given order | 1,389 | 0.859 | Easy | 20,864 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/1568331/Most-straightfoward-solution-list-slicing-only-no-insert | class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
out = []
for i, idx in enumerate(index):
out = out[:idx] + [nums[i]] + out[idx:]
return out | create-target-array-in-the-given-order | Most straightfoward solution - list slicing only, no insert | Sima24 | 1 | 84 | create target array in the given order | 1,389 | 0.859 | Easy | 20,865 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/1232071/easy-solution-in-python | class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
ot=[]
for i in range(len(nums)):
ot.insert(index[i],nums[i])
return ot | create-target-array-in-the-given-order | easy solution in python | souravsingpardeshi | 1 | 91 | create target array in the given order | 1,389 | 0.859 | Easy | 20,866 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/1018221/Simple-Python-code-which-gave-result-%22faster-than-98.75%22 | class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
#target = [101] * len(index)
target = []
if len(nums)==1:
return [nums[0]]
for i in range(0,len(index)):
target.insert(index[i],nums[i])
return target | create-target-array-in-the-given-order | Simple Python code which gave result "faster than 98.75%" | Ahmad-Bilal | 1 | 198 | create target array in the given order | 1,389 | 0.859 | Easy | 20,867 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/942584/Python-Easy-Solution | class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
t=[]
for i in range(len(nums)):
t.insert(index[i],nums[i])
return t | create-target-array-in-the-given-order | Python Easy Solution | lokeshsenthilkumar | 1 | 510 | create target array in the given order | 1,389 | 0.859 | Easy | 20,868 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/629037/Python-O(n2)-solution-Faster-than-97.83-Memory-less-than-100.00 | class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
finalList = [None] * len(nums)
counter = 0
for i in range(len(nums)):
counter+=1
finalList.insert(index[i], nums[i])
return finalList[:len(finalList)-counter] | create-target-array-in-the-given-order | Python O(n^2) solution - Faster than 97.83%, Memory less than 100.00% | ahmedkidwai | 1 | 191 | create target array in the given order | 1,389 | 0.859 | Easy | 20,869 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/2814513/PYTHON3-BEST | class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
target =[]
for i in range(len(nums)):
if index[i] == len(target) :
target.append(nums[i])
else:
target = target[:index[i]] + [nums[i]] + target[index[i]:]
return target | create-target-array-in-the-given-order | PYTHON3 BEST | Gurugubelli_Anil | 0 | 2 | create target array in the given order | 1,389 | 0.859 | Easy | 20,870 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/2772006/Python-Easy-Solution-90 | class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
target = []
for i in range(len(nums)):
target.insert(index[i], nums[i])
return target | create-target-array-in-the-given-order | Python Easy Solution 90% | Jashan6 | 0 | 5 | create target array in the given order | 1,389 | 0.859 | Easy | 20,871 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/2733587/Simple-Solution-Python3 | class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
res = []
for i in range(len(nums)):
res.insert(index[i], nums[i])
return res | create-target-array-in-the-given-order | Simple Solution Python3 | MikheilU | 0 | 4 | create target array in the given order | 1,389 | 0.859 | Easy | 20,872 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/2723510/Python-Solution | class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
output = []
for i in range(len(nums)):
output.insert(index[i], nums[i])
return output | create-target-array-in-the-given-order | Python Solution | keioon | 0 | 4 | create target array in the given order | 1,389 | 0.859 | Easy | 20,873 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/2676290/Simple-python-code-with-explanation | class Solution:
#keyword--> insert(index,value)
def createTargetArray(self, nums, index):
#create a list(target) --> to store the result
target = []
#iterate over the elements in nums:
for i in range(len(nums)):
#insert the value(nums[i]) at index(index[i]) in target(list)
target.insert(index[i],nums[i])
#for inserting all the elements from nums to target
#return the target list
return target | create-target-array-in-the-given-order | Simple python code with explanation | thomanani | 0 | 18 | create target array in the given order | 1,389 | 0.859 | Easy | 20,874 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/2613181/Create-Target-Array-in-the-Given-Order | class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
temp = []
for i in range(len(nums)):
temp.insert(index[i],nums[i])
return temp | create-target-array-in-the-given-order | Create Target Array in the Given Order | PravinBorate | 0 | 24 | create target array in the given order | 1,389 | 0.859 | Easy | 20,875 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/2592918/optimized-solution-(python3) | class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
n = len(nums)
result =[]
for i in range (n):
index_no = index[i]
value = nums[i]
if index_no >= len(result):
result.append(value)
else:
result.insert(index_no,value)
return result | create-target-array-in-the-given-order | optimized-solution-(python3) | kingshukmaity60 | 0 | 34 | create target array in the given order | 1,389 | 0.859 | Easy | 20,876 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/2567030/python3-code | class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
target = []
for i in range(len(index)):
if index[i]>=len(target):
target.append(nums[i])
target.insert(index[i],nums[i])
target.pop()
return target | create-target-array-in-the-given-order | python3 code | kaoshlendra | 0 | 18 | create target array in the given order | 1,389 | 0.859 | Easy | 20,877 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/2516457/Python-97-Faster-Simple-Solution | class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
#output list
ol = []
#iterate both list, build output list
for (num,idx) in zip(nums,index):
ol.insert(idx,num)
return(ol) | create-target-array-in-the-given-order | Python 97% Faster - Simple Solution | ovidaure | 0 | 41 | create target array in the given order | 1,389 | 0.859 | Easy | 20,878 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/2469690/python-or-without-using-python-list-insert-operation | class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
ini = [-1]*len(nums)
for i in range(len(nums)):
if ini[index[i]] == -1:
ini[index[i]] = nums[i]
else:
ini = ini[0:index[i]] + [nums[i]] + ini[index[i]: len(nums)-1]
return ini | create-target-array-in-the-given-order | python | without using python list insert operation | rohannayar8 | 0 | 32 | create target array in the given order | 1,389 | 0.859 | Easy | 20,879 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/2436488/Python-99.54-faster-insertion-method | class Solution:
def createTargetArray(self, m: List[int], ind: List[int]) -> List[int]:
a = []
n = len(m)
for i in range(n):
a.insert(ind[i], m[i])
return a | create-target-array-in-the-given-order | Python 99.54% faster insertion method | amit0693 | 0 | 54 | create target array in the given order | 1,389 | 0.859 | Easy | 20,880 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/2162845/Python-simple-4-line-code | class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
target = []
for i, n in enumerate(nums):
target.insert(index[i], n)
return target | create-target-array-in-the-given-order | Python simple 4 line code | pro6igy | 0 | 88 | create target array in the given order | 1,389 | 0.859 | Easy | 20,881 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/2147963/Python-Solution-oror-easy-code-oror-46ms-runtime | class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
l=[]
for i in range(len(nums)):
l.insert(index[i],nums[i])
return l | create-target-array-in-the-given-order | Python Solution || easy code || 46ms runtime | T1n1_B0x1 | 0 | 83 | create target array in the given order | 1,389 | 0.859 | Easy | 20,882 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/2128029/Python-Easy-solutions-with-complexities | class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
target = []
i = 0 # pointer for traversing nums and index
while(i < len(nums)):
if index[i] == i:
target.append(nums[i])
else:
target = target[:index[i]] + [nums[i]] + target[index[i]:]
i+=1
return target
# time O(N*N) - for traversal * list slicing
# space O(N) - target array | create-target-array-in-the-given-order | [Python] Easy solutions with complexities | mananiac | 0 | 63 | create target array in the given order | 1,389 | 0.859 | Easy | 20,883 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/2128029/Python-Easy-solutions-with-complexities | class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
target = []
i = 0 # pointer for traversing nums and index
while(i < len(nums)):
target.insert(index[i],nums[i])
i += 1
return target
# time O(N*N) - for traversal + insert function
# space O(N) - target array | create-target-array-in-the-given-order | [Python] Easy solutions with complexities | mananiac | 0 | 63 | create target array in the given order | 1,389 | 0.859 | Easy | 20,884 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/2110230/Python-or-Easy-and-simple-solution-using-insert-function | class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
res = []
for e,i in zip(nums,index):
res.insert(i, e)
return res | create-target-array-in-the-given-order | Python | Easy and simple solution using insert function | nikhitamore | 0 | 52 | create target array in the given order | 1,389 | 0.859 | Easy | 20,885 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/2036715/Python3-using-insert | class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
target = []
for i in range(len(nums)):
target.insert(index[i], nums[i])
return target | create-target-array-in-the-given-order | [Python3] using insert | Shiyinq | 0 | 49 | create target array in the given order | 1,389 | 0.859 | Easy | 20,886 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/2014823/Using-enumerate | class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
target = []
for i, n in enumerate(nums):
target.insert(index[i], n)
return target | create-target-array-in-the-given-order | Using enumerate | andrewnerdimo | 0 | 35 | create target array in the given order | 1,389 | 0.859 | Easy | 20,887 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/1962726/93.91-faster-python | class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
res = []
for i, j in zip(nums, index):
res.insert(j, i)
return res | create-target-array-in-the-given-order | 93.91% faster python | blckvia | 0 | 89 | create target array in the given order | 1,389 | 0.859 | Easy | 20,888 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/1954875/Python-list-comprehension-and-insert()-method | class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
target = []
target_loop = [target.insert(i, number) for number, i in zip(nums, index) ]
return target
``` | create-target-array-in-the-given-order | Python list comprehension and insert() method | vidamaleki | 0 | 23 | create target array in the given order | 1,389 | 0.859 | Easy | 20,889 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/1931934/Python-Solution-O(n)-Time-Complexity-Beginner-Friendly | class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
arr = []
for x,y in zip(nums,index):
arr.insert(y,x)
return arr | create-target-array-in-the-given-order | Python Solution O(n) Time Complexity Beginner Friendly | itsmeparag14 | 0 | 42 | create target array in the given order | 1,389 | 0.859 | Easy | 20,890 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/1888203/Python-Simple-and-Clean! | class Solution:
def createTargetArray(self, nums, index):
output = []
for i in range(len(nums)): output.insert(index[i], nums[i])
return output | create-target-array-in-the-given-order | Python - Simple and Clean! | domthedeveloper | 0 | 69 | create target array in the given order | 1,389 | 0.859 | Easy | 20,891 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/1809269/3-Lines-Python-Solution-oror-85-Faster-(36ms)-oror-Memory-less-than-82 | class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
ans = []
for i in range(len(nums)): ans.insert(index[i], nums[i])
return ans | create-target-array-in-the-given-order | 3-Lines Python Solution || 85% Faster (36ms) || Memory less than 82% | Taha-C | 0 | 67 | create target array in the given order | 1,389 | 0.859 | Easy | 20,892 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/1789221/Python3-recursive-NoForLoops-allowed | class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
data = []
def noForLoops(j):
if j >= len(nums):
return
n = nums[j]
i = index[j]
data.insert(i,n)
j += 1
noForLoops(j)
noForLoops(0)
return data | create-target-array-in-the-given-order | Python3 recursive NoForLoops allowed | user5571e | 0 | 37 | create target array in the given order | 1,389 | 0.859 | Easy | 20,893 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/1786903/PYTHON-or-Easy-solution-87-memory-Usage | class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
ans= []
for i in range(len(nums)):
ans[index[i]:index[i]]=[nums[i]]
return ans | create-target-array-in-the-given-order | PYTHON | Easy solution 87% memory Usage | Noyha | 0 | 55 | create target array in the given order | 1,389 | 0.859 | Easy | 20,894 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/1763967/small-runtime-story-in-python | class Solution(object):
def createTargetArray(self, nums, index):
target_array = []
for i in range(len(nums)):
#len(nums) == len(index)
target_array.insert(index[i], nums[i])
return target_array | create-target-array-in-the-given-order | small runtime story in python | ankit61d | 0 | 40 | create target array in the given order | 1,389 | 0.859 | Easy | 20,895 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/1763967/small-runtime-story-in-python | class Solution:
def createTargetArray(self, nums, index):
target_array = []
if len(nums)==1:
return [nums[0]]
for i in range(0,len(index)):
target_array.insert(index[i],nums[i])
return target_array | create-target-array-in-the-given-order | small runtime story in python | ankit61d | 0 | 40 | create target array in the given order | 1,389 | 0.859 | Easy | 20,896 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/1763967/small-runtime-story-in-python | class Solution(object):
def createTargetArray(self, nums, index):
target_array = []
for idx, num in zip(index, nums):
target_array.insert(idx, num)
return target_array | create-target-array-in-the-given-order | small runtime story in python | ankit61d | 0 | 40 | create target array in the given order | 1,389 | 0.859 | Easy | 20,897 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/1691811/3-Methods-or-Python3 | class Solution:
def createTargetArray(self, nums, index):
target = [0]*len(nums)
target_copy = [0]*len(nums)
for i,v in zip(index,nums):
if target[i]:
for j in range(i+1,len(target_copy)):
target[j] = target_copy[j-1]
target[i]=v
target[i]=v
target_copy = target[:]
return target | create-target-array-in-the-given-order | 3 Methods | Python3 🐍 | hritik5102 | 0 | 57 | create target array in the given order | 1,389 | 0.859 | Easy | 20,898 |
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/1691811/3-Methods-or-Python3 | class Solution:
def createTargetArray(self, nums, index):
target = []
for i in range(len(nums)):
target = target[:index[i]] + [nums[i]] + target[index[i]:]
return target | create-target-array-in-the-given-order | 3 Methods | Python3 🐍 | hritik5102 | 0 | 57 | create target array in the given order | 1,389 | 0.859 | Easy | 20,899 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.