File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ } ;
Original file line number Diff line number Diff line change 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+ } ;
You can’t perform that action at this time.
0 commit comments