Skip to content

Commit 8e21346

Browse files
committed
1064_Fixed_Point
1 parent 5a693a6 commit 8e21346

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ Also, there are open source implementations for basic data structs and algorithm
207207
| 954 | [Array of Doubled Pairs](https://leetcode.com/contest/weekly-contest-114/problems/array-of-doubled-pairs/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/954_Array_of_Doubled_Pairs.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/954_Array_of_Doubled_Pairs.java) | Sort, then use hashmap to store the frequency of each value. Then, check n, 2 * n in hashmap, O(nlogn) and O(n) |
208208
| 961 | [N-Repeated Element in Size 2N Array](https://leetcode.com/problems/n-repeated-element-in-size-2n-array/submissions/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/961_N-Repeated_Element_in_Size_2N_Array.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/961_N-Repeated_Element_in_Size_2N_Array.java) | Hash and count number, O(n) and O(n) |
209209
| 962 | [Maximum Width Ramp](https://leetcode.com/problems/maximum-width-ramp/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/962_Maximum_Width_Ramp.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/962_Maximum_Width_Ramp.java) | 1. Sort index by value, then transfer problem into finding max gap between index, O(nlogn) and O(1)<br>2. Binary Search for candicates, O(nlogn) and O(n) |
210+
| 1064 | [Fixed Point](https://leetcode.com/problems/fixed-point/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1064_Fixed_Point.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/1064_Fixed_Point.java) | 1. Go through index and value, until find solution encounter index < value, O(n) and O(1)<br>2. Binary search, O(logn) and O(1) |
210211

211212
| # | To Understand |
212213
|---| ----- |

java/1064_Fixed_Point.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
/* public int fixedPoint(int[] A) {
3+
for (int i = 0; i < A.length; i++) {
4+
// Because if A[i] > i, then i can never be greater than A[i] any more
5+
if (A[i] == i) return i;
6+
else if (A[i] > i) return -1;
7+
}
8+
return -1;
9+
} */
10+
public int fixedPoint(int[] A) {
11+
int l = 0;
12+
int h = A.length;
13+
while (l <= h) {
14+
int mid = (l + h) / 2;
15+
if (A[mid] > mid) h = mid - 1;
16+
else if (A[mid] < mid) l = mid + 1;
17+
else return mid;
18+
}
19+
return -1;
20+
}
21+
}

python/1064_Fixed_Point.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution(object):
2+
# def fixedPoint(self, A):
3+
# """
4+
# :type A: List[int]
5+
# :rtype: int
6+
# """
7+
# for index, value in enumerate(A):
8+
# # Because if A[i] > i, then i can never be greater than A[i] any more
9+
# if index == value:
10+
# return index
11+
# elif index < value:
12+
# return -1
13+
14+
def fixedPoint(self, A):
15+
l, h = 0, len(A) - 1
16+
while l <= h:
17+
mid = (l + h) // 2
18+
if A[mid] < mid:
19+
l = mid + 1
20+
elif A[mid] > mid:
21+
h = mid - 1
22+
else:
23+
return mid
24+
return -1

0 commit comments

Comments
 (0)