VMware Coding Challenge: The Heist

类似BackpackII问题

 1     static int maximize_loot(int[] gold, int[] silver) {
 2         int[][] res = new int[gold.length+silver.length+1][10001];
 3         res[0][0] = 0;
 4         for (int i=1; i<=gold.length; i++) {
 5             for (int j=0; j<=10000; j++) {
 6                 res[i][j] = Math.max(res[i-1][j], (j>=gold[i-1]? res[i-1][j-gold[i-1]]+10*gold[i-1] : 0));
 7             }
 8         }
 9         for (int i=1; i<=silver.length; i++) {
10             for (int j=0; j<=10000; j++) {
11                 res[i+gold.length][j] = Math.max(res[i+gold.length-1][j], (j>=silver[i-1]? res[i+gold.length-1][j-silver[i-1]]+1*silver[i-1] : 0));
12             }
13         }
14         int maxVal = 0;
15         for (int k=0; k<=10000; k++) {
16             maxVal = Math.max(maxVal, res[gold.length+silver.length][k]);
17         }
18         return maxVal;
19
20     }
时间: 2024-08-03 08:12:50

VMware Coding Challenge: The Heist的相关文章

VMware coding Challenge

思路:这道题要观察,举个例子,1 2 * * 3 * 4 * 5 * * 6 7 * 8 * *, 用Stack,先序遍历,遇到数字就入栈,如果遇到 * *,说明栈顶节点是叶子节点,一条根到叶子的路径这时候就存在于栈之中,只要计算栈的size(),就知道当前这条路径的深度,树的height就是这些深度的最大值. 空格的引入增添了不少判断上的麻烦, 比如: ab * *, 这棵树的height是1 而不是2. 在遍历节点的时候需要注意取空格之前的所有元素一起看

VMware coding Challenge: Coin Toss Betting

1 static int CoinTossEndAmount(int betAmount, String coinTossResults) { 2 if (betAmount <=0 || coinTossResults.length() == 0) return betAmount; 3 long Amount = betAmount; 4 long onebet = 1; 5 for (int i=0; i<coinTossResults.length(); i++) { 6 if (co

VMware Coding Challenge: Removing Duplicates Entries

1 static LinkedListNode removeDuplicates(LinkedListNode list) { 2 LinkedListNode cur = list; 3 HashSet<Integer> set = new HashSet<Integer>(); 4 set.add(list.val); 5 while (cur.next != null) { 6 if (!set.contains(cur.next.val)) { 7 set.add(cur.

VMware Coding Challenge: Possible Scores &amp;&amp; Summary: static

Combination Sum I 那道题的变体 1 /* 2 * Complete the function below. 3 */ 4 5 static int is_score_possible(int score, int[] increments) { 6 Arrays.sort(increments); 7 ArrayList<Integer> res = new ArrayList<Integer>(); 8 res.add(0); 9 helper(res, inc

VMware coding Challenge:Date of Weekday

这道题再次证明了这种细节的题目,画个图容易搞清楚 1 import java.util.Scanner; 2 3 4 public class Solution2 { 5 static int DateOfWeekday(int date, int weekday) { 6 int cur = date%7; 7 int res = 0; 8 int dif = 0; 9 if (weekday > 0) { 10 dif = 7*((weekday-1)/7) + (weekday%7-cur

一些算法刷题的网站

1. leetcode http://leetcode.com/ 2. careerup http://www.careercup.com/ http://hawstein.com/posts/ctci-solutions-contents.html 3. glassdoor http://www.glassdoor.com/index.htm 4. topcoder http://www.topcoder.com/ 5. zoj http://acm.zju.edu.cn/onlinejudg

[转帖] 一些算法刷题的网站

1. leetcode http://leetcode.com/ 2. careerup http://www.careercup.com/ http://hawstein.com/posts/ctci-solutions-contents.html 3. glassdoor http://www.glassdoor.com/index.htm 4. topcoder http://www.topcoder.com/ 5. zoj http://acm.zju.edu.cn/onlinejudg

刷算法题网站

1. leetcode http://leetcode.com/ 2. careerup http://www.careercup.com/ http://hawstein.com/posts/ctci-solutions-contents.html 3. glassdoor http://www.glassdoor.com/index.htm 4. topcoder http://www.topcoder.com/ 5. zoj http://acm.zju.edu.cn/onlinejudg

Why coding like This ------ 递归以及枚举中的递归

title: "Why coding like This -- 递归以及枚举中的递归" date: 2015-08-28 21:34:16 categories: "why coding like this" tags: [swift进阶] Topic 1: 输入一个数组xs:[Int],对全体元素求和. Discuss 思路一: Hey,伙计,遍历数组,逐个相加,so easy! code: func sum1(xs:[Int])->Int{ var sum