Skip to content

Commit

Permalink
Added solution for 134. Gas Station: https://leetcode.com/problems/ga…
Browse files Browse the repository at this point in the history
  • Loading branch information
asesh committed Nov 16, 2024
1 parent deaee6b commit 2bcd75a
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 21 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "447"
endingLineNumber = "447"
startingLineNumber = "449"
endingLineNumber = "449"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -30,8 +30,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "413"
endingLineNumber = "413"
startingLineNumber = "415"
endingLineNumber = "415"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -46,8 +46,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "338"
endingLineNumber = "338"
startingLineNumber = "340"
endingLineNumber = "340"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -62,8 +62,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "306"
endingLineNumber = "306"
startingLineNumber = "308"
endingLineNumber = "308"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand Down Expand Up @@ -110,8 +110,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "268"
endingLineNumber = "268"
startingLineNumber = "270"
endingLineNumber = "270"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -126,8 +126,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "231"
endingLineNumber = "231"
startingLineNumber = "233"
endingLineNumber = "233"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -142,8 +142,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "190"
endingLineNumber = "190"
startingLineNumber = "192"
endingLineNumber = "192"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand Down Expand Up @@ -174,8 +174,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "179"
endingLineNumber = "179"
startingLineNumber = "181"
endingLineNumber = "181"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -190,8 +190,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "165"
endingLineNumber = "165"
startingLineNumber = "167"
endingLineNumber = "167"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand Down
4 changes: 3 additions & 1 deletion algorithm/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
#include "graph.hpp"

int main(int argc, const char* argv[]) {
invoke_h_index();
invoke_gas_station();

// invoke_h_index();

// invoke_add_binary();

Expand Down
61 changes: 59 additions & 2 deletions algorithm/number.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1731,12 +1731,18 @@ void invoke_spiral_matrix() {
Input: [3,0,6,1,5] Output: 3
Process:
sort: 6,5,3,1,0
h_index: 1
6 >= 1 true
h_index: 2
5 >= 2 true
h_index: 3
3 >= 3 true => Output
h_index: 1
1 >= 3 false
Input: [1,3,1] Output: 1
Process:
sort: 1,1,3
Input: [2,2,4,4,5] Output:
*/
int h_index(std::vector<int>& input) {
int output = 0;
Expand Down Expand Up @@ -1815,3 +1821,54 @@ void invoke_add_binary() {
std::string one = "11", two = "1";
std::cout<<"The sum of binary is: "<<add_binary(one, two);
}

/*
Input: gas: [1,2,3,4,5], cost: [3,4,5,1,2] => Output: 3
Process:
0 1 2 3 4 0 1 2 3 4
gas: [1,2,3,4,5], cost: [3,4,5,1,2]
Start at gas[3]: 0 + 4
gas[4]: 4 - 1 + 5 = 8
gas[0]: 8 - 2 + 1 = 7
gas[1]: 7 - 3 + 2 = 6
gas[2]: 6 - 4 + 3 = 5
gas[3]: 5
Input: gas: [2,3,4], cost: [3,4,3] => Output: -1
Process:
Start at gas[2]: 0 + 4
gas[0]: 4 - 3 + 2 = 3
gas[1]: 3 - 3 + 3 = 3
gas[2]: 3 < 4 => Output: -1
Input: gas: [5,1,2,3,4], cost: [4,4,1,5,1] => Output: 4
Process:
index
0 = 5 - 4 1, current_gas = 1
1 = 1 + 1 - 4, current_gas = -2, current_index = 1 + 1 = 2
2 = 2 - 1, current_gas = 1
3 = 1 + 3 - 5, current_gas = -1, current_index = 3 + 1 = 4
4 = 4 - 1, current_gas = 3
*/
int gas_station(std::vector<int>& gas, std::vector<int>& cost) {
int output = 0, current_gas = 0;
int total_gas = 0, total_cost = 0;

for(int index = 0; index < gas.size(); ++index) {
total_gas += gas[index];
total_cost += cost[index];

current_gas += gas[index] - cost[index]; // 1
if(current_gas < 0) {
output = index + 1;
current_gas = 0;
}
}

return total_gas < total_cost ? -1 : output;
}
void invoke_gas_station() {
std::vector<int> gas = {5,1,2,3,4};
std::vector<int> cost = {4,4,1,5,1};
std::cout<<"The starting gas station's index: "<<gas_station(gas, cost);
}
5 changes: 5 additions & 0 deletions algorithm/number.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,4 +329,9 @@ void invoke_single_number_ii();
*/
void invoke_add_binary();

/*
134. Gas Station: https://leetcode.com/problems/gas-station
*/
void invoke_gas_station();

#endif /* NUMBER_HPP */

0 comments on commit 2bcd75a

Please sign in to comment.