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

Maximum Product of Two Elements in an Array

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

첫번째 코드

 

굳이 answer이라는 리스트를 만들지 말고 Max값을 비교해가면서 Max 값만을 갖고 움직이자!

 

class Solution:
    def maxProduct(self, nums: List[int]) -> int:
        Max = 0
        
        for i in range(len(nums)):
            for j in nums[i+1:]:
                Max = max(Max, (nums[i]-1)*(j-1))
        
        return Max

 

통과!


링크

https://github.com/ornni/leetcode/tree/main/1464-maximum-product-of-two-elements-in-an-array

 

leetcode/1464-maximum-product-of-two-elements-in-an-array 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' 카테고리의 다른 글

Minimum Number of Moves to Seat Everyone  (0) 2024.06.17
Range Sum of BST  (0) 2024.06.14
Search Insert Position  (0) 2024.06.13
Counting Bits  (0) 2024.06.11
Minimum Number Game  (0) 2024.06.05