Algorithms and Data Structures/Coding Practices

AlgoExpert Array of Products

brightlightkim 2022. 7. 22. 09:19

def arrayOfProducts(array):
    # Write your code here.
    products = 1
    zeroIdx = []
    for i in range(len(array)):
        if (array[i] == 0):
            zeroIdx.append(i)
        else:
            products *= array[i]
    
    for i in range(len(array)):
        if len(zeroIdx) > 1:
            array[i] = 0
        elif len(zeroIdx) == 1:
            if i in zeroIdx:
                array[i] = products
            else:
                array[i] = 0
        else:
            array[i] = products / array[i]
                
    return array

'Algorithms and Data Structures > Coding Practices' 카테고리의 다른 글

AlgoExpert Merge Overlapping Intervals  (0) 2022.07.22
AlgoExpert First Duplicate Value  (0) 2022.07.22
AlgoExpert Monotonic Array  (0) 2022.07.21
AlgoExpert Move Element to End  (0) 2022.07.20
Kattis Above Average  (0) 2022.07.20