Skip to content

feat: add solutions to lc problem: No.2049 #2096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
feat: add cs solution
  • Loading branch information
yanglbme committed Dec 13, 2023
commit 4a755ee92cb90ec1f60817256ea33bec1975e790
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,57 @@ function countHighestScoreNodes(parents: number[]): number {
}
```

### **C#**

```cs
public class Solution {
private List<int>[] g;
private int ans;
private long mx;
private int n;

public int CountHighestScoreNodes(int[] parents) {
n = parents.Length;
g = new List<int>[n];
for (int i = 0; i < n; ++i) {
g[i] = new List<int>();
}
for (int i = 1; i < n; ++i) {
g[parents[i]].Add(i);
}

Dfs(0, -1);
return ans;
}

private int Dfs(int i, int fa) {
int cnt = 1;
long score = 1;

foreach (int j in g[i]) {
if (j != fa) {
int t = Dfs(j, i);
cnt += t;
score *= t;
}
}

if (n - cnt > 0) {
score *= n - cnt;
}

if (mx < score) {
mx = score;
ans = 1;
} else if (mx == score) {
++ans;
}

return cnt;
}
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,57 @@ function countHighestScoreNodes(parents: number[]): number {
}
```

### **C#**

```cs
public class Solution {
private List<int>[] g;
private int ans;
private long mx;
private int n;

public int CountHighestScoreNodes(int[] parents) {
n = parents.Length;
g = new List<int>[n];
for (int i = 0; i < n; ++i) {
g[i] = new List<int>();
}
for (int i = 1; i < n; ++i) {
g[parents[i]].Add(i);
}

Dfs(0, -1);
return ans;
}

private int Dfs(int i, int fa) {
int cnt = 1;
long score = 1;

foreach (int j in g[i]) {
if (j != fa) {
int t = Dfs(j, i);
cnt += t;
score *= t;
}
}

if (n - cnt > 0) {
score *= n - cnt;
}

if (mx < score) {
mx = score;
ans = 1;
} else if (mx == score) {
++ans;
}

return cnt;
}
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
public class Solution {
private List<int>[] g;
private int ans;
private long mx;
private int n;

public int CountHighestScoreNodes(int[] parents) {
n = parents.Length;
g = new List<int>[n];
for (int i = 0; i < n; ++i) {
g[i] = new List<int>();
}
for (int i = 1; i < n; ++i) {
g[parents[i]].Add(i);
}

Dfs(0, -1);
return ans;
}

private int Dfs(int i, int fa) {
int cnt = 1;
long score = 1;

foreach (int j in g[i]) {
if (j != fa) {
int t = Dfs(j, i);
cnt += t;
score *= t;
}
}

if (n - cnt > 0) {
score *= n - cnt;
}

if (mx < score) {
mx = score;
ans = 1;
} else if (mx == score) {
++ans;
}

return cnt;
}
}