Trapping Rain Water Problem ( LeetCode #42 ) Part-1 | FREE DSA Course in JAVA | Lecture 72
Вставка
- Опубліковано 8 лют 2023
- This is a popular Leetcode question asked in many product-based companies like Google, Facebook, and Salesforce.
The question reads out:-
Given n non negative integers representing an elevation map where the width of each bar is 1. Calculate how much water it can trap after raining.
So basically we have to find out how many units of water will be stored with the given arrangement of bars in form of an array.
There are certain observations you can make some base conditions
1. There should be minimum of three bars
2. Increasing bars will not store any water
3. Decreasing water will not store any water
4. 1st and last bar will not store any water
So you can use them to directly return 0 as the answer.
Check out the complete lecture to know the complete solution to the program to find the trapped rain water.
For more information, fill this form: forms.gle/8eiUmM92Fx563Aen9
or call us at 8884881203
Facebook: / thetapacademy
Instagram: / tapacademy_online
Linkedin: / 73820805
Website: www.thetapacademy.com
#java #javaarray #array #arrayjava #javaprogramming #programming #programmer
Seriously, i have learnt from many youtube channels but the way you explain concepts, i gurauntee no one can. Even this DSA series is far better than the paid courses.
You are doing extremely well. Thank you so much, you are a great teacher.👍
Bayya, You are the only teacher who is dedicating more than paid teachers.
Oh my god! So nicely explained. Best way to teach coding I think. Subscribed to your channel and shared to my friends bro!
Incredible explanation wow 🎉🎉🎉
Best explanation
Amazing Explaination 👑👑
best explanation ever
Excellent explanation sir ❤
Best explanation ever .. after kunal kushwaha this is the best course
I find Rohit is far ahead of Kunal Kushwaha
🥰🥰
In the animation code, Result variable should be out of the forloop. It is getting overridden in every iteration.
yes this is the comment i was searching for
Sir what is the midddle bar is greater than the left as well as the right bar, when there will be total number of 3 bars.
Sorry for asking this. Are you anirudh ravichandar's brother? Your voice and appearance same as him. Kudos to you and your team. Vera level teaching ♥️. Soon you will teach 1M I'm one of them.
Sir, some issues with the recorded voice.
Sir,
your code run this my pc to execute output written 7
what r u saying?
explain properly
@@ShivKumar-py4oe my output written by 7
@@ShivKumar-py4oe answer from your code gives output 7 whereas required output is 9 on leetcode
What happened
time complexity is O(n2) as there is a loop inside a loop.
This is more optimal and is in O(n).
Using JavaScript:
const input = [3, 1, 2, 4, 0, 1, 3, 2];
const input2 = [4, 2, 0, 3, 2, 5];
const input3 = [4];
const getTrappedWater = (input) => {
let leftGreatestValue = input[0];
let rightGreatestValue = input[input.length - 1];
let leftArray = [];
let rightArray = [];
input.map((e) => {
leftGreatestValue = Math.max(e, leftGreatestValue);
leftArray.push(leftGreatestValue);
});
for (let i = input.length - 1; i >= 0; i--) {
rightGreatestValue = Math.max(input[i], rightGreatestValue);
rightArray.unshift(rightGreatestValue);
}
let trappedWater = 0;
leftArray.map((value, index) => {
trappedWater +=
Math.min(leftArray[index], rightArray[index]) - input[index];
});
return trappedWater;
};
console.log(getTrappedWater(input));
console.log(getTrappedWater(input2));
console.log(getTrappedWater(input3));
Bro takes 36 minutes and ended after finding brute force solution 🤡
part 2 dekho na