141. Sqrt(x)

https://www.lintcode.com/problem/sqrtx/description?_from=ladder&&fromId=4

public class Solution {
/**
* @param x: An integer
* @return: The sqrt of x
*/
public int sqrt(int x) {
// write your code here
if(x==0){
return 0;
} long start =1;
long end = x; while(start+1<end){
long mid = start+(end-start)/2;
if(mid*mid<x){
start = mid;
}else{
end = mid;
}
} if(end*end<=x){
return (int)end;
} return (int)start;
}
}

586. Sqrt(x) II

https://www.lintcode.com/problem/sqrtx-ii/description?_from=ladder&&fromId=4

public class Solution {
/**
* @param x: a double
* @return: the square root of x
*/
public double sqrt(double x) {
// write your code here
double start =0;
//如果x<1,那么右边界=1
double end = Math.max(1,x);
double diff = 1e-12; while(start+diff<end){
double mid = start +(end-start)/2;
if(mid*mid<x){
start = mid;
}else{
end = mid;
}
} return start;
}
}

390. Find Peak Element II

https://www.lintcode.com/problem/find-peak-element-ii/description?_from=ladder&&fromId=4

public class Solution {
/*
* @param A: An integer matrix
* @return: The index of the peak
*/
public List<Integer> findPeakII(int[][] A) {
// write your code here
if(A==null|| A.length==0|| A[0].length==0){
return new ArrayList<>();
} int r = A.length;
int c = A[0].length; int low =1;
int high = r-2;
List<Integer> ans = new ArrayList<>(); while(low<=high){
int mid = low +(high-low)/2;
int col = maxForEachRow(mid,A);
if(A[mid][col]<A[mid-1][col]){
high = mid-1;
}else if(A[mid][col]<A[mid+1][col]){
low = mid+1;
}else{
ans.add(mid);
ans.add(col);
break;
} } return ans; } private int maxForEachRow(int row,int[][] A){
int col = 0;
int max = A[row][0];
for(int i=0;i<A[row].length;i++){
if(A[row][i]>max){
max = A[row][i];
col = i;
}
} return col;
}
}

183. Wood Cut

https://www.lintcode.com/problem/wood-cut/description?_from=ladder&&fromId=4

public 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
*/
public int woodCut(int[] L, int k) {
// write your code here
if(L==null ||L.length ==0){
return 0;
} int l =0;
int r =0;
for(int len:L){
r= Math.max(r,len);
} while(l+1<r){
int mid = l+ (r-l)/2;
if(count(mid,L)>=k){
l = mid;
}else{
r = mid;
}
} if(count(r,L)>=k){
return r;
} return l;
} public int count(int len,int[] L){
int sum =0;
for(int wood:L){
sum+=wood/len;
} return sum;
}
}

437. Copy Books

https://www.lintcode.com/problem/copy-books/description?_from=ladder&&fromId=4

public class Solution {
/**
* @param pages: an array of integers
* @param k: An integer
* @return: an integer
*/
public int copyBooks(int[] pages, int k) {
// write your code here
if(pages==null || pages.length==0){
return 0;
} int start = getMaxPages(pages);
int end = getTotalPages(pages); while(start+1<end){
int mid = start + (end-start)/2;
if(countPeople(pages,mid)<=k){
end = mid;
}else{
start = mid;
}
} if(countPeople(pages,start)<=k){
return start;
}
return end;
} public int getTotalPages(int[]pages){
int sum =0;
for(int i =0;i<pages.length;i++){
sum+=pages[i];
}
return sum;
} public int getMaxPages(int[] pages){
int max =0;
for(int i=0;i<pages.length;i++){
max = Math.max(max,pages[i]);
}
return max;
} public int countPeople(int[] pages,int min){
int count =0;
int sum = 0;
for(int i =0;i<pages.length;i++){
if(sum+pages[i]>min){
count++;
sum = pages[i];
}else{
sum+=pages[i];
}
}
if(sum>0){
count++;
}
return count;
}
}

438. Copy Books II

https://www.lintcode.com/problem/copy-books-ii/description?_from=ladder&&fromId=4

public class Solution {
/**
* @param n: An integer
* @param times: an array of integers
* @return: an integer
*/
public int copyBooksII(int n, int[] times) {
// write your code here
if(times==null || times.length==0){
return 0;
} int start = getMin(times);
int end = start*n; while(start+1<end){
int mid = start +(end-start)/2;
if(canCopy(times,n,mid)){
end = mid;
}else{
start = mid;
}
} if(canCopy(times,n,start)){
return start;
} return end; } public int getMin(int[] times){
int min = Integer.MAX_VALUE;
for(int t:times){
min = Math.min(t,min);
}
return min;
} public boolean canCopy(int[] times,int n,int time){
int sum = 0;
for(int t:times){
sum += time/t;
} return sum>=n;
}
}

414. Divide Two Integers

https://www.lintcode.com/problem/divide-two-integers/description?_from=ladder&&fromId=4

1. 基本思想是不断地减掉除数,直到为0为止。但是这样会太慢。

2. 我们可以使用2分法来加速这个过程。不断对除数*2,直到它比被除数还大为止。加倍的同时,也记录下cnt,将被除数减掉加倍后的值,并且结果+cnt。

因为是2倍地加大,所以速度会很快,指数级的速度。

3. 另外要注意的是:最小值的越界问题。对最小的正数取abs,得到的还是它。。。 因为最小的正数的绝对值大于最大的正数(INT)

所以,我们使用Long来接住这个集合就可以了。

public class Solution {
/**
* @param dividend: the dividend
* @param divisor: the divisor
* @return: the result
*/
public int divide(int dividend, int divisor) {
// Note: 在这里必须先取long再abs,否则int的最小值abs后也是原值
long a = Math.abs((long)dividend);
long b = Math.abs((long)divisor); long ret =0;
// 这里必须是= 因为相等时也可以减
while(a>=b){
for(long deduce = b,cnt=1;deduce<=a;deduce<<=1,cnt<<=1){
a = a-deduce;
ret+=cnt;
}
} // 获取符号位。根据除数跟被除数的关系来定
// bug 4: out of range:
/*
Input: -2147483648, -1
Output: -2147483648
Expected: 2147483647
*/
ret = ((dividend > 0) ^ (divisor > 0)) ? -ret: ret;
//另一种写法
//ret = ((((dividend ^ divisor) >> 31) & 1) == 1) ? -ret: ret; if (ret > Integer.MAX_VALUE || ret < Integer.MIN_VALUE) {
return Integer.MAX_VALUE;
} else {
return (int)ret;
}
}
}

617. Maximum Average Subarray II

https://www.lintcode.com/problem/maximum-average-subarray-ii/description?_from=ladder&&fromId=4

public class Solution {
/**
* @param nums: an array with positive and negative numbers
* @param k: an integer
* @return: the maximum average
*/
public double maxAverage(int[] nums, int k) {
// write your code here
if(nums==null || nums.length ==0){
return 0d;
} double end =Double.MIN_VALUE;
double start = Double.MAX_VALUE;
for(int num:nums){
end = Math.max(end,num);
start = Math.min(start,num);
} double diff = 1e-6; while(start+diff<end){
double mid = start + (end-start)/2;
if(hasAverage(nums,k,mid)){
start = mid;
}else{
end = mid;
}
} if(hasAverage(nums,k,end)){
return end;
} return start;
} public boolean hasAverage(int[] nums, int k,double aver){
double preMin =0;
double preSum =0;
double sum =0;
for(int i=0;i<k;i++){
sum+=nums[i]-aver;
}
if(sum>=0){
return true;
} for(int i=k;i<nums.length;i++){
preSum += nums[i-k]-aver;
preMin= Math.min(preSum,preMin);
sum+= nums[i]-aver; if(sum-preMin>=0){
return true;
}
} return false;
}
}

633. Find the Duplicate Number

思路:设定重复的数字为x,那么x的特征是数组中比x小的个数>x, 而对于比x小的数y,应该是比y小的个数<=y

https://www.lintcode.com/problem/find-the-duplicate-number/description?_from=ladder&&fromId=4

public class Solution {
/**
* @param nums: an array containing n + 1 integers which is between 1 and n
* @return: the duplicate one
*/
public int findDuplicate(int[] nums) {
// write your code here
int l = 1;
int r = nums.length-1; while(l+1<r){
int mid = l +(r-l)/2;
if(count(nums,mid)<=mid){
l = mid;
}else{
r = mid;
}
} if(count(nums,l)>l){return l;}
return r;
} //<=mid的数字个数
private int count(int[] nums,int mid){
int cnt =0;
for(int item:nums){
if(item<=mid){
cnt++;
}
}
return cnt;
}
}

362. Sliding Window Maximum

思路:1.插入时,如果发现尾部<插入值,那么尾部无需保留,poll出去,这样会保留一个递减序列

2.最大值即为头部,没加入一个最大值,如果发现该值坐标为窗口第一个值,即下次滑动将不在窗口中,那么poll掉

3.用双端队列实现

https://www.lintcode.com/problem/sliding-window-maximum/description?_from=ladder&&fromId=4

public class Solution {
/**
* @param nums: A list of integers.
* @param k: An integer
* @return: The maximum number inside the window at each moving.
*/
public List<Integer> maxSlidingWindow(int[] nums, int k) {
// write your code here
List<Integer> res = new ArrayList<>();
if(nums==null || nums.length<k){
return res;
} //queue中记录下标
Deque<Integer> queue = new LinkedList<>(); for(int i=0;i<nums.length;i++){
while(!queue.isEmpty() && nums[i]>nums[queue.peekLast()]){
queue.pollLast();
}
queue.offer(i); if(i>=k-1){
res.add(nums[queue.peekFirst()]);
if(i-k+1==queue.peekFirst()){
queue.pollFirst();
}
} } return res; } }

二分难题 && deque的更多相关文章

  1. bzoj 2402: 陶陶的难题II 二分答案维护凸包

    2402: 陶陶的难题II Time Limit: 40 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 68  Solved: 45[Submi ...

  2. HDU 4604 Deque 二分最长上升子序列

    题目大意就是给一个deque 然后有n个数,依次进行操作,每种操作,你可以把这个数放在deque首部,也可以放在尾部,也可以扔掉不管,但是要保证deque中的数是非递减的.最要求deque中最长能是多 ...

  3. UVA 11610 Reverse Prime (数论+树状数组+二分,难题)

    参考链接http://blog.csdn.net/acm_cxlove/article/details/8264290http://blog.csdn.net/w00w12l/article/deta ...

  4. 【bzoj2402】陶陶的难题II 分数规划+树链剖分+线段树+STL-vector+凸包+二分

    题目描述 输入 第一行包含一个正整数N,表示树中结点的个数.第二行包含N个正实数,第i个数表示xi (1<=xi<=10^5).第三行包含N个正实数,第i个数表示yi (1<=yi& ...

  5. BZOJ 2402 陶陶的难题II (01分数规划+树剖+线段树+凸包+二分)

    题目大意:略 一定范围内求最大值,考虑二分答案 设现在选择的答案是$mid$,$max \left \{ \frac{yi+qj}{xi+pj} \right \} \geq mid $ 展开可得,$ ...

  6. bzoj2402 陶陶的难题II 分数规划+树剖+线段树维护凸壳+二分

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=2402 题解 看上去很像分数规划的模型.于是就二分吧.令 \[ \begin{align*}\f ...

  7. BZOJ1758 WC2010 重建计划 二分答案、点分治、单调队列

    传送门 看到平均数最大,自然地想到二分答案.那么我们的$check$函数就是要求:是否存在一条长度在$[L,U]$的路径,满足其权值和$\geq 0$. 看到长度在$[L,U]$,自然地想到点分治求解 ...

  8. 八数码难题之 A* 算法

    人生第一个A*算法-好激动-- 八数码难题--又称八数码水题,首先要理解一些东西: 1.状态可以转化成整数,比如状态: 1 2 3 4 5 6 7 8 0 可以转化成:123456780这个整数 2. ...

  9. BZOJ 1014: [JSOI2008]火星人prefix [splay 二分+hash] 【未完】

    1014: [JSOI2008]火星人prefix Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 6243  Solved: 2007[Submit] ...

随机推荐

  1. 20155318 2016-2017-2 《Java程序设计》第九学习总结

    20155318 2016-2017-2 <Java程序设计>第九学习总结 教材学习内容总结 学习目标 了解JDBC架构 掌握JDBC架构 掌握反射与ClassLoader 了解自定义泛型 ...

  2. POJ1087 A Plug for UNIX 2017-02-12 13:38 40人阅读 评论(0) 收藏

    A Plug for UNIX Description You are in charge of setting up the press room for the inaugural meeting ...

  3. (2)-生成JSONObject的方法

    生成JSONObject一般有两种方式,通过javabean或者map类型来生成.如下面的例子:   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 2 ...

  4. .NET中Debug模式与Release模式差别

    Debug里的PDB是full,保存着调试和项目状态信息.有断言.堆栈检查等代码.Release 里的PDB是pdb-only,基本上:出什么错了+错误在哪行. 因为很多人把PDB理解成:调试文件.P ...

  5. Markdown编辑器——常用语法

    Markdown是什么? 简短来说,他就是一款特别适用于写博客的编辑器.为什么适合呢,因为它特别的方便.以博客园的编辑界面来说,它原本的界面是这样的(有没有一种Word2003的既视感): 但是,当你 ...

  6. centos 中 mongodb 启动失败的修复

    mongodb是使用centos的yum命令安装的,整个的安装过程如下: 1. 运行 yum info mongo-10gen查看是否有mongodb源,如有跳至第3步. 2. 运行 vim /etc ...

  7. svn自动更新服务器最新代码

    1.很简单打开dos界面 cd到svn exe目录下,运行 cd C:\Program Files\TortoiseSVN\bin    --svn安装目录(作者使用时TortoiseSVN客户端,其 ...

  8. C#基础笔记(第十九天)

    1.CSS 层叠样式表 对HTML的补充实现网页内容和页面效果的彻底分离1.内联样式表(在标签内设置元素的样式)<p style="background:red; font-size: ...

  9. ceph pg_num 数值计算

    通常在创建pool之前,需要覆盖默认的pg_num,官方推荐: 若少于5个OSD, 设置pg_num为128. 5~10个OSD,设置pg_num为512. 10~50个OSD,设置pg_num为40 ...

  10. 苹果appID的获取方法

    1.登陆 iTunes Connect iTunes Connect 2.选择我的app 3.选择相应的应用 4.查看结果