Code Interview Note
  • 0. Introduction
  • 1. Basic
    • Python Basic
    • Java Basic
    • Primitive Type
    • Basic Question
    • Number
  • 2. Array and Numbers
    • General
    • TwoSum
    • Buy and Sell Stock
    • SubArray
      • SubArray + HashMap
    • Sliding Window
      • Sliding Window At Most Problem
    • Word Break
    • Passes Problem
    • Majority Element
    • Partition Array
    • Sort Colors
    • Anagram
    • Ugly Number
    • TwoPointer
    • Swipe Line
    • Calculator
    • Sudoku
  • 2.1 String
    • String
    • Palindrome
    • Parentheses
    • Decode String
    • Calculator
    • Abbreviation
  • 3. Linkedlist
    • Dummy Node
    • Double Pointers
  • 4. Stack and Queue
    • General
    • Increase/Decrease Stack
  • 5. Binary Search
    • General
    • BS on result
    • Save the half which has result
    • Rotated Sorted Array
    • Split Array Largest Sum
  • 6. Binary Tree
    • General
    • Path Sum
    • Lowest Common Ancestor
    • BST
    • Convert
    • Traverse
    • Valid Ordered Tree
    • Construct Binary Tree
    • Tree depth and width
    • Vertical Order Traverse
  • 7. Heap
    • Geneal
    • TopK
  • 8. Simulation
    • General
    • Read4
    • Encode Decode
    • LRU/LFU
    • Robot
    • GetRandom O(1)
    • Probability
  • 9. DFS
    • Backtrack
    • General
    • Subset
    • Permutation
    • Combination
  • 10. HashTable
    • General
  • 11. Sort
    • General
  • 12. Recursion
    • General
  • 13. Dynamic Programming
    • Graph
    • General
    • Coordinate
    • Double Sequence
    • Longest Common Subsequence
    • Rolling Array
    • House Robber
    • Backpack
    • Memorization
    • Diagonal
  • 14. BFS
    • General
    • Number of Islands
    • The Maze
  • 15. Graph
    • Shortest Path
    • Undirected Graph
    • Topology Sort
    • Word Ladder
    • Tarjan's Algo
  • 16. Divide & Conquer
    • General
  • 17. UnionFind
    • General
    • Grouping
  • 18. Trie
    • General
    • Word Square
  • 19. Company Summary
    • Oracle
    • Amazon
      • DP
    • Google
    • Hackerrank
    • LinkedIn
  • 20. Design
  • 21. Math
  • Behavior Question
  • Internet
  • OS
Powered by GitBook
On this page
  • 554. Brick Wall
  • 403. Frog Jump

Was this helpful?

  1. 19. Company Summary

Oracle

Previous19. Company SummaryNextAmazon

Last updated 4 years ago

Was this helpful?

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
"""
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
"""
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

[0,1,3,5,6,8,12,17] -> True
[0,1,2,3,4,8,9,11] -> False
"""
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
"""
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