Lv.1queuesimulation
Last Card Standing
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
n cards numbered 1 to n are stacked with card 1 on top and card n at the bottom. Until one card remains, repeat:
- Discard the top card.
- Move the card that is now on top to the bottom.
Return the number of the last remaining card.
Examples
| n | result |
|---|---|
4 | 4 |
6 | 4 |
1 | 1 |
Example 1 — 1 2 3 4 → 3 4 2 → 2 4 → 4.
Efficiency
This problem has performance tests. Judge the required time complexity from the input sizes in the constraints.
Function signature
solution(n: int) → int
Constraints
-
n
Expected complexity
- Time
- O(N)
- Space
- O(N)
Examples
| n | Expected |
|---|---|
4 | 4 |
6 | 4 |
1 | 1 |
Hint
Hint
Removing from the front and appending to the back is exactly what a queue does in , so the whole process is .
Erasing the first element of an array (erase(begin), pop(0), remove(0)) shifts every other element and makes the
process , which exceeds the time limit. (List the answers for small n and you may spot a closed form.)
Time limits per language
| Language | Efficiency time limit | |
|---|---|---|
| C (GCC, gnu17) | 200 ms | estimated |
| C++ (GCC, gnu++23) | 200 ms | derived |
| Python 3.13 | 1000 ms | derived |
| Java 25 | 1000 ms | derived |
| Kotlin 2.4 (JVM) | 1400 ms | estimated |
| C# 14 (.NET 10) | 1400 ms | estimated |
Test groups
| Group | Weight | Tests | Visibility |
|---|---|---|---|
| sample | 0 | 3 | inputs and outputs |
| correctness | 40 | 10 | verdict only |
| performance | 60 | 3 | verdict only |