-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1064.Fixed-Point.cpp
More file actions
61 lines (53 loc) · 1.09 KB
/
1064.Fixed-Point.cpp
File metadata and controls
61 lines (53 loc) · 1.09 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
#include "bits/stdc++.h"
using namespace std;
// #include "Tree.h"
#define deb(x) cout<<x<<endl;
const int inf = 1e9;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef pair<int,int> pii;
//#include "LinkedList.h"
void print(vi &out){
for(auto x: out) cout<<x<<" ";
cout<<endl;
}
class Solution0 {
public:
int fixedPoint(vector<int>& arr) {
int n = arr.size();
int ans=-1 ;
for(int i=0; i<n; i++){
if(arr[i]==i){
ans = i;
break;
}
}
return ans==n ? -1: ans;
}
};
class Solution {
public:
int fixedPoint(vector<int>& arr) {
int n = arr.size();
int l = 0;
int r = n-1;
int ans= -1 ;
while(l<=r){
int mid = l+(r-l)/2;
if(arr[mid]==mid){
ans = mid;
}
if(arr[mid]<mid){
l = mid+1;
}
else
r = mid-1;
}
return ans;
}
};
int main(){
return 0;
}