Advertisement
Guest User

Grokking 243-2

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