3771. Total Score of Dungeon Runs Medium

@problem@discussion
#Array#Binary Search#Prefix Sum



1/**
2 * [3771] Total Score of Dungeon Runs
3 *
4 * You are given a positive integer hp and two positive 1-indexed integer arrays damage and requirement.
5 * There is a dungeon with n trap rooms numbered from 1 to n. Entering room i reduces your health points by damage[i]. After that reduction, if your remaining health points are at least requirement[i], you earn 1 point for that room.
6 * Let score(j) be the number of points you get if you start with hp health points and enter the rooms j, j + 1, ..., n in this order.
7 * Return the integer score(1) + score(2) + ... + score(n), the sum of scores over all starting rooms.
8 * Note: You cannot skip rooms. You can finish your journey even if your health points become non-positive.
9 *  
10 * <strong class="example">Example 1:
11 * <div class="example-block">
12 * Input: <span class="example-io">hp = 11, damage = [3,6,7], requirement = [4,2,5]</span>
13 * Output: <span class="example-io">3</span>
14 * Explanation:
15 * score(1) = 2, score(2) = 1, score(3) = 0. The total score is 2 + 1 + 0 = 3.
16 * As an example, score(1) = 2 because you get 2 points if you start from room 1.
17 * 
18 * 	You start with 11 health points.
19 * 	Enter room 1. Your health points are now 11 - 3 = 8. You get 1 point because 8 >= 4.
20 * 	Enter room 2. Your health points are now 8 - 6 = 2. You get 1 point because 2 >= 2.
21 * 	Enter room 3. Your health points are now 2 - 7 = -5. You do not get any points because -5 < 5.
22 * </div>
23 * <strong class="example">Example 2:
24 * <div class="example-block">
25 * Input: <span class="example-io">hp = 2, damage = [10000,1], requirement = [1,1]</span>
26 * Output: <span class="example-io">1</span>
27 * Explanation:
28 * score(1) = 0, score(2) = 1. The total score is 0 + 1 = 1.
29 * score(1) = 0 because you do not get any points if you start from room 1.
30 * 
31 * 	You start with 2 health points.
32 * 	Enter room 1. Your health points are now 2 - 10000 = -9998. You do not get any points because -9998 < 1.
33 * 	Enter room 2. Your health points are now -9998 - 1 = -9999. You do not get any points because -9999 < 1.
34 * 
35 * score(2) = 1 because you get 1 point if you start from room 2.
36 * 
37 * 	You start with 2 health points.
38 * 	Enter room 2. Your health points are now 2 - 1 = 1. You get 1 point because 1 >= 1.
39 * </div>
40 *  
41 * Constraints:
42 * 
43 * 	1 <= hp <= 10^9
44 * 	1 <= n == damage.length == requirement.length <= 10^5
45 * 	1 <= damage[i], requirement[i] <= 10^4
46 * 
47 */
48pub struct Solution {}
49
50// problem: https://leetcode.com/problems/total-score-of-dungeon-runs/
51// discuss: https://leetcode.com/problems/total-score-of-dungeon-runs/discuss/?currentPage=1&orderBy=most_votes&query=
52
53// submission codes start here
54
55impl Solution {
56    pub fn total_score(hp: i32, damage: Vec<i32>, requirement: Vec<i32>) -> i64 {
57        
58    }
59}
60
61// submission codes end
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66
67    #[test]
68    fn test_3771() {
69    }
70}
71

Back
© 2026 bowen.ge All Rights Reserved.