An algorithm question written by Alibaba and an algorithm question written by Alibaba

Source: Internet
Author: User

An algorithm question written by Alibaba and an algorithm question written by Alibaba
Question: to obtain the optimal beat mode of a positive integer array, the requirements are as follows: 1) jump from any position in the middle of the array to the right, the number of hops per time cannot exceed the value of the corresponding element at this position. 2) the following steps are calculated when the number of beats is the least. java is used for implementation ~

Package test;/*** is a static tool class, for specific functions, see Method * @ author ZOUHENG */public class MinStepToArrayEnd {/*** to return an optimal position based on the beating range of the current position, this position enables two consecutive jumps to obtain the longest distance. If the current position can jump out of the array range directly, the most position is the current position. * The default parameter passed in here has been verified, meet requirements ** @ param target * target array * @ param currentLocation * Current position. The corresponding elements are arranged from left to right. The first element is 1, the last element is the array length * @ return the best position */private static int getBestLocation (int [] target, int currentLocation) {// initialize the farthest position of the second hop, it can be out of the array Range int maxLoca Tion = currentLocation + target [currentLocation-1]; // initialize the current position int bestLocation = currentLocation; // traverses the elements overwritten by the current position, obtain the farthest and optimal for (int I = 0; I <= target [currentLocation-1]; I ++) {int temp = target [(I + currentLocation)-1] + (I + currentLocation); if (temp> = target. length) {return I + currentLocation;} if (temp> maxLocation) {maxLocation = temp; bestLocation = I + currentlow.o N ;}} return bestLocation ;} /*** calculate the number of moves based on the best position and current position. ** @ param target * @ param currentLocation * @ return */private static int getJumpSteps (int [] target, int currentLocation) {int bestLocation = getBestLocation (target, currentLocation); // if the best position is the same as the current position, you can directly jump from the current position to the end, therefore, directly return the remaining steps if (bestLocation = currentLocation) {return target. length-currentLocation;} // if not, return the difference between the best position and the current position. retur N bestLocation-currentLocation;}/*** obtain the optimal beat mode of an array of positive integers. The following requirements apply: * Jump right from any position in the middle of the array, the number of hops per time cannot exceed the value of the corresponding element of the position * calculate the stride of each beat with the least number of beats ** @ param target * @ param currentLocation * @ return *@ throws Exception **/public static String getStepsResult (int [] target, int currentLocation) throws Exception {if (null = target | target. length = 0) throw new CheckParamException ("the array parameter is null or not initialized. Please check..."); for (I Nt I: target) if (I <1) throw new CheckParamException ("array member contains smaller than non-positive integer"); if (currentLocation <1 | currentLocation> target. length) throw new CheckParamException ("the start position parameter is incorrect. It must be greater than or equal to 1 and less than or equal to" + target. length); String result = ""; int jumpSteps = getJumpSteps (target, currentLocation); while (jumpSteps + currentLocation! = Target. length) {result + = jumpSteps + ","; currentLocation = getBestLocation (target, currentLocation); jumpSteps = getJumpSteps (target, currentLocation);} return result + jumpSteps ;} /*** test */public static void main (String [] args) throws Exception {int a [] = {4, 1, 5, 1, 3, 2, 1, 3, 1, 2, 3, 1, 1}; System. out. println (getStepsResult (a, 1 ));} /*** custom parameter check Exception ** @ author ZOUHENG */private static class CheckParamException extends Exception {private static final long serialVersionUID =-5470930382435803070L; public CheckParamException (String message) {super (message );}}}

 

Related Article

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.