/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int num = 0;
TreeNode* convertBST(TreeNode* root) {
if(root != NULL)
{
convertBST(root->right);
root->val = root->val + num;
num = root->val;
convertBST(root->left);
return root;
}
return NULL;
}
};
class Solution {
public:
const int DAY = 24 * 60;
int findMinDifference(vector<string>& timePoints) {
int n = timePoints.size();
vector<int>times(n, 0);
for(int i = 0; i < n; i ++)
times[i] = time2int(timePoints[i]);
sort(times.begin(), times.end());
int res = min(times[n - 1]- times[0], times[0] + DAY - times[n - 1]);
for(int i = 1; i < n; i ++)
res = min(res, times[i] - times[i - 1]);
return res;
}
int time2int(string& t)
{
int hour = stoi(t.substr(0, 2));
int minute = stoi(t.substr(3));
return hour * 60 + minute;
}
};
class Solution {
public:
int singleNonDuplicate(vector<int>& nums) {
int res = nums[0];
for(int i = 1; i < nums.size(); i ++)
res ^= nums[i];
return res;
}
};
class Solution {
public:
int singleNonDuplicate(vector<int>& nums) {
int l = 0, r = nums.size() - 1;
while(l < r)
{
int mid = l + r >> 1;
if(mid % 2 == 1) //奇数
{
if(nums[mid] == nums[mid + 1])
r = mid - 1;
else l = mid + 1;
}
else
{
if(nums[mid] == nums[mid + 1])
l = mid + 2;
else r = mid;
}
}
return nums[l];
}
};
class Solution {
public:
string reverseStr(string s, int k) {
string::iterator it = s.begin();
while(it < s.end() && it + k <= s.end())
{
reverse(it, it + k);
it = it + 2 * k;
}
if(it + k > s.end())
reverse(it, s.end());
return s;
}
};
class Solution {
public:
vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) {
int n = matrix.size(), m = matrix[0].size();
vector<vector<int>>dis(n, vector<int>(m, -1));
queue<pair<int, int>>q;
for(int i = 0; i < n; i ++)
for(int j = 0; j < m; j ++)
if(!matrix[i][j]) //是0
{
dis[i][j] = 0;
q.push(make_pair(i, j));
}
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
while(q.size())
{
auto t =q.front();
q.pop();
for(int i = 0; i < 4; i ++)
{
int x = t.first + dx[i], y = t.second + dy[i];
if(x >= 0 && x < n && y >= 0 && y < m && dis[x][y] == -1)
{
dis[x][y] = dis[t.first][t.second] + 1;
q.push(make_pair(x, y));
}
}
}
return dis;
}
};
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int res = 0;
int diameterOfBinaryTree(TreeNode* root) {
dfs(root);
return res;
}
int dfs(TreeNode* root)
{
if(!root) return 0;
int l = dfs(root->left);
int r = dfs(root->right);
res = max(res, l + r);
return max(l, r) + 1;
}
};
class Solution {
public:
int removeBoxes(vector<int>& boxes) {
vector<vector<vector<int>>>dp(150, vector<vector<int>>(150, vector<int>(150)));
return calPoints(boxes, dp, 0, boxes.size() - 1, 0);
}
int calPoints(vector<int>&boxes, vector<vector<vector<int>>>&dp, int l, int r, int k)
{
if(l > r) return 0;
if(dp[l][r][k] != 0) return dp[l][r][k];
while(l < r && boxes[r] == boxes[r - 1])
{
r--;
k++;
}
dp[l][r][k] = calPoints(boxes, dp, l, r - 1, 0) + (k + 1) * (k + 1);
for(int i = l; i < r; i ++)
{
if(boxes[i] == boxes[r])
dp[l][r][k] = max(dp[l][r][k], calPoints(boxes, dp, l, i, k + 1) + calPoints(boxes, dp, i + 1, r - 1, 0));
}
return dp[l][r][k];
}
};
class Solution {
public:
int findCircleNum(vector<vector<int>>& M) {
vector<bool>visted(M.size());
int res = 0;
for(int i = 0; i < M.size(); i ++)
{
if(!visted[i])
{
dfs(M, visted, i);
res ++;
}
}
return res;
}
void dfs(vector<vector<int>>&M, vector<bool>&visted, int i)
{
for(int j = 0; j < M.size(); j ++)
{
if(M[i][j] == 1 && !visted[j])
{
visted[j] = true;
dfs(M, visted, j);
}
}
}
};
class Solution {
public:
bool checkRecord(string s) {
int countA = 0, countL = 0;
for(int i = 0; i < s.size(); i ++)
{
if(s[i] == 'A')
{
countA ++;
countL = 0;
if(countA > 1) return false;
}
else if(s[i] == 'L')
{
countL ++;
if(countL > 2) return false;
}
else
countL = 0;
}
cout << countA << ' ' << countL << endl;
return true;
}
};
class Solution {
public:
int checkRecord(int n) {
int MOD = 1e9 + 7;
// i, A{0:non, 1:contain}, L:{0, 1, 2}
vector<vector<vector<long>>>dp(n + 1, vector<vector<long>>(2, vector<long>(3)));
dp[1][1][0] = 1; // A
dp[1][0][1] = 1; // L
dp[1][0][0] = 1; // P
for(int i = 2; i <= n; i ++)
{
//当前p结尾 0A0L = 0A0L + 0A1L + 0A2L
dp[i][0][0] = (dp[i - 1][0][0] + dp[i - 1][0][1] + dp[i - 1][0][2]) % MOD;
// 1A0L = 1A0L + 1A1L + 1A2L
dp[i][1][0] = (dp[i - 1][1][0] + dp[i - 1][1][1] + dp[i - 1][1][2]) % MOD;
//当前L结尾 0A1L = 0A0L
dp[i][0][1] = dp[i - 1][0][0];
// 0A2L = 0A1L
dp[i][0][2] = dp[i - 1][0][1];
// 1A1L = 1A0L
dp[i][1][1] = dp[i - 1][1][0];
// 1A2L = 1A1L
dp[i][1][2] = dp[i - 1][1][1];
//当前A结尾 1A0L = 0A0L + 0A1L + 0A2L
dp[i][1][0] += (dp[i - 1][0][0] + dp[i - 1][0][1] + dp[i - 1][0][2]) % MOD;
}
return (dp[n][0][0] + dp[n][0][1] + dp[n][0][2] + dp[n][1][0] + dp[n][1][1] + dp[n][1][2]) % MOD;
}
};
class Solution {
public:
string optimalDivision(vector<int>& nums) {
int n = nums.size();
if(n == 0) return "";
if(n == 1) return to_string(nums[0]);
if(n == 2) return to_string(nums[0]) + "/" + to_string(nums[1]);
string res = "";
for(int i = 0; i < n; i ++)
{
if(i == 0)
res += to_string(nums[i]) + "/(";
else
res += to_string(nums[i]) + "/";
}
res.pop_back();
res += ")";
return res;
}
};
class Solution {
public:
int leastBricks(vector<vector<int>>& wall) {
unordered_map<int, int>hash; //边界位置 出现次数
int res = 0;
for(auto blocks : wall)
{
int s = 0;
for(int i = 0; i + 1 < blocks.size(); i ++)
{
s += blocks[i];
hash[s] ++;
res = max(res, hash[s]);
}
}
return wall.size() - res;
}
};
class Solution {
public:
int nextGreaterElement(int n) {
string s = to_string(n);
for(int i = s.size() - 1; ~i; i --)
{
if(i == 0)
return -1;
if(s[i - 1] < s[i])
{
sort(s.begin() + i, s.end());
for(int j = i; j < s.size(); j ++)
{
if(s[i - 1] < s[j])
{
swap(s[i - 1], s[j]);
if(stol(s) > INT_MAX)
return -1;
return stol(s);
}
}
}
}
return -1;
}
};