Lintcode to find the maximum mean sub-array

Source: Internet
Author: User
Tags array length lintcode

I have a deep understanding of this problem, in the prior description of the code at run time only 34% pass the test, and the wrong reason I attributed it to the system error


(。。。。。。。 )

The answer to my output is wrong ....

I used violence all over the calendar, and every one of them could be compared. Of course I looked at others and there was a dichotomy, but I didn't understand. My code is as follows:

Package com.test;
public class Zuidapingjunzishuzu {public
	static void Main (string[] args) {
		//int arr[] = {1,12,-5,-6,50,3};
		int arr[] = { -1,-2,-3,-100,-1,-50};
		Double A = Maxaverage (arr,4);
		System.out.println (a);
	}
	/**
	 * asks for a maximum subarray whose sub-arrays are of the length k
	 *
	/public static double Maxaverage (int[] nums, int k) {
		
		int len = nums.length;	//Array length
		int count = 1;	Array pointer
		double maxsum = 0;	The AND/* * of the maximum subarray
		 first assigns the sum of the first k numbers to Maxsun */for
		(int i = 0; i < K; i++) {
			maxsum + nums[i];
		}
		 /* * Main algorithm *
		/while (Count <= len-k) {
			//temp resets every cycle, so create a
			double temp = 0 here;
			Sum each k number from the second number and assign to Temo for
			(int j = 0; J < K; J + +) {
				temp + = Nums[count+j];
			}
			Compare the values of temp and maxsun, remembering that each counter moves backwards one
			if (temp > Maxsum) {
				maxsum = temp;
				count++;
			} else{
				count++;
			}
		}
		return maxsum/k;
    }
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.