Press "Enter" to skip to content

Posts tagged as “BST”

花花酱 LeetCode 915. Partition Array into Disjoint Intervals

Problem

Given an arrayĀ A, partition itĀ into two (contiguous) subarraysĀ leftĀ andĀ rightĀ so that:

  • Every element inĀ leftĀ is less than or equal to every element inĀ right.
  • leftĀ andĀ rightĀ are non-empty.
  • leftĀ has the smallest possible size.

Return theĀ lengthĀ ofĀ leftĀ after such a partitioning.Ā  It is guaranteed that such a partitioning exists.

Example 1:

Input: [5,0,3,8,6]
Output: 3
Explanation: left = [5,0,3], right = [8,6]

Example 2:

Input: [1,1,1,0,6,12]
Output: 4
Explanation: left = [1,1,1,0], right = [6,12]

Note:

  1. 2 <= A.lengthĀ <= 30000
  2. 0 <= A[i] <= 10^6
  3. It is guaranteed there is at least one way to partitionĀ AĀ as described.

Solution 1: BST

Time complexity: O(nlogn)

Space complexity: O(n)

C++

Solution 2: Greedy

Time complexity: O(n)

Space complexity: O(1)

C++

花花酱 LeetCode 855. Exam Room

Problem

In an exam room, there areĀ NĀ seats in a single row, numberedĀ 0, 1, 2, ..., N-1.

When a student enters the room, they must sit in the seat that maximizes the distance to the closest person.Ā  If there are multiple such seats, they sit in the seat with the lowest number.Ā  (Also, if no one is in the room, then the student sits at seat number 0.)

Return a classĀ ExamRoom(int N)Ā that exposes two functions:Ā ExamRoom.seat()Ā returning anĀ intĀ representing what seat the student sat in, andĀ ExamRoom.leave(int p)Ā representing that the student in seat numberĀ pĀ now leaves the room.Ā  It is guaranteed that any calls toĀ ExamRoom.leave(p)Ā have a student sitting in seatĀ p.

Example 1:

Input: ["ExamRoom","seat","seat","seat","seat","leave","seat"], [[10],[],[],[],[],[4],[]]
Output: [null,0,9,4,2,null,5]
Explanation:
ExamRoom(10) -> null
seat() -> 0, no one is in the room, then the student sits at seat number 0.
seat() -> 9, the student sits at the last seat number 9.
seat() -> 4, the student sits at the last seat number 4.
seat() -> 2, the student sits at the last seat number 2.
leave(4) -> null
seat() -> 5, the studentā€‹ā€‹ā€‹ā€‹ā€‹ā€‹ā€‹ sits at the last seat number 5.

ā€‹ā€‹ā€‹ā€‹Note:

  1. 1 <= N <= 10^9
  2. ExamRoom.seat()Ā andĀ ExamRoom.leave()Ā will be called at mostĀ 10^4Ā times across all test cases.
  3. Calls toĀ ExamRoom.leave(p)Ā are guaranteed to have a student currently sitting in seat numberĀ p.

Solution: BST

Use a BST (ordered set) to track the current seatings.

Time Complexity:

init: O(1)

seat: O(P)

leave: O(logP)

Space complexity: O(P)

 

花花酱 LeetCode 701. Insert into a Binary Search Tree

Problem

Given the root node of a binary search tree (BST) and a value to be inserted into the tree,Ā insert the value into the BST. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST.

Note that there may existĀ multiple valid ways for theĀ insertion, as long as the tree remains a BST after insertion. You can return any of them.

For example,

You can return this binary search tree:

This tree is also valid:

Solution: Recursion

Time complexity: O(logn ~ n)

Space complexity: O(logn ~ n)

 

花花酱 LeetCode 703. Kth Largest Element in a Stream

Problem

Design a class to findĀ theĀ kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth distinct element.

YourĀ KthLargestĀ class will have a constructor which accepts an integerĀ kĀ and an integer arrayĀ nums, which contains initial elements fromĀ the stream. For each call to the methodĀ KthLargest.add, return the element representing the kth largest element in the stream.

Example:

int k = 3;
int[] arr = [4,5,8,2];
KthLargest kthLargest = new KthLargest(3, arr);
kthLargest.add(3);Ā  Ā // returns 4
kthLargest.add(5);Ā  Ā // returns 5
kthLargest.add(10);Ā  // returns 5
kthLargest.add(9);Ā  Ā // returns 8
kthLargest.add(4);Ā  Ā // returns 8

Note:Ā 
You may assume thatĀ nums‘ lengthĀ ā‰„Ā k-1Ā andĀ kĀ ā‰„Ā 1.

Solution: BST / Min Heap

Time complexity: O(nlogk)

Space complexity: O(k)

C++ / BST

C++ / Min Heap

 

 

花花酱 LeetCode 700. Search in a Binary Search Tree

Problem

Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the node’s value equals the given value. Return the subtree rooted with that node. If such node doesn’t exist, you should return NULL.

For example,

Given the tree:
        4
       / \
      2   7
     / \
    1   3

And the value to search: 2

You should return this subtree:

      2     
     / \   
    1   3

In the example above, if we want to search the valueĀ 5, since there is no node with valueĀ 5, we should returnĀ NULL.

Solution: Recursion

Time complexity: O(logn ~ n)

Space complexity: O(logn ~ n)