본문 바로가기
코딩 테스트/leetcode

Final Prices With a Special Discount in a Shop

by ornni 2024. 6. 27.
728x90
반응형

첫번째 코드

 

인덱스를 통해서 다음 값 중 작은 값이 있다면 현재 값에서 빼고

그렇지 않으면 그냥 현재 값을 정답 리스트에 추가한다.

 

class Solution:
    def finalPrices(self, prices: List[int]) -> List[int]:
        answer = []
        output = 0

        for i in range(len(prices)):
            output = prices[i]
            for j in range(i+1, len(prices)):
                if prices[i] >= prices[j]:
                    output = prices[i] - prices[j]
                    break
            answer.append(output)

        return answer

 

통과!


링크

https://github.com/ornni/leetcode/tree/main/1475-final-prices-with-a-special-discount-in-a-shop

 

leetcode/1475-final-prices-with-a-special-discount-in-a-shop at main · ornni/leetcode

Collection of LeetCode questions to ace the coding interview! - Created using [LeetHub v3](https://github.com/raphaelheinz/LeetHub-3.0) - ornni/leetcode

github.com

 

반응형

'코딩 테스트 > leetcode' 카테고리의 다른 글

Count Pairs Whose Sum is Less than Target  (0) 2024.06.29
Neither Minimum nor Maximum  (0) 2024.06.28
Removing Stars From a String  (0) 2024.06.26
Shuffle the Array  (0) 2024.06.25
Divisor Game  (0) 2024.06.24