-
Code import java.util.*;; class Solution { public static int peakIndex(int[] nums) { if (nums.length == 1) return 0; if (nums.length == 2) return nums[0] > nums[1] ? 0 : 1; boolean ascending = false; for (int i = 0; i < nums.length - 1; ++i) { if (nums[i] < nums[i + 1]) { ascending = true; } else { if (ascending && nums[i] > nums[i + 1]) { return i; } ascending = false; } } if (ascending) { return nums.length - 1; } else { return -1; } } }
Test Cases import org.junit.Test; import static org.junit.Assert.assertEquals; import org.junit.runners.JUnit4; // TODO: Replace examples and use TDD development by writing your own tests public class SolutionTest { public void test1() { assertEquals(2, Solution.peakIndex(new int[] {1, 2, 3, 1})); } public void test2() { assertEquals(1, Solution.peakIndex(new int[] {1, 2, 1})); } public void test3() { assertEquals(0, Solution.peakIndex(new int[] {2})); } public void test4() { assertEquals(4, Solution.peakIndex(new int[] {1, 2, 3, 4, 5, 4, 3, 2})); } public void peakIsTheLastNumber() { assertEquals(4, Solution.peakIndex(new int[] {1, 2, 3, 4, 5})); } public void flatland() { assertEquals(-1, Solution.peakIndex(new int[] {1, 1, 1, 1, 1})); } }
Output:
-
Code - import java.util.*;;
- class Solution {
- public static int peakIndex(int[] nums) {
int left = 0;int right = nums.length - 1;int peak = -1;- if (nums.length == 1) return 0;
- if (nums.length == 2) return nums[0] > nums[1] ? 0 : 1;
while(left <= right) {if(left == right) {peak = left;break;}int mid = (left + right) / 2;if(nums[mid] < nums[mid + 1]) {left = mid + 1;- boolean ascending = false;
- for (int i = 0; i < nums.length - 1; ++i) {
- if (nums[i] < nums[i + 1]) {
- ascending = true;
- } else {
right = mid;- if (ascending && nums[i] > nums[i + 1]) {
- return i;
- }
- ascending = false;
- }
- }
return peak;- if (ascending) {
- return nums.length - 1;
- } else {
- return -1;
- }
- }
- }
Test Cases - import org.junit.Test;
- import static org.junit.Assert.assertEquals;
- import org.junit.runners.JUnit4;
- // TODO: Replace examples and use TDD development by writing your own tests
- public class SolutionTest {
- @Test
- public void test1() {
- assertEquals(2, Solution.peakIndex(new int[] {1, 2, 3, 1}));
- }
- @Test
- public void test2() {
- assertEquals(1, Solution.peakIndex(new int[] {1, 2, 1}));
- }
- @Test
- public void test3() {
- assertEquals(0, Solution.peakIndex(new int[] {2}));
- }
- @Test
- public void test4() {
- assertEquals(4, Solution.peakIndex(new int[] {1, 2, 3, 4, 5, 4, 3, 2}));
- }
- @Test
public void test5() {assertEquals(14, Solution.peakIndex(new int[] {1, 2, 3, 2, 1, 2, 3, 4, 5, 4, 3, 4, 5, 6, 7, 6, 5, 4, 3, 7, 8, 1}));- public void peakIsTheLastNumber() {
- assertEquals(4, Solution.peakIndex(new int[] {1, 2, 3, 4, 5}));
- }
- @Test
- public void flatland() {
- assertEquals(-1, Solution.peakIndex(new int[] {1, 1, 1, 1, 1}));
- }
- }
- All
- {{group.name}} ({{group.count}})
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}