Find Score of an Array After Marking All Elements | 2 Approaches | Leetcode 2593 | codestorywithMIK

  Рет қаралды 5,318

codestorywithMIK

codestorywithMIK

Күн бұрын

Пікірлер: 60
@bashirahmad6668
@bashirahmad6668 Ай бұрын
Good Morning bhaiya, Yesterday I got placed(on campus) in E2E networking on package around 12LPA. A huge thank to you, I am following you from last 8 month and your content and motivation help me a lot in my journey. Again Thank you soo much for your content and guidance. 💗❤
@codestorywithMIK
@codestorywithMIK Ай бұрын
Many Many Congratulations 🎊🎊🎉🎉❤️❤️ So happy to hear this ❤️
@soumyajitpaul-p8z
@soumyajitpaul-p8z Ай бұрын
Vhiya question kase Aya tha
@bashirahmad6668
@bashirahmad6668 Ай бұрын
@@codestorywithMIK thank you so much sir 😊
@bashirahmad6668
@bashirahmad6668 Ай бұрын
@@soumyajitpaul-p8z mostly resume base and project. DSA me linked list, graph and oops.
@aws_handles
@aws_handles Ай бұрын
Congratulations
@Shubhammi1100
@Shubhammi1100 Ай бұрын
Thanks, Mik. I solved this question in under 5 minutes by simply recalling the concept you explained in the robot collisions (2751) video.
@bhaskar_saini
@bhaskar_saini Ай бұрын
This channel taught me how to break down the question and think about what is required to solve the step. For eg in this question we require minimum element ...min heap rings in my mind, then for marking adjacent element I thought to take pair in min heap. All it takes 5 min to figure out the solution all thanks to MIk bhai. This is my approach class Solution { public: long long findScore(vector& nums) { long long sum = 0; //min heap priority_queuepq; int n = nums.size(); for(int i=0;i 0){ //add in sum sum += p.first; //mark adjacents make them negative if(p.second+1 < n){ nums[p.second+1] = -1; } if(p.second-1 >= 0){ nums[p.second-1] = -1; } } } return sum; } };
@codestorywithMIK
@codestorywithMIK Ай бұрын
❤️❤️🙏🙏
@FanIQQuiz
@FanIQQuiz Ай бұрын
Done already. Here for motivation and learning
@kishorkale3905
@kishorkale3905 Ай бұрын
Approach is good and easy to understand
@DeadCode_Debugs
@DeadCode_Debugs Ай бұрын
i only followed these 2 guys for dsa one is striver and this legend
@amarnathtripathy4350
@amarnathtripathy4350 Ай бұрын
loved the first way literally GOATED
@gauravbanerjee2898
@gauravbanerjee2898 Ай бұрын
Thanks a lot bhaiya ❤❤
@bhuppidhamii
@bhuppidhamii Ай бұрын
Coding after a long time, thanks mik
@kishorkale3905
@kishorkale3905 Ай бұрын
Nice explanation
@kishorkale3905
@kishorkale3905 Ай бұрын
Approach is good
@harshitgoyal7206
@harshitgoyal7206 Ай бұрын
attendance marking here 😀😇
@Tech_Explorer-d6g
@Tech_Explorer-d6g Ай бұрын
Amazing!! eazzyyyyyyyyyyy hai.
@rajivkumarkale
@rajivkumarkale Ай бұрын
This can also be solved using monotonic stack, there is no need to keep the stack, we can simulate it using two pointers thus making Time complexity O(n) and space complexity O(1)
@kaushikdutta2666
@kaushikdutta2666 Ай бұрын
best content. :)
@Coder_Buzz07
@Coder_Buzz07 Ай бұрын
Motivation ❤❤
@GAURAVKUMAR-pc6bk
@GAURAVKUMAR-pc6bk 27 күн бұрын
Hi MIK, i just finished this playlist of Heaps of yours. I just wanna say WOW. I loved it, the way you explained it is just amazing. Your channel is so so underrated. Hats off ! You gained another subscriber and a follower today
@codestorywithMIK
@codestorywithMIK 26 күн бұрын
Thank you for your kind words ❤️❤️🙏🙏 Start from Graph Concepts first. Find the link and some details below. I usually create two playlists for every topic : 1) Concepts Playlist - Contains from basic concepts to expert concepts. 2) Popular Interview Problems playlist. I have created concepts and interview problems playlist for 1) Graph 2) Recursion 3) DP 4) Segment Tree Planning soon to create concepts playlist for Tree as well. Graph Concepts - kzbin.info/aero/PLpIkg8OmuX-LZB9jYzbbZchk277H5CbdY&si=lZG2IJTmSW4kRrx- Graph Popular Interview Problems - kzbin.info/aero/PLpIkg8OmuX-I_49pdy1XFY6OcATnxUrrO&si=CG2JvGWVmvoSqvWA Recursion Concepts - kzbin.info/aero/PLpIkg8OmuX-IBcXsfITH5ql0Lqci1MYPM&si=614iI4NyHY-FTeJH Recursion Problems (In progress) - kzbin.info/aero/PLpIkg8OmuX-IXOgDP_YYiJFqfCFKFBDkO&si=88fBhRnr62OYTnDP DP Concepts (In progress) - kzbin.info/aero/PLpIkg8OmuX-JhFpkhgrAwZRtukO0SkwAt&si=laFVYy6ep2BkOg0s DP Popular interview problems - kzbin.info/aero/PLpIkg8OmuX-L_QqcKB5abYynQbonaNcq3&si=VHEn9b-wqTnAVyyi Segment Tree Concepts - kzbin.info/aero/PLpIkg8OmuX-K1qUIQToCllUO0UIKXt8dB&si=xm7DqRN4H0eZwna4
@GAURAVKUMAR-pc6bk
@GAURAVKUMAR-pc6bk 26 күн бұрын
@codestorywithMIK Thank you it's such a great help, gotta complete this all 😊❤️
@user-du6et6ek4n
@user-du6et6ek4n Ай бұрын
Thanks bhaiya , generally i won't use set data struct that much but through your videos and appraoch i learned new technique and find multiple answer for a single problem 🍁🍁🎖🎖 Here is my appraoch using priority queue and unordered_set class Solution { public: #define ll long long #define p pair long long findScore(vector& nums) { ll sum = 0; priority_queue< p , vector , greater > pq; unordered_set st; for(int i=0; i 0 && index < nums.size()-1){ st.insert(index -1); st.insert(index +1); } else if(index == 0) st.insert(index + 1); else st.insert(index -1); } } return sum; } };
@GovindSinghGurjar-kj8ej
@GovindSinghGurjar-kj8ej Ай бұрын
class Solution { public: long long findScore(vector& nums) { stack st; long long ans=0; for(int i=0;i
@vishwashsoni610
@vishwashsoni610 Ай бұрын
Sir, this is how I solved this question. I know it's not the most optimal solution, but I tried it on my own. class Solution { public: long long findScore(vector& nums) { unordered_mapmp; int n = nums.size(); for(int i=0;i= 0){ marked[idx-1] = true; } if(idx+1 < n){ marked[idx+1] = true; } } return ans; } };
@codestorywithMIK
@codestorywithMIK Ай бұрын
Thank you for sharing your solution! Glad to know you tried and came up with a different approach. 👌
@NikunjKumarGupta
@NikunjKumarGupta Ай бұрын
instead you continue at marked[idx] == true condition , you can break ;
@vishwashsoni610
@vishwashsoni610 Ай бұрын
@@NikunjKumarGupta no we can not use break; continue ensures that the current iteration is skipped, but the loop keeps running to process the next element in the priority queue.
@NikunjKumarGupta
@NikunjKumarGupta Ай бұрын
@@vishwashsoni610 hm I just noticed , thanks 👍
@Tech_Explorer-d6g
@Tech_Explorer-d6g Ай бұрын
Hi Mazhar, I’d be grateful if you could make a playlist about computer fundamentals. If that's not possible right now, maybe you could make a short video on the resources you found most useful while learning. There are tons of resources out there, in PDFs and long videos, and it confuses me how much I should know. I understand the importance of having thorough knowledge, but given my time constraints, I want to use my time wisely. One question that puzzles me is how much I should study. I wasn't really a serious student before, but I'm trying now, and it feels overwhelming at times, especially with DSA, development, computer fundamentals, and aptitude. Your videos have been really helpful with DSA, and I am truly grateful for that. I hope you can come up with something for the other topics as well. Thanks!
@RishabhChatterjee-fg2gz
@RishabhChatterjee-fg2gz Ай бұрын
bhaiya pehle mene map and heap se banaya tha class Solution { public: long long findScore(vector& nums) { long score = 0; unordered_map mp; priority_queue pq; for(int i = 0; i < nums.size(); i++) { mp[i] = nums[i]; pq.push({nums[i], i}); } while(!pq.empty()) { long smallestElement = pq.top().first; long index = pq.top().second; pq.pop(); if(mp.find(index) != mp.end()) { score += smallestElement; mp.erase(index); if(index - 1 >= 0) mp.erase(index - 1); if(index + 1 < nums.size()) mp.erase(index + 1); } } return score; } }; uske baad map ko nhi use karke sirf ek visited array use kar liya class Solution { public: long long findScore(vector& nums) { long score = 0; vector visited(nums.size()); priority_queue pq; for(int i = 0; i < nums.size(); i++) pq.push({nums[i], i}); while(!pq.empty()) { long smallestElement = pq.top().first; long index = pq.top().second; pq.pop(); if(!visited[index]) { score += smallestElement; visited[index] = true; if(index - 1 >= 0) visited[index -1] = true; if(index + 1 < nums.size()) visited[index + 1] = true; } } return score; } };
@yashagarwal9784
@yashagarwal9784 Ай бұрын
Hi Mazhar, I saw an order of N solution also using deque data structure, If you could explain that approach too. Also, You are the best!!! I have been folloowing you since day 1 on of your YT
@soumyajitpaul-p8z
@soumyajitpaul-p8z Ай бұрын
Vhiya ek algorithms ka playlists banado
@NihitGupta-dw4pb
@NihitGupta-dw4pb Ай бұрын
Can you plz explain the stack approach ??
@pun3eth_
@pun3eth_ Ай бұрын
Bhayya,Can you explain the deque solution for above problem
@joydeep-halder
@joydeep-halder Ай бұрын
@helloliuhbnn
@helloliuhbnn Ай бұрын
Hi sir make structure course i will join
@codestorywithMIK
@codestorywithMIK Ай бұрын
Hi there, Appreciate your kind suggestion! I'll keep it in mind and explore the possibility ❤️❤️🙏🙏
@jeevan-23
@jeevan-23 Ай бұрын
hi mik i have now confidence in all topics and I'm in final year and i have a question, what should i do like how should i make myself even stronger , like where should i practice the questions. can you help me please
@vaibhavshahi5601
@vaibhavshahi5601 Ай бұрын
Can you make a video on leetcode 30?
@HeetVichhivora
@HeetVichhivora Ай бұрын
mai "SHOONYA" hu
@aizad786iqbal
@aizad786iqbal Ай бұрын
one doubt, you said heapify takes taken O(n), but if we insert via for loop and push, takes O(n log n), would be good to know how exactly...
@codestorywithMIK
@codestorywithMIK Ай бұрын
A very good Question. Thanks for asking ❤️ Heapify (O(n)): When we build a heap using the heapify process, we start from the bottom-most non-leaf nodes and move upwards. Each level has fewer nodes, and the cost of heapifying decreases as we move up. The total time complexity sums up to O(n) - I will create a video on why is this so. Insert via for loop and push (O(n log n)): If we insert elements one by one into the heap using a loop, each insertion takes O(log n) time (to maintain the heap property). For n elements, this results in O(n log n) complexity. So, the difference lies in the approach: Heapify is a bulk operation that optimizes the process. Push works element by element, which is less efficient for building a heap from scratch.
@zaffarzeshan1308
@zaffarzeshan1308 Ай бұрын
same ele min index how to choose?
@shreyabajaj4588
@shreyabajaj4588 Ай бұрын
class Solution { public long findScore(int[] nums) { int []arr=new int [nums.length]; Arrays.fill(arr,0); PriorityQueuepq=new PriorityQueue((a,b)->{ if(a[0]!=b[0]){ return Integer.compare(a[0],b[0]); }else{ return Integer.compare(a[1],b[1]); } }); long ans=0; int count=0; for(int i=0;i
@100solarmass
@100solarmass Ай бұрын
class Solution public long findScore(int[] nums) { int n nums.length; long sum = 0; var idx = new Integer[n]; for (int i = 0; i < n; i++) idx[i] = i; Arrays.sort(idx, (a,b) nums[a]-nums[b]); var visited new boolean[n + 2]; for (int i: idx) { if (!visited[i+1]) { sum += nums[i]; visited[i] = true; visited[i + 2] = true; } } return sum Some solved like this🥶🥶🥶
@aizad786iqbal
@aizad786iqbal Ай бұрын
this question is marked with topic greedy, bit manipulation etc, not sure if that is possible... it is marked with a lot of topics on leetcode...
@codestorywithMIK
@codestorywithMIK Ай бұрын
I have a feeling that they mentioned bitset because it can be used here to mark the visited numbers instead of using a visited vector or hashset etc. But i don’t think it’s even required. We can ignore it .
@YogeshBora07
@YogeshBora07 Ай бұрын
bhaiya please upload a video onn leetcode 3259
@aizad786iqbal
@aizad786iqbal Ай бұрын
easy tha but nahi ban paa raha, I didn't think of visited array for some reason, bas map/set aa raha tha dimag mai... btw PriorityQueue pq = new PriorityQueue((x, y) -> x[0] == y[0] ? Integer.compare(x[1],y[1]) : Integer.compare(x[0],y[0])); this is easier to understand in java...
@codestorywithMIK
@codestorywithMIK Ай бұрын
Thank you for sharing ❤️
@100solarmass
@100solarmass Ай бұрын
Queue pq = new PriorityQueue((x, y) -> if( x[0] == y[0] ) return x[1] - y[1]; return x[0]-y[0] ));
@dayashankarlakhotia4943
@dayashankarlakhotia4943 Ай бұрын
public long findScore(int[]nums){ long score; for(int i=0;i
@rajeshkumar-ws5ku
@rajeshkumar-ws5ku Ай бұрын
this is the brute force of this questions class Solution { public: pair smallestNumber(vector &nums){ int n=nums.size(); int ind=-1; int min=1e9; for(int i=0;inums[i]){ min=nums[i]; ind=i; } } return { min,ind}; } long long findScore(vector& nums) { /// brutr force int n=nums.size(); long long score=0; while (true) { auto [small, ind] = smallestNumber(nums); if (small == 1e9) break; // Exit when no valid numbers are left score += small; nums[ind] = 1e9; // Mark the current number as processed // Mark neighbors as processed if (ind > 0) nums[ind - 1] = 1e9; if (ind < nums.size() - 1) nums[ind + 1] = 1e9; } return score; } };
@100solarmass
@100solarmass Ай бұрын
PriorityQueue pq = new PriorityQueue(); for (int x: nums) pq.add(x); for (int j = 0; j < n; j++) { int min = pq.poll(); for (int i = 0; i < n; i++) { if (nums [i] min && nums[i] > 0) { sum += nums[i]; nums[i] = -nums [i]; if (n>1) { if (i = 0) { nums[i + 1] = -Math.abs(nums[i + 1]); else if (i = n - 1) { nums[i - 1] = -Math.abs(nums[i - 1]); } else { nums[i - 1] = -Math.abs(nums[i - 1]); nums [i + 1] = Math.abs(nums [i + 1]); } } return sum; 5 test were failed due to TLE
We Attempted The Impossible 😱
00:54
Topper Guild
Рет қаралды 56 МЛН
coco在求救? #小丑 #天使 #shorts
00:29
好人小丑
Рет қаралды 120 МЛН
I made Tetris in C, this is what I learned
15:15
Austin Larsen
Рет қаралды 26 М.
BRAIN ROT | Why You Are Losing Control Of Your Brain?
17:40
Aevy TV
Рет қаралды 906 М.
Dependency Injection, The Best Pattern
13:16
CodeAesthetic
Рет қаралды 908 М.
one year of studying (it was a mistake)
12:51
Jeffrey Codes
Рет қаралды 149 М.