an array is in ascending order and then descending in order to find the maximum value. For example, [1, 2, 2, 2, 2, 2, 2, 3, 1], using the optimal time complexity, the algorithm achieves the maximum value 3
.an array is in ascending order and then descending in order to find the maximum value. For example, [1, 2, 2, 2, 2, 2, 2, 3, 1], using the optimal time complexity, the algorithm achieves the maximum value 3
./**
*
* @param arr :
* @param low :
* @param high:
* @return :
*/
public static int getMax(int[] arr, int low, int high) {
while (low < high)
{
int mid = low + ((high - low) >> 1);
if (arr[mid] > arr[mid + 1]) {
high = mid;
}
else if (arr[mid] < arr[mid + 1]) {
low = mid + 1;
}
else //arr[mid]arr[mid+1]
{
if (mid-1 >= low) // [low~high]
{
if(arr[mid-1] > arr[mid])
{
high = mid-1;
}
else if(arr[mid-1] < arr[mid])
{
low = mid+1;
}
else //arr[mid-1] arr[mid] arr[mid-1]arr[mid],arr[mid+1]
//midmidmidmid
//
{
int one = getMax(arr, low, mid-1);
int two = getMax(arr, mid+1, high);
return one > two ? one : two;
}
}
else { // mid-1 < low midlowarr[low] == arr[high]
return arr[low];
}
}
}
return arr[low];
}
= update
Recursion and array copy are removed, and the return value of the function is changed to index to facilitate the handling of empty arrays
Thank you for the same problem
Previous: React dynamic brush elements
Next: Vue modifies the content of an input box and then clicks cancel to get the original content.
as in the question, 10 numbers are randomly selected from 1 to 16, and the 10 numbers cannot be the same. Want to detailed code and ideas, this part is really not good, thank you ...
whether the data transmitted by socket needs to be encrypted ...
the requirement is 010203 = > [01JEI 02jue 03] to write a regular "010203 ". Split (regular) = [01J 02pr 03] how do you write this? (0 [1-9] | (1 [0-2])) + g.test ( 0122 ) Why true is returned here. I want to match the number between 01 and 12....
I know that just because the socket send function returns successfully does not mean that the other party will receive it successfully. In general, we have to ask the other party to answer "copy " or not? Especially on the mobile end, the traffic on the...
hide the sensitive value in the native layer to prevent decompilation from being searched for by keywords. I use this method. charstringsrcstring -sharpinclude <stdio.h> int main() { char src[] = "ab"; int len = 0; ...
In the nginx server, after hearing an event in the socket, put the file descriptor in the queue and parse it. how to implement async in this process? where does the nginx server reflect async? there is a question I can t figure out all the time. Are ...
if there is an abc, there are 3! The order of is abc acb bac bca cab cba to any number, such as Numeric 4, output bca. now there are 26 English letters, a total of 26! Given any number N, how to find the alphabetic column? listen to others say tha...
determine whether ip is from Shanghai? Return Bool value is there any good implementation plan? ...
topic description Mathematical Combinatorial problems sources of topics and their own ideas competitive programs what result do you expect? What is the error message actually seen? I tried a way to solve this problem in O (n). But get the wron...
compiling openjdk under mac environment appears-sharpinclude < new > header file is missing environment xcode10, it is said that because libstdcPP was deleted later by xcode10,-sharpinclude < new > just need to copy Applications Xcode.app Contents D...
recently, I have done a project to read IC card information on the browser side. Now I have a customer s plug and play IC card reader. Shenzhen Minghua products have no clue. I don t know how to call the web end. Which god can give some advice ...
for example, the ip address of this machine is: 123.57.XX.XX. if you enter the domain name of ftp, the upload fails, and the error in the following figure is reported. what is the principle that you can succeed if you fill in localhost? $ftp_server =...
Please design 3 to start a program to find primes in ascending order, find 100 primes, and output numbers from large to small in comma-separated order. however, the program must meet the following conditions. do not use circular syntax such as For,Wh...