diff --git "a/topic02/submit/LC0222_\345\274\240\344\270\226\344\273\273.cpp" "b/topic02/submit/LC0222_\345\274\240\344\270\226\344\273\273.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..199090a81445884add284ecb6f7189adb002ee53 --- /dev/null +++ "b/topic02/submit/LC0222_\345\274\240\344\270\226\344\273\273.cpp" @@ -0,0 +1,30 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +#include +using namespace std; +class Solution { +public: +void iscomplete(TreeNode* node,int &count){ + if(!node){return;} + if(node->left||(!node->left&&!node->right)){ + count++; + } + if(node->left){iscomplete(node->left,count);} + if(node->right){iscomplete(node->right,count);} +} + int countNodes(TreeNode* root) { + if(!root){return 0;} + int count=0; + iscomplete(root,count); + return count; + } +}; \ No newline at end of file diff --git "a/topic02/submit/LC1552_\345\274\240\344\270\226\344\273\2731.cpp" "b/topic02/submit/LC1552_\345\274\240\344\270\226\344\273\2731.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..3ae286f05dbb7f39353fef9d3cf8cd74bba1dcc3 --- /dev/null +++ "b/topic02/submit/LC1552_\345\274\240\344\270\226\344\273\2731.cpp" @@ -0,0 +1,55 @@ +#include +#include +#include +#include // 包含INT_MAX的定义 +using namespace std; + +class Solution { +public: + // 验证函数:判断能否以至少x的间距放置m个球 + bool check(int x, vector& a, int m) { + int cnt = 0; // 记录已形成的有效间隔数(m个球需要m-1个间隔) + int target = a[0] + x; // 第一个球放在a[0],下一个球的最小位置 + for (int i = 0; i < a.size() - 1; i++) { + // 找到第一个>=target的位置,放置下一个球 + if (a[i] < target && a[i + 1] >= target) { + cnt++; + target = a[i + 1] + x; // 更新下一个球的最小位置 + } + } + return cnt >= m - 1; // 间隔数足够则返回true + } + + int maxDistance(vector& position, int m) { + vector& a = position; // 统一变量名,避免混淆 + sort(a.begin(), a.end()); // 排序位置数组(必须先排序) + int len = a.size(); + int maxd = a[len - 1] - a[0]; // 最大可能间距(首尾差) + int mind = INT_MAX; // 最小可能间距(相邻元素的最小差) + + // 计算相邻元素的最小间距 + for (int i = 0; i < len - 1; i++) { + mind = min(mind, a[i + 1] - a[i]); + } + + // 特判:m=2时,最大间距就是首尾差 + if (m == 2) { + return maxd; + } else { + // 二分查找:左边界为最小间距,右边界为理论最大可能的最小间距 + int l = mind; + int r = maxd / (m - 1); + while (l <= r) { + int mid = (l + r) / 2; // 当前猜测的最小间距 + if (check(mid, a, m)) { + // 能放下,尝试更大的间距 + l = mid + 1; + } else { + // 放不下,尝试更小的间距 + r = mid - 1; + } + } + return l - 1; // 退出时l-1是最大有效间距 + } + } +}; \ No newline at end of file diff --git "a/topic03/submit/LC0053_\345\274\240\344\270\226\344\273\273.cpp" "b/topic03/submit/LC0053_\345\274\240\344\270\226\344\273\273.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..1d45686c1f50f393b5b08fbf8acf91442b10306d --- /dev/null +++ "b/topic03/submit/LC0053_\345\274\240\344\270\226\344\273\273.cpp" @@ -0,0 +1,14 @@ +class Solution { +public: + int maxSubArray(vector& nums) { + int ans=INT_MIN; + int min_presum=0; + int presum=0; + for(int x:nums){ + presum+=x; + ans=max(ans,presum-min_presum); + min_presum=min(min_presum,presum); + } + return ans; + } +}; \ No newline at end of file diff --git "a/topic03/submit/LC1685_\345\274\240\344\270\226\344\273\273.cpp" "b/topic03/submit/LC1685_\345\274\240\344\270\226\344\273\273.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..c72cbd33e00a8e239261122400faff8ac7fa984a --- /dev/null +++ "b/topic03/submit/LC1685_\345\274\240\344\270\226\344\273\273.cpp" @@ -0,0 +1,16 @@ +class Solution { +public: + vector getSumAbsoluteDifferences(vector& nums) { + int n=nums.size(); + int sum=0; + for(auto m:nums){ + sum+=m; + } + int presum=0; + for(int i=0;i