베어_
TechBear
베어_
전체 방문자
오늘
어제
  • 분류 전체보기 (336)
    • Spring (33)
      • 개념 (13)
      • Security (5)
      • 실습 (1)
      • 토비 스프링 (11)
    • JPA (6)
    • 프로젝트 기록 (24)
    • DB (13)
    • JAVA (18)
    • 알고리즘 (50)
      • 유형정리 (8)
      • Baekjoon (21)
      • LeetCode (18)
    • 디자인패턴 (0)
    • 개발서적 (79)
      • Effective Java (78)
      • 객체지향의 사실과 오해 (1)
    • 독후감 (4)
    • 보안 (2)
    • 운영체제(OS) (53)
      • 공룡책 (53)
    • 컴퓨터 네트워크 (28)
      • 컴퓨터 네트워크 하향식 접근 (23)
    • 자료구조 (1)
    • DevOps (2)
    • 앱 개발 (20)
      • 안드로이드 스튜디오 (20)

블로그 메뉴

    공지사항

    인기 글

    태그

    • dfs
    • jpa
    • C++
    • 데이터베이스
    • Spring
    • 자바
    • 스프링
    • BFS
    • 스프링시큐리티
    • 함수형인터페이스
    • 스레드
    • 운영체제
    • 자바8
    • 백준
    • 알고리즘
    • 코드업
    • 이펙티브자바
    • leetcode
    • 토비스프링
    • java

    최근 댓글

    최근 글

    티스토리

    hELLO · Designed By 정상우.
    베어_

    TechBear

    알고리즘/LeetCode

    [LeetCode] Combination Sum IV (Java)

    2023. 7. 16. 21:53

    ✉️문제

    https://leetcode.com/problems/combination-sum-iv/description/

     

    Combination Sum IV - LeetCode

    Can you solve this real interview question? Combination Sum IV - Given an array of distinct integers nums and a target integer target, return the number of possible combinations that add up to target. The test cases are generated so that the answer can fi

    leetcode.com

     

    📝 접근

    전형적인 DP 알고리즘 문제이다. 이전 경우의 수를 더 해나가면 된다. 

    [1,2,3]이라는 숫자가 있고 target = 4일 때 다음과 같이 1로 만들 수 있는 경우의 수, 1과 2를 만드는 경우의 수로 나눌 수 있지만

    // 1이라는 숫자로 만드는 경우의 수 
    |-------------------|
    | 1 | 1 | 1 | 1 | 1 |
    |-------------------|
    
    // 1과 2로 만드는 경우의 수 
    |-------------------|
    | 1 | 1 | 2 | 2 | 3 |
    |-------------------|

    코드적으로는 i번 째일 때, 이전 경우의 수와 조합이 생기는지 확인하는 편이 더 간단하다.

     

    🗝 문제풀이

        public int combinationSum4(int[] nums, int target) {
            int[] dp = new int[target + 1];
            dp[0] = 1;
            for(int i = 1; i <= target; i++) {
                for(int num : nums) {
                    if(i >= num) {
                        dp[i] += dp[i - num];
                    }
                }
            }
    
            return dp[target];
        }
    저작자표시 비영리 변경금지 (새창열림)
      '알고리즘/LeetCode' 카테고리의 다른 글
      • [LeetCode] Longest Consecutive Sequence (Java)
      • [LeetCode] House Robber II (Java)
      • [LeetCode] Word Break (Java)
      • [LeetCode] Longest Common Subsequence (Java)
      베어_
      베어_
      Today I learned | 문제를 해결하는 개발자

      티스토리툴바