Rylah's Study & Daily Life
03. LeetCode 724. Find Pivot Index 본문
LeetCode 724. Find Pivot Index
https://leetcode.com/problems/move-zeroes/
Move Zeroes - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
// 724. Find Pivot Index
// Sliding Basic O(n) + O(n) => O(n)
// LeetCode 724. Find Pivot Index
// https://leetcode.com/problems/find-pivot-index/
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int pivotIndex(vector<int> &nums)
{
int sum = accumulate(nums.begin(), nums.end(), 0);
int leftSum = 0;
int rightSum = sum;
int pastPivotNum = 0; // Previous Pivot Num
for (int i = 0; i < (int)nums.size(); i++)
{
int num = nums[i];
rightSum = rightSum - num;
leftSum = leftSum + pastPivotNum;
if (leftSum == rightSum)
return i;
pastPivotNum = num;
}
return -1;
}
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
vector<int> nums = { 1, 7, 3, 6, 5, 6 };
cout << pivotIndex(nums) << '\n';
return 0;
}
원리는 다음과 같다.
'LeetCode' 카테고리의 다른 글
02. LeetCode 283. Move Zeroes (0) | 2022.01.11 |
---|---|
01. LeetCode 704 - Binary Search (0) | 2022.01.11 |