Skip to content

Commit 7f36727

Browse files
committed
[leet] prodct of array except self, gas station (mid)
1 parent d05fd74 commit 7f36727

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

허현빈/7주차/260211-1.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {number[]} gas
3+
* @param {number[]} cost
4+
* @return {number}
5+
*/
6+
var canCompleteCircuit = function (gas, cost) {
7+
const balance = gas.map((e, i) => e - cost[i]);
8+
let total = 0;
9+
let current = 0;
10+
let ans = 0;
11+
for (let i = 0; i < gas.length; i++) {
12+
current += balance[i];
13+
total += balance[i];
14+
if (current < 0) {
15+
current = 0;
16+
ans = i + 1;
17+
}
18+
}
19+
return total >= 0 ? ans : -1;
20+
};

허현빈/7주차/260211-2.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[]}
4+
*/
5+
var productExceptSelf = function (nums) {
6+
const left = [1];
7+
const right = [1];
8+
for (let i = 0; i < nums.length - 1; i++) {
9+
left.push(left[left.length - 1] * nums[i]);
10+
}
11+
for (let i = nums.length - 1; i >= 1; i--) {
12+
right.push(right[right.length - 1] * nums[i]);
13+
}
14+
right.reverse();
15+
const ans = [];
16+
for (let i = 0; i < nums.length; i++) {
17+
ans.push(left[i] * right[i]);
18+
}
19+
return ans;
20+
};

0 commit comments

Comments
 (0)