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

里口战拖 - 通向法斗的道路

🔗
 楼主| yyy884849 2018-4-22 03:47:06 | 只看该作者
全局:
317. Shortest Distance from All Buildings
从每一个1的位置开始bfs,计算到每个0的距离。
AC的时间位于两个驼峰之间。。。不知道前面的AC是怎么写的。待研究 4/21

  1. class Solution {
  2.    
  3.     private int[] dx = { 0, -1, 0, 1 };
  4.     private int[] dy = { 1, 0, -1, 0 };
  5.    
  6.     public int shortestDistance(int[][] grid) {
  7.         int row = grid.length, col = grid[0].length;
  8.         int[][] count = new int[row][col];
  9.         
  10.         ArrayList<Integer> buildx = new ArrayList<>();
  11.         ArrayList<Integer> buildy = new ArrayList<>();
  12.         for(int i = 0; i < row; i++){
  13.             for(int j = 0; j < col; j++){
  14.                 if(grid[i][j] == 1){
  15.                     buildx.add(i);
  16.                     buildy.add(j);
  17.                 }
  18.                 if(grid[i][j] != 0){
  19.                     count[i][j] = -1;
  20.                 }
  21.             }
  22.         }
  23.         
  24.         for(int i = 0; i < buildx.size(); i++){
  25.             bfs(grid, count, buildx.get(i), buildy.get(i));
  26.         }
  27.         
  28.         int res = Integer.MAX_VALUE;
  29.         for(int i = 0; i < row; i++){
  30.             for(int j = 0; j < col; j++){
  31.                 if(count[i][j] != - 1){
  32.                     res = Math.min(res, count[i][j]);
  33.                 }
  34.             }
  35.         }
  36.         return res == Integer.MAX_VALUE ? -1 : res;
  37.     }
  38.    
  39.     // (m,n) is 1
  40.     private void bfs(int[][] grid, int[][] count, int m, int n){
  41.         Queue<Integer> x = new LinkedList<>();
  42.         Queue<Integer> y = new LinkedList<>();
  43.         
  44.         int row = grid.length, col = grid[0].length, steps = 0;
  45.         int[][] distance = new int[row][col];
  46.         distance[m][n] = -1;
  47.         for(int i = 0; i < 4; i++){
  48.             x.offer(m + dx[i]);
  49.             y.offer(n + dy[i]);
  50.         }
  51.         
  52.         while(x.size() > 0){
  53.             int size = x.size();
  54.             steps += 1;
  55.             for(int i = 0; i < size; i++){
  56.                 int px = x.poll();
  57.                 int py = y.poll();
  58.                 if(px < 0 || px >= row || py < 0 || py >= col || distance[px][py] != 0 || count[px][py] == -1) continue;
  59.                 if(grid[px][py] != 0) {
  60.                     distance[px][py] = -1;
  61.                     continue;
  62.                 }
  63.                 distance[px][py] = steps;
  64.                 for(int j = 0; j < 4; j++){
  65.                     x.offer(px + dx[j]);
  66.                     y.offer(py + dy[j]);
  67.                 }
  68.             }
  69.         }
  70.         
  71.         for(int i = 0; i < row; i++){
  72.             for(int j = 0; j < col; j++){
  73.                 if(count[i][j] != -1){
  74.                     if(distance[i][j] <= 0){
  75.                         count[i][j] = -1;
  76.                     }else{
  77.                         count[i][j] += distance[i][j];
  78.                     }
  79.                 }
  80.             }
  81.         }
  82.         
  83.     }   
  84. }
复制代码
回复

使用道具 举报

🔗
 楼主| yyy884849 2018-4-22 05:08:38 | 只看该作者
全局:
5. Longest Palindromic Substring
O(N*N)

4/21
回复

使用道具 举报

🔗
 楼主| yyy884849 2018-4-22 06:32:48 | 只看该作者
全局:
37. Sudoku Solver
好多老题都不记得刷过。。。
4/21
回复

使用道具 举报

🔗
 楼主| yyy884849 2018-4-23 01:21:01 | 只看该作者
全局:
本帖最后由 yyy884849 于 2018-4-22 16:51 编辑

51. N-Queens && 52. N-Queens
22. Generate Parentheses
24. Swap Nodes in Pairs
25. Reverse Nodes in k-Group
6. ZigZag Conversion
32. Longest Valid Parentheses413. Arithmetic Slices
795. Number of Subarrays with Bounded Maximum 根据答案,转化成413类似的思想

4/22







回复

使用道具 举报

🔗
 楼主| yyy884849 2018-4-24 06:18:51 | 只看该作者
全局:
本帖最后由 yyy884849 于 2018-4-23 17:29 编辑

76. Minimum Window Substring
159. Longest Substring with At Most Two Distinct Characters
340. Longest Substring with At Most K Distinct Characters
几个sliding window的题

99. Recover Binary Search Tree 直接拿inorder做的,速度很慢就是了

31. Next Permutation
60. Permutation Sequence


4/23
回复

使用道具 举报

🔗
 楼主| yyy884849 2018-6-12 22:39:15 | 只看该作者
全局:
本帖最后由 yyy884849 于 2018-6-12 21:10 编辑

很久没来,最近回国又忙了一点其他的事情,今儿开始继续。。。

826. Most Profit Assigning Work:
排序,更新每个difficulty可以达到的最大profit,然后二分查找。 排序: 难度从低到高,profit从高到低;二分查找的时候考虑right=0的edge case

221. Maximal Square: 先用brute force解的,mn square的复杂度,居然木有超时。。。dp
652. Find Duplicate Subtrees:  dfs; computeIfAbsent(), 第一次见,高端了

775. Global and Local Inversions:
  1. class Solution {
  2.     public boolean isIdealPermutation(int[] A) {
  3.         int n = A.length;
  4.         int curMax = 0;
  5.         for(int i = 0; i < n; i++){
  6.             if(i != 0 && curMax >= A[i]) continue;
  7.             curMax = Math.max(curMax, A[i]);
  8.             for(int j = i+2; j < n; j++){
  9.                 if(A[j] < A[i]) return false;
  10.             }
  11.         }
  12.         return true;
  13.     }
  14. }
复制代码


运气好了可以过,还是看答案吧。。。[/i][/i][/i]
464. Can I Win:187ms,没用bitwise operation,就酱了。。。
735. Asteroid Collision: stack
542. 01 Matrix: bfs
718. Maximum Length of Repeated Subarray: dp

[i][i]6/12
[/i][/i]
回复

使用道具 举报

🔗
 楼主| yyy884849 2018-6-14 04:19:02 | 只看该作者
全局:
本帖最后由 yyy884849 于 2018-6-13 21:06 编辑

513. Find Bottom Left Tree Value: bfs
399. Evaluate Division: graph +dfs
791. Custom Sort String: 想写一个customized comparator,但是Arrays.sort()如果需要一个comparator的话,要先转成Character[],出奇的复杂

6/13
回复

使用道具 举报

🔗
 楼主| yyy884849 2018-6-15 09:39:35 | 只看该作者
全局:
814. Binary Tree Pruning - dfs
334. Increasing Triplet Subsequence: 两个local variable一遍扫
300. Longest Increasing Subsequence: N方和NLogN两个解法;自己写bInary search的就是 find insertion place的easy题 (35)
821. Shortest Distance to a Character: stack
498. Diagonal Traverse : 有点恶心。。。
402. Remove K Digits: stack

6/14
回复

使用道具 举报

🔗
 楼主| yyy884849 2018-6-18 10:45:04 | 只看该作者
全局:
280. Wiggle Sort
324. Wiggle Sort II
215. Kth Largest Element in an Array: quick selection sort

6/15
回复

使用道具 举报

🔗
 楼主| yyy884849 2018-6-18 10:49:13 | 只看该作者
全局:
368. Largest Divisible Subset
373. Find K Pairs with Smallest Sums

6/17
回复

使用道具 举报

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

本版积分规则

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