[Java] DFS Permutation TLE
This solution only passes 10/41 testcases. It\'s apparently very slow because it tries to search through permutations of doing tasks in different orders, so tha
This solution only passes 10/41 testcases. It\'s apparently very slow because it tries to search through permutations of doing tasks in different orders, so that gives a runtime of O(n!) where n is number of tasks...\nComments/thoughts on improvement are welcome!\n```java\nclass Solution {\n    // dfs, do task i, find min energy for doing rest of the tasks\n    // res = min of doing i now for i = 0...n\n    // can use dp to save some results\n    // pass in required energy for each pass, if not enough at any point, increase\n    public int minimumEffort(int[][] tasks) {\n        int n = tasks.length, required = 0;\n        boolean[] visited = new boolean[n];\n        for (int i = 0; i < n; i++) {\n            required += tasks[i][0];\n        }\n        return minHelper(tasks, visited, n, required);\n    }\n    \n    private int minHelper(int[][] tasks, boolean[] visited, int remain, int required) {\n        if (remain == 0) {\n            return required;\n        }\n        int res = Integer.MAX_VALUE, n = tasks.length;\n        for (int i = 0; i < n; i++) {\n            if (visited[i]) {\n                continue;\n            }\n            visited[i] = true;\n            if (required < tasks[i][1]) {\n                required = tasks[i][1];\n            }\n            res = Math.min(res, tasks[i][0] + minHelper(tasks, visited, remain - 1, required - tasks[i][0]));\n            visited[i] = false;\n        }\n        return res;\n    }\n}\n```
[]