2 - Binary Search & LogN Algorithm

254. Drop Eggs

https://www.lintcode.com/problem/drop-eggs/description?_from=ladder&&fromId=1

28. Search a 2D Matrix

https://www.lintcode.com/problem/search-a-2d-matrix/description?_from=ladder&&fromId=1

思路1:

  1. find the row index, the last number <= target

  2. find the column index, the number equal to target

public class Solution {
    /**
     * @param matrix: matrix, a list of lists of integers
     * @param target: An integer
     * @return: a boolean, indicate whether matrix contains target
     */
    public boolean searchMatrix(int[][] matrix, int target) {
        // write your code here
        if(matrix == null || matrix.length == 0) return false;
        int left = 0, right = matrix[0].length - 1;
        int row = findRow(matrix, target);
        if(matrix[row][0] > target || matrix[row][right] < target) {
            return false;
        }
        while(left + 1 < right) {
            int mid = left + (right - left) / 2;
            if(matrix[row][mid] == target) {
                return true;
            } else if(matrix[row][mid] > target) {
                right = mid;
            } else {
                left = mid;
            }
        }
        if(matrix[row][left] == target || matrix[row][right] == target) {
            return true;
        } else {
            return false;
        }
    }

    public int findRow(int[][] matrix, int target) {
        int top = 0, bottom = matrix.length - 1, right = matrix[0].length - 1;
        while(top + 1 < bottom) {
            int mid = top + (bottom - top) / 2;
            if(matrix[mid][right] == target) {
                return mid;
            } else if(matrix[mid][right] > target) {
                bottom = mid;
            } else {
                top = mid;
            }
        }
        if(matrix[top][right] >= target) {
            return top;
        } else {
            return bottom;
        }
    }
}

思路2:

1. 可以看作是一个有序数组被分成了n段,每段就是一行。因此依然可以二分求解。

对每个数字,根据其下标 i,j 进行编号,每个数字可被编号为 0 ~ n *(n - 1)

2. 相当于是在一个数组中的下标,然后直接像在数组中二分一样来做。取得 mid 要还原成二维数组中的下标,i = mid / n, j = mid % n

3. int start = 0, end = row * row * column - 1;

int number = matrix[mid / column][mid % column];

// Binary Search Once
public class Solution {
    /**
     * @param matrix, a list of lists of integers
     * @param target, an integer
     * @return a boolean, indicate whether matrix contains target
     */
    public boolean searchMatrix(int[][] matrix, int target) {
        // write your code here
        if(matrix == null || matrix.length == 0){
            return false;
        }

        if(matrix[0] == null || matrix[0].length == 0){
            return false;
        }

        int row = matrix.length;
        int column = matrix[0].length;

        int start = 0, end = row * column - 1;
        while(start <= end){
            int mid = start + (end - start) / 2;
            int number = matrix[mid / column][mid % column];
            if(number == target){
                return true;
            }else if(number > target){
                end = mid - 1;
            }else{
                start = mid + 1;
            }
        }

        return false;

    }
}

14. First Position of Target 

https://www.lintcode.com/problem/first-position-of-target/description?_from=ladder&&fromId=1

思路:这道题很简单。套用二分法的模版即可

 1 public class Solution {
 2     /**
 3      * @param nums: The integer array.
 4      * @param target: Target to find.
 5      * @return: The first position of target. Position starts from 0.
 6      */
 7     public int binarySearch(int[] nums, int target) {
 8         // write your code here
 9         if(nums == null || nums.length == 0) return -1;
10         int start = 0, end = nums.length - 1;
11         while(start + 1 < end) {
12             int mid = start + (end - start) / 2;
13             if(nums[mid] >= target) {
14                 end = mid;
15             } else {
16                 start = mid;
17             }
18         }
19         if(nums[start] == target) {
20             return start;
21         }
22         if(nums[end] ==  target) {
23             return end;
24         }
25         return -1;
26     }
27 }

414. Divide Two Integers

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

1. 凡是要移位,要做的第一件事就是把 int 转换成 long,为了防止移位时溢出。

2. 基本思路是利用减法,看看被除数可以减去多少次除数。使用倍增的思想优化,可以将减法的次数优化到对数的时间复杂度。

3. 我们将除数左移一位(或加上它自己),即得到了二倍的除数,这时一次相当于减去了两个除数,通过不断倍增,时间复杂度很优秀。

4. 与此同时,还需要一个变量记录此时的除数是最初除数的多少倍,每次减法后都加到结果上即可。

 1 public class Solution {
 2     /**
 3      * @param dividend: the dividend
 4      * @param divisor: the divisor
 5      * @return: the result
 6      */
 7     public int divide(int dividend, int divisor) {
 8         // write your code here
 9         if(divisor == 0) {
10             return dividend >= 0 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
11         }
12         if(dividend == 0) {
13             return 0;
14         }
15         if(divisor == -1 && dividend == Integer.MIN_VALUE) {
16             return Integer.MAX_VALUE;
17         }
18         boolean isNegative = ((divisor > 0 && dividend < 0) || (divisor < 0 && dividend > 0)) ? true : false;
19         long divisorL = Math.abs((long)divisor);
20         long dividendL = Math.abs((long)dividend);
21         int result = 0;
22         while(dividendL >= divisorL) {
23             int shift = 0;
24             while(dividendL >= (divisorL << shift)) {
25                 shift++;
26             }
27             result += 1 << (shift - 1);
28             dividendL -= divisorL << (shift - 1);
29         }
30         if(isNegative) {
31             return result * (-1);
32         }
33         return result;
34     }
35 }

61. Search for a Range

https://www.lintcode.com/problem/search-for-a-range/description?_from=ladder&&fromId=1

这道题同样很简单,套用模版即可。

 1 public class Solution {
 2     /**
 3      * @param A: an integer sorted array
 4      * @param target: an integer to be inserted
 5      * @return: a list of length 2, [index1, index2]
 6      */
 7     public int[] searchRange(int[] A, int target) {
 8         // write your code here
 9         if(A == null || A.length == 0) return new int[]{-1, -1};
10         int start = findStart(A, target);
11         int end = findEnd(A, target);
12         return new int[]{start, end};
13
14     }
15
16     public int findStart(int[] A, int target) {
17         int start = 0;
18         int end = A.length - 1;
19         while(start + 1 < end) {
20             int mid = start + (end - start) / 2;
21             if(A[mid] < target) {
22                 start = mid;
23             } else {
24                 end = mid;
25             }
26         }
27         if(A[start] == target) {
28             return start;
29         }
30         if(A[end] == target) {
31             return end;
32         }
33         return -1;
34     }
35
36     public int findEnd(int[] A, int target) {
37         int start = 0;
38         int end = A.length - 1;
39         while(start + 1 < end) {
40             int mid = start + (end - start) / 2;
41             if(A[mid] <= target) {
42                 start = mid;
43             } else {
44                 end = mid;
45             }
46         }
47         if(A[end] == target) {
48             return end;
49         }
50         if(A[start] == target) {
51             return start;
52         }
53         return -1;
54     }
55 }

原文地址:https://www.cnblogs.com/jenna/p/10733567.html

时间: 2024-10-15 07:48:28

2 - Binary Search & LogN Algorithm的相关文章

[Algorithms] Binary Search Algorithm using TypeScript

(binary search trees) which form the basis of modern databases and immutable data structures. Binary search works very much the way humans intuitively search for a name in a yellow pages directory (if you have ever seen one) or the dictionary. In thi

[Algorithm] Delete a node from Binary Search Tree

The solution for the problem can be divided into three cases: case 1: if the delete node is leaf node, then we can simply remove it case 2: if the delete node is has single side or substree case 3: it has two children, then to keep it as a valid bina

[Leetcode] Binary search, Divide and conquer--240. Search a 2D Matrix II

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in ascending from left to right. Integers in each column are sorted in ascending from top to bottom.

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 ele

ZOJ - 2243 - Binary Search Heap Construction

先上题目: Binary Search Heap Construction Time Limit: 5 Seconds      Memory Limit: 32768 KB Read the statement of problem G for the definitions concerning trees. In the following we define the basic terminology of heaps. A heap is a tree whose internal n

[leetcode-95-Unique Binary Search Trees II]

Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 思路: This probl

九章算法系列(#2 Binary Search)-课堂笔记

前言 先说一些题外的东西吧.受到春跃大神的影响和启发,推荐了这个算法公开课给我,晚上睡觉前点开一看发现课还有两天要开始,本着要好好系统地学习一下算法,于是就爬起来拉上两个小伙伴组团报名了.今天听了第一节课,说真的很实用,特别是对于我这种算法不扎实,并且又想找工作,提高自己的情况. 那就不多说废话了,以后每周都写个总结吧,就趁着这一个月好好把算法提高一下.具体就从:课堂笔记.leetcode和lintcode相关习题.hdu和poj相关习题三个方面来写吧.希望自己能够坚持下来,给大家分享一些好的东

编程算法 - 二叉搜索树(binary search tree) 代码(C)

二叉搜索树(binary search tree) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 二叉搜索树(binary search tree)能够高效的进行插入, 查询, 删除某个元素, 时间复杂度O(logn). 简单的实现方法例如以下. 代码: /* * main.cpp * * Created on: 2014.7.20 * Author: spike */ /*eclipse cdt, gcc 4.8.1*/ #include <s

4.7 Lowest Common Ancestor of a Binary Search Tree 二叉树的最小共同父节点

4.7 Design an algorithm and write code to find the first common ancestor of two nodes in a binary tree. Avoid storing additional nodes in a data structure. NOTE: This is not necessarily a binary search tree. LeetCode上的原题,请参见我之前的博客Lowest Common Ancest