-
[codility] TapeEquilibrium c++Coding Test/codility 2022. 4. 9. 19:06728x90
- 문제
|(A[1] + ... + A[P-1] ) - (A[P] + ... +A[N])| 을 구하는 문제이다.
즉, A배열의 0~(P-1)번 까지 합 - 나머지 원소의 합의 절댓값을 구한다.
P 값에 따른 절댓값 중 최솟값을 구하는 문제이다.
P의 범위 : (0 < P < N)
- 문제 해결
#include <string> #include <algorithm> #include <vector> #include <numeric> #include <limits> int solution2(std::vector<int> &A) { long end = std::accumulate(A.begin(), A.end(), 0); long len = A.size(); long front =0; int minimum = INT8_MAX; for (int i = 0; i < len - 1; i++) { front += A.at(i); end -= A.at(i); int sumP = std::abs(front - end); minimum = std::min(minimum, sumP); } return minimum; } #define TapeEquilibrium #ifdef TapeEquilibrium int main() { std::vector<int> nVec = { 3,1,2,4,3 }; std::cout << solution2(nVec); return 0; } #endif
728x90'Coding Test > codility' 카테고리의 다른 글
[codility] PermCheck c++ (0) 2022.04.09 [codility] FrogRiverOne c++ (0) 2022.04.09 [codility] PermMissingElem c++ (0) 2022.04.09 [codility] FrogJmp c++ (0) 2022.04.08 [codility] OddOccurrencesInArray c++ (0) 2022.04.08