forked from super30admin/Binary-Search-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sample
129 lines (97 loc) · 3.48 KB
/
Sample
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Time Complexity : O(logm*n)
// Space Complexity : O(m*n)
// Did this code successfully run on Leetcode : YES
// Any problem you faced while coding this : NO
// We have visualized the 2D Matrix to be one single array.
// and performed binary search on it
// the challenge was to find mid's exact location in the matrix but we did that by using division to find the row and mod to find the column.
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 c = mid % n;
int r = mid / n;
if(matrix[r][c] == target){
return true;
}else if(matrix[r][c] >= target){
high = mid-1;
}else{
low = mid+1;
}
}
return false;
}
}
// Time Complexity : O(logn)
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : YES
// Any problem you faced while coding this : NO
// we were searching the position of an element in am array of unknown size.
// Instead of reducing to half we went on increasing the window by double till the time high is greater than the target.
//once we find the appropriate window that is when high is higher then the target then apple traditional binary search method on it.
/**
* // This is ArrayReader's API interface.
* // You should not implement it, or speculate about its implementation
* interface ArrayReader {
* public int get(int index) {}
* }
*/
class Solution {
public int search(ArrayReader reader, int target) {
int high = 1,low = 0;
while(reader.get(high) < target){
low = high;
high = high*2;
}
while(low <= high){
int mid = low + (high-low)/2;
if(reader.get(mid) == target){
return mid;
} else if(reader.get(mid) < target){
low = mid+1;
}
else{
high = mid -1;
}
}
return -1;
}
}
// Time Complexity : O(log n)
// Space Complexity : O(m*n)
// Did this code successfully run on Leetcode : YES
// Any problem you faced while coding this : NO
//We were searching for a target in a rotated array that was sorted.
//we first find whether it is correctly sorted on the left side or the right side
//Then just compare the target variable.
class Solution {
public int search(int[] nums, int target) {
int n = nums.length;
int low = 0, high = n-1;
while(low<high){
int mid = low + (high-low)/2;
if (nums[mid] == target){
return mid;
}
else if (nums[low] <= nums[mid]){ //left sorted array
if(target >= nums[low] && nums[mid] > target){
high = mid -1;
} else{
low = mid +1;
}
}else{
if(target > nums[mid] && nums[high] >= target){
low = mid +1;
} else{
high = mid -1;
}
}
}
if(nums[low]==target) return low;
return -1;
}
}