Submission Detail

65 / 65 test cases passed.
Status:

Accepted

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

Accepted Solutions Runtime Distribution

Sorry. We do not have enough accepted submissions to show distribution chart.

Accepted Solutions Memory Distribution

41000
41250
41500
41750
42000
42250
42500
42750
43000
43250
43500
43750
2
4
6
8
10
java
You are here!
Your memory usage beats 27.09 % of java submissions.
Memory (KB)
Distribution (%)

41000
41500
42000
42500
43000
43500
5
10
Zoom area by dragging across this chart

Invite friends to challenge Search Insert Position


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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
class Solution {
public int searchInsert(int[] nums, int target) {
if (nums.length == 1)
if (nums[0] >= target)
return 0;
else
return 1;
// result array: index 0 stores index result if found
// index 1 stores value of nearest result if not found
int[] result = new int[2];
result[0] = -1;
result[1] = -1;
result = binarySearch(nums,target,result);
if (result[0] != -1)
return result[0];
else
if (result[1] == -2)
return nums.length;
for (int i = 0; i < nums.length; i++)
if (nums[i] == result[1])
return i;
return 0;
}
public int[] binarySearch(int[] nums, int target, int[] res) {
int mp = nums.length/2;
if (nums.length == 0) {
res[0] = -1;
return res;
}
if (nums[mp] == target) {
res[0] = mp;
return res;
}
if (nums.length == 1) {
res[0] = -1;
return res;
}
// split array into left and right
int[] left = new int[mp];
int[] right = new int[nums.length-mp-1];
for (int i = 0; i < nums.length-mp-1; i++)
right[i] = nums[mp+i+1];
for (int i = 0; i < mp; i++)
left[i] = nums[i];
// left subcall
int ind = 0;
if (target < nums[mp]) {
res = binarySearch(left,target,res);
// not found in left subcall
if (res[0] == -1) {
for (int i = 0; i < left.length; i++) {
if (left[i] > target) {
res[1] = left[i];
return res;
}
}
res[1] = nums[mp];
} else {
return res;
}
// right subcall
} else {
res = binarySearch(right,target,res);
// not found in right subcall
if (res[0] == -1) {
for (int i = 0; i < right.length; i++)
if (right[i] > target) {
res[1] = right[i];
return res;
}
res[1] = -2;
return res;
} else {
res[0] += mp + 1;
return res;
}
}
return res;
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX