Wrong!!! why??? java


  • 0
    L

    //思路:先排序,然后从尾部逐次相加,若和超过S,跳出,并返回length
    pulic class MinimumSizeSubarraySum {
    public static int minSubArrayLen(int s, int[] nums) {
    if(nums.length==0||nums==null)
    return 0;

    	 int res=0;
    	 
    	 int tmp=0;
    	 Arrays.sort(nums);
    	 for(int i=nums.length-1;i>=0;){
    		 tmp+=nums[i];
    		 if(tmp>=s){
    			 res=nums.length-i;
    			 break;
    		 }
    		 else{
    			 i--;
    		 }
    	 }
    	
    
    	return res;
    

    }


  • 0

    The order of given array should not be changed.

    The minimum size subarray is based on the original array.

    So we can not sort this array first.


  • 0
    G

    我也过不去一个testcase。他的输出是132,我的是80。
    如果nlogn的话有两种方法。如果用java的话可以用treemap。for loop一次build treemap, for loop第二次不断减少s直到s<0。第二种就是先merge sort然后从最后开始找。但是不知道为啥不能通过.....个人感觉是如果能等于s的话,就要求等于s的count。否则的话就求大于s的和的count最小值.....个人估计


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.