题目来源:
leetcode题目,网址:110. 平衡二叉树 - 力扣(LeetCode)
解题思路:
暴力遍历所有可能然后返回最大美味度或 -1 即可。
解题代码:
- class Solution {
- public int perfectMenu(int[] materials, int[][] cookbooks, int[][] attribute, int limit) {
- return assist(0,materials,cookbooks,attribute,limit,new int[2]);
- }
- public int assist(int startPos,int[] materials, int[][] cookbooks, int[][] attribute, int limit,int[] attNow){
- if(startPos==cookbooks.length){
- if(attNow[1]>=limit){
- return attNow[0]>0?attNow[0]:-1;
- }else{
- return -1;
- }
- }
- int res=-1;
- for(int i=startPos;i<cookbooks.length;i++){
- int[] thisAtt=new int[]{attNow[0],attNow[1]};
- boolean flag=false;//判断是否能够制作当前料理
- int[] leftover=new int[materials.length];
- for(int j=0;j<materials.length;j++){
- leftover[j]=materials[j]-cookbooks[i][j];
- if(leftover[j]<0){
- flag=true;;
- break;
- }
- }
- if(flag){
- int thisRes=assist(i+1,materials,cookbooks,attribute,limit,thisAtt);
- res=Math.max(res,thisRes);
-
- }else{
- thisAtt[0]+=attribute[i][0];
- thisAtt[1]+=attribute[i][1];
- int thisRes=assist(i+1,leftover,cookbooks,attribute,limit,thisAtt);
- res=Math.max(res,thisRes);
- }
- }
- return res;
- }
- }
总结:
无官方题解。