Relative Sort Array - Leetcode 1122 - Python

preview_player
Показать описание


0:00 - Read the problem
0:30 - Drawing Explanation
4:42 - Coding Explanation

leetcode 1122

#neetcode #leetcode #python
Рекомендации по теме
Комментарии
Автор

Bro went from Alpha to Sigma in 4 years, actually the Java comparator solution was quite smart tbh 👌👌👌

jotrades
Автор

As an Android developer I also switched from Kotlin to Python

JamesBond-mqpd
Автор

cant we use count sort for this problem similar to yesterday to maintain O(n) TC?

julianelmasry
Автор

You can also make the space complexity O(m) by making a hashmap with arr2's indices and then sorting arr1 in place with a lambda (hm[n] if n in hm else len(arr2) + n). Still nlogn time complexity though.

daniellarson
Автор

y don't u use the extend method to append a resulting list and keep using 2 for loops? is it related to a TC issue or smth?

md_pedia
Автор

I see how your java code was incredible faster than python ...lol

ashanksaini
Автор

Hey Neet can you post a video solution on this problem "987. Vertical Order Traversal of a Binary Tree", Thanks in advance 🙂

varunpalsingh
Автор

my solution:
counter = {}
for n in arr1:
counter[n] = counter.get(n, 0)+1
matching, distinct = [], []
for n in arr2:
matching += [n]*counter[n]
counter[n] = 0
for n in counter:
distinct += [n]*counter[n]
return matching+sorted(distinct)

sri_harsha_dv
Автор

My solution using count sort.

result = []
max_number = max(arr1)
count = [0] * (max_number + 1)

for n in arr1:
count [n] += 1

for n in arr2:
result = result + [n for _ in range(count [n])]

for i, n in enumerate(count ):
if i not in arr2 and n > 0:
result = result + [i for _ in range(n)]

return result

JerickMamuric
Автор

class Solution:
def relativeSortArray(self, a: List[int], b: List[int]) -> List[int]:
l=len(b:={v:i for i, v in enumerate(b)})
return sorted(a, key=lambda x:([b[x]] if x in b else [l, x]))

qulinxao
Автор

thank you Navdeep .... pls keep 'em coming

metarus
Автор

Count sort : O(n) time

class Solution:
def relativeSortArray(self, arr1: List[int], arr2: List[int]) -> List[int]:
arr = [0]*1001

for a in arr1:
arr[a]+=1

res=[]

for a in arr2:
while arr[a]:
res.append(a)
arr[a]-=1
for a in range(len(arr)):
while arr[a]:
res.append(a)
arr[a]-=1

return res

vnnyboy
Автор

Quick question, if this question is given in a real interview, will a self defined compare function be a bad answer?

eg
from functools import cmp_to_key
class Solution:
def relativeSortArray(self, arr1: List[int], arr2: List[int]) -> List[int]:
order = {}
for i in range(len(arr2)):
order[arr2[i]] = i

def cmp(a, b):
if a in order and b in order:
return order[a] - order[b]
elif a in order and b not in order:
return -1
elif a not in order and b in order:
return 1
else:
return a - b

arr1.sort(key = cmp_to_key(cmp))
return arr1

thunderstorm-dc
Автор

i m so happy i did the exact approach 😝

jhanavikumari
Автор

class Solution:
def relativeSortArray(self, a: List[int], b: List[int]) -> List[int]:
l=len(b:={v:(i, ) for i, v in enumerate(b)})
return sorted(a, key=lambda x:b.get(x, (l, x)))

qulinxao
Автор

I think that Java is a much more desirable solution or at least way of thinking. If someone is hiring. But both are okay.

asagiai
Автор

I am also shocked the same way to see my 1 year old code.

Someoneunknown
Автор

Since arr[i] constraint is very low from 0 to 1000, we can use count sort very efficiently and can come with 0(n) solution.

class Solution {
public int[] relativeSortArray(int[] arr1, int[] arr2) {

int[] count = new int[1001];

for(int num : arr1){
count[num]++;
}

int[] op = new int[arr1.length];

int i = 0;

for(int num : arr2){

while(count[num] > 0){

op[i++] = num;
count[num]--;

}

}

int countIdx = 0;

while(countIdx < 1001){

while(count[countIdx] > 0){

op[i++] = countIdx;
count[countIdx]--;

}

countIdx++;
}

return op;
}
}

nirmalgurjar
Автор

element of arr2 are distinct, so why to use set there in 2nd line

SujeetKumar-hhjz
Автор

bro since you're collecting the counts and range is 0-1000 anyway you coulda did counting sort for the end bit

greatfate
join shbcf.ru