[LeetCode] 198. House Robber

2023. 8. 26. 11:29·Problem Solving/LeetCode

출처 : https://leetcode.com/problems/house-robber/

 

House Robber - LeetCode

Can you solve this real interview question? House Robber - You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent ho

leetcode.com

 

아직 부족하다는 생각이 많이 든 문제였다. DP를 사용하였지만 솔루션을 보면 내가 짠 코드가 무색해진다. 어려운 난이도가 아니였지만 아래와 같은 아이디어를 적용하면 내가 짠 코드처럼 길게 적지 않아도 되는 것...! 이것이 DP가 아닐까?

 

 

내 코드 :

class Solution {
    public int rob(int[] nums) {
        int l = nums.length;
        int[] dp = new int[l];
        
        if (l == 1) return nums[0];
        else if (l == 2) return Math.max(nums[0], nums[1]);
        else {
            dp[0] = nums[0]; dp[1] = nums[1];
            dp[2] = Math.max(dp[1], dp[0] + nums[2]);
            
            if (l == 3) return Math.max(dp[2], dp[1]);
        
            for(int i=3; i<l; i++) {
                dp[i] = Math.max(nums[i] + dp[i-2], Math.max(nums[i] + dp[i-3], dp[i-1]));
            }
            /*
            for(int i=0; i<l; i++) {
                System.out.println(dp[i] + " ");
            }*/
            return Math.max(dp[l-1], dp[l-2]);
        }
    }
}

 

 

다른 사람 코드 : 

class Solution {
    public int rob(int[] nums) {
        int prev1 = 0;
        int prev2 = 0;
        for(int num : nums) {
            int dp = Math.max(prev1, prev2+num);
            prev2 = prev1;
            prev1 = dp;
        }

        return prev1;
    }
}
'Problem Solving/LeetCode' 카테고리의 다른 글
  • [LeetCode] 152. Maximum Product Subarray
  • [LeetCode] 238. Product of Array Except Self
  • [LeetCode] 36. Valid Sudoku
  • [LeetCode] 347. Top K Frequent Elements
kimdozzi
kimdozzi
끝까지 포기하지 않으면, 내가 다 이겨!
  • kimdozzi
    도브로
    kimdozzi
  • 전체
    오늘
    어제
    • 분류 전체보기 (132)
      • Problem Solving (49)
        • Baekjoon (29)
        • Programmers (0)
        • LeetCode (17)
        • 삼성 유형 (2)
      • Computer Science (27)
        • Operating System (2)
        • Algorithms (13)
        • Network (6)
        • DataBase (6)
      • Backend (33)
        • JavaScript (0)
        • TypeScript (6)
        • Java (7)
        • Spring Boot (7)
        • Spring Security (6)
        • JPA (2)
        • Mybatis (1)
        • Junit5 (1)
        • Redis (3)
      • DevOps (14)
        • Git, Github (5)
        • docker (4)
        • AWS (3)
        • nginx (2)
      • etc (6)
        • IntelliJ (3)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
    • 티스토리
    • 설정
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    온라인 쿼리
    세그먼트 트리
    TypeScript
    docker image
    티스토리챌린지
    구간합
    구간 업데이트
    도커
    점 업데이트
    인덱서블 타입
    파이썬
    오블완
    Bucket
    CORS
    누적합
    컨테이너
    imos법
    S3
    오프라인 쿼리
    interface
    인덱스 시그니처
    삼성기출
    python
    인터페이스
    PrefixSum
    백준
    타입스크립트
    segment tree
    AWS
    docker
    알고리즘
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.0
kimdozzi
[LeetCode] 198. House Robber
상단으로

티스토리툴바