You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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;
}
}
key points: 数字溢出,side effect下10^5 * 10^5,超过Integer类型
所以要用Long类型存储和。
Source: https://leetcode.com/problems/node-with-highest-edge-score/
The text was updated successfully, but these errors were encountered: