Skip to content

Commit 9559060

Browse files
authored
feat: add solutions to lc problem: No.2374 (doocs#3546)
No.2374.Node With Highest Edge Score
1 parent 18f564b commit 9559060

File tree

8 files changed

+161
-114
lines changed

8 files changed

+161
-114
lines changed

solution/2300-2399/2374.Node With Highest Edge Score/README.md

Lines changed: 55 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,15 @@ tags:
6868

6969
<!-- solution:start -->
7070

71-
### 方法一:遍历计数
71+
### 方法一:一次遍历
7272

73-
定义 $cnt$,其中每个元素 $cnt[i]$ 表示到节点 $i$ 的所有节点编号之和
73+
我们定义一个长度为 $n$ 的数组 $\textit{cnt}$,其中 $\textit{cnt}[i]$ 表示节点 $i$ 的边积分,初始时所有元素均为 $0$。定义一个答案变量 $\textit{ans}$,初始时为 $0$
7474

75-
最后找出 $cnt$ 中最大的元素 $cnt[i]$,返回 $i$。
75+
接下来,我们遍历数组 $\textit{edges}$,对于每个节点 $i$,以及它的出边节点 $j$,我们更新 $\textit{cnt}[j]$ 为 $\textit{cnt}[j] + i$。如果 $\textit{cnt}[\textit{ans}] < \textit{cnt}[j]$ 或者 $\textit{cnt}[\textit{ans}] = \textit{cnt}[j]$ 且 $j < \textit{ans}$,我们更新 $\textit{ans}$ 为 $j$。
7676

77-
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是节点的数量。
77+
最后,返回 $\textit{ans}$ 即可。
78+
79+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{edges}$ 的长度。
7880

7981
<!-- tabs:start -->
8082

@@ -83,13 +85,12 @@ tags:
8385
```python
8486
class Solution:
8587
def edgeScore(self, edges: List[int]) -> int:
86-
cnt = Counter()
87-
for i, v in enumerate(edges):
88-
cnt[v] += i
8988
ans = 0
90-
for i in range(len(edges)):
91-
if cnt[ans] < cnt[i]:
92-
ans = i
89+
cnt = [0] * len(edges)
90+
for i, j in enumerate(edges):
91+
cnt[j] += i
92+
if cnt[ans] < cnt[j] or (cnt[ans] == cnt[j] and j < ans):
93+
ans = j
9394
return ans
9495
```
9596

@@ -100,13 +101,12 @@ class Solution {
100101
public int edgeScore(int[] edges) {
101102
int n = edges.length;
102103
long[] cnt = new long[n];
103-
for (int i = 0; i < n; ++i) {
104-
cnt[edges[i]] += i;
105-
}
106104
int ans = 0;
107105
for (int i = 0; i < n; ++i) {
108-
if (cnt[ans] < cnt[i]) {
109-
ans = i;
106+
int j = edges[i];
107+
cnt[j] += i;
108+
if (cnt[ans] < cnt[j] || (cnt[ans] == cnt[j] && j < ans)) {
109+
ans = j;
110110
}
111111
}
112112
return ans;
@@ -122,13 +122,12 @@ public:
122122
int edgeScore(vector<int>& edges) {
123123
int n = edges.size();
124124
vector<long long> cnt(n);
125-
for (int i = 0; i < n; ++i) {
126-
cnt[edges[i]] += i;
127-
}
128125
int ans = 0;
129126
for (int i = 0; i < n; ++i) {
130-
if (cnt[ans] < cnt[i]) {
131-
ans = i;
127+
int j = edges[i];
128+
cnt[j] += i;
129+
if (cnt[ans] < cnt[j] || (cnt[ans] == cnt[j] && j < ans)) {
130+
ans = j;
132131
}
133132
}
134133
return ans;
@@ -139,19 +138,15 @@ public:
139138
#### Go
140139
141140
```go
142-
func edgeScore(edges []int) int {
143-
n := len(edges)
144-
cnt := make([]int, n)
145-
for i, v := range edges {
146-
cnt[v] += i
147-
}
148-
ans := 0
149-
for i, v := range cnt {
150-
if cnt[ans] < v {
151-
ans = i
141+
func edgeScore(edges []int) (ans int) {
142+
cnt := make([]int, len(edges))
143+
for i, j := range edges {
144+
cnt[j] += i
145+
if cnt[ans] < cnt[j] || (cnt[ans] == cnt[j] && j < ans) {
146+
ans = j
152147
}
153148
}
154-
return ans
149+
return
155150
}
156151
```
157152

@@ -160,17 +155,38 @@ func edgeScore(edges []int) int {
160155
```ts
161156
function edgeScore(edges: number[]): number {
162157
const n = edges.length;
163-
const sum = new Array(n).fill(0);
164-
for (let i = 0; i < n; i++) {
165-
sum[edges[i]] += i;
158+
const cnt: number[] = Array(n).fill(0);
159+
let ans: number = 0;
160+
for (let i = 0; i < n; ++i) {
161+
const j = edges[i];
162+
cnt[j] += i;
163+
if (cnt[ans] < cnt[j] || (cnt[ans] === cnt[j] && j < ans)) {
164+
ans = j;
165+
}
166166
}
167-
let res = 0;
168-
for (let i = 0; i < n; i++) {
169-
if (sum[res] < sum[i]) {
170-
res = i;
167+
return ans;
168+
}
169+
```
170+
171+
#### Rust
172+
173+
```rust
174+
impl Solution {
175+
pub fn edge_score(edges: Vec<i32>) -> i32 {
176+
let n = edges.len();
177+
let mut cnt = vec![0_i64; n];
178+
let mut ans = 0;
179+
180+
for (i, &j) in edges.iter().enumerate() {
181+
let j = j as usize;
182+
cnt[j] += i as i64;
183+
if cnt[ans] < cnt[j] || (cnt[ans] == cnt[j] && j < ans) {
184+
ans = j;
185+
}
171186
}
187+
188+
ans as i32
172189
}
173-
return res;
174190
}
175191
```
176192

solution/2300-2399/2374.Node With Highest Edge Score/README_EN.md

Lines changed: 58 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,15 @@ Nodes 0 and 2 both have an edge score of 3. Since node 0 has a smaller index, we
6868

6969
<!-- solution:start -->
7070

71-
### Solution 1
71+
### Solution 1: Single Traversal
72+
73+
We define an array $\textit{cnt}$ of length $n$, where $\textit{cnt}[i]$ represents the edge score of node $i$. Initially, all elements are $0$. We also define an answer variable $\textit{ans}$, initially set to $0$.
74+
75+
Next, we traverse the array $\textit{edges}$. For each node $i$ and its outgoing edge node $j$, we update $\textit{cnt}[j]$ to $\textit{cnt}[j] + i$. If $\textit{cnt}[\textit{ans}] < \textit{cnt}[j]$ or $\textit{cnt}[\textit{ans}] = \textit{cnt}[j]$ and $j < \textit{ans}$, we update $\textit{ans}$ to $j$.
76+
77+
Finally, return $\textit{ans}$.
78+
79+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{edges}$.
7280

7381
<!-- tabs:start -->
7482

@@ -77,13 +85,12 @@ Nodes 0 and 2 both have an edge score of 3. Since node 0 has a smaller index, we
7785
```python
7886
class Solution:
7987
def edgeScore(self, edges: List[int]) -> int:
80-
cnt = Counter()
81-
for i, v in enumerate(edges):
82-
cnt[v] += i
8388
ans = 0
84-
for i in range(len(edges)):
85-
if cnt[ans] < cnt[i]:
86-
ans = i
89+
cnt = [0] * len(edges)
90+
for i, j in enumerate(edges):
91+
cnt[j] += i
92+
if cnt[ans] < cnt[j] or (cnt[ans] == cnt[j] and j < ans):
93+
ans = j
8794
return ans
8895
```
8996

@@ -94,13 +101,12 @@ class Solution {
94101
public int edgeScore(int[] edges) {
95102
int n = edges.length;
96103
long[] cnt = new long[n];
97-
for (int i = 0; i < n; ++i) {
98-
cnt[edges[i]] += i;
99-
}
100104
int ans = 0;
101105
for (int i = 0; i < n; ++i) {
102-
if (cnt[ans] < cnt[i]) {
103-
ans = i;
106+
int j = edges[i];
107+
cnt[j] += i;
108+
if (cnt[ans] < cnt[j] || (cnt[ans] == cnt[j] && j < ans)) {
109+
ans = j;
104110
}
105111
}
106112
return ans;
@@ -116,13 +122,12 @@ public:
116122
int edgeScore(vector<int>& edges) {
117123
int n = edges.size();
118124
vector<long long> cnt(n);
119-
for (int i = 0; i < n; ++i) {
120-
cnt[edges[i]] += i;
121-
}
122125
int ans = 0;
123126
for (int i = 0; i < n; ++i) {
124-
if (cnt[ans] < cnt[i]) {
125-
ans = i;
127+
int j = edges[i];
128+
cnt[j] += i;
129+
if (cnt[ans] < cnt[j] || (cnt[ans] == cnt[j] && j < ans)) {
130+
ans = j;
126131
}
127132
}
128133
return ans;
@@ -133,19 +138,15 @@ public:
133138
#### Go
134139
135140
```go
136-
func edgeScore(edges []int) int {
137-
n := len(edges)
138-
cnt := make([]int, n)
139-
for i, v := range edges {
140-
cnt[v] += i
141-
}
142-
ans := 0
143-
for i, v := range cnt {
144-
if cnt[ans] < v {
145-
ans = i
141+
func edgeScore(edges []int) (ans int) {
142+
cnt := make([]int, len(edges))
143+
for i, j := range edges {
144+
cnt[j] += i
145+
if cnt[ans] < cnt[j] || (cnt[ans] == cnt[j] && j < ans) {
146+
ans = j
146147
}
147148
}
148-
return ans
149+
return
149150
}
150151
```
151152

@@ -154,17 +155,38 @@ func edgeScore(edges []int) int {
154155
```ts
155156
function edgeScore(edges: number[]): number {
156157
const n = edges.length;
157-
const sum = new Array(n).fill(0);
158-
for (let i = 0; i < n; i++) {
159-
sum[edges[i]] += i;
158+
const cnt: number[] = Array(n).fill(0);
159+
let ans: number = 0;
160+
for (let i = 0; i < n; ++i) {
161+
const j = edges[i];
162+
cnt[j] += i;
163+
if (cnt[ans] < cnt[j] || (cnt[ans] === cnt[j] && j < ans)) {
164+
ans = j;
165+
}
160166
}
161-
let res = 0;
162-
for (let i = 0; i < n; i++) {
163-
if (sum[res] < sum[i]) {
164-
res = i;
167+
return ans;
168+
}
169+
```
170+
171+
#### Rust
172+
173+
```rust
174+
impl Solution {
175+
pub fn edge_score(edges: Vec<i32>) -> i32 {
176+
let n = edges.len();
177+
let mut cnt = vec![0_i64; n];
178+
let mut ans = 0;
179+
180+
for (i, &j) in edges.iter().enumerate() {
181+
let j = j as usize;
182+
cnt[j] += i as i64;
183+
if cnt[ans] < cnt[j] || (cnt[ans] == cnt[j] && j < ans) {
184+
ans = j;
185+
}
165186
}
187+
188+
ans as i32
166189
}
167-
return res;
168190
}
169191
```
170192

solution/2300-2399/2374.Node With Highest Edge Score/Solution.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ class Solution {
33
int edgeScore(vector<int>& edges) {
44
int n = edges.size();
55
vector<long long> cnt(n);
6-
for (int i = 0; i < n; ++i) {
7-
cnt[edges[i]] += i;
8-
}
96
int ans = 0;
107
for (int i = 0; i < n; ++i) {
11-
if (cnt[ans] < cnt[i]) {
12-
ans = i;
8+
int j = edges[i];
9+
cnt[j] += i;
10+
if (cnt[ans] < cnt[j] || (cnt[ans] == cnt[j] && j < ans)) {
11+
ans = j;
1312
}
1413
}
1514
return ans;
1615
}
17-
};
16+
};
Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
func edgeScore(edges []int) int {
2-
n := len(edges)
3-
cnt := make([]int, n)
4-
for i, v := range edges {
5-
cnt[v] += i
6-
}
7-
ans := 0
8-
for i, v := range cnt {
9-
if cnt[ans] < v {
10-
ans = i
1+
func edgeScore(edges []int) (ans int) {
2+
cnt := make([]int, len(edges))
3+
for i, j := range edges {
4+
cnt[j] += i
5+
if cnt[ans] < cnt[j] || (cnt[ans] == cnt[j] && j < ans) {
6+
ans = j
117
}
128
}
13-
return ans
14-
}
9+
return
10+
}

solution/2300-2399/2374.Node With Highest Edge Score/Solution.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ class Solution {
22
public int edgeScore(int[] edges) {
33
int n = edges.length;
44
long[] cnt = new long[n];
5-
for (int i = 0; i < n; ++i) {
6-
cnt[edges[i]] += i;
7-
}
85
int ans = 0;
96
for (int i = 0; i < n; ++i) {
10-
if (cnt[ans] < cnt[i]) {
11-
ans = i;
7+
int j = edges[i];
8+
cnt[j] += i;
9+
if (cnt[ans] < cnt[j] || (cnt[ans] == cnt[j] && j < ans)) {
10+
ans = j;
1211
}
1312
}
1413
return ans;
1514
}
16-
}
15+
}
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
class Solution:
22
def edgeScore(self, edges: List[int]) -> int:
3-
cnt = Counter()
4-
for i, v in enumerate(edges):
5-
cnt[v] += i
63
ans = 0
7-
for i in range(len(edges)):
8-
if cnt[ans] < cnt[i]:
9-
ans = i
4+
cnt = [0] * len(edges)
5+
for i, j in enumerate(edges):
6+
cnt[j] += i
7+
if cnt[ans] < cnt[j] or (cnt[ans] == cnt[j] and j < ans):
8+
ans = j
109
return ans
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
impl Solution {
2+
pub fn edge_score(edges: Vec<i32>) -> i32 {
3+
let n = edges.len();
4+
let mut cnt = vec![0_i64; n];
5+
let mut ans = 0;
6+
7+
for (i, &j) in edges.iter().enumerate() {
8+
let j = j as usize;
9+
cnt[j] += i as i64;
10+
if cnt[ans] < cnt[j] || (cnt[ans] == cnt[j] && j < ans) {
11+
ans = j;
12+
}
13+
}
14+
15+
ans as i32
16+
}
17+
}

0 commit comments

Comments
 (0)