3427. Sum of Variable Length Subarrays Easy

@problem@discussion
#Array#Prefix Sum



1/**
2 * [3427] Sum of Variable Length Subarrays
3 *
4 * You are given an integer array nums of size n. For each index i where 0 <= i < n, define a <span data-keyword="subarray-nonempty">subarray</span> nums[start ... i] where start = max(0, i - nums[i]).
5 * Return the total sum of all elements from the subarray defined for each index in the array.
6 *  
7 * <strong class="example">Example 1:
8 * <div class="example-block">
9 * Input: <span class="example-io">nums = [2,3,1]</span>
10 * Output: <span class="example-io">11</span>
11 * Explanation:
12 * <table style="border: 1px solid black;">
13 * 	<tbody>
14 * 		<tr>
15 * 			<th style="border: 1px solid black;">i</th>
16 * 			<th style="border: 1px solid black;">Subarray</th>
17 * 			<th style="border: 1px solid black;">Sum</th>
18 * 		</tr>
19 * 		<tr>
20 * 			<td style="border: 1px solid black;">0</td>
21 * 			<td style="border: 1px solid black;">nums[0] = [2]</td>
22 * 			<td style="border: 1px solid black;">2</td>
23 * 		</tr>
24 * 		<tr>
25 * 			<td style="border: 1px solid black;">1</td>
26 * 			<td style="border: 1px solid black;">nums[0 ... 1] = [2, 3]</td>
27 * 			<td style="border: 1px solid black;">5</td>
28 * 		</tr>
29 * 		<tr>
30 * 			<td style="border: 1px solid black;">2</td>
31 * 			<td style="border: 1px solid black;">nums[1 ... 2] = [3, 1]</td>
32 * 			<td style="border: 1px solid black;">4</td>
33 * 		</tr>
34 * 		<tr>
35 * 			<td style="border: 1px solid black;">Total Sum</td>
36 * 			<td style="border: 1px solid black;"> </td>
37 * 			<td style="border: 1px solid black;">11</td>
38 * 		</tr>
39 * 	</tbody>
40 * </table>
41 * The total sum is 11. Hence, 11 is the output.
42 * </div>
43 * <strong class="example">Example 2:
44 * <div class="example-block">
45 * Input: <span class="example-io">nums = [3,1,1,2]</span>
46 * Output: <span class="example-io">13</span>
47 * Explanation:
48 * <table style="border: 1px solid black;">
49 * 	<tbody>
50 * 		<tr>
51 * 			<th style="border: 1px solid black;">i</th>
52 * 			<th style="border: 1px solid black;">Subarray</th>
53 * 			<th style="border: 1px solid black;">Sum</th>
54 * 		</tr>
55 * 		<tr>
56 * 			<td style="border: 1px solid black;">0</td>
57 * 			<td style="border: 1px solid black;">nums[0] = [3]</td>
58 * 			<td style="border: 1px solid black;">3</td>
59 * 		</tr>
60 * 		<tr>
61 * 			<td style="border: 1px solid black;">1</td>
62 * 			<td style="border: 1px solid black;">nums[0 ... 1] = [3, 1]</td>
63 * 			<td style="border: 1px solid black;">4</td>
64 * 		</tr>
65 * 		<tr>
66 * 			<td style="border: 1px solid black;">2</td>
67 * 			<td style="border: 1px solid black;">nums[1 ... 2] = [1, 1]</td>
68 * 			<td style="border: 1px solid black;">2</td>
69 * 		</tr>
70 * 		<tr>
71 * 			<td style="border: 1px solid black;">3</td>
72 * 			<td style="border: 1px solid black;">nums[1 ... 3] = [1, 1, 2]</td>
73 * 			<td style="border: 1px solid black;">4</td>
74 * 		</tr>
75 * 		<tr>
76 * 			<td style="border: 1px solid black;">Total Sum</td>
77 * 			<td style="border: 1px solid black;"> </td>
78 * 			<td style="border: 1px solid black;">13</td>
79 * 		</tr>
80 * 	</tbody>
81 * </table>
82 * The total sum is 13. Hence, 13 is the output.
83 * </div>
84 *  
85 * Constraints:
86 * 
87 * 	1 <= n == nums.length <= 100
88 * 	1 <= nums[i] <= 1000
89 * 
90 */
91pub struct Solution {}
92
93// problem: https://leetcode.com/problems/sum-of-variable-length-subarrays/
94// discuss: https://leetcode.com/problems/sum-of-variable-length-subarrays/discuss/?currentPage=1&orderBy=most_votes&query=
95
96// submission codes start here
97
98impl Solution {
99    pub fn subarray_sum(nums: Vec<i32>) -> i32 {
100        0
101    }
102}
103
104// submission codes end
105
106#[cfg(test)]
107mod tests {
108    use super::*;
109
110    #[test]
111    fn test_3427() {
112    }
113}
114


Back
© 2025 bowen.ge All Rights Reserved.