2849. Determine if a Cell Is Reachable at a Given Time

T: O(1)
S: O(1)
```java
class Solution {
    public boolean isReachableAtTime(int sx, int sy, int fx, int fy, int t) {
        if (sx == fx && sy == fy && t == 1) { // can't go back to original point
            return false;
        }

        int diffX = Math.abs(sx - fx);
        int diffY = Math.abs(sy - fy);

        int minDiag = Math.min(diffX, diffY); 
        int moveStep = Math.abs(diffX - diffY);

        int needTime = minDiag + moveStep;
        if (needTime <= t) {
            return true;
        }
        return false;
    }
}

/**
T: O(1)
S: O(1)

只有 sx == fx && sy == fy && t == 1
因為點重合, 題目又說 ""exactly"" time t to arrive
所以怎樣 t == 1 都無法回到出發點

如果是 >=2 都可以再走回來, 來回來回重複走之類的


同理, 如果 max(x座標差, y座標差), 也就是 “到達目的地的所需時間” <= t
代表都可以 “來回來回重複走(走直線或對角線的可以)” 在剛好 t time時到達

0,0 t==3
  0
可以對角線走


why max(x座標差, y座標差)? 就是 “到達目的地的所需時間”?
如果要快點到, “我們盡可能的走對角線”
ex: 

sx = 3, sy = 1, fx = 7, fy = 3 

min(diff x, diff y) -> 就是對角線可以走的最多步數
= min(abs(3-7), abs(1-3)) = min(4, 2) = 2

so 
sx = 3+2 = 5
sy = 1+2 = 3

then x move 2 (actually diff x - diff y = 4-2) -> arrive fx = 7, fy = 3 

所以 走 4 步 -> 也就是 max(diff x, diff y)

    x 
    1 2 3 4 5 6 7
  3             f (7,3)
  2.     /    
  1     s (3,1)
  y      x - dist = 4, y dist = 2 (x diff > y diff)

     | 2
-----|
  4

  so 斜對角先走 2, 在走 x 軸 2步

為什麼斜對角 只能走2步?因為多走就超過目的地了對吧
     (7,3)

(3,1)

-> 

     
(5,3). (7,3)


## 如果不知道這其實可以求 max (diff x, diff y)

the 走 x 2步 -> 到達(2 步, 其實就是 xdiff - ydiff) -> why?
    2
   --
 /2  |
/    | 2
-2---
  4
  你看鞋對角線...上升y = 2, 右移x=2 => (5,3) 跟 (7,3) 差2, 其實就是平移就會是 xdiff - ydiff 因為 xdiff 大所以可以對角線移動到同高度
  然後剩下的就是x平移 => key: ""但因為對角線移動高度已經用掉 ydiff, 所以 x平移就是 xdiff - ydiff""





 */
```

Last updated