Binary search for the first element greater than target
We all know how to search through an array for an element whose value equals the target value, but how to search for the element that has value greater than the target value?
A particularly elegant way of thinking about this problem is to think about doing a binary search over a transformed version of the array, where the array has been modified by applying the function
f(x) = 1 if x > target
0 else
Now, the goal is to find the very first place that this function takes on the value 1. We can do that using a binary search as follows:
int low = 0; high = numElems;
while (low != high) {
int mid = (low + high) / 2; // Or a fancy way to avoid int overflow
if (arr[mid] <= target) {
/* This index, and everything below it, must not be the first element
* greater than what we're looking for because this element is no greater
* than the element.
*/
low = mid + 1.
}
else {
/* This element is at least as large as the element, so anything after it can't
* be the first element that's at least as large.
*/
high = mid;
}
}
/* Now, low and high both point to the element in question. */
Reference: http://stackoverflow.com/questions/6553970/find-the-first-element-in-an-array-that-is-greater-than-the-target
Binary search for the first element greater than target的更多相关文章
- Binary search tree system and method
A binary search tree is provided for efficiently organizing values for a set of items, even when val ...
- 【leetcode】1038. Binary Search Tree to Greater Sum Tree
题目如下: Given the root of a binary search tree with distinct values, modify it so that every node has ...
- [LeetCode] 34. Find First and Last Position of Element in Sorted Array == [LintCode] 61. Search for a Range_Easy tag: Binary Search
Description Given a sorted array of n integers, find the starting and ending position of a given tar ...
- my understanding of (lower bound,upper bound) binary search, in C++, thanks to two post 分类: leetcode 2015-08-01 14:35 113人阅读 评论(0) 收藏
If you understand the comments below, never will you make mistakes with binary search! thanks to A s ...
- PAT题库-1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- stl binary search
stl binary search */--> pre { background-color: #2f4f4f;line-height: 1.6; FONT: 10.5pt Consola,&q ...
- Remove Node in Binary Search Tree 解答
从BST中移除一个节点是比较复杂的问题,需要分好几种情况讨论. 如这篇文章,就讨论了删除节点 1.有无左右子树 2.只有右子树 3.只有左子树 三种情况. 一种简单些的思维是只考虑删除节点是否有右子树 ...
- 35. leetcode 501. Find Mode in Binary Search Tree
501. Find Mode in Binary Search Tree Given a binary search tree (BST) with duplicates, find all the ...
- LeetCode 501. Find Mode in Binary Search Tree (找到二叉搜索树的众数)
Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred ...
随机推荐
- Unity3D鼠标点击物体产生事件
如果需要处理鼠标点击物体的情况, 可以当数据接触物体时,鼠标手势改变,然后点击后和NPC产生对话等: using UnityEngine; using System.Collections; publ ...
- pdf文件流生成pdf文件
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { Code.Login Starr_mode ...
- Android之屏幕测试
MainActivity: package com.example.touchscreentest; import android.os.Bundle; import android.R.layout ...
- Aliyun OSS SDK 异步分块上传导致应用异常退出
问题描述: 使用Aliyun OSS SDK的BeginUploadPart/EndUploadPart执行异步分块上传操作,程序出现错误并异常退出! 原因分析: Using .NET Framewo ...
- apktool反编译工具
几个报错的解决办法 apktool反编译时经常会出现下面的信息 Input file was not found or was not readable. Destination directory ...
- Java知识思维导图
注:图片来源于网络,谢谢分享. 1 开发环境Java SDK 下载和安装2 环境变量的配置(path和classpath)3 编程基础 标识符命名规范4 Java数据类型5 运算符6 分支语句(if, ...
- 高性能CSS(三)
CSS选择器对性能的影响源于浏览器匹配选择器和文档元素时所消耗的时间,所以优化选择器的原则是应尽量避免需要消耗更多匹配时间的选择器.而在这之前我们需要了解CSS选择器匹配的机制,如例子的子选择器规则: ...
- 5中方式实现String反转
这里介绍Java中5中实现String反转的方式. 一.数组实现String反转 //数组实现String反转 public String reverseByArray(){ if(str == nu ...
- maven入门程序(二)
这里就使用myeclipse简单创建一个实例程序. 一.创建项目 在myeclipse中创建项目选Maven Project,然后直接下一步用默认的项目空间.在archetype中选择quicksta ...
- Android - 代码片段
转载说明 本篇文章可能已经更新,最新文章请转:http://www.sollyu.com/android-code-snippets/ 说明 此篇文章为个人日常使用所整理的一此代码片段,此篇文正将会不 ...