classSolution { public: intdietPlanPerformance(vector<int>& calories, int k, int lower, int upper){ if (k > calories.size()) return0; int sum = accumulate(calories.begin(), calories.begin() + k, 0); int ans = 0; if (sum > upper) ++ans; elseif (sum < lower) --ans; for (int i = k; i < calories.size(); ++i) { sum = sum + calories[i] - calories[i - k]; if (sum > upper) ++ans; elseif (sum < lower) --ans; } return ans; } };
structUF { vector<int> parents; UF(int n) { parents.resize(n); for (int i = 0; i < n; ++i) { parents[i] = i; } }
intfind(int x){ return parents[x] == x ? x : (parents[x] = find(parents[x])); }
voidunio(int x, int y){ int px = find(x); int py = find(y); parents[px] = py; } };
intmain(){ int T; cin >> T; for (int i = 0; i < T; ++i) { int N, M; cin >> N >> M; UF uf(N); int ans = 0; for (int j = 0; j < M; ++j) { int l, r; cin >> l >> r; --l; --r; if (uf.find(l) == uf.find(r)) { continue; } uf.unio(l, r); ans += 1; } ans += (N - 1 - ans) * 2; cout << "Case #" << i + 1 << ": " << ans << endl; } return0; }
classFileSystem { structNode { unordered_map<string, shared_ptr<Node>> children; int value; Node(int v) : value(v) {} }; vector<string> split(const string& path){ string tmp; vector<string> stk; stringstream ss(path); while(getline(ss,tmp,'/')) { stk.push_back(tmp); } return stk; } shared_ptr<Node> m; public: FileSystem() { m = make_shared<Node>(0); } boolcreate(string path, int value){ auto words = split(path); auto current = m; for (int i = 1; i < words.size() - 1; ++i) { constauto& word = words[i]; if (current->children.find(word) == current->children.end()) { returnfalse; } else { current = current->children[word]; } } if (current->children.find(words.back()) != current->children.end()) { returnfalse; } current->children[words.back()] = make_shared<Node>(value); returntrue; } intget(string path){ auto words = split(path); auto current = m; for (int i = 1; i < words.size(); ++i) { constauto& word = words[i]; if (current->children.find(word) == current->children.end()) { return-1; } else { current = current->children[word]; } } return current->value; } };
/** * Your FileSystem object will be instantiated and called as such: * FileSystem* obj = new FileSystem(); * bool param_1 = obj->create(path,value); * int param_2 = obj->get(path); */
classSolution { constint mod = 1e9 + 7; int f; map<pair<int, int>, int> memo; // [begin, end), the index of insert bar intbacktracking(int begin, int end, int bar){ if (memo.find({begin, bar}) != memo.end()) { return memo[{begin, bar}]; } if (bar == 0) { if (end - begin + 1 <= f) return1; else return0; } int ans = 0; for (int i = begin; i < end && i - begin + 1 <= f; ++i) { // insert in i ans += backtracking(i + 1, end, bar - 1); ans %= mod; } memo[{begin, bar}] = ans; return ans; } public: intnumRollsToTarget(int d, int f, int target){ this->f = f; int ans = backtracking(0, target - 1, d - 1); return ans; } };
1156. Swap For Longest Repeated Character Substring
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ classSolution { int l, r; // return the number of node in subtree intdfs(TreeNode* root, int x){ if (root == nullptr) return0; int left = dfs(root->left, x); int right = dfs(root->right, x); if (root->val == x) { l = left; r = right; } return left + right + 1; } public: boolbtreeGameWinningMove(TreeNode* root, int n, int x){ dfs(root, x); if (n == l + r + 1) { return l != r; } else { int parent = n - l - r - 1; return parent - 1 > l + r || l - 1 > r + parent || r - 1 > l + parent; } } };
classSolution { intpow(int x, int y){ if (y == 0) return1; else return x * pow(x, y - 1); } public: boolisArmstrong(int N){ constint number = N; vector<int> digits; while (N > 0) { digits.push_back(N % 10); N /= 10; } int s = 0; int power = digits.size(); for (auto digit : digits) { s += pow(digit, power); } return s == number; } };