Submission Detail

195 / 195 test cases passed.
Status:

Accepted

Runtime: 9 ms
Memory Usage: 41.9 MB
Submitted: 0 minutes ago
loading
Runtime Error Message:
Last executed input:
Input:
Output:
Expected:

Accepted Solutions Runtime Distribution

0
1
2
3
4
5
6
7
8
9
10
11
20
40
60
80
100
java
You are here!
Your runtime beats 5.70 % of java submissions.
Runtime (ms)
Distribution (%)

0
1
2
3
4
5
6
7
8
9
10
11
50
100
Zoom area by dragging across this chart

Accepted Solutions Memory Distribution

40000
40250
40500
40750
41000
41250
41500
41750
42000
42250
2
4
6
8
10
java
You are here!
Your memory usage beats 23.07 % of java submissions.
Memory (KB)
Distribution (%)

40000
40250
40500
40750
41000
41250
41500
41750
42000
42250
5
10
Zoom area by dragging across this chart

Invite friends to challenge Search in Rotated Sorted Array


Submitted Code: 0 minutes ago

Language: java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class Solution {
public int search(int[] nums, int target) {
int pivot = 0;
int ind = 0;
// determine pivot index
if (nums.length > 1 && nums[0] > nums[1])
pivot = 0;
else {
for (int i = 1; i < nums.length-1; i++)
if (nums[i] < nums[i-1])
pivot = i;
}
System.out.println("pivot = "+pivot);
// split array at pivot
int[] left = new int[pivot];
int[] right = new int[nums.length-pivot];
for (int i = 0; i < pivot; i++)
left[i] = nums[i];
for (int i = 0; i < nums.length-pivot; i++)
right[i] = nums[pivot+i];
// perform binary search on left and right sides
ind = bin_search(left,target);
if (ind != -1)
return ind;
if (ind == -1)
ind = bin_search(right,target);
if (ind != -1)
ind += pivot;
return ind;
}
public int bin_search(int[] arr, int target) {
int mp = arr.length/2;
int ind = 0;
if (arr.length == 0)
return -1;
if (arr[mp] == target) {
return mp;
}
int[] left = new int[mp];
int[] right = new int[arr.length-mp-1];
for (int i = 0; i < mp; i++)
left[i] = arr[i];
for (int i = 0; i < arr.length-mp-1; i++)
right[i] = arr[mp+i+1];
ind = bin_search(left,target);
if (ind != -1)
return ind;
ind = bin_search(right,target);
if (ind == -1)
return -1;
return (ind + mp + 1);
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX