Divide Players Into Teams of Equal Skill - Leetcode 2491 - Python

  Рет қаралды 7,791

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 30
@Kokurorokuko
@Kokurorokuko Ай бұрын
I did it exactly like you did and was surprised to see so many solutions with sorting
@s0lomate
@s0lomate Ай бұрын
It’s just a habit to watch your videos, even though I solved the problem easily.
@zweitekonto9654
@zweitekonto9654 Ай бұрын
Naah u just come to make fun of that one struggling beginner dude
@dontignore5567
@dontignore5567 Ай бұрын
@@zweitekonto9654he came here to make fun on me
@rjarora
@rjarora Ай бұрын
7:52 It's because this edge case would never arise since the count of elements is even.
@rohitkumaram
@rohitkumaram Ай бұрын
bingo
@aitormonrealurcelay3500
@aitormonrealurcelay3500 Ай бұрын
As always, thank you for your daily solutions!
@Drakula1996
@Drakula1996 Ай бұрын
Beginner here... is this a good solution? if len(skill) % 2: return -1 skill.sort() maxChem = sum(skill) // (len(skill) // 2) l, r = 0, len(skill) - 1 res = [] while l < r: if skill[l] + skill[r] == maxChem: res.append(skill[l] * skill[r]) else: return -1 l += 1 r -= 1 return sum(res)
@leeroymlg4692
@leeroymlg4692 Ай бұрын
I like the way you're thinking, but it's a O(nlogn) solution which is worse than O(n). This is because you are sorting the input. But it's a good nlogn solution, just could be more time efficient.
@PerseRos285
@PerseRos285 Ай бұрын
amazing explanation, thank you!
@praveenkumarv2727
@praveenkumarv2727 Ай бұрын
Answer in Java class Solution { public long dividePlayers(int[] skill) { if(skill.length==2){ return skill[0]*skill[1]; } Arrays.sort(skill); long sum = 0; int n = skill.length; int targetSum = skill[0] + skill[n - 1]; for (int i = 0, j = n - 1; i < j; i++, j--) { if (skill[i] + skill[j] != targetSum) { return -1; } sum += (long) skill[i] * skill[j]; } return sum; } }
@satyamjha68
@satyamjha68 Ай бұрын
Solved it!
@eternal8lueforever
@eternal8lueforever Ай бұрын
Bro I just had this question in an interview this week
@sabukuna
@sabukuna Ай бұрын
solved it with sort
@romi._.
@romi._. Ай бұрын
Why we are doing 2* total % Len (skill), can't we just do total % Len (skill)? same for the target?
@eliteturtle6090
@eliteturtle6090 Ай бұрын
I did it the same way as you, but it is slower than the other solutions
@awalkingnosebleed
@awalkingnosebleed Ай бұрын
i dont understand the last bit about diff and s being same, why should we move the decrementing condition above?
@salami6721
@salami6721 Ай бұрын
The thought process is that we already looked at s, and if s and diff are the same then we're looking at the same element twice. So, the solution is to decrement s so that we can track we already checked it once. But because we checked for the array to be even, I don't think this edge case will happen anyways.
@faaeizkhan6134
@faaeizkhan6134 Ай бұрын
even though I solved it, I had to watch the vid.
@shikharupadhyay7435
@shikharupadhyay7435 Ай бұрын
My O(n) solution beats 40% whereas O(nlogn) beats 87%.... Leetcode.....😅😅😅😅
@reeree519
@reeree519 Ай бұрын
It's not uncommon for hash table to be pretty slow. It has a trash O(1).
@tucho6
@tucho6 Ай бұрын
well, let's be honest, if your O(n) solution is slower, then is not Leetcode fault.. is a layer 8 issue...
@maazfaisal1649
@maazfaisal1649 Ай бұрын
first
@pruthvinarayana9568
@pruthvinarayana9568 Ай бұрын
as we are any way traversing the whole array , we can skip the 1st If statement in side the loop, and decrement the another value of the pair afterwards , any way it doesn't change anything 😺 class Solution: def dividePlayers(self, skill: List[int]) -> int: myhash = {} for n in skill: if n in myhash: myhash[n] += 1 else: myhash[n] = 1 target = sum(skill) // (len(skill) // 2) ans = 0 for n in skill: if (target - n in myhash) and (myhash[target - n] > 0) : myhash[target - n] -= 1 ans += n * (target - n) else: return -1 return ans // 2
@tucho6
@tucho6 Ай бұрын
you can do the last for from 0 to target and then directly do: ans += myhash[i]i*(target-i); you'll go from O(N) to O(target) in that part
@adoreyourpain
@adoreyourpain Ай бұрын
Quick tip for this part; for n in skill: if n in myhash: myhash[n] += 1 else: myhash[n] = 1 You could instead write for n in skill: myhash[n] = 1 + myhash.get(n, 0) Just to make it even cleaner, that way you don't need to check if it is in there before hand because it'll just return 1+0, otherwise 1 + whatever it's value already is
@TheFactualKai
@TheFactualKai Ай бұрын
great
@tylera3126
@tylera3126 Ай бұрын
First
My 10 “Clean” Code Principles (Start These Now)
15:12
Conner Ardman
Рет қаралды 287 М.
Sentence Similarity III - Leetcode 1813 - Python
15:07
NeetCodeIO
Рет қаралды 10 М.
How To Choose Mac N Cheese Date Night.. 🧀
00:58
Jojo Sim
Рет қаралды 98 МЛН
One day.. 🙌
00:33
Celine Dept
Рет қаралды 44 МЛН
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 695 М.
Divide Players Into Teams of Equal Skill | Leetcode 2491
16:50
Microservices Gone Wrong at DoorDash
17:22
NeetCodeIO
Рет қаралды 161 М.
5 Useful F-String Tricks In Python
10:02
Indently
Рет қаралды 330 М.
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 151 М.
Dynamic Programming isn't too hard. You just don't know what it is.
22:31
DecodingIntuition
Рет қаралды 199 М.
Programming Is Cooked
9:30
ThePrimeTime
Рет қаралды 276 М.
Count the Number of Fair Pairs - Leetcode 2563 - Python
18:52
NeetCodeIO
Рет қаралды 10 М.
Why is Python 150X slower than C?
10:45
Mehul - Codedamn
Рет қаралды 19 М.
How To Choose Mac N Cheese Date Night.. 🧀
00:58
Jojo Sim
Рет қаралды 98 МЛН