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

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

全局:
马一个
争取9月开始
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-6 08:50:10 | 只看该作者
全局:
621. Task Scheduler -- greedy
  1. class Solution {
  2.     public int leastInterval(char[] tasks, int n) {
  3.         if (tasks == null || tasks.length == 0) {
  4.             return 0;
  5.         }
  6.         int[] map = new int[26];
  7.         for (char c : tasks) {
  8.             map[c - 'A']++;
  9.         }
  10.         PriorityQueue<Integer> queue = new PriorityQueue<>(26, (a, b) -> b - a);
  11.         for (int num : map) {
  12.             if (num != 0) {
  13.                 queue.add(num);
  14.             }
  15.         }
  16.         int times = 0;
  17.         while(!queue.isEmpty()) {
  18.             int count = 0;
  19.             List<Integer> buffer = new ArrayList<>();
  20.             while(count <= n) {
  21.                 if(!queue.isEmpty()) {
  22.                     if (queue.peek() > 1) {
  23.                         buffer.add(queue.poll() - 1);
  24.                     } else {
  25.                         queue.poll();
  26.                     }                        
  27.                 }
  28.                 times++;
  29.                 if(queue.isEmpty() && buffer.size() == 0) {
  30.                     break;
  31.                 }
  32.                 count++;
  33.             }
  34.             for (int num : buffer) {
  35.                 queue.add(num);
  36.             }
  37.         }
  38.         return times;
  39.     }
  40. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-6 10:03:14 | 只看该作者
全局:
358. Rearrange String k Distance Apart --greedy

  1. class Solution {
  2.     public String rearrangeString(String s, int k) {
  3.         if (s == null || s.length() == 0 || k <= 0 ) {
  4.             return s;
  5.         }
  6.         StringBuilder sb = new StringBuilder();
  7.         char[] array = s.toCharArray();
  8.         Map<Character, Integer> map = new HashMap<>();
  9.         for (char c : array) {
  10.             map.put(c, map.getOrDefault(c, 0) + 1);
  11.         }
  12.         PriorityQueue<Map.Entry<Character, Integer>> queue = new PriorityQueue<>((a,b) -> {
  13.             return a.getValue() == b.getValue() ? a.getKey().compareTo(b.getKey()) : b.getValue() - a.getValue();
  14.         });
  15.         for (Map.Entry<Character, Integer> entry : map.entrySet()) {
  16.             queue.add(entry);
  17.         }
  18.         Queue<Map.Entry<Character, Integer>> buffer = new LinkedList<>();
  19.         while(!queue.isEmpty()) {
  20.             Map.Entry<Character, Integer> entry = queue.poll();
  21.             sb.append(entry.getKey());
  22.             entry.setValue(entry.getValue() - 1);
  23.             buffer.add(entry); // if the value is zero, still need to be in the queue as place holder
  24.             if (buffer.size() < k) {
  25.                 continue;
  26.             }
  27.             Map.Entry<Character, Integer> unit = buffer.poll();
  28.             if (unit.getValue() > 0) {
  29.                 queue.add(unit);
  30.             }
  31.             
  32.         }
  33.         return sb.length() == s.length() ? sb.toString() : "";
  34.     }
  35. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-7 09:00:58 | 只看该作者
全局:
767. Reorganize String -- priorityQueue greddy
  1. class Solution {
  2.     public String reorganizeString(String S) {
  3.         if (S == null || S.length() <= 1) {
  4.             return S;
  5.         }
  6.         char[] array = S.toCharArray();
  7.         Map<Character, Integer> map = new HashMap<>();
  8.         PriorityQueue<Map.Entry<Character, Integer>> queue = new PriorityQueue<>((a, b) ->
  9.             a.getValue() == b.getValue() ? a.getKey().compareTo(b.getKey()) : b.getValue() - a.getValue());
  10.         for (char c : array) {
  11.             map.put(c, map.getOrDefault(c, 0) + 1);
  12.         }
  13.         
  14.         for (Map.Entry<Character, Integer> entry : map.entrySet()) {
  15.             queue.add(entry);
  16.         }
  17.         StringBuilder sb = new StringBuilder();
  18.         Queue<Map.Entry<Character, Integer>> buffer = new LinkedList<>();
  19.         while(!queue.isEmpty()) {
  20.             Map.Entry<Character, Integer> cur = queue.poll();
  21.             sb.append(cur.getKey());
  22.             cur.setValue(cur.getValue() - 1);
  23.             buffer.offer(cur);
  24.             if (buffer.size() < 2) {
  25.                 continue;
  26.             }
  27.             while(!buffer.isEmpty()) {
  28.                Map.Entry<Character, Integer> curEntry = buffer.poll();
  29.                 if (curEntry.getValue() > 0) {
  30.                     queue.add(curEntry);
  31.                 }
  32.             }
  33.         }
  34.         
  35.         return sb.length() == S.length() ? sb.toString() : "";
  36.     }
  37. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-7 09:42:51 | 只看该作者
全局:
251. Flatten 2D Vector

  1. class Vector2D {
  2.     int row = 0;
  3.     int col = 0;
  4.     int[][] v;
  5.     int next = 0;
  6.    
  7.     public Vector2D(int[][] v) {
  8.         this.v = v;
  9.     }
  10.    
  11.     public int next() {
  12.         if (getNext()) {
  13.             col++;
  14.         }
  15.         return next;
  16.     }
  17.    
  18.     public boolean hasNext() {
  19.         if (getNext())
  20.             return true;
  21.         return false;
  22.     }
  23.    
  24.     private boolean getNext() {
  25.         while (row < v.length) {
  26.             int[] curRow = v[row];
  27.             if (curRow.length == 0) {
  28.                 row++;
  29.                 continue;
  30.             }
  31.             
  32.             if (col >= curRow.length) {
  33.                 col = 0;
  34.                 row++;
  35.                 continue;
  36.             }
  37.             next = curRow[col];
  38.             return true;
  39.         }
  40.         return false;
  41.     }
  42. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-8 22:23:45 | 只看该作者
全局:
993. Cousins in Binary Tree -- my code  is aguly!!!

  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.     int depth;
  12.     int xP;
  13.     int yP;
  14.     boolean ans = true;
  15.     public boolean isCousins(TreeNode root, int x, int y) {
  16.         if (root == null) {
  17.             return false;
  18.         }
  19.         if (root.val == x || root.val == y) {
  20.             return false;
  21.         }
  22.         travel(root, x, y, 0);
  23.         if (xP == yP) {
  24.             ans = false && ans;
  25.         }
  26.         return ans;
  27.     }
  28.     private void travel(TreeNode root, int x, int y, int depth) {
  29.         if (root == null) {
  30.             return;
  31.         }
  32.         if (root.val == x || root.val == y) {
  33.             if (this.depth == 0) {
  34.                 this.depth = depth;
  35.             }else {
  36.                 if (this.depth != depth) {
  37.                     this.ans = false;
  38.                 }
  39.             }
  40.         }
  41.         if (root.left != null) {
  42.             if (root.left.val == x) {
  43.                 this.xP = root.val;
  44.             }
  45.             if (root.left.val == y) {
  46.                 this.yP = root.val;
  47.             }
  48.         }
  49.         if (root.right != null) {
  50.             if (root.right.val == x) {
  51.                 this.xP = root.val;
  52.             }
  53.             if (root.right.val == y) {
  54.                 this.yP = root.val;
  55.             }
  56.         }
  57.         travel(root.left, x, y, depth + 1);
  58.         travel(root.right, x, y, depth + 1);
  59.     }
  60.    
  61.    
  62. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-8 23:28:40 | 只看该作者
全局:
994. Rotting Oranges --think about a clean way to handle a
  1. class Solution {
  2.     public int orangesRotting(int[][] grid) {
  3.         if (grid == null || grid.length == 0) {
  4.             return -1;
  5.         }
  6.         int ans = -1;
  7.         Queue<int[]> queue = new LinkedList<>();
  8.         for (int i = 0; i < grid.length; i++) {
  9.             for (int j = 0; j < grid[0].length; j++) {
  10.                 if (grid[i][j] == 2) {
  11.                     queue.offer(new int[]{i, j});
  12.                 }
  13.             }
  14.         }
  15.         if (queue.size() == 0) {
  16.             for (int i = 0; i < grid.length; i++) {
  17.                 for (int j = 0; j < grid[0].length; j++) {
  18.                     if (grid[i][j] == 1) {
  19.                         return -1;
  20.                     }
  21.                 }
  22.             }
  23.             return 0;
  24.         }
  25.         //starting BFS;
  26.         int[][] directions = new int[][]{{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
  27.         while(!queue.isEmpty()) {
  28.             int size = queue.size();
  29.             for (int i = 0; i < size; i++) {
  30.                 int[] cur = queue.poll();
  31.                 for (int[] dir : directions) {
  32.                     int newRow = cur[0] + dir[0];
  33.                     int newCol = cur[1] + dir[1];
  34.                     if (validePosition(newRow, newCol, grid)) {
  35.                         grid[newRow][newCol] = 2;
  36.                         queue.offer(new int[]{newRow, newCol});
  37.                     }
  38.                 }
  39.             }
  40.             ans++;
  41.         }
  42.         for (int i = 0; i < grid.length; i++) {
  43.             for (int j = 0; j < grid[0].length; j++) {
  44.                 if (grid[i][j] == 1) {
  45.                     return -1;
  46.                 }
  47.             }
  48.         }
  49.         return ans;
  50.     }
  51.    
  52.     private boolean validePosition(int row, int col, int[][] grid) {
  53.         return row >= 0 && row < grid.length && col >= 0 && col < grid[0].length && grid[row][col] == 1;
  54.     }
  55. }
复制代码
corner case
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-9 05:11:30 | 只看该作者
全局:
281. Zigzag Iterator -try to write less and clean code to solve this
  1. public class ZigzagIterator {
  2.     private int curV1;
  3.     private int curV2;
  4.     private List<Integer> v1;
  5.     private List<Integer> v2;
  6.     private boolean isV1;
  7.     public ZigzagIterator(List<Integer> v1, List<Integer> v2) {
  8.         this.v1 = v1;
  9.         this.v2 = v2;
  10.         this.curV1 = 0;
  11.         this.curV2 = 0;
  12.         this.isV1 = true;
  13.     }

  14.     public int next() {
  15.         if (!hasNext()) {
  16.             return -1;
  17.         }
  18.         int ans = 0;
  19.         if (isV1 && curV1 < v1.size()) {
  20.             ans = v1.get(curV1);
  21.             curV1++;
  22.         }else if (!isV1 && curV2 < v2.size()) {
  23.             ans = v2.get(curV2);
  24.             curV2++;
  25.         }else if (curV1 >= v1.size()) {
  26.             ans = v2.get(curV2);
  27.             curV2++;
  28.         }else {
  29.             ans = v1.get(curV1);
  30.             curV1++;
  31.         }
  32.         isV1 = !isV1;
  33.         return ans;
  34.     }

  35.     public boolean hasNext() {
  36.         return curV1 < v1.size() || curV2 < v2.size();
  37.     }
  38. }

  39. /**
  40. * Your ZigzagIterator object will be instantiated and called as such:
  41. * ZigzagIterator i = new ZigzagIterator(v1, v2);
  42. * while (i.hasNext()) v[f()] = i.next();
  43. */
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-9 08:19:20 | 只看该作者
全局:
987. Vertical Order Traversal of a Binary Tree -- posting answer, watch out for the logic inside compare method

  1. class Location implements Comparable<Location>{
  2.     int x, y, val;
  3.     Location(int x, int y, int val) {
  4.         this.x = x;
  5.         this.y = y;
  6.         this.val = val;
  7.     }

  8.     @Override
  9.     public int compareTo(Location that) {
  10.         if (this.x != that.x)
  11.             return Integer.compare(this.x, that.x);
  12.         else if (this.y != that.y)
  13.             return Integer.compare(this.y, that.y);
  14.         else
  15.             return Integer.compare(this.val, that.val);
  16.     }
  17. }

  18. class Solution {
  19.     List<Location> locations;
  20.     public List<List<Integer>> verticalTraversal(TreeNode root) {
  21.         // Each location is a node's x position, y position, and value
  22.         locations = new ArrayList();
  23.         dfs(root, 0, 0);
  24.         Collections.sort(locations);

  25.         List<List<Integer>> ans = new ArrayList();
  26.         ans.add(new ArrayList<Integer>());

  27.         int prev = locations.get(0).x;

  28.         for (Location loc: locations) {
  29.             // If the x value changed, it's part of a new report.
  30.             if (loc.x != prev) {
  31.                 prev = loc.x;
  32.                 ans.add(new ArrayList<Integer>());
  33.             }

  34.             // We always add the node's value to the latest report.
  35.             ans.get(ans.size() - 1).add(loc.val);
  36.         }

  37.         return ans;
  38.     }

  39.     public void dfs(TreeNode node, int x, int y) {
  40.         if (node != null) {
  41.             locations.add(new Location(x, y, node.val));
  42.             dfs(node.left, x-1, y+1);
  43.             dfs(node.right, x+1, y+1);
  44.         }
  45.     }
  46. }

复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-9 09:50:05 | 只看该作者
全局:
42. Trapping Rain Water -- start from brute force and then optimize the complexity
  1. class Solution {
  2.     public int trap(int[] height) {
  3.         if (height == null || height.length == 0) {
  4.             return 0;
  5.         }
  6.         int left = 0;
  7.         int right = height.length - 1;
  8.         int leftMax = 0;
  9.         int rightMax = 0;
  10.         int ans = 0;
  11.         while(left < right) {
  12.             if (height[left] < height[right]) {
  13.                 if (height[left] < leftMax) {
  14.                    ans += leftMax - height[left];
  15.                 }else {
  16.                    leftMax = height[left];
  17.                 }
  18.                 left++;
  19.             }else {
  20.                 if (height[right] < rightMax) {
  21.                    ans += rightMax - height[right];
  22.                 }else {
  23.                    rightMax = height[right];
  24.                 }
  25.                 right--;
  26.             }
  27.         }
  28.         return ans;
  29.     }
  30. }
复制代码
回复

使用道具 举报

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

本版积分规则

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