2022年2月3日 星期四

542, 01_matrix

====
542, 01_matrix
====
BFS,
Shortest path
Path existed
====
1. 題目希望將給予的matrix
化成
每個單元離它最近的'0'的距離

2. BFS用的是queue, 因為用了座標
所以用queue< pair<int,int> >來紀錄加入queue的座標

3. 從(0,0)開始做BFS
當queue不為空的時候
-> pop單元
-> 針對pop的動作處理BFS //加入新的單元
//新增step or cost

4. Visited是必須的
產生一個vec<vec<bool>>visited來紀錄已經處理過的單元
以免單元間產生無窮迴圈
->
當沒有visited
符合題目條件
則處理
BFS //push

====
class Solution{

public:
vec<vec<int>> updateMatrix( vec<vec<int>>&matrix ){
  int m= matrix.size()
  int n= matrix[0].size()
  queue<pair<int,int>> pq

  for i-m
    for j-n
      if(matrix(i,j) == 0){
      //先從'0'開始 因為第一次與零的距離是“0”
      //之後逐漸加一
        pq.push({i-1, j});
        pq.push({i+1, j});
        pq.push({i, j-1});
        pq.push({i,j-1});
        //上下左右加入queue //所有第一層的node
      }

  int step=0
  vec<vec<bool>> visited(m, vec<bool>(n, false) )
  while( !pq.empty() ){
  step++
  int size= pq.size() //紀錄當前的queue size來控制當次需要做多少次
  for(i=0; i<size; i++){
    auto Front= pq.front()
    int x=Front.first
    int y=Front.second
    pq.pop()

    if(x>=0&& y>=0&& x<m&& y<n&& !visited(x,y)&& matrix(x,y)==1){
      //開始找‘1’,因為開始要填1到0的距離
      visited(x,y)=true
      matrix(x,y)=step
      //因為拜訪過了 且當前的距離已經填入
      pq.push({x-1, j});
      pq.push({x+1, j});
      pq.push({i, y-1});
      pq.push({i,y-1});
      //第n次層的上下左右 加入queue

    }//if
  }//for
  }//while

}
};

2022年2月2日 星期三

Then it's baby's turn to be crazy.

- She's pregnant.
She gets to be crazy. You have to be the sane one.

- So what, I don't get to be crazy again til the baby comes?

- No. Then it's baby's turn to be crazy.

- Then when do I get to be crazy again?

- Never.

<How I Met Your Mother, s07ep21, Now We're Even>

Every time I go out and have an awesome night and you're not there, that's another hundred points in my column

I'm talking about the game of life, Ted.
Scoreboard. Better wardrobe: 75 points.
Apartment on a higher floor: 90 points.
Longer name: 110 points.

Every time I go out and have an awesome night and you're not there,
that's another hundred points in my column.

So, yeah, I'm in the lead.
<How I Met Your Mother, s07ep21, Now We're Even>

2022年2月1日 星期二

Top10 things Marshall said on his wedding night

Marshall: It's hard for the little guy to perform under pressure.
Barney: "Top10 things Marshall said on his wedding night."

Kevin: Whoa! It was small, but I think I felt something.
Robin: "Top10 things Lily said on her wedding night."

Marshall: Stop laughing at it, Lily.
(All): Top10 things Marshall said on his wedding night!!

<How I Met Your Mother, s07ep16, The Drunk Train>

Oh, God. This is parenthood, isn't it?

Every 30 seconds, there's another crisis that needs to be dealt with, and I have to deal with it.
This is..

Oh, God. This is parenthood, isn't it?
<How I Met Your Mother s07ep15, The Burning Beekeeper>

802, Find eventual safe states

====
802, Find eventual safe states
====
DFS,
cycle find

有向/無向圖找cycle不一樣
有向需要三個顏色(還沒看過/ 還在看/ 已經看完
無向需要兩個顏色(visited/ un-visited

過程中有碰到訪問過/看過的node
就是有cycle

====
a)
1. 題目給予一個graph
定義一個int dp(n, 0)初值

0, 還沒看過
-1,還在看
1,已經看完

2. DFS一進去
先將dp[i]=-1 //還在看
然後針對graph[i][]一一DFS檢查
如果有false則return false

3. 如果graph[i][]跑完 都沒看到cycle 
//如果沒有看到dp==-1, i.e還在看

則dp[i]=1, i.e已經看完
return true
else
return dp[i]==1

4. main一個for跑DFS
如果false,就不加入結果
否則是安全的,加入

====
class Solution{

public:
bool DFS( vec<vec<int>>&graph, vec<int>&dp, int i){
  if ( dp[i] ) //-1 ->false, 1 ->true
    return dp[i]==1

  dp[i]=-1
  for auto t:graph[i].begin; t!=graph[i].end; t++
    if ( !DFS( graph, dp, *t ) )
      return false

  dp[i]=1
  return true;
}
vec<int> eventualSafe( vec<vec<int>>&graph ){
  int n=graph.size()
  vec<int> res
  vec<int> dp(n, 0)
  for i-n
    if( DFS( graph, dp, i )
      res.push_back(i)

  return res
}
}

733, Flood Fill

====
733, Flood Fill
====
DFS,
from each un-visited node/island problems

====
1. 題目給予一個vec<vec<int>>M
一個起始的座標
以及變更的新數值

2. 定義一個vec<vec<bool>>visited
DFS( M, visited, i, j, m, n, new )走過上下左右的同值/同於起始 的所有node

====
class Solution{

void DFS( vec<vec<int>>& matrix, vec<vec<bool>>&visited, int i, int j, int m, int n, int old, int new){
  if( i<0 || j<0 || i>=m || j>=n || matrix(i,j)!=old || visited(i,j) ) return;

  visited(i,j)= true;
  matrix(i,j)= new;
  DFS( matrix, visited, i-1, j, m, n, matrix(i,j), new)
  DFS i+1
  DFS j-1
  DFS j+1

}

vec<vec<int>> FloodFill( vec<vec<int>>& matrix, int x, int y, int newColor ){
  int m= matirx.size(), n= matrix[0].size()
  vec<vec<bool>> visited= (m, false)

  if matrix(x,y)!=newColor
    DFS( matrix, visited, x, y, matrix(x,y), newColor)

  return matrix
}