classSolution { public: intcountDaysTogether(string arriveAlice, string leaveAlice, string arriveBob, string leaveBob){ vector<int> days = {31,28,31,30,31,30,31,31,30,31,30,31}; vector<int> already(1,0); for(int i = 0; i < 12; i++){ already.push_back(already.back()+days[i]); } int aa = getDate(arriveAlice,already); int al = getDate(leaveAlice,already); int ba = getDate(arriveBob,already); int bl = getDate(leaveBob,already); returnmax(0,min(bl,al) - max(ba,aa)+1); }
intgetDate(string str,vector<int> &already){ int mon = stoi(str.substr(0,2)); int date = stoi(str.substr(3)); return already[mon-1] + date; }
classSolution { public: intmaximumTastiness(vector<int>& price, int k){ int n = price.size(); sort(price.begin(),price.end()); int left = 0, right = price[n - 1] - price[0]; while(left < right) { int mid = (left + right + 1) >> 1; if (calculate(price, k, mid)){ right = mid-1; } else { left = mid; } } return right; } boolcalculate(vector<int> &price, int k, int mid){ int num = 0; int pre = INT_MIN >> 1;; for(int i = 0; i < price.size(); i++) { if(price[i] - pre >= mid) { num++; pre = price[i]; } } return num < k; } };