Leetcode-5207 Get Equal Substrings Within Budget(尽可能使字符串相等)
1 typedef long long ll; 2 #define _for(i,a,b) for(int i = (a);i < b;i ++) 3 #define _rep(i,a,b) for(int i = (a);i > b;i --) 4 #define INF 0x3f3f3f3f 5 #define pb push_back 6 #define maxn 10000 7 8 class Solution 9 { 10 public: 11 vector<int> d; 12 int equalSubstring(string s, string t, int maxCost) 13 { 14 int ans = 0; 15 _for(i,0,s.size()) 16 { 17 if(s[i]>t[i]) 18 d.pb(s[i]-t[i]); 19 else 20 d.pb(t[i]-s[i]); 21 } 22 int st = 0,ed = 0; 23 int sum = 0; 24 while(ed < d.size() && st<=ed) 25 { 26 sum += d[ed]; 27 if(sum <= maxCost) 28 { 29 ans = max(ans,ed-st+1); 30 ed ++; 31 } 32 else 33 { 34 sum -= d[st]; 35 st ++; 36 ed ++; 37 } 38 } 39 return ans; 40 } 41 };