8000 update 162 · githubniraj/Leetcode@9d1d91f · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9d1d91f

Browse files
update 162
1 parent bf9fdd0 commit 9d1d91f

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

src/main/java/com/fishercoder/solutions/_162.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,31 @@ public class _162 {
44

55
public static class Solution1 {
66
/**
7-
* credit: https://discuss.leetcode.com/topic/29329/java-solution-and-explanation-using-invariants
8-
* <p>
9-
* Basically, we need to keep this invariant:
10-
* nums[left] > nums[left-1], then we could return left as the result
11-
* or nums[right] > nums[right+1], then we could return right as the result
12-
* <p>
7+
* credit: https://leetcode.com/problems/find-peak-element/solutions/1290642/intuition-behind-conditions-complete-explanation-diagram-binary-search/
138
* Time: O(logn)
9+
* <p>
10+
* draw three cases with three examples, it's pretty self-explanatory
1411
*/
1512
public int findPeakElement(int[] nums) {
16-
if (nums == null || nums.length == 0) {
13+
if (nums == null || nums.length <= 1 || nums[0] > nums[1]) {
1714
return 0;
1815
}
19-
int left = 0;
20-
int right = nums.length - 1;
21-
while (left + 1 < right) {
16+
if (nums[nums.length - 1] > nums[nums.length - 2]) {
17+
return nums.length - 1;
18+
}
19+
int left = 1;
20+
int right = nums.length - 2;
21+
while (left <= right) {
2222
int mid = left + (right - left) / 2;
23-
if (nums[mid] < nums[mid + 1]) {
24-
left = mid;
25-
} else {
26-
right = mid;
23+
if (nums[mid] > nums[mid - 1] && nums[mid] > nums[mid + 1]) {
24+
return mid;
25+
} else if (nums[mid] < nums[mid - 1]) {
26+
right = mid - 1;
27+
} else if (nums[mid] < nums[mid + 1]) {
28+
left = mid + 1;
2729
}
2830
}
29-
return (left == nums.length - 1 || nums[left] > nums[left + 1]) ? left : right;
31+
return -1;
3032
}
3133
}
3234

src/test/java/com/fishercoder/_162Test.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package com.fishercoder;
22

33
import com.fishercoder.solutions._162;
4-
import org.junit.BeforeClass;
5-
import org.junit.Test;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
66

7-
import static org.junit.Assert.assertEquals;
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
88

99
public class _162Test {
1010
private static _162.Solution1 solution1;
1111
private static _162.Solution2 solution2;
1212
private static int[] nums;
1313

14-
@BeforeClass
15-
public static void setup() {
14+
@BeforeEach
15+
public void setup() {
1616
solution1 = new _162.Solution1();
1717
solution2 = new _162.Solution2();
1818
}

0 commit comments

Comments
 (0)
0