This commit is contained in:
2025-09-15 21:12:04 +08:00
commit 3f58f483ff
144 changed files with 5298 additions and 0 deletions

57
23/05/1373.c Normal file
View File

@@ -0,0 +1,57 @@
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
const int INF = 0x3f3f3f3f;
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};
typedef struct SubTree {
bool isBST;
int minValue;
int maxValue;
int sumValue;
} SubTree;
SubTree *createSubTree(bool isBST, int minValue,int maxValue, int sumValue) {
SubTree *obj = (SubTree *)malloc(sizeof(SubTree));
obj->isBST = isBST;
obj->minValue = minValue;
obj->maxValue = maxValue;
obj->sumValue = sumValue;
return obj;
}
SubTree* dfs(struct TreeNode* root, int *res) {
if (root == NULL) {
return createSubTree(true, INF, -INF, 0);
}
SubTree *left = dfs(root->left, res);
SubTree *right = dfs(root->right, res);
SubTree *ret = NULL;
if (left->isBST && right->isBST &&
root->val > left->maxValue &&
root->val < right->minValue) {
int sum = root->val + left->sumValue + right->sumValue;
*res = MAX(*res, sum);
ret = createSubTree(true, MIN(left->minValue, root->val), \
MAX(root->val, right->maxValue), sum);
} else {
ret = createSubTree(false, 0, 0, 0);
}
free(left);
free(right);
return ret;
}
int maxSumBST(struct TreeNode* root){
int res = 0;
SubTree *obj = dfs(root, &res);
free(obj);
return res;
}

26
23/05/33.c Normal file
View File

@@ -0,0 +1,26 @@
static int max(int a, int b) {
return a > b ? a : b;
}
static int min(int a, int b) {
return a < b ? a : b;
}
int storeWater(int* bucket, int bucketSize, int* vat, int vatSize) {
int maxk = 0;
for (int i = 0; i < vatSize; i++) {
maxk = max(maxk, vat[i]);
}
if (maxk == 0) {
return 0;
}
int res = INT_MAX;
for (int k = 1; k <= maxk && k < res; ++k) {
int t = 0;
for (int i = 0; i < bucketSize; ++i) {
t += max(0, (vat[i] + k - 1) / k - bucket[i]);
}
res = min(res, t + k);
}
return res;
}