Skip to content

Commit 2231165

Browse files
refactor 162
1 parent a79cb97 commit 2231165

File tree

2 files changed

+102
-50
lines changed

2 files changed

+102
-50
lines changed

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

Lines changed: 62 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,71 +2,83 @@
22

33
/**
44
* 162. Find Peak Element
5-
*
6-
* A peak element is an element that is greater than its neighbors.
7-
Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.
5+
6+
A peak element is an element that is greater than its neighbors.
7+
8+
Given an input array nums, where nums[i] ≠ nums[i+1], find a peak element and return its index.
9+
810
The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.
9-
You may imagine that num[-1] = num[n] = -∞.
10-
For example, in array [1, 2, 3, 1], 3 is a peak element and your function should return the index number 2.*/
11+
12+
You may imagine that nums[-1] = nums[n] = -∞.
13+
14+
Example 1:
15+
Input: nums = [1,2,3,1]
16+
Output: 2
17+
Explanation: 3 is a peak element and your function should return the index number 2.
18+
19+
Example 2:
20+
Input: nums = [1,2,1,3,5,6,4]
21+
Output: 1 or 5
22+
Explanation: Your function can return either index number 1 where the peak element is 2,
23+
or index number 5 where the peak element is 6.
24+
25+
Note:
26+
Your solution should be in logarithmic complexity.
27+
*/
1128

1229
public class _162 {
1330

31+
public static class Solution1 {
1432
/**
15-
* On discuss, this post has very good explanation about an O(logn) solution:
16-
* https://discuss.leetcode.com/topic/29329/java-solution-and-explanation-using-invariants
17-
*
33+
* credit: https://discuss.leetcode.com/topic/29329/java-solution-and-explanation-using-invariants
34+
*
1835
* Basically, we need to keep this invariant:
1936
* nums[left] > nums[left-1], then we could return left as the result
2037
* or nums[right] > nums[right+1], then we could return right as the result
38+
*
39+
* Time: O(Ologn)
2140
*/
22-
public static int findPeakElement_Ologn(int[] nums) {
23-
24-
if (nums == null || nums.length == 0) {
25-
return 0;
26-
}
27-
int left = 0;
28-
int right = nums.length - 1;
29-
while (left + 1 < right) {
30-
int mid = left + (right - left) / 2;
31-
if (nums[mid] < nums[mid + 1]) {
32-
left = mid;
33-
} else {
34-
right = mid;
35-
}
41+
public int findPeakElement(int[] nums) {
42+
if (nums == null || nums.length == 0) {
43+
return 0;
44+
}
45+
int left = 0;
46+
int right = nums.length - 1;
47+
while (left + 1 < right) {
48+
int mid = left + (right - left) / 2;
49+
if (nums[mid] < nums[mid + 1]) {
50+
left = mid;
51+
} else {
52+
right = mid;
3653
}
37-
return (left == nums.length - 1 || nums[left] > nums[left + 1]) ? left : right;
38-
39-
}
40-
41-
public static void main(String... strings) {
42-
// int[] nums = new int[]{1,2};
43-
// int[] nums = new int[]{1};
44-
int[] nums = new int[]{1, 2, 3, 1};
45-
// System.out.println(findPeakElement(nums));
46-
System.out.println(findPeakElement_Ologn(nums));
54+
}
55+
return (left == nums.length - 1 || nums[left] > nums[left + 1]) ? left : right;
4756
}
57+
}
4858

59+
public static class Solution2 {
4960
/**
5061
* My original O(n) solution.
5162
*/
52-
public static int findPeakElement(int[] nums) {
53-
if (nums == null || nums.length == 0) {
54-
return 0;
55-
}
56-
int n = nums.length;
57-
int result = 0;
58-
for (int i = 0; i < n; i++) {
59-
if (i == 0 && n > 1 && nums[i] > nums[i + 1]) {
60-
result = i;
61-
break;
62-
} else if (i == n - 1 && i > 0 && nums[i] > nums[i - 1]) {
63-
result = i;
64-
break;
65-
} else if (i > 0 && i < n - 1 && nums[i] > nums[i - 1] && nums[i] > nums[i + 1]) {
66-
result = i;
67-
break;
68-
}
63+
public int findPeakElement(int[] nums) {
64+
if (nums == null || nums.length == 0) {
65+
return 0;
66+
}
67+
int n = nums.length;
68+
int result = 0;
69+
for (int i = 0; i < n; i++) {
70+
if (i == 0 && n > 1 && nums[i] > nums[i + 1]) {
71+
result = i;
72+
break;
73+
} else if (i == n - 1 && i > 0 && nums[i] > nums[i - 1]) {
74+
result = i;
75+
break;
76+
} else if (i > 0 && i < n - 1 && nums[i] > nums[i - 1] && nums[i] > nums[i + 1]) {
77+
result = i;
78+
break;
6979
}
70-
return result;
80+
}
81+
return result;
7182
}
83+
}
7284
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._162;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _162Test {
10+
private static _162.Solution1 solution1;
11+
private static _162.Solution2 solution2;
12+
private static int[] nums;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _162.Solution1();
17+
solution2 = new _162.Solution2();
18+
}
19+
20+
@Test
21+
public void test1() {
22+
nums = new int[] {1, 2};
23+
assertEquals(1, solution1.findPeakElement(nums));
24+
assertEquals(1, solution2.findPeakElement(nums));
25+
}
26+
27+
@Test
28+
public void test2() {
29+
nums = new int[] {1};
30+
assertEquals(0, solution1.findPeakElement(nums));
31+
assertEquals(0, solution2.findPeakElement(nums));
32+
}
33+
34+
@Test
35+
public void test3() {
36+
nums = new int[] {1, 2, 3, 1};
37+
assertEquals(2, solution1.findPeakElement(nums));
38+
assertEquals(2, solution2.findPeakElement(nums));
39+
}
40+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy