题解 | #获得最多的奖金#

获得最多的奖金

https://www.nowcoder.com/practice/247f7bd088764aefa7474cff27489095

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        sc.nextLine();
        int[] nums=new int[n];
        for(int i=0;i<n;i++){
            nums[i]=sc.nextInt();
        }
        int left=0,right=n-1;
        long lo=nums[0],hi=nums[n-1],res=0;;
        while(left<right){
            if(lo<hi){
                left++;
                lo+=nums[left];
                
            }
            else if(lo>hi){
                right--;
                hi+=nums[right];
                
            }
            else{
                if(res<lo)
                    res=lo;
                left++;
                lo+=nums[left];
                right--;
                hi+=nums[right];
            }
        }
        System.out.println(res);
    }
    
双指针方法
全部评论

相关推荐

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