Skip to content

Instantly share code, notes, and snippets.

@selimslab
Last active May 21, 2020 22:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save selimslab/92f81d60aa59a6a0618901af5968f0c1 to your computer and use it in GitHub Desktop.
Save selimslab/92f81d60aa59a6a0618901af5968f0c1 to your computer and use it in GitHub Desktop.
def climb_stairs(n: int) -> int:
memo = {1:1, 2:2}
def climb(n):
if n not in memo:
memo[n] = climb(n-1) + climb(n-2)
return memo.get(n)
return climb(n)
func climbStairs(n int) int {
memo := map[int]int{
1:1,
2:2,
}
var climb func(n int) int
climb = func (n int) int {
_, ok := memo[n];
if !ok {
memo[n] = climb(n-1) + climb(n-2)
}
return memo[n]
}
return climb(n)
}
let climbStairs = function(n) {
memo = {1:1,2:2}
function climb(n){
if ( !(n in memo)){
memo[n] = climb(n-1) + climb(n-2)
}
return memo[n]
}
return climb(n)
};
"""
Given a 2D binary matrix filled with 0's and 1's,
find the largest square containing only 1's and return its area.
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
Output: 4
"""
def maximalSquare(self, matrix: List[List[str]]) -> int:
rows = len(matrix)
if rows:
cols = len(matrix[0])
else:
cols = 0
dp = [0] * (cols+1)
maxsq = 0
prev = 0
for i in range(1,rows+1):
for j in range(1,cols+1):
temp = dp[j]
if matrix[i-1][j-1] == "1":
min_prev = min(dp[j-1], prev)
min_cur = min(min_prev, dp[j])
dp[j] = min_cur + 1
maxsq = max(maxsq,dp[j])
else:
dp[j] = 0
prev = temp
return maxsq*maxsq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment