55. ์ ํ”„ ๊ฒŒ์ž„ ๐Ÿš€

5470 ๋‹จ์–ด javapythoncppleetcode

์งˆ๋ฌธ



์ด ๊ธฐ์‚ฌ์—์„œ๋Š” Leetcode์˜ '55. Jump Game' ์งˆ๋ฌธ์„ ๋‹ค๋ฃฐ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ์งˆ๋ฌธ์€ ๊ณ ์ „์ ์ธ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ๊ด€์ ์— ๋”ฐ๋ผ Greedy Algorithm ๋ฌธ์ œ ๋˜๋Š” Dynamic Programming ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

์˜๋ฌธ:

You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position.
Return true if you can reach the last index, or false otherwise.



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.


์งˆ๋ฌธ ์„ค๋ช…



์ด ์งˆ๋ฌธ์˜ ๋“ฑ๊ธ‰์€ ๋ณดํ†ต์ž…๋‹ˆ๋‹ค. Greedy Algorithm ๊ธฐ์ˆ ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ์ •ํ™•ํ•˜๋‹ค๊ณ  ๋งํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Dynamic Programming ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ์ด ์งˆ๋ฌธ์€ ์–ด๋ ค์›€์œผ๋กœ ๊ฐ„์ฃผ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์งˆ๋ฌธ์—์„œ๋Š” Greedy Algorithm ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

์šฐ๋ฆฌ๊ฐ€ ์š”์ฒญ๋ฐ›์€ ๊ฒƒ์€ ์ธ๋ฑ์Šค 0์—์„œ ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค๋กœ ์ ํ”„ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ฐ ์ธ๋ฑ์Šค๋Š” ์ ํ”„ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฑฐ๋ฆฌ๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๊ฐ’์ด 2์ธ ์ธ๋ฑ์Šค์— ์žˆ๋Š” ๊ฒฝ์šฐ 2๊ฐœ์˜ ์ธ๋ฑ์Šค๋ฅผ ์•ž์œผ๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค(๋˜๋Š” ์›ํ•˜๋Š” ๊ฒฝ์šฐ 1๊ฐœ). ์ฒ˜์Œ์—๋Š” ์ด ๋ฌธ์ œ๊ฐ€ ์‚ฌ์†Œํ•ด ๋ณด์ด๊ณ Brute Force ๋ชจ๋“  ๊ฒฝ๋กœ๊ฐ€ ๋งˆ์ง€๋ง‰ ์ƒ‰์ธ์— ๋„๋‹ฌํ•˜๋ฉด ์™„๋ฃŒ๋œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ๊ฑฐ์˜ ๊ทธ๋ž˜ํ”„ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด๊ฒƒ์€ ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๊ฐ€์žฅ ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ•์€ ์•„๋‹™๋‹ˆ๋‹ค. O(n^n) ์‹œ๊ฐ„์ž…๋‹ˆ๋‹ค. ๋งค์šฐ ๋Š๋ฆฝ๋‹ˆ๋‹ค.

๋Œ€์‹ , ์šฐ๋ฆฌ๋Š” Greedy์œผ๋กœ ๊ฐ€๊ณ  ์ด O(n) ์‹œ๊ฐ„์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.


๊ถŒ์žฅ ์ง€์‹


  • Array
  • Greedy Algorithm
  • Big O Notation

  • ์šฐ๋ฆฌ๋Š” ๋ฌด์—‡์„ ์•Œ๊ณ  ์žˆ์Šต๋‹ˆ๊นŒ?


  • ์Œ์ˆ˜๊ฐ€ ์•„๋‹Œ ์ •์ˆ˜ ๋ฐฐ์—ด์ด ์ฃผ์–ด์กŒ์Šต๋‹ˆ๋‹ค.
  • ๊ฐ index๋Š” ์ ํ”„ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฑฐ๋ฆฌ๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค๋กœ ์ ํ”„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

  • ์–ด๋–ป๊ฒŒ ํ•  ๊ฒƒ์ธ๊ฐ€:



    ์ฒ˜์Œ์—๋Š” '์ธ๋ฑ์Šค 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค์— ๋„๋‹ฌํ•  ๋•Œ๊นŒ์ง€ ๊ณ„์† ์ ํ”„ํ•˜๊ณ  ์œ ํšจํ•˜์ง€ ์•Š์€ ๊ฒฝ๋กœ์—์„œ ์—ญ์ถ”์ ํ•ฉ๋‹ˆ๋‹ค.'๋ผ๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ Dynamic Programming๊ณผ Graph Problem์ด ๋งค์šฐ ์œ ์‚ฌํ•œ Backtracking ์†”๋ฃจ์…˜์ธ Brute Force ์†”๋ฃจ์…˜์ž…๋‹ˆ๋‹ค.

    ๋Œ€์‹  ์ธ๋ฑ์Šค 0์—์„œ ์‹œ์ž‘ํ•˜๋Š” ๋Œ€์‹  ๋…ผ๋ฆฌ๋ฅผ ๋ฐ˜๋Œ€๋กœ ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค์—์„œ ์‹œ์ž‘ํ•˜์ง€ ์•Š๋Š” ์ด์œ ๋Š” ๋ฌด์—‡์ž…๋‹ˆ๊นŒ? ์ฒ˜์Œ์—๋Š” ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค์ธ ๊ณจ ํฌ์ŠคํŠธ๋ฅผ ๊ฐ–๊ฒŒ ๋˜๋ฉฐ ๊ณจ ํฌ์ŠคํŠธ์˜ ๋ชฉํ‘œ๋Š” ์ธ๋ฑ์Šค 0์— ๋„๋‹ฌํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. '์ด ์š”์†Œ๊ฐ€ ๊ณจ ํฌ์ŠคํŠธ๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๊นŒ?'๋ผ๊ณ  ๋ฌป๋Š” ๋ฐฐ์—ด์„ ๊ฑฐ๊พธ๋กœ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ํ•ด๋‹น ์š”์†Œ๋Š” ์ ํ”„ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ƒˆ ๊ณจ ํฌ์ŠคํŠธ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ „์ฒด ๋ฐฐ์—ด์„ ์‚ดํŽด๋ณผ ๋•Œ๊นŒ์ง€ ์ด ์ž‘์—…์„ ๊ณ„์†ํ•ฉ๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ธ๋ฑ์Šค์— ๋„๋‹ฌํ–ˆ๋‹ค๋ฉด ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค๋กœ ์ ํ”„ํ•  ์ˆ˜ ์žˆ๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์ ํ”„ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
  • ์ดˆ๊ธฐ์— ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค์ธ ๊ณจ ํฌ์ŠคํŠธ๋ฅผ ์ดˆ๊ธฐํ™”ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.
  • ๋ฐฐ์—ด์„ ๊ฑฐ๊พธ๋กœ ๋ฐ˜๋ณตํ•˜๋Š” For ๋ฃจํ”„๋ฅผ ์„ค์ •ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.
  • ๊ฐ ์š”์†Œ์— '์—ฌ๊ธฐ์—์„œ ๊ณจ๋Œ€ ์œ„๋กœ ์ ํ”„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๊นŒ?'๋ฅผ ๋ฌป์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ๋ชฉํ‘œ ํฌ์ŠคํŠธ๋ฅผ ํ•ด๋‹น ์š”์†Œ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
  • ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ ๊ณ„์† ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.
  • ์ „์ฒด ๋ฐฐ์—ด์„ ์‚ดํŽด๋ณผ ๋•Œ๊นŒ์ง€ ์ด ๋…ผ๋ฆฌ๋ฅผ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.
  • ์ด์ œ ๊ณจ ํฌ์ŠคํŠธ๊ฐ€ ์ฒซ ๋ฒˆ์งธ ์ธ๋ฑ์Šค์— ์žˆ์Šต๋‹ˆ๊นŒ? ๊ทธ๋ ‡๋‹ค๋ฉด ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ๊ณ  ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์ด๋™ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

  • ๋น…์˜ค ํ‘œ๊ธฐ๋ฒ•:


  • ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n) | ์—ฌ๊ธฐ์„œ n์€ ๋ฐฐ์—ด์˜ ๊ธธ์ด์ž…๋‹ˆ๋‹ค.
  • ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1) | ์ถ”๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹นํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.



  • ํŒŒ์ด์ฌ ์†”๋ฃจ์…˜




    class Solution:
        def canJump(self, nums: List[int]) -> bool:
    
            goal_post = len(nums) - 1
    
            for i in range(len(nums) - 2, -1, -1):
                if i + nums[i] >= goal_post:
                    goal_post = i
    
            return (goal_post == 0)
    
    


    ์ž๋ฐ” ์†”๋ฃจ์…˜




    class Solution {
        public boolean canJump(int[] nums) {
    
            int goal_post = nums.length - 1;
    
            for (int i = nums.length - 1; i >= 0; i--) {
                int jump_distance = i + nums[i];
                if (jump_distance >= goal_post) {
                    goal_post = i;
                }
            }
    
            return (goal_post == 0) ? true : false;
        }
    }
    


    C++ ์†”๋ฃจ์…˜




    class Solution {
    public:
        bool canJump(vector<int>& nums) {
    
            int goal_post = nums.size() - 1;
    
            for (int i = nums.size() - 1; i >= 0; i--) {
                int jump_distance = i + nums[i];
                if (jump_distance >= goal_post){
                    goal_post = i;
                }
            }
    
            return (!goal_post) ? true : false;
        }
    };
    


    ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์†”๋ฃจ์…˜




    /**
     * @param {number[]} nums
     * @return {boolean}
     */
    var canJump = function (nums) {
    
        let goal_post = nums.length - 1;
    
        for (let i = nums.length - 1; i >= 0; i--) {
            const jump_distance = i + nums[i];
            if (jump_distance >= goal_post) {
                goal_post = i;
            }
        }
    
        return !goal_post ? true : false;
    };
    
    

    ์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ