File tree 4 files changed +55
-3
lines changed
4 files changed +55
-3
lines changed Original file line number Diff line number Diff line change 11
11
// "skipFiles": [
12
12
// "<node_internals>/**"
13
13
// ],
14
- // "program": "${workspaceFolder }\\LeetCode-75\\17-1493 .js"
14
+ // "program": "${workspaceFolder }\\LeetCode-75\\19-724 .js"
15
15
// }
16
16
]
17
17
}
Original file line number Diff line number Diff line change
1
+ // https://leetcode.com/problems/find-the-highest-altitude/description/?envType=study-plan-v2&envId=leetcode-75
2
+
3
+ const largestAltitude = ( gain : number [ ] ) : number => {
4
+ let highest = 0 ,
5
+ sum = 0 ;
6
+
7
+ // O(N) SOLUTION
8
+ for ( let val of gain ) {
9
+ sum += val ;
10
+ if ( sum > highest ) highest = sum ;
11
+ }
12
+
13
+ return highest ;
14
+ } ;
Original file line number Diff line number Diff line change
1
+ // https://leetcode.com/problems/find-pivot-index/description/?envType=study-plan-v2&envId=leetcode-75
2
+
3
+ const pivotIndex = ( nums : number [ ] ) : number => {
4
+ // O(N) SOLUTION, BUT UNACCEPTED; IT DOESN'T WORK PROPERLY WITH POSITIVE/NEGATIVE VALUES
5
+ // let i = 0,
6
+ // j = nums.length - 1,
7
+ // leftSum = 0,
8
+ // rightSum = 0;
9
+
10
+ // while (i < j) {
11
+ // const nextLeftSum = leftSum + nums[i];
12
+ // const nextRightSum = rightSum + nums[j];
13
+
14
+ // if (nextLeftSum > nextRightSum) {
15
+ // // if (Math.abs(nextLeftSum) > Math.abs(nextRightSum)) {
16
+ // rightSum = nextRightSum;
17
+ // j--;
18
+ // } else {
19
+ // leftSum = nextLeftSum;
20
+ // i++;
21
+ // }
22
+ // }
23
+
24
+ // return rightSum === leftSum ? i : -1;
25
+
26
+ // O(N) SOLUTION
27
+ let i = 1 ,
28
+ leftSum = 0 ,
29
+ rightSum = nums . reduce ( ( acc , val ) => acc + val , 0 ) - nums [ 0 ] ;
30
+
31
+ while ( i < nums . length && leftSum !== rightSum ) {
32
+ leftSum += nums [ i - 1 ] ;
33
+ rightSum -= nums [ i ] ;
34
+ i ++ ;
35
+ }
36
+
37
+ return leftSum === rightSum ? i - 1 : - 1 ;
38
+ } ;
Original file line number Diff line number Diff line change 30
30
31
31
### Prefix Sum
32
32
33
- - [ ] 18 . Find the Highest Altitude
34
- - [ ] 19 . Find Pivot Index
33
+ - [X ] 18 . Find the Highest Altitude
34
+ - [X ] 19 . Find Pivot Index
35
35
36
36
### Hash Map / Set
37
37
You can’t perform that action at this time.
0 commit comments