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
Last active April 8, 2020 07:13
Day 8 - Middle of the linked list
# https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/529/week-2/3290/
class Solution:
def middleNode(self, head: ListNode) -> ListNode:
slow = head
fast = head
while fast:
fast = fast.next
if not fast:
break
@munguial
munguial / Solution.py
Created April 10, 2020 05:03
Day 9 - Backspace String Compare
# https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/529/week-2/3291/
class Solution:
def backspaceCompare(self, S: str, T: str) -> bool:
a = len(S) - 1
b = len(T) - 1
skipA = 0
skipB = 0
while a >= 0 or b >= 0:
@munguial
munguial / Solution.py
Created April 11, 2020 01:59
Day 10 - Min Stack
# https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/529/week-2/3292/
class MinStack:
def __init__(self):
self.stack = []
self.minStack = []
def push(self, x: int) -> None:
@munguial
munguial / Solution.py
Last active April 11, 2020 17:20
Day 11 - Diameter of binary tree
# https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/529/week-2/3293/
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Result:
@munguial
munguial / Solution.kt
Last active April 12, 2020 20:21
Day 12 - Last Stone Weight
# https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/529/week-2/3297/
import java.util.Collections
import java.util.PriorityQueue
class Solution {
fun lastStoneWeight(stones: IntArray): Int {
val pq = PriorityQueue<Int>(Collections.reverseOrder())
for (s in stones) {
pq.add(s)
}
@munguial
munguial / Solution.py
Created April 14, 2020 07:11
Day 13 - Contiguous Array
class Solution:
def findMaxLength(self, nums: List[int]) -> int:
history = {0: -1}
balance = 0
res = 0
for i in range(len(nums)):
balance += 1 if nums[i] else -1
if balance in history:
@munguial
munguial / Solution.kt
Last active April 15, 2020 01:54
Day 14 - Perform String Shifts
// https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/529/week-2/3299/
class Solution {
fun stringShift(s: String, shift: Array<IntArray>): String {
val singleShift: Pair<Int, Int> = calculateSingleShift(shift)
return shiftCharacters(s, singleShift.first, singleShift.second % s.length)
}
fun shiftCharacters(s: String, direction: Int, n: Int): String {
if (n == 0) {
@munguial
munguial / Solution.kt
Created April 15, 2020 15:18
Day 15 - Product Of Array Except Self
// https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/530/week-3/3300/
class Solution {
fun productExceptSelf(nums: IntArray): IntArray {
var output = nums.copyOf()
for (i in nums.lastIndex - 1 downTo 0) {
output[i] *= output[i + 1]
}
@munguial
munguial / Solution.py
Created April 16, 2020 18:57
Day 16 - Valid Parenthesis String
# https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/530/week-3/3301/
class Solution:
def checkValidString(self, s: str) -> bool:
dp = {}
return self.isValid(s, 0, 0, dp)
def isValid(self, s: str, i: int, c: int, dp: dict) -> bool:
if (i, c) in dp:
return dp[i, c]
@munguial
munguial / Solution.kt
Created April 18, 2020 01:04
Day 17 - Number of Islands
// https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/530/week-3/3302/
class Solution {
fun numIslands(grid: Array<CharArray>): Int {
var result = 0
for (row in grid.indices) {
for (col in grid[0].indices) {
if (grid[row][col] == '1') {
result += 1
dfs(grid, row, col)
}