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:
i
should be anint
and not along
since it is the variable initialized in afor
loop. If it was along
, you'd get a type mismatch error.Since
c
stores instances ofLong
,coin
should also be aLong
Now, since
i
is anint
andcoin
is aLong
, you need to cast the difference of the two to anint
as well.