[LeetCode#55] Jump Game

2024. 9. 16. 00:19·Algorithm/문제풀이

❒ Description


날짜 2024.09.15 (일)
난이도 Medium
제목 Jump Game
링크 https://leetcode.com/problems/jump-game/description/
자료구조 / 알고리즘 배열 / 그리디
시간 복잡도 O(n)

 

이번 문제는 Greedy 알고리즘을 사용해서 해결하는 문제다. 처음에 재귀를 통해 접근을 했지만 풀지 못했고,

풀었다고 하더라도 Time Limie Exceeded 에러를 만나게 된다. 

 

 

 

 

 

 

❒ Greedy(탐욕적) 알고리즘


Greedy 알고리즘은 기준에 따라 좋은 것을 선택하는 알고리즘이므로 문제에서 '가장 큰 순서대로', '가장 작은

순서대로'와 같은 기준을 알게 모르게 제시해준다. 대체로 이 기준은 정렬 알고리즘을 사용 했을 때 만족시킬 수

있으므로 그리디 알고리즘 문제는 자주 정렬 알고리즘과 짝을 이뤄 출제된다.

 

 

 

 

 

❒ 문제 분석


이 문제는 마지막 index까지 도달할 수 있냐 없냐를 확인해야 하는 문제이다. 배열 `{2, 3, 1, 1, 4}`이 주어졌을 때,

1번 째 index는 최대 2칸을 갈 수 있다. 여기서 최대 2칸이라고 했기 때문에 index#1도 갈 수 있고, index#2 도

갈 수 있다는 의미이다.

 

풀이 순서

  1. 최대로 도달할 수 있는 값을 담는 reachable 선언
  2. 배열 순회
    • 현재 index > reachable : 그 index에는 도달할 수 없다.
    • 현재 index =< reachable : 도달할 수 있는 최대 index 갱신 

 

 

 

 

 

❒ Solution


1. Greedy 알고리즘

public boolean canJump(int[] nums) {
    int reachable = 0;
    for (int i = 0; i < nums.length; i++) {
        if (i > reachable) {
            return false;
        }
        reachable = Math.max(reachable, i + nums[i]);
    }
    return true;
}

 

 

 

2. Recursive

public boolean canJump(int[] nums) {
   canJumpFromPosition(0, nums);
}

public boolean canJumpFromPosition(int position, int[] nums) {
    if (position == nums.length - 1) {
        return true;
    }
    int reachable = Math.min(position + nums[i], nums.length - 1);
    for (nextPositsion = position + 1; nextPosition <= reachable; nextPosition ++) {
        if (canJumpFromPosition(nextPosition, nums) {
            return true;
        }
    }
    return false;
}

 

 

 

 

 


'Algorithm > 문제풀이' 카테고리의 다른 글

[LeetCode#406] Queue reconstruction by height  (0) 2024.09.17
[LeetCode#122] Best Time to Buy and Sell II  (0) 2024.09.16
[LeetCode#743] Network Delay Time  (0) 2024.09.15
[Programmers#49191] 순위  (0) 2024.09.14
[Programmers] 게임 맵 최단거리  (0) 2024.09.12
'Algorithm/문제풀이' 카테고리의 다른 글
  • [LeetCode#406] Queue reconstruction by height
  • [LeetCode#122] Best Time to Buy and Sell II
  • [LeetCode#743] Network Delay Time
  • [Programmers#49191] 순위
gilbert9172
gilbert9172
gilbert9172 님의 블로그 입니다.
  • gilbert9172
    バックエンド
    gilbert9172
  • 전체
    오늘
    어제
    • All Categories (207)
      • 우테코 7기 (21)
        • 1주차 (8)
        • 2주차 (5)
        • 3주차 (6)
      • Langauge (6)
        • Java (3)
        • Kotlin (3)
      • Back-End (13)
        • SpringBoot (1)
        • Trouble Shooting (0)
        • Setup & Configuration (1)
        • SQL (3)
        • Redis (8)
      • Architecture (6)
        • Multi Module (1)
        • DDD (5)
      • CS (30)
        • Data Structure (6)
        • Operating System (0)
        • Network (12)
        • Database (10)
        • Design Pattern (2)
      • Algorithm (78)
        • 내용 정리 (18)
        • 문제풀이 (60)
      • DevOps (6)
        • AWS (5)
        • Git (1)
      • Front-End (1)
        • Trouble Shooting (1)
      • Project (6)
        • 페이스콕 (6)
      • Book (39)
        • 친절한 SQL 튜닝 (9)
        • 데이터 중심 애플리케이션 설계 (14)
        • 이벤트 기반 마이크로서비스 구축 (6)
        • Spring Batch docs (10)
        • Quartz docs (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    오블완
    Two-Pointer
    greedy
    sliding-window
    binarysearch
    Back-Tracking
    부분단조성
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.2
gilbert9172
[LeetCode#55] Jump Game
상단으로

티스토리툴바