12#include <unordered_set>
17 namespace license_key_formatting {
19 auto S2 = ostringstream();
20 auto output = ostringstream();
25 if(isdigit(c) != 0 || isupper(c) != 0) {
27 }
else if(islower(c) != 0) {
28 S2 << static_cast<char>(toupper(c));
32 string str = S2.str();
37 string str1 = str.substr(0, first);
38 string str2 = str.substr(first);
55 namespace distribute_candies {
57 auto m = map<long, int>();
58 auto ans = set<int>();
59 const int n =
static_cast<int>(candies.size()) / 2;
61 for(
const auto candy: candies) {
66 if(candy.second != 0) {
67 if(!ans.contains(
static_cast<int>(candy.first))) {
68 ans.insert(
static_cast<int>(candy.first));
69 if(ans.size() == m.size()) {
70 return static_cast<int>(ans.size());
81 return static_cast<int>(ans.size());
85 namespace remove_extra {
87 auto output = ostringstream();
90 for(
const char c: s) {
106 namespace character_deletion {
108 auto oss = ostringstream();
109 auto us = unordered_set<char>();
114 if(!us.contains(ch)) {
122 namespace judge_circle {
126 for(
const char ch: moves) {
143 return x == 0 && y == 0;
147 namespace intersection {
149 vector<vector<int>> res;
150 if(a.empty() || b.empty()) {
153 for(
int i = 0, j = 0; i < a.size() && j < b.size();) {
155 res.emplace_back(vector({i, j}));
157 if(a[i][1] == b[j][1]) {
160 }
else if(a[i][1] > b[j][1]) {
182 if(
root ==
nullptr) {
189 if(node->
left !=
nullptr) {
190 if(node->
right !=
nullptr) {
191 auto *tmp = node->
right;
193 auto *current = node->
right;
194 while(current->right !=
nullptr) {
195 current = current->
right;
198 node->
left =
nullptr;
201 node->
left =
nullptr;
204 if(node->
right !=
nullptr) {
214 unsigned long long prefix = index / 702 + 1;
215 if(index % 702 == 0) {
226 ch =
static_cast<char>(index % 26 + 63);
229 ch =
static_cast<char>(index % 26 + 64);
231 if(ch ==
'@' && index >= 26) {
234 }
else if(ch ==
'?' && index >= 26) {
238 if(
'A' <= ch && ch <=
'Z') {
239 ans.insert(0, 1, ch);
243 return to_string(prefix) + ans;
247 namespace min_path_sum {
249 const unsigned int m = grid.size();
250 const unsigned int n = grid[0].size();
251 auto *dp =
new int *[m + 1];
252 for(
int i = 0; i <= m; i++) {
253 dp[i] =
new int[n + 1];
254 memset(dp[i], INT_MAX, (n + 1) *
sizeof(
int));
256 dp[1][1] = grid[0][0];
257 for(
int i = 1; i <= m; i++) {
258 for(
int j = 1; j <= n; j++) {
259 if(i == 1 && j == 1) {
262 unsigned int min = dp[i - 1][j];
263 if(dp[i][j - 1] < min) {
266 dp[i][j] = grid[i - 1][j - 1] + min;
269 const auto ans = dp[m][n];
270 for(
int i = 0; i <= m; i++) {
278 namespace digit_counts {
281 for(
int i = 0; i <= n; i++) {
297 namespace min_diff_in_BST {
299 auto vals = vector<int>();
300 auto que = queue<TreeNode *>();
302 while(!que.empty()) {
303 const auto *node = que.front();
305 vals.push_back(node->val);
306 if(node->left !=
nullptr) {
307 que.push(node->left);
309 if(node->right !=
nullptr) {
310 que.push(node->right);
313 sort(vals.begin(), vals.end());
314 int minimum = INT_MAX;
315 for(
int i = 0; i + 1 < vals.size(); i++) {
316 minimum = min(vals[i + 1] - vals[i], minimum);
static string licenseKeyFormatting(string &, int)
static int distributeCandies(vector< int > &candies)
static string removeExtra(string &s)
static string CharacterDeletion(string &str, string &sub)
static bool judgeCircle(string &moves)
static vector< vector< int > > Intersection(vector< vector< int > > &a, vector< vector< int > > &b)
static bool is_intersected(const vector< int > &l, const vector< int > &r)
static TreeNode * vlr(TreeNode *)
static void flatten(TreeNode *root)
static string convert(long long index)
static int minPathSum(vector< vector< int > > &grid)
static int digitCounts(int k, int n)
static int minDiffInBST(TreeNode *root)