Make Lexicographically Smallest Array by Swapping Elements | Leetcode 2948

  Рет қаралды 6,169

Techdose

Techdose

Күн бұрын

Пікірлер: 46
@freecourseplatformenglish2829
@freecourseplatformenglish2829 Күн бұрын
Seriously it is hard to beat you when its comes to explaining the intution. Thank you
@techdose4u
@techdose4u Күн бұрын
By God’s grace 🙏🏼
@abhishekshukla5747
@abhishekshukla5747 Күн бұрын
This problem was tough , not easy to get the intution if solving for first time ! But your explainantion made it crystal clear sir
@techdose4u
@techdose4u Күн бұрын
yes
@az-zm4ji
@az-zm4ji 23 сағат бұрын
WHAT A GREAT DUDE! I was able to come up wth the DSU approach but didnt code it as it looked too diffcult. This appraoch is just genius.
@techdose4u
@techdose4u 21 сағат бұрын
👌🏽
@BCS-IshtiyakAhmadKhan
@BCS-IshtiyakAhmadKhan 14 сағат бұрын
Best way to do this problem is to sort the array in decreasing order and maintain a adjacency list of groups .Now the elements in the adjacency list will also be sorted in decreasing fashion and as you travel in original array find the current element group(using hash)and remove the last element from that group in the adjacency list which will take O(1) time .Here's the code int n = nums.size(); vector v(nums); sort(v.begin(), v.end(), greater()); vector dsu(1); map mp; dsu[0].push_back(v[0]); mp[v[0]] = 0; int curr = 0; for (int i = 1; i < n; i++) { if (v[i - 1] - v[i] > limit) { dsu.emplace_back(); curr++; } dsu[curr].push_back(v[i]); mp[v[i]] = curr; } for (int i = 0; i < n; i++) { nums[i] = dsu[mp[nums[i]]].back(); dsu[mp[nums[i]]].pop_back(); } return nums;
@heatedmuscle2329
@heatedmuscle2329 18 сағат бұрын
You are so smart. How are you so smart. I really really want to know. Thanks for providing this insight. Commented, Liked and Subscribed,
@techdose4u
@techdose4u 10 сағат бұрын
👌🏽
@virajmandlik-it9bj
@virajmandlik-it9bj Күн бұрын
Insightful Explanation ....Thanks Sir..💯
@techdose4u
@techdose4u Күн бұрын
Welcome!
@programming6177
@programming6177 22 сағат бұрын
nice explanation, thank you bro😇
@techdose4u
@techdose4u 21 сағат бұрын
welcome :)
@BananaButcher
@BananaButcher Күн бұрын
Excellent explanation as always
@techdose4u
@techdose4u Күн бұрын
Appreciate it 😊
@bhashitmaheshwari5205
@bhashitmaheshwari5205 Күн бұрын
Just a Suggestion try to give Brute Better and Optimal Sol for Problems might Help us in Interviews,Thank You
@techdose4u
@techdose4u Күн бұрын
already explained selection sort right ?
@bhashitmaheshwari5205
@bhashitmaheshwari5205 Күн бұрын
@techdose4u yeah sure today you explained it I was talking in general sorry if i offended you in someway.
@techdose4u
@techdose4u Күн бұрын
@bhashitmaheshwari5205 No problem. I generally expect people to know bruteforce as its just simulation of goal :)
@anantakesharipanda4085
@anantakesharipanda4085 19 сағат бұрын
Instead of using two pointers to build the result vector, I was thinking of creating the result vectors of size nums by initializing it with zeroes, then create a map of groups where each Key will be the group index, and value will be vector of sorted indices from original vector. Then just traverse the sorted copy vector and place each element within same group within the index from the map’s values into the result vector initialised with zero. I know this not so space efficient, but this approach seemed more easy to visualise to me. 😅
@shivnathkahar8985
@shivnathkahar8985 Күн бұрын
Excellent
@techdose4u
@techdose4u Күн бұрын
Thanks :)
@nabihsara
@nabihsara Күн бұрын
Thanks, and i was waiting you yesterday but you have solved late, I wish you solve every day early and thanks again for your effort and explanation
@techdose4u
@techdose4u Күн бұрын
I never solve the same day. I solve days before and predict the question to come. Yesterday’s prediction dint work out. Hence it was late :(
@challengemania4447
@challengemania4447 Күн бұрын
​@@techdose4u but how sir.. Do you have any ML algo to predict next question ?
@rameshgoud2986
@rameshgoud2986 23 сағат бұрын
@@challengemania4447 😂😂😂
@sruthinteja4644
@sruthinteja4644 Күн бұрын
How to do using unionfind
@techdose4u
@techdose4u Күн бұрын
Maintain multiple sets using disjojnt set array. Please follow my disjoint set video for clarity.
@vbhv-gupta
@vbhv-gupta Күн бұрын
Time complexity of its Brute Force approach will be O(n^3) class Solution: def lexicographicallySmallestArray(self, nums, limit) : copy = None while copy != nums: # -> Loop 1 copy = nums.copy() for i in range(len(nums)): # -> Loop 2 best_idx = i for j in range(i+1,len(nums)): #-> Loop 3 if 0 < nums[i] - nums[j]
@techdose4u
@techdose4u Күн бұрын
yes
@challengemania4447
@challengemania4447 Күн бұрын
Can we able to do it in O(n^2).. I think we can do as usual comparing one element with each other. Will be this approach is brutefoecr ?
@rameshgoud2986
@rameshgoud2986 23 сағат бұрын
@@challengemania4447 no wont work
@Ice-2706
@Ice-2706 6 сағат бұрын
the custom selection sort would not work for every case. ex - [10, 4, 7], limit = 3 although the idea of swappable groups is awesome!
@techdose4u
@techdose4u Сағат бұрын
yes it will require an extra loop
@sailendrachettri8521
@sailendrachettri8521 Күн бұрын
Thank you sir :)
@techdose4u
@techdose4u Күн бұрын
welcome :)
@UMESAURAVKUMAR
@UMESAURAVKUMAR Күн бұрын
The selection sort algo will break here: {10,3,5,8,2} , limit = 3 after first iteration: best value for 10 is 8 {8, 3, 5, 10, 2} after second iter: best value for 3 {8, 2,5,10,3} after 3rd iter: best value for 5 is 3 {8,2,3,10,5} after 4th iter: best value for 10 is no one in right {8,2,3,10,5} but the actual answer for this problem is : {2,3,5,8,10}
@techdose4u
@techdose4u Күн бұрын
Yes. One extra loop would be required if elements are in increasing order and we always happen to pick from right.
@kshitijvarma2566
@kshitijvarma2566 Күн бұрын
This was hard to think of .... 😅
@techdose4u
@techdose4u Күн бұрын
yes
@kthamim7552
@kthamim7552 Күн бұрын
you are sorting a copy array as normal sort but why sir you are saying,you'r done a lexicographically sorting a copy array bit confusing in that line after finding a logic there is no doubt one is that you are using a normal sorting concept or anyother lexico sorts ?
@techdose4u
@techdose4u Күн бұрын
I dint understand your problem. Can you please mention a little more clearly possibly with example.
@hhcdghjjgsdrt235
@hhcdghjjgsdrt235 22 сағат бұрын
10, 8 were green rest were yellow the idea clicked
@techdose4u
@techdose4u 21 сағат бұрын
made for you :)
Maximum Employees to Be Invited to a Meeting | Leetcode 2127
52:53
Find Eventual Safe States | Leetcode 802
20:44
Techdose
Рет қаралды 2,2 М.
Жездуха 41-серия
36:26
Million Show
Рет қаралды 5 МЛН
КОНЦЕРТЫ:  2 сезон | 1 выпуск | Камызяки
46:36
ТНТ Смотри еще!
Рет қаралды 3,7 МЛН
Vampire SUCKS Human Energy 🧛🏻‍♂️🪫 (ft. @StevenHe )
0:34
Alan Chikin Chow
Рет қаралды 138 МЛН
УНО Реверс в Амонг Ас : игра на выбывание
0:19
Фани Хани
Рет қаралды 1,3 МЛН
Trapping Rain Water II | Leetcode 407
31:10
Techdose
Рет қаралды 7 М.
Grid Game | Leetcode 2017
21:51
Techdose
Рет қаралды 7 М.
Map of Highest Peak | Leetcode 1765
15:19
Techdose
Рет қаралды 4,4 М.
Find the Prefix Common Array of Two Arrays | Leetcode 2657
15:34
Count Servers that Communicate | Leetcode 1267
11:51
Techdose
Рет қаралды 4,1 М.
String Matching in an Array | Leetcode 1408
19:20
Techdose
Рет қаралды 4,9 М.
Check if a Parentheses String Can Be Valid | Leetcode 2116
25:49
Жездуха 41-серия
36:26
Million Show
Рет қаралды 5 МЛН