leetcode today 1

947. Most Stones Removed with Same Row or Column

  • For each stone, union its column and row. Count the number of connected components.
  • union find

635. Design Log Storage System

  • Use TreeMap to sort the date, change the string value according to granularity
  • TreeMap, String

776. Split BST

  • Check whether tree root is put to the left part or the right part and change the root accordingly.
  • Recursion

529. Minesweeper

  • First check for 8 directions and calculate the mines count. If the count is 0, do classic BFS.
  • BFS

503. Next Greater Element II

  • Run two cycles. First cycle is the normal cycle; the second cycle never push and will pop if the current number not before it.
  • Stack

694. Number of Distinct Islands

  • Use string to represent the shape of island.
  • HashMap, DFS

230. Kth Smallest Element in a BST

  • inorder traversal

729. My Calendar I

  • Only check the previous start and the next start.
  • TreeMap

730-731. My Calendar II III

  • Start makes value + 1, end makes value - 1. Iterating and adding up all the start and end values to see the current active events.
  • TreeMap

304. Range Sum Query 2D - Immutable

  • Calculate the Presum of 2D array first, which is the 2D area starting from origin. Then any interim area can be calculated by Sum(ABCD)=Sum(OD)−Sum(OB)−Sum(OC)+Sum(OA)
  • Presum