I did it exactly like you did and was surprised to see so many solutions with sorting
@s0lomateАй бұрын
It’s just a habit to watch your videos, even though I solved the problem easily.
@zweitekonto9654Ай бұрын
Naah u just come to make fun of that one struggling beginner dude
@dontignore5567Ай бұрын
@@zweitekonto9654he came here to make fun on me
@rjaroraАй бұрын
7:52 It's because this edge case would never arise since the count of elements is even.
@rohitkumaramАй бұрын
bingo
@aitormonrealurcelay3500Ай бұрын
As always, thank you for your daily solutions!
@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Ай бұрын
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Ай бұрын
amazing explanation, thank you!
@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Ай бұрын
Solved it!
@eternal8lueforeverАй бұрын
Bro I just had this question in an interview this week
@sabukunaАй бұрын
solved it with sort
@romi._.Ай бұрын
Why we are doing 2* total % Len (skill), can't we just do total % Len (skill)? same for the target?
@eliteturtle6090Ай бұрын
I did it the same way as you, but it is slower than the other solutions
@awalkingnosebleedАй бұрын
i dont understand the last bit about diff and s being same, why should we move the decrementing condition above?
@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Ай бұрын
even though I solved it, I had to watch the vid.
@shikharupadhyay7435Ай бұрын
My O(n) solution beats 40% whereas O(nlogn) beats 87%.... Leetcode.....😅😅😅😅
@reeree519Ай бұрын
It's not uncommon for hash table to be pretty slow. It has a trash O(1).
@tucho6Ай бұрын
well, let's be honest, if your O(n) solution is slower, then is not Leetcode fault.. is a layer 8 issue...
@maazfaisal1649Ай бұрын
first
@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Ай бұрын
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Ай бұрын
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