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

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

🔗
 楼主| zhangrz2 2019-6-18 09:37:35 | 只看该作者
全局:
268. Missing Number -- Math

  1. class Solution {
  2.     //using summation
  3.     public int missingNumber(int[] nums) {
  4.         int n = nums.length;
  5.         if (n <= 0) {
  6.             return 0;
  7.         }
  8.         int sum = 0;
  9.         for (int i = 0; i <= n; i++) {
  10.             sum += i;
  11.         }
  12.         int secSum = 0;
  13.         for (int i : nums) {
  14.             secSum += i;
  15.         }
  16.         return sum - secSum;
  17.     }
  18. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-19 08:56:54 | 只看该作者
全局:
70. Climbing Stairs

  1. class Solution {
  2.     public int climbStairs(int n) {
  3.         if (n <= 0) {
  4.             return 0;
  5.         }else if (n == 1) {
  6.             return 1;
  7.         }else if (n == 2) {
  8.             return 2;
  9.         }
  10.         int secLast = 1;
  11.         int last = 2;
  12.         int cur = 0;
  13.         for (int i = 3; i <= n; i++) {
  14.             cur = secLast + last;
  15.             secLast = last;
  16.             last = cur;
  17.         }
  18.         return cur;
  19.     }
  20. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-19 09:30:06 | 只看该作者
全局:
134. Gas Station -- as always -- greedy is hard to solve哎
  1. class Solution {
  2.     public int canCompleteCircuit(int[] gas, int[] cost) {
  3.         if (gas == null || cost == null || gas.length == 0 || cost.length == 0) {
  4.             return -1;
  5.         }
  6.         int curTank = 0;
  7.         int total = 0;
  8.         int station = 0;
  9.         for (int i = 0; i < gas.length; i++) {
  10.             total += gas[i] - cost[i];
  11.             curTank += gas[i] - cost[i];
  12.             if (curTank < 0) {
  13.                 station = i + 1;
  14.                 curTank = 0;
  15.             }
  16.         }
  17.         return total < 0 ? -1 : station;
  18.     }
  19. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-19 10:04:20 | 只看该作者
全局:
152. Maximum Product Subarray -- dynamic programming my ass

  1. class Solution {
  2.     public int maxProduct(int[] nums) {
  3.         if (nums == null || nums.length == 0) {
  4.             return 0;
  5.         }
  6.         int ans = nums[0];
  7.         int max = nums[0];
  8.         int min = nums[0];
  9.         for (int i = 1; i < nums.length; i++) {
  10.             if (nums[i] < 0) {
  11.                 int temp = max;
  12.                 max = min;
  13.                 min = temp;
  14.             }
  15.             max = Math.max(nums[i] * max, nums[i]);
  16.             min = Math.min(nums[i] * min, nums[i]);
  17.             ans = Math.max(ans, max);
  18.         }
  19.         return ans;
  20.     }
  21. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-20 08:18:41 | 只看该作者
全局:
今天有点不舒服,刷题休息一天,看看system design 去
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-21 09:19:00 | 只看该作者
全局:
997. Find the Town Judge -- directed graph, count in-out
if in - out == N-1
then that's the judge
  1. class Solution {
  2.     //directed graph
  3.     //in - out == N - 1 is the judge
  4.     public int findJudge(int N, int[][] trust) {
  5.         if (N <= 0) {
  6.             return -1;
  7.         }
  8.         int[] per = new int[N + 1];
  9.         for (int[] unit : trust) {
  10.             per[unit[0]]--;
  11.             per[unit[1]]++;
  12.         }
  13.         for (int i = 1; i <= N; i++) {
  14.             if (per[i] == N - 1) {
  15.                 return i;
  16.             }
  17.         }
  18.         return -1;
  19.     }
  20. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-21 10:35:59 | 只看该作者
全局:
490. The Maze -- move all the way

  1. class Solution {
  2.     private int[][] directions = new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
  3.     public boolean hasPath(int[][] maze, int[] start, int[] destination) {
  4.         boolean[][] visited = new boolean[maze.length][maze[0].length];
  5.         //bfs
  6.         Queue<int[]> queue = new LinkedList<>();
  7.         queue.offer(start);
  8.         visited[start[0]][start[1]] = true;
  9.         while(!queue.isEmpty()) {
  10.             int[] cur = queue.poll();
  11.             int curRow = cur[0];
  12.             int curCol = cur[1];
  13.             if(curRow == destination[0] && curCol == destination[1]) {
  14.                 return true;
  15.             }
  16.             for (int[] dir : directions) {
  17.                 int row = curRow + dir[0];
  18.                 int col = curCol + dir[1];
  19.                 while (row >= 0 && row < maze.length && col >= 0 && col < maze[0].length && maze[row][col] == 0) {
  20.                     row += dir[0];
  21.                     col += dir[1];
  22.                 }
  23.                 if (!visited[row - dir[0]][col - dir[1]]) {
  24.                     queue.offer(new int[]{row - dir[0], col - dir[1]});
  25.                     visited[row - dir[0]][col - dir[1]] = true;
  26.                 }
  27.             }
  28.         }
  29.         return false;
  30.     }
  31.    


  32. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-23 09:41:09 | 只看该作者
全局:
今天看System Design, 明天 pick up 刷题
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-25 09:57:43 | 只看该作者
全局:
505. The Maze II -- BFS plus keep track min distance

  1. class Solution {
  2.     public int shortestDistance(int[][] maze, int[] start, int[] destination) {
  3.         if (maze == null || maze.length == 0 || start == null || destination == null) {
  4.             return -1;
  5.         }
  6.         int[][] distance = new int[maze.length][maze[0].length];
  7.         initDist(distance);
  8.         Queue<int[]> queue = new LinkedList<>();
  9.         queue.offer(new int[]{start[0], start[1]});
  10.         distance[start[0]][start[1]] = 0;
  11.         int[][] dir = new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
  12.         while(!queue.isEmpty()) {
  13.             int[] cur = queue.poll();
  14.             for (int[] unit : dir) {
  15.                 int count = 0;
  16.                 int row = cur[0] + unit[0];
  17.                 int col = cur[1] + unit[1];
  18.                 while(row >= 0 && row < maze.length && col >= 0 && col < maze[0].length && maze[row][col] == 0) {
  19.                     row += unit[0];
  20.                     col += unit[1];
  21.                     count++;
  22.                 }
  23.                 if (count + distance[cur[0]][cur[1]] < distance[row - unit[0]][col - unit[1]]) {
  24.                     distance[row - unit[0]][col - unit[1]] = count + distance[cur[0]][cur[1]];
  25.                     queue.offer(new int[]{row - unit[0], col - unit[1]});
  26.                 }
  27.                
  28.             }
  29.         }
  30.         return distance[destination[0]][destination[1]] == Integer.MAX_VALUE ? -1 : distance[destination[0]][destination[1]];
  31.         
  32.     }
  33.    
  34.     private void initDist(int[][] distance) {
  35.         for (int[] array : distance) {
  36.             Arrays.fill(array, Integer.MAX_VALUE);
  37.         }
  38.     }
  39. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-25 10:13:20 | 只看该作者
全局:
62. Unique Paths --DP

  1. class Solution {
  2.     //typical Dp
  3.     public int uniquePaths(int m, int n) {
  4.         int[][] path = new int[m][n];
  5.         for (int i = 0; i < m; i++) {
  6.             for (int j = 0; j < n; j++) {
  7.                 if (i == 0 || j == 0) {
  8.                     path[i][j] = 1;
  9.                 } else {
  10.                     path[i][j] = path[i - 1][j] + path[i][j - 1];
  11.                 }
  12.             }
  13.         }   
  14.         return path[m - 1][n - 1];
  15.    
  16.     }
  17. }
复制代码
回复

使用道具 举报

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

本版积分规则

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