英文:
Number of score combinations, dynamic programming help, ( from elements of programming interviews, java)
问题
import java.util.*;
public class Main {
public static int combinations(int target, List<Integer> plays) {
Collections.sort(plays);
HashMap<Integer, Integer> map = new HashMap<>(); //map score to combinations.
map.put(0, 1);
combHelper(target, plays, plays.size() - 1, map);
return map.get(target);
}
private static int combHelper(int target, List<Integer> plays, int i, HashMap<Integer, Integer> map) {
if (target < 0 || i < 0) {
return 0;
}
if (!map.containsKey(target)) {
int out = combHelper(target, plays, i - 1, map) + combHelper(target - plays.get(i), plays, i, map);
map.put(target, out);
}
return map.get(target);
}
public static void main(String[] args) {
List<Integer> points = new ArrayList<>(Arrays.asList(3, 1));
System.out.println(combinations(6, points)); //output is 2
}
}
英文:
Given scores for individual plays and a final target score, the objective is to compute the number of possible combinations. (for example, if the target score is 6 and play scores are <1,3>, there are 3 ways to reach the target score - 3x2, 3x1 + 1x3, 1x6)
I can't see why my code is wrong:
import java.util.*;
public class Main
{
public static int combinations(int target, List<Integer> plays) {
Collections.sort(plays);
HashMap<Integer, Integer> map = new HashMap<>(); //map score to combinations.
map.put(0,1);
combHelper(target,plays,plays.size() -1, map);
return map.get(target);
}
private static int combHelper(int target, List<Integer> plays, int i, HashMap<Integer, Integer> map) {
if (target < 0 || i < 0) {
return 0;
}
if (!map.containsKey(target)) {
int out = combHelper(target,plays,i - 1, map) + combHelper(target - plays.get(i),plays,i,map);
map.put(target,out);
}
return map.get(target);
}
public static void main(String[] args) {
List<Integer> points = new ArrayList<>(Arrays.asList(3,1));
System.out.println(combinations(6,points)); //output is 2
}
}
any help and feedback would be appreciated!
答案1
得分: 0
我不知道你的代码有什么问题,但它看起来很难理解。
有一种更简单更容易的方法可以在不使用递归的情况下解决这个问题。
步骤:
- 创建一个大小为
target + 1
的数组dp
。 - 对于
points
中的每个点,更新dp
数组。
import java.util.List;
import java.util.Arrays;
public class Main
{
static int noOfWays(List<Integer> points, int target) {
int[] dp = new int[target + 1];
dp[0] = 1;
for(int point: points)
for(int i = point; i <= target; i++)
dp[i] += dp[i - point];
return dp[target];
}
public static void main(String[] args) {
List<Integer> points = Arrays.asList(1, 3);
int target = 6;
System.out.println(noOfWays(points, target));
}
}
英文:
I don't know what is wrong with your code, but it looks difficult to understand.
There is a simpler & easier we to solve this without recursion.
Steps:
- create an array
dp
with sizetarget + 1
- for each point in
points
updatedp
import java.util.List;
import java.util.Arrays;
public class Main
{
static int noOfWays(List<Integer> points, int target) {
int[] dp = new int[target + 1];
dp[0] = 1;
for(int point: points)
for(int i = point; i <= target; i++)
dp[i] += dp[i - point];
return dp[target];
}
public static void main(String[] args) {
List<Integer> points = Arrays.asList(1, 3);
int target = 6;
System.out.println(noOfWays(points, target));
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论