Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fe3ef09

Browse files
feat(LC): add completed 724, notes, complexity, cleaned & optimized
1 parent b483cd4 commit fe3ef09

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/**
2+
* @see {@link https://leetcode.com/problems/find-pivot-index/description/?envType=study-plan&id=level-1 724. Find Pivot Index}
3+
*/
4+
5+
/**
6+
* EXPLAIN
7+
* Pivot index is EXCLUSIVE, not to be counted in comparative sums
8+
* [1,7,3,6,5,6] has pivot index 3 since 1+7+3 === 5+6
9+
* The 6 at pivot index 3 is NOT counted
10+
*
11+
* First index and last index sums are always 0!
12+
* [2,1,-1] has PI 0; 2@0 not counted, so it's 1 + -1
13+
* [1,-1,2] also has PI 0, but on right edge, same reason
14+
* Return -1 if no pivot index found
15+
*
16+
* Flow
17+
* [1,3,7,2,2]
18+
* l = 0 r = 15 - arr[0] = 14
19+
* l = 0 + arr[0] = 1 r = 14 - arr[1] = 11
20+
* l = 1 + arr[1] = 4 r = 11 - arr[2] = 4
21+
*/
22+
23+
/**
24+
* APPROACH
25+
* Create leftSum, rightSum vals; default left to 0
26+
*
27+
* START LOOP at second val
28+
* running sum to get first right sum
29+
* END LOOP
30+
*
31+
* if l === r, return 0 (left edge)
32+
*
33+
* START 2ND LOOP at second val
34+
* i - 1 added to leftVal
35+
* subtract curr val from right sum
36+
* check left===right; if so, return i
37+
* END 2ND LOOP
38+
*
39+
* no PI found, return -1
40+
*/
41+
42+
/**
43+
* @param {number[]} nums
44+
* @return {number}
45+
*/
46+
var pivotIndex = function (nums) {
47+
let left = 0;
48+
let right = 0;
49+
50+
// 1st Loop to establish right sum
51+
for (let i = 1; i < nums.length; i++) {
52+
const currVal = nums[i];
53+
right += currVal;
54+
}
55+
56+
// Left-edge check
57+
if (left === right) {
58+
return 0;
59+
}
60+
61+
// Begin looping through array to add/subtract from left/right totals
62+
for (let j = 1; j < nums.length; j++) {
63+
// Add prevVal to left
64+
left += nums[j - 1];
65+
66+
// Subtract currVal from right
67+
right -= nums[j];
68+
69+
// Equality check
70+
if (left === right) {
71+
return j;
72+
}
73+
}
74+
75+
// Loop ended, no PI found
76+
return -1;
77+
};
78+
79+
/**
80+
* COMPLEXITY
81+
* TIME O(N) - b/c of one loop
82+
* SPACE O(1) - left and right vars are constant integers
83+
*/
84+
85+
/**
86+
* OPTIMIZE
87+
*
88+
* calc sum of right first
89+
* LOOP
90+
* grab curr num
91+
* subtract curr from right
92+
* test (left === right) ? return index
93+
* add curr to left
94+
* END LOOP
95+
*
96+
* no match found; return -1
97+
*/
98+
99+
/**
100+
* READ 01:25
101+
* EXPLAIN 02:00 03:25
102+
* APPROACH 22:35 26:01
103+
* CODE 11:36 37:38
104+
* TEST 1st Attempt
105+
* OPTIMIZE
106+
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /