공부기록

96. Unique Binary Search Trees 본문

코테/LeetCode

96. Unique Binary Search Trees

코타쿠 2022. 3. 21. 20:15

문제

https://leetcode.com/problems/unique-binary-search-trees/

내 코드

import java.util.*;

class Solution {
    int dp[];
    public int numTrees(int n) {
        dp = new int[20];
        for(int i=0; i<dp.length; i++)
            dp[i] = -1;
        dp[0] = 1; dp[1] = 1;
        return dfs(n);
    }

    int dfs(int n){
        if(dp[n] != -1)
            return dp[n];        
        dp[n] = 0;
        for(int i=0; i<n; i++)
            dp[n] += (dfs(i)*dfs(n-1-i));
        return dp[n];
    }

}

피드백

'코테 > LeetCode' 카테고리의 다른 글

210. Course Schedule II  (0) 2022.04.08
240. Search a 2D Matrix II  (0) 2022.04.07
242. Valid Anagram  (0) 2022.03.14
1626. Best Team With No Conflicts  (0) 2022.03.06
202. Happy Number  (0) 2022.02.28