class Solution {
public:
vector<vector<vector<int>>>f;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int mod = 1e9 + 7;
int findPaths(int m, int n, int N, int i, int j) {
f = vector<vector<vector<int>>>(m, vector<vector<int>>(n, vector<int>(N + 1, -1)));
return dp(m, n, N, i, j);
}
int dp(int m, int n, int k, int x, int y)
{
int &v = f[x][y][k];
if(v != -1) return v;
v = 0;
if(!k) return v;
for(int i = 0; i < 4; i ++)
{
int a = x + dx[i], b = y + dy[i];
if(a < 0 || a == m || b < 0 || b == n) v ++;
else v += dp(m, n, k - 1, a, b);
v %= mod;
}
return v;
}
};
class Solution {
public:
int findUnsortedSubarray(vector<int>& nums) {
vector<int>tmp = nums;
sort(tmp.begin(), tmp.end());
int l = nums.size(), r = 0;
for(int i = 0; i < nums.size(); i ++)
{
if(nums[i] != tmp[i])
{
l = min(l, i);
r = max(r, i);
}
}
return r - l < 0 ? 0 : r - l + 1;
}
};
class Solution {
public:
int minDistance(string word1, string word2) {
int n = word1.size(), m = word2.size();
vector<vector<int>>dp(n + 1, vector<int>(m + 1));
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= m; j ++)
{
if(word1[i - 1] == word2[j - 1])
dp[i][j] = dp[i - 1][j - 1] + 1;
else
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
return n + m - 2 * dp[n][m];
}
};
class Solution {
public:
vector<int>res;
vector<int> preorder(Node* root) {
dfs(root);
return res;
}
void dfs(Node* root)
{
if(!root) return;
res.push_back(root->val);
for(auto child : root->children)
dfs(child);
return;
}
};
class Solution {
public:
vector<int>res;
stack<Node*>stk;
vector<int> preorder(Node* root) {
if(!root) return res;
stk.push(root);
while(!stk.empty())
{
Node* node = stk.top();
stk.pop();
res.push_back(node->val);
for(int i = node->children.size() - 1; ~i; i--)
stk.push(node->children[i]);
}
return res;
}
};
class Solution {
public:
vector<int>res;
vector<int> postorder(Node* root) {
if(!root) return res;
for(auto child : root->children)
postorder(child);
res.push_back(root->val);
return res;
}
};
class Solution {
public:
vector<int>res;
stack<Node*>stk;
vector<int> postorder(Node* root) {
if(!root) return res;
stk.push(root);
while(!stk.empty())
{
auto node = stk.top();
stk.pop();
res.push_back(node->val);
for(Node* child : node->children)
stk.push(child);
}
reverse(res.begin(), res.end());
return res;
}
};
class Solution {
public:
string fractionAddition(string expression) {
if(expression.empty()) return "";
string exp = isSign(expression[0]) ? expression : '+' + expression;
pair<int, int>res{0, 1};
for(int i = 0; i < exp.size(); i ++)
{
if(isSign(exp[i]))
res = add(res, parse(exp, i));
}
return to_string(res.first) + "/" + to_string(res.second);
}
bool isSign(char c)
{
return c == '+' || c == '-';
}
bool isDigit(char c)
{
return c >= '0' && c <= '9';
}
void simplify(int& n, int& m)
{
if(n == 0)
{
m = 1;
return;
}
int t = __gcd(n, m);
n /= t;
m /= t;
}
pair<int, int>add(const pair<int, int>p1, const pair<int, int>p2)
{
int n1 = p1.first;
int d1 = p1.second;
int n2 = p2.first;
int d2 = p2.second;
int n = n1 * d2 + n2 * d1;
int d = d1 * d2;
simplify(n, d);
if(n * d < 0)
{
n = -abs(n);
d = abs(d);
}
return {n, d};
}
pair<int, int>parse(const string& exp, int l)
{
int sign = exp[l] == '+' ? 1 : -1;
int i = l + 1;
int n = 0;
while(i < exp.size() && exp[i] != '/')
{
n = n * 10 + exp[i] - '0';
i ++;
}
i ++;
int d = 0;
while(i < exp.size() && isDigit(exp[i]))
{
d = d * 10 + exp[i] - '0';
i ++;
}
simplify(n, d);
return {sign * n, d};
}
};
class Solution {
public:
double dis(vector<int>& p1, vector<int>& p2)
{
return (p2[1] - p1[1]) * (p2[1] - p1[1]) + (p2[0] - p1[0]) * (p2[0] - p1[0]);
}
bool validSquare(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4) {
vector<double>d;
d.push_back(dis(p1, p2));
d.push_back(dis(p2, p3));
d.push_back(dis(p3, p4));
d.push_back(dis(p4, p1));
d.push_back(dis(p1, p3));
d.push_back(dis(p2, p4));
sort(d.begin(), d.end());
if(d[0] == 0)
return false;
else if(d[0] == d[1] && d[1] == d[2] && d[2] == d[3] && d[4] == d[5])
return true;
return false;
}
};
class Solution {
public:
int findLHS(vector<int>& nums) {
unordered_map<int, int>hash;
for(auto x : nums) hash[x] ++;
int res = 0;
for(auto t : hash)
{
if(hash.count(t.first + 1))
res = max(res, t.second + hash[t.first + 1]);
}
return res;
}
};
class Solution {
public:
int findLHS(vector<int>& nums) {
sort(nums.begin(), nums.end());
int begin = 0, res = 0;
for(int end = 0; end < nums.size(); end++)
{
while(nums[end] - nums[begin] > 1)
begin ++;
if(nums[end] - nums[begin] == 1)
res = max(res, end - begin + 1);
}
return res;
}
};
select name, population, area
from World
where area > 3000000 or population > 25000000;
select class
from courses
group by class
having count(distinct student) >= 5;
class Solution {
public:
int maxCount(int m, int n, vector<vector<int>>& ops) {
int x_min = m, y_min = n;
for(int i = 0; i < ops.size(); i ++)
{
x_min = min(x_min, ops[i][0]);
y_min = min(y_min, ops[i][1]);
}
return x_min * y_min;
}
};
class Solution {
public:
vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) {
vector<string>res;
unordered_map<string, int>hash;
for(int i = 0; i < list1.size(); i ++)
hash[list1[i]] = i;
int minIndex = list1.size() + list2.size() - 2;
for(int i = 0; i < list2.size(); i ++)
{
if(hash.count(list2[i]))
{
if(hash[list2[i]] + i == minIndex)
res.push_back(list2[i]);
else if(hash[list2[i]] + i < minIndex)
{
res.clear();
res.push_back(list2[i]);
minIndex = hash[list2[i]] + i;
}
}
}
return res;
}
};
class Solution {
public:
int findIntegers(int num) {
vector<int> f(32);
f[0] = 1;
f[1] = 2;
for(int i = 2; i < f.size(); i ++)
f[i] = f[i - 1] + f[i - 2];
int i = 30, sum = 0, prev_bit = 0;
while(i >= 0)
{
if((num & (1 << i)) != 0)
{
sum += f[i];
if(prev_bit == 1)
{
sum --;
break;
}
prev_bit = 1;
}else
prev_bit = 0;
i --;
}
return sum + 1;
}
};