According to editorial of leet code this can be optimised to O(n*log(sumof elements)) No one on entire youtube is discussin this approach sab easy easy approach likh rahe hai
@mrf9211 ай бұрын
Time complexity calculation is wrong. you skipped the cost of sorting a (n*(n+1))/2 sized array. which would be O(n^2 log(n^2)). So total time complexity will be O(n^2log(n^2)). I think time/space complexity-wise your approach 2 is no better than approach 1.
@shaurya478 Жыл бұрын
line: 18 is wrong, zero[i] = c1 shoud be outside the if block.
@redline_ghatak Жыл бұрын
why do we need this mod over here?
@sanjithchidhirala3116 Жыл бұрын
Comment section has more factful information than video😂
@SANDEEPKUMAR-fs1tt Жыл бұрын
muh utha kr video daldi ....... phle solution cla kr to dekhlia kro
@WiFiWaLaaa Жыл бұрын
//ALL TC IS PASSED class Solution { public boolean validateBinaryTreeNodes(int n, int[] leftChild, int[] rightChild) { Queue<Integer> q = new LinkedList<>(); HashSet<Integer> hs = new LinkedHashSet<>(); int[] indegree = new int[n]; for (int i = 0; i < n; i++) { if (leftChild[i] != -1) indegree[leftChild[i]]++; if (rightChild[i] != -1) indegree[rightChild[i]]++; } int rootCount = 0; int root = -1; for (int i = 0; i < n; i++) { if (indegree[i] == 0) { rootCount++; root = i; } if (rootCount > 1) return false; } if (root == -1) return false; q.add(root); while (!q.isEmpty()) { int curr = q.remove(); if (hs.contains(curr)) return false; hs.add(curr); if (leftChild[curr] != -1) q.add(leftChild[curr]); if (rightChild[curr] != -1) q.add(rightChild[curr]); } return hs.size() == n; } }
@Anuj-vf7bg Жыл бұрын
worst way to waste time
@WiFiWaLaaa Жыл бұрын
class Solution { public boolean validateBinaryTreeNodes(int n, int[] leftChild, int[] rightChild) { Queue<Integer> q = new LinkedList<>(); HashSet<Integer> hs = new LinkedHashSet<>(); int[] indegree = new int[n]; for (int i = 0; i < n; i++) { if (leftChild[i] != -1) indegree[leftChild[i]]++; if (rightChild[i] != -1) indegree[rightChild[i]]++; } int rootCount = 0; int root = -1; for (int i = 0; i < n; i++) { if (indegree[i] == 0) { rootCount++; root = i; } if (rootCount > 1) return false; } if (root == -1) return false; q.add(root); while (!q.isEmpty()) { int curr = q.remove(); if (hs.contains(curr)) return false; hs.add(curr); if (leftChild[curr] != -1) q.add(leftChild[curr]); if (rightChild[curr] != -1) q.add(rightChild[curr]); } return hs.size() == n; } }
@atishaykumar6006 Жыл бұрын
first find the root node then do the same as above. unordered_map< int, int > mp; queue< int > q; vector< bool > rootArr(n, 1); for(int i=0; i<n; i++) { if(leftChild[i] != -1) rootArr[leftChild[i]] = false; if(rightChild[i] != -1) rootArr[rightChild[i]] = false; } int count = 0; int root = 0; for(int i=0; i<n; i++) { if(rootArr[i] == true) { count++; root = i; } } if(count > 1) return false; q.push(root); while(!q.empty()) { int node = q.front(); q.pop(); if(mp.find(node) != mp.end()) return false; mp[node]++; if(leftChild[node] != -1) q.push(leftChild[node]); if(rightChild[node] != -1) q.push(rightChild[node]); } return mp.size() == n;
@Mihir_kathpal Жыл бұрын
wrong solution
@WiFiWaLaaa Жыл бұрын
class Solution { public boolean validateBinaryTreeNodes(int n, int[] leftChild, int[] rightChild) { Queue<Integer> q = new LinkedList<>(); HashSet<Integer> hs = new LinkedHashSet<>(); int[] indegree = new int[n]; for (int i = 0; i < n; i++) { if (leftChild[i] != -1) indegree[leftChild[i]]++; if (rightChild[i] != -1) indegree[rightChild[i]]++; } int rootCount = 0; int root = -1; for (int i = 0; i < n; i++) { if (indegree[i] == 0) { rootCount++; root = i; } if (rootCount > 1) return false; } if (root == -1) return false; q.add(root); while (!q.isEmpty()) { int curr = q.remove(); if (hs.contains(curr)) return false; hs.add(curr); if (leftChild[curr] != -1) q.add(leftChild[curr]); if (rightChild[curr] != -1) q.add(rightChild[curr]); } return hs.size() == n; } }
@ganeshbunny2689 Жыл бұрын
n = 4 leftChild = [3,-1,1,-1] rightChild = [-1,-1,0,-1] Output false Expected true explain me about this
@WiFiWaLaaa Жыл бұрын
class Solution { public boolean validateBinaryTreeNodes(int n, int[] leftChild, int[] rightChild) { Queue<Integer> q = new LinkedList<>(); HashSet<Integer> hs = new LinkedHashSet<>(); int[] indegree = new int[n]; for (int i = 0; i < n; i++) { if (leftChild[i] != -1) indegree[leftChild[i]]++; if (rightChild[i] != -1) indegree[rightChild[i]]++; } int rootCount = 0; int root = -1; for (int i = 0; i < n; i++) { if (indegree[i] == 0) { rootCount++; root = i; } if (rootCount > 1) return false; } if (root == -1) return false; q.add(root); while (!q.isEmpty()) { int curr = q.remove(); if (hs.contains(curr)) return false; hs.add(curr); if (leftChild[curr] != -1) q.add(leftChild[curr]); if (rightChild[curr] != -1) q.add(rightChild[curr]); } return hs.size() == n; } }
@ersoumyajitpan7205 Жыл бұрын
wrong solution Input n = 4 leftChild = [3,-1,1,-1] rightChild = [-1,-1,0,-1] Use Testcase Output false Expected true
@Devender-Verma Жыл бұрын
Here is Correct code with impovement of this testcase 4 [3,-1,1,-1] [3,-1,1,-1] ``` class Solution { public: int findroot(int n, vector<int>& leftChild, vector<int>& rightChild){ vector<bool> visited(n, false); // visit the both array and tick true for(int i=0; i<n; i++){ if(leftChild[i] != -1) visited[leftChild[i]] = true; if(rightChild[i] != -1) visited[rightChild[i]] = true; } int root = -1; // find the index where it's false, it will be the root which has no parent, for(int i=0; i<n; i++){ if(root == -1 and visited[i] == false){ root = i; // if found more than 1 false index it means more than 1 parent exist, then simlpy return false }else if(visited[i] == false){ return -1; } } return root; } bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) { queue<int> q; set<int> hs; int root = findroot(n, leftChild, rightChild); // had more then 1 parent or cycle so return false if(root == -1) return false; q.push(root); while(!q.empty()){ int node = q.front(); q.pop(); if(hs.find(node) != hs.end()){ return false; } hs.insert(node); if(leftChild[node] != -1){ q.push(leftChild[node]); } if(rightChild[node] != -1){ q.push(rightChild[node]); } } return hs.size() == n; } }; ```
@sourabh258 Жыл бұрын
Good explanation, here is one with greedy approach: public int movesToMakeZigzag(int[] nums) { int evenMoves=0,oddMoves=0; for(int i=0;i<nums.length;i++){ int left = i-1 < 0? Integer.MAX_VALUE:nums[i-1]; int curr = nums[i]; int right= i+1>=nums.length?Integer.MAX_VALUE:nums[i+1]; int min=Math.min(left,right); if(i%2==0) evenMoves+= curr-min>=0? curr-min+1:0; else oddMoves+= curr-min>=0? curr-min+1:0; } return Math.min(evenMoves,oddMoves); }
why we are doing arr[i-k] to delete first element??
@SG-tt4pg2 жыл бұрын
Thanks, the volume is toooo low
@lloydlasrado2 жыл бұрын
good detail explanation. please keep it up same way
@rahulkumar67262 жыл бұрын
It was really helpful
@kabilduke20002 жыл бұрын
In java return Integer.parseInt(String.valueOf(num).replaceFirst("6","9"));
@qazaqempire38282 жыл бұрын
for this problem this is better video than others
@krishnagarg20602 жыл бұрын
this code is not valid if in a one row only seat no. 8 is reservd
@JitendraKumar-ti6yd2 жыл бұрын
Awesome bro!
@udhayachandhar47702 жыл бұрын
suffle is not creating new array and modifying the existing
@munendragaur49652 жыл бұрын
Bsdk apni original awaj me samja na angrej kyu ban raha he
@haoyucheng54012 жыл бұрын
not going to work if you are using set, cause it is possible for two trans to be the same, and we need them both in final answer.
@carlosj.castillo2542 жыл бұрын
thnks
@ravimane55082 жыл бұрын
this code is not running ,it giving an error that- expected ) in foreach(var kvp in map). I copied the code from the github link you provided in the comments.
@pecan84702 жыл бұрын
thank you
@rahulpothula19022 жыл бұрын
Can anyone pls find out the error (logical) in my smaallll code?: class Solution { public: int tupleSameProduct(vector<int>& nums) { int n = nums.size(); vector<int> hash(1e8, 0); int ans = 0; for(int i = 0; i < n - 1; i++) { for(int j = i + 1; j < n; j++) { int ind = nums[i] * nums[j]; hash[ind]++; if(hash[ind] >= 2) ans += 8; } } return ans; } };
@karannnful2 жыл бұрын
horrible
@AyushRaj-pm1dz2 жыл бұрын
C++ Code : int minFlips(int a, int b, int c) { int count=0; while(c || a || b){ if(c&1){ if((a&1)==0 && (b&1)==0) count++; } else{ if(a&1) count++; if(b&1) count++; } //moving through the bits right to left a >>= 1; b >>= 1; c >>= 1; } return count; }
Same solution after finding the root node. public boolean validateBinaryTreeNodes(int n, int[] leftChild, int[] rightChild) { //kzbin.info/www/bejne/qoKaqpaOrbNpepo //Declare set to find unique node in Tree Set<Integer> uniqueNode = new HashSet(); for(int left : leftChild){ if(left!=-1) uniqueNode.add(left); } for(int right : rightChild){ if(right!=-1) uniqueNode.add(right); } //If any node is not child of any other node means it's root. int rootNode = -1; for(int i=0; i<n; i++){ if(uniqueNode.contains(i)) continue; rootNode = i; break; } if(rootNode==-1){ return false; } Queue<Integer> queue = new LinkedList(); Set<Integer> visitedSet = new HashSet(); //Adding root in queue queue.add(rootNode); while(!queue.isEmpty()){ int size = queue.size(); while(size>0){ int node = queue.poll(); if(visitedSet.contains(node)) return false; visitedSet.add(node); if(leftChild[node]!=-1){ queue.add(leftChild[node]); } if(rightChild[node]!=-1){ queue.add(rightChild[node]); } size--; } } return visitedSet.size()==n; }
@ani682 жыл бұрын
The explaination was awesome....💯
@Ryan-fe2du2 жыл бұрын
Should this be time: O(n + m)? Because you looped twice on each?
@dhanashreegodase44453 жыл бұрын
Thanku
@mdmusadali91593 жыл бұрын
Hi
@mashab91293 жыл бұрын
cool question and great walk through, thank you so much for sharing your knowledge!
@kirant55483 жыл бұрын
woow. really nice solution and explained well too 😃
@jasmeenkaur60013 жыл бұрын
Why we do in opposite direction????
@vishruthreddy70783 жыл бұрын
what if q queries are given like a range of l to r how will you solve this?
@molyoxide83582 жыл бұрын
what is l and r here?
@mashab91293 жыл бұрын
great walktrhough. thanks for sharing.
@MOHITRANA-to7rf3 жыл бұрын
thank you
@AlbertoRodriguez-oe6jo3 жыл бұрын
Feels like you're reading the solution off the book or something. You could explain better with pen and paper.
@nikunjjain49973 жыл бұрын
Really simple code and explanation . Great work 👍
@arijitroy83903 жыл бұрын
Bhai tu ye fake accent mat kiya kar. Isliye dislikes zyada h tere video me
@arnabpersonal67293 жыл бұрын
This is not an expected solution in an interview we have to do a lazy increment ie increment only during pop if that element is within bottom k