From 828069bf1afa4ff5c50b00456eff9c6fa7085aef Mon Sep 17 00:00:00 2001 From: carry Date: Mon, 13 Jan 2025 12:50:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E6=AF=8F=E6=97=A5=E4=B8=80?= =?UTF-8?q?=E9=A2=98=EF=BC=9A=E5=88=86=E5=89=B2=E6=95=B0=E7=BB=84=E7=9A=84?= =?UTF-8?q?=E6=96=B9=E6=A1=88=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- waysToSplitArray.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 waysToSplitArray.cpp diff --git a/waysToSplitArray.cpp b/waysToSplitArray.cpp new file mode 100644 index 0000000..a088f7e --- /dev/null +++ b/waysToSplitArray.cpp @@ -0,0 +1,33 @@ +#include +#include +using namespace std; + +class Solution { +public: + int waysToSplitArray(vector& 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 nums = {10,4,-8,7}; + Solution solution; + cout << solution.waysToSplitArray(nums) << endl; + return 0; +} \ No newline at end of file