二分查找总结及部分Lintcode题目分析 4
二分法不只能像之前的记录,可以找到index~第二种类型是找到二分答案。有以下几个例子,都是之前二分法的扩展,不再赘述,只记录下忽略的点,以后回顾多注意~
1. wood cut
class Solution:
"""
@param L: Given n pieces of wood with length L[i]
@param k: An integer
return: The maximum length of the small pieces.
"""
def pieces(self, L, length):
p = 0
for i in L:
p += i / length
return p def woodCut(self, L, k):
# at first, this if check condition is ignored
if sum(L) < k:
return 0
start = 1
# at first, use the average of sum of array.
end = max(L)
while start + 1 < end:
mid = start + (end - start) / 2
if self.pieces(L, mid) >= k:
start = mid
else:
end = mid
if self.pieces(L, end) >= k:
return end
return start
2. First Bad Version:可以看成找到第一个是false的位置
#class SVNRepo:
# @classmethod
# def isBadVersion(cls, id)
# # Run unit tests to check whether verison `id` is a bad version
# # return true if unit tests passed else false.
# You can use SVNRepo.isBadVersion(10) to check whether version 10 is a
# bad version.
class Solution:
"""
@param n: An integers.
@return: An integer which is the first bad version.
"""
def findFirstBadVersion(self, n):
start = 1
end = n
while(start + 1 < end):
mid = start + (end - start) / 2
if SVNRepo.isBadVersion(mid):
end = mid
else:
start = mid
if SVNRepo.isBadVersion(start):
return start
return end
3. Search for a range:找到first position和last position的结合体,不过需要两次遍历,还没有找到更好的方法
class Solution:
"""
@param A : a list of integers
@param target : an integer to be searched
@return : a list of length 2, [index1, index2]
"""
def searchRange(self, A, target):
# write your code here
if A is None or len(A) == 0:
return[-1,-1]
start = 0
end = len(A) - 1
while(start + 1 < end):
mid = start + (end - start) / 2
if A[mid] == target:
end = mid
elif A[mid] < target:
start = mid
else:
end = mid
if A[start] == target:
left = start
elif A[end] == target:
left = end
else:
return[-1,-1] start = left
end = len(A) - 1
while start + 1 < end:
mid = start + (end - start) / 2
if A[mid] == target:
start = mid
elif A[mid] < target:
start = mid
else:
end = mid
if A[end] == target:
right = end
# one tip: cannot use another if, because there maybe two continuous
# same num. so A[start] and A[end] maybe the same and the value of
# A[start] may override the right value
elif A[start] == target:
right = start
return [left,right]
二分查找总结及部分Lintcode题目分析 4的更多相关文章
- 二分查找总结及部分Lintcode题目分析 1
进行二分查找课程回顾与总结,包括以下几个方面,二分法的模板总结和解题思路.应用. 二分法模板总结classical binary search: 1. 必须要做的排除极端情况,也就是数组(用A表示)不 ...
- 二分查找总结及部分Lintcode题目分析 2
Search in a big sorted array,这个比之前的二分法模板多了一个很不同的特性,就是无法知道一个重要的条件end值,也是题目中强调的重点 The array is so big ...
- 二分查找总结及部分Lintcode题目分析 3
Search in rotated sorted array,题目中也给出了相应的例子,就是在sorted array某个节点发生了翻转(ie.0 1 2 4 5 6 7 might become 4 ...
- 二叉树总结及部分Lintcode题目分析 1
1. 遍历问题 Preorder / Inorder / Postorder preorder: root left right inorder: left root right postorder: ...
- Find Minimum in Rotated Sorted Array 典型二分查找
https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array/ Suppose a sorted array is rot ...
- HDU 3763 CD【二分查找】
解题思路:给出两个数列an,bn,求an和bn中相同元素的个数因为注意到n的取值是0到1000000,所以可以用二分查找来做,因为题目中给出的an,bn,已经是单调递增的,所以不用排序了,对于输入的每 ...
- POJ 3273 Monthly Expense二分查找[最小化最大值问题]
POJ 3273 Monthly Expense二分查找(最大值最小化问题) 题目:Monthly Expense Description Farmer John is an astounding a ...
- 二分查找里的upper bound与lower bound的实现与分析
1. 问题引入 最近参选了学堂在线的课程数据结构(2015秋).课程由清华大学的邓俊辉老师主讲,在完成课后作业时,遇到了这样一个题目范围查询.在这个题目中,我需要解决这样一个子问题:给定了一组已经排好 ...
- (二分查找 拓展) leetcode 34. Find First and Last Position of Element in Sorted Array && lintcode 61. Search for a Range
Given an array of integers nums sorted in ascending order, find the starting and ending position of ...
随机推荐
- nodejs MYSQL数据库执行多表查询
1.设计数据库 2.设计数据库表 genres表: books表: 3.安装MySQL模块 4. 代码编写 (1) 第一种方法: 在query中使用nextTables属性,将属性值设置为ture d ...
- Yii2 : Active Record add Not In condition
$query = MyModel::find()->where(['not in','attribute',$array]); 參考 Yii2 : Active Record add Not I ...
- leetcood学习笔记-203-移除链表元素
题目描述: 方法:#在改pre链表时 head中的值也改变 class Solution(object): def removeElements(self, head, val): "&qu ...
- 使用canvas绘制6X6调色盘
<canvas id="canvas" height="150" width="150"></canvas> var ...
- delphi 单元 MSHTML 之Ihtmldocument2
delphi : Ihtmldocument2接口的利用 MSHTML是微软公司的一个COM组件,该组件封装了HTML语言中的所有元素及其属性,穿越其供给的规范接口,能够访问指定网页的所有元素. MS ...
- flutter 显示HTML代码
需求是后台返回的是富文本,所以需要吧富文本转成flutter 能识别的内容 找了几个插件只有这个比较好用写下来 dependencies: flutter_html: ^0.9.8 安装 下剩下的就比 ...
- ZK4字命令
zookeeper4字命令:两种方式,1.通过telnet链接服务器,执行stat.2.echo stat|nc xxx.xxx.xxx.xxx 2181效果是一样的conf:zk服务器运行时的基本信 ...
- PAT_A1053#Path of Equal Weight
Source: PAT A1053 Path of Equal Weight (30 分) Description: Given a non-empty tree with root R, and w ...
- ICPC 2018 焦作区域赛
// 2019.10.7 练习赛 // 赛题来源:2018 ICPC 焦作区域赛 // CF链接:http://codeforces.com/gym/102028 A Xu Xiake in Hena ...
- axios全局拦截响应
在系统开发过程中,若遇到长时间未操作,则需要将页面跳转到登录页面.因为现在都是前后端分离的开发模式,路由跳转都交给前端,而后端只返回一个报错信息,例如"errorMsg":&quo ...