Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- DDD #도메인 #도메인 주도 설계 #도메인 주도 섥계 핵심
- aws #cloudwatch #log insight
- 코딩게임
- DDD #도메인 #도메인 주도 설계 #도메인 주도 설계 핵심
- leetcode #20. Valid Parentheses #알고리즘 #leetcode Valid Parentheses
- Find Pivot Index
- leetcode
- codingame #코딩게임 #codingame fall challenge2023 #코딩게임 2023 가을 챌린지
- #move zeroes
- 867. Transpose Matrix #Transpose Matrix
- Fall Challenge 2023
- 도메인 주도 설계 핵심
- base7
- leetcode #알고리즘 #릿코드
- LeetCode #
- leetcode #2206. Divide Array Into Equal Pairs
- ddd
- #20. Valid Parentheses java
- 도메인 주도 설계 핵심 #DDD #도메인 주도 설계 #도메인
- codinGame
- 반 버논
- 도메인 #도메인 주도 설계 #도메인 주도 설계 핵심 #DDD
Archives
- Today
- Total
서하아빠의 개발 블로그
724. Find Pivot Index 본문
문제
풀이방법
pivot 인덱스를 기준으로 왼쪽과 오른쪽의 합의 같을 때의 pivot 인덱스를 반환하는 문제
전체 합을 먼저 구해놓고 앞에서 부터 빼서 왼쪽 합, 오른쪽 합을 비교해서
- 왼쪽 합과 오른쪽 합이 동일 : 해당 index를 반환
- 왼쪽 합과 오른쪽 합이 다름 : for문을 계속 순환
var pivotIndex = function(nums) {
let total = nums.reduce((a,b) => a+b)
let leftSum = 0;
for (let i = 0; i < nums.length; i++) {
const rightSum = total - leftSum - nums[i];
if (leftSum === rightSum) return i;
leftSum += nums[i];
}
return -1;
};
'알고리즘 > LeetCode' 카테고리의 다른 글
1832. Check if the Sentence Is Pangram (0) | 2023.12.10 |
---|---|
867. Transpose Matrix (0) | 2023.07.07 |
20. Valid Parentheses (0) | 2022.05.24 |
121. Best Time to Buy and Sell Stock (0) | 2022.05.20 |
217. Contains Duplicate (0) | 2022.05.20 |