Amazing explanation for the 4th problem. But I feel code could have been more concise. Please check below implementation with same idea as yours. class Solution { public: long long tetrisVal(int start, int end, vector&diff){ long long val = abs(diff[start]); for(int i=start+1;iabs(diff[i-1]))val+=abs(diff[i])-abs(diff[i-1]); } return val; } long long minimumOperations(vector& nums, vector& target) { vectordiff; long long ans = 0, n = nums.size(), start = 0; for(int i=0;i0 || diff[i]
yeah i have done a similar code in c++ Here it is: int count=0; int r=s.size()-1; int operations=0; while(r>=0 && s[r]=='0'){ r--; } if(r!=s.size()-1){ count++; } while(r>=0){ if(s[r]=='1'){ operations=operations+count; int t=r-1; while(t>=0 && s[t]!='1'){ t--; } if(t!=r-1){ count=count+1; } r=t; } else{ r--; } } return operations
🎓 Learn from the best! TLE 11.0 offers live classes with top mentors. Join TLE 11.0 today at www.tle-eliminators.com
Oh god, you are just an excellent teacher, literally you have the best way of explaining how it works. Thanks a lot for this editorial
Amazing explanation for the 4th problem. But I feel code could have been more concise. Please check below implementation with same idea as yours.
class Solution {
public:
long long tetrisVal(int start, int end, vector&diff){
long long val = abs(diff[start]);
for(int i=start+1;iabs(diff[i-1]))val+=abs(diff[i])-abs(diff[i-1]);
}
return val;
}
long long minimumOperations(vector& nums, vector& target) {
vectordiff;
long long ans = 0, n = nums.size(), start = 0;
for(int i=0;i0 || diff[i]
This is great! To keep it extra detailed, I prefer to go for big solutions. But this will work too.
The explanation of 4th problem was amazing .
Please discuss biweekly solutions too.
i thought the same idea, but was not able to think prefixsum can be applied to optimize it
Amazing Explanation
Thank you 🙏🙏😊
Please fill the feedback form: forms.gle/zhg513TvRNcquMd3A
awesome vid
thanku
I think the 3rd question can be solved with O(1) space complexity.
Java code
---------------------
class Solution {
public int maxOperations(String s) {
int oneCount = 0, result = 0;
for(int i = 0 ; i < s.length() - 1 ; i++) {
char ch = s.charAt(i);
char nxtCh = s.charAt(i + 1);
if(ch == '0' && nxtCh == '1')
result += oneCount;
if(ch == '1')
oneCount++;
}
if(s.charAt(s.length() - 1) == '0')
result += oneCount;
return result;
}
}
yeah i have done a similar code in c++
Here it is:
int count=0;
int r=s.size()-1;
int operations=0;
while(r>=0 && s[r]=='0'){
r--;
}
if(r!=s.size()-1){
count++;
}
while(r>=0){
if(s[r]=='1'){
operations=operations+count;
int t=r-1;
while(t>=0 && s[t]!='1'){
t--;
}
if(t!=r-1){
count=count+1;
}
r=t;
}
else{
r--;
}
}
return operations