Excellent explanation. It was a tricky problem. I have observed that in these types of problems figuring out the operation done on the array. If you can figure out the brute force approach then you can easily solve it using binary search. *You can also take the left value as nums[0], because max of nums can not become less than nums[0]* *So, the range will be [nums[0], max(nums)]* Java Code: class Solution { boolean maxPossible(int[] nums, int max){ int n = nums.length; long[] arr = new long[n]; for(int i = 0; i < n; i++){ arr[i] = nums[i]; } for(int i = 0; i < n-1; i++){ if(arr[i] > max){ return false; } long buffer = max - arr[i]; arr[i] += buffer; arr[i+1] -= buffer; } if(arr[n-1] > max){ return false; } return true; } public int minimizeArrayValue(int[] nums) { int l = nums[0]; int r = Integer.MIN_VALUE; for(int num: nums){ r = Math.max(r, num); } int ans = 0; while(l
The maxL can be taken as nums[0] instead of 1 as nums[0] as it the lowest value of result, as already explained in the video. So the binary search will be between nums[0] and maximum value of nums[ ]. It will make the TC a bit better. Thank you for your amazing explanation.
we learn from u importance of dry run ... which was get frustations also but... its very important we see it in this vedio .... love u bhaiya ur hard works
That’s really good. I am noting this down as an improvement over my code. Thanks a lot JJ and yes, today Graph video is coming Thank you for your patience and trust
Hi Uday, Actually DSU is almost covered. We solved qns too. But more qns will come in Graphs Qns playlist. Let me know if I have missed anything in DSU. I will definitely cover
Java solution : O(1) space complexity class Solution { boolean isMinMax(int[] nums, int midMax){
int n = nums.length; long lastVal = nums[0]; long buffer = 0; for(int i= 1; i midMax){ return false; } lastVal = nums[i]-buffer; } return true; } public int minimizeArrayValue(int[] nums) { int n = nums.length;
int s = nums[0]; // minMax can't be less than first elem as it can't be reduced int e = Arrays.stream(nums).max().getAsInt(); // max elem of nums arr int possibleAns = nums[0]; while(s
Here is the javascript code of above problem var isValid = function(nums,expectedMax){ let arr = [...nums]; for(let i = 0 ; i < arr.length-1 ; i++){ if(arr[i] > expectedMax){ return false; }else{ let buffer = Math.abs(arr[i]-expectedMax); arr[i+1] -= buffer; } } return arr[arr.length-1]
Sir plz make a video on leetcode 1482 (minimum no.of days to make m bouquets)...which based on binary search on answer concept. I find it really difficult to understand this problem and you have a really unique way of making difficult concepts quite understandable. Thank you
ALTERNATE SOLUTION class Solution { public: bool isPossible(vector&arr,int mid){ vectornums(arr.begin(),arr.end()); int n = nums.size(); long long buffer = 0; for(int i = n-1; i>= 1;i--){ if(nums[i] > mid){ nums[i-1] += nums[i]-mid; nums[i] = mid; // no need to do this just done for visualization. } } return nums[0]
Can we traverse the array if any value is bigger than exp max make it exp max( we will handle o index by our own) and increase the predecessor by arr[i] - exp if max of array is still larger than exp then return false
My main question is why do we need to get the 0th element to maximum? Why not change only the elements that are > x. x being the desired max. What is the requirement to do it. Can you please discuss the proof for it bhaiya?
bhaiya aapke method se new vector create he krna pd rha h long long type ka if isValid function me uski copy pass kre toh test case 29 pr fss rha h class Solution { public: bool isValidMaxm(vectornums,long long mid){ int n=nums.size(); for(long long i=0;imid) return false; long long buffer=mid-nums[i]; nums[i+1]=nums[i+1]-buffer; } return nums[n-1]
Bhai jaise aapne 1 line mein ek vector se doosre vector mein jo copy kiya kya vaise vector to map copy ho skta hai kya ek hi line mein. Ya vohi for loop lagana padega??
bhaiya i requested you to make video on Minimise Maximum Distance between Gas Stations this question is primium in leetcode but in other platform its free if you already make video on this please pin in the comment section
This is my java code and it is failing on few test cases: class Solution { public int minimizeArrayValue(int[] nums) { int min = 0, max = Integer.MIN_VALUE; int ans = 0; for(int x : nums){ if(x > max) max = x; } while(min
Great !!! Was not getting intuition behind the use of binary search.
Thanks Uday for watching my video ❤️
Excellent explanation. It was a tricky problem. I have observed that in these types of problems figuring out the operation done on the array. If you can figure out the brute force approach then you can easily solve it using binary search.
*You can also take the left value as nums[0], because max of nums can not become less than nums[0]*
*So, the range will be [nums[0], max(nums)]*
Java Code:
class Solution {
boolean maxPossible(int[] nums, int max){
int n = nums.length;
long[] arr = new long[n];
for(int i = 0; i < n; i++){
arr[i] = nums[i];
}
for(int i = 0; i < n-1; i++){
if(arr[i] > max){
return false;
}
long buffer = max - arr[i];
arr[i] += buffer;
arr[i+1] -= buffer;
}
if(arr[n-1] > max){
return false;
}
return true;
}
public int minimizeArrayValue(int[] nums) {
int l = nums[0];
int r = Integer.MIN_VALUE;
for(int num: nums){
r = Math.max(r, num);
}
int ans = 0;
while(l
🙏🙏❤️
Best explanation
Thank you so so much
The maxL can be taken as nums[0] instead of 1 as nums[0] as it the lowest value of result, as already explained in the video. So the binary search will be between nums[0] and maximum value of nums[ ]. It will make the TC a bit better.
Thank you for your amazing explanation.
Absolutely
Class solution {
Public int minimize Array Value (int[]nums){
long ans=0 long sum=0;
for(int i=0;i
Please do vedios for leetcode contest solutions
we learn from u importance of dry run ... which was get frustations also but... its very important we see it in this vedio .... love u bhaiya ur hard works
Means a lot 😇🙏❤️
Little change to avoid creating new array , space O(1)
class Solution {
private boolean isValid(int nums[], int mid, int n) {
if(nums[0] > mid) return false; // nums[0] ko kum toh kr skte ni
long prev = nums[0];
for(int i = 1; i < nums.length; i ++) {
long buffer = mid - prev;
if(nums[i] - buffer > mid) return false;
prev = nums[i] - buffer;
}
return true;
}
public int minimizeArrayValue(int[] nums) {
int n = nums.length;
int s = 0;
int e = 0;
int result = e;
for(int i=0; i
That’s really good. I am noting this down as an improvement over my code.
Thanks a lot JJ and yes, today Graph video is coming
Thank you for your patience and trust
@@codestorywithMIK I have full trust on you bhai and looking forward to learn from you.
PS: I'll keep bugging for concepts series😛♥
@@codestorywithMIKdsu?
Hi Uday,
Actually DSU is almost covered. We solved qns too. But more qns will come in Graphs Qns playlist.
Let me know if I have missed anything in DSU. I will definitely cover
@@codestorywithMIK As u told u'll cover dsu union by size. please cover that and anything else if it's left u can cover that too..
superb
nice explanation
story to code is magic
Good explanation. 2nd approach code write. Understood code with dryrun
Thank you 🙏
Brilliant , I knew the intution behind BS , But was not sure how to detect/logic if that max chosen can be answer or not for each mid.
❤️❤️❤️
Java implementation:
class Solution {
private boolean isValid(int nums[], int mid, int n) {
long[] arr = Arrays.stream(nums).asLongStream().toArray();
for(int i=0; i mid) {
return false;
}
long buffer = mid - arr[i];
arr[i+1] = arr[i+1] - buffer;
}
return arr[n-1]
Story explanation is next level❤
Thanku bhaiya
Thank you so so much for watching ❤️❤️❤️
the solution clicked me at 17:31 .... coded it by myself then thanks
Bahut tricky question tha
It was actually a tricky Question!!
Was waiting for your video only.
Java solution : O(1) space complexity
class Solution {
boolean isMinMax(int[] nums, int midMax){
int n = nums.length;
long lastVal = nums[0];
long buffer = 0;
for(int i= 1; i midMax){
return false;
}
lastVal = nums[i]-buffer;
}
return true;
}
public int minimizeArrayValue(int[] nums) {
int n = nums.length;
int s = nums[0]; // minMax can't be less than first elem as it can't be reduced
int e = Arrays.stream(nums).max().getAsInt(); // max elem of nums arr
int possibleAns = nums[0];
while(s
Very nice explanation sir ❤
13:30 was an epic line🤣 🤣 🤣
haha.
true
You are the best!
Means a lot 😇❤️
if we use greedy, we can do it in O(n) but the binary seach intuition was awesome....
True Akhil.
Thanks a lot
♥
please upload solutions of weekly contest as well. it will help us a lot.
Great Explanation!!!
Such a great channel😊
Thank you so much Mohit ❤️
Here is the javascript code of above problem
var isValid = function(nums,expectedMax){
let arr = [...nums];
for(let i = 0 ; i < arr.length-1 ; i++){
if(arr[i] > expectedMax){
return false;
}else{
let buffer = Math.abs(arr[i]-expectedMax);
arr[i+1] -= buffer;
}
}
return arr[arr.length-1]
Sir plz make a video on leetcode 1482 (minimum no.of days to make m bouquets)...which based on binary search on answer concept. I find it really difficult to understand this problem and you have a really unique way of making difficult concepts quite understandable. Thank you
ALTERNATE SOLUTION
class Solution {
public:
bool isPossible(vector&arr,int mid){
vectornums(arr.begin(),arr.end());
int n = nums.size();
long long buffer = 0;
for(int i = n-1; i>= 1;i--){
if(nums[i] > mid){
nums[i-1] += nums[i]-mid;
nums[i] = mid; // no need to do this just done for visualization.
}
}
return nums[0]
Thanks a lot
sir,
how much time have u taken to solve this perticular problem i mean total time with story to submission.
Can we traverse the array if any value is bigger than exp max make it exp max( we will handle o index by our own) and increase the predecessor by arr[i] - exp if max of array is still larger than exp then return false
What if nums[i+1] goes in negative when subtracted with buffer wont that be a problem ? It is given in question nums[i]>0
bhaiya maxL=nums[0] bhi kr skte hai na
Absolutely correct.
My main question is why do we need to get the 0th element to maximum? Why not change only the elements that are > x. x being the desired max. What is the requirement to do it. Can you please discuss the proof for it bhaiya?
Sir can u explain O(n) approach mentioned in this question editorial
Sure, that comes under Greedy.
Will create one in Greedy playlist soon.
If(nums[i] > epmax) return false kyu kiya? Nhi samajh aya 😅🥹
SIR isme out of bound kb kaise jaa rha smj nhi aa rha plz ek video banaye kb hme long long use or kaise krna chahiye.
bhaiya aapke method se new vector create he krna pd rha h long long type ka if isValid function me uski copy pass kre toh test case 29 pr fss rha h class Solution {
public:
bool isValidMaxm(vectornums,long long mid){
int n=nums.size();
for(long long i=0;imid) return false;
long long buffer=mid-nums[i];
nums[i+1]=nums[i+1]-buffer;
}
return nums[n-1]
tumhara nums[i] integer me hi hai usko bhi explicitly long long karo
TC is O(nlogn)??
Correct.
Log n for binary search and O(n) for isValid
yes
Bhai jaise aapne 1 line mein ek vector se doosre vector mein jo copy kiya kya vaise vector to map copy ho skta hai kya ek hi line mein.
Ya vohi for loop lagana padega??
I think not possible. Loop is the only option
@@codestorywithMIK OK
map me to key and value pair hoga na, to directly copy kaise karna chahte ho ?
@@elakstein key apne aap store ho jaaye aur uske frequency bhi apne aap increment ho jaaye iss hisab se soch rha tha.
bhaiya i requested you to make video on Minimise Maximum Distance between Gas Stations this question is primium in leetcode but in other platform its free if you already make video on this please pin in the comment section
This is my java code and it is failing on few test cases:
class Solution {
public int minimizeArrayValue(int[] nums) {
int min = 0, max = Integer.MIN_VALUE;
int ans = 0;
for(int x : nums){
if(x > max) max = x;
}
while(min
bhai -> nums[i+1] = nums[i+1] - buffer;
kya yeh kabhi neagative mein nhi jaayega??
nhi
Good qn.
Thinking about some example where I can put this think 🤔
@@sharcodes kaise?
sir kaise pata ki maximum value 5 hi ayega 4 bhi toh ho sakta hai nah?😭
4 kaisa arha
Greedy? for this question?
Will soon post in greedy playlist
Bro can't you make shorter videos. Concentration 30 minutes ka bohot difficult hai. Videos are too long
Thanks a lot
Thank you for watching ❤️