How to think when you meet the question?(thinking process)
Considered straightly counting all the subarrays that have maximum value between (L, R).\nIt costs O(n^2) time and O(n^2) space.\n\n\nint numSubarrayBoundedMax(
Considered straightly counting all the subarrays that have maximum value between (L, R).\nIt costs O(n^2) time and O(n^2) space.\n\n```\nint numSubarrayBoundedMax(vector<int>& A, int L, int R) {\n        int l = A.size();\n        int dp[l][l] = {0};\n\n        for(int i = 0; i < l; ++i)\n            for(int j = i + 1; j < l; ++j)\n                dp[i][j] = max(dp[i][j - 1], A[j]);\n        \n        int ans = 0;\n        for(int i = 0; i < l; ++i)\n        {\n            for(int j = i; j < l; ++j)\n                if (dp[i][j] >= L && dp[i][j] <= R) ans++;\n        }\n        return ans;\n    }\n```\n\t\nNo need to store every substring. To get the maxium of (i, j) (dp[i, j] denotes the maxium of (i, j)), according to dp[i, j] = max(dp[i,j-1],A[j]) \n\n   ```\n int numSubarrayBoundedMax(vector<int>& A, int L, int R) {\n        int l = A.size();\n        int cur = 0, pre = 0, ans = 0;\n        for(int i = 0; i < l; ++i)\n        {\n            pre = A[i];\n            if (pre >= L && pre <= R) ans++;\n            for(int j = i + 1; j < l; ++j)\n            {\n                cur = max(pre, A[j]);\n                if (cur >= L && cur <= R) ans++;\n                pre = cur;\n            }\n        }\n        return ans;\n    }\n```\npre means dp[i, j-1], cur means dp[i, j], remember **dp[i, j] denotes the maxium of (i, j)**\nspace down to O(1), then to think about how to reduce time cost.\nIn fact, when maxium(i, j -1) > R, all maxium(i, j + k) larger than R holds, then no need to compute them.\n```\n\nint numSubarrayBoundedMax(vector<int>& A, int L, int R) {\n        int l = A.size();\n        int cur = 0, pre = 0, ans = 0;\n        for(int i = 0; i < l; ++i)\n        {\n            pre = A[i];\n            if (pre >= L && pre <= R) ans++;\n            if (pre > R) continue; //here\n            for(int j = i + 1; j < l; ++j)\n            {\n                cur = max(pre, A[j]);\n                if (cur >= L && cur <= R) ans++;\n                if (cur > R) break; //here\n                pre = cur;\n            }\n        }\n        return ans;\n    }\n```\nThis is my thinking process.
[]