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
  • PriorityQueue
  • Comparator: 指定comparator物件告知如何排序,改寫compare
  • Comparable:
  • Python

Was this helpful?

7. Heap

  • Insert and delete: O(logn)

  • lookup: O(1)

  • k largest elements, store in minHeap. If element > k, drop the smallest

PriorityQueue

  • add

  • poll: retrieve and remove head of the queue

  • peek: retrieve but not remove the head of queue

Comparator: 指定comparator物件告知如何排序,改寫compare

  • PriorityQueue

PriorityQueue heap = new PriorityQueue(k, new Comparator<String>()
  public int compare(String s1, String s2){});

Comparable:

物件本身繼承comparable,在class裡改寫compareTo. e.g. Arrays.sort(A), Collections.sort(C)

public static class Star implements Comparable<Star>{
   private double x, y, z;

   Star(double x, double y, double z){
       this.x = x;
       this.y = y;
       this.z = z;
   }

   public double distance(){
       return Math.sqrt(x*x + y*y + z*z);
   }

   @Override
   public int compareTo(Star rhs){
       return Double.compare(this.distance(), rhs.distance());
       // return Double.compare(rhs.distance(), this.distance()); // decreasing

   }
}

Python

  • class 必須實作 def __lt__(self, other)

from queue import PriorityQueue

class ListNode:
    def __init__(self, x):
     self.val = x
     self.next = None
     
class Wrapper():
    def __init__(self, node):
        self.node = node
    def __lt__(self, other):
        return self.node.val < other.node.val

PreviousVertical Order TraverseNextGeneal

Last updated 5 years ago

Was this helpful?