I'm posting my code for a LeetCode problem copied here. If you would like to review, please do so. Thank you for your time!


In a network of nodes, each node i is directly connected to another node j if and only if graph[i][j] = 1.

Some nodes initial are initially infected by malware. Whenever two nodes are directly connected and at least one of those two nodes is infected by malware, both nodes will be infected by malware. This spread of malware will continue until no more nodes can be infected in this manner.

Suppose M(initial) is the final number of nodes infected with malware in the entire network, after the spread of malware stops.

We will remove one node from the initial list. Return the node that if removed, would minimize M(initial). If multiple nodes could be removed to minimize M(initial), return such a node with the smallest index.

Note that if a node was removed from the initial list of infected nodes, it may still be infected later as a result of the malware spread.












#include <vector>
#include <algorithm>

struct Solution {
    std::vector<int> parents;
    inline int minMalwareSpread(const std::vector<std::vector<int>> &graph, const std::vector<int> &initial) {
        size_t length = graph.size();

        for (size_t index = 0; index < length; index++) {

        for (size_t row = 0; row < length; row++) {
            for (size_t col = row + 1; col < length; col++) {
                if (graph[row][col]) {
                    union_uf(row, col);

        std::vector<int> areas(length, 0);
        std::vector<int> malwares(length, 0);

        for (size_t area = 0; area < length; area++) {

        for (size_t init : initial) {

        std::vector<int> min_spread = {1, 0};

        for (int init : initial) {
            min_spread = std::min(min_spread, {(malwares[find(init)] == 1) * (-areas[find(init)]), init});

        return min_spread[1];

    const inline size_t find(const size_t x) {
        if (x != parents[x]) {
            parents[x] = find(parents[x]);

        return parents[x];

    const inline void union_uf(const size_t x, const size_t y) {
        parents[find(x)] = find(y);


You can read about various algorithms here in this link:

  • We'd first map the graph using Disjoint Set Union (DSU).
  • We'd count all areas as well as all infected sets.
  • If there'd be only one malware set, we would return the largest. Otherwise, we'd return the set with lowest index using the last loop.


  • 1
    \$\begingroup\$ Hi Emma, Could you please add some algorithmic explanation about your solution? \$\endgroup\$
    – Yonlif
    Commented Jul 9, 2020 at 19:37

1 Answer 1


Function prefix qualifiers

I seem to remember this being covered in your reviews before, but:

const inline size_t find(const size_t x)

This benefits from neither of the const declarations nor the inline. Both the return value and the argument are passed-by-value scalars and do not benefit from const. inline, for all intents and purposes, has no effect, particularly where a compiler is told to optimize your code - and even if it did have an effect, it's a bad idea to tell the optimizer what it should and shouldn't inline. It will understand what's good for your code better than the programmer does 99% of the time.

If these concepts are confusing or difficult to apply to code on a practical basis, that is understandable - feel free to ask follow-up questions.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.