搜狗第三题吃糖代码

暴力解天下无敌

虽然我只做了这一个
前两个题看着就怕
。。


思路:
小明和小红都想要赢,所以他们每次都会选当下最多的糖盒,一开始的时候小明会选全局最大的,然后就有了断点,以后就从每一次的断点的两边取糖盒,所以比较的就是左和右的大小,每一次取走糖盒,就把环重新排成新的环,然后再进行此过程,直到没有糖盒。
最后比较小明和小红拿的糖的大小,计算差值,就好啦
package test;

import java.util.Scanner;

public class Sougou{
    
    public static int solution(int [] array){
        
        int xiaoming=0;
        int xiaohong=0;
        
        int cutIndex=max(array);   //定义切点,首先取第一个切点(小明先拿)。
        
        xiaoming=xiaoming+array[cutIndex];
        
        array[cutIndex]=0;    //先选掉最大的那盒,变成环状。
        
        array=transform(array);   //transform方法是每次拿走一个糖盒之后,把数组重新排成新的数组。可以理解为形成新的环。
        
        int num=array.length;
        for(int i=0,j=1;i<num;i++,j++){    //num是一共还有多少个糖盒,就循环取多少次
            int maxIndex;//最大值的下标
            
            if(cutIndex==array.length) {                     //最大值下标如果是新数组长度,例如新数组长度是4,下标有0123,maxIndex是4,说明上一次取走的糖盒是4,这种情况的左和右是array.length-1和0;             
                 maxIndex=maxLeftOrRight(array,array.length-1,0);
            }else if(cutIndex==0){                              //最大值下标如果是0,那么说明上次取走的是数组第一个,那么数组重新组合后,曾经在1位置的数左移到了0位置,那左和右分别是array.length-1(数组最后一个数)和cutIndex=0;
                maxIndex=maxLeftOrRight(array,array.length-1,cutIndex);
            }else if(cutIndex==array.length-1){                //最大值下标如果是最后一个数,说明上一次取走的是数组倒数第二个数,那左和右就分别为cutIndex-1和cutIndex。
                maxIndex=maxLeftOrRight(array,cutIndex-1,cutIndex);
            }else{                                            //其他情况,表示的就是在中间那些被取走的糖盒,他们不属于特殊情况,只需要取左AND右就行。
                maxIndex=maxLeftOrRight(array,cutIndex-1,cutIndex);
            }
            
            if(j%2==1){                                            //定义一个J值,从1开始,因为小明和小红循环取糖盒,所以循环每人加数字。
                xiaohong=xiaohong+array[maxIndex];
                array[maxIndex]=0;
                cutIndex=maxIndex;
                array=transform(array);
            }else if(j%2==0){
                xiaoming=xiaoming+array[maxIndex];
                array[maxIndex]=0;
                cutIndex=maxIndex;
                array=transform(array);
            }
            
        }
        
        int result=xiaoming>xiaohong?xiaoming-xiaohong:xiaohong-xiaoming;
        
        return result;
    }
    
    public static int [] transform(int [] array){      //每次取值后,因为被取走的位置是0,所以应该把数组转换为新的环,去掉0值,就是这个过程。
        int []  temp=new int [array.length];
        for(int i=0,j=0;i<array.length;i++){
            if(array[i]!=0){
                temp[j]=array[i];
                j++;
            }
        }
          int count=0;
        for(int i=0;i<temp.length;i++){
            if(temp[i]!=0){
                count++;
            }
        }
        int [] newArray=new int [count];
        for(int i=0;i<count;i++){
            newArray[i]=temp[i];
        }
        
        return newArray;
    }
    
    public static int max(int [] array){    //由于小明和小红都想赢,所以在最开始小明肯定选最大的,这个Max是选最大的过程。
        int index=0;
        for(int i=0;i<array.length;i++){
            index=array[i]>array[index]?i:index;
        }
        return index;
    }
    
    public static int maxLeftOrRight(int [] array ,int left,int right){    //在左和右中选最大的过程
        return array[left]>array[right]?left:right;
    }
    
    public static void main(String[] args){
        
        Scanner sc=new Scanner(System.in);
        
        int n=sc.nextInt();
        
        int [] array= new int [n];
        
        for(int i=0;i<array.length;i++){
            array[i]=sc.nextInt();
        }
        
        int result=solution(array);
        System.out.println(result);
    }
}
        

#搜狗##笔试题目#
全部评论
然鹅Python暴力解就超时,还得搞hashmap
点赞 回复
分享
发布于 2018-09-14 18:15
大佬能讲下思路吗?代码太多了😄
点赞 回复
分享
发布于 2018-09-14 18:16
联易融
校招火热招聘中
官网直投
第一次拿最大的就一定最大吗。。
点赞 回复
分享
发布于 2018-09-14 18:37
为什么一定要拿最大的...
点赞 回复
分享
发布于 2018-09-14 19:55
3道只会这一个。。用的线性规划
点赞 回复
分享
发布于 2018-09-14 20:27
哈哈,兄弟,我们算法 一样,不过我用的c语言
点赞 回复
分享
发布于 2018-09-15 08:02

相关推荐

4 收藏 评论
分享
牛客网
牛客企业服务