Press "Enter" to skip to content

Posts published in “Simulation”

花花酱 LeetCode 504. Base 7

Problem

Given an integer, return its base 7 string representation.

Example 1:

Input: 100
Output: "202"

Example 2:

Input: -7
Output: "-10"

Note:Ā The input will be in range of [-1e7, 1e7].

Solution: Simulation

Time complexity: O(logn)

Space complexity: O(logn)

 

花花酱 LeetCode 551. Student Attendance Record I

Problem

You are given a string representing an attendance record for a student. The record only contains the following three characters:

  1. ‘A’Ā : Absent.
  2. ‘L’Ā : Late.
  3. ‘P’Ā : Present.

A student could be rewarded if his attendance record doesn’t containĀ more than one ‘A’ (absent)Ā orĀ more than two continuous ‘L’ (late).

You need to return whether the student could be rewarded according to his attendance record.

Example 1:

Input: "PPALLP"
Output: True

Example 2:

Input: "PPALLL"
Output: False

Solution1: Simulation

Time complexity: O(n)

Space complexity: O(1)

Solution 2: Regex

 

花花酱 LeetCode 889. Spiral Matrix III

Problem

On a 2 dimensional grid withĀ RĀ rows andĀ CĀ columns, we start atĀ (r0, c0)Ā facing east.

Here, the north-west corner of the grid is at theĀ first row and column, and the south-east corner of the grid is at the last row and column.

Now, we walk in a clockwise spiral shape to visit every position in this grid.

Whenever we would move outside the boundary of the grid, we continue our walk outside the grid (but may return to the grid boundary later.)

Eventually, we reach allĀ R * CĀ spaces of the grid.

Return a list of coordinates representing the positions of the grid in the order they were visited.

 

Example 1:

Input: R = 1, C = 4, r0 = 0, c0 = 0
Output: [[0,0],[0,1],[0,2],[0,3]]


 

Example 2:

Input: R = 5, C = 6, r0 = 1, c0 = 4
Output: [[1,4],[1,5],[2,5],[2,4],[2,3],[1,3],[0,3],[0,4],[0,5],[3,5],[3,4],[3,3],[3,2],[2,2],[1,2],[0,2],[4,5],[4,4],[4,3],[4,2],[4,1],[3,1],[2,1],[1,1],[0,1],[4,0],[3,0],[2,0],[1,0],[0,0]]



Note:

  1. 1 <= R <= 100
  2. 1 <= C <= 100
  3. 0 <= r0 < R
  4. 0 <= c0 < C

Solution: Simulation

We can find out the moving sequence is ESWWNNEEESSSWWWWNNNN.

The pattern is 1,1,2,2,3,3,4,4,… steps in one direction, and turn right for 90 degrees.

directions are E,S,W,N,E,S,W,N…

Time complexity: O(max(R,C)^2)

Space complexity: O(1) or O(RC) if ans included.

 

花花酱 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 43. Multiply Strings

Problem

Given two non-negative integersĀ num1Ā andĀ num2Ā represented as strings, return the product ofĀ num1Ā andĀ num2, also represented as a string.

Example 1:

Input: num1 = "2", num2 = "3"
Output: "6"

Example 2:

Input: num1 = "123", num2 = "456"
Output: "56088"

Note:

  1. The length of bothĀ num1Ā andĀ num2Ā is < 110.
  2. BothĀ num1Ā andĀ num2Ā containĀ only digitsĀ 0-9.
  3. BothĀ num1Ā andĀ num2Ā do not contain any leading zero, except the number 0 itself.
  4. YouĀ must not use any built-in BigInteger libraryĀ orĀ convert the inputs to integerĀ directly.

Solution: Simulation

Simulate multiplication one digit at a time.

Time complexity: O(l1*l2)

Space complexity: O(l1 + l2)

C++