Hi I am new to Data Structure and I have some problem understanding the line "ds.remove(ds.size()-1);". Could anyone please help me out here?
class Solution {
private void findCombinations(int ind,int[] arr,int target,List<List<Integer>> ans, List<Integer> ds)
{
if(ind == arr.length)
{
if(target == 0)
ans.add(new ArrayList<>(ds));
return;
}
if(arr[ind] <= target)
{
ds.add(arr[ind]);
findCombinations(ind, arr, target - arr[ind], ans, ds);
ds.remove(ds.size() - 1);
}
findCombinations(ind + 1, arr, target, ans, ds);
}
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> ans = new ArrayList<>();
findCombinations(0, candidates, target, ans, new ArrayList<>());
return ans;
}
}
With this line you are deleting the last element inside the array. ds.size() is the number of elements inside the array, for example there are 2 elements, if you want to get the index of the last element you have to say ds.size() - 1 since the first elements has an index of 0 and the second one index of 1