Advertisement
Guest User

Grokking 243

a guest
Dec 1st, 2022
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. class Solution {
  2. int helper(int[][] costs, int i, int j) {
  3. if (i + j >= costs.length) {
  4. return 0;
  5. }
  6.  
  7. int minCost = Integer.MAX_VALUE;
  8. if (i >= costs.length) {
  9. minCost = costs[i + j][1] + helper(costs, i, j + 1);
  10. } else if (j >= costs.length / 2) {
  11. minCost = costs[i + j][0] + helper(costs, i + 1, j);
  12. } else {
  13. minCost = Math.min(costs[i + j][0] + helper(costs, i + 1, j),
  14. costs[i + j][1] + helper(costs, i, j + 1));
  15. }
  16. return minCost;
  17. }
  18.  
  19. public int twoCitySchedCost(int[][] costs) {
  20. return helper(costs, 0, 0);
  21. }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement