Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit eed4b10

Browse files
feat: solve No.2374,2390
1 parent 757b2af commit eed4b10

File tree

2 files changed

+181
-0
lines changed

2 files changed

+181
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# 2374. Node With Highest Edge Score
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Hash Table, Graph.
5+
- Similar Questions: Two Sum, Sort Characters By Frequency, Sort Array by Increasing Frequency.
6+
7+
## Problem
8+
9+
You are given a directed graph with `n` nodes labeled from `0` to `n - 1`, where each node has **exactly one** outgoing edge.
10+
11+
The graph is represented by a given **0-indexed** integer array `edges` of length `n`, where `edges[i]` indicates that there is a **directed** edge from node `i` to node `edges[i]`.
12+
13+
The **edge score** of a node `i` is defined as the sum of the **labels** of all the nodes that have an edge pointing to `i`.
14+
15+
Return **the node with the highest **edge score****. If multiple nodes have the same **edge score**, return the node with the **smallest** index.
16+
17+
18+
Example 1:
19+
20+
![](https://assets.leetcode.com/uploads/2022/06/20/image-20220620195403-1.png)
21+
22+
```
23+
Input: edges = [1,0,0,0,0,7,7,5]
24+
Output: 7
25+
Explanation:
26+
- The nodes 1, 2, 3 and 4 have an edge pointing to node 0. The edge score of node 0 is 1 +たす 2 +たす 3 +たす 4 = 10.
27+
- The node 0 has an edge pointing to node 1. The edge score of node 1 is 0.
28+
- The node 7 has an edge pointing to node 5. The edge score of node 5 is 7.
29+
- The nodes 5 and 6 have an edge pointing to node 7. The edge score of node 7 is 5 + 6 = 11.
30+
Node 7 has the highest edge score so return 7.
31+
```
32+
33+
Example 2:
34+
35+
![](https://assets.leetcode.com/uploads/2022/06/20/image-20220620200212-3.png)
36+
37+
```
38+
Input: edges = [2,0,0,2]
39+
Output: 0
40+
Explanation:
41+
- The nodes 1 and 2 have an edge pointing to node 0. The edge score of node 0 is 1 + 2 = 3.
42+
- The nodes 0 and 3 have an edge pointing to node 2. The edge score of node 2 is 0 + 3 = 3.
43+
Nodes 0 and 2 both have an edge score of 3. Since node 0 has a smaller index, we return 0.
44+
```
45+
46+
47+
**Constraints:**
48+
49+
50+
51+
- `n == edges.length`
52+
53+
- `2 <= n <= 105`
54+
55+
- `0 <= edges[i] < n`
56+
57+
- `edges[i] != i`
58+
59+
60+
61+
## Solution
62+
63+
```javascript
64+
/**
65+
* @param {number[]} edges
66+
* @return {number}
67+
*/
68+
var edgeScore = function(edges) {
69+
var sumMap = Array(edges.length).fill(0);
70+
var maxSumNode = 0;
71+
for (var i = 0; i < edges.length; i++) {
72+
sumMap[edges[i]] += i;
73+
if (sumMap[edges[i]] > sumMap[maxSumNode]
74+
|| (sumMap[edges[i]] === sumMap[maxSumNode] && maxSumNode > edges[i])) {
75+
maxSumNode = edges[i];
76+
}
77+
}
78+
return maxSumNode;
79+
};
80+
```
81+
82+
**Explain:**
83+
84+
nope.
85+
86+
**Complexity:**
87+
88+
* Time complexity : O(n).
89+
* Space complexity : O(n).
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# 2390. Removing Stars From a String
2+
3+
- Difficulty: Medium.
4+
- Related Topics: String, Stack, Simulation.
5+
- Similar Questions: Backspace String Compare, Remove All Adjacent Duplicates In String.
6+
7+
## Problem
8+
9+
You are given a string `s`, which contains stars `*`.
10+
11+
In one operation, you can:
12+
13+
14+
15+
- Choose a star in `s`.
16+
17+
- Remove the closest **non-star** character to its **left**, as well as remove the star itself.
18+
19+
20+
Return **the string after **all** stars have been removed**.
21+
22+
**Note:**
23+
24+
25+
26+
- The input will be generated such that the operation is always possible.
27+
28+
- It can be shown that the resulting string will always be unique.
29+
30+
31+
32+
Example 1:
33+
34+
```
35+
Input: s = "leet**cod*e"
36+
Output: "lecoe"
37+
Explanation: Performing the removals from left to right:
38+
- The closest character to the 1st star is 't' in "leet**cod*e". s becomes "lee*cod*e".
39+
- The closest character to the 2nd star is 'e' in "lee*cod*e". s becomes "lecod*e".
40+
- The closest character to the 3rd star is 'd' in "lecod*e". s becomes "lecoe".
41+
There are no more stars, so we return "lecoe".
42+
```
43+
44+
Example 2:
45+
46+
```
47+
Input: s = "erase*****"
48+
Output: ""
49+
Explanation: The entire string is removed, so we return an empty string.
50+
```
51+
52+
53+
**Constraints:**
54+
55+
56+
57+
- `1 <= s.length <= 105`
58+
59+
- `s` consists of lowercase English letters and stars `*`.
60+
61+
- The operation above can be performed on `s`.
62+
63+
64+
65+
## Solution
66+
67+
```javascript
68+
/**
69+
* @param {string} s
70+
* @return {string}
71+
*/
72+
var removeStars = function(s) {
73+
var stack = [];
74+
for (var i = 0; i < s.length; i++) {
75+
if (s[i] === '*') {
76+
stack.pop();
77+
} else {
78+
stack.push(s[i]);
79+
}
80+
}
81+
return stack.join('');
82+
};
83+
```
84+
85+
**Explain:**
86+
87+
nope.
88+
89+
**Complexity:**
90+
91+
* Time complexity : O(n).
92+
* Space complexity : O(n).

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /