二分法不只能像之前的记录,可以找到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的更多相关文章

  1. 二分查找总结及部分Lintcode题目分析 1

    进行二分查找课程回顾与总结,包括以下几个方面,二分法的模板总结和解题思路.应用. 二分法模板总结classical binary search: 1. 必须要做的排除极端情况,也就是数组(用A表示)不 ...

  2. 二分查找总结及部分Lintcode题目分析 2

    Search in a big sorted array,这个比之前的二分法模板多了一个很不同的特性,就是无法知道一个重要的条件end值,也是题目中强调的重点 The array is so big ...

  3. 二分查找总结及部分Lintcode题目分析 3

    Search in rotated sorted array,题目中也给出了相应的例子,就是在sorted array某个节点发生了翻转(ie.0 1 2 4 5 6 7 might become 4 ...

  4. 二叉树总结及部分Lintcode题目分析 1

    1. 遍历问题 Preorder / Inorder / Postorder preorder: root left right inorder: left root right postorder: ...

  5. Find Minimum in Rotated Sorted Array 典型二分查找

    https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array/ Suppose a sorted array is rot ...

  6. HDU 3763 CD【二分查找】

    解题思路:给出两个数列an,bn,求an和bn中相同元素的个数因为注意到n的取值是0到1000000,所以可以用二分查找来做,因为题目中给出的an,bn,已经是单调递增的,所以不用排序了,对于输入的每 ...

  7. POJ 3273 Monthly Expense二分查找[最小化最大值问题]

    POJ 3273 Monthly Expense二分查找(最大值最小化问题) 题目:Monthly Expense Description Farmer John is an astounding a ...

  8. 二分查找里的upper bound与lower bound的实现与分析

    1. 问题引入 最近参选了学堂在线的课程数据结构(2015秋).课程由清华大学的邓俊辉老师主讲,在完成课后作业时,遇到了这样一个题目范围查询.在这个题目中,我需要解决这样一个子问题:给定了一组已经排好 ...

  9. (二分查找 拓展) 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 ...

随机推荐

  1. Spring入门(二)

    Spring IOC&DI 控制反转(inversion of control):控制什么?什么反转? 我们都知道,传统的程序中,如果A类需要使用B类对象,会在程序中直接创建B类对象实例,此时 ...

  2. zmq的send

    int zmq_send (void *socket, zmq_msg_t *msg, int flags);      2.2.1 nt zmq_send (void *socket, void * ...

  3. [转]PHP base64_encode 在URL地址参数编码上使用

    因为我使用公钥,使用php的 openssl 加密之后的资料 base64_encode()字符串,然后放入请求 url 中当作参数,urlencode(base64_encode()),然后用 ur ...

  4. [转]sourceforge文件下载过慢

    sourceforge文件下载过慢,可以用下面网址镜像下载, 通过 下载Sourceforge等国内无法下载站点文件的另一种方法博文,好像主站点是 https://www.mirrorservice. ...

  5. look at me

    I would bet my life, like I bet my heart我以生命与真心担保That you were the one, baby你就是我的命中注定I've never been ...

  6. JZOJ5146:港湾

    Description 放假啦!小林和康娜来到了港口,看到有货船正在卸货.港口十分狭窄,只有两个卸货区可以使用.每个卸货区上面可以堆积任意多个箱子.每卸下来一个箱子,工作人员都会把这个箱子放在某个卸货 ...

  7. linux环境下创建domain

    首先进入weblogic的安装目录,具体如下: cd /wls/Oracle/Middleware/Oracle_Home/wlserver/common/bin 图形化创建 1.下载xmanager ...

  8. NX-二次开发删除对象UF_OBJ_delete_object

    NX9+VS2012 #include <uf.h> #include <uf_curve.h> #include <uf_obj.h> UF_initialize ...

  9. Java-Class-@I:org.junit.runner.RunWith

    ylbtech-Java-Class-@I:org.junit.runner.RunWith 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部 1. package org.juni ...

  10. 剑指offer——25合并两个排序的链表

    题目描述 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则.   题解: 使用普通方法,或者递归,注意新的头节点即可. //使用普通的合并方法 class S ...