박상길, 『파이썬 알고리즘 인터뷰』를 정리한 내용입니다.
https://leetcode.com/problems/product-of-array-except-self/
Product of Array Except Self - LeetCode
Can you solve this real interview question? Product of Array Except Self - 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 nu
leetcode.com
❗️나눗셈을 사용할 수 없기 때문에 모든 요소의 곱에서 자기 자신을 나눠서 답을 구하는 전략은 사용할 수 없다...!!😟
0 ~ i-1번째까지의 곱셈결과와 i+1 ~ n-1까지의 곱셈결과를 곱하는 전략을 사용한다
교재의 코드에서 내가 이해하기 쉽게 코드를 아주 조금 수정했다
class Solution:
def productExceptSelf(self, nums: [int]):
N = len(nums)
answer = [1]
for i in range(1, N):
answer.append(answer[i-1]*nums[i-1])
p = 1
for i in range(N-1, 0, -1):
answer[i] *= p
p *= nums[i]
return answer
[프로그래머스] 150365. 미로 탈출 명령어| 그리디, BFS | 파이썬, 소스코드, 정답 (0) | 2023.05.30 |
---|---|
[프로그래머스] 150367. 표현 가능한 이진트리| 시뮬레이션, 구현 | 파이썬, 소스코드, 정답 (1) | 2023.05.26 |
[프로그래머스] 152995. 인사고과| 정렬, 구현 | 파이썬, 소스코드, 정답 (1) | 2023.05.23 |
[파이썬 알고리즘 인터뷰] 팰린드롬 판별 | 리스트, deque, 슬라이싱 (0) | 2023.05.22 |
[SW Expert Academy] 15230. 알파벳 공부 | 문자열비교 | 파이썬, 소스코드, 정답 (2) | 2023.05.20 |