Files
leetcode/axis/task2.cpp
2023-10-06 20:42:19 +02:00

61 lines
1.9 KiB
C++

#include <cassert>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <tuple>
// D: Vector of numbers that show distance.
// C: Vector of numbers that show quantities to be delivered at D[K], where K is the location.
// P: The totalt numbers of product available.
// Delivery start from lowest distance.
// Only delivers when P is the same or greater as C[K].
// If we should deliver everything then the order left need to be 0
// else we don't deliver even if we have more product available.
int solution(std::vector<int> &D, std::vector<int> &C, int P) {
assert(D.size() == C.size() && "Vector D and C needs to be the same size");
std::vector<std::tuple<int, int>> zip{};
for (std::size_t i = 0; i < D.size(); ++i)
zip.emplace_back(D[i], C[i]);
std::sort(std::begin(zip), std::end(zip), [](auto const& a, auto const& b) {
return std::get<0>(a) < std::get<0>(b);
});
int delivered = 0;
for (std::size_t i = 0; i < zip.size(); ++i) {
auto const CK = std::get<1>(zip[i]);
auto const total = delivered + CK;
std::printf("CK: %d, total: %d\n", CK, total);
if (total >= P) break;
if (P - total > 0 && i == zip.size() - 1) break;
delivered = total;
}
if (delivered == 0) return 0;
return P - delivered;
}
int main() {
// std::vector<int> D = {5, 11, 1, 3};
// std::vector<int> C = {6, 1, 3, 2};
// int P = 7;
// int res = solution(D, C, P);
// std::printf("%d\n", res);
// {
// std::vector<int> D = {10, 15, 1};
// std::vector<int> C = {10, 1, 2};
// int P = 3;
// int res = solution(D, C, P);
// std::printf("test 2: %d\n", res);
// }
{
std::vector<int> D = {1, 4, 2, 5};
std::vector<int> C = {4, 9, 2, 3};
int P = 19;
int res = solution(D, C, P);
std::printf("test 3: %d\n", res);
}
return 0;
}