This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |