Skip to content

Commit

Permalink
Create 08. Maximum Star Sum of a Graph.py
Browse files Browse the repository at this point in the history
  • Loading branch information
SamirPaulb committed Dec 13, 2022
1 parent 23514bb commit 7ea17f3
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions 08_Heap/08. Maximum Star Sum of a Graph.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# https://leetcode.com/problems/maximum-star-sum-of-a-graph/

import heapq
class Solution:
def maxStarSum(self, vals: List[int], edges: List[List[int]], k: int) -> int:
adjList = {i:[] for i in range(len(vals))}

for a, b in edges:
if vals[b] > 0:
heapq.heappush(adjList[a], vals[b])
if len(adjList[a]) > k:
heapq.heappop(adjList[a])
if vals[a] > 0:
heapq.heappush(adjList[b], vals[a])
if len(adjList[b]) > k:
heapq.heappop(adjList[b])

res = -2**31
for i in range(len(vals)):
tmp = vals[i]
tmp += sum(adjList[i])
res = max(res, tmp)

return res


# Time: O(N + E * log(K))
# Space: O(N*K)

0 comments on commit 7ea17f3

Please sign in to comment.