-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pather_fen_fa.py
More file actions
52 lines (44 loc) · 1.18 KB
/
er_fen_fa.py
File metadata and controls
52 lines (44 loc) · 1.18 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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2020 ubuntu <ubuntu@VM-0-13-ubuntu>
#
# Distributed under terms of the MIT license.
"""
举一个二分搜索的例子。给定一个非递减整数数组,和一个 target 值,要求你找到数组中最小的一个数 x,满足 x*x > target,如果不存在,则返回 -1。
"""
def fn(array, target):
l,r = 0, len(array) - 1
ret = -1
while l <= r:
m = (l + r)//2
if array[m] * array[m] > target:
ret = m
r = m -1
else:
l = m + 1
if ret == -1:
return -1
else:
return array[ret]
print(fn(list(range(10)), 58))
print(fn(list(range(8)), 28))
def com(num, target):
return num * num > target
def binary_search(array, target):
l, r = 0, len(array) - 1
ret = -1
while l <= r:
m = (l + r)//2
if com(array[m], target):
r = m -1
ret = m
else:
l = m + 1
return ret
def solve(array, target):
ret = binary_search(array, target)
return ret if ret == -1 else array[ret]
print(solve(list(range(10)), 58))
print(solve(list(range(8)), 28))