I'm trying to solve a challenge and the function must return a long integer and takes an int and List<Long> as parameters but i keep getting the following error:
Solution.java:32: error: incompatible types: possible lossy conversion from long to int
if (i >= coin) dp[i] += dp[i - coin];
I've tried different casting but it all comes back to this. My code is below:
public static long count(int n, List<Long> c) {
long[] dp = new long[n + 1];
dp[0] = 1;
for (long coin : c) {
for (long i = 1; i <= n; i++){
if (i >= coin) dp[i] += dp[i - coin];
}
}
return dp[n];
}
}
Couple problems here:
ishould be anintand not alongsince it is the variable initialized in aforloop. If it was along, you'd get a type mismatch error.Since
cstores instances ofLong,coinshould also be aLongNow, since
iis anintandcoinis aLong, you need to cast the difference of the two to anintas well.