C++ || simulate || easy to understand || 0ms (100% fast) || O(n) time, O(1) space
We can just simluate this. I have renamed the method arguments to make the code a bit more terse.\n\n\n    int minNumberOfHours(int energy, int experience, vect
We can just simluate this. I have renamed the method arguments to make the code a bit more terse.\n\n```\n    int minNumberOfHours(int energy, int experience, vector<int>& energies, vector<int>& experiences) {\n        int training = 0;\n        for (int i = 0; i < size(energies); ++i) {\n            // Energy.\n            if (energy <= energies[i]) {\n                // How much more training hours do we need to be 1 higher?\n                training += energies[i] + 1 - energy;\n                energy = energies[i] + 1;\n            }\n            energy -= energies[i];\n            \n            // Experience.\n            if (experience <= experiences[i]) {\n                // Same here.\n                training += experiences[i] + 1 - experience;\n                experience = experiences[i] + 1;\n            }\n            experience += experiences[i];\n        }\n        return training;\n    }\n```\n\nTime complexity: ```O(n)``` we look at each of the ```energies``` and ```experiences``` element onces.\nSpace complexity: ```O(1)``` only a loop variable and one integer for the result\n\nWe could also simluate energy and experience independently, this allows use to use structured loop.\n\n```\n    int minNumberOfHours(int energy, int experience, vector<int>& energies, vector<int>& experiences) {\n        int training = 0;\n\n        // Energy.\n        for (int eng : energies) {\n            if (energy <= eng) {\n                // How much more training hours do we need to be 1 higher?\n                training += eng + 1 - energy;\n                energy = eng + 1;\n            }\n            energy -= eng;\n        }\n            \n        // Experience.\n        for (int exp : experiences) {\n            if (experience <= exp) {\n                // Same here.\n                training += exp + 1 - experience;\n                experience = exp + 1;\n            }\n            experience += exp;\n        }\n\n        return training;\n    }\n```\n\nTime and space complexity is the same. The actual runtime seem to be a bit slower, maybe because we have more loop overhead, but I guess without proper microbench marks this is hard to compare.\n\n\n
['C', 'Simulation']