一道动态规划 在线coding 题目



对动态规划一直不算很熟悉,最近面算法专家职位,不幸遇到了高维的动态规划相关题目,在线coding过程中没有把题目解出来。

测试结束后,辗转反侧,夜不能寐,还是想再试试看能否解出正确答案。

花了将近3小时,总算解出来了,虽然是答案是正确的,但还是有很多可以优化的地方,比如:其实DP方程是可以降维的。


下面把题目和代码放在文章里,欢迎大家一起讨论优化。


【题目:

现在有abcdef六种颜色的球,集齐其中满足一定组合的三个可以抽一次奖,这样的组合有:3a, 

  2a1b, 1a2b, 2a1d, 1a2d, 2a1c, 2a1e, f可以充当任何类型,给出六种球的个数,如何找出最大的抽奖次数。


我的代码如下:


import java.io.*;
import java.util.*;
import java.util.Map.Entry;

class Solution {
	
	/**
	 * 利用动态规划方法
	 * 【分析】:
	 * 想要中奖的话,在a方向上,上一步到这一步的步长可以有4种{0,1,2,3};
	 * 想要中奖的话,在b方向上,上一步到这一步的步长可以有3种{0,1,2};
	 * 想要中奖的话,在c方向上,上一步到这一步的步长可以有2种{0,1};
	 * 想要中奖的话,在d方向上,上一步到这一步的步长可以有3种{0,1,2};
	 * 想要中奖的话,在e方向上,上一步到这一步的步长可以有2种{0,1};
	 * 想要中奖的话,在e方向上,上一步到这一步的步长可以有4种{0,1,2,3};
	 */
	public static int getMaxValue(int[] num) {  
	    		    
	    int aN=num[0];//a的个数
	    int bN=num[1];
	    int cN=num[2];
	    int dN=num[3];
	    int eN=num[4];
	    int fN=num[5];
	    
	 
	    
	    HashMap maxTable=new HashMap();
	   
	    
	    for(int a=0;a<=aN;a++)
	     for(int b=0;b<=bN;b++)
	       for(int c=0;c<=cN;c++)
	    	 for(int d=0;d<=dN;d++)      
	    	  for(int e=0;e<=eN;e++)  
	    		for(int f=0;f<=fN;f++)
	    		{
	   		
	    			int max=0;
	    			int isBonus=0;
	   		
	    			//C方向 :2a1c
	    		if(c-1>=0){
	   			
	    		if(a+f-2>=0)
	    		{
	    			if(a-2>=0)
	    			{
	    			    String key=a-2+","+b+","+(c-1)+","+d+","+e+","+f;
	    										
	    			    if(maxTable.get(key)>max){
	    					max=maxTable.get(key);
	    											
	    				}
	    				isBonus=1;
	   					
	    			}
	    			if(f-2>=0)
	    			{
	    			    String key=a+","+b+","+(c-1)+","+d+","+e+","+(f-2);
	    				if(maxTable.get(key)>max){
	    					max=maxTable.get(key);
	    											
	    				}
	    				isBonus=1;
	   					
	    			}
	    			if((f-1>=0)&&(a-1>=0)){
	    			String key=(a-1)+","+b+","+(c-1)+","+d+","+e+","+(f-1);
	    			if(maxTable.get(key)>max){
	    					max=maxTable.get(key);
	    				}
	    			    isBonus=1;
	   					
	    				}
	    		}
	   			   						   			
	    	}
	    		//E方向: 2a1e
	    		if(e-1>=0){
	   			
	    		if(a+f-2>=0)
	    			{
	    			if(a-2>=0)
	    			{
	    				String key=a-2+","+b+","+c+","+d+","+(e-1)+","+f;
	    						if(maxTable.get(key)>max){
	    								max=maxTable.get(key);
	    											
	    				}
	    				isBonus=1;
	    										
	    			}
	    			if(f-2>=0)
	    			{
	    				String key=a+","+b+","+c+","+d+","+(e-1)+","+(f-2);
	    					if(maxTable.get(key)>max){
	    						max=maxTable.get(key);
	    				}
	    				isBonus=1;
	   					
	    				}
	    				if((f-1>=0)&&(a-1>=0)){
	    				String key=(a-1)+","+b+","+c+","+d+","+(e-1)+","+(f-1);
	    					if(maxTable.get(key)>max){
	    				max=maxTable.get(key);
	    				}
	    					isBonus=1;
	   					
	    					}
	    				}
	   			   					
	   			
	    			}
	   		
	    				//d方向:  1a2d
	    				if(d-2>=0){
	   			
	    				if(a+f-1>=0)
	    			{
	    			    if(a-1>=0)
	    				{
	    						String key=a-1+","+b+","+c+","+(d-2)+","+e+","+f;
	    						if(maxTable.get(key)>max){
	    							max=maxTable.get(key);
	    											
	    						}
	    						isBonus=1;
	   					
	    				}
	    				if(f-1>=0)
	    			    {
	    					String key=a+","+b+","+c+","+(d-2)+","+e+","+(f-1);
	    					if(maxTable.get(key)>max){
	    						max=maxTable.get(key);
	    											
	    					}
	    				    isBonus=1;
	   					
	    				}
	   				
	    		}
	   			   					
	   		
	    	}
	    	//d方向: 2a1d
	    	if(d-1>=0){
	    								
	    	if(a+f-2>=0)
	    	{
	    		if(a-2>=0)
	    		{
	    			String key=a-2+","+b+","+c+","+(d-1)+","+e+","+f;
	    						if(maxTable.get(key)>max){
	    							max=maxTable.get(key);
	    											
	    						}
	    				isBonus=1;
	   					
	    		}
	    		if(f-2>=0)
	    		{
	    			String key=a+","+b+","+c+","+(d-1)+","+e+","+(f-2);
	                	if(maxTable.get(key)>max){
	    					max=maxTable.get(key);
	    											
	    					}
	    				isBonus=1;
	   					
	    				}
	    			if((f-1>=0)&&(a-1>=0)){
	    			String key=(a-1)+","+b+","+c+","+(d-1)+","+e+","+(f-1);
	    						if(maxTable.get(key)>max){
	    						max=maxTable.get(key);
	    											
	    						}
	    				isBonus=1;
	   					
	    				}
	    			}
	   			   					
	    								
	    		}
	    		//b方向:   1a2b
	    		if(b-2>=0){
	   			
	    			if(a+f-1>=0)
	    			{
	    				if(a-1>=0)
	    				{
	    					String key=a-1+","+(b-2)+","+c+","+d+","+e+","+f;
	    						if(maxTable.get(key)>max){
	    							max=maxTable.get(key);
	    								
	    					        }
	    						isBonus=1;
	   					
	    					}
	    					if(f-1>=0)
	    			    	{
	    					String key=a+","+(b-2)+","+c+","+d+","+e+","+(f-1);
	    						if(maxTable.get(key)>max){
	    						max=maxTable.get(key);
	    											
	    					}
	    						isBonus=1;
	   					
	    			    }
	   				
	    			}
	   			   					
	   			
	    	}
	    	//b方向: 2a1b
	    	if(b-1>=0){
	   			
	    	    if(a+f-2>=0)
	    	{
	    		if(a-2>=0)
	    	    {
	    			String key=a-2+","+(b-1)+","+c+","+d+","+e+","+f;
	    					if(maxTable.get(key)>max){
	    					max=maxTable.get(key);
	    											
	    					}
	    				isBonus=1;
	    										
	    		}
	    		if(f-2>=0)
	    		{
	    			String key=a+","+(b-1)+","+c+","+d+","+e+","+(f-2);
	    				if(maxTable.get(key)>max){
	    					max=maxTable.get(key);
	    											
	    				}
	    				isBonus=1;
	   					
	    		}
	    		if((f-1>=0)&&(a-1>=0)){
	    			String key=(a-1)+","+(b-1)+","+c+","+d+","+e+","+(f-1);
	    				if(maxTable.get(key)>max){
	    					max=maxTable.get(key);
	    											
	    				}
	    			isBonus=1;
	   					
	    		}
	    	}
	   			
	    	}
	    	//a方向 3a
	    	if(a+f-3>=0){
	    		if(a-3>=0)
	        {
	    			String key=(a-3)+","+b+","+c+","+d+","+e+","+f;
	    				if(maxTable.get(key)>max){
    							max=maxTable.get(key);
    										
    				}
	    			isBonus=1;
   					
	    	}
	    	if((a-2>=0)&&(f-1>=0)){
	    			String key=(a-2)+","+b+","+c+","+d+","+e+","+(f-1);
	    				if(maxTable.get(key)>max){
    					max=maxTable.get(key);
    											
    					}
	    				isBonus=1;
   			}
	    	if((a-1>=0)&&(f-2>=0)){
	    			String key=(a-1)+","+b+","+c+","+d+","+e+","+(f-2);
	    				if(maxTable.get(key)>max){
    					max=maxTable.get(key);
    											
    			}
	    			    isBonus=1;
   							
	    	}
	    	if(f-3>=0){
	    			String key=a+","+b+","+c+","+d+","+e+","+(f-3);
	    				if(maxTable.get(key)>max){
    						max=maxTable.get(key);
    											
    					}	
	    				isBonus=1;
   					
	    	    }
	    	}
	   		
	   		
	    		String key=a+","+b+","+c+","+d+","+e+","+f;
	    		maxTable.put(key, max+isBonus);
	   		
	   	}   
	    
	    return maxTable.get(aN+","+bN+","+cN+","+dN+","+eN+","+fN);
			                 
    } 
	
	
		
	
	
  public static void main(String[] args) {
	  
	  int[] num=new int[]{7,4,2,2,1,8};
	  System.out.println(getMaxValue(num));
	  

  }
}

你可能感兴趣的:(算法)