The solution for Range Sum Query - Immutable appears to create an array/list of the prefix sum, but the question is actually asking for an integer that represents the sum between the left and right pointers. I passed this question without issue, but noticed my passing solution had a different return than the site solution
This is my solution FWIW:
public static int rangeSumQueryImmutable(List nums, int left, int right) {
HashMap<Integer, Integer> prefix = new HashMap<>();
prefix.put(0,0);
int sum = 0;
for (int i = 0; i < nums.size(); i++) {
sum += nums.get(i);
prefix.put(i + 1, sum);
}
return prefix.get(right + 1) - prefix.get(left);
}
The solution (java) on the site is this
public static int[] initSumArray(List<Integer> nums) {
int[] prefixSum = new int[nums.size() + 1];
for (int i = 0; i < nums.size(); i++) {
prefixSum[i + 1] = prefixSum[i] + nums.get(i);
}
return prefixSum;
}
Note that it’s returning int instead of int. Other languages look the same. The explenation looks right, it looks like the solution code is just for a different problem or just code for a generic prefix sum.