Тёмный

Organizing Containers of Balls | HackerRank (JavaScript) 

Glitched Failure
Подписаться 719
Просмотров 2,7 тыс.
50% 1

Coding Challenge: www.hackerrank.com/challenges...
Solution 11:40
Solution Code: gist.github.com/Shaddyjr/2996...
As a way to improve my coding skills and technical interview prep, I practice HackerRank problems and solve them live with minimal edits. This allows viewers to see my thought process and problem-solving techniques along with the true struggles I go through when solving these technical coding problems. I also delve into tutorials about building out data structures and algorithms.
I mainly code in JavaScript and Python. I also try to focus on Easy and Medium difficulty HackerRank problems, but occasionally detour onto other challenges, like those from my own technical interviews, daily coding challenges, or just fun coding puzzles I come across.

Наука

Опубликовано:

 

27 окт 2019

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 13   
@patrickkajar2305
@patrickkajar2305 4 года назад
smart approach
@abhijit-sarkar
@abhijit-sarkar 2 года назад
You don't need to sort, just convert to set and compare. Sorting takes O(n log n) time while converting to set takes O(n) time.
@GlitchedFailure
@GlitchedFailure 2 года назад
Are you referring to something like this? function organizingContainers(containers) { const n = containers.length; // keep # of balls per container const containerCounts = new Array(n).fill(0); // keep # of types of balls const ballTypes = new Array(n).fill(0); for(let i = 0; i
@jensBendig
@jensBendig Год назад
Thank you. I see that this approach is solving it. What I don‘t quite get is this: Why can‘t I just compare the rowsums with the colsums? imho rowsum(i)==colsum(i) - otherwise impossible. (I tried that, but doesn‘t work for all tests.)
@GlitchedFailure
@GlitchedFailure Год назад
I totally get where you're coming from. It wasn't clear when I was first doing this either. Let's look at an example that I hope is clarifying: 5 1 3 1 5 2 2 3 5 Each row is a container. - The first row/container has 9 balls in it, which means it can only ever hold 9 balls (given the swap mechanic). Let's call this container A. - The second row/container has 8 balls in it (container B). - The third row/container has 10 balls in it (container C). Each column is a unique color - There are 8 balls belonging to the first column/color. Let's call this color Red. - There are 9 balls belonging to the second column/color (color Blue). - There are 10 balls belonging to the third column/color (color Yellow). We have one container that can fit each color: - Container A can hold color Blue - Container B can hold color Red - Container C can hold color Yellow However, if we just compare the row and column sum by their original index we'd have a problem, since there is no order enforced on which containers or colors are listed first. - row 0 (container A) holds 9 balls, but column 0 (color Red) only has 8 balls in total. - row 1 (container B) holds 8 balls, but column 1 (color Blue) only has 9 balls in total. - row 2 (container C) holds 10 balls and column 2 (color Yellow) has 10 balls in total, so this happens to work out. If we sort the counts for rows and columns then we can line them up by their new sorted index. - sorted row list 0 holds 8 balls (this corresponds to container B), which can hold sorted column list 0 (color Red). - sorted row list 1 holds 9 balls (this corresponds to container A), which can hold sorted column list 1 (color Blue). - sorted row list 2 holds 10 balls (this corresponds to container C), which can hold sorted column list 2 (color Yellow). I hope this helps. Thanks for watching!
@jensBendig
@jensBendig Год назад
@@GlitchedFailure Thank you very much!
@pragadeeshsb3249
@pragadeeshsb3249 4 года назад
flag = 1 for i in range(n): r=0 c=0 for j in range(n): r+=container[i][j] c+=container[j][i] if r!=c: flag=0 break if flag==1: return 'Possible' return 'Impossible' This was my solution, the examples satify but the remaining test cases fail. Instead of doing with an array i directly the sum of row and col. I dont where its going wrong. Please help to sort it out
@pragadeeshsb3249
@pragadeeshsb3249 4 года назад
I have used Python3 rather than JS. Please Help me out buddy my head's gonna burst i dont understand why my code isn't working
@GlitchedFailure
@GlitchedFailure 4 года назад
@@pragadeeshsb3249 Taking the sum won't work, since the containers should be considered separately. Adding 1 + 3 = 4 and 2 + 2 = 4 suggests the same sum, but the containers won't match up.
@pragadeeshsb3249
@pragadeeshsb3249 4 года назад
@@GlitchedFailure Let me consider 3x3 matrix 1 1 1 1 2 1 3 2 1 Rows are container First row has 0-1 1-1 2-1 and so on Now for container 0 possible swap is 2 That is sum of sum of first row - first element Now, total 0 other than considered container would be sum of first coloum - first element Since my code is adding first element on both the cases it does not change the result If no of swaps == total 0 in other container For all containers if the above case is satisfied isn't that possible. Im not understanding where have I gone wrong
@GlitchedFailure
@GlitchedFailure 4 года назад
Hmm, let's verify we're referring to that 3x3 matrix in the same way. 1 1 1 1 2 1 3 2 1 Each row is a container. - The first row/container has 3 balls in it, which means it can only ever hold 3 balls (given the swap mechanic). - The second row/contianer has 4 balls in it - The third row/contianer has 6 balls in it. Each column is a unique color - There are 5 balls belonging to the first column/color - There are 5 balls belonging to the second column/color - There are 3 balls belonging to the third column/color In order for each container to hold each unique ball color, the containers must have the exact amount of space needed for them. In this case, the first color ball needs a container that fits exaclty 5 balls, but there are none. The same is true for the other colors. This is an "Impossible" situation. ================== Let's consider the following 3x3 matrix: 1 2 2 1 1 1 1 2 1 Each row is a container. - The first row/container has 5 balls in it, which means it can only ever hold 5 balls (given the swap mechanic). - The second row/contianer has 3 balls in it - The third row/contianer has 4 balls in it. Each column is a unique color - There are 3 balls belonging to the first column/color - There are 5 balls belonging to the second column/color - There are 4 balls belonging to the third column/color In order for each container to hold each unique ball color, the containers must have the exact amount of space needed for them. In this case, the first color ball needs a container that fits exaclty 3 balls, which we have. The same is true for the other colors and the containers for 4 and 5 balls respectively. This is a "Possible" situation.
@pragadeeshsb3249
@pragadeeshsb3249 4 года назад
@@GlitchedFailure thanks a lot buddy. Your explanation was amazing subscribed your channel ;)
Далее
Palindrome Index | HackerRank (JavaScript)
7:17
Ручка из шланга, лайфхак
00:11
Просмотров 16 тыс.
My Puzzle Robot is 200x Faster Than a Human
21:21
Просмотров 8 млн
More SQLi | PicoCTF
10:14
Просмотров 122
Altimetrik L1 -Java SpringBoot Interview Question
20:53
Shop | PicoCTF
4:46
Просмотров 594
Towers of Hanoi: A Complete Recursive Visualization
21:13
Big Sorting | HackerRank
5:12
Просмотров 548
Happy Ladybugs | HackerRank (JavaScript)
7:25
ЗАКОПАЛ НОВЫЙ ТЕЛЕФОН!!!🎁😱
0:28
Acer Predator Тараканьи Бега!
1:00
Просмотров 465 тыс.
Battery  low 🔋 🪫
0:10
Просмотров 3,8 млн