class Solution {
public:
bool PredictTheWinner(vector<int>& nums){int n = nums.size();
vector<vector<int>>dp(n, vector<int>(n,0));for(int i =0; i < n; i ++) dp[i][i]= nums[i];for(int i = n -1; i >=0; i --)for(int j = i +1; j < n; j ++)
dp[i][j]=max(nums[i]- dp[i +1][j], nums[j]- dp[i][j -1]);return dp[0][n -1]>=0;}};
using Pss = pair<string, string>;struct cmp{
bool operator()(Pss x, Pss y){return x.second.size()< y.second.size();}};
class Solution {
public:intfindMinStep(string board, string hand){
priority_queue<Pss, vector<Pss>, cmp>md;
md.push({board, hand});while(md.size()){auto pt = md.top(); md.pop();auto t = pt.first, st = pt.second;if(t.size()==0)return hand.size()- st.size();for(int i =0, j; i < t.size(); i = j){
j = i +1;while(j < t.size()&& t[j]== t[i]) j ++;auto nt = t, nst = st;auto len = j - i;if(len >=3) nt.erase(i, len);else{auto c = t[i];auto pos = st.find(c);if(pos != st.npos){
nst.erase(pos,1);
nt.insert(nt.begin()+ i, c);}elsecontinue;}
md.push({nt, nst});}}return-1;}};
class Solution {
public:
vector<int>constructRectangle(int area){int W =sqrt(area);while(area % W !=0) W--;return{area / W, W};}};
class Solution {
public:intreversePairs(vector<int>& nums){
vector<int>tmp(nums.size(),0);returnmergeSort(nums, tmp,0, nums.size()-1);}intmergeSort(vector<int>&nums, vector<int>&tmp,int l,int r){if(l >= r)return0;int m = l +(r - l)/2;int res =mergeSort(nums, tmp, l, m)+mergeSort(nums, tmp, m +1, r);int tl = l;int tr = m +1;while(tl <= m && tr <= r){if((long)nums[tl]>2*(long)nums[tr]){
res += m - tl +1;++ tr;}else{++tl;}}//进行归并int ind =0;
tl = l;
tr = m +1;while(tl <= m && tr <= r){if(nums[tl]<= nums[tr])
tmp[ind++]= nums[tl ++];else
tmp[ind++]= nums[tr ++];}while(tl <= m) tmp[ind++]= nums[tl ++];while(tr <= r) tmp[ind ++]= nums[tr ++];for(int i = l; i <= r; i ++)
nums[i]= tmp[i - l];return res;}};
class Solution {
public:intfindTargetSumWays(vector<int>& nums,int S){returndfs(nums, S,0);}intdfs(vector<int>&nums,long target,int start){if(target ==0&& start == nums.size())return1;if(start >= nums.size())return0;int res =0;
res +=dfs(nums, target - nums[start], start +1);
res +=dfs(nums, target + nums[start], start +1);return res;}};
class Solution {
public:intfindTargetSumWays(vector<int>& nums,int S){int sum =0;for(int n : nums)
sum += n;return sum < S ||(S + sum)%2==1?0:subSum(nums, S + sum >>1);}intsubSum(vector<int>&nums,int s){//int *dp = new int[s + 1];//memset(dp, 0, (s + 1) * sizeof(int));int dp[s +1]={0};//for(int i = 0; i < s + 1; i ++) cout << dp[i] << ' ';
dp[0]=1;for(int n : nums)for(int i = s; i >= n; i --)//从后往前遍历
dp[i]+= dp[i - n];return dp[s];}};
class Solution {
public:intfindPoisonedDuration(vector<int>& timeSeries,int duration){if(timeSeries.empty())return0;int total =0;for(int i =0; i < timeSeries.size()-1; i ++)
total +=min(timeSeries[i +1]- timeSeries[i], duration);return total + duration;}};
class Solution {
public:
vector<int>nextGreaterElement(vector<int>& nums1, vector<int>& nums2){
vector<int> res;
stack<int>stk;//单调栈
unordered_map<int,int>hash;for(int i = nums2.size()-1;~i; i --){int t = nums2[i];while(stk.size()&& stk.top()<= t) stk.pop();
hash[t]= stk.size()? stk.top():-1;
stk.push(t);}for(auto x : nums1)
res.push_back(hash[x]);return res;}};
class Solution {
public:
vector<vector<int>> rects;
vector<int> areas;int total =0;Solution(vector<vector<int>>& rects){
this->rects = rects;
areas.push_back(0);for(auto&rec : rects){int a =(rec[2]- rec[0]+1)*(rec[3]- rec[1]+1);
areas.push_back(areas.back()+ a);}
total = areas.back();}
vector<int>pick(){int r =rand()% total +1;int index =lower_bound(areas.begin(), areas.end(), r)- areas.begin()-1;int delta = r - areas[index]-1;int width = rects[index][2]- rects[index][0]+1;int x = delta % width + rects[index][0];int y = delta / width + rects[index][1];return{x, y};}};/**
* Your Solution object will be instantiated and called as such:
* Solution* obj = new Solution(rects);
* vector<int> param_1 = obj->pick();
*/
class Solution {
public:
vector<int>findDiagonalOrder(vector<vector<int>>& matrix){
vector<int>res;if(matrix.size()==0|| matrix[0].size()==0)return{res};int r =0, c =0;int row = matrix.size(), col = matrix[0].size();
res = vector<int>(row * col);for(int i =0; i < res.size(); i ++){
res[i]= matrix[r][c];if((r + c)%2==0)//r + c为遍历层数 偶数向上遍历 奇数向下遍历{if(c == col -1)//列遍历完 行向增加准备向下遍历
r ++;elseif(r ==0)//行遍历完 列增加准备向下遍历
c ++;else//向斜上移动{
r --;
c ++;}}else// 奇数{if(r == row -1)//行遍历完 列增加向上遍历
c ++;elseif(c ==0)//列遍历完 行增加向向上遍历
r ++;else//向斜下移动{
r ++;
c --;}}}return res;}};