-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathSample.java
More file actions
96 lines (89 loc) · 2.68 KB
/
Copy pathSample.java
File metadata and controls
96 lines (89 loc) · 2.68 KB
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
// Approach:
// Treat the 2D matrix as a single sorted array and apply binary search.
// Convert the middle index into row and column indices to access the matrix element.
// Return true if the target is found, otherwise return false.
// Time Complexity: O(log(M * N))
// Space Complexity: O(1)
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int m=matrix.length;
int n=matrix[0].length;
int low=0;
int high=m*n-1;
while(low<=high){
int mid=low+(high-low)/2;
int r=mid/n;
int c=mid%n;
if(matrix[r][c]==target) return true;
else if(matrix[r][c]<target) {
low=mid+1;
}else{
high=mid-1;
}
}
return false;
}
}
//problem 2
// Approach:
// Use binary search to determine which half of the array is sorted.
// Check whether the target lies in the sorted half and discard the other half.
// Continue until the target is found or the search space becomes empty.
// Time Complexity: O(log N)
// Space Complexity: O(1)
class Solution {
public int search(int[] nums, int target) {
int n=nums.length;
int low=0;
int high=n-1;
while(low<=high){
int mid=low+(high-low)/2;
if(nums[mid]==target){
return mid;
}if(nums[low]<=nums[mid]){//left sorted
if(nums[low]<=target && target<nums[mid]){
high=mid-1;
}else{
low=mid+1;
}
}else{ //right sorted
if(nums[mid]<target && nums[high]>=target){
low=mid+1;
}else{
high=mid-1;
}
}
}
return -1;
}
}
//problem 3
// Approach:
// Expand the search range exponentially until the target is within the range or exceeded.
// Apply binary search on the identified range to locate the target.
// Return the index if found, otherwise return -1.
// Time Complexity: O(log N)
// Space Complexity: O(1)
class Solution {
public int search(ArrayReader reader, int target) {
int low = 0;
int high = 1;
// Expand the search range
while (reader.get(high) < target) {
low = high;
high = high * 2;
}
// Binary search
while (low <= high) {
int mid = low + (high - low) / 2;
int value = reader.get(mid);
if (value == target)
return mid;
else if (value < target)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
}