Skip to content

Commit 029f046

Browse files
committed
add search the max num
1 parent 2bda8b9 commit 029f046

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

Algorithm/binary_search.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,29 @@ if __name__ == '__main__':
5656
value = int(input("Please input the value: "))
5757
print(binary_search_rec(lst, value, left, right))
5858
```
59+
60+
昨天面试,面试官出了一道算法题:
61+
62+
> 有一个数组,其内元素先递增后递减,请找出其中的最大值.
63+
64+
对于我来说,当时第一个想起来的是,`排序`但是转念间就知道肯定不是最好的啦.于是就在哪儿想啊想,还是想不起来.气氛挺尴尬的,外面也挺冷的(电话面试,外面安静).我想不起来,面试小哥也不急着催我,最后也算是在小哥的提示下,想起了怎么做啦!(太感谢小哥啦, 小哥好人! 喂, 你们几个不许笑啊喂!)
65+
66+
当然是**二分**啦,下面是算法实现!
67+
68+
```python
69+
# coding=utf-8
70+
71+
72+
def search_max_num(seq, left, right):
73+
mid = (right + left) // 2
74+
if left > right:
75+
return seq[mid]
76+
if seq[mid] > seq[mid - 1]:
77+
return search_max_num(seq, mid + 1, right)
78+
else:
79+
return search_max_num(seq, left, mid - 1)
80+
81+
if __name__ == "__main__":
82+
seq = [32, 55, 54, 54, 54, 54, 32, 15, 6, 4, 2, 1]
83+
print(search_max_num(seq, 0, len(seq)))
84+
```

0 commit comments

Comments
 (0)