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

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

🔗
 楼主| zhangrz2 2019-8-26 05:40:21 | 只看该作者
全局:
448. Find All Numbers Disappeared in an Array

  1. class Solution {
  2.     // have a two pass idea with no extra space
  3.     public List<Integer> findDisappearedNumbers(int[] nums) {
  4.         List<Integer> ans = new ArrayList<>();
  5.         if (nums == null || nums.length == 0) {
  6.             return ans;
  7.         }
  8.         for (int i = 0; i < nums.length; i++) {
  9.             int index = Math.abs(nums[i]) - 1;
  10.             if (nums[index] > 0) {
  11.                 nums[index] = -nums[index];
  12.             }
  13.         }
  14.         
  15.         for (int i = 0; i < nums.length; i++) {
  16.             if (nums[i] > 0) {
  17.                 ans.add(i + 1);
  18.             }
  19.         }
  20.         return ans;
  21.     }
  22. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-8-26 09:36:47 | 只看该作者
全局:
47. Permutations II

  1. class Solution {
  2.     //Dfs with memorization
  3.     public List<List<Integer>> permuteUnique(int[] nums) {
  4.         Set<List<Integer>> ans = new HashSet<>();
  5.         if (nums == null || nums.length == 0) {
  6.             return new ArrayList<List<Integer>>();
  7.         }
  8.         boolean[] visited = new boolean[nums.length];
  9.         visit(ans, new ArrayList<Integer>(), nums, visited);
  10.         return new ArrayList<List<Integer>>(ans);
  11.     }
  12.    
  13.     private void visit(Set<List<Integer>> ans, List<Integer> list, int[] nums, boolean[] visited) {
  14.         if (list.size() == nums.length) {
  15.             ans.add(new ArrayList<Integer>(list));
  16.             return;
  17.         }
  18.         for (int i = 0; i < nums.length; i++) {
  19.             if (!visited[i]) {
  20.                 list.add(nums[i]);
  21.                 visited[i] = true;
  22.                 visit(ans, list, nums, visited);
  23.                 visited[i] = false;
  24.                 list.remove(list.size() - 1);
  25.             }
  26.         }
  27.         return;
  28.     }
  29. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-8-27 09:32:55 | 只看该作者
全局:
743. Network Delay Time

  1. class Solution {
  2.     //Dijkstra's Algorithm
  3.     //forgot how to implement the dijkstra
  4.     //so close to figure out solution
  5.     //using priority queue solution
  6.     //triky part is to know when to terminate the loop
  7.     public int networkDelayTime(int[][] times, int N, int K) {
  8.         if (times == null || times.length == 0) {
  9.             return -1;
  10.         }
  11.         Map<Integer, Map<Integer, Integer>> edges = convertEdgeToMap(times);
  12.         boolean[] visited = new boolean[N + 1];
  13.         int ans = 0;
  14.         PriorityQueue<int[]> queue = new PriorityQueue<>((a, b) -> a[0] - b[0]);
  15.         queue.add(new int[]{0, K});
  16.         while(!queue.isEmpty()){
  17.             int[] curNode = queue.remove();
  18.             if (visited[curNode[1]]) {
  19.                 continue;
  20.             }
  21.             ans = curNode[0];
  22.             N--;// when N == 0, then all the node visited with minimum steps
  23.             visited[curNode[1]] = true;
  24.             if (edges.containsKey(curNode[1])) { // forgot to check if the node has downstream that connected
  25.                 for (int next : edges.get(curNode[1]).keySet()) {
  26.                     queue.offer(new int[]{curNode[0] + edges.get(curNode[1]).get(next), next});
  27.                 }
  28.             }
  29.         }
  30.         return N == 0 ? ans : -1;
  31.     }
  32.    
  33.     private Map<Integer, Map<Integer, Integer>> convertEdgeToMap(int[][] times) {
  34.         Map<Integer, Map<Integer, Integer>> ans = new HashMap<>();        
  35.         for (int[] time : times) {
  36.             if (ans.containsKey(time[0])) {
  37.                 ans.get(time[0]).put(time[1], time[2]);
  38.             }else {
  39.                 Map<Integer, Integer> unit = new HashMap<>();
  40.                 unit.put(time[1], time[2]);
  41.                 ans.put(time[0], unit);
  42.             }
  43.         }
  44.         return ans;
  45.     }
  46. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-8-27 09:38:29 | 只看该作者
全局:
136. Single Number

  1. class Solution {
  2.     public int singleNumber(int[] nums) {
  3.         if (nums.length == 1) {
  4.             return nums[0];
  5.         }
  6.         int ans = nums[0];
  7.         for (int i = 1; i < nums.length; i++) {
  8.             ans = ans ^ nums[i];
  9.         }
  10.         return ans;
  11.     }
  12. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-8-27 10:05:19 | 只看该作者
全局:
40. Combination Sum II

  1. class Solution {
  2.     public List<List<Integer>> combinationSum2(int[] candidates, int target) {
  3.         List<List<Integer>> ans = new ArrayList<>();
  4.         if (candidates == null || candidates.length == 0) {
  5.             return ans;
  6.         }
  7.         Arrays.sort(candidates);
  8.         dfs(ans, new ArrayList<Integer>(), candidates, target, 0, 0);
  9.         return ans;
  10.     }
  11.    
  12.     private void dfs(List<List<Integer>> ans, List<Integer> list, int[] candidates, int target, int curSum, int curIndex) {
  13.         if (curSum == target) {
  14.             ans.add(new ArrayList<Integer>(list));
  15.             return;
  16.         }else if (curSum > target) {
  17.             return;
  18.         }
  19.         for (int i = curIndex; i < candidates.length; i++) {
  20.             if (i > curIndex && candidates[i] == candidates[i-1]) continue; // only this part need to twick from my solution
  21.             list.add(candidates[i]);
  22.             dfs(ans, list, candidates, target, curSum + candidates[i], i + 1);
  23.             list.remove(list.size() - 1);
  24.         }
  25.         return;
  26.     }
  27. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-8-28 09:11:24 | 只看该作者
全局:
516. Longest Palindromic Subsequence

  1. class Solution {
  2.     //DP question, did before,
  3.     //but hard to remember how I did it
  4.     //apparently it's 2D dp
  5.     // the code can explan the transition function itself
  6.     public int longestPalindromeSubseq(String s) {
  7.         if (s == null || s.length() == 0) {
  8.             return 0;
  9.         }
  10.         int[][] dp = new int[s.length()][s.length()];
  11.         for (int i = s.length() - 1 ; i >= 0; i--) {// note: loop from end to front
  12.             dp[i][i] = 1;
  13.             for (int j = i + 1; j < s.length(); j++) {
  14.                 if (s.charAt(i) == s.charAt(j)) {
  15.                     dp[i][j] = dp[i + 1][j - 1] + 2;
  16.                 }else {
  17.                     dp[i][j] = Math.max(dp[i+1][j], dp[i][j - 1]);
  18.                 }
  19.             }
  20.         }
  21.         return dp[0][s.length() - 1];
  22.     }
  23. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-8-28 10:58:24 | 只看该作者
全局:
528. Random Pick with Weight

  1. class Solution {
  2.     //using binary search with accumulated sum
  3.     //second binary search template
  4.     private int[] wSum;
  5.     private Random random;
  6.     public Solution(int[] w) {
  7.         this.random = new Random();
  8.         this.wSum = new int[w.length];
  9.         this.wSum[0] = w[0];
  10.         for (int i = 1; i < w.length; i++) {
  11.             wSum[i] = w[i] + wSum[i - 1]; // -- also made stupid mistake here
  12.         }
  13.     }
  14.    
  15.     public int pickIndex() {
  16.         int index = random.nextInt(wSum[wSum.length - 1]) + 1; // the the random number from range 1 to acculumated sum
  17.         int left = 0;
  18.         int right = wSum.length - 1;
  19.         while(left < right) {
  20.             int mid = left + (right - left) / 2;
  21.             if (wSum[mid] == index) {
  22.                 return mid;
  23.             }else if (wSum[mid] < index) {
  24.                 left = mid + 1;
  25.             }else {
  26.                 right = mid;
  27.             }
  28.         }
  29.         return left;
  30.     }
  31. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-8-29 09:12:59 | 只看该作者
全局:
1026. Maximum Difference Between Node and Ancestor

  1. class Solution {
  2.     private int ans = 0;
  3.     public int maxAncestorDiff(TreeNode root) {
  4.         if (root == null) {
  5.             return ans;
  6.         }
  7.         int[] maxAndMin = getMaxAndMin(root);
  8.         return ans;
  9.     }
  10.    
  11.     private int[] getMaxAndMin(TreeNode root) {
  12.         if (root.left == null && root.right == null) {
  13.             return new int[]{root.val, root.val};
  14.         }
  15.         int[] res = new int[]{root.val, root.val};
  16.         if (root.left != null) {
  17.             int[] leftRes = getMaxAndMin(root.left);
  18.             res[0] = Math.min(res[0], leftRes[0]);
  19.             res[1] = Math.max(res[1], leftRes[1]);
  20.         }
  21.         if (root.right != null) {
  22.             int[] rightRes = getMaxAndMin(root.right);
  23.             res[0] = Math.min(res[0], rightRes[0]);
  24.             res[1] = Math.max(res[1], rightRes[1]);
  25.         }
  26.         this.ans = Math.max(ans, Math.max(Math.abs(root.val - res[0]), Math.abs(root.val - res[1]))); //made a stupid mistake here
  27.         return res;
  28.     }
  29.      
  30.    
  31. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-9-1 04:38:26 | 只看该作者
全局:
503. Next Greater Element II

  1. class Solution {
  2.     //derived from problem 496
  3.     //using stack to keep track the decresing numbers
  4.     public int[] nextGreaterElements(int[] nums) {
  5.         if (nums == null || nums.length == 0) {
  6.             return new int[0];
  7.         }
  8.         Map<Integer, Queue<Integer>> map = new HashMap<>();
  9.         Stack<Integer> stack = new Stack<>();
  10.         int max = Integer.MIN_VALUE;
  11.         for (int num : nums) {
  12.             while(!stack.isEmpty() && stack.peek() < num) {
  13.                 int key = stack.pop();
  14.                 Queue<Integer> queue = map.getOrDefault(key, new LinkedList<Integer>());
  15.                 queue.offer(num);
  16.                 map.put(key, queue);
  17.             }
  18.             stack.push(num);
  19.             max = Math.max(max, num);
  20.         }
  21.         for (int i = 0; i < nums.length && !stack.isEmpty(); i++) {
  22.             while(!stack.isEmpty() && stack.peek() < nums[i]) {
  23.                 int key = stack.pop();
  24.                 Queue<Integer> queue = map.getOrDefault(key, new LinkedList<Integer>());
  25.                 queue.offer(nums[i]);
  26.                 map.put(key, queue);
  27.             }
  28.             if (stack.peek() == max) {
  29.                 int key = stack.pop();
  30.                 Queue<Integer> queue = map.getOrDefault(key, new LinkedList<Integer>());
  31.                 queue.offer(-1);
  32.                 map.put(key, queue);
  33.             }
  34.         }
  35.         
  36.         int[] ans = new int[nums.length];
  37.         for (int i = 0; i < nums.length; i++) {
  38.             ans[i] = map.get(nums[i]).poll();
  39.         }
  40.         return ans;
  41.     }
  42. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-9-1 04:38:42 | 只看该作者
全局:
496. Next Greater Element I
  1. class Solution {
  2.     //can't even think the best answer for this easy question...
  3.     public int[] nextGreaterElement(int[] nums1, int[] nums2) {
  4.         if (nums1 == null || nums2 == null || nums1.length == 0 || nums2.length == 0) {
  5.             return new int[0];
  6.         }
  7.         int[] ans = new int[nums1.length];
  8.         Map<Integer, Integer> map = new HashMap<>(); // a map keep the next greater numer
  9.         Stack<Integer> stack = new Stack<>();
  10.         for (int num : nums2) {
  11.             while(!stack.isEmpty() && stack.peek() < num) {
  12.                 map.put(stack.pop(), num);
  13.             }
  14.             stack.push(num);
  15.         }
  16.         for (int i = 0; i < nums1.length; i++) {
  17.             int next = map.getOrDefault(nums1[i], - 1);
  18.             ans[i] = next;
  19.         }
  20.         return ans;
  21.     }
  22. }
复制代码

回复

使用道具 举报

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

本版积分规则

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