diff --git a/solution/2300-2399/2374.Node With Highest Edge Score/README.md b/solution/2300-2399/2374.Node With Highest Edge Score/README.md index 85072537277b..f045992cac6d 100644 --- a/solution/2300-2399/2374.Node With Highest Edge Score/README.md +++ b/solution/2300-2399/2374.Node With Highest Edge Score/README.md @@ -68,13 +68,15 @@ tags: -### 方法一:遍历计数 +### 方法一:一次遍历 -定义 $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}$ 的长度。 @@ -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 ``` @@ -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; @@ -122,13 +122,12 @@ public: int edgeScore(vector& edges) { int n = edges.size(); vector 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; @@ -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 } ``` @@ -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 { + 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; } ``` diff --git a/solution/2300-2399/2374.Node With Highest Edge Score/README_EN.md b/solution/2300-2399/2374.Node With Highest Edge Score/README_EN.md index af10493a773d..806f45f7f50d 100644 --- a/solution/2300-2399/2374.Node With Highest Edge Score/README_EN.md +++ b/solution/2300-2399/2374.Node With Highest Edge Score/README_EN.md @@ -68,7 +68,15 @@ Nodes 0 and 2 both have an edge score of 3. Since node 0 has a smaller index, we -### 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}$. @@ -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 ``` @@ -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; @@ -116,13 +122,12 @@ public: int edgeScore(vector& edges) { int n = edges.size(); vector 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; @@ -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 } ``` @@ -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 { + 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; } ``` diff --git a/solution/2300-2399/2374.Node With Highest Edge Score/Solution.cpp b/solution/2300-2399/2374.Node With Highest Edge Score/Solution.cpp index 93b1a019e3d7..e4fad3732da7 100644 --- a/solution/2300-2399/2374.Node With Highest Edge Score/Solution.cpp +++ b/solution/2300-2399/2374.Node With Highest Edge Score/Solution.cpp @@ -3,15 +3,14 @@ class Solution { int edgeScore(vector& edges) { int n = edges.size(); vector 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; } -}; \ No newline at end of file +}; diff --git a/solution/2300-2399/2374.Node With Highest Edge Score/Solution.go b/solution/2300-2399/2374.Node With Highest Edge Score/Solution.go index 17d5337f8e95..fd956a67127c 100644 --- a/solution/2300-2399/2374.Node With Highest Edge Score/Solution.go +++ b/solution/2300-2399/2374.Node With Highest Edge Score/Solution.go @@ -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 -} \ No newline at end of file + return +} diff --git a/solution/2300-2399/2374.Node With Highest Edge Score/Solution.java b/solution/2300-2399/2374.Node With Highest Edge Score/Solution.java index 124f0130ae6e..77223ba92c9c 100644 --- a/solution/2300-2399/2374.Node With Highest Edge Score/Solution.java +++ b/solution/2300-2399/2374.Node With Highest Edge Score/Solution.java @@ -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; } -} \ No newline at end of file +} diff --git a/solution/2300-2399/2374.Node With Highest Edge Score/Solution.py b/solution/2300-2399/2374.Node With Highest Edge Score/Solution.py index 1f0d13764fdc..e7339b81d823 100644 --- a/solution/2300-2399/2374.Node With Highest Edge Score/Solution.py +++ b/solution/2300-2399/2374.Node With Highest Edge Score/Solution.py @@ -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 diff --git a/solution/2300-2399/2374.Node With Highest Edge Score/Solution.rs b/solution/2300-2399/2374.Node With Highest Edge Score/Solution.rs new file mode 100644 index 000000000000..0cd989c692dc --- /dev/null +++ b/solution/2300-2399/2374.Node With Highest Edge Score/Solution.rs @@ -0,0 +1,17 @@ +impl Solution { + pub fn edge_score(edges: Vec) -> 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 + } +} diff --git a/solution/2300-2399/2374.Node With Highest Edge Score/Solution.ts b/solution/2300-2399/2374.Node With Highest Edge Score/Solution.ts index 2d872c16ce15..19f088536fde 100644 --- a/solution/2300-2399/2374.Node With Highest Edge Score/Solution.ts +++ b/solution/2300-2399/2374.Node With Highest Edge Score/Solution.ts @@ -1,14 +1,13 @@ 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; - } - let res = 0; - for (let i = 0; i < n; i++) { - if (sum[res] < sum[i]) { - res = 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; } } - return res; + return ans; }