LeetCode #724 FindPivotIndex. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접,연결리스트
Runtime: 28 ms, faster than 80.93% of C++ online submissions for Find Pivot Index.
Memory Usage: 9.9 MB, less than 67.87% of C++ online submissions for Find Pivot Index.
LeetCode #705
Q.
Given an array of integers nums, write a method that returns the "pivot" index of this array.
We define the pivot index as the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to the right of the index.
If no such index exists, we should return -1. If there are multiple pivot indexes, you should return the left-most pivot index.
주어진 정수 배열에서, 이 배열의 pivot 인덱스를 찾아서 반환하는 함수를 만들어라
우리는 pivot 인덱스를, 좌측과 우측의 합이 같은 인덱스라고 정의한다.
맞는 인덱스가 없으면 -1 을 리턴한다. 피벗인덱스가 여러개면, 가장 왼쪽 인덱스를 반환한다.
Example 1:
Input: nums = [1, 7, 3, 6, 5, 6]
Output: 3
Explanation: The sum of the numbers to the left of index 3 (nums[3] = 6) is equal to the sum of numbers to the right of index 3. Also, 3 is the first index where this occurs.
Example 2:
Input: nums = [1, 2, 3]
Output: -1
Explanation: There is no index that satisfies the conditions in the problem statement.
Note:
- The length of nums will be in the range [0, 10000].
- Each element nums[i] will be an integer in the range [-1000, 1000].
Process
// Process
//1. Input
// 처리과정
//1.
Code.. lemme see example code!!!
코드.. 예제코드를 보자!!!
// high performance
class Solution {
public:
int pivotIndex(vector<int>& nums) {
int pivotIndex = -1;
int frontSum = 0;
float totalSum = 0;
for (int val : nums) {
totalSum+=val;
}
int sum = 0;
for (int i = 0; pivotIndex == -1 && i < nums.size(); ++i) {
sum += nums[i];
if (sum - nums[i] == (totalSum - nums[i]) / 2) {
pivotIndex = i;
}
}
return pivotIndex;
}
};
// basic concept
class Solution {
public:
int pivotIndex(vector<int>& nums) {
int pivotIndex = -1;
int frontSum;
int rearSum;
for (int i = 0; pivotIndex == -1 && i < nums.size(); ++i) {
frontSum = 0;
rearSum = 0;
for (int j = i - 1; j >= 0; --j) {
frontSum += nums[j];
}
for (int j = i + 1; j < nums.size(); ++j) {
rearSum += nums[j];
}
if (frontSum == rearSum)
pivotIndex = i;
}
return pivotIndex;
}
};
Something else you might like...?
2019/04/28 - [Algorithm/Leet Code] - LeetCode #121 BestTimeToBuyAndSellStock. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접
2019/04/24 - [Algorithm/Leet Code] - LeetCode #119 Pascal'sTriangle2. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접, 파스칼..
2019/04/24 - [Algorithm/Leet Code] - LeetCode #104 MaximumDepthOfBinaryTree. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접
2019/04/22 - [Algorithm/Leet Code] - LeetCode #118 Pascal'sTriangle. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접
2019/04/17 - [Algorithm/Leet Code] - LeetCode #88 MergeSortedAray. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접
2019/04/17 - [Algorithm/Leet Code] - LeetCode #021 MergeTwoSortedLists. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접
2019/04/14 - [Programming/C++] - C++ Math - sqrt (square root, 제곱근, 루트). stl, math.h, 씨쁠쁠, example code, 예제코드
2018/10/19 - [Programming/Design Pattern ] - Design pattern - Prototype (디자인패턴 - 프로토타입) / Java C++ C#
2019/01/12 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #60 sudoku. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive
2019/01/12 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #59 spiralNumbers. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive
2019/01/08 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #58 messageFromBinaryCode. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive
2019/01/07 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #57 fileNaming. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive
2019/01/04 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #56 digitsProduct. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제,문제해결능력,example, c++ java c# scalar
2019/01/04 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #55 differentSquares. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제,문제해결능력,example, c++ java c# scalar
2018/12/28 - [Programming/Software Architecture] - Perfecting OO's Small Classes and Short Methods. 완벽한 객체지향의 작은 클래스와 짧은 메소드, Book:ThoughtWorks Anthology, Java,cpp,자바,oop,좋은코드,객체지향프로그래밍 - (#9, Tell, Don't Ask)
2018/12/26 - [Programming/Software Architecture] - Perfecting OO's Small Classes and Short Methods. 완벽한 객체지향의 작은 클래스와 짧은 메소드, Book:ThoughtWorks Anthology, Java,cpp,자바,oop,좋은코드,객체지향프로그래밍 (1)
2019/01/14 - [Programming/Java] - 자바 메모리 누수 체크/확인/고치는 방법, Memory leak check/fix in Java application, cleanCode/좋은코드/oop/객체지향
2019/02/19 - [Life/Health care] - Lysine 라이신 usage/side effects/dosage 효과/효능/부작용/성인,소아 용법, 복용법
2019/02/16 - [Life/Health care] - Finasteride 피나스테라이드,탈모약 usage/side effects/dosage 효능/부작용/효과/sexual effect/두타스테라이드/프로페시아/propecia/finpecia/카피약/copy drug/hair loss
2019/02/25 - [Life/Health care] - Folic Acid 엽산 vitaminB9,비타민M usage/side effects/dosage 효과/효능/부작용/성인,소아 용법, 복용법
2019/02/28 - [Life/Health care] - Vitamin K, 비타민 K usage/side effects/dosage 효능/부작용/성인,소아 용법
2019/03/03 - [Life/Health care] - Vitamin B1, Thiamine, 비타민 B1, 티아민 usage/side effects/dosage 효능/부작용/성인,소아 용법