[LeetCode] 2873. Maximum Value of an Ordered Triplet I (Python)

2025. 4. 3. 02:56·알고리즘/LeetCode

 

 

난이도: Easy

Problem Description


You are given a 0-indexed integer array nums.

 

Return the maximum value over all triplets of indices (i, j, k) such that i < j < k. If all such triplets have a negative value, return 0.

 

The value of a triplet of indices (i, j, k) is equal to (nums[i] - nums[j]) * nums[k].

 

Problem Example


Example 1:

Input: nums = [12,6,1,2,7]
Output: 77
Explanation: The value of the triplet (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77.
It can be shown that there are no ordered triplets of indices with a value greater than 77.

Example 2:

Input: nums = [1,10,3,4,19]
Output: 133
Explanation: The value of the triplet (1, 2, 4) is (nums[1] - nums[2]) * nums[4] = 133.
It can be shown that there are no ordered triplets of indices with a value greater than 133.

Example 3:

Input: nums = [1,2,3]
Output: 0
Explanation: The only ordered triplet of indices (0, 1, 2) has a negative value of (nums[0] - nums[1]) * nums[2] = -3. Hence, the answer would be 0.

 

Constraints


  • 3 <= nums.length <= 100
  • 1 <= nums[i] <= 106

 

 

✏️ Solution


class Solution:
    def maximumTripletValue(self, nums: List[int]) -> int:
        answer = 0
        for triplet in tuple(combinations(nums, 3)):
            answer = max(answer, (triplet[0] - triplet[1]) * triplet[2])
        
        return answer

오랜만에 Easy문제였다.

 

문제를 보자마자 이건 itertools의 combinations 함수로 충분히 해결할 수 있을것 같아서 한번 활용해보았다.

 

combinations함수를 써볼 기회가 많이 없었는데 이번에 한번 써볼 수 있어서 좋았다.

 

 

⚙️ Runtime & Memory


사실 combinations(nums, 3)의 시간복잡도는 O(n^3)이라서 성능이 좋진 않다..

 

 


문제: 2873. Maximum Value of an Ordered Triplet I

 

 

깃허브: github

 

algorithmPractice/LeetCode/3154-maximum-value-of-an-ordered-triplet-i at main · laewonJeong/algorithmPractice

하루 한 문제 챌린지. Contribute to laewonJeong/algorithmPractice development by creating an account on GitHub.

github.com

 

'알고리즘 > LeetCode' 카테고리의 다른 글

[LeetCode] 3396. Minimum Number of Operations to Make Elements in Array Distinct (Python)  (0) 2025.04.08
[LeetCode] 416. Partition Equal Subset Sum (C++)  (0) 2025.04.07
[LeetCode] 2140. Solving Questions With Brainpower (Python)  (0) 2025.04.01
[LeetCode] 2780. Minimum Index of a Valid Split (Python)  (0) 2025.03.27
[LeetCode] 2033. Minimum Operations to Make a Uni-Value Grid (Python)  (0) 2025.03.26
'알고리즘/LeetCode' 카테고리의 다른 글
  • [LeetCode] 3396. Minimum Number of Operations to Make Elements in Array Distinct (Python)
  • [LeetCode] 416. Partition Equal Subset Sum (C++)
  • [LeetCode] 2140. Solving Questions With Brainpower (Python)
  • [LeetCode] 2780. Minimum Index of a Valid Split (Python)
Laewon Jeong
Laewon Jeong
  • Laewon Jeong
    래원
    Laewon Jeong
    글쓰기 | 관리
  • GitHub

    • github.com/laewonJeong
  • 전체
    오늘
    어제
    • 분류 전체보기 (172)
      • Docker 및 Kubernetes (11)
        • Docker (5)
        • Kubernetes (6)
      • Data Engineering (18)
        • Hadoop (5)
        • Spark (5)
        • Kafka (5)
        • Airflow (3)
      • CI|CD (3)
      • 알고리즘 (136)
        • 알고리즘 (2)
        • LeetCode (118)
        • 프로그래머스 (11)
        • BOJ (1)
        • 코딩테스트 대비 (4)
      • 서버 (2)
        • 미니 서버 (2)
      • 잡담 (1)
  • 태그

    알고리즘
    String
    오블완
    Kubernetes
    코딩테스트
    쿠버네티스
    이진탐색
    dfs
    누적합
    Apache Spark
    Python
    leetcode
    리트코드
    DP
    파이썬
    분산처리
    분산
    아파치 하둡
    우선순위큐
    programmers
    BFS
    티스토리챌린지
    docker
    백트래킹
    아파치 스파크
    프로그래머스
    heapq
    문자열
    그래프
    도커
  • 인기 글

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
Laewon Jeong
[LeetCode] 2873. Maximum Value of an Ordered Triplet I (Python)
상단으로

티스토리툴바