[LeetCode] 1014. Best Sightseeing Pair (Python)

2024. 12. 27. 21:02·알고리즘/LeetCode

 

난이도: Medium

문제 설명


You are given an integer array values where values[i] represents the value of the ith sightseeing spot. Two sightseeing spots i and j have a distance j - i between them.

 

The score of a pair (i < j) of sightseeing spots is values[i] + values[j] + i - j: the sum of the values of the sightseeing spots, minus the distance between them.

 

Return the maximum score of a pair of sightseeing spots.

 

문제 예제


Example 1:

Input: values = [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, values[i] + values[j] + i - j = 8 + 5 + 0 - 2 = 11

Example 2:

Input: values = [1,2]
Output: 2

 

제한 사항


  • 2 <= values.length <= 5 * 10^4
  • 1 <= values[i] <= 1000

 

✏️ Solution(솔루션)

class Solution:
    def maxScoreSightseeingPair(self, values: List[int]) -> int:
        answer = 0
        n = len(values)
        dp = [0 for _ in range(n)]
        dp[0] = values[0]

        for i in range(1, n):
            answer = max(answer, dp[i-1] + values[i] - i)
            dp[i] = max(dp[i-1], values[i] + i)

        return answer

 

문제에서는 values[i] + values[j] + i - j 라고 나와있지만, values[i] + i + values[j] - j로 볼 수도 있다.

 

 

따라서, dp 배열을 사용해 dp[i]에 values[i]+i 의 최대값을 저장하고, dp[i-1]값과 values[i]−i를 더해 answer변수와 비교해서 더큰 값을 answer에 저장했다.

 

그리고 answer를 return하여 정답을 맞출 수 있었다.


1014. Best Sightseeing Pair 

 

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

[LeetCode] 3286. Find a Safe Walk Through a Grid (Python)  (0) 2024.12.29
[LeetCode] 1122. Relative Sort Array (Python)  (1) 2024.12.28
[LeetCode] 494. Target Sum (Python)  (0) 2024.12.26
[LeetCode] 1962. Remove Stones to Minimize the Total (Python)  (2) 2024.12.23
[LeetCode] 2337. Move Pieces to Obtain a String (Python)  (1) 2024.12.20
'알고리즘/LeetCode' 카테고리의 다른 글
  • [LeetCode] 3286. Find a Safe Walk Through a Grid (Python)
  • [LeetCode] 1122. Relative Sort Array (Python)
  • [LeetCode] 494. Target Sum (Python)
  • [LeetCode] 1962. Remove Stones to Minimize the Total (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)
  • 태그

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

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
Laewon Jeong
[LeetCode] 1014. Best Sightseeing Pair (Python)
상단으로

티스토리툴바