Trying to solve [this](https://stackoverflow.com/questions/39031099/perfect-square-in-leetcode) problem with recursion and memoization but for input 7168 I'm getting wrong answer.

```
    public int numSquares(int n) {
        Map<Integer, Integer> memo = new HashMap();
        List<Integer> list = fillSquares(n, memo);
        if (list == null)
            return 1;
        return helper(list.size()-1, list, n, memo);
    }

    private int helper(int index, List<Integer> list, int left, Map<Integer, Integer> memo) {

        if (left == 0)
            return 0;
        if (left < 0 || index < 0)
            return Integer.MAX_VALUE-1;

        if (memo.containsKey(left)) {
            return memo.get(left);
        }

        int d1 = 1+helper(index, list, left-list.get(index), memo);
        int d2 = 1+helper(index-1, list, left-list.get(index),  memo);
        int d3 = helper(index-1, list, left, memo);

        int d = Math.min(Math.min(d1,d2), d3);
        memo.put(left, d);
        return d;
    }

    private List<Integer> fillSquares(int n, Map<Integer, Integer> memo) {
        int curr = 1;
        List<Integer> list = new ArrayList();
        int d = (int)Math.pow(curr, 2);
        while (d < n) {
            list.add(d);
            memo.put(d, 1);
            curr++;
            d = (int)Math.pow(curr, 2);
        }
        if (d == n)
            return null;
        return list;
    }

```

I'm calling like this:

```
numSquares(7168)

```

All test cases pass (even complex cases), but this one fails. I suspect something is wrong with my memoization but cannot pinpoint what exactly. Any help will be appreciated.