JAVA Code: public int editDistance(String str1, String str2) { // Code here int n = str1.length(); int m = str2.length(); int dp[][] = new int[n + 1][m + 1]; for(int[] arr : dp){ Arrays.fill(arr, -1); } return util(str1, str2, n, m, dp); } int util(String str1, String str2, int n, int m, int[][] dp){ if(n == 0){ return m; } if(m == 0){ return n; } if(dp[n][m] != -1){ return dp[n][m]; } if(str1.charAt(n - 1) == str2.charAt(m - 1)){ return dp[n][m] = util(str1, str2, n - 1, m - 1, dp); } return dp[n][m] = Math.min(util(str1, str2, n, m - 1, dp) + 1, Math.min(util(str1, str2, n - 1, m, dp) + 1, util(str1, str2, n - 1, m - 1, dp) + 1)); }
@ajinkyajain2302Күн бұрын
C++ Code: int editDistanceUtil(const std::string& str1, const std::string& str2, int pos1, int pos2, std::vector<std::vector<int>>& dp) { if (pos1 == 0) { return pos2; } if (pos2 == 0) { return pos1; } if (dp[pos1][pos2] != -1) { return dp[pos1][pos2]; } if (str1[pos1 - 1] == str2[pos2 - 1]) { return dp[pos1][pos2] = editDistanceUtil(str1, str2, pos1 - 1, pos2 - 1, dp); } return dp[pos1][pos2] = std::min(editDistanceUtil(str1, str2, pos1 - 1, pos2, dp), std::min(editDistanceUtil(str1, str2, pos1, pos2 - 1, dp), editDistanceUtil(str1, str2, pos1 - 1, pos2 - 1, dp))) + 1; } int editDistance(const std::string& str1, const std::string& str2) { int m = str1.size(); int n = str2.size(); std::vector<std::vector<int>> dp(m + 1, std::vector<int>(n + 1, -1)); return editDistanceUtil(str1, str2, m, n, dp); }
@abdihaashin135012 күн бұрын
you talk too fast. talk slow
@selfsigma976422 күн бұрын
nice code..thanks!!
@_arnav_chaudhuri22 күн бұрын
crazy knowledge !!
@venumarsh319522 күн бұрын
Great
@parameshkumar658022 күн бұрын
i too thought about similar approach but failed to implement it🙃.Thank you.
@bharatmittal137523 күн бұрын
Great approach. Appreciate it
@sachingaikwad507727 күн бұрын
Clear and concise explanation.
@ajinkyajain230227 күн бұрын
Glad you liked it
@Lawandorder02 ай бұрын
thelogictutorial.blogspot.com/2024/05/find-pair-given-difference-17-may.html#google_vignette daily solution here
@ankurverma40422 ай бұрын
sort(arr.begin(),arr.end()); int i = 0, j = 1; while (i < n && j < n) { int diff = abs(arr[j] - arr[i]); if (diff == x && i != j) { return 1; } else if (diff < x) { j++; } else { i++; } // Ensure i and j don't overlap incorrectly if (i == j) { j++; } } return -1; } This is the correct code the above code you have given is giving an error for the test case where n=10 and x=0 and the array is [1,2,3,4,5,6,7,8,9,10] when i and j are coming at the same place they are giving 0 but they cannot make a pair since they are the same number
@ajinkyajain23022 ай бұрын
If you check the video we have taken care of that check in C++code. For Java, the tests are passing without the check.
@ajinkyajain23022 ай бұрын
Also I have given the code links in the description.
@ankurverma40422 ай бұрын
okay cool
@lakshya70892 ай бұрын
Bro I can give you cool looking obs setup for recording
@SparkSpirit-pq4qq3 ай бұрын
Bro, Please make a videos for POTD in Gfg. your videos help me to improve my Programming skills as well as Getting idea to solve a POTD problems on gfg.
@nitby274 ай бұрын
sir the java code which you have provided in the github is not working
@ajinkyajain23024 ай бұрын
Let me check.
@ajinkyajain23024 ай бұрын
My bad, I have corrected the code on Github, please check, it should work.
@nitby274 ай бұрын
@@ajinkyajain2302 thank you
@Code_loading4 ай бұрын
Sir my java code is not working it's not submitting
@ajinkyajain23024 ай бұрын
Can you post your code here?
@Code_loading4 ай бұрын
@@ajinkyajain2302 class Solution { public int ans=Integer.MAX_VALUE; public int absolute_diff(Node root) { int pre=-1; inorder(root,pre); return ans; } public void inorder(Node root,int pre){ if(root==null) return ; inorder(root.left,pre); if(pre!=-1) ans=Math.min(ans,Math.abs(root.data-pre)); pre=root.data; inorder(root.right,pre); } } when i submit this code in c++ it runs fine but java code showing error.
@jawa40984 ай бұрын
@@ajinkyajain2302 class Solution { int prev = -1; int minAns = Integer.MAX_VALUE; public int absolute_diff(Node root) { // Call helper method to traverse the tree and calculate the minimum absolute difference helper(root); return minAns; } void helper(Node root) { if (root == null) { return; } // Traverse left subtree helper(root.left); // Update minAns if needed if (prev != -1) { minAns = Math.min(minAns, Math.abs(root.data - prev)); } prev = root.data; // Traverse right subtree helper(root.right); } } This code works fine
@ajinkyajain23024 ай бұрын
My bad, I have corrected the code on Github, please check, it should work.
@anupamahalder47924 ай бұрын
Nice intuition helped a lot
@suryapratapsingh68014 ай бұрын
Thank you for the clear and concise explanation!
@jawa40984 ай бұрын
Sir can you add gitHub link code for POTD always
@ajinkyajain23024 ай бұрын
Yes, will keep this in mind, will share the Git hub link in sometime.
@jawa40984 ай бұрын
@@ajinkyajain2302 Yeah you may forgot to add link in vid, no prob make a git repository for GFG POTD and upload ,so we will refer always there in repository, Thank you
@soundarr58284 ай бұрын
Thanks for the video sir. It was very helpful to understand and implement the solution.
@jawa40984 ай бұрын
yeah wonderful , request you to add GitHub Link
@sanasainath77584 ай бұрын
nice
@jawa40984 ай бұрын
Great Teaching Skills Sir , could you add your java code gihub link in videos , it would great for us to develop our problem solving skills
@ajinkyajain23024 ай бұрын
Noted. Will check on this! Thanks.
@gauravmishra13414 ай бұрын
thankyou sir
@abhishekdubey99204 ай бұрын
Dark theme is must
@ajinkyajain23024 ай бұрын
Thanks. Action taken!
@NKSmartWorld4 ай бұрын
Great sir
@parthbhatti41515 ай бұрын
Crystal Clear
@ThisIsPrince5 ай бұрын
Your video was really helpful🙌
@rohhittz5 ай бұрын
Very nice explanation bro.! When encountering a problem, the approach taken by you to resolve the issue leads to a clearer understanding. Really helpful! Keep it up...
@ajinkyajain23025 ай бұрын
Thanks 😊
@machans-2035 ай бұрын
In : [ab, bcd, b, a] , str = "abcd" Out : 1 Can u tell me, how this above input works in recursion In gfg , output is true, but my thought using this recursion it may be false. Can u explain me bro?
@garvitarora77775 ай бұрын
it can be in any order . the second and fourth element made up the answer
@ajinkyajain23025 ай бұрын
You got the answer explained in the above comment, or should I explain in detail? Let me know, thanks.
@machans-2035 ай бұрын
@@ajinkyajain2302 no bro, it's ok😁.. i find out how it's ok..
@AbhishekKumar-db5ec5 ай бұрын
thanks
@vovanspictures93286 ай бұрын
brilliant as always! 👍
@vovanspictures93286 ай бұрын
it's better to use string as a key
@ajinkyajain23026 ай бұрын
Yes this will work too for this problem, but it is not the most optimal solution, as storing strings will result in a lot of space consumption.
@rushabhlegion25606 ай бұрын
Bro explanation samjha hi nhi. You were not trying to teach the intuition, you were just trying to explain the code.
@ajinkyajain23026 ай бұрын
Hey buddy I would request you to watch the video again, Especially i think the first 5 minutes wherein I have explained the intuition (i.e. using prefix sum and remainder)
@raganaroak1827 ай бұрын
Best Solution || great explanation
@ajinkyajain23027 ай бұрын
Thanks! Would also appreciate it if you subscribe to the channel(if not subscribed) this will motivate me 😊
@RohitKumar-us8my7 ай бұрын
for n =2 in same .. we will have k*k options? if we are choosing same colors then n1 has k options and n2 also has k options ?
@ajinkyajain23027 ай бұрын
No.. it will have (k * 1) ways i.e. k ways only.. because as it will be the same colour scenario .. for the first fence it will take k ways(k colour choices).. and for the second fence it will have 1 way only(1 choice only as it will have to take the colour of the previous fence, it does not have the choice of selecting other colours), I hope it is clear.
@RohitKumar-us8my7 ай бұрын
@@ajinkyajain2302 understood.. thanks
@vovanspictures93287 ай бұрын
Thanks for video 👍
@ajinkyajain23027 ай бұрын
Most Welcome 😊
@Bankai-kamishininoyari8 ай бұрын
thanks for the O(1) approach explained very beautifully
@ajinkyajain23028 ай бұрын
Glad you liked it!
@andressaavelar9910 Жыл бұрын
Ajinkya Jain fiz os mesmos procedimentos que vc e apareceu me isto: ERR_CONNECTION_REFUSED. ja tentei varias formas de resolver como alteracao do zeppelin-0.10.1-bin-all para outras versoes, desativei e reinstalei o chrome, varios videos e stackoverflow com solucoes diferentes, mas ainda nada! Podes dar-me uma dica por favor?
@andressaavelar9910 Жыл бұрын
ahhhhhhhh que lindo! obrigada pelo video, ajudou me bastante :D
@rahulgoswami1161 Жыл бұрын
Great explanation!!! Could you please do the same in app insight in place of creating local log!
@shikhajain4554 Жыл бұрын
Nice explanation. Thanks
@ajaynikalje3857 Жыл бұрын
Nice explanation... It saved my day ..
@RPATechnology222 жыл бұрын
thank you brother for explanation
@lxtelxte86142 жыл бұрын
hello can I use zeppelin with python, pls help
@tugrulgokce2 жыл бұрын
How to install Python Libraries in Apache Zeppelin.When ı run ipynb file then I am facing ModuleNotFoundError: No module named 'findspark'.
Пікірлер
JAVA Code: public int editDistance(String str1, String str2) { // Code here int n = str1.length(); int m = str2.length(); int dp[][] = new int[n + 1][m + 1]; for(int[] arr : dp){ Arrays.fill(arr, -1); } return util(str1, str2, n, m, dp); } int util(String str1, String str2, int n, int m, int[][] dp){ if(n == 0){ return m; } if(m == 0){ return n; } if(dp[n][m] != -1){ return dp[n][m]; } if(str1.charAt(n - 1) == str2.charAt(m - 1)){ return dp[n][m] = util(str1, str2, n - 1, m - 1, dp); } return dp[n][m] = Math.min(util(str1, str2, n, m - 1, dp) + 1, Math.min(util(str1, str2, n - 1, m, dp) + 1, util(str1, str2, n - 1, m - 1, dp) + 1)); }
C++ Code: int editDistanceUtil(const std::string& str1, const std::string& str2, int pos1, int pos2, std::vector<std::vector<int>>& dp) { if (pos1 == 0) { return pos2; } if (pos2 == 0) { return pos1; } if (dp[pos1][pos2] != -1) { return dp[pos1][pos2]; } if (str1[pos1 - 1] == str2[pos2 - 1]) { return dp[pos1][pos2] = editDistanceUtil(str1, str2, pos1 - 1, pos2 - 1, dp); } return dp[pos1][pos2] = std::min(editDistanceUtil(str1, str2, pos1 - 1, pos2, dp), std::min(editDistanceUtil(str1, str2, pos1, pos2 - 1, dp), editDistanceUtil(str1, str2, pos1 - 1, pos2 - 1, dp))) + 1; } int editDistance(const std::string& str1, const std::string& str2) { int m = str1.size(); int n = str2.size(); std::vector<std::vector<int>> dp(m + 1, std::vector<int>(n + 1, -1)); return editDistanceUtil(str1, str2, m, n, dp); }
you talk too fast. talk slow
nice code..thanks!!
crazy knowledge !!
Great
i too thought about similar approach but failed to implement it🙃.Thank you.
Great approach. Appreciate it
Clear and concise explanation.
Glad you liked it
thelogictutorial.blogspot.com/2024/05/find-pair-given-difference-17-may.html#google_vignette daily solution here
sort(arr.begin(),arr.end()); int i = 0, j = 1; while (i < n && j < n) { int diff = abs(arr[j] - arr[i]); if (diff == x && i != j) { return 1; } else if (diff < x) { j++; } else { i++; } // Ensure i and j don't overlap incorrectly if (i == j) { j++; } } return -1; } This is the correct code the above code you have given is giving an error for the test case where n=10 and x=0 and the array is [1,2,3,4,5,6,7,8,9,10] when i and j are coming at the same place they are giving 0 but they cannot make a pair since they are the same number
If you check the video we have taken care of that check in C++code. For Java, the tests are passing without the check.
Also I have given the code links in the description.
okay cool
Bro I can give you cool looking obs setup for recording
Bro, Please make a videos for POTD in Gfg. your videos help me to improve my Programming skills as well as Getting idea to solve a POTD problems on gfg.
sir the java code which you have provided in the github is not working
Let me check.
My bad, I have corrected the code on Github, please check, it should work.
@@ajinkyajain2302 thank you
Sir my java code is not working it's not submitting
Can you post your code here?
@@ajinkyajain2302 class Solution { public int ans=Integer.MAX_VALUE; public int absolute_diff(Node root) { int pre=-1; inorder(root,pre); return ans; } public void inorder(Node root,int pre){ if(root==null) return ; inorder(root.left,pre); if(pre!=-1) ans=Math.min(ans,Math.abs(root.data-pre)); pre=root.data; inorder(root.right,pre); } } when i submit this code in c++ it runs fine but java code showing error.
@@ajinkyajain2302 class Solution { int prev = -1; int minAns = Integer.MAX_VALUE; public int absolute_diff(Node root) { // Call helper method to traverse the tree and calculate the minimum absolute difference helper(root); return minAns; } void helper(Node root) { if (root == null) { return; } // Traverse left subtree helper(root.left); // Update minAns if needed if (prev != -1) { minAns = Math.min(minAns, Math.abs(root.data - prev)); } prev = root.data; // Traverse right subtree helper(root.right); } } This code works fine
My bad, I have corrected the code on Github, please check, it should work.
Nice intuition helped a lot
Thank you for the clear and concise explanation!
Sir can you add gitHub link code for POTD always
Yes, will keep this in mind, will share the Git hub link in sometime.
@@ajinkyajain2302 Yeah you may forgot to add link in vid, no prob make a git repository for GFG POTD and upload ,so we will refer always there in repository, Thank you
Thanks for the video sir. It was very helpful to understand and implement the solution.
yeah wonderful , request you to add GitHub Link
nice
Great Teaching Skills Sir , could you add your java code gihub link in videos , it would great for us to develop our problem solving skills
Noted. Will check on this! Thanks.
thankyou sir
Dark theme is must
Thanks. Action taken!
Great sir
Crystal Clear
Your video was really helpful🙌
Very nice explanation bro.! When encountering a problem, the approach taken by you to resolve the issue leads to a clearer understanding. Really helpful! Keep it up...
Thanks 😊
In : [ab, bcd, b, a] , str = "abcd" Out : 1 Can u tell me, how this above input works in recursion In gfg , output is true, but my thought using this recursion it may be false. Can u explain me bro?
it can be in any order . the second and fourth element made up the answer
You got the answer explained in the above comment, or should I explain in detail? Let me know, thanks.
@@ajinkyajain2302 no bro, it's ok😁.. i find out how it's ok..
thanks
brilliant as always! 👍
it's better to use string as a key
Yes this will work too for this problem, but it is not the most optimal solution, as storing strings will result in a lot of space consumption.
Bro explanation samjha hi nhi. You were not trying to teach the intuition, you were just trying to explain the code.
Hey buddy I would request you to watch the video again, Especially i think the first 5 minutes wherein I have explained the intuition (i.e. using prefix sum and remainder)
Best Solution || great explanation
Thanks! Would also appreciate it if you subscribe to the channel(if not subscribed) this will motivate me 😊
for n =2 in same .. we will have k*k options? if we are choosing same colors then n1 has k options and n2 also has k options ?
No.. it will have (k * 1) ways i.e. k ways only.. because as it will be the same colour scenario .. for the first fence it will take k ways(k colour choices).. and for the second fence it will have 1 way only(1 choice only as it will have to take the colour of the previous fence, it does not have the choice of selecting other colours), I hope it is clear.
@@ajinkyajain2302 understood.. thanks
Thanks for video 👍
Most Welcome 😊
thanks for the O(1) approach explained very beautifully
Glad you liked it!
Ajinkya Jain fiz os mesmos procedimentos que vc e apareceu me isto: ERR_CONNECTION_REFUSED. ja tentei varias formas de resolver como alteracao do zeppelin-0.10.1-bin-all para outras versoes, desativei e reinstalei o chrome, varios videos e stackoverflow com solucoes diferentes, mas ainda nada! Podes dar-me uma dica por favor?
ahhhhhhhh que lindo! obrigada pelo video, ajudou me bastante :D
Great explanation!!! Could you please do the same in app insight in place of creating local log!
Nice explanation. Thanks
Nice explanation... It saved my day ..
thank you brother for explanation
hello can I use zeppelin with python, pls help
How to install Python Libraries in Apache Zeppelin.When ı run ipynb file then I am facing ModuleNotFoundError: No module named 'findspark'.
Thank you!
Thanks sir! Keep up the good work