Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.2374 (#3546)
Browse files Browse the repository at this point in the history
No.2374.Node With Highest Edge Score
  • Loading branch information
yanglbme committed Sep 21, 2024
1 parent 18f564b commit 9559060
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 114 deletions.
94 changes: 55 additions & 39 deletions solution/2300-2399/2374.Node With Highest Edge Score/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,15 @@ tags:

<!-- solution:start -->

### 方法一:遍历计数
### 方法一:一次遍历

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

最后找出 $cnt$ 中最大的元素 $cnt[i]$,返回 $i$。
接下来,我们遍历数组 $\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$。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是节点的数量。
最后,返回 $\textit{ans}$ 即可。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{edges}$ 的长度。

<!-- tabs:start -->

Expand All @@ -83,13 +85,12 @@ tags:
```python
class Solution:
def edgeScore(self, edges: List[int]) -> int:
cnt = Counter()
for i, v in enumerate(edges):
cnt[v] += i
ans = 0
for i in range(len(edges)):
if cnt[ans] < cnt[i]:
ans = i
cnt = [0] * len(edges)
for i, j in enumerate(edges):
cnt[j] += i
if cnt[ans] < cnt[j] or (cnt[ans] == cnt[j] and j < ans):
ans = j
return ans
```

Expand All @@ -100,13 +101,12 @@ class Solution {
public int edgeScore(int[] edges) {
int n = edges.length;
long[] cnt = new long[n];
for (int i = 0; i < n; ++i) {
cnt[edges[i]] += i;
}
int ans = 0;
for (int i = 0; i < n; ++i) {
if (cnt[ans] < cnt[i]) {
ans = i;
int j = edges[i];
cnt[j] += i;
if (cnt[ans] < cnt[j] || (cnt[ans] == cnt[j] && j < ans)) {
ans = j;
}
}
return ans;
Expand All @@ -122,13 +122,12 @@ public:
int edgeScore(vector<int>& edges) {
int n = edges.size();
vector<long long> cnt(n);
for (int i = 0; i < n; ++i) {
cnt[edges[i]] += i;
}
int ans = 0;
for (int i = 0; i < n; ++i) {
if (cnt[ans] < cnt[i]) {
ans = i;
int j = edges[i];
cnt[j] += i;
if (cnt[ans] < cnt[j] || (cnt[ans] == cnt[j] && j < ans)) {
ans = j;
}
}
return ans;
Expand All @@ -139,19 +138,15 @@ public:
#### Go
```go
func edgeScore(edges []int) int {
n := len(edges)
cnt := make([]int, n)
for i, v := range edges {
cnt[v] += i
}
ans := 0
for i, v := range cnt {
if cnt[ans] < v {
ans = i
func edgeScore(edges []int) (ans int) {
cnt := make([]int, len(edges))
for i, j := range edges {
cnt[j] += i
if cnt[ans] < cnt[j] || (cnt[ans] == cnt[j] && j < ans) {
ans = j
}
}
return ans
return
}
```

Expand All @@ -160,17 +155,38 @@ func edgeScore(edges []int) int {
```ts
function edgeScore(edges: number[]): number {
const n = edges.length;
const sum = new Array(n).fill(0);
for (let i = 0; i < n; i++) {
sum[edges[i]] += i;
const cnt: number[] = Array(n).fill(0);
let ans: number = 0;
for (let i = 0; i < n; ++i) {
const j = edges[i];
cnt[j] += i;
if (cnt[ans] < cnt[j] || (cnt[ans] === cnt[j] && j < ans)) {
ans = j;
}
}
let res = 0;
for (let i = 0; i < n; i++) {
if (sum[res] < sum[i]) {
res = i;
return ans;
}
```

#### Rust

```rust
impl Solution {
pub fn edge_score(edges: Vec<i32>) -> i32 {
let n = edges.len();
let mut cnt = vec![0_i64; n];
let mut ans = 0;

for (i, &j) in edges.iter().enumerate() {
let j = j as usize;
cnt[j] += i as i64;
if cnt[ans] < cnt[j] || (cnt[ans] == cnt[j] && j < ans) {
ans = j;
}
}

ans as i32
}
return res;
}
```

Expand Down
94 changes: 58 additions & 36 deletions solution/2300-2399/2374.Node With Highest Edge Score/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,15 @@ Nodes 0 and 2 both have an edge score of 3. Since node 0 has a smaller index, we

<!-- solution:start -->

### Solution 1
### Solution 1: Single Traversal

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$.

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$.

Finally, return $\textit{ans}$.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{edges}$.

<!-- tabs:start -->

Expand All @@ -77,13 +85,12 @@ Nodes 0 and 2 both have an edge score of 3. Since node 0 has a smaller index, we
```python
class Solution:
def edgeScore(self, edges: List[int]) -> int:
cnt = Counter()
for i, v in enumerate(edges):
cnt[v] += i
ans = 0
for i in range(len(edges)):
if cnt[ans] < cnt[i]:
ans = i
cnt = [0] * len(edges)
for i, j in enumerate(edges):
cnt[j] += i
if cnt[ans] < cnt[j] or (cnt[ans] == cnt[j] and j < ans):
ans = j
return ans
```

Expand All @@ -94,13 +101,12 @@ class Solution {
public int edgeScore(int[] edges) {
int n = edges.length;
long[] cnt = new long[n];
for (int i = 0; i < n; ++i) {
cnt[edges[i]] += i;
}
int ans = 0;
for (int i = 0; i < n; ++i) {
if (cnt[ans] < cnt[i]) {
ans = i;
int j = edges[i];
cnt[j] += i;
if (cnt[ans] < cnt[j] || (cnt[ans] == cnt[j] && j < ans)) {
ans = j;
}
}
return ans;
Expand All @@ -116,13 +122,12 @@ public:
int edgeScore(vector<int>& edges) {
int n = edges.size();
vector<long long> cnt(n);
for (int i = 0; i < n; ++i) {
cnt[edges[i]] += i;
}
int ans = 0;
for (int i = 0; i < n; ++i) {
if (cnt[ans] < cnt[i]) {
ans = i;
int j = edges[i];
cnt[j] += i;
if (cnt[ans] < cnt[j] || (cnt[ans] == cnt[j] && j < ans)) {
ans = j;
}
}
return ans;
Expand All @@ -133,19 +138,15 @@ public:
#### Go
```go
func edgeScore(edges []int) int {
n := len(edges)
cnt := make([]int, n)
for i, v := range edges {
cnt[v] += i
}
ans := 0
for i, v := range cnt {
if cnt[ans] < v {
ans = i
func edgeScore(edges []int) (ans int) {
cnt := make([]int, len(edges))
for i, j := range edges {
cnt[j] += i
if cnt[ans] < cnt[j] || (cnt[ans] == cnt[j] && j < ans) {
ans = j
}
}
return ans
return
}
```

Expand All @@ -154,17 +155,38 @@ func edgeScore(edges []int) int {
```ts
function edgeScore(edges: number[]): number {
const n = edges.length;
const sum = new Array(n).fill(0);
for (let i = 0; i < n; i++) {
sum[edges[i]] += i;
const cnt: number[] = Array(n).fill(0);
let ans: number = 0;
for (let i = 0; i < n; ++i) {
const j = edges[i];
cnt[j] += i;
if (cnt[ans] < cnt[j] || (cnt[ans] === cnt[j] && j < ans)) {
ans = j;
}
}
let res = 0;
for (let i = 0; i < n; i++) {
if (sum[res] < sum[i]) {
res = i;
return ans;
}
```

#### Rust

```rust
impl Solution {
pub fn edge_score(edges: Vec<i32>) -> i32 {
let n = edges.len();
let mut cnt = vec![0_i64; n];
let mut ans = 0;

for (i, &j) in edges.iter().enumerate() {
let j = j as usize;
cnt[j] += i as i64;
if cnt[ans] < cnt[j] || (cnt[ans] == cnt[j] && j < ans) {
ans = j;
}
}

ans as i32
}
return res;
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ class Solution {
int edgeScore(vector<int>& edges) {
int n = edges.size();
vector<long long> cnt(n);
for (int i = 0; i < n; ++i) {
cnt[edges[i]] += i;
}
int ans = 0;
for (int i = 0; i < n; ++i) {
if (cnt[ans] < cnt[i]) {
ans = i;
int j = edges[i];
cnt[j] += i;
if (cnt[ans] < cnt[j] || (cnt[ans] == cnt[j] && j < ans)) {
ans = j;
}
}
return ans;
}
};
};
20 changes: 8 additions & 12 deletions solution/2300-2399/2374.Node With Highest Edge Score/Solution.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
func edgeScore(edges []int) int {
n := len(edges)
cnt := make([]int, n)
for i, v := range edges {
cnt[v] += i
}
ans := 0
for i, v := range cnt {
if cnt[ans] < v {
ans = i
func edgeScore(edges []int) (ans int) {
cnt := make([]int, len(edges))
for i, j := range edges {
cnt[j] += i
if cnt[ans] < cnt[j] || (cnt[ans] == cnt[j] && j < ans) {
ans = j
}
}
return ans
}
return
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ class Solution {
public int edgeScore(int[] edges) {
int n = edges.length;
long[] cnt = new long[n];
for (int i = 0; i < n; ++i) {
cnt[edges[i]] += i;
}
int ans = 0;
for (int i = 0; i < n; ++i) {
if (cnt[ans] < cnt[i]) {
ans = i;
int j = edges[i];
cnt[j] += i;
if (cnt[ans] < cnt[j] || (cnt[ans] == cnt[j] && j < ans)) {
ans = j;
}
}
return ans;
}
}
}
11 changes: 5 additions & 6 deletions solution/2300-2399/2374.Node With Highest Edge Score/Solution.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
class Solution:
def edgeScore(self, edges: List[int]) -> int:
cnt = Counter()
for i, v in enumerate(edges):
cnt[v] += i
ans = 0
for i in range(len(edges)):
if cnt[ans] < cnt[i]:
ans = i
cnt = [0] * len(edges)
for i, j in enumerate(edges):
cnt[j] += i
if cnt[ans] < cnt[j] or (cnt[ans] == cnt[j] and j < ans):
ans = j
return ans
17 changes: 17 additions & 0 deletions solution/2300-2399/2374.Node With Highest Edge Score/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
impl Solution {
pub fn edge_score(edges: Vec<i32>) -> i32 {
let n = edges.len();
let mut cnt = vec![0_i64; n];
let mut ans = 0;

for (i, &j) in edges.iter().enumerate() {
let j = j as usize;
cnt[j] += i as i64;
if cnt[ans] < cnt[j] || (cnt[ans] == cnt[j] && j < ans) {
ans = j;
}
}

ans as i32
}
}
Loading

0 comments on commit 9559060

Please sign in to comment.