[LeetCode The Hard Way] Solution with Explaination
Please check out LeetCode The Hard Way for more solution explanations and tutorials. If you like it, please give a star and watch my Github Repository.\n\n---\n
Please check out [LeetCode The Hard Way](https://wingkwong.github.io/leetcode-the-hard-way/) for more solution explanations and tutorials. If you like it, please give a star and watch my [Github Repository](https://github.com/wingkwong/leetcode-the-hard-way).\n\n---\n\n```cpp\nclass Solution {\npublic:\n    int minNumberOfHours(int initialEnergy, int initialExperience, vector<int>& energy, vector<int>& experience) {\n        int n = energy.size(), ans = 0;\n        for (int i = 0; i < n; i++) {\n            // if initialEnergy is not enough, then we need to add some training hours\n            if (initialEnergy <= energy[i]) {\n                // + 1 here because we need it to be strictly greater\n                int need = energy[i] - initialEnergy + 1;\n                // ans is the number of training hours\n                ans += need;\n                // since we train, we gain energy\n                initialEnergy += need;\n            }\n            // similarly for experience\n            // if initialExperience is not enough, then we need to add some training hours\n            if (initialExperience <= experience[i]) {\n                // + 1 here because we need it to be strictly greater\n                int need = experience[i] - initialExperience + 1;\n                // ans is the number of training hours\n                ans += need;\n                // since we train, we gain experience\n                initialExperience += need;\n            }\n            // start a competition\n            // increases your experience by experience[i]\n            initialEnergy -= energy[i];\n            // and decreases your energy by energy[i]\n            initialExperience += experience[i];\n        }\n        return ans;\n    }\n};\n```
['Greedy', 'C++']