-
Notifications
You must be signed in to change notification settings - Fork 115
/
1242-WebCrawlerMultithreaded.cs
66 lines (57 loc) · 2 KB
/
1242-WebCrawlerMultithreaded.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//-----------------------------------------------------------------------------
// Runtime: 368ms
// Memory Usage: 41.2 MB
// Link: https://leetcode.com/submissions/detail/367598201/
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace LeetCode
{
/**
* // This is the HtmlParser's API interface.
* // You should not implement it, or speculate about its implementation
* class HtmlParser {
* public List<String> GetUrls(String url) {}
* }
*/
public class _1242_WebCrawlerMultithreaded
{
public IList<string> Crawl(string startUrl, HtmlParser htmlParser)
{
var uri = new Uri(startUrl);
var visited = new HashSet<string>();
var queue = new List<string>();
queue.Add(startUrl);
while (queue.Count > 0)
{
var nextWave = new List<string>();
Parallel.ForEach(queue, (next) =>
{
if (visited.Contains(next) || !next.StartsWith($"http://{uri.Host}")) return;
visited.Add(next);
nextWave.AddRange(htmlParser.GetUrls(next));
});
queue = nextWave;
}
return visited.ToArray();
}
public class HtmlParser
{
private readonly IDictionary<string, List<string>> connections;
public HtmlParser(List<string> urls, List<int[]> edges)
{
connections = new Dictionary<string, List<string>>();
foreach (var url in urls)
connections.Add(url, new List<string>());
foreach (var edge in edges)
connections[urls[edge[0]]].Add(urls[edge[1]]);
}
public List<string> GetUrls(string url)
{
return connections[url];
}
}
}
}