Skip to content

Commit 436a34e

Browse files
add 1287
1 parent 4c72eed commit 436a34e

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
1010
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
11+
|1287|[Element Appearing More Than 25% In Sorted Array](https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1287.java) | | | |Easy||
1112
|1282|[Group the People Given the Group Size They Belong To](https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1282.java) | | | |Medium||
1213
|1281|[Subtract the Product and Sum of Digits of an Integer](https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1281.java) | | | |Easy||
1314
|1277|[Count Square Submatrices with All Ones](https://leetcode.com/problems/count-square-submatrices-with-all-ones/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1277.java) | | | |Medium||
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 1287. Element Appearing More Than 25% In Sorted Array
5+
*
6+
* 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.
7+
* Return that integer.
8+
*
9+
* Example 1:
10+
* Input: arr = [1,2,2,6,6,6,6,7,10]
11+
* Output: 6
12+
*
13+
* Constraints:
14+
*
15+
* 1 <= arr.length <= 10^4
16+
* 0 <= arr[i] <= 10^5
17+
* */
18+
public class _1287 {
19+
public static class Solution1 {
20+
public int findSpecialInteger(int[] arr) {
21+
for (int i = 0; i < arr.length - 1; ) {
22+
int count = 1;
23+
if (arr[i] == arr[i + 1]) {
24+
do {
25+
i++;
26+
count++;
27+
if (count > arr.length / 4) {
28+
return arr[i];
29+
}
30+
} while (i < arr.length - 1 && arr[i] == arr[i + 1]);
31+
} else {
32+
i++;
33+
}
34+
}
35+
return arr[0];
36+
}
37+
}
38+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1287;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1287Test {
10+
private static _1287.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1287.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(6, solution1.findSpecialInteger(new int[]{1, 2, 2, 6, 6, 6, 6, 7, 10}));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(1, solution1.findSpecialInteger(new int[]{1}));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(3, solution1.findSpecialInteger(new int[]{1, 2, 3, 3}));
30+
}
31+
32+
}

0 commit comments

Comments
 (0)