55. Jump Game

2021. 10. 22. 22:12·Algorithm
 

Jump Game - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

0과 같거나 큰 정수로 구성된 1차원 배열이 주어진다.

0번 인덱스에서 시작해서 해당 인덱스가 가리키는 값만큼 인덱스를 이동할 수 있을 때 정수 배열의 마지막 인덱스로 이동이 가능한지를 판단하는 문제다.

 

예시

Input: nums = [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.

Input: nums = [3,2,1,0,4]
Output: false
Explanation: You will always arrive at index 3 no matter what.
Its maximum jump length is 0, which makes it impossible to reach the last index.

접근

DP 문제라고 되어있는데 Greedy 하게 풀 수 있을 것 같았다.

 

1. Greedy

class Solution:
    def canJump(self, nums: List[int]) -> bool:

        maximum = 0
        for index, num in enumerate(nums[:len(nums) - 1]):
            if index > maximum:
                return False
            maximum = max(maximum, index + num)

        return maximum >= len(nums) - 1

index 0을 기준으로 최대로 뛸 수 있는 거리를 maximum에 저장한다.

배열의 마지막 값을 제외하고 반복문을 돌면서 만일 index가 maximum보다 크다는 것은 해당 index에 도달할 수 있는 방법이 존재하지 않는다는 뜻과 같기 때문에 False를 반환한다.

그리고 maximum의 최댓값을 index+num과 비교해서 갱신한다.

 

반복문이 끝난 이후 maximum이 배열의 최대 인덱스보다 크거나 같다면 True를 반환한다.

 

2. 최적화

class Solution:
    def canJump(self, nums: List[int]) -> bool:
        
        if nums[0] == 0 and len(nums) == 1:
            return True
        
        maximum = 0
        for index, num in enumerate(nums):
            if index > maximum:
                return False
            maximum = max(maximum, index + num)
            if maximum >= len(nums) - 1:
                return True

중간에 조건에 도달하면 탈출할 수 있게 해 주면 약간 더 빠르게 통과한다. 큰 차이는 없다.

'Algorithm' 카테고리의 다른 글

134. Gas Station  (0) 2021.10.24
395. Longest Substring with At Least K Repeating Characters  (0) 2021.10.23
213. House Robber II  (1) 2021.10.21
79. Word Search  (0) 2021.10.20
572. Subtree of Another Tree  (0) 2021.10.16
'Algorithm' 카테고리의 다른 글
  • 134. Gas Station
  • 395. Longest Substring with At Least K Repeating Characters
  • 213. House Robber II
  • 79. Word Search
Doljae
Doljae
  • Doljae
    Zero to Hero
    Doljae
  • 전체
    오늘
    어제
    • 분류 전체보기 (349)
      • Programming (54)
      • Algorithm (161)
      • Review (102)
      • Career (8)
      • Diary (18)
      • Shorts (4)
      • Temp (2)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
    • 글 쓰기
    • 관리
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    jpa
    database
    2021
    leetcode
    개발자
    프로그래머스
    2023
    AI
    한빛미디어
    인프콘
    백준
    나는리뷰어다
    면접
    BOJ
    ChatGPT
    PYTHON
    공채
    2022
    코딩테스트
    mysql
    컨퍼런스
    line
    sql
    나는 리뷰어다
    코딩
    회고
    라인
    sql튜닝
    java
    db
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.4
Doljae
55. Jump Game
상단으로

티스토리툴바