0

I've been trying to work on my Java skills, and I'm currently working on a LeetCode problem. (link: https://leetcode.com/problems/minimum-path-sum/)

I have my solution below, the error I'm getting is "Line 16: error: unexpected type". I have no clue what I'm doing wrong. Any help would be much appreciated.

public class Solution {

public int minPathSum(int[][] grid) {
    if (grid.length == 0) { return 0; }
    if (grid[0].length == 0) { return 0; }
    int m = grid.length;
    int n = grid[0].length;
    HashMap<int[], int> memoize = new HashMap<>();
    return minPathSumHelper(memoize, grid, 0, 0, m, n);
}


public int minPathSumHelper(HashMap<int[], int> memoize, int[][] grid, int mIndex, int nIndex, int m, int n) {
    int[] coordinatesBottom = {mIndex+1,nIndex};
    int[] coordinatesRight = {mIndex,nIndex+1};

    if (m==mIndex && n==nIndex) {
        return grid[m][n];
    } else if (m==mIndex) {
        int minPathRight = memoize.containsKey(coordinatesRight) ? memoize.get(coordinatesRight) : minPathSumHelper(memoize,grid,mIndex,nIndex+1,m,n);
        int sum = grid[mIndex][nIndex] + minPathBottom;
        memoize.put(coordinates, sum);
        return sum;
    } else if (n==nIndex) {
        int minPathBottom = memoize.containsKey(coordinatesBottom) ? memoize.get(coordinatesBottom) : minPathSumHelper(memoize,grid,mIndex+1,nIndex,m,n);
        int sum = grid[mIndex][nIndex] + minPathBottom;
        memoize.put(coordinates, sum);
        return sum;
    }

    int minPathBottom = memoize.containsKey(coordinatesBottom) ? memoize.get(coordinatesBottom) : minPathSumHelper(memoize,grid,mIndex+1,nIndex,m,n);
    int minPathRight = memoize.containsKey(coordinatesRight) ? memoize.get(coordinatesRight) : minPathSumHelper(memoize,grid,mIndex,nIndex+1,m,n);
    int sum = grid[mIndex][nIndex]+Math.min(minPathBottom,minPathRight);

    int[] coordinates = {mIndex,nIndex};
    memoize.put(coordinates, sum);

    return sum;
}

}

  • 1
    `HashMap` won't work. You can't use *primitive types* as generic type arguments. I would suggest creating a `Coordinate` class for the key (must implement `equals()` and `hashCode()`), and use `Integer` for the value. – Andreas Aug 27 '16 at 04:21
  • Ahh, I see now. Thank you! – mr krabs Aug 27 '16 at 04:22

0 Answers0