Java | Best Solution | Minimum Time to Make Array Sum At Most x
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public int minimumTime(List<Integer> nums1, List<Integer> nums2, int x) {\n        ArrayList<Integer> ids = new ArrayList<Integer>();\n        int n = nums1.size();\n        for (int i = 0; i < n; ++i) {\n            ids.add(nums2.get(i)*1001+nums1.get(i));\n        }\n        Collections.sort(ids);\n        List<Integer> a = new ArrayList<>();\n        List<Integer> b = new ArrayList<>();\n        int sum1 = 0;\n        int sum2 = 0;\n        for (int i = 0; i < n; ++i) {\n            b.add(ids.get(i)/1001);\n            a.add(ids.get(i)%1001);\n            sum1 += a.get(i);\n            sum2 += b.get(i);\n        }\n        int[][] dp = new int[n+1][n+1];\n        dp[0][0] = sum1;\n        for (int i = 1; i <= n; ++i) {\n            dp[0][i] = sum1;\n            dp[i][0] = dp[i-1][0]+sum2;\n        }\n        if (sum1<=x)\n            return 0;\n        for (int i = 1; i <= n; ++i) {\n            for (int j = 1; j <= n; ++j) {\n                dp[i][j] = dp[i][j-1];\n                dp[i][j] = Math.min(dp[i][j], dp[i-1][j-1]+sum2-a.get(j-1)-b.get(j-1)*i);\n                if (dp[i][j]<=x)\n                    return i;\n            }\n        }\n        return -1;\n    }\n}\n```
['Java']