Skip to Content

IPO

Explicación

Podemos resolver este problema con un enfoque voraz (greedy).

La idea es priorizar los proyectos que ofrecen el mayor beneficio. Al mismo tiempo, nos aseguramos de que los proyectos que elijamos se puedan empezar con el capital que tenemos en ese momento.

Implementación

Complejidad temporal: O(NlogN)\mathcal{O} (N \log N)

class Solution { public: int findMaximizedCapital(int k, int w, vector<int> &profits, vector<int> &capital) { const int n = (int)profits.size(); vector<pair<int, int>> projects(n); for (int i = 0; i < n; i++) { projects[i] = {capital[i], profits[i]}; } sort(begin(projects), end(projects)); priority_queue<int> pq; int j = 0; while (j < n && k > 0) { if (projects[j].first <= w) { pq.push(projects[j].second); j++; } else { if (pq.empty()) { return w; } w += pq.top(); pq.pop(); k--; } } while (k > 0 && !pq.empty()) { w += pq.top(); pq.pop(); k--; } return w; } };
class Solution { public int findMaximizedCapital(int k, int w, int[] profits, int[] capital) { int n = profits.length; List<Project> projects = new ArrayList<>(); for (int i = 0; i < n; i++) { projects.add(new Project(capital[i], profits[i])); } Collections.sort(projects, Comparator.comparingInt(p -> p.capital)); PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder()); int j = 0; while (j < n && k > 0) { if (projects.get(j).capital <= w) { pq.offer(projects.get(j).profit); j++; } else { if (pq.isEmpty()) { return w; } w += pq.poll(); k--; } } while (k > 0 && !pq.isEmpty()) { w += pq.poll(); k--; } return w; } private static class Project { int capital; int profit; Project(int capital, int profit) { this.capital = capital; this.profit = profit; } } }
import heapq class Solution: def findMaximizedCapital( self, k: int, w: int, profits: List[int], capital: List[int] ) -> int: n = len(profits) projects = sorted((capital[i], profits[i]) for i in range(n)) j = 0 pq = [] while j < n and k > 0: if projects[j][0] <= w: heapq.heappush(pq, -projects[j][1]) j += 1 else: if not pq: return w w += -heapq.heappop(pq) k -= 1 while k > 0 and pq: w += -heapq.heappop(pq) k -= 1 return w