# Oracle

## 554. Brick Wall

Draw a vertical line from the **top** to the **bottom** and cross the **least** bricks.

```
Example:

Input: [[1,2,2,1],
        [3,1,2],
        [1,3,2],
        [2,4],
        [3,1,2],
        [1,3,1,1]]

Output: 2
```

![](https://2249014260-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LrHBOmnlXe2UPlScnAC%2F-M2NB4MsZYMH-MP9OTVR%2F-M2PQLSoFpt0rQQElehQ%2Fbrick_wall.png?alt=media\&token=ab97fe9f-9c18-4ff3-aeaa-3e3b7aca238c)

```python
"""
Brute Force
用一條掃描線掃過sum的數字，記錄每個點會穿過多少brick
idxes list 紀錄每個wall idx, wall_sum list 紀錄每個wall 當前的sum
O(n*w) n is sum number, w in # of walls
"""

class Solution:
    def leastBricks(self, wall: List[List[int]]) -> int:
        n = len(wall)
        ans = n
        size = sum(wall[0])
        
        wall_sum = [0] * n
        idxes = [0] * n
        
        for num in range(1, size):
            for i, w in enumerate(wall_sum):
                if w < num:
                    wall_sum[i] += wall[i][idxes[i]]
                    idxes[i]+=1
                    
            count = 0
            for w in wall_sum:
                if w > num:
                    count +=1
            ans = min(ans, count)  
        return ans
```

```python
"""
HashMap
Use HashMap 紀錄每個wall brick的right edge跟起始的距離，最後一個不用加上
# of walls - right edge = # of 穿越數
O(b) b is # of total bricks
"""
from collections import defaultdict
class Solution:
    def leastBricks(self, wall: List[List[int]]) -> int:
        n = len(wall)
        dic = defaultdict(int)
        
        for w in wall:
            sumUp = 0
            for b in w[:-1]:
                sumUp+=b
                dic[sumUp] += 1
                
        ans = n
        for v in dic.values():
            ans = min(ans, n-v)
            
        return ans
```

## 403. Frog Jump

Given a list of stones' positions (in units) in sorted ascending order, determine if the frog is able to cross the river by landing on the last stone. Initially, the frog is on the first stone and assume the first jump must be 1 unit. If the frog's last jump was k units, then its next jump must be either k - 1, k, or k + 1 units

```python
[0,1,3,5,6,8,12,17] -> True
[0,1,2,3,4,8,9,11] -> False
```

```python
"""
DFS with Memorization
T: O(n^3)
S: O(n^2)
"""
def canCross(self, stones: List[int]) -> bool:
        n = len(stones)
        dp = [[-1]*n for _ in range(n)] 
        return self.helper(0, 0, dp, stones) == 1
     
def helper(self, idx, jump, dp, stones):
    n = len(stones)
    if idx == n-1: return 1
    
    if dp[idx][jump] >= 0:
        return dp[idx][jump]
    
    for i in range(idx+1, n):
        gap = stones[i] - stones[idx]
        if abs(jump - gap) <= 1:
            if self.helper(i, gap, dp, stones) == 1:
                dp[idx][gap] = 1
                return 1
    
    dp[idx][jump] = 0
    return 0
```

```python
"""
DP
用set紀錄jump過來的step
T: O(n^3)
S: O(n^2)
"""

def canCross(self, stones: List[int]) -> bool:
    d = {}
    for s in stones:
        d[s] = set()
    d[0].add(0)
    
    for s in stones:
        for k in d[s]:
            for step in range(k-1, k+2):
                if step > 0 and (s + step) in d:
                    d[s+step].add(step)
    
    return len(d[stones[-1]]) != 0
```

