Redian新闻
>
DT以见利就收为原则?
avatar
DT以见利就收为原则?# Stock
s*t
1
A sparse matrix and find the connected components using multi threading
using all the cores.
execution time criteria 3 s => good, 2s =>great 700 ms => excellent
avatar
q*u
2
不要贪心?
这个大起大落的股市?
赚钱盈利要快?
搞不好就要悲惨?
股市大方向严重不明?
青蛙蝌蚪还是小心为妙?
观望学习为主?
不能狂妄自大?
战略多头和战略空头不是一般人可以胜任?
是不?
avatar
s*t
3
下面是单线程,而且图是adjacency list
public class Solution {
//5763 ms, method: bfs, 时间O(N + E),空间O(N)。
public List> connectedSet(ArrayList
nodes) {
List> res = new ArrayList<>();
List path = new ArrayList<>();
Set visited = new HashSet<>();
for (UndirectedGraphNode node : nodes) {
if (!visited.contains(node)) {
path.clear();
Queue queue = new LinkedList<>();
queue.offer(node);
visited.add(node);
path.add(node.label);
while (!queue.isEmpty()) {
UndirectedGraphNode p = queue.poll();
for (UndirectedGraphNode v : p.neighbors) {
if (!visited.contains(v)) {
visited.add(v);
queue.offer(v);
path.add(v.label);
}
}
}
Collections.sort(path);
res.add(new ArrayList(path));
}
}
return res;
}
//7451 ms, method: dfs, 时间O(N + E),空间O(N)
public List> connectedSet2(ArrayList
nodes) {
List> res = new ArrayList<>();
List path = new ArrayList<>();
Set visited = new HashSet<>();
for (UndirectedGraphNode p : nodes) {
if (!visited.contains(p)) {
dfs(p, visited, path);
Collections.sort(path);
res.add(new ArrayList(path));
path.clear();
}
}
return res;
}
private void dfs(UndirectedGraphNode p, Set visited
, List path) {
visited.add(p);
path.add(p.label);
for (UndirectedGraphNode v : p.neighbors) {
if (!visited.contains(v))
dfs(v, visited, path);
}
}
}
avatar
s*t
4
Nobody knows?
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。