TapeEquilibrium
- 初始化
head = A[0]
和tail = accumulate(A.begin() + 1, A.end(), 0)
- 再用一個迴圈加一個元素、減一個元素,比
minDiff
的大小
#include <bits/stdc++.h>
using namespace std;
int solution(vector<int> &A)
{
int n = A.size();
int head = A[0];
int tail = accumulate(A.begin() + 1, A.end(), 0);
int minDiff = abs(head - tail);
for (int i = 1; i < n - 1; ++i)
{
head += A[i];
tail -= A[i];
int cur = abs(head - tail);
minDiff = min(minDiff, cur);
}
return minDiff;
}
- T:
- S: