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

多伦多在职每日刷题打卡也欢迎组队

🔗
 楼主| zhangrz2 2019-6-15 09:18:26 | 只看该作者
全局:
909. Snakes and Ladders -- tricky: handle position mapping

  1. class Solution {
  2.     public int snakesAndLadders(int[][] board) {
  3.         int len = board.length;
  4.         Queue<Integer> curS = new LinkedList<>();
  5.         curS.offer(1);
  6.         Map<Integer, Integer> steps = new HashMap<>();
  7.         steps.put(1, 0);
  8.         while(!curS.isEmpty()) {
  9.             int cur = curS.poll();
  10.             if (cur == len * len) {
  11.                 return steps.get(cur);
  12.             }
  13.             for (int i = cur + 1; i <= Math.min(cur + 6, len * len); i++) {
  14.                 int[] pos = getPosition(i, len);
  15.                 int row = pos[0];
  16.                 int col = pos[1];
  17.                 int next = board[row][col] == -1 ? i : board[row][col];
  18.                 if (!steps.containsKey(next)) {
  19.                     steps.put(next, steps.get(cur) + 1);
  20.                     curS.offer(next);
  21.                 }
  22.             }
  23.         }
  24.         return -1;
  25.         
  26.     }
  27.    
  28.     private int[] getPosition(int cur, int len) {
  29.         int row = len - 1 - (cur - 1) / len;
  30.         int col = 0;
  31.         if (len % 2 == 0) {
  32.            if (row % 2 == 1) {
  33.             col = (cur - 1) % len;   
  34.             }else {
  35.              col = len - 1 - (cur - 1) % len;
  36.             }
  37.         }else {
  38.             if (row % 2 == 0) {
  39.             col = (cur - 1) % len;   
  40.             }else {
  41.              col = len - 1 - (cur - 1) % len;
  42.             }
  43.         }
  44.         
  45.         return new int[]{row, col};
  46.     }
  47. }
复制代码
回复

使用道具 举报

🔗
xiongml81 2019-6-15 09:29:02 | 只看该作者
全局:
nice and keep going
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-15 10:03:11 | 只看该作者
全局:
122. Best Time to Buy and Sell Stock II -- greedy

  1. class Solution {
  2.     public int maxProfit(int[] prices) {
  3.         if (prices == null || prices.length <= 1) {
  4.             return 0;
  5.         }
  6.         int max = 0;
  7.         for (int i = 1; i < prices.length; i++) {
  8.             if (prices[i] >= prices[i - 1]) {
  9.                 max += prices[i] - prices[i - 1];
  10.             }
  11.         }
  12.         return max;
  13.     }
  14. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-15 23:58:39 | 只看该作者
全局:
545. Boundary of Binary Tree -- 自己写的总是过不了,还是答案写的clean

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. *     int val;
  5. *     TreeNode left;
  6. *     TreeNode right;
  7. *     TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11.     //0 = root, 1 = left, 2 = right, 3 = middle
  12.     public List<Integer> boundaryOfBinaryTree(TreeNode root) {
  13.         List<Integer> left = new ArrayList<>();
  14.         List<Integer> leaf = new ArrayList<>();
  15.         List<Integer> right = new ArrayList<>();
  16.         dfs(root, left, right, leaf, 0);
  17.         left.addAll(leaf);
  18.         left.addAll(right);
  19.         return left;
  20.     }
  21.    
  22.     private void dfs(TreeNode root, List<Integer> left, List<Integer> right, List<Integer> leaf, int flag) {
  23.         if (root == null) {
  24.             return;
  25.         }
  26.         if (isLeft(flag) || isRoot(flag)) {
  27.             left.add(root.val);
  28.         }else if (isRight(flag)) {
  29.             right.add(0, root.val);
  30.         }else if (isLeaf(root)) {
  31.             leaf.add(root.val);
  32.         }
  33.         dfs(root.left, left, right, leaf, getLeftFlag(root, flag));
  34.         dfs(root.right, left, right, leaf, getRightFlag(root, flag));
  35.         return;
  36.     }
  37.    
  38.     private int getLeftFlag(TreeNode root, int flag) {
  39.         if (isLeft(flag) || isRoot(flag)) {
  40.             return 1;
  41.         }else if (isRight(flag) && root.right == null) {
  42.             return 2;
  43.         }
  44.         return 3;
  45.     }
  46.    
  47.     private int getRightFlag(TreeNode root, int flag) {
  48.         if (isRight(flag) || isRoot(flag)) {
  49.             return 2;
  50.         }else if (isLeft(flag) && root.left == null) {
  51.             return 1;
  52.         }
  53.         return 3;
  54.     }
  55.    
  56.     private boolean isLeft(int flag) {
  57.         return flag == 1;
  58.     }
  59.    
  60.     private boolean isRight(int flag) {
  61.         return flag == 2;
  62.     }
  63.    
  64.     private boolean isRoot(int flag) {
  65.         return flag == 0;
  66.     }
  67.    
  68.     private boolean isLeaf(TreeNode root) {
  69.         return root.left == null && root.right == null;
  70.     }
  71. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-16 07:38:52 | 只看该作者
全局:
957. Prison Cells After N Days -- simulation 14 is a cycle

  1. class Solution {
  2.     //14 days one cycle
  3.     public int[] prisonAfterNDays(int[] cells, int N) {
  4.         int numOfIt = (N - 1) % 14 + 1;
  5.         for (int i = 0; i < numOfIt; i++) {
  6.             int temp = cells[0];
  7.             for (int j = 1; j < cells.length - 1; j++) {
  8.                 int val = temp == cells[j + 1] ? 1: 0;
  9.                 temp = cells[j];
  10.                 cells[j] = val;
  11.             }
  12.             if (i == 0) {
  13.                 cells[0] = 0;
  14.                 cells[cells.length - 1] = 0;
  15.             }
  16.         }
  17.         return cells;
  18.     }
  19. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-16 08:09:53 | 只看该作者
全局:
572. Subtree of Another Tree
  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. *     int val;
  5. *     TreeNode left;
  6. *     TreeNode right;
  7. *     TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11.     private boolean ans;
  12.     //DFS on tree s and then find the
  13.     public boolean isSubtree(TreeNode s, TreeNode t) {
  14.         if (s == null && t != null || t == null && s != null) {
  15.             return false;
  16.         }
  17.         isSubTree(s, t);
  18.         return ans;
  19.     }
  20.    
  21.     private void isSubTree(TreeNode s, TreeNode t) {
  22.         if (s == null || ans) {
  23.             return;
  24.         }
  25.         if (s.val == t.val) {
  26.             ans = checkIsSubTree(s, t);
  27.         }
  28.         isSubTree(s.left, t);
  29.         isSubTree(s.right, t);
  30.         
  31.         return;
  32.     }
  33.    
  34.     private boolean checkIsSubTree(TreeNode s, TreeNode t) {
  35.         if (s == null && t != null || t == null && s != null) {
  36.             return false;
  37.         }
  38.         if (s == null && t == null) {
  39.             return true;
  40.         }
  41.         return s.val == t.val && checkIsSubTree(s.left, t.left) && checkIsSubTree(s.right, t.right);
  42.     }
  43. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-17 00:39:33 | 只看该作者
全局:
694. Number of Distinct Islands -- hardest part is how to mark each shape
  1. class Solution {
  2.     //using DFS to find inland,
  3.     //how to translate to mark as unique
  4.     //mark the path to count number of distinct
  5.     private Set<List<Integer>> path;
  6.     public int numDistinctIslands(int[][] grid) {
  7.         path = new HashSet<List<Integer>>();
  8.         for (int i = 0; i < grid.length; i++) {
  9.             for (int j = 0; j < grid[0].length; j++) {
  10.                 if (grid[i][j] == 1) {
  11.                     List<Integer> shape = new ArrayList<>();
  12.                     explore(grid, 0, shape, i, j);
  13.                     if (!shape.isEmpty()) {
  14.                         path.add(shape);
  15.                     }
  16.                 }
  17.             }
  18.         }
  19.         
  20.         return path.size();
  21.     }
  22.    
  23.     private void explore(int[][] grid, int dis, List<Integer> shape, int row, int col) {
  24.         if (row >= 0 && col >= 0 && row < grid.length && col < grid[0].length && grid[row][col] == 1) {
  25.             grid[row][col] = 0;
  26.             shape.add(dis);
  27.             explore(grid, 1, shape, row + 1, col);
  28.             explore(grid, 2, shape, row - 1, col);
  29.             explore(grid, 3, shape, row, col + 1);
  30.             explore(grid, 4, shape, row, col - 1);
  31.             shape.add(0);// place holder to distinct the shape that can be transformed
  32.         }
  33.         return;
  34.     }
  35.    
  36. }
复制代码
[/i]
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-17 02:39:26 | 只看该作者
全局:
508. Most Frequent Subtree Sum -- dfs and hashmap
  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. *     int val;
  5. *     TreeNode left;
  6. *     TreeNode right;
  7. *     TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11.     private int maxFre = 0;
  12.     public int[] findFrequentTreeSum(TreeNode root) {
  13.         if (root == null) {
  14.             return new int[0];
  15.         }
  16.         Map<Integer, Integer> map = new HashMap<>();
  17.         buildMap(root, map);
  18.         List<Integer> ansList = new ArrayList<>();
  19.         for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
  20.             if (entry.getValue() == maxFre) {
  21.                 ansList.add(entry.getKey());
  22.             }
  23.         }
  24.         int[] ans =  ansList.stream().mapToInt(Integer::intValue).toArray();;

  25.         return ans;
  26.     }
  27.    
  28.     private int buildMap(TreeNode root, Map<Integer, Integer> map) {
  29.         if (root == null) {
  30.             return 0;
  31.         }
  32.         int sum = root.val + buildMap(root.left, map) + buildMap(root.right, map);
  33.         this.maxFre = Math.max(maxFre, map.getOrDefault(sum, 0) + 1);
  34.         map.put(sum, map.getOrDefault(sum, 0) + 1);
  35.         return sum;
  36.     }
  37. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-17 08:59:12 来自APP | 只看该作者
全局:
这周末有点不舒服,拉下了点进度,留个记录,以后多刷点题补回来
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-18 09:32:35 | 只看该作者
全局:
139. Word Break

  1. class Solution {
  2.     //dynamic programming, break the problem into subproblems
  3.     public boolean wordBreak(String s, List<String> wordDict) {
  4.         Set<String> wordList = new HashSet<>(wordDict);
  5.         boolean[] dp = new boolean[s.length() + 1];
  6.         dp[0] = true;
  7.         for (int i = 1; i <= s.length(); i++) {
  8.             for (int j = 0; j < i; j++) {
  9.                 if (dp[j] && wordList.contains(s.substring(j, i))) {
  10.                     dp[i] = true;
  11.                 }
  12.             }
  13.         }
  14.         return dp[s.length()];
  15.     }
  16. }
复制代码
回复

使用道具 举报

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

本版积分规则

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