二分查找总结及部分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 ...
随机推荐
- 笔记47 Hibernate快速入门(四)
Hibernate注解,完成原来xml文件里的映射信息. 使用注解时,要修改hibernate.cfg.xml中的映射,不再是通过xml文件 <mapping class="hiber ...
- 几种数据类型的常用API
数字 int() 字符串 #==========join===========在字符串前后插入字符 li = 'alexericrain' new_li = '_'.join(li) print(ne ...
- 如何运行linux shell程序
原文地址:http://www.sohu.com/a/138822796_610671 首先,我们从一个十分简单的例子test.sh开始吧: #!/bin/sh #this is a test. cd ...
- 转 直接在浏览器运行Python代码
到这个链接将代码下载到本地,然后打开cmd,使用python运行此文件,然后不要关闭窗口: https://raw.githubusercontent.com/michaelliao/learn-py ...
- vue组件间函数调用
vue父子组件间函数调用 <Child ref="myChild"></Child> // 父组件 // 引入子组件 import Child from ' ...
- 《DNS攻击防范科普系列3》 -如何保障 DNS 操作安全
引言 前两讲我们介绍了 DNS 相关的攻击类型,以及针对 DDoS 攻击的防范措施.这些都是更底层的知识,有同学就来问能否讲讲和我们的日常操作相关的知识点,今天我们就来说说和我们日常 DNS 操作相关 ...
- poi之Excel下载之详细设置
1.设置标题格式 /** * HEAD样式 * * @param workbook * @param sheet */ public void setHeadCellStyles(HSSFWorkbo ...
- 秒懂机器学习---分类回归树CART
秒懂机器学习---分类回归树CART 一.总结 一句话总结: 用决策树来模拟分类和预测,那些人还真是聪明:其实也还好吧,都精通的话想一想,混一混就好了 用决策树模拟分类和预测的过程:就是对集合进行归类 ...
- Codeforces 1167C - News Distribution
题目链接:http://codeforces.com/problemset/problem/1167/C 题意:大概就是分成几个小团体,给每个人用1 - n编号,当对某个人传播消息的时候,整个小团体就 ...
- 拾遗:Linux 用户及权限管理基础
Lacks of Knowledge 1: Linux has large amount of COMMANDS,but many of them have similar funtions,it's ...