查看: 1124| 回复: 0
跳转到指定楼层
上一主题 下一主题
收起左侧

[其他] 我对刷题的浅见

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
1. 如果你觉得你付出努力,都没啥效果。
1. 我建议你看看这个: https://www.zhihu.com/question/24051063
2. 大部份人是慢思维,至少我是
4. 现在要用几天的时间刷一道题
5. 我看别人都叫我一道题刷几十遍,就是记不住才刷几十遍。就是说刷一次的功效极低, 才要重复?
6. 我觉得每一行代码都ask why. 问得越深越好,你一次就记住了. 你为什么看答案的时候要ask why, 因为你自己写的时候就像dfs, 会有n步骤, 你会开始confuse, swing, 也不知道写什么,为什么要写?
7. so you have to deep thinking for each line of code

e.g. https://leetcode.com/problems/minesweeper/
  1. /**
  2. * @param {character[][]} board
  3. * @param {number[]} click
  4. * @return {character[][]}
  5. */

  6. var dfs = function(d, b, click) {
  7.     // * why the cell in board is the underneath actual value? <----
  8.     // * because if we know the actual value, we can display the value in the board for user
  9.    
  10.     // * why click has row and col? <---
  11.     // * because it is the start position in the grid
  12.     const r = click[0];
  13.     const c = click[1];
  14.     // * this is row, go down
  15.     const m = b.length;
  16.     // * this is col, go right
  17.     const n = b[0].length;
  18.    
  19.     let mNum = 0;
  20.     // * why the dfs queue arr is empty at each dfs level? <---
  21.     // * because next dfs level, the click will have its own dfs queue
  22.     // * because 8 directions, cause it own queue, in own dfs level
  23.     const empty = [];
  24.    
  25.     // * why do we hit the directions straight away, there is no loop to travel? <---
  26.     // * yes, because there is no loop here, so only direction
  27.    
  28.     // * if we click a cell, the direction is like a sum with diff direciton of shine, why? <---
  29.     // * because the middle is the central cell, then 8 directions are the shines
  30.    
  31.     // * why we loop the 8 directions? <---
  32.     // * because click 1 spot, we will hit all 8 directions
  33.     // * it can be 4 directions or an upgrade: 8 directions
  34.     for(let [r1, c1] of d) {
  35.         // * why we need row = curr_row + row_d, col = curr_col + col_d? <---
  36.         // * because after loop each direction, we have a new move
  37.         // * we can check boundary with this new move
  38.         const r2 = r + r1;
  39.         const c2 = c + c1;
  40.         
  41.         // * so this is to check the future boundary
  42.         if(r2 < 0 || c2 < 0 || r2 >= m || c2 >= n) continue;
  43.         
  44.         // * why we only check boundary, we don't have other condi to check? <--
  45.         // * because after boundary check pass, we will hit mine (M, acc), E (empty, queue), B (blank, seen),
  46.         // * D (digit, seen), X (reveal, never)
  47.         // * why we don't need seen here? <---
  48.         // * because there is no cycle;
  49.         // * seen good for cycle
  50.         
  51.         // * why we need to care mine (M = mine)? <---
  52.         // * because if we acc all 8 directions, we know the total mine number, so we can assign to start point????????
  53.         // * why we need to care empty square (E = empty)? <---
  54.         // * because if we see empty (E), then we can push to arr, then loop this arr -> dfs
  55.         // * this allow new start point for dfs
  56.         // * why we don't care revealved square (B = blank)? <--
  57.         // * because already know before
  58.         // * not contribute to acc mine or dfs queue
  59.         // * why we don't care digit? (D = digit)? <--
  60.         // * because already know before
  61.         // * not contribute to acc mine or dfs queue
  62.         // * why we don't care revealed mine (X = explode)? <--
  63.         // * I don't think we will see this, if we see explode, then it is game over
  64.         // * the game still going so no care revealed.
  65.         
  66.         if(b[r2][c2] === 'M') {
  67.             // * why acc mine num? <---
  68.             // * because we need to asssgin to start point
  69.             // * why we see a future mine here, we stop, we do not do dfs? <----
  70.             // * because if we see a mine, it means this path cannot go further, so need to stop dfs path!!!!!!!!!
  71.             ++mNum;   
  72.         } else if(b[r2][c2] === 'E') {
  73.             // * if we can see empty, this means dfs path can continue, can keep going <---
  74.             // * so why put start point to arr? <---
  75.             // * because those dfs path can go down
  76.             empty.push([r2, c2]);
  77.         }
  78.     } // el
  79.    
  80.     // * if we see mine, we need to stop directly, even we have some dfs queue?
  81.     // * because ...?
  82.     if(mNum > 0) {
  83.         b[r][c] = String(mNum);
  84.     } else {
  85.         // * so why we revreal the click? <---
  86.         // * because in this dfs level, this click is used, so reveal it
  87.         // * the dfs queue is below, is acting like that it will simulate the click further (further reveal)
  88.         b[r][c] = 'B';
  89.         
  90.         // * why the element in queue order is the dir order? <----
  91.         // * because we loop direction, then add to dfs queue, so dfs queue order === direction order
  92.         for(let [x, y] of empty) {
  93.             dfs(d, b, [x, y]);
  94.         }
  95.     }
  96. }

  97. var updateBoard = function(b, click) {
  98.     // * why do we need the 8 cell grid direction? <---
  99.     // * because click 1 spot, then all 8 directions get exploded.
  100.     const d = [[-1, 0], [-1, 1], [0, 1], [1, 1], [1, 0], [1, -1], [0, -1], [-1, -1]];
  101.     // * why click has row?
  102.     // * because it will land on grid
  103.     const r = click[0];
  104.     // * why click has col?
  105.     // * because it will land on the grid
  106.     const c = click[1];
  107.    
  108.     // * why there is no loop then dfs? <---
  109.     // * because we have a click (start_row, start_col), then we just dfs init, goes down
  110.    
  111.     // * why we need to handle click the mine directly? <---
  112.     // * because if we don't handle here now, when goes into dfs, we only look at future 8 directions.
  113.     // * the curr one will be ignored, so we need to handle now.
  114.     // * why M === untouched mine? <----
  115.     // * because M is the init letter of Mine; it is a good and untouched mine.
  116.     // * why X === revealed mine? <----
  117.     // * because X looks like exploding and cross means bad
  118.     if(b[r][c] === 'M') {
  119.         b[r][c] = 'X';
  120.         // * why we need to return the board? <---
  121.         // * because we click on the mine, then explode, need to return board
  122.         return b;
  123.     } else {
  124.         // * why pass direction, beacuse new_row = row + row_d? <---
  125.         // * new_col = col + col_d
  126.         // * so we need the next move
  127.         dfs(d, b, click);
  128.         return b;
  129.     }
  130. };
复制代码

评分

参与人数 4大米 +5 收起 理由
14417335 + 2 给你点个赞!
小亩_ll22qw3 + 1 给你点个赞!
Akimodo_kya + 1 赞一个
Zasdivc + 1 赞一个

查看全部评分


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

本版积分规则

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