Code Interview Note
  • 0. Introduction
  • 1. Basic
    • Python Basic
    • Java Basic
    • Primitive Type
    • Basic Question
    • Number
  • 2. Array and Numbers
    • General
    • TwoSum
    • Buy and Sell Stock
    • SubArray
      • SubArray + HashMap
    • Sliding Window
      • Sliding Window At Most Problem
    • Word Break
    • Passes Problem
    • Majority Element
    • Partition Array
    • Sort Colors
    • Anagram
    • Ugly Number
    • TwoPointer
    • Swipe Line
    • Calculator
    • Sudoku
  • 2.1 String
    • String
    • Palindrome
    • Parentheses
    • Decode String
    • Calculator
    • Abbreviation
  • 3. Linkedlist
    • Dummy Node
    • Double Pointers
  • 4. Stack and Queue
    • General
    • Increase/Decrease Stack
  • 5. Binary Search
    • General
    • BS on result
    • Save the half which has result
    • Rotated Sorted Array
    • Split Array Largest Sum
  • 6. Binary Tree
    • General
    • Path Sum
    • Lowest Common Ancestor
    • BST
    • Convert
    • Traverse
    • Valid Ordered Tree
    • Construct Binary Tree
    • Tree depth and width
    • Vertical Order Traverse
  • 7. Heap
    • Geneal
    • TopK
  • 8. Simulation
    • General
    • Read4
    • Encode Decode
    • LRU/LFU
    • Robot
    • GetRandom O(1)
    • Probability
  • 9. DFS
    • Backtrack
    • General
    • Subset
    • Permutation
    • Combination
  • 10. HashTable
    • General
  • 11. Sort
    • General
  • 12. Recursion
    • General
  • 13. Dynamic Programming
    • Graph
    • General
    • Coordinate
    • Double Sequence
    • Longest Common Subsequence
    • Rolling Array
    • House Robber
    • Backpack
    • Memorization
    • Diagonal
  • 14. BFS
    • General
    • Number of Islands
    • The Maze
  • 15. Graph
    • Shortest Path
    • Undirected Graph
    • Topology Sort
    • Word Ladder
    • Tarjan's Algo
  • 16. Divide & Conquer
    • General
  • 17. UnionFind
    • General
    • Grouping
  • 18. Trie
    • General
    • Word Square
  • 19. Company Summary
    • Oracle
    • Amazon
      • DP
    • Google
    • Hackerrank
    • LinkedIn
  • 20. Design
  • 21. Math
  • Behavior Question
  • Internet
  • OS
Powered by GitBook
On this page
  • Find maximum or minimum
  • 69 Sqrt(x)
  • First bad version
  • Wood cut

Was this helpful?

  1. 5. Binary Search

BS on result

Find maximum or minimum

No list, find result based on binary search

69 Sqrt(x)

  • Similar code, but if (mid < x/mid)

  • Jump out the loop. if (end*end <= x) return end

public int mySqrt(int x) {
    if(x == 0) return 0;
    int start = 0, end = x;

    while(start+1<end){
        int mid = start + (end-start)/2;
        if(mid == x/mid) return mid; // mid移項避免int越界
        else if(mid > x/mid) end = mid;
        else start = mid;
    }
    if(end <= x/end) return end;
    return start;
}

First bad version

Given a boolean function isBadVersion

public int findFirstBadVersion(int n) {
    // write your code here
    long start = 1, end = n;
    while(start + 1 < end){
        long mid = (start + end) / 2;
        if(SVNRepo.isBadVersion((int)mid)){
            end = mid;
        }else{
            start = mid;
        }
    }

    // 2 case [false, true], [true, true]
    if(SVNRepo.isBadVersion((int)start)){
        return (int) start;
    }else{
        return (int) end;
    }
}

Wood cut

Given an integer array representing different length of wood. Cut them into small pieces to guarantee you could have equal or more than k pieces with the same length

  • create function to count the piece sum of L

  • if count >= k, means k is small, set start = mid

public int woodCut(int[] L, int k) {
    // find the longest wood
    int max = 0;
    for (int i = 0; i < L.length; i++){
        max = Math.max(L[i], max);
    }

    int start = 1, end = max;

    while(start + 1 < end){
        int mid = start + (end-start)/2;
        if (count(L, mid) >= k){
            start = mid;
        }else{
            end = mid;
        }
    }

    if(count(L, start) >= k){
        return start;
    }
    if(count(L, end) >= k){
        return end;
    }
    return 0;
}

    // count the total pieces of wood length L

private int count(int[] L, int len){
    int piece = 0;
    for (int i = 0; i < L.length; i++){
        piece += (int)L[i]/len;
    }

    return piece;
}
PreviousGeneralNextSave the half which has result

Last updated 5 years ago

Was this helpful?