반응형

설명

코드

public class Solution {
    private static final Map<Long, Long> memo = new HashMap<>();

    public static long fibonacci(long x) {
        if (x <= 2) {
            return 1;
        }

        Long result = memo.get(x);

        if (result == null) {
            result = fibonacci(x - 1) + fibonacci(x - 2);
            memo.put(x, result);
        }

        return result;
    }
}
class SolutionTest extends Specification {
    @Unroll
    def "#x -> #result"() {
        expect:
        Solution.fibonacci(x) == result

        where:
        x  | result
        10 | 55
        50 | 12586269025
    }
}
반응형

+ Recent posts