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
- 코드로배우는스프링부트웹프로젝트
- 알파회계
- 네트워크 설정
- 서버설정
- ㅒ
- 처음 만나는 AI 수학 with Python
- 자바편
- 자료구조와함께배우는알고리즘입문
- 목록처리
- iterator
- 이터레이터
- GIT
- baeldung
- resttemplate
- 친절한SQL튜닝
- Kernighan의 C언어 프로그래밍
- 구멍가게코딩단
- 리눅스
- 스프링 시큐리티
- 데비안
- /etc/network/interfaces
- 선형대수
- 처음 만나는 AI수학 with Python
- d
- 페이징
- 자료구조와 함께 배우는 알고리즘 입문
- 스프링부트핵심가이드
- 코드로배우는스프링웹프로젝트
- 티스토리 쿠키 삭제
- network configuration
Archives
- Today
- Total
bright jazz music
238. Product of Array Except Self 본문
// 나의 접근 방법. O(n2)
function productExceptSelf(nums: number[]): number[] {
let answerArr: number[] = []
for(let i = 0; i < nums.length; i++) { // ← for로 수정
if(i === 0) { // ← 인덱스 체크로 수정
let answer = 1;
for(let j = i + 1; j < nums.length; j++) {
answer *= nums[j]
}
answerArr.push(answer);
} else if(i === nums.length - 1) { // ← 마지막 인덱스 체크
let answer = 1;
for(let j = i - 1; j >= 0; j--) { // ← >= 0으로 수정
answer *= nums[j]
}
answerArr.push(answer);
} else { // ← 중간 요소들 처리 추가 필요
let answer = 1;
// 왼쪽 곱
for(let j = 0; j < i; j++) {
answer *= nums[j];
}
// 오른쪽 곱
for(let j = i + 1; j < nums.length; j++) {
answer *= nums[j];
}
answerArr.push(answer);
}
}
return answerArr // ← 오타 수정
}
Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].
The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
You must write an algorithm that runs in O(n) time and without using the division operation.
Example 1:
Input: nums = [1,2,3,4]
Output: [24,12,8,6]
Example 2:
Input: nums = [-1,1,0,-3,3]
Output: [0,0,9,0,0]
Constraints:
2 <= nums.length <= 105
-30 <= nums[i] <= 30
The input is generated such that answer[i] is guaranteed to fit in a 32-bit integer.
Follow up: Can you solve the problem in O(1) extra space complexity? (The output array does not count as extra space for space complexity analysis.)
function productExceptSelf(nums) {
const n = nums.length;
const answer = new Array(n);
// 1. 왼쪽에서 오른쪽으로 prefix product 계산
const leftProducts = new Array(n);
leftProducts[0] = 1;
for (let i = 1; i < n; i++) {
leftProducts[i] = leftProducts[i - 1] * nums[i - 1];
}
// 2. 오른쪽에서 왼쪽으로 suffix product 계산
const rightProducts = new Array(n);
rightProducts[n - 1] = 1;
for (let i = n - 2; i >= 0; i--) {
rightProducts[i] = rightProducts[i + 1] * nums[i + 1];
}
// 3. 두 값을 곱함
for (let i = 0; i < n; i++) {
answer[i] = leftProducts[i] * rightProducts[i];
}
return answer;
}
풀이 2: O(1) 공간 (최적화)
결과 배열을 재사용해서 추가 공간을 줄이기
function productExceptSelf(nums) {
const n = nums.length;
const answer = new Array(n);
// 1. answer에 왼쪽 곱을 먼저 저장
answer[0] = 1;
for (let i = 1; i < n; i++) {
answer[i] = answer[i - 1] * nums[i - 1];
}
// 2. 오른쪽에서 오면서 곱해줌
let rightProduct = 1;
for (let i = n - 1; i >= 0; i--) {
answer[i] = answer[i] * rightProduct;
rightProduct *= nums[i];
}
return answer;
}
```
## 동작 과정 (nums = [1,2,3,4])
**Step 1: 왼쪽 곱 계산**
```
answer = [1, 1, 1×2, 1×2×3]
= [1, 1, 2, 6]
```
**Step 2: 오른쪽에서 곱하기**
```
i=3: answer[3] = 6 × 1 = 6, rightProduct = 1×4 = 4
i=2: answer[2] = 2 × 4 = 8, rightProduct = 4×3 = 12
i=1: answer[1] = 1 × 12 = 12, rightProduct = 12×2 = 24
i=0: answer[0] = 1 × 24 = 24, rightProduct = 24×1 = 24
answer = [24, 12, 8, 6] ✓'LeetCode' 카테고리의 다른 글
| 334. Increasing Triplet Subsequence (0) | 2026.01.17 |
|---|---|
| 151. Reverse Words in a String (0) | 2026.01.15 |
| 345. Reverse Vowels of a String (0) | 2026.01.14 |
| 605. Can place flowers (0) | 2026.01.13 |
Comments
