楼主: Lingzhi..0614
跳转到指定楼层
上一主题 下一主题
收起左侧

2020秋招- 刷题-项目-OOD-System Design

🔗
 楼主| Lingzhi..0614 2020-4-22 14:33:12 | 只看该作者
全局:
reviewed :
85. Maximal Rectangle
221. Maximal Square
304. Range Sum Query 2D - Immutable
1277. Count Square Submatrices with All Ones
1143. Longest Common Subsequence
115. Distinct Subsequences
1092. Shortest Common Supersequence
583. Delete Operation for Two Strings
688. Knight Probability in Chessboard

Today :
155. Min Stack
       solution: method one, we can store int[]{value, min} to stack , each time we push , we calculate the min and push to stack
                      method 2: we can use two stacks, stack, minstack, minstack is used to store min value, if stack.peek() == minstack.peek() , and it is pop operation , we pop both
                      method 3: stack, minstack. when push, if minstack is empty or x < minstack.peek(), we push to minstack({x,1}). if x == minstack.peek(), add  minstack.peek()[1]++
                                                                   when pop, if stack.peek() == minstack.peek(), we do count--, if count ==0 , we pop the value from minstack.
844. Backspace String Compare
          we start from the end to begin, while s[i] == '#', skipcount++; i--;
                                                                 if s[i] != '#' && skipcount >0 , skipcount--; i--;
                                                              while t[j] =='#', skipcont++; j--;
                                                                 if(t[j] !='#', && skipcount >0      skipcount--; j--;
                                                               if(s[i] == t[j]) i--; j--


576. Out of Boundary Paths
         dp[i][j][k] : the number of path starting at(i,j) with at most k move to move out of the boundary
          base case: dp[i][j][0] =0; dp[i][j][1] = (i ==0 || j ==0 || i==n-1 || j==n-1) ? 2 : 0;
                            dp[i][j][k] = for(x,y) if x,y out of boundary, +1
                                                              else += dp[x][y][k-1];
           return dp[i][j][k];

935. Knight Dialer
       dp[i][n] : the number of distince number you can dial with in n-1 hop. with last digit end at i.
       base case : dp[i][0] = 0;  dp[i][1] = 1;
       induction rule dp[i][k] += dp[j][k-1] where j is the pre digit that can reach at i with 1 hop.
       return   sum of dp[i][n];
322. Coin Change
           dp[i] = number of coins that make up to i with coins.
           base case dp[0] = 0;
           induction rule dp[i] = for all coins[j] <= i,  dp[i] = Math.min(dp[i], 1+dp[i-coins[j]]);
           return dp[amount];


518. Coin Change 2
          method 1: dp[i][j] the number of combination that sum up to j, with coins[0, i-1]
           dp[*][0] = 1 ; dp[0][*] =0;
            induction rule :
                      dp[i][j] = use i or not use i, dp[i-1][j] + (coins[i-1] <= j ? dp[i][j-coins[i-1]] : 0);
           return dp[n][amount];

            method 2: dp[i] : number of comnbination of that sum up to i;
               for each coin[j] , if amount >= coint[j]
                             combination[amount] += combination[amount-coins[j]);

                                     0,1,2,3,4,5
coins[1]                        1,1,1,1,1,1
coins[1,2]                      dp[k] +=dp[k-2] it mean for combination of k-2, we can add 2 to make those combination sum to k;
coins[1,2,3]
回复

使用道具 举报

🔗
 楼主| Lingzhi..0614 2020-4-23 14:56:40 | 只看该作者
全局:
4-22
155. Min Stack
844. Backspace String Compare
576. Out of Boundary Paths
935. Knight Dialer
322. Coin Change
518. Coin Change 2
1139. Largest 1-Bordered Square
718. Maximum Length of Repeated Subarray
198. House Robber
213. House Robber II
309. Best Time to Buy and Sell Stock with Cooldown
740. Delete and Earn
790. Domino and Tromino Tiling

Today:
377. Combination Sum IV    (518)
       dp[i] the number of combination that sum to i
        base case : dp[0]= 1;
        induction rule  dp[ i] = for each coin  dp[i] +=dp[i-coin].


1220. Count Vowels Permutation
            dp[i][k] the number of string of length k, that ends at A[i]
             base case dp[i][1] = 1;
             inducion rule : dp[i][k] = for p : pre, dp[i][k] += dp[p][k-1];
             return sum(dp[i][k]) for all i.
416. Partition Equal Subset Sum
            first get sum , if sum%2 != 0, false.
            dp[i][j] : whether can sum to j with nums[0,i-1]
            base case dp[0][0] = true;  dp[0][j] = false; dp[i][0] = true;
                              dp[i][j] = dp[i-1][j] || dp[i-1][j-num[i-1]]
1230. Toss Strange Coins
               dp[i][j] : the prob that has j heads with first i toss
               dp[0][0] = 1;
                 dp[0][*] = 0;
                 dp[i] [0] = tail * dp[i-1][0];
                 dp[i][j] = tail *dp[i-1][j] + head*dp[i-1][j-1];
return dp[n][target]
494. Target Sum
      dp[i][j] = number of ways to make sum to j with first [0,i-1]
     first get sum, if   target>sum || target<-sum, 0
        range [-sum, sum]
          base case dp[0][0+sum] =1;
           for j in range      dp[i][j] = dp[i-1][j-nums[i-1]] + dp[i-1][j+nums[i-1]].  make sure it is not out of boundary.
        return dp[n][target+sum];
回复

使用道具 举报

🔗
 楼主| Lingzhi..0614 2020-4-24 15:01:23 | 只看该作者
全局:
4-23
801. Minimum Swaps To Make Sequences Increasing
279. Perfect Squares
139. Word Break
140. Word Break II
688. Knight Probability in Chessboard
377. Combination Sum IV    (518)
1220. Count Vowels Permutation
416. Partition Equal Subset Sum
1230. Toss Strange Coins
494. Target Sum

Today:
1262. Greatest Sum Divisible by Three
      dp[i] the sum that dp[i] %3 ==i;
     base case dp[0] = 0, dp[1] = Integer.MIN_VALUE, dp[2] = Integer.MIN_VALUE
     induction rule dp2[ (j+num)%3] = Math.max(dp[(j+num)%3], dp[j] + num )
1269. Number of Ways to Stay in the Same Place After Some Steps
     dp[i][k] : the number pof ways that stay at ith pos with exact k step
     base case dp[0][0] = 1;
    induction rule: dp[i][k] = dp[i-1][k-1] +dp[i][k-1]+dp[i+1][k-1];
1043. Partition Array for Maximum Sum
      dp[i] the max sum of the array 0,i-1
      dp[0] = 0;
       dp[k] : 左大段右小段,  for  j in [i-1, i-k]  dp[k] = Math.max(dp[j]+count*max, dp[k]);
1046. Last Stone Weight
       method one : use maxheap, each time pop the first two
        method two ; first we use the bucket to get to number of each value
                               count[i], i is the value, count[i] is the count.
                               we start from the end, if count[i] is even, we skip,
                                              else  we get the next value, use that one to cancle the stone.

1049. Last Stone Weight II
         it is same as divide the array to two group, which make diff the minimum
           dp[i][j] whether array[0,i-1] can sum up to j.   0<=j <=sum/2
           base case dp[*][0] = true;
                            dp[i][j] = dp[i-1][j] || (j>= nums[i-1] ? dp[i-1][j-nums[i-1]] : false)
                           get the maximum of j which dp[i][j] = true;

回复

使用道具 举报

🔗
 楼主| Lingzhi..0614 2020-4-27 13:52:46 | 只看该作者
全局:
4-26
525. Contiguous Array(best time to buy and sell stock)
1262. Greatest Sum Divisible by Three
1269. Number of Ways to Stay in the Same Place After Some Steps
1043. Partition Array for Maximum Sum
1046. Last Stone Weight
1049. Last Stone Weight II
688. Knight Probability in Chessboard

Today:
238. Product of Array Except Self
           use two array, one is pre produce, second is suffix product.  res is pre * suffix
678. Valid Parenthesis String
            min : the number of unpaired '(' must be paired
            max: the number of unpaired'(' could be paired
             if (,  min++, max++
              if ), if min >0 , min --, max--;
               else if min >0, min --, max++;     a,b,c  -> a-1, a, a+1, b-1,b, b+1, c-1, c, c+1 ----> a-1, a, b,c,c+1
            return min ==0;
33. Search in Rotated Sorted Array
       we first find the rotated idx, and the do the binary search.
       step 1: find rotated idx, a[mid] > a[right] left = mid+1;
       step 2: realmid = (mid+rotate)%n
81. Search in Rotated Sorted Array II
        we use binary search,
        ifa[mid] == target , return true;
         if first half is sorted,   if a[left] <= target && a[mid} > target, right = mid-1; else left = mid+1
          else if second half is sorted, if a[mid] < target && a[right] >= target, left = mid+1; else right = mid-1;
回复

使用道具 举报

🔗
 楼主| Lingzhi..0614 2020-4-28 13:58:11 | 只看该作者
全局:
4-27
1048. Longest String Chain
96. Unique Binary Search Trees
1105. Filling Bookcase Shelves
89. Gray Code
72. Edit Distance
10. Regular Expression Matching
155. Min Stack
844. Backspace String Compare
576. Out of Boundary Paths
935. Knight Dialer
322. Coin Change
518. Coin Change 2
813. Largest Sum of Averages(1043)(左大段, 右小段)
1278. Palindrome Partitioning III(左大段, 右小段)
238. Product of Array Except Self
678. Valid Parenthesis String
33. Search in Rotated Sorted Array
81. Search in Rotated Sorted Array II

Today:
1335. Minimum Difficulty of a Job Schedule (左大段, 右小段)
        dp[i][j] : the minimum difficulty to arrange job[0,i] into j days;
        base case dp[i][1] = max( job[0,i])
         induction rule : res =  dp[k][j-1] + max{ job[k+1][i] } for( 0<=k<i)
                                 dp[i][j] = Math.min(dp[i][j], res);


410. Split Array Largest Sum(左大段, 右小段)
           method 1: dp[i][m] = the minimum of(largest sum after spliting the array into m subarrays). for array[0,i]
                             dp[i][1] = sum(a[0,i]);
                induction rule:    for j = i; j>=0 && j  >=k-1  ; j--
                                          dp[i][k] = Math.min(dp[i][k],        Math.max(dp[j-1][k-1], cur_max);

           method 2: intuition we need to find a value X that all subarray less than or equal to X, and minimize the X;
                     step1: shrink the range from (-inf, inf) to (Max(array), sum(array))
                     step 2: do binary search (left+right )/2      if F(mid) == true, ans = mid, right = mid-1; else left = mid+1;
                      step 3 : F(x) whether array can be split into m groups with all subarray <=x
                                cur_sum =0; cnt = 1;
                                 for(num : a) if cur_sum + num <= x continue
                                                       else cnt++, cur_sum= num. (means we split the array here)
                                    return cnt<=m? true : false;        
1223. Dice Roll Simulation
        dp[k][j] : the number of valid sequence that end at j, with len== k.
       base case : dp[1][j] = 1;
       induction rule:   dp[k][j] =
                                 case 1 :  k - constrain[j] <=0, dp[k][j] = sum(dp[k-1][1~6])
                                  case 2 : k - constain[j] == 1( e.g xx1 --> 111) dp[k][j] = sum(dp[k-1][1~6] )-1;
                                   case 3: k- constrain[j] >=2,   e.g. axxb           dp[k][j] = sum(dp[k-1][1~6])  - sum(dp[k-1-constrain[j]][1~6(except j));
                                                                                     axx1 ---> should remove 211x, 311x,411x, 511x, 611x.   111x is not include int sum(dp[k-1][1~6])
312. Burst Balloons
      first we expand the array = 1, array, 1
      dp[i][j] the max coin I can get after burst all balloons of A[i,j] not include i, j.
      return dp[0][n-1]
      base case dp[i][i+3-1] = nums[i]*nums[i+1]*nums[i+2];
       induction rule :
                    dp[i][i+len-1]  = Math.max(dp[i][i+len-1],             dp[i][j]+dp[j][i+len-1] + nums[i]*nums[i+len-1]*nums[j]
                                                                                                          for j = i+1; j< i+len-1; j++,         

664. Strange Printer
        dp[i][j] : the min turn to print S[i][j]
        base case dp[i][i] = 1;
                         dp[i][i+1] = s[i] == s[i+1] ? 1 : 2;
         induction rule:
                        dp[i][i+len-1] = len
                        for(j = i; j<i+len-1; j++)
                                res = dp[i][j]+dp[j+1][i+len-1]?
                                  res +=   s[j] == s[i+len-1] ? -1 : 0;
                            dp[i][i+len-1] = Math.min(dp[i][i+len-1] ,res);
回复

使用道具 举报

🔗
 楼主| Lingzhi..0614 2020-4-29 14:35:31 | 只看该作者
全局:
4-28
712. Minimum ASCII Delete Sum for Two Strings
44. Wildcard Matching
97. Interleaving String
1187. Make Array Strictly Increasing
377. Combination Sum IV    (518)
1220. Count Vowels Permutation
416. Partition Equal Subset Sum
1230. Toss Strange Coins
494. Target Sum
525. Contiguous Array(best time to buy and sell stock)
1335. Minimum Difficulty of a Job Schedule (左大段, 右小段·
410. Split Array Largest Sum(左大段, 右小段)
1223. Dice Roll Simulation
312. Burst Balloons
664. Strange Printer

Today
Leftmost Column with at Least a One
         same like the 240. Search a 2D Matrix II,, we start from top right conor
               if it is 1, move left, if it is 0 move down
741. Cherry Pickup
         0,0 -> n-1,n-1 --> 0,0
        change the problem to
           dp(x1,y1,x2,y2) : the max cherry we can get when A (x1,y1) --> 0,0   B(x2,y2)----> 0,0
           use a memo[x1][y1][x2]  to recored the
            base case:
             if(x1 <0 || x2<0 || y1<0 || y2<0 || grid[x1][y1] <0 || grid[x2][y2] <0)   return -1;
             if( x1== && y1 ==0 ) return grid[x1][y1]
             if( memo[x1][y1][x2] != Integer.MIN_Value)  return memo[x1][y1][x2]
             ans = max( dp(x1,y1-1,x2,y2-1), dp(x1,y1-1,x2-1,y2),dp(x1-1,y1,x2,y2-1) ,dp(x1-1,y1,x2-1,y2))
              ans += grid[x1][y1];
                 if(x1 != x2)
                   ans += grid[x2][y2];
1024. Video Stitching
      dp[i][j] the min clip that need to cover [i,j]
      for(len =1; len<=T; LEN++)
          FOR(int i =0; i+len<=T; i++){
             i, j == i, i+len
              for each clip      s= clip[0] e = clip[1]
             case 1 if s>= j || e <=i    dp[i][j] = no exist
             case 2 if s<=i && e >= j dp[i][j] =1
             case 3        else if e >=j   dp[i][j] = Math.min(dp[i][j], 1+ dp[i][s]);
             case 4        else if s<=i   dp[i][j] = Math.min(dp[i][j], 1+ dp[e][j]);
                    else    p[i][j] = Math.min(dp[i][j], 1+ dp[e][j] + dp[i][s]);
回复

使用道具 举报

🔗
 楼主| Lingzhi..0614 2020-4-30 14:35:23 | 只看该作者
全局:
4-29
1143. Longest Common Subsequence
115. Distinct Subsequences
1092. Shortest Common Supersequence
583. Delete Operation for Two Strings
1262. Greatest Sum Divisible by Three
1269. Number of Ways to Stay in the Same Place After Some Steps
1043. Partition Array for Maximum Sum (左大段, 右小段)
1046. Last Stone Weight
1049. Last Stone Weight II
238. Product of Array Except Self
678. Valid Parenthesis String
33. Search in Rotated Sorted Array
81. Search in Rotated Sorted Array II
Leftmost Column with at Least a One
741. Cherry Pickup
1024. Video Stitching

Today
201. Bitwise AND of Numbers Range
         it is same as find the same prefix for all integer in [m,n].
          method 1: each time we move m>>1 n>>1, until n == m, we find the prefix, we leftremove x time.
          method 2: n&(n-1)  will remove the right most 1 for n, while m < n , -----> find the prefix, we we cut off the right most 1 for n, until n <=m, we return m&n

146. LRU Cache
          use hashmap, double linkedlist
           get:   1 check the map, in not contains return -1.
                    2 exists, we get the value, and remove, and append( to udate it to the newest)
            put:  1. check if map.contains(key), update the value, remove, append
                     2. if not contains    2.1 size ()< limit, we just add to map, append
                                                    2.2  size() == limit, remove the oldest, remove in map, add to map , append.
55. Jump Game
       method: dp[i] whether we can jump to the len-1
                    base case dp[n-1] = true;
                                     dp[i] = dp[i] | dp[i+j]    where 0<j<= a[i]
                              time(O(n^2)
                method 2: O(n)
                           last : the left most idx that can reach the n-1
                            last = n-1;
                             for each i , if (i+a[i] >= last)
                                           last = i;
                           return last == 0;
1039. Minimum Score Triangulation of Polygon
                     dp[i][j] the minscore for A[i]~A[j]
                       base case: dp[i][i] =0; dp[i][i+1] = 0;
                          induction rule dp[i][j] = Math.min(dp[i][j],  dp[i][k]+ dp[k][j] + nums[i] * nums[j] * nums[k])    i+1<=k <j
503. Next Greater Element II
                      method we iterative the array twice to make it circle
                                     idx in stack means we have not find the next greater yet
                               for each i
                                       while( !stack.isEmpty() && A[stack.peekFirst()] < A[i]
                                                   A[stack.peekFirst()] = A[i]
                                         stack.offerFirst(i);
1130. Minimum Cost Tree From Leaf Values
                     dp[i][j] : the min cost to merge A[i] ~A[j]
                       base case: dp[i][i] = 0;
                         induction rule : dp[i][j] = Math.min(dp[i][j], dp[i][k] + dp[k+1][j] + Max(A[i]~ A[k]) * MAX(A[k+1] ~A[j]);
                     

                     method 2: O(n)
                          intuition each time we combine a,b, we delete the smaller one with cost a*b. inorder to min a*b , b= min(first bigger word for [left,a-1], [a+1,right]
                          rephrase: give an array, the minimum cost to delete the whole array until one.
                          we use stack.
                                  for each a
                                          while(stack.peekFirst()<= a)
                                                 we find the local minimum, we get the left first greater, and this a(right first greater) res += x * min(left, right);
                                            stack.offerFirst(a);
                           while(stack.size() >2)  e.g   8,6,4,2
                                       res+=stack.pollFirst()  *stack.peekFirst();
回复

使用道具 举报

🔗
 楼主| Lingzhi..0614 2020-5-1 14:36:12 | 只看该作者
全局:
1139. Largest 1-Bordered Square
718. Maximum Length of Repeated Subarray
813. Largest Sum of Averages(1043)(左大段, 右小段)
1278. Palindrome Partitioning III(左大段, 右小段)
1335. Minimum Difficulty of a Job Schedule (左大段, 右小段·
410. Split Array Largest Sum(左大段, 右小段)
1223. Dice Roll Simulation
312. Burst Balloons (1039)
664. Strange Printer
201. Bitwise AND of Numbers Range
146. LRU Cache
55. Jump Game
1039. Minimum Score Triangulation of Polygon
503. Next Greater Element II
1130. Minimum Cost Tree From Leaf Values

Today
1140. Stone Game II
                 recursive with memp  helper(piles, i, m) means the max number the player can get with pile[i : ] and M =m
                    base case: i == length , return 0;
                                      or len- i <= 2*m , return the sum from i.  means the player can take all
                                       or memo[i][m] != 0, return memp[i][m]
                     recursive rule:
                                           for k = 1; k<=2*m
                                     memp[i][m] = Math.max(memo[i][m], sum(a[i] ~ a[n]) -  min(helper(piles, i+k, Math.max(k, m)));
546. Remove Boxes
          dp[i][j][k] : the max points for array[i, j] with k same value as array[j] form array[j+1] ~ array[n]
          base if i> j, return 0
         
         
           case 1 : dp[i][j][k] : helper([i][j-1][0] +(k+1)(k+1)         --------------------> we directly add the j to the right
           case 2 : for each  m in [i,j), if array[m] == array[j]
                         dp[i][j][k] = Math.max( dp[i][j][k], helper[i][m][k+1] + helper[m+1][j-1][0])
回复

使用道具 举报

🔗
 楼主| Lingzhi..0614 2020-5-5 14:20:36 | 只看该作者
全局:
5-4
688. Knight Probability in Chessboard
1428 Leftmost Column with at Least a One
741. Cherry Pickup
1024. Video Stitching
201. Bitwise AND of Numbers Range
146. LRU Cache
55. Jump Game
1039. Minimum Score Triangulation of Polygon
503. Next Greater Element II
1130. Minimum Cost Tree From Leaf Values
1140. Stone Game II
546. Remove Boxes
980. Unique Paths III
943. Find the Shortest Superstring

Today
996. Number of Squareful Arrays
      1. first get the count and map of valid squareful array,
       2. use dfs to try to put in array
            how many level:  array.length
            how many states: for each position, if we still have count, we put the number here, and then go to put squareful num in next position
1125. Smallest Sufficient Team
             dp[i][j] the min number of people that can reach j state with first i people
              dp[0][0] = 0;
               induction rule:
                dp[i][j|k] = min(dp[i-1][j|k] , dp[i-1][j] +1) use i ith or not use ith people,  k is the state for ith people
             1. need to calculate the bitmask for a people's skill
             2. List[] path = new List[1<<n], used to recored the previous state, and end i.             path[t] = {j, i}  j--> take ith people ---> i (j|k )
285. Inorder Successor in BST
         if root.val <= p.val , root = root.right
         if root.val > p.val , res = root; root = root.left;
1009. Complement of Base 10 Integer
          n ^ 1111 ---> complement,
         method 1 to get 1111,  calculate the len of N, and use 1<<n -1;     int len = (int)(Math.log(N)/ Math.log(2)) +1
          method 2: use bitmask to right shift 1, bitmask = n,     
    bitmask |= (bitmask >> 1);
    bitmask |= (bitmask >> 2);
    bitmask |= (bitmask >> 4);
    bitmask |= (bitmask >> 8);
    bitmask |= (bitmask >> 16);
510. Inorder Successor in BST II
      if node.right != null, res = node.right, then go as left as possible
       if node.right == null, go to parent until cur.val > node.val
       else return null;
回复

使用道具 举报

🔗
 楼主| Lingzhi..0614 2020-5-6 14:30:06 | 只看该作者
全局:
5-5
reviewed:
155. Min Stack
844. Backspace String Compare
576. Out of Boundary Paths
935. Knight Dialer
322. Coin Change
518. Coin Change 2
201. Bitwise AND of Numbers Range
146. LRU Cache
55. Jump Game
1039. Minimum Score Triangulation of Polygon
503. Next Greater Element II
1130. Minimum Cost Tree From Leaf Values
996. Number of Squareful Arrays
1125. Smallest Sufficient Team
285. Inorder Successor in BST
1009. Complement of Base 10 Integer
510. Inorder Successor in BST II

Today:
finished daily challeng for May in leet code
160. Intersection of Two Linked Lists
       key thought : m+n , we do two iteration.
387. First Unique Character in a String
          hashmap.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表