위지원의 데이터 일기 🐈
Home
  • 분류 전체보기 (563)
    • ✎ 2025년 (4)
    • 2024년 (16)
    • 2023년 (6)
    • 2022년 (35)
      • Developement (22)
      • Error (9)
    • 2021년 (68)
      • ERROR (9)
      • 알고리즘 (11)
      • 개발공부 (21)
      • Data (15)
      • 21.下 (12)
    • 2020년 (164)
      • 코테 (84)
      • Development (29)
      • 정처기 (41)
    • 2019년 (27)
    • 2018년 (89)
      • English Speaking (8)
      • Error (12)
      • C, Java, FileSystem (13)
      • DataBase (15)
      • Java (2)
      • 지식 (16)
      • Go (3)
      • spark (9)
      • 영어 (5)
      • 알고리즘 (6)
    • 2017년 (143)
      • Error (17)
      • machine learning (16)
      • Spark (20)
      • Database (19)
      • Python (17)
      • Spring (9)
      • etc. (10)
      • 백준 (5)
      • Google Platform (12)
      • web Development (7)
      • Docker (3)
      • Linux (8)
Home
  • 분류 전체보기 (563)
    • ✎ 2025년 (4)
    • 2024년 (16)
    • 2023년 (6)
    • 2022년 (35)
      • Developement (22)
      • Error (9)
    • 2021년 (68)
      • ERROR (9)
      • 알고리즘 (11)
      • 개발공부 (21)
      • Data (15)
      • 21.下 (12)
    • 2020년 (164)
      • 코테 (84)
      • Development (29)
      • 정처기 (41)
    • 2019년 (27)
    • 2018년 (89)
      • English Speaking (8)
      • Error (12)
      • C, Java, FileSystem (13)
      • DataBase (15)
      • Java (2)
      • 지식 (16)
      • Go (3)
      • spark (9)
      • 영어 (5)
      • 알고리즘 (6)
    • 2017년 (143)
      • Error (17)
      • machine learning (16)
      • Spark (20)
      • Database (19)
      • Python (17)
      • Spring (9)
      • etc. (10)
      • 백준 (5)
      • Google Platform (12)
      • web Development (7)
      • Docker (3)
      • Linux (8)
블로그 내 검색
포트폴리오

위지원의 데이터 일기 🐈

데이터를 사랑하고 궁금해하는 기록쟁이입니다! 😉 Super Data Girl이 되는 그날까지🏃‍♀️ 화이팅!

  • 🖥 깃블로그
  • 🌍 위키원
  • 📑 내맘대로 스크랩
  • 💌 메일
  • 2020년/코테

    [코테 연습] Product of Array Except Self

    2020. 9. 24. 23:04

    by. 위지원

    leetcode.com/problems/product-of-array-except-self/

     

    Product of Array Except Self - LeetCode

    Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

    leetcode.com

    Given an array nums of n integers where n > 1,  return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].

     


    0이 들어오는 경우만 잘 해결하면 쉽게 푸는 문제였다.

    from functools import reduce
    from collections import Counter
    
    # 이중 for문으로 풀 수 있겠지만 O(n)으로 풀어야 함
    class Solution:
        def multiply(self,arr):
            return reduce(lambda x, y: x * y, arr)
    
        def productExceptSelf(self, nums: List[int]) -> List[int]:
            # 0의 개수를 체크 
            counterForZero = Counter(nums)[0]
            
            # 0 이 1개라면 0이 선택되는 경우 빼고 다 0이고 0의 자리에만 product value
            if counterForZero == 1:
                res = [0]*len(nums)
                idx = nums.index(0)
                res[idx] = self.multiply(nums[:idx]+nums[idx+1:])
                return res
        
            #0이 1개 이상이면 언제든 0
            if counterForZero  > 1:
                return [0]*len(nums)
            
            #0이 하나도 없는 경우
            res = self.multiply(nums)
            for idx,num in enumerate(nums):
                nums[idx] = int(res/num)
            
            return nums 

     

    저작자표시 (새창열림)

    '2020년 > 코테' 카테고리의 다른 글

    [codility] 몸풀이용 문제 3개(BinaryGap, CyclicRotation, OddOccurrencesInArray)  (0) 2020.09.28
    [코테 연습] find-the-difference  (0) 2020.09.24
    [코테 연습] Fair Candy Swap  (0) 2020.09.24
    [코테 연습] Valid Parentheses  (0) 2020.09.24
    [코테 연습] All Elements in Two Binary Search Trees  (0) 2020.09.16

    잠깐만요~! 읽으신김에 이런 글들은 어떠세요? 👀

    • [codility] 몸풀이용 문제 3개(BinaryGap, CyclicRotation, OddOccurrencesInArray) 2020.09.28
    • [코테 연습] find-the-difference 2020.09.24
    • [코테 연습] Fair Candy Swap 2020.09.24
    • [코테 연습] Valid Parentheses 2020.09.24
    맨 위로
전체 글 보기
Tistory 로그인
Tistory 로그아웃
로그아웃 글쓰기 관리

열정! 열정! 열정! 🔥

Designed by Nana
블로그 이미지
위지원
데이터 엔지니어로 근무 중에 있으며 데이터와 관련된 일을 모두 좋아합니다!. 특히 ETL 부분에 관심이 가장 크며 데이터를 빛이나게 가공하는 일을 좋아한답니다 ✨

티스토리툴바

티스토리툴바