Skip to content

Commit b6fee72

Browse files
authored
Merge pull request #4284 from NAVJOT-786/main
added leetcode solution for problem 2242
2 parents c3eaf48 + b602ecd commit b6fee72

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
---
2+
id: Maximum-Score-of-a-Node-Sequence
3+
title: Maximum-Score-of-a-Node-Sequence
4+
sidebar_label: 2242-Maximum-Score-of-a-Node-Sequence
5+
tags:
6+
- Arrays
7+
- graph
8+
- sorting
9+
- Enumeration
10+
description: "This document provides solutions to this problem implemented in Java, and Python."
11+
---
12+
13+
## Problem
14+
15+
There is an undirected graph with n nodes, numbered from 0 to n - 1.
16+
17+
You are given a 0-indexed integer array scores of length n where scores[i] denotes the score of node i. You are also given a 2D integer array edges where edges[i] = [ai, bi] denotes that there exists an undirected edge connecting nodes ai and bi.
18+
19+
A node sequence is valid if it meets the following conditions:
20+
21+
There is an edge connecting every pair of adjacent nodes in the sequence.
22+
No node appears more than once in the sequence.
23+
The score of a node sequence is defined as the sum of the scores of the nodes in the sequence.
24+
25+
Return the maximum score of a valid node sequence with a length of 4. If no such sequence exists, return -1.
26+
27+
### Examples
28+
29+
**Example 1:**
30+
31+
Input: scores = [5,2,9,8,4], edges = [[0,1],[1,2],[2,3],[0,2],[1,3],[2,4]]
32+
Output: 24
33+
Explanation: The figure above shows the graph and the chosen node sequence [0,1,2,3].
34+
The score of the node sequence is 5 + 2 + 9 + 8 = 24.
35+
It can be shown that no other node sequence has a score of more than 24.
36+
Note that the sequences [3,1,2,0] and [1,0,2,3] are also valid and have a score of 24.
37+
The sequence [0,3,2,4] is not valid since no edge connects nodes 0 and 3.
38+
### Constraints
39+
40+
- `banknotesCount.length == 5`
41+
- `0 <= banknotesCount[i] <= 109`
42+
- `1 <= amount <= 109`
43+
44+
### Solution
45+
46+
#### Code in Different Languages
47+
48+
### Java Solution
49+
50+
```java
51+
public int maximumScore(int[] A, int[][] edges) {
52+
int n = A.length;
53+
PriorityQueue<Integer>[] q = new PriorityQueue[n];
54+
for (int i = 0; i < n; i++)
55+
q[i] = new PriorityQueue<>((a, b) -> A[a] - A[b]);
56+
for (int[] e : edges) {
57+
q[e[0]].offer(e[1]);
58+
q[e[1]].offer(e[0]);
59+
if (q[e[0]].size() > 3) q[e[0]].poll();
60+
if (q[e[1]].size() > 3) q[e[1]].poll();
61+
}
62+
int res = -1;
63+
for (int[] edge : edges)
64+
for (int i : q[edge[0]])
65+
for (int j : q[edge[1]])
66+
if (i != j && i != edge[1] && j != edge[0])
67+
res = Math.max(res, A[i] + A[j] + A[edge[0]] + A[edge[1]]);
68+
return res;
69+
}
70+
71+
72+
```
73+
74+
### Python Solution
75+
76+
```python
77+
def maximumScore(self, A, edges):
78+
n = len(A)
79+
G = [[] for i in range(n)]
80+
for i,j in edges:
81+
G[i].append([A[j], j])
82+
G[j].append([A[i], i])
83+
for i in range(n):
84+
G[i] = nlargest(3, G[i])
85+
86+
res = -1
87+
for i,j in edges:
88+
for vii, ii in G[i]:
89+
for vjj, jj in G[j]:
90+
if ii != jj and ii != j and j != ii:
91+
res = max(res, vii + vjj + A[i] + A[j])
92+
return res
93+
94+
```
95+
96+
### Complexity Analysis
97+
98+
### Time Complexity: $O(1)$
99+
100+
> **Reason**:Both deposit and withdraw operations are $O(1)$, assuming a fixed number of denominations.
101+
102+
### Space Complexity: $O(1)$
103+
104+
> **Reason**: for the fixed-size array or deque used to store banknote counts.

0 commit comments

Comments
 (0)