Compare commits

...

13 Commits

Author SHA1 Message Date
carry
fd0dc19352 完成了两道dp题 2025-03-26 11:58:33 +08:00
carry
9a570843c4 完成了动态规划题:牛客不同路径的数目 2025-03-23 14:44:12 +08:00
carry
9ec8ab41d9 完成最大子数组和(动态规划搜素,答案正确,leetcode超内存) 2025-03-23 14:22:23 +08:00
carry
cb17a48cc9 完成了暨大计算机复试的所有真题 2025-03-22 20:00:04 +08:00
carry
755923c349 cpp模版 2025-02-20 17:32:49 +08:00
carry
f6adc921e5 括号匹配 2025-02-14 13:41:13 +08:00
carry
8dc2f5e9a5 盛最多水的容器(双指针法) 2025-02-13 22:00:59 +08:00
carry
16b0c2ae64 盛最多水的容器(暴力解) 2025-02-13 21:53:34 +08:00
carry
1610db5ede 完成寻找两个正序数组的中位数 2025-02-11 22:59:01 +08:00
carry
53dc1271ec 完成每日一题:螺旋矩阵 II 2025-02-07 21:46:27 +08:00
carry
b7d0c2caf8 完成每日一题:超过阈值的最少操作数 I 2025-01-14 17:55:31 +08:00
carry
828069bf1a 完成每日一题:分割数组的方案数 2025-01-13 12:50:43 +08:00
carry
3c514a3cda 两数相加,做了一半 2025-01-13 12:41:42 +08:00
38 changed files with 1277 additions and 0 deletions

40
01背包.cpp Normal file
View File

@@ -0,0 +1,40 @@
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
int knapsack(int V, vector<vector<int>>& vw) {
sort(vw.begin(),vw.end(),[](vector<int> a,vector<int> b){
return a[0] < b[0];
});
vector<vector<int>> dp(vw.size() + 1,vector<int>(V + 1,0));
for(int i = 1 ;i <= vw.size();i++){
for(int j = 1;j < vw[i-1][0];j++){
dp[i][j] = dp[i-1][j];
//cout << dp[i][j] << " ";
}
//cout << "change";
for(int j = vw[i-1][0];j <= V;j++){
dp[i][j] = max(dp[i-1][j],vw[i-1][1] + dp[i-1][j - vw[i-1][0]]);
cout << dp[i][j] << ":" << j << " ";
}
//cout << endl;
}
return dp[vw.size()][V];
}
};
int main(){
Solution mysolution;
vector<vector<int>> array = {{1,3},{10,4}};
cout << mysolution.knapsack(10,array);
return 0;
}

32
2014冒泡排序.cpp Normal file
View File

@@ -0,0 +1,32 @@
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main(){
int n,temp;
cin >> n;
vector<int> array;
for (int i = 0; i < n; i++) {
cin >> temp;
array.push_back(temp);
}
for (int i = 0; i < n; i++) {
for(int j = 0;j < n - 1;j++){
if(array[j] > array[j + 1]){
temp = array[j + 1];
array[j + 1] = array[j];
array[j] = temp;
}
}
}
for(auto i: array){
cout << i << " ";
}
return 0;
}

28
2014矩形覆盖.cpp Normal file
View File

@@ -0,0 +1,28 @@
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main(){
int m,n,a;
cin >> m >> n >> a;
if(m%a == 0){
m = m/a;
}
else{
m = m/a + 1;
}
if(n%a == 0){
n = n/a;
}
else{
n = n/a + 1;
}
cout << m*n;
return 0;
}

31
2014砍树修路.cpp Normal file
View File

@@ -0,0 +1,31 @@
#include<iostream>
#include<vector>
using namespace std;
int main(){
int L;
cin >> L;
int n;
cin >> n;
int a,b;
vector<int> route(L + 1,1);
for(int i = 0;i < n;i++){
cin >> a >> b;
for(int j = a;j <= b;j++){
route[j] = 0;
}
}
int counter = 0;
for(auto i:route){
counter += i;
}
cout << counter;
return 0;
}

28
2015元音处理.cpp Normal file
View File

@@ -0,0 +1,28 @@
#include<iostream>
#include<string>
#include<stdio.h>
using namespace std;
int main(){
string str;
cin >> str;
for(auto i:str){
switch(i){
case 'a':case 'o':case 'e':case 'i':case 'u':
case 'A':case 'O':case 'E':case 'I':case 'U':
break;
default:
cout << '.';
if(i < 92){
printf("%c",i+32);
}
else{
printf("%c",i-32);
}
}
}
return 0;
}

22
2015高精度乘法.cpp Normal file
View File

@@ -0,0 +1,22 @@
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main(){
string str;
vector<int> num1,num2;
cin >> str;
for(auto i:str){
num1.push_back(stoi(&i));
}
cin >> str;
for(auto i:str){
num2.push_back(stoi(&i));
}
return 0;
}

28
2016字符统计.cpp Normal file
View File

@@ -0,0 +1,28 @@
#include<iostream>
#include<string>
using namespace std;
int main(){
string str;
int counter_at = 0;
int counter_endl = 1;
while(1){
cin >> str;
for(auto i = str.begin()+1;i != str.end();i++){
if((*(i-1) == 'a')&&(*i == 'a')){
counter_at++;
}
}
if(*(str.end()-1) == '.'){
break;
}
else{
counter_endl++;
}
}
cout << counter_at << endl << counter_endl << endl;
return 0;
}

31
2016猜数字.cpp Normal file
View File

@@ -0,0 +1,31 @@
#include<iostream>
#include<string>
using namespace std;
int main(){
int max,target;
cin >> max >> target;
int min = 1;
int guest = -1;
int counter = 0;
while(1){
counter++;
guest = (max - min + 1)/2 + min;
//cout << counter << " " << guest << " "<< max << "~" << min << endl;
if(guest > target){
max = guest;
}
else if(guest < target){
min = guest;
}
else{
break;
}
}
cout << counter;
return 0;
}

32
2017成绩排名.cpp Normal file
View File

@@ -0,0 +1,32 @@
#include<iostream>
#include<set>
#include<vector>
using namespace std;
int main(){
int n,temp;
vector<int> array;
set<int,greater<int>> score;
cin >> n;
for(int i = 0;i < n;i++){
cin >> temp;
score.insert(temp);
}
cin >> n;
temp = 1;
for(auto i = score.begin();i != score.end();i++){
if(*i != n){
temp++;
}
else{
cout << temp;
break;
}
}
return 0;
}

31
2017数塔路径.cpp Normal file
View File

@@ -0,0 +1,31 @@
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
int n;
cin >> n;
vector<vector<int>> tree(n);
int temp;
for(int i = 0;i<n;i++){
for(int j = 0;j < i+1;j++){
cin >> temp;
tree[i].push_back(temp);
}
}
vector<vector<int>> dp = tree;
for(int i = n - 2;i >= 0;i--){
for(int j = i;j >= 0;j--){
dp[i][j] = max(dp[i + 1][j],dp[i + 1][j + 1]) + tree[i][j];
}
}
cout << dp[0][0];
return 0;
}

42
2017计算第几天.cpp Normal file
View File

@@ -0,0 +1,42 @@
#include<iostream>
using namespace std;
int is_runnian(int y){
return (y%4 == 0)&&(y%2000 != 0);
}
int main(){
int counter;
int y,m,d;
cin >> y >> m >> d;
if(m <= 2){
cout << (m - 1)*31 + d;
return 0;
}
else if(m <= 7){
counter = 31 + 28 + is_runnian(y) + d;
for(int i = 3;i<m;i++){
if(i %2 == 0){
counter += 30;
}
else{
counter += 31;
}
}
}
else{
counter = 212 + is_runnian(y) + d;
for(int i = 8;i<m;i++){
if(i %2 == 0){
counter += 31;
}
else{
counter += 30;
}
}
}
cout << counter;
return 0;
}

25
2018偶数分解.cpp Normal file
View File

@@ -0,0 +1,25 @@
#include<iostream>
using namespace std;
int is_sushu(int n){
for(int i = 2;i*i<n;i++){
if(n%i == 0){
return 0;
}
}
return 1;
}
int main(){
int n;
cin >> n;
for(int i = 2;i<=n/2;i++){
if(is_sushu(i)&&is_sushu(n-i)){
cout << i << " " << n - i << endl;
}
}
return 0;
}

View File

@@ -0,0 +1,22 @@
#include<iostream>
using namespace std;
int main(){
int n;
cin >> n;
int a=0,b=1;
if((n == 1)||(n == 0)){
cout << n;
return 0;
}
for(int i = 1;i<n;i++){
a = a + b;
swap(a,b);
}
cout << b;
return 0;
}

View File

@@ -0,0 +1,41 @@
#include<iostream>
#include<vector>
using namespace std;
int sum(vector<int> array,int start,int end){
int num = 0;
for(int i = start;i <=end ;i++){
num += array[i];
}
return num;
}
int main(){
int n;
cin >> n;
vector<int> array(n);
for(auto &i:array){
cin >> i;
}
int max = 0;
int start = 0;
int end = 0;
for(int i = 0;i < n;i++){
for (int j = 0;j < n - i;j++){
if(max<sum(array,j,j+i)){
max = sum(array,j,j+i);
start = j;
end = j+i;
}
}
}
cout << max << " " << start+1 << " " << end+1 << endl;
return 0;
}

23
2018蛇形矩阵.cpp Normal file
View File

@@ -0,0 +1,23 @@
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for(int i = 1;i<=n;i++){
if(i%2 != 0){
for(int j=(i-1)*n+1;j<=i*n;j++){
cout << j << " ";
}
}
else{
for(int j=i*n;(i-1)*n+1<=j;j--){
cout << j << " ";
}
}
cout << endl;
}
return 0;
}

26
2019判断回文串.cpp Normal file
View File

@@ -0,0 +1,26 @@
#include<iostream>
#include<string>
using namespace std;
int main(){
string str;
cin >> str;
auto i = str.begin();
auto j = str.end() - 1;
while(i<j){
if(*i != *j){
cout << "No!" << endl;
return 0;
}
else{
i++;
j--;
}
}
cout << "Yes!" << endl;
return 0;
}

23
2019格子涂色.cpp Normal file
View File

@@ -0,0 +1,23 @@
#include<iostream>
using namespace std;
int func(int n){
if(n == 1){
return 0;
}
else if(n == 2){
return 6;
}
else{
return func(n - 1) + func(n - 2)*2;
}
}
int main(){
int n;
cin >> n;
cout << func(n);
return 114514;
}

View File

@@ -0,0 +1,38 @@
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
int n;
cin >> n;
vector<int> array(n);
for(auto &i:array){
cin >> i;
}
int max_sum = -1;
vector<int> dp(n);
dp[0] = array[0];
for(int i = 1;i < array.size();i++){
dp[i] = array[i];
for(int j = 0;j < i;j++){
if(array[j] < array[i]){
max_sum = max(dp[j],max_sum);
}
}
dp[i] += max_sum;
max_sum = -1;
}
for(auto i:dp){
max_sum = max(i,max_sum);
}
cout << max_sum;
return 0;
}

18
2019递归求阶乘.cpp Normal file
View File

@@ -0,0 +1,18 @@
#include<iostream>
using namespace std;
int func(int n){
if(n == 1){
return n;
}
else{
return n*func(n-1);
}
}
int main(){
int n;
cin >> n;
cout << func(n);
}

29
2023成绩排名.cpp Normal file
View File

@@ -0,0 +1,29 @@
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
vector<int> a;
int main() {
int n;
cin >> n;
vector<int> list(n);
for (auto i = list.begin(); i != list.end(); i++) {
cin >> *i;
}
unique(list.begin(),list.end());
sort(list.begin(), list.end(),[](int n1,int n2){
return n1>n2;
});
cin >> n;
cout << list[n - 1];
return 0;
}

29
2023括号匹配.cpp Normal file
View File

@@ -0,0 +1,29 @@
#include<iostream>
#include<stack>
#include<string>
using namespace std;
int main(){
stack<char> s;
string str;
cin >> str;
for(auto i = str.begin();i != str.end();i++){
switch(*i){
case '(':s.push(')');break;
case '{':s.push('}');break;
case '[':s.push(']');break;
default:
if((*i == '}')||(*i == ']')||(*i == ')')){
if(s.top() != *i){
cout << "no" << endl;
return 1;
}
s.pop();
}
}
}
cout << "yes" << endl;
return 0;
}

24
2023斐波那契数.cpp Normal file
View File

@@ -0,0 +1,24 @@
#include<iostream>
#include<vector>
using namespace std;
int fbnqs(int n1,int n2,int deepth){
if(deepth == 1){
return n1 + n2;
}
else{
return fbnqs(n2,n1+n2,deepth -1);
}
}
int main(){
int n;
cin >> n;
cout << fbnqs(1,1,n - 2);
return 0;
}

18
2023求个位数.cpp Normal file
View File

@@ -0,0 +1,18 @@
#include<iostream>
using namespace std;
int main(){
int n = 0;
cin >> n;
int num = n;
for(int i =0;i < n - 1;i++){
num = num * n;
num = num%10;
}
cout << num;
return 0;
}

25
2024二元素数组.cpp Normal file
View File

@@ -0,0 +1,25 @@
#include<iostream>
using namespace std;
bool is_sushu(int n){
for(int i = 2;i*i<=n;i++){
if(n%i == 0){
return false;
}
}
return true;
}
int main(){
int n;
cin >> n;
for(int i = 0;i < n/2;i++){
if(is_sushu(i) && is_sushu(n - i)){
cout << i <<" "<< n-i << endl;
}
}
return 0;
}

33
2024体重排序.cpp Normal file
View File

@@ -0,0 +1,33 @@
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
int n;
pair<string,float> person;
cin >> n;
vector<pair<string,float> > list;
for(int i = 0;i < n;i++){
cin >> person.first >> person.second;
list.push_back(person);
}
sort(list.begin(),list.end(),
[](pair<string,float> p1,pair<string,float> p2){
return (p1.second < p2.second);
});
for (const auto& p : list) {
cout << p.first << " ";
}
return 0;
}

36
2024删除区间.cpp Normal file
View File

@@ -0,0 +1,36 @@
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
int n;
cin >> n;
vector<pair<int,int>> array(n);
for(auto &i:array){
cin >> i.first >> i.second;
}
sort(array.begin(),array.end(),[](pair<int,int> a,pair<int,int> b){
return a.second < b.second;
});
int last_end = -114514;
int counter = 0;
for(auto i:array){
if(last_end < i.first){
last_end = i.second;
}
else{
counter++;
}
}
cout << counter;
return 0;
}

View File

@@ -0,0 +1,28 @@
#include<iostream>
#include<vector>
using namespace std;
int main(){
int n;
int count = 0;
cin >> n;
vector<int> list(n);
for(int i = 0;i < n;i++){
cin >> list[i];
}
for(int i = 0;i < n;i++){
for(int j = i;j < n;j++){
if(list[i]>list[j]){
count++;
}
}
}
cout << count;
return 0;
}

28
PrintMinNumber.cpp Normal file
View File

@@ -0,0 +1,28 @@
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <algorithm>
using namespace std;
class Solution {
public:
string PrintMinNumber(vector<int>& numbers) {
sort(numbers.begin(),numbers.end(),[](int a,int b){
return stoi(to_string(a) + to_string(b)) < stoi(to_string(b) + to_string(a));
});
string result = "";
for(auto &i:numbers){
result += to_string(i);
}
return result;
}
};
int main(){
vector<int> nums = {3,33,3334,33332};
Solution solution;
cout << solution.PrintMinNumber(nums) << endl;
return 0;
}

75
addTwoNumbers.cpp Normal file
View File

@@ -0,0 +1,75 @@
#include <iostream>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
class Solution
{
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2)
{
ListNode *l3 = new ListNode();
ListNode *begin = l3;
int carry = 0;
while (1) {
if (l1 == nullptr && l2 == nullptr) {
return begin;
} else if (l1 == nullptr) {
l3->val = l2->val + carry;
l2 = l2->next;
if (l3->val > 10) {
carry = 1;
l3->val -= 10;
} else {
carry = 0;
}
if (l2->next == nullptr) {
return begin;
} else {
l3->next = new ListNode();
l3 = l3->next;
}
} else if (l2 == nullptr) {
l3->val = l1->val + carry;
l1 = l1->next;
if (l3->val > 10) {
carry = 1;
l3->val -= 10;
} else {
carry = 0;
}
if (l1->next == nullptr) {
return begin;
} else {
l3->next = new ListNode();
l3 = l3->next;
}
} else {
l3->val = l1->val + l2->val + carry;
if (l3->val >= 10) {
carry = 1;
l3->val -= 10;
} else {
carry = 0;
}
l1 = l1->next;
l2 = l2->next;
l3->next = new ListNode();
l3 = l3->next;
}
}
}
};
int main()
{
}

View File

@@ -0,0 +1,87 @@
#include<iostream>
#include<vector>
#include<math.h>
using namespace std;
class Solution
{
public:
double findMedianSortedArrays(vector<int> &nums1,vector<int> &nums2)
{
int lenth = nums1.size() + nums2.size();
if(nums1.size() == 0){
nums1.push_back(pow(10,6) + 1);
}
if(nums2.size() == 0){
nums2.push_back(pow(10,6) + 1);
}
int a = 0, b = 0;
if (lenth % 2 == 1) {
lenth = lenth / 2;
for (int i = 0; i < lenth; i++) {
if (nums1[a] < nums2[b]) {
if(a == nums1.size() -1){
nums1[a] = pow(10,6) + 1;
}
else{
a++;
}
} else {
if(b == nums2.size() -1){
nums2[b] = pow(10,6) + 1;
}
else{
b++;
}
}
}
return nums1[a] < nums2[b] ? nums1[a] : nums2[b];
} else {
int n[2] = {0,0};
int sw = 0;
lenth = lenth / 2 + 1;
for (int i = 0; i < lenth; i++) {
if (nums1[a] < nums2[b]) {
n[sw] = nums1[a];
sw = !sw;
if(a == nums1.size() -1){
nums1[a] = pow(10,6) + 1;
}
else{
a++;
}
} else {
n[sw] = nums2[b];
sw = !sw;
if(b == nums2.size() -1){
nums2[b] = pow(10,6) + 1;
}
else{
b++;
}
}
}
return (double(n[0]) + double(n[1]))/2;
}
}
};
int main() {
// 示例测试用例
vector<int> nums1 = {1, 3};
vector<int> nums2 = {2};
Solution solution;
double median = solution.findMedianSortedArrays(nums1, nums2);
cout << "中位数是: " << median << endl;
// 更多测试用例
vector<int> nums3 = {100000};
vector<int> nums4 = {100001};
double median2 = solution.findMedianSortedArrays(nums3, nums4);
cout << "中位数是: " << median2 << endl;
return 0;
}

81
generateMatrix.cpp Normal file
View File

@@ -0,0 +1,81 @@
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
int x = 0;
int y = 0;
int pos = 0;
int n = 0;
vector<vector<int>> result;
void go(){
switch(pos){
case 0:
x++;
if(x==n || result[y][x] != 0){
x--;
pos++;
}
else{
break;
}
case 1:
y++;
if(y==n || result[y][x] != 0){
y--;
pos++;
}
else{
break;
}
case 2:
x--;
if(x==-1 || result[y][x] != 0){
x++;
pos++;
}
else{
break;
}
case 3:
y--;
if(y==-1 || result[y][x] != 0){
y++;
pos=0;
x++;
}
else{
break;
}
}
}
vector<vector<int>> generateMatrix(int n) {
this->n = n;
int num = 1;
this->result = vector<vector<int>>(n,vector<int>(n));
this->result[0][0] = num;
while (true)
{
cout << x << "," << y << "," << pos << endl;
num++;
this->go();
if(this->result[y][x] != 0){
break;
}
this->result[y][x] = num;
}
return this->result;
}
};
int main(){
Solution s;
vector<vector<int>> output = s.generateMatrix(1);
cout << output[0][0];
return 0;
}

33
maxArea.cpp Normal file
View File

@@ -0,0 +1,33 @@
#include <iostream>
#include <vector>
using namespace std;
class Solution
{
public:
int maxArea(vector<int> &height)
{
int maxArea = 0;
int left = 0;
int right = height.size() - 1;
while (left < right){
maxArea = max(maxArea,(right - left)*min(height[right] , height[left]));
if( height[left] < height[right]){
left++;
}
else{
right--;
}
}
return maxArea;
}
};
int main()
{
vector<int> height = {1, 8, 6, 2, 5, 4, 8, 3, 7};
Solution s;
cout << s.maxArea(height);
return 0;
}

35
maxSubArray.cpp Normal file
View File

@@ -0,0 +1,35 @@
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
int maxSubArray(vector<int>& nums) {
if(nums.size()==1){
return nums[0];
}
else{
int max_num = *max_element(nums.begin(),nums.end());
vector<vector<int>> dp;
dp.push_back(nums);
for(int i = 1;i < nums.size();i++){
dp.push_back(vector<int>(dp[0].size()-1));
for(int j = 0;j < nums.size() - i;j++){
dp[1][j] = dp[0][j] + nums[j + i];
max_num = max(max_num,dp[1][j]);
}
dp.erase(dp.begin());
}
return max_num;
}
}
};
int main(){
Solution mysolution;
vector<int> array = {-2,1,-3,4,-1,2,1,-5,4};
cout << mysolution.maxSubArray(array);
return 0;
}

25
minOperations.cpp Normal file
View File

@@ -0,0 +1,25 @@
#include <vector>
#include <iostream>
using namespace std;
class Solution {
public:
int minOperations(vector<int>& nums, int k) {
int counter = 0;
for(auto i:nums){
if(i < k){
counter++;
}
}
return counter;
}
};
int main() {
Solution solution;
vector<int> nums = {2,11,10,1,3};
int k = 10;
cout << solution.minOperations(nums, k) << endl;
return 0;
}

12
template.cpp Normal file
View File

@@ -0,0 +1,12 @@
#include<iostream>
#include<vector>
class Solution {
public:
};
int main(){
return 0;
}

35
uniquePaths.cpp Normal file
View File

@@ -0,0 +1,35 @@
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
int uniquePaths(int m, int n) {
if((m == 1)||(n == 1)){
return 1;
}
else{
vector<vector<int>> dp(m, std::vector<int>(n,0));
for(int i = 0;i < m;i++){
dp[i][0] = 1;
}
for(int i = 0;i < n;i++){
dp[0][i] = 1;
}
for(int i = 1;i < m;i++){
for(int j = 1;j < n;j++){
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
return dp[m - 1][n - 1];
}
}
};
int main(){
Solution mysolution;
cout << mysolution.uniquePaths(2,1);
return 0;
}

50
validParentheses.cpp Normal file
View File

@@ -0,0 +1,50 @@
#include<iostream>
#include<stack>
using namespace std;
class Solution {
public:
bool isValid(string s) {
stack<char> str;
for(auto i: s){
switch(i){
case '(':
case '{':
case '[':
str.push(i);
break;
default:
if(str.size() == 0){
return false;
}
switch(i){
case ')':
if (str.top() != '('){return false;}
else{str.pop();}
break;
case ']':
if (str.top() != '['){return false;}
else{str.pop();}
break;
case '}':
if (str.top() != '{'){return false;}
else{str.pop();}
break;
}
}
}
if(str.size() == 0){
return true;
}
else{
return false;
}
}
};
int main(){
cout << Solution().isValid("([])");
return 0;
}

33
waysToSplitArray.cpp Normal file
View File

@@ -0,0 +1,33 @@
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int waysToSplitArray(vector<int>& nums) {
long long sum1 = 0;
int lens = nums.size();
for(int i = 0; i < lens;i++){
sum1 += nums[i];
}
long long sum2 = 0;
int counter = 0;
lens--;
for(int i = 0; i < lens;i++){
sum2 += nums[i];
sum1 -= nums[i];
if(sum2 >= sum1){
counter++;
}
}
return counter;
}
};
int main(){
vector<int> nums = {10,4,-8,7};
Solution solution;
cout << solution.waysToSplitArray(nums) << endl;
return 0;
}