Skip to content

Instantly share code, notes, and snippets.

View munguial's full-sized avatar

Alberto Munguia munguial

View GitHub Profile
@munguial
munguial / Solution.py
Created April 18, 2020 01:13
Day 17 - Number Of Islands
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
result = 0
for row in range(len(grid)):
for column in range(len(grid[0])):
if grid[row][column] == '1':
result += 1
self.dfs(grid, row, column)
return result
@munguial
munguial / Solution.py
Created April 18, 2020 18:09
Day 18 - Minimum Path Sum
class Solution:
def minPathSum(self, grid: List[List[int]]) -> int:
dp = [[0 for col in range(len(grid[0]))] for row in range(len(grid))]
lastCol = len(grid[0]) - 1
lastRow = len(grid) - 1
dp[lastRow][lastCol] = grid[lastRow][lastCol]
# Populate the right-most column
for row in reversed(range(len(grid) - 1)):
@munguial
munguial / Solution.kt
Last active April 20, 2020 04:53
Day 19 - Search in Rotated Sorted Array
// https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/530/week-3/3304/
class Solution {
fun search(nums: IntArray, target: Int): Int {
return bs(nums, target, 0, nums.size - 1)
}
fun bs(nums: IntArray, target: Int, a: Int, b: Int): Int {
if (a > b) {
return -1
@munguial
munguial / Solution.kt
Created April 20, 2020 08:10
Day 20 - Construct Binary Search Tree from Preorder Traversal
// https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/530/week-3/3305/
/**
* Example:
* var ti = TreeNode(5)
* var v = ti.`val`
* Definition for a binary tree node.
* class TreeNode(var `val`: Int) {
* var left: TreeNode? = null
* var right: TreeNode? = null
@munguial
munguial / Solution.kt
Created April 22, 2020 05:17
Day 21 - Leftmost column with at least a one
// https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/530/week-3/3306/
class Solution {
fun leftMostColumnWithOne(binaryMatrix:BinaryMatrix):Int {
var result = 100
for (row in 0 until binaryMatrix.dimensions()[0]) {
result = minOf(result, bs(binaryMatrix, row))
}
return if (result == 100) -1 else result
}
@munguial
munguial / Solution.kt
Created April 23, 2020 05:55
Day 22 - Subarray Sum Equals K
// https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/531/week-4/3307/
class Solution {
fun subarraySum(nums: IntArray, k: Int): Int {
val map = mutableMapOf<Int, Int>().withDefault { 0 }
map[0] = 1
var currSum = 0
var result = 0
nums.forEach {
@munguial
munguial / Solution.kt
Created April 24, 2020 05:01
Day 23 - Bitwise AND of Numbers Range
# https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/531/week-4/3308/
class Solution {
fun rangeBitwiseAnd(m: Int, n: Int): Int {
var mShifts = 0
var nShifts = 0
var M = m
var N = n
while (M > 0) {
@munguial
munguial / Solution.kt
Created April 25, 2020 06:10
Day 24 - LRU Cache
// https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/531/week-4/3309/
class LRUCache(capacity: Int) {
var map: HashMap<Int, ListNode>
var head: ListNode
var tail: ListNode
var currSize: Int
val maxCapacity: Int
init {
@munguial
munguial / Solution.kt
Last active April 25, 2020 19:15
Day 25 - Jump Game
// https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/531/week-4/3310/
class Solution {
fun canJump(nums: IntArray): Boolean {
var aux = BooleanArray(nums.size)
aux[aux.size - 1] = true
for (i in nums.size - 1 downTo 0) {
if (aux[i]) {
for (j in i - 1 downTo 0) {
@munguial
munguial / Solution.kt
Created April 27, 2020 00:07
Day 26 - Longest Common Subsequence
// https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/531/week-4/3311/
class Solution {
fun longestCommonSubsequence(text1: String, text2: String): Int {
val dp = Array(text1.length + 1, {IntArray(text2.length + 1)})
for (i in 1..text1.length) {
for (j in 1..text2.length) {
dp[i][j] = when {
text1.get(i - 1) == text2.get(j - 1) -> dp[i - 1][j - 1] + 1