[LeetCode] 238. Product of Array Except Self

2023. 8. 18. 11:16·Problem Solving/LeetCode

출처 : https://leetcode.com/problems/product-of-array-except-self/description/

 

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

 

접근법을 몰라서 풀지 못하였던 문제.

 

 

코드 :

class Solution:
    def productExceptSelf(self, nums: List[int]) -> List[int]:
        out = []
        p = 1 
        for i in range(len(nums)) : 
            out.append(p)
            p = p * nums[i]
        
        print(out)
        p = 1
        for i in range(len(nums)-1,-1,-1) :
            out[i] = out[i] * p
            p = p * nums[i]
        
        return out

 

아래와 같은 방식으로 prefix, suffix 곱셈을 수행해주면 자기 자신을 제외한 나머지 수들에 대한 곱을 구할 수 있다.

'Problem Solving/LeetCode' 카테고리의 다른 글
  • [LeetCode] 152. Maximum Product Subarray
  • [LeetCode] 198. House Robber
  • [LeetCode] 36. Valid Sudoku
  • [LeetCode] 347. Top K Frequent Elements
kimdozzi
kimdozzi
끝까지 포기하지 않으면, 내가 다 이겨!
  • kimdozzi
    도브로
    kimdozzi
  • 전체
    오늘
    어제
    • 분류 전체보기 (132)
      • Problem Solving (49)
        • Baekjoon (29)
        • Programmers (0)
        • LeetCode (17)
        • 삼성 유형 (2)
      • Computer Science (27)
        • Operating System (2)
        • Algorithms (13)
        • Network (6)
        • DataBase (6)
      • Backend (33)
        • JavaScript (0)
        • TypeScript (6)
        • Java (7)
        • Spring Boot (7)
        • Spring Security (6)
        • JPA (2)
        • Mybatis (1)
        • Junit5 (1)
        • Redis (3)
      • DevOps (14)
        • Git, Github (5)
        • docker (4)
        • AWS (3)
        • nginx (2)
      • etc (6)
        • IntelliJ (3)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
    • 티스토리
    • 설정
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    imos법
    누적합
    docker
    interface
    docker image
    점 업데이트
    백준
    티스토리챌린지
    컨테이너
    CORS
    AWS
    알고리즘
    삼성기출
    오프라인 쿼리
    온라인 쿼리
    구간합
    Bucket
    S3
    segment tree
    도커
    python
    인터페이스
    구간 업데이트
    타입스크립트
    인덱서블 타입
    오블완
    TypeScript
    PrefixSum
    파이썬
    세그먼트 트리
    인덱스 시그니처
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.0
kimdozzi
[LeetCode] 238. Product of Array Except Self
상단으로

티스토리툴바