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

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

🔗
 楼主| zhangrz2 2019-6-3 04:18:20 | 只看该作者
全局:
  1. class Node {
  2.         public Node prev, next;
  3.         public final int count;
  4.         public LinkedHashSet<Integer> keys = new LinkedHashSet<>();

  5.         public Node(Node prev, Node next, int count, int key) {
  6.             this.prev = prev;
  7.             this.next = next;
  8.             this.count = count;
  9.             keys.add(key);
  10.         }
  11. }

  12. class LFUCache {
  13.     Node head = null;
  14.     final int capacity;
  15.     Map<Integer, Integer> valueMap = new HashMap<>();
  16.     Map<Integer, Node> nodeMap = new HashMap<>();

  17.     public LFUCache(int capacity) {
  18.         this.capacity = capacity;
  19.     }

  20.     public int get(int key) {
  21.         if (nodeMap.containsKey(key)) increase(key);
  22.         return valueMap.getOrDefault(key, -1);
  23.     }

  24.     private void increase(int key) {
  25.         Node node = nodeMap.get(key);
  26.         node.keys.remove(key);
  27.         if (Objects.isNull(node.next)) node.next = new Node(node, null, node.count + 1, key);
  28.         else if (node.next.count == node.count + 1) node.next.keys.add(key);
  29.         else node.next = node.next.prev = new Node(node, node.next, node.count + 1, key);
  30.         nodeMap.put(key, node.next);
  31.         if (node.keys.isEmpty()) remove(node);
  32.     }

  33.     private void remove(Node node) {
  34.         if (head == node) head = node.next;
  35.         else node.prev.next = node.next;
  36.         if (Objects.nonNull(node.next)) node.next.prev = node.prev;
  37.     }

  38.     public void put(int key, int value) {
  39.         if (0 == this.capacity) return;
  40.         if (Objects.nonNull(valueMap.put(key, value))) increase(key);
  41.         else {
  42.             if (nodeMap.size() == this.capacity) remove();
  43.             add(key);
  44.         }
  45.     }

  46.     private void add(int key) {
  47.         if (Objects.isNull(head)) head = new Node(null, null, 1, key);
  48.         else if (head.count == 1) head.keys.add(key);
  49.         else {
  50.             Node node = new Node(null, head, 1, key);
  51.             head.prev = node;
  52.             head = node;
  53.         }
  54.         nodeMap.put(key, head);
  55.     }

  56.     private void remove() {
  57.         int oldest = head.keys.iterator().next();
  58.         head.keys.remove(oldest);
  59.         if (head.keys.isEmpty()) remove(head);
  60.         nodeMap.remove(oldest);
  61.         valueMap.remove(oldest);
  62.     }
  63. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-3 04:50:26 | 只看该作者
全局:
819. Most Common Word -- 感觉就是靠基础,不知道亚麻为啥出这样的题。。。

  1. class Solution {
  2.     public String mostCommonWord(String paragraph, String[] banned) {
  3.         if (paragraph == null || paragraph.length() == 0) {
  4.             return "";
  5.         }
  6.         paragraph = paragraph.replaceAll("[\\!+|\\?+|\\'+|\\,+|\\;+|\\.+]", " ");
  7.         paragraph = paragraph.toLowerCase();
  8.         String[] array = paragraph.split("\\s+");
  9.         Set<String> bans = new HashSet<>(Arrays.asList(banned));
  10.         Map<String, Integer> count = new HashMap<>();
  11.         int maxCount = 0;
  12.         String ans = "";
  13.         for (String str : array) {
  14.             if (!bans.contains(str)) {
  15.                 count.put(str, count.getOrDefault(str, 0) + 1);
  16.                 if (count.get(str) > maxCount) {
  17.                     ans = str;
  18.                     maxCount = count.get(str);
  19.                 }
  20.             }
  21.         }
  22.         return ans;
  23.     }
  24. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-3 08:13:38 | 只看该作者
全局:
143. Reorder List
  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. *     int val;
  5. *     ListNode next;
  6. *     ListNode(int x) { val = x; }
  7. * }
  8. */
  9. class Solution {
  10.     public void reorderList(ListNode head) {
  11.         if (head == null || head.next == null) {
  12.             return;
  13.         }
  14.         //find the secode half;
  15.         ListNode prev = null;
  16.         ListNode slow = head;
  17.         ListNode fast = head;
  18.         while(fast != null && fast.next != null) {
  19.             prev = slow;
  20.             slow = slow.next;
  21.             fast = fast.next.next;
  22.         }
  23.         if (fast != null) { //odd node
  24.             prev = slow;
  25.             slow = slow.next;
  26.         }
  27.         System.out.println(slow.val);
  28.         prev.next = null;
  29.         ListNode secHead = reverse(slow);
  30.         fast = head;
  31.         while(fast != null && secHead != null) {
  32.             ListNode fastNext = fast.next;
  33.             ListNode slowNext = secHead.next;
  34.             fast.next = secHead;
  35.             secHead.next = fastNext;
  36.             fast = fastNext;
  37.             secHead = slowNext;
  38.         }
  39.         return;
  40.     }
  41.    
  42.     private ListNode reverse(ListNode head) {
  43.         ListNode prev = null;
  44.         while(head != null) {
  45.             ListNode next = head.next;
  46.             head.next = prev;
  47.             prev = head;
  48.             head = next;
  49.         }
  50.         return prev;
  51.     }
  52. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-3 08:19:49 | 只看该作者
全局:
369. Plus One Linked List -- I used stack to try to avoid change the input argument

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. *     int val;
  5. *     ListNode next;
  6. *     ListNode(int x) { val = x; }
  7. * }
  8. */
  9. class Solution {//using stack/ reverse and then do plus
  10.     public ListNode plusOne(ListNode head) {
  11.         if (head == null) {
  12.             return new ListNode(1);
  13.         }
  14.         ListNode dummy = head;
  15.         Stack<ListNode> stack = new Stack<>();
  16.         while(dummy != null) {
  17.             stack.push(dummy);
  18.             dummy = dummy.next;
  19.         }
  20.         int carry = 1;
  21.         ListNode newHead = null;
  22.         while(!stack.isEmpty()) {
  23.             ListNode oldNode = stack.pop();
  24.             int val = carry + oldNode.val;
  25.             ListNode node = new ListNode(val % 10);
  26.             carry = val / 10;
  27.             node.next = newHead;
  28.             newHead = node;
  29.         }
  30.         if (carry != 0) {
  31.             ListNode node = new ListNode(carry);
  32.             node.next = newHead;
  33.             newHead = node;
  34.         }
  35.         return newHead;
  36.     }
  37. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-3 10:09:18 | 只看该作者
全局:
128. Longest Consecutive Sequence -- derive the optimal solution from brute force

//TODO: do one more using Union-Find

  1. class Solution {
  2.     public int longestConsecutive(int[] nums) {
  3.         if (nums == null || nums.length == 0) {
  4.             return 0;
  5.         }
  6.         Set<Integer> set = new HashSet<>();
  7.         for (int num : nums) {
  8.             set.add(num);
  9.         }
  10.         int ans = 0;
  11.         for (int num : nums) {
  12.             if (!set.contains(num - 1)) {
  13.                 int curAns = 1;
  14.                 int cur = num;
  15.                 while(set.contains(cur + 1)) {
  16.                     cur = cur + 1;
  17.                     curAns++;
  18.                 }
  19.                 ans = Math.max(ans, curAns);
  20.             }
  21.         }
  22.         return ans;
  23.     }
  24. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-4 08:56:32 | 只看该作者
全局:
303. Range Sum Query - Immutable
  1. class NumArray {
  2.     int[] sums;
  3.     public NumArray(int[] nums) {
  4.         this.sums = new int[nums.length + 1];
  5.         buildSums(nums);
  6.     }
  7.    
  8.     private void buildSums(int[] nums) {
  9.         for (int i = 0; i < nums.length; i++) {
  10.             sums[i + 1] += sums[i] + nums[i];
  11.         }
  12.     }
  13.    
  14.     public int sumRange(int i, int j) {
  15.         return sums[j + 1] - sums[i];
  16.     }
  17. }

  18. /**
  19. * Your NumArray object will be instantiated and called as such:
  20. * NumArray obj = new NumArray(nums);
  21. * int param_1 = obj.sumRange(i,j);
  22. */
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-4 09:52:31 | 只看该作者
全局:
307. Range Sum Query - Mutable -- binary segment tree (classic)
  1. class Node {
  2.     private int sum;
  3.     private int start;
  4.     private int end;
  5.     private Node left;
  6.     private Node right;
  7.    
  8.     public Node(int start, int end) {
  9.         this.sum = 0;
  10.         this.start = start;
  11.         this.end = end;
  12.         this.left = null;
  13.         this.right = null;
  14.     }
  15.    
  16.     public void setSum(int sum) {
  17.         this.sum = sum;
  18.     }
  19.    
  20.     public int getSum() {
  21.         return this.sum;
  22.     }
  23.    
  24.     public void setLeft(Node left) {
  25.         this.left = left;
  26.     }
  27.    
  28.     public void setRight(Node right) {
  29.         this.right = right;
  30.     }
  31.    
  32.     public int getStart() {
  33.         return this.start;
  34.     }
  35.    
  36.     public int getEnd() {
  37.         return this.end;
  38.     }
  39.    
  40.     public Node getLeft() {
  41.         return this.left;
  42.     }
  43.    
  44.     public Node getRight() {
  45.         return this.right;
  46.     }
  47. }

  48. class NumArray {
  49.    
  50.     Node head = null;
  51.    
  52.     public NumArray(int[] nums) {
  53.         head = buildTree(nums, 0, nums.length - 1);   
  54.     }
  55.    
  56.     private Node buildTree(int[] nums, int start, int end) {
  57.         if (start > end) {
  58.             return null;
  59.         }
  60.         Node head = new Node(start, end);
  61.         if (start == end) {
  62.             head.setSum(nums[start]);   
  63.         }else {
  64.             int mid = start + (end - start) / 2;
  65.             head.setLeft(buildTree(nums, start, mid));
  66.             head.setRight(buildTree(nums, mid + 1, end));
  67.             int sum = 0;
  68.             if (head.getLeft() != null) {
  69.                 sum += head.getLeft().getSum();
  70.             }
  71.             if (head.getRight() != null) {
  72.                 sum += head.getRight().getSum();
  73.             }
  74.             head.setSum(sum);
  75.         }
  76.         return head;
  77.         
  78.     }
  79.    
  80.     public void update(int i, int val) {
  81.         update(head, i, val);
  82.     }
  83.    
  84.     private void update(Node head, int index, int val) {
  85.         int start = head.getStart();
  86.         int end = head.getEnd();
  87.         if (start == end) {
  88.             head.setSum(val);
  89.             return;
  90.         }
  91.         int mid = start + (end - start) / 2;
  92.         if (index <= mid) {
  93.             update(head.getLeft(), index, val);
  94.         }else {
  95.             update(head.getRight(), index, val);
  96.         }
  97.         head.setSum(head.getLeft().getSum() + head.getRight().getSum());
  98.     }
  99.    
  100.     public int sumRange(int i, int j) {
  101.         return sumRange(head, i, j);
  102.     }
  103.    
  104.     private int sumRange(Node head, int small, int large) {
  105.         if (head.getLeft() == head.getRight()) {
  106.             return head.getSum();
  107.         }
  108.         int start = head.getStart();
  109.         int end = head.getEnd();
  110.         int mid = start + (end - start) / 2;
  111.         if (large <= mid) {
  112.             return sumRange(head.getLeft(), small, large);
  113.         }else if (small > mid) {
  114.             return sumRange(head.getRight(), small, large);
  115.         }else {
  116.             return sumRange(head.getLeft(), small, mid) + sumRange(head.getRight(), mid + 1, large);
  117.         }
  118.     }
  119. }

  120. /**
  121. * Your NumArray object will be instantiated and called as such:
  122. * NumArray obj = new NumArray(nums);
  123. * obj.update(i,val);
  124. * int param_2 = obj.sumRange(i,j);
  125. */
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-4 10:41:31 | 只看该作者
全局:
本帖最后由 zhangrz2 于 2019-6-4 10:42 编辑

725. Split Linked List in Parts -- make sure to go through some examples(try to make it bug free)

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. *     int val;
  5. *     ListNode next;
  6. *     ListNode(int x) { val = x; }
  7. * }
  8. */
  9. class Solution {
  10.     public ListNode[] splitListToParts(ListNode root, int k) {
  11.         if (root == null) {
  12.             return new ListNode[k];
  13.         }
  14.         ListNode[] ans = new ListNode[k];
  15.         int count = 0;
  16.         ListNode head = root;
  17.         while(head != null) {
  18.             count++;
  19.             head = head.next;
  20.         }
  21.         int mod = count % k;
  22.         int num = count / k;
  23.         head = root;
  24.         int index = 0;
  25.         while(head != null) {
  26.             int innerCount = mod == 0 ? num : num + 1;
  27.             if (mod > 0) {
  28.                 mod--;
  29.             }
  30.             if (innerCount > 0) {
  31.                 ans[index] = head;
  32.                 while(innerCount > 1 && head != null) {
  33.                     head = head.next;
  34.                     innerCount--;
  35.                 }
  36.                 if (head != null) {
  37.                     ListNode next = head.next;
  38.                     head.next = null;
  39.                     head = next;
  40.                 }
  41.             }
  42.             index++;
  43.         }
  44.         return ans;
  45.     }
  46. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-5 09:38:07 | 只看该作者
全局:
406. Queue Reconstruction by Height -- greedy is always being hard for me
  1. class Solution {
  2.     public int[][] reconstructQueue(int[][] people) {
  3.         if (people == null || people.length == 0) {
  4.             return new int[0][0];
  5.         }
  6.         Arrays.sort(people, (a, b) -> b[0] == a[0] ? a[1] - b[1] : b[0] - a[0]);
  7.         List<int[]> ansList = new ArrayList<>();
  8.         for (int[] array : people) {
  9.             ansList.add(array[1], array);
  10.         }
  11.         int[][] ans = ansList.toArray(new int[people.length][2]);
  12.         return ans;
  13.     }
  14. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-6 07:22:29 | 只看该作者
全局:
121. Best Time to Buy and Sell Stock -- 昨天拉下的这周补回来
  1. class Solution {
  2.     public int maxProfit(int[] prices) {
  3.         if (prices == null || prices.length == 0) {
  4.             return 0;
  5.         }
  6.         int globalMin = Integer.MAX_VALUE;
  7.         int ans = 0;
  8.         for (int i = 0; i < prices.length; i++) {
  9.             globalMin = Math.min(globalMin, prices[i]);
  10.             ans = Math.max(ans, prices[i] - globalMin);
  11.         }
  12.         return ans;
  13.     }
  14. }
复制代码
回复

使用道具 举报

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

本版积分规则

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