Notice
Recent Posts
Recent Comments
Link
«   2025/03   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
Tags more
Archives
Today
Total
관리 메뉴

개발의변화

LeetCode 1220. Count Vowels Permutation 본문

알고리즘

LeetCode 1220. Count Vowels Permutation

refindmySapporo 2024. 3. 28. 10:02
반응형

https://leetcode.com/problems/count-vowels-permutation/description/?envType=study-plan-v2

 

전형적인 탑다운 문제였던 것 같다.

정해진 규칙에 맞게 진행하면서 마지막 배열의 값을 리턴하면 되는 문제였다.

 

특히 DP를 사용할 때는 항상 초기 설정이 중요한 것 같다.

1차원 배열을 쓸지, 2차원 배열을 쓸지 row,col의 값으로 어떠한 값을 넣어서 기준을 잡을지 

조금 더 깊은 고민을 해봐야 겠다.

 

class Solution {
    public int countVowelPermutation(int n) {
        long [][] arr = new long[n+1][5];
        long N = (int)Math.pow(10,9) + 7;
        for (int i = 0; i < 5; i++) {
            arr[1][i] = 1;
        }
        for (int i = 2 ; i < arr.length; i++) {
            arr[i][0] = (arr[i-1][1] % N + arr[i-1][2] % N + arr[i-1][4] % N) % N;
            arr[i][1] = (arr[i-1][0] % N + arr[i-1][2] % N) % N;
            arr[i][2] = (arr[i-1][1] % N  + arr[i-1][3] % N) % N ;
            arr[i][3] = (arr[i-1][2]) % N;
            arr[i][4] = (arr[i-1][2] % N + arr[i-1][3] % N) % N ;
        } 

        int answer = 0;
        for (int i = 0; i < arr[0].length; i++) {
            answer = (int)(answer % N + arr[arr.length-1][i] % N) ; 
        }

        return answer % (int)N;
    }
}
반응형