Lv.1arraydpmath
Best Pair of Viewpoints
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
Viewpoints stand in a row; viewpoint i (0-based) has score scores[i]. Visiting two viewpoints i < j is worth
. Return the maximum possible value.
Examples
| scores | result |
|---|---|
[8, 1, 5, 2, 6] | 11 |
[1, 2] | 2 |
[1000000000, 1, 1000000000] | 1999999998 |
Example 1 — i = 0, j = 2 gives . The result can exceed the 32-bit range.
Efficiency
This problem has performance tests. Judge the required time complexity from the input sizes in the constraints.
Function signature
solution(scores: int[]) → long
Constraints
-
scores.length -
scores[i] - The answer may not fit in 32 bits; use a 64-bit integer (
long).
Expected complexity
- Time
- O(N)
- Space
- O(1)
Examples
| scores | Expected |
|---|---|
[8,1,5,2,6] | 11 |
[1,2] | 2 |
[1000000000,1,1000000000] | 1999999998 |
Hint
Hint
Split the value into . Sweep j from left to right while keeping the best
scores[i] + i seen so far. Checking every pair is and exceeds the time limit; the sweep is .
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 | 1038 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 | 4 | verdict only |