Press "Enter" to skip to content

Posts tagged as “simulation”

花花酱 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++

 

花花酱 LeetCode 2. Add Two Numbers

Problem

You are given twoĀ non-emptyĀ linked lists representing two non-negative integers. The digits are stored inĀ reverse orderĀ and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

Solution: Simulation

Simulate the addition, draw two numbers (one each) from l1, l2, if list is empty draw 0.

Using a dummy head makes things easier.

Time complexity: O(max(l1,l2))

Space complexity: O(max(l1,l2))

C++

Java

Python3

Related Problems

花花酱 LeetCode 874. Walking Robot Simulation

Problem

A robot on an infinite grid starts at point (0, 0) and faces north.Ā  The robot can receive one of three possible types of commands:

  • -2: turn left 90 degrees
  • -1: turn right 90 degrees
  • 1 <= x <= 9: move forwardĀ xĀ units

Some of the grid squares are obstacles.

TheĀ i-th obstacle is at grid pointĀ (obstacles[i][0], obstacles[i][1])

If the robot would try to move onto them, the robot stays on the previous grid square instead (but still continues following the rest of the route.)

Return theĀ squareĀ of the maximum Euclidean distance that the robot will be from the origin.

Example 1:

Input: commands = [4,-1,3], obstacles = []
Output: 25
Explanation: robot will go to (3, 4)

Example 2:

Input: commands = [4,-1,4,-2,4], obstacles = [[2,4]]
Output: 65
Explanation: robot will be stuck at (1, 4) before turning left and going to (1, 8)

Note:

  1. 0 <= commands.length <= 10000
  2. 0 <= obstacles.length <= 10000
  3. -30000 <= obstacle[i][0] <= 30000
  4. -30000 <= obstacle[i][1] <= 30000
  5. The answer is guaranteed to be less thanĀ 2 ^ 31.

Solution: Simulation

Time complexity: O(n + sum(x)) = O(n)

Space complexity: O(n)

C++

 

花花酱 LeetCode 445. Add Two Numbers II

Problem

You are given twoĀ non-emptyĀ linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.

Example:

Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7

Solution: Simulation

Using a stack to “reverse” the list. Simulate the addition digit by digit.

Time complexity: O(l1 + l2)

Space complexity: O(l1 + l2)

C++

Related Problems

花花酱 LeetCode 860. Lemonade Change

Problem

At a lemonade stand, each lemonade costsĀ $5.

Customers are standing in a queue to buy from you, and order one at a time (in the order specified byĀ bills).

Each customer will only buy one lemonade andĀ pay with either aĀ $5,Ā $10, orĀ $20Ā bill.Ā  You must provide the correct change to each customer, so that the net transaction is that the customer pays $5.

Note that you don’t have any changeĀ in hand at first.

ReturnĀ trueĀ if and only if you can provide every customer with correct change.

 

Example 1:

Input: [5,5,5,10,20]
Output: true
Explanation: 
From the first 3 customers, we collect three $5 bills in order.
From the fourth customer, we collect a $10 bill and give back a $5.
From the fifth customer, we give a $10 bill and a $5 bill.
Since all customers got correct change, we output true.

Example 2:

Input: [5,5,10]
Output: true

Example 3:

Input: [10,10]
Output: false

Example 4:

Input: [5,5,10,10,20]
Output: false
Explanation: 
From the first two customers in order, we collect two $5 bills.
For the next two customers in order, we collect a $10 bill and give back a $5 bill.
For the last customer, we can't give change of $15 back because we only have two $10 bills.
Since not every customer received correct change, the answer is false.

Solution: Simulation + Greedy

Always use 10 bill first.

Time complexity: O(n)

Space complexity: O(1)

C++