Skip to content

Commit 488a1de

Browse files
committed
153 (3) add template solution and new tests
1 parent 1c24038 commit 488a1de

File tree

4 files changed

+140
-22
lines changed

4 files changed

+140
-22
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Time : O(lgN) ; Space: O(1)
3+
* @tag : Array; Binary Search
4+
* @by : Steven Cooks
5+
* @date: Aug 4, 2015
6+
***************************************************************************
7+
* Description:
8+
*
9+
* Suppose a sorted array is rotated at some pivot unknown to you beforehand.
10+
* (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
11+
*
12+
* Find the minimum element. You may assume no duplicate exists in the array.
13+
*
14+
***************************************************************************
15+
* {@link https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ }
16+
*/
17+
package _153_FindMinimumInRotatedSortedArray;
18+
19+
/** see test {@link _153_FindMinimumInRotatedSortedArray.SolutionTemplateTest } */
20+
public class SolutionTemplate {
21+
22+
// solution that using binary search template
23+
public int findMin(int[] nums) {
24+
if (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[right]) {
32+
left = mid;
33+
} else {
34+
right = mid;
35+
}
36+
}
37+
return Math.min(nums[left], nums[right]);
38+
}
39+
40+
}

test/_153_FindMinimumInRotatedSortedArray/PracticeTest.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,28 @@ public void tearDown() throws Exception {
2828

2929
@Test
3030
public void Test1() {
31-
int[] nums = { 1 };
31+
int[] nums = { 2 };
3232
int actual = solution.findMin(nums);
33-
int expected = 1;
33+
int expected = 2;
3434
assertEquals(expected, actual);
3535
}
3636

3737
@Test
3838
public void Test2() {
39-
int[] nums = { 1, 2, 3 };
39+
int[] nums = { 0, 1, 2, 3 };
4040
int actual = solution.findMin(nums);
41-
int expected = 1;
41+
int expected = 0;
4242
assertEquals(expected, actual);
4343
}
4444

45-
4645
@Test
4746
public void Test3() {
48-
int[] nums = { 2, 3, 1 };
47+
int[] nums = { 3, 4, 2 };
4948
int actual = solution.findMin(nums);
50-
int expected = 1;
49+
int expected = 2;
5150
assertEquals(expected, actual);
5251
}
5352

54-
5553
@Test
5654
public void Test4() {
5755
int[] nums = { 3, 1, 2 };
@@ -60,16 +58,14 @@ public void Test4() {
6058
assertEquals(expected, actual);
6159
}
6260

63-
6461
@Test
6562
public void Test5() {
66-
int[] nums = { 0, 1, 2, 3, 4 };
63+
int[] nums = { 0, 1, 2, 3, 4, 5 };
6764
int actual = solution.findMin(nums);
6865
int expected = 0;
6966
assertEquals(expected, actual);
7067
}
7168

72-
7369
@Test
7470
public void Test6() {
7571
int[] nums = { 4, 0, 1, 2, 3 };
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package _153_FindMinimumInRotatedSortedArray;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.After;
6+
import org.junit.Before;
7+
import org.junit.Rule;
8+
import org.junit.Test;
9+
import org.junit.rules.Timeout;
10+
11+
public class SolutionTemplateTest {
12+
13+
/** Test method for {@link _153_FindMinimumInRotatedSortedArray.SolutionTemplate } */
14+
SolutionTemplate solution;
15+
16+
@Rule
17+
public Timeout globalTimeout = new Timeout(200);
18+
19+
@Before
20+
public void setUp() throws Exception {
21+
solution = new SolutionTemplate();
22+
}
23+
24+
@After
25+
public void tearDown() throws Exception {
26+
solution = null;
27+
}
28+
29+
@Test
30+
public void Test1() {
31+
int[] nums = { 2 };
32+
int actual = solution.findMin(nums);
33+
int expected = 2;
34+
assertEquals(expected, actual);
35+
}
36+
37+
@Test
38+
public void Test2() {
39+
int[] nums = { 0, 1, 2, 3 };
40+
int actual = solution.findMin(nums);
41+
int expected = 0;
42+
assertEquals(expected, actual);
43+
}
44+
45+
@Test
46+
public void Test3() {
47+
int[] nums = { 3, 4, 2 };
48+
int actual = solution.findMin(nums);
49+
int expected = 2;
50+
assertEquals(expected, actual);
51+
}
52+
53+
@Test
54+
public void Test4() {
55+
int[] nums = { 3, 1, 2 };
56+
int actual = solution.findMin(nums);
57+
int expected = 1;
58+
assertEquals(expected, actual);
59+
}
60+
61+
@Test
62+
public void Test5() {
63+
int[] nums = { 0, 1, 2, 3, 4, 5 };
64+
int actual = solution.findMin(nums);
65+
int expected = 0;
66+
assertEquals(expected, actual);
67+
}
68+
69+
@Test
70+
public void Test6() {
71+
int[] nums = { 4, 0, 1, 2, 3 };
72+
int actual = solution.findMin(nums);
73+
int expected = 0;
74+
assertEquals(expected, actual);
75+
}
76+
77+
78+
@Test
79+
public void Test7() {
80+
int[] nums = { 2, 3, 4, 0, 1 };
81+
int actual = solution.findMin(nums);
82+
int expected = 0;
83+
assertEquals(expected, actual);
84+
}
85+
86+
}

test/_153_FindMinimumInRotatedSortedArray/SolutionTest.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,28 @@ public void tearDown() throws Exception {
2828

2929
@Test
3030
public void Test1() {
31-
int[] nums = { 1 };
31+
int[] nums = { 2 };
3232
int actual = solution.findMin(nums);
33-
int expected = 1;
33+
int expected = 2;
3434
assertEquals(expected, actual);
3535
}
3636

3737
@Test
3838
public void Test2() {
39-
int[] nums = { 1, 2, 3 };
39+
int[] nums = { 0, 1, 2, 3 };
4040
int actual = solution.findMin(nums);
41-
int expected = 1;
41+
int expected = 0;
4242
assertEquals(expected, actual);
4343
}
4444

45-
4645
@Test
4746
public void Test3() {
48-
int[] nums = { 2, 3, 1 };
47+
int[] nums = { 3, 4, 2 };
4948
int actual = solution.findMin(nums);
50-
int expected = 1;
49+
int expected = 2;
5150
assertEquals(expected, actual);
5251
}
5352

54-
5553
@Test
5654
public void Test4() {
5755
int[] nums = { 3, 1, 2 };
@@ -60,16 +58,14 @@ public void Test4() {
6058
assertEquals(expected, actual);
6159
}
6260

63-
6461
@Test
6562
public void Test5() {
66-
int[] nums = { 0, 1, 2, 3, 4 };
63+
int[] nums = { 0, 1, 2, 3, 4, 5 };
6764
int actual = solution.findMin(nums);
6865
int expected = 0;
6966
assertEquals(expected, actual);
7067
}
7168

72-
7369
@Test
7470
public void Test6() {
7571
int[] nums = { 4, 0, 1, 2, 3 };

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