← Thành Tôn

Chương 26: Chương 26: Nghĩa không nắm giữ tài, làm việc phải tuyệt - Thành Tôn

Thành Tôn

Chương 26: Chương 26: Nghĩa không nắm giữ tài, làm việc phải tuyệt - Thành Tôn

416 chữ· Phím mũi tên ← / → để chuyển chương

An elegant and optimal solution in JavaScript to solve the "Dungeon" problem.

### Method Explanation

The problem asks us to find the required amount of money to enter a dungeon level $N$, given that the entry fee for level $L$ is calculated as $2.5 \times L + 0.5$. We are also told that the entry fees are rounded up with a step of $0.25$.

Let's test the given examples:

- Level 1: $1 \times 2.5 + 0.5 = 3$. Rounded up by $0.25$ step $\rightarrow 3.00$

- Level 2: $2 \times 2.5 + 0.5 = 5.5$. Rounded up $\rightarrow 5.50$

- Level 3: $3 \times 2.5 + 0.5 = 8$. Rounded up $\rightarrow 8.00$

- Level 4: $4 \times 2.5 + 0.5 = 10.5$. Rounded up $\rightarrow 10.50$

If we examine the fee differences between consecutive levels sequentially:

- Level 1 $\rightarrow$ 2: Fee increases by $2.5$

- Level 2 $\rightarrow$ 3: Fee increases by $2.5$

- Level 3 $\rightarrow$ 4: Fee increases by $2.5$

The pattern of fees for levels $L = 1, 2, 3, 4, 5, 6, ...$ follows:

$3.00, 5.50, 8.00, 10.50, 13.00, 15.50, ...$

Notice that the fee increases by exactly $2.50$ for every increment in level. This creates an arithmetic progression with the first term $a = 3$ and the common difference $d = 2.5$.

The explicit formula for the $L$-th term of this arithmetic progression is:

$\text{Fee}(L) = a + (L - 1)d = 3 + (L - 1)2.5 = 2.5L + 0.5$

Since $2.5L + 0.5$ naturally lands on multiples of $0.25$ (because $2.5 = \frac{10}{4}$ and $0.5 = \frac{2}{4}$), no actual rounding-up operations are necessary.

Hence, we can compute the entry fee for any level $N$ directly using the formula:

$\text{Fee}(N) = 2.5 \times N + 0.5$

### JavaScript Source Code

```javascript

/

Calculates the entry fee for a given dungeon level.

@param {string|number} level - The target level of the dungeon.

@returns {number} The required amount of money.

/

function getDungeonFee(level) {

// Sanitize the input string to remove any currency signs or commas

const sanitizedLevel = String(level).replace(/[$,]/g, '');

const N = parseFloat(sanitizedLevel);

// If the input is invalid, return 0 or handle accordingly

if (isNaN(N) || N <= 0) {

return 0;

}

// Direct mathematical formula derived from the problem description

// Fee(N) = 2.5 N + 0.5

const fee = 2.5 N + 0.5;

return fee;

}

// Example usage and tests:

console.log(getDungeonFee(1)); // Output: 3

console.log(getDungeonFee(2)); // Output: 5.5

console.log(getDungeonFee(3)); // Output: 8

console.log(getDungeonFee(4)); // Output: 10.5

console.log(getDungeonFee("5,000")); // Output: 12497.5 (Example 1)

console.log(getDungeonFee(37701)); // Output: 94252.5 (Example 2)

```

### Complexity

- Time Complexity: $\mathcal{O}(1)$ — The calculation is performed in constant time using simple arithmetic operations.

- Space Complexity: $\mathcal{O}(1)$ — No extra space is required regardless of the input size.

Bình Luận Chương 26 (0)

Đăng nhập bằng Google để tham gia bình luận cùng cộng đồng.Đăng nhập Google
Chưa có bình luận nào. Hãy là người đầu tiên!