my understanding of (lower bound,upper bound) binary search, in C++, thanks to two post

thanks to A simple CPP solution with lower_bound

and C++ O(logn) Binary Search that handles duplicate, thanks to phu1ku ‘s answer on the second post.

http://en.cppreference.com/w/cpp/algorithm/upper_bound

Returns an iterator pointing to the first element in the range [first, last) that is greater than value.

http://en.cppreference.com/w/cpp/algorithm/lower_bound

Returns an iterator pointing to the first element in the range [first, last) that is not less than (i.e. greater or equal to) value.

if want to practice, code on your own, https://leetcode.com/problems/search-insert-position/

int binarySearch(vector<int>& nums, int target) {
    /// return index of first one that comp(item,target)==true, or nums.size() if not found
    /// comp is greater or equal to for lower_bound
    /// comp is greater for upper_bound
    int first=0, last=nums.size(), mid;
    while (first<last) {
        mid=first+((last-first)>>1); // first<=mid, mid<last
        /// if comp(item,target)==false, advance first
        // if(nums[mid]<=target) // for upper_bound
        if (nums[mid]<target) // for lower_bound
        first=mid+1; // first always increases
        else /// else recede last
        last=mid; // last always decreases (even last-first==1)
    }
    return first;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。// ps. If in any way improment can be achieved, better performance or whatever, it will be well-appreciated to let me know, thanks in advance.

时间: 2024-10-18 16:00:26

my understanding of (lower bound,upper bound) binary search, in C++, thanks to two post的相关文章

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

1. 问题引入 最近参选了学堂在线的课程数据结构(2015秋).课程由清华大学的邓俊辉老师主讲,在完成课后作业时,遇到了这样一个题目范围查询.在这个题目中,我需要解决这样一个子问题:给定了一组已经排好序的整数集合A[0...n]和一组闭区间[L,R],求这个整数集合中落在这个区间中的点的个数.解决这个问题,我们很容易想到查找效率很高的二分查找,但是这又不是一般求key是否在一个数组里面的二分查找问题.对于区间左端点L,要找到数组里面大于或等于它的最小的元素的下标indexL.对于区间右端点R,要

[分治] lower/upper bound(非STL)

lower/upper bound 有些时候我们需要知道有序数列中第一个大于或等于k(lower bound)的数或者第一个大于k的数(upper bound),如果我们一个一个查找时间复杂度是O(n)的,在STL(Algorithm库)中有两个函数lower_bound,和upper_bound,函数支持以上操作,为了更好地理解以上函数我们将手写上述函数并讲解它的工作原理. [算法描述] 我们假设我们所需要查找的数组是a[]对于每个函数我们都设置3个变量,第一个是左端点,第二个是右端点,第三个

PKU2018校赛 H题 Safe Upper Bound

http://poj.openjudge.cn/practice/C18H 题目 算平均数用到公式\[\bar{x}=\frac{x_1+x_2+x_3+\cdots+x_n}{n}\] 但如果用int型计算,那么\(x_1+x_2+x_3+\cdots+x_n\)可能会超过\(2^{31}-1\) 算6个数的平均数可以这么算 Calculate the average of\(x_1,x_2,x_3\)\[\bar{x}_1=\frac{x_1+x_2+x_3}{3}\]Calculate t

lintcode900 - Closest Binary Search Tree Value - easy

Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target.ExampleGiven root = {1}, target = 4.428571, return 1.Notice* Given target value is a floating point.* You are guaranteed to have only one

LeetCode Verify Preorder Sequence in Binary Search Tree

原题链接在这里:https://leetcode.com/problems/verify-preorder-sequence-in-binary-search-tree/ 题目: Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary search tree. You may assume each number in the sequence is u

[LeetCode] 255. Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary search tree. You may assume each number in the sequence is unique. Follow up:Could you do it using only constant space complexity? 给一个数组,验证是否为一个二叉搜索树的

二分搜索 - Binary Search

二分搜索是一种在有序数组中寻找目标值的经典方法,也就是说使用前提是『有序数组』.非常简单的题中『有序』特征非常明显,但更多时候可能需要我们自己去构造『有序数组』.下面我们从最基本的二分搜索开始逐步深入. 一.lower/upper bound 定义 lower bound 为在给定升序数组中大于等于目标值的最小索引,upper bound 则为小于等于目标值的最大索引,下面上代码和测试用例. import java.util.*; public class Main { public stati

LeetCode: Validate Binary Search Tree 解题报告

Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key.The right subtree of a node co

[LC] Binary Search 题目/方法总结

Binary search 是最简单也十分常用的一种算法,能把O(n) 的搜索降到O(logn)这个帖子的总结是参考大神Grandyang的,甚至很多地方是直接抄袭过来,原贴在这里http://www.cnblogs.com/grandyang/p/6854825.html Binary Search Template 二分法常见关键词:sorted: 二分常见痛点有以下几个: 循环条件:start + 1 < end mid = start + (end - start) /2; A[mid]