code in cpp
Erstellt am: 14. Januar 2026
Beantwortet mit GPT-5.2 Thinking von Chat01
Erstellt am: 14. Januar 2026
Beantwortet mit GPT-5.2 Thinking von Chat01
code in cpp
cpp#include <bits/stdc++.h> using namespace std; // red[i] = time from city i -> i+1 on Red line // blue[i] = time from city i -> i+1 on Blue line // blueCost is paid whenever you START on Blue or SWITCH from Red -> Blue vector<long> minimumCost(vector<int> red, vector<int> blue, int blueCost) { int n = (int)red.size(); // number of edges vector<long> ans(n + 1); long long r = 0; // min cost to be at current city on Red long long b = (long long)blueCost; // min cost to be at current city on Blue (start on Blue pays blueCost) ans[0] = 0; for (int i = 0; i < n; i++) { // Move to next city: long long nr = min(r, b) + (long long)red[i]; // switching to Red is free long long nb = min( b + (long long)blue[i], // stay on Blue r + (long long)blueCost + (long long)blue[i] // switch Red -> Blue (pay blueCost) ); r = nr; b = nb; ans[i + 1] = (long)min(r, b); } return ans; }
code in cpp
cpp#include <bits/stdc++.h> using namespace std; /* Pick a maximum number of transactions in order such that the running balance (starting at 0) never becomes negative. Greedy: take every transaction; if balance drops below 0, remove the most negative chosen transaction (min element) to restore feasibility. */ int maximizeTransactions(vector<int> transaction) { long long bal = 0; priority_queue<long long, vector<long long>, greater<long long>> minHeap; // chosen txns for (long long x : transaction) { bal += x; minHeap.push(x); if (bal < 0) { bal -= minHeap.top(); // remove the smallest (most negative) txn minHeap.pop(); } } return (int)minHeap.size(); }