# Posts tagged as “simulation”

You have a bomb to defuse, and your time is running out! Your informer will provide you with a circular array code of length of n and a key k.

To decrypt the code, you must replace every number. All the numbers are replaced simultaneously.

• If k > 0, replace the ith number with the sum of the next k numbers.
• If k < 0, replace the ith number with the sum of the previous k numbers.
• If k == 0, replace the ith number with 0.

As code is circular, the next element of code[n-1] is code, and the previous element of code is code[n-1].

Given the circular array code and an integer key k, return the decrypted code to defuse the bomb!

Example 1:

Input: code = [5,7,1,4], k = 3
Output: [12,10,16,13]
Explanation: Each number is replaced by the sum of the next 3 numbers. The decrypted code is [7+1+4, 1+4+5, 4+5+7, 5+7+1]. Notice that the numbers wrap around.


Example 2:

Input: code = [1,2,3,4], k = 0
Output: [0,0,0,0]
Explanation: When k is zero, the numbers are replaced by 0.


Example 3:

Input: code = [2,4,9,3], k = -2
Output: [12,5,6,13]
Explanation: The decrypted code is [3+9, 2+3, 4+2, 9+4]. Notice that the numbers wrap around again. If k is negative, the sum is of the previous numbers.


Constraints:

• n == code.length
• 1 <= n <= 100
• 1 <= code[i] <= 100
• -(n - 1) <= k <= n - 1

## Solution 1: Simulation

Time complexity: O(n*k)
Space complexity: O(n)

## C++

You are given an integer n. An array nums of length n + 1 is generated in the following way:

• nums = 0
• nums = 1
• nums[2 * i] = nums[i] when 2 <= 2 * i <= n
• nums[2 * i + 1] = nums[i] + nums[i + 1] when 2 <= 2 * i + 1 <= n

Returnthe maximum integer in the array nums​​​.

Example 1:

Input: n = 7
Output: 3
Explanation: According to the given rules:
nums = 0
nums = 1
nums[(1 * 2) = 2] = nums = 1
nums[(1 * 2) + 1 = 3] = nums + nums = 1 + 1 = 2
nums[(2 * 2) = 4] = nums = 1
nums[(2 * 2) + 1 = 5] = nums + nums = 1 + 2 = 3
nums[(3 * 2) = 6] = nums = 2
nums[(3 * 2) + 1 = 7] = nums + nums = 2 + 1 = 3
Hence, nums = [0,1,1,2,1,3,2,3], and the maximum is 3.


Example 2:

Input: n = 2
Output: 1
Explanation: According to the given rules, the maximum between nums, nums, and nums is 1.


Example 3:

Input: n = 3
Output: 2
Explanation: According to the given rules, the maximum between nums, nums, nums, and nums is 2.


Constraints:

• 0 <= n <= 100

## Solution: Simulation

Generate the array by the given rules.

Time complexity: O(n)
Space complexity: O(n)

## C++

You have k servers numbered from 0 to k-1 that are being used to handle multiple requests simultaneously. Each server has infinite computational capacity but cannot handle more than one request at a time. The requests are assigned to servers according to a specific algorithm:

• The ith (0-indexed) request arrives.
• If all servers are busy, the request is dropped (not handled at all).
• If the (i % k)th server is available, assign the request to that server.
• Otherwise, assign the request to the next available server (wrapping around the list of servers and starting from 0 if necessary). For example, if the ith server is busy, try to assign the request to the (i+1)th server, then the (i+2)th server, and so on.

You are given a strictly increasing array arrival of positive integers, where arrival[i] represents the arrival time of the ith request, and another array load, where load[i] represents the load of the ith request (the time it takes to complete). Your goal is to find the busiest server(s). A server is considered busiest if it handled the most number of requests successfully among all the servers.

Return a list containing the IDs (0-indexed) of the busiest server(s). You may return the IDs in any order.

Example 1:

Input: k = 3, arrival = [1,2,3,4,5], load = [5,2,3,3,3]
Output: 
Explanation:
All of the servers start out available.
The first 3 requests are handled by the first 3 servers in order.
Request 3 comes in. Server 0 is busy, so it's assigned to the next available server, which is 1.
Request 4 comes in. It cannot be handled since all servers are busy, so it is dropped.
Servers 0 and 2 handled one request each, while server 1 handled two requests. Hence server 1 is the busiest server.


Example 2:

Input: k = 3, arrival = [1,2,3,4], load = [1,2,1,2]
Output: 
Explanation:
The first 3 requests are handled by first 3 servers.
Request 3 comes in. It is handled by server 0 since the server is available.
Server 0 handled two requests, while servers 1 and 2 handled one request each. Hence server 0 is the busiest server.


Example 3:

Input: k = 3, arrival = [1,2,3], load = [10,12,11]
Output: [0,1,2]
Explanation: Each server handles a single request, so they are all considered the busiest.


Example 4:

Input: k = 3, arrival = [1,2,3,4,8,9,10], load = [5,2,10,3,1,2,2]
Output: 


Example 5:

Input: k = 1, arrival = , load = 
Output: 


Constraints:

• 1 <= k <= 105
• 1 <= arrival.length, load.length <= 105
• arrival.length == load.length
• 1 <= arrival[i], load[i] <= 109
• arrival is strictly increasing.

## Solution: Heap + TreeSet

Use a min heap to store the release time -> server.
Use a treeset to track the current available servers.
For reach request, check whether servers can be released at that time.

Time complexity: O(nlogk)
Space complexity: O(k)

## C++

Design a parking system for a parking lot. The parking lot has three kinds of parking spaces: big, medium, and small, with a fixed number of slots for each size.

Implement the ParkingSystem class:

• ParkingSystem(int big, int medium, int small) Initializes object of the ParkingSystem class. The number of slots for each parking space are given as part of the constructor.
• bool addCar(int carType) Checks whether there is a parking space of carType for the car that wants to get into the parking lot. carType can be of three kinds: big, medium, or small, which are represented by 12, and 3 respectively. A car can only park in a parking space of its carType. If there is no space available, return false, else park the car in that size space and return true.

Example 1:

Input
[[1, 1, 0], , , , ]
Output
[null, true, true, false, false]


Explanation ParkingSystem parkingSystem = new ParkingSystem(1, 1, 0); parkingSystem.addCar(1); // return true because there is 1 available slot for a big car parkingSystem.addCar(2); // return true because there is 1 available slot for a medium car parkingSystem.addCar(3); // return false because there is no available slot for a small car parkingSystem.addCar(1); // return false because there is no available slot for a big car. It is already occupied.

Constraints:

• 0 <= big, medium, small <= 1000
• carType is 12, or 3
• At most 1000 calls will be made to addCar

## Solution: Simulation

Time complexity: O(1) per addCar call
Space complexity: O(1)

## Python3

You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars.

You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times before customers[i] arrive. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again.

You can stop the wheel at any time, including before serving all customers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation.

Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return -1.

Example 1:

Input: customers = [8,3], boardingCost = 5, runningCost = 6
Output: 3
Explanation: The numbers written on the gondolas are the number of people currently there.
1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 *$6 = $14. 2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 *$5 - 2 * $6 =$28.
3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 *$6 = $37. The highest profit was$37 after rotating the wheel 3 times.

Example 2:

Input: customers = [10,9,6], boardingCost = 6, runningCost = 4
Output: 7
Explanation:
1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 *$4 = $20. 2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 *$6 - 2 * $4 =$40.
3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 *$4 = $60. 4. 4 board and 9 wait, the wheel rotates. Current profit is 16 *$6 - 4 * $4 =$80.
5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 *$4 = $100. 6. 4 board and 1 waits, the wheel rotates. Current profit is 24 *$6 - 6 * $4 =$120.
7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 *$4 = $122. The highest profit was$122 after rotating the wheel 7 times.



Example 3:

Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92
Output: -1
Explanation:
1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 *$92 = -$89. 2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 *$1 - 2 * $92 = -$177.
3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 *$92 = -$269. 4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 12 *$1 - 4 * $92 = -$356.
5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 *$92 = -$447. The profit was never positive, so return -1.  Example 4: Input: customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8 Output: 9 Explanation: 1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 *$3 - 1 * $8 =$4.
2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * $3 - 2 *$8 = $8. 3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 *$3 - 3 * $8 =$12.
4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * $3 - 4 *$8 = $16. 5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 *$3 - 5 * $8 =$20.
6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * $3 - 6 *$8 = $24. 7. 4 board and 9 wait, the wheel rotates. Current profit is 28 *$3 - 7 * $8 =$28.
8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * $3 - 8 *$8 = $32. 9. 4 board and 1 waits, the wheel rotates. Current profit is 36 *$3 - 9 * $8 =$36.
10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * $3 - 10 *$8 = $31. The highest profit was$36 after rotating the wheel 9 times.


Constraints:

• n == customers.length
• 1 <= n <= 105
• 0 <= customers[i] <= 50
• 1 <= boardingCost, runningCost <= 100

## Solution: Simulation

Process if waiting customers > 0 or i < n.

Pruning, if runningCost > 4 * boardingCost (max revenue), there is no way to make profit.

Time complexity: sum(consumers) / 4
Space complexity: O(1)

## C++

Mission News Theme by Compete Themes.