分数组合数量,动态规划辅助,(摘自《编程面试元素》Java 版本)

huangapple go评论62阅读模式
英文:

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&lt;Integer&gt; plays) {
        Collections.sort(plays);
        HashMap&lt;Integer, Integer&gt; map = new HashMap&lt;&gt;(); //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&lt;Integer&gt; plays, int i, HashMap&lt;Integer, Integer&gt; map) {
        if (target &lt; 0 || i &lt; 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&lt;Integer&gt; points = new ArrayList&lt;&gt;(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 size target + 1
  • for each point in points update dp
import java.util.List;
import java.util.Arrays;

public class Main
{   
    static int noOfWays(List&lt;Integer&gt; points, int target) {
        
        int[] dp = new int[target + 1];
        dp[0] = 1;
        for(int point: points)
            for(int i = point; i &lt;= target; i++)
                dp[i] += dp[i - point];

        return dp[target];
    }

    public static void main(String[] args) {
        List&lt;Integer&gt; points = Arrays.asList(1, 3);
        int target = 6;
        System.out.println(noOfWays(points, target));
    }
}

huangapple
  • 本文由 发表于 2020年10月18日 18:11:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/64412091.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定