|
1 | 1 | packagecom.fishercoder.solutions;
|
2 | 2 |
|
3 |
| -/** |
4 |
| - * 1150. Check If a Number Is Majority Element in a Sorted Array |
5 |
| - * |
6 |
| - * Given an array nums sorted in non-decreasing order, and a number target, return True if and only if target is a majority element. |
7 |
| - * A majority element is an element that appears more than N/2 times in an array of length N. |
8 |
| - * |
9 |
| - * Example 1: |
10 |
| - * Input: nums = [2,4,5,5,5,5,5,6,6], target = 5 |
11 |
| - * Output: true |
12 |
| - * Explanation: |
13 |
| - * The value 5 appears 5 times and the length of the array is 9. |
14 |
| - * Thus, 5 is a majority element because 5 > 9/2 is true. |
15 |
| - * |
16 |
| - * Example 2: |
17 |
| - * Input: nums = [10,100,101,101], target = 101 |
18 |
| - * Output: false |
19 |
| - * Explanation: |
20 |
| - * The value 101 appears 2 times and the length of the array is 4. |
21 |
| - * Thus, 101 is not a majority element because 2 > 4/2 is false. |
22 |
| - * |
23 |
| - * Note: |
24 |
| - * 1 <= nums.length <= 1000 |
25 |
| - * 1 <= nums[i] <= 10^9 |
26 |
| - * 1 <= target <= 10^9 |
27 |
| - **/ |
28 | 3 | publicclass_1150 {
|
29 | 4 | publicstaticclassSolution1 {
|
30 |
| -/**credit: https://leetcode.com/problems/check-if-a-number-is-majority-element-in-a-sorted-array/discuss/358130/Java-just-one-binary-search-O(logN))-0ms-beats-100*/ |
| 5 | +/** |
| 6 | + * credit: https://leetcode.com/problems/check-if-a-number-is-majority-element-in-a-sorted-array/discuss/358130/Java-just-one-binary-search-O(logN))-0ms-beats-100 |
| 7 | + */ |
31 | 8 | publicbooleanisMajorityElement(int[]nums,inttarget) {
|
32 | 9 | intfirstIndex =findFirstOccur(nums,target);
|
33 | 10 | intplusHalfIndex =firstIndex +nums.length /2;
|
|