Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2339293

Browse files
add 1150
1 parent 75c597a commit 2339293

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ _If you like this project, please leave me a star._ ★
2020
|1165|[Single-Row Keyboard](https://leetcode.com/problems/single-row-keyboard/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1165.java) | | | |Easy||
2121
|1160|[Find Words That Can Be Formed by Characters](https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1160.java) | O(n) | O(m) | |Easy||
2222
|1154|[Day of the Year](https://leetcode.com/problems/day-of-the-year/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1154.java) | O(1) | O(1) | |Easy||
23+
|1150|[Check If a Number Is Majority Element in a Sorted Array](https://leetcode.com/problems/check-if-a-number-is-majority-element-in-a-sorted-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1150.java) | | | |Easy||
2324
|1137|[N-th Tribonacci Number](https://leetcode.com/problems/n-th-tribonacci-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1137.java) | O(n) | O(n) | |Easy||
2425
|1134|[Armstrong Number](https://leetcode.com/problems/armstrong-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1134.java) | | | [:tv:](https://www.youtube.com/watch?v=HTL7fd4HPf4)|Easy||
2526
|1122|[Relative Sort Array](https://leetcode.com/problems/relative-sort-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1122.java) | O(n) | O(n) | |Easy||
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fishercoder.solutions;
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+
public class _1150 {
29+
public static class Solution1 {
30+
public boolean isMajorityElement(int[] nums, int target) {
31+
int left = 0;
32+
int right = nums.length - 1;
33+
int mid = left + (right - left) / 2;
34+
return nums[mid] == target;
35+
}
36+
}
37+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1150;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1150Test {
10+
private static _1150.Solution1 solution1;
11+
private static int[] nums;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1150.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
nums = new int[]{2, 4, 5, 5, 5, 5, 5, 6, 6};
21+
assertEquals(true, solution1.isMajorityElement(nums, 5));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
nums = new int[]{10, 100, 101, 101};
27+
assertEquals(false, solution1.isMajorityElement(nums, 101));
28+
}
29+
30+
}

0 commit comments

Comments
 (0)