Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leetcode 2374. Node With Highest Edge Score #134

Open
Woodyiiiiiii opened this issue Aug 14, 2022 · 0 comments
Open

Leetcode 2374. Node With Highest Edge Score #134

Woodyiiiiiii opened this issue Aug 14, 2022 · 0 comments

Comments

@Woodyiiiiiii
Copy link
Owner

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/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant