Gas Station

Deadline
Oct 9, 2021
Status
Archived
Type
greedy
 

πŸ“„λ¬Έμ œ

notion image
notion image

βœοΈν’€μ΄

 
재영
첫 번째 풀이
κ½€λ‚˜ λ§Žμ€ 연산을 ν•„μš”λ‘œ ν•΄μ„œ λΆˆλ§Œμ΄μ—ˆμ–΄μ˜€.
const canCompleteCircuit = (gas, cost) => { const total = gas.map((g, i) => g - cost[i]); const totalLength = total.length; let i = 0; while (i < totalLength) { let cnt = total[i]; let flag = true; for (let j = 0; j < totalLength; j += 1) { const idx = ((i + j) % totalLength); const nextIdx = ((idx + 1) % totalLength); if (cnt < 0 || cnt + total[nextIdx] < 0) { flag = false; break; }; cnt += total[nextIdx]; } if (flag && cnt >= 0) return i; i += 1; } return -1 }
두 번째 방법
ν•΄λ΄€λŠ”λ°, ꡳ이 μ΄ν›„μ˜ 값을 λΉ„κ΅ν•˜λ‚˜ μ‹Άμ—ˆμ–΄μ˜€.
ν˜„μž¬ + μ΄ν›„μ˜ κ°’ = κ³Όκ±° + ν˜„μž¬μ˜ κ°’κ³Ό κ°™κΈ° λ•Œλ¬Έμ΄μ—ˆμ–΄μš”. (점화식)
λ”°λΌμ„œ, 이λ₯Ό λ‹€ 쳐내고, ν˜„μž¬μ˜ κ°’ < μ§€κΈˆκΉŒμ§€μ˜ 합을 λΉ„κ΅ν•˜κ³ , μ•„λ‹ˆλ©΄ κ·Έλƒ₯ λ‹€μŒμœΌλ‘œ λ„˜μ–΄κ°€μ„œ ν™•μΈν•˜λŠ” λ°©μ‹μœΌλ‘œ ν–ˆμ–΄μš”.
μ΄λ•Œ, 맨 처음 체크할 λ•Œ 총 합이 -1κ°€ μ•„λ‹Œ 이상, 이 λ¬Έμ œλŠ” 닡이 있게 λ˜μ–΄ μžˆμλ‹ˆλ‹€.
(μ™œλƒ! 닡은 고유의 κ°’λ§Œ ν—ˆμš©ν•˜λ©°, 총 합이 0만 μ•ˆλ„˜μ–΄κ°€λ©΄ μ–΄μ°Œμ–΄μ°Œ λ‹€ 돌게 λ˜λ”λΌκ΅¬μš”.)
λ”°λΌμ„œ μ΄λŒ€λ‘œ ν’€μ–΄ λ³΄λ‹ˆ, O(N)κΉŒμ§€λŠ” λ‚˜μ˜€λŠ” ꡰ유.
const canCompleteCircuit = (gas, cost) => { const total = gas.map((g, i) => g - cost[i]); if (total.reduce((acc, cur) => acc + cur, 0) < 0) return -1; let lastIdx = 0; let cnt = 0; total.forEach((val, idx) => { if (cnt + val < 0) { cnt = 0; lastIdx = idx + 1; return; } cnt += val; }) return lastIdx; }
νš¨μ„±
var canCompleteCircuit = function (gas, cost) { let start = 0, tank = 0, total = 0; for(let i=0; i<gas.length; i++) { const consume = gas[i] - cost[i]; tank += consume; if(tank < 0) { tank = 0; start = i + 1; } total += consume; } return total < 0 ? -1 : start; };
은찬
const checkCircuit = (goal, remainingGas, length) => { let goingIndex = goal + 1; let currentGas = remainingGas[goal]; if(goingIndex === length){ goingIndex = 0; } while(goingIndex !== goal){ currentGas += remainingGas[goingIndex++]; if(currentGas < 0){ return false; } if(goingIndex === length){ goingIndex = 0; } } return true; } const canCompleteCircuit = (gas, cost) => { const length = gas.length; const remainingGas = Array(length + 1).fill(0); const startIndex = []; let minusGas = 0; let plusGas = 0; for(let i = 0; i < length; i++){ remainingGas[i] = gas[i] - cost[i]; if(remainingGas[i] >= 0){ startIndex.push(i); plusGas += remainingGas[i]; } else{ minusGas += remainingGas[i]; } } if(plusGas + minusGas < 0){ return -1; } for(let i = 0; i < startIndex.length; i++){ if(checkCircuit(startIndex[i], remainingGas, length)){ return startIndex[i]; } } return -1; };
ν˜„μ„
var canCompleteCircuit = function(gas, cost) { const gasCycle = [...gas, ...gas] const costCycle = [...cost, ...cost] let startIdx = -1; for (let i = 0; i < gas.length; i++) { if (gas[i] < cost[i]) continue; let acc = 0; for (let j = i; j < i + gas.length; j++) { acc += gasCycle[j] acc -= costCycle[j] if (acc < 0) break; } if (acc >= 0 ) { startIdx = i break; } } return startIdx };
κ°€μ˜
var canCompleteCircuit = function(gas, cost) { const arr = gas.map((elt, idx) => [elt, idx]) .filter((elt, idx) => elt[0] >= cost[idx]); for (const a of arr) { const startIdx = a[1]; let total = a[0] - cost[startIdx]; let nextIdx = startIdx === cost.length - 1 ? 0 : startIdx + 1; while (total >= 0) { if (nextIdx === startIdx) return startIdx; total += (gas[nextIdx] - cost[nextIdx]); nextIdx = nextIdx + 1 < cost.length ? nextIdx + 1 : 0; } } return -1; };