-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
key points: 数字溢出,side effect下10^5 * 10^5,超过Integer类型
所以要用Long类型存储和。
class Solution {
public int edgeScore(int[] edges) {
// key is point, value is sum of edges
Map<Integer, Long> map = new HashMap<>();
for (int i = 0; i < edges.length; ++i) {
if (!map.containsKey(edges[i])) {
map.put(edges[i], 0L);
}
long sum = map.get(edges[i]);
map.put(edges[i], sum + i);
}
// iteration
int highestPoint = edges.length;
long sum = -1;
for (Map.Entry<Integer, Long> entry : map.entrySet()) {
// System.out.println(entry.getKey() + ": " + entry.getValue());
if (entry.getValue() > sum) {
sum = entry.getValue();
highestPoint = entry.getKey();
} else if (entry.getValue() == sum) {
highestPoint = Math.min(highestPoint, entry.getKey());
}
}
return highestPoint;
}
}
Source: https://leetcode.com/problems/node-with-highest-edge-score/
Metadata
Metadata
Assignees
Labels
No labels