leetcode: TC of top-down memorization

example to explain how to calculate Time Complexity

the memo size means each state will be calculated only once

how about the TC in each state? 

class Solution {
    public boolean wordBreak(String s, List<String> wordDict) {
        return dfs(s,wordDict,0,new Boolean[s.length()]);
    }
    // Given n as the length of s, m as the length of wordDict, and k as the average length of the words in wordDict,
    public boolean dfs(String s, List<String> wordDict,int start,Boolean[] memo) {
        if(start == s.length()){
            return true;
        }
        if(memo[start] != null) return memo[start]; // n states
        boolean res = false;
        for(String str: wordDict){ // to calculate each state, iterate m times
            if(start+str.length() > s.length()) continue;
            String t = s.substring(start,start+str.length()); // cost k times
            if(t.equals(str) && dfs(s,wordDict,start+str.length(),memo)){
               res = true;
            }
        }
        return memo[start] = res;
    }
}

so the TC is O(n * m * k)

same with Coin Change 

public class Solution {

  public int coinChange(int[] coins, int amount) {
    if (amount < 1) return 0;
    return coinChange(coins, amount, new int[amount]);
  }

  private int coinChange(int[] coins, int rem, int[] count) {
    if (rem < 0) return -1;
    if (rem == 0) return 0;
    if (count[rem - 1] != 0) return count[rem - 1]; // amount states
    int min = Integer.MAX_VALUE;
    for (int coin : coins) { // for each state, iterate n times
      int res = coinChange(coins, rem - coin, count);
      if (res >= 0 && res < min)
        min = 1 + res;
    }
    count[rem - 1] = (min == Integer.MAX_VALUE) ? -1 : min;
    return count[rem - 1];
  }
}

so total is O(S*N)

class Solution {
    List<String> res = new ArrayList<>();
    public List<String> letterCombinations(String digits) {
        HashMap<Character,Character[]> map = new HashMap<>();
        map.put('2',new Character[]{'a','b','c'});
        map.put('3',new Character[]{'d','e','f'});
        map.put('4',new Character[]{'g','h','i'});
        map.put('5',new Character[]{'j','k','l'});
        map.put('6',new Character[]{'m','n','o'});
        map.put('7',new Character[]{'p','q','r','s'});
        map.put('8',new Character[]{'t','u','v'});
        map.put('9',new Character[]{'w','x','y','z'});
        if(digits.equals("")) return res;
        dfs(0,digits,map,new StringBuilder(""));
        return res;
    }
    public void dfs(int idx, String digits, HashMap<Character, Character[]> map, StringBuilder sb){ 
        if(idx == digits.length()){ // N stages
            res.add(new String(sb));
            return;
        }
        char num = digits.charAt(idx);
        if(!map.containsKey(num)) return;
        for(Character curr : map.get(num)){ // each stage iterate 4^N times
            sb.append(curr);
            dfs(idx+1,digits,map,sb);
            sb.deleteCharAt(sb.length()-1);
        }

    }
}

O(N * 4 ^ N)

死记硬背dfs的tc: 当第一层9,第二层8,。。。一共k层:

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    public List<List<Integer>> combinationSum3(int k, int n) {
        dfs(0,1,k,n,new ArrayList<>());
        return res;
    }
    public void dfs(int depth, int start, int k, int target, List<Integer> lst){
        if(depth == k){
            if(target == 0) res.add(new ArrayList<>(lst));  // each exploration takes O(K) to make a copy
            return;
        }
        for(int i = start; i<= 9; i++){
            lst.add(i);
            if(target - i >=0)  dfs(depth+1,i+1,k,target-i,lst);
            lst.remove(lst.size()-1);
        }
    }
}

 

posted @ 2023-08-19 22:37  PEAR2020  阅读(1)  评论(0编辑  收藏  举报