From b117165b888f27b739c0b4cfbce01a4a11eca864 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Tue, 11 Feb 2025 13:22:16 +0000 Subject: [PATCH] =?UTF-8?q?=EC=9D=B4=EC=8A=88=20#451=EC=97=90=EC=84=9C=20?= =?UTF-8?q?=EC=86=94=EB=A3=A8=EC=85=98=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LeetCode/Gas_Station.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 LeetCode/Gas_Station.cpp diff --git a/LeetCode/Gas_Station.cpp b/LeetCode/Gas_Station.cpp new file mode 100644 index 0000000..c925a1d --- /dev/null +++ b/LeetCode/Gas_Station.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + int canCompleteCircuit(vector& gas, vector& cost) { + int totalTank = 0; + int currentTank = 0; + int startStation = 0; + + for (int i = 0; i < gas.size(); i++) { + int netGas = gas[i] - cost[i]; + totalTank += netGas; + currentTank += netGas; + + if (currentTank < 0) { + startStation = i + 1; + currentTank = 0; + } + } + + return (totalTank >= 0) ? startStation : -1; + } +}; \ No newline at end of file