Can anyone help me explain my TLE code with memorization?
I just use a map to store each K and corresponding probability.\nEach time I try a num in range[1,W]\nI think in this way, no duplicate dfs will be calculated a
I just use a map to store each K and corresponding probability.\nEach time I try a num in range[1,W]\nI think in this way, no duplicate dfs will be calculated and it should be similar to dp solution.\nCan anyone tell how this can be slower than dp...? It cannot pass all the test cases.\n\nclass Solution {\n    Map<Integer, Double> map;  //record K\n    public double new21Game(int N, int K, int W) {\n        map = new HashMap<>();\n        return dfs(N, K, W);\n    }\n    \n    public double dfs(int N, int K, int W) {\n        if (map.containsKey(K)) {\n            return map.get(K);\n        }\n        if (K <= 0) {\n            if (N >= 0) {\n                return 1.0;\n            }\n            return 0.0;\n        }\n        double p = 0.0;\n        for (int i = W; i >= 1; i--) {\n            p = p + dfs(N - i, K - i, W) / W;\n        }\n        map.put(K, p);\n        return p;\n    }\n}\n
[]