|
1 |
| -# 1732. Find the Highest Altitude |
| 1 | +# 724. Find Pivot Index |
2 | 2 |
|
3 | 3 | ## Description
|
4 |
| -See https://leetcode.com/problems/find-the-highest-altitude/description/ |
| 4 | +See https://leetcode.com/problems/find-pivot-index/description/ |
5 | 5 |
|
6 | 6 | ## Problem
|
7 |
| -There is a biker going on a road trip. The road trip consists of `n + 1` points at different altitudes. The biker starts his trip on point `0` with altitude equal `0`. |
| 7 | +Given an array of integers `nums`, calculate the pivot index of this array. |
8 | 8 |
|
9 |
| -You are given an integer array `gain` of length `n` where `gain[i]` is the net gain in altitude between points `i` and `i + 1` for all `(0 <= i < n)`. Return the highest altitude of a point. |
| 9 | +The pivot index is the index where the sum of all the numbers strictly to the left of the index is equal to the sum of all the numbers strictly to the index's right. |
| 10 | + |
| 11 | +If the index is on the left edge of the array, then the left sum is `0` because there are no elements to the left. This also applies to the right edge of the array. |
| 12 | + |
| 13 | +Return the leftmost pivot index. If no such index exists, return `-1`. |
10 | 14 |
|
11 | 15 | ## Example 1
|
12 | 16 |
|
13 | 17 | ```
|
14 |
| -Input: gain = [-5,1,5,0,-7] |
15 |
| -Output: 1 |
16 |
| -Explanation: The altitudes are [0,-5,-4,1,1,-6]. The highest is 1. |
| 18 | +Input: nums = [1,7,3,6,5,6] |
| 19 | +Output: 3 |
| 20 | +Explanation: |
| 21 | +The pivot index is 3. |
| 22 | +Left sum = nums[0] + nums[1] + nums[2] = 1 + 7 + 3 = 11 |
| 23 | +Right sum = nums[4] + nums[5] = 5 + 6 = 11 |
17 | 24 | ```
|
18 | 25 |
|
19 | 26 | ## Example 2
|
20 | 27 |
|
21 | 28 | ```
|
22 |
| -Input: gain = [-4,-3,-2,-1,4,3,2] |
| 29 | +Input: nums = [1,2,3] |
| 30 | +Output: -1 |
| 31 | +Explanation: |
| 32 | +There is no index that satisfies the conditions in the problem statement. |
| 33 | +``` |
| 34 | + |
| 35 | +## Example 3 |
| 36 | + |
| 37 | +``` |
| 38 | +Input: nums = [2,1,-1] |
23 | 39 | Output: 0
|
24 |
| -Explanation: The altitudes are [0,-4,-7,-9,-10,-6,-3,-1]. The highest is 0. |
| 40 | +Explanation: |
| 41 | +The pivot index is 0. |
| 42 | +Left sum = 0 (no elements to the left of index 0) |
| 43 | +Right sum = nums[1] + nums[2] = 1 + -1 = 0 |
25 | 44 | ```
|
26 | 45 |
|
27 | 46 | ## Constraints
|
28 | 47 |
|
29 | 48 | ```
|
30 |
| -n == gain.length |
31 |
| -1 <= n <= 100 |
32 |
| --100 <= gain[i] <= 100 |
| 49 | +1 <= nums.length <= 104 |
| 50 | +-1000 <= nums[i] <= 1000 |
33 | 51 | ```
|
0 commit comments