A peak element is an element that is greater than its neighbors.
Given an input array nums, where nums[i] ≠ nums[i+1], find a peak element and return its index.
The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.
You may imagine that nums[-1] = nums[n] = -∞.
Example 1:
Input:nums = [1,2,3,1]Output: 2
Explanation: 3 is a peak element and your function should return the index number 2.
Example 2:
Input:nums = [1,2,1,3,5,6,4]
Output: 1 or 5
Explanation: Your function can return either index number 1 where the peak element is 2, or index number 5 where the peak element is 6.
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time.
Return that integer.
Example 1:
1
2
<strong>Input:</strong>arr=[1,2,2,6,6,6,6,7,10]
<strong>Output:</strong>6
Constraints:
1 <= arr.length <= 10^4
0 <= arr[i] <= 10^5
Solution 1: Linear Scan
if arr[i] == arr[i + len/4] => arr[i] is the special integer.
Time complexity: O(n) Space complexity: O(1)
C++
1
2
3
4
5
6
7
8
9
10
// Author: Huahua
classSolution{
public:
intfindSpecialInteger(vector<int>& arr) {
int s = arr.size() / 4;
for(inti=0;i+s<arr.size();++i)
if(arr[i]==arr[i+s])returnarr[i];
return-1;
}
};
Solution 2: Binary Search
The answer must be one of (s[0], s[l/4], s[l/2], s[l*3/4]) Using binary search to find the range of each number, the one has more than 1/4 of total elements is the answer.
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold.
Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5).
It is guaranteed that there will be an answer.
Example 1:
Input: nums = [1,2,5,9], threshold = 6
Output: 5
Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1.
If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2).
Given an array of strings products and a string searchWord. We want to design a system that suggests at most three product names from products after each character of searchWord is typed. Suggested products should have common prefix with the searchWord. If there are more than three products with a common prefix return the three lexicographically minimums products.
Return list of lists of the suggested products after each character of searchWord is typed.
Example 1:
Input: products = ["mobile","mouse","moneypot","monitor","mousepad"], searchWord = "mouse"
Output: [
["mobile","moneypot","monitor"],
["mobile","moneypot","monitor"],
["mouse","mousepad"],
["mouse","mousepad"],
["mouse","mousepad"]
]
Explanation: products sorted lexicographically = ["mobile","moneypot","monitor","mouse","mousepad"]
After typing m and mo all products match and we show user ["mobile","moneypot","monitor"]
After typing mou, mous and mouse the system suggests ["mouse","mousepad"]