Larry's Array problem solving ideas and process

Source: Internet
Author: User

First throw the link Https://www.hackerrank.com/challenges/larrys-array

The first thought is: Find the minimum value from the sequence and determine the position of the minimum value.

1. If the position of the minimum value is in the first digit of the sequence, then in 1. N continues to find the location of the sub-value (which can be understood as the minimum value to remove the first, and then find the minimum value in the remaining series);

2. If the position of the minimum value is the second digit of the sequence, select 0: 33 Number of sub-series make a rotation to move the minimum value to the first place

3. If the position of the minimum value is after the second digit of the sequence, then the two numbers before the minimum value are selected together with the minimum value to make the sub-series two rotations move the minimum value to the first digit of the sub-column, and then continue the loop operation

4. When the length of the remaining series is less than 3, exit the loop and check whether the result sequence is in ascending order, output Yes, no output no

Input criteria for the topic:

First line: Number of test cases

Second line: Number of numbers in a series

Line three: Each number of the series, separated by a single space

The implementation code is as follows:

1 #rotate the list of 3 numbers 0<-1, 1<-2, 2<-02 defRotate (p_arr_sub):3Arr_sub_r =p_arr_sub4temp =Arr_sub_r[0]5Arr_sub_r[0] = arr_sub_r[1]6ARR_SUB_R[1] = arr_sub_r[2]7ARR_SUB_R[2] =Temp8     returnArr_sub_r9 Tent =int (input (). Strip ()) One  A  forA0inchRange (t): -n =int (input (). Strip ()) -arr = [Int (arr_temp) forArr_tempinchInput (). Strip (). Split (' ')] the  -     #start ordinal of the list -Begin_index =0 -  +      whileBegin_index < N-2: -         #gets the position ordinal of the minimum value in the list +Min_value_index =arr.index (Min (arr[begin_index:])) A         #The minimum position ordinal is the same as the starting point of the list, which means no rotation at         #list start position move back one -         ifMin_value_index = =Begin_index: -Begin_index + = 1 -         #The minimum position is one after the start number, and a rotation is required -         elifMin_value_index = = Begin_index + 1: -Arr_sub = Rotate (arr[begin_index:begin_index+3]) inArr[begin_index:begin_index + 3] =arr_sub -         #The minimum position is more than two digits after the start ordinal, and the two bits before the minimum value are combined with the minimum value to form a sub-list of length 3 for two rotations to         Else: +arr_sub = rotate (rotate (arr[min_value_index-2:min_value_index+1])) -ARR[MIN_VALUE_INDEX-2:MIN_VALUE_INDEX+1] =arr_sub the  *     ifSorted (arr) = =arr: $         Print("YES")Panax Notoginseng     Else: -         Print("NO")

The results are as follows:

A total of 20 test cases the first 10 are passed, the last 10 all time out, get the final case of the input and expected output in the local test results are correct, so the remaining thing is how to optimize the efficiency.

Re-analysis of the topic, the output requirement is yes or NO, so does not necessarily need to carry out the actual rotation operation, referring to Https://en.wikipedia.org/wiki/Parity_of_a_permutation's idea of the final Solution code is as follows:

1t =int (input (). Strip ())2 3  forA0inchRange (t):4n =int (input (). Strip ())5arr = [Int (arr_temp) forArr_tempinchInput (). Strip (). Split (' ')]6     7Inversions =08      forIinchRange (n-1):9          forJinchRange (i+1, N):Ten             ifArr[i] >Arr[j]: OneInversions + = 1 A      -     ifInversions% 2 = =0: -         Print("YES") the     Else: -         Print("NO")

Larry's Array problem solving ideas and process

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.