80% Beats in C++ ||  Easy understandable DP Tabulation solution for absolute Beginners 💯✅
Approach\n Describe your approach to solving the problem. \nFor understanding of the approach form scratch please refer to this\n\nNow for optimising the code i
# Approach\n<!-- Describe your approach to solving the problem. -->\nFor understanding of the approach form scratch please refer to [this](https://leetcode.com/problems/find-the-count-of-monotonic-pairs-i/solutions/5623232/92-beats-in-c-easy-understandable-dp-tabulation-solution-for-absolute-beginners)\n\nNow for optimising the code in the solution given in the link , we are modifying the condition in the if statement from \nthis (cur>=prev and nums[i]-cur <=nums[i-1]-prev)\nto this (prev <= min(cur, cur - nums[i] + nums[i - 1])) \nNow from the new condition we can understand that while cur is increasing the range for prev also increases , now for calculating the value for dp[i][cur] we need the previous values in the same row which is basically the prefix sum logic so because of that we are initialising the variable ways outside the loop for cur , so that the ways will store the number of ways till the previous cur and we can add our new ways to it .\nwe removed a loop so we have decreased the time complexity.\n\n# Code\n```\nclass Solution {\npublic:\n    int mod = 1e9 + 7;\n    int countOfPairs(vector<int>& nums) {\n        ios_base::sync_with_stdio(false);\n        cin.tie(NULL);\n        int n = nums.size();\n        vector<vector<int>> dp(n, vector<int>(1001, 0));\n        for (int i = 0; i <= nums[0]; i++) {\n            dp[0][i] = 1;\n        }\n        for (int i = 1; i < n; i++) {\n            int ways = 0;\n            int prev = 0;\n            for (int cur = 0; cur <= nums[i]; cur++) {\n                if (prev <= min(cur, cur - nums[i] + nums[i - 1])) {\n                    ways = (ways + dp[i - 1][prev]) % mod;\n                    prev++;\n                }\n                dp[i][cur] = ways;\n            }\n        }\n\n        int ans = 0;\n        for (int i = 0; i < 1001; i++) {\n            ans = (ans + dp[n - 1][i]) % mod;\n        }\n        return ans;\n    }\n};\n```
['Dynamic Programming', 'C++']