728x90
반응형
1480. Running Sum of 1d Array
LeetCode
https://leetcode.com/problems/running-sum-of-1d-array/
Running Sum of 1d Array - LeetCode
Can you solve this real interview question? Running Sum of 1d Array - Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]). Return the running sum of nums. Example 1: Input: nums = [1,2,3,4] Output: [1,3,6,
leetcode.com
Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]).
Return the running sum of nums.
배열 nums가 주어집니다. 배열의 누적 합(running sum)은 runningSum[i] = sum(nums[0]…nums[i])로 정의됩니다.
nums의 누적 합 배열을 반환하세요.
Example
Input: nums = [1,2,3,4]
Output: [1,3,6,10]
Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4]
Input: nums = [1,1,1,1,1]
Output: [1,2,3,4,5]
Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1]
Input: nums = [3,1,2,10,1]
Output: [3,4,6,16,17]
1. 문제 요구사항 분석
배열의 누적합을 구하는 문제
2. 접근 전략
😭 2중 for문 사용 → 시간복잡도
class Solution {
public int[] runningSum(int[] nums) {
int[] answer = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
for (int j = i; j < nums.length; j++) {
answer[j] += nums[i];
}
}
return answer;
}
}
😁 누적합 문제. 누적합의 기본 문제
이전 238번 문제를 풀면서 누적합 문제를 좀 더 연습해야겠다는 생각으로 기본 문제를 풀게 됨
class Solution {
public int[] runningSum(int[] nums) {
int[] answer = new int[nums.length];
answer[0] = nums[0];
for (int i = 1; i < nums.length; i++) {
answer[i] = answer[i - 1] + nums[i];
}
return answer;
}
}728x90
반응형
'프로그래밍 > 알고리즘' 카테고리의 다른 글
| [LeetCode] 53. Maximum Subarray (0) | 2026.05.25 |
|---|---|
| [LeetCode] 2574. Left and Right Sum Differences (0) | 2026.05.19 |
| [LeetCode] 238. Product of Array Except Self (0) | 2026.05.18 |
| [LeetCode] 110. Balanced Binary Tree (0) | 2026.05.15 |
| [LeetCode] 300. Longest Increasing Subsequence (0) | 2026.05.13 |