-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathTotal Occurrence of Target.java
executable file
·104 lines (92 loc) · 2.31 KB
/
Total Occurrence of Target.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
97
98
99
100
101
102
103
104
M
想法很简单。写起来有点长。
找total number of occurance. 首先找first occurance, 再找last occurance.
```
/*
Total Occurrence of Target
Given a target number and an integer array sorted in ascending order. Find the total number of occurrences of target in the array.
Example
Given [1, 3, 3, 4, 5] and target = 3, return 2.
Given [2, 2, 3, 4, 6] and target = 4, return 1.
Given [1, 2, 3, 4, 5] and target = 6, return 0.
Challenge
Time complexity in O(logn)
Tags Expand
Binary Search
*/
/*
Thought:
Similar to find last occurance of the target. Now: find the occurance, jump out. Find front, end occurance index.
*/
public class Solution {
/**
* @param A an integer array sorted in ascending order
* @param target an integer
* @return an integer
*/
public int totalOccurrence(int[] A, int target) {
if (A == null || A.length == 0) {
return 0;
}
int start = 0;
int end = A.length - 1;
int mid = start + (end - start)/2;
//Find first occurance
int first = 0;
int last = 0;
while (start + 1 < end){
mid = start + (end - start)/2;
if (A[mid] == target) {
if (mid - 1 >= 0 && A[mid - 1] == target) {
end = mid;
} else {
break;
}
} else if (A[mid] < target) {
start = mid;
} else {
end = mid;
}
}
if (A[start] == target) {
first = start;
} else if (A[mid] == target){
first = mid;
} else if (A[end] == target){
first = end;
} else {
return 0;
}
//If no 2nd occurance, just return
if (mid + 1 < A.length && A[mid + 1] != target) {
return 1;
}
//Find last occurance
start = first;
last = start + 1;
end = A.length - 1;
while (start + 1 < end){
mid = start + (end - start)/2;
if (A[mid] == target) {
if (mid + 1 < A.length && A[mid + 1] == target) {
start = mid;
} else {
break;
}
} else if (A[mid] < target) {
start = mid;
} else {
end = mid;
}
}
if (A[end] == target) {
last = end;
} else if (A[mid] == target){
last = mid;
} else if (A[start] == target) {
last = start;
}
return last - first + 1;
}
}
```