Skip to content

Instantly share code, notes, and snippets.

View phongngtuan's full-sized avatar

Phong Nguyen phongngtuan

View GitHub Profile
class TrieNode:
def __init__(self, c: str, path: str):
self.c = c # type: str
self.counter = 0 # type: str
self.path = path # type: str
self.children = dict() # type: Dict[str, TrieNode]
def add(self, key: str, path: str):
if len(key) == 0:
self.counter += 1
class Solution:
def generateMatrix(self, n: int) -> List[List[int]]:
if n == 1:
return [[1]]
mat = [[0 for i in range(n)] for j in range(n)]
counter = 1
for i in range(0, n):
x, y = i, i
width = n - i * 2
height = width - 2
import heapq
class Solution:
def twoCitySchedCost(self, costs: List[List[int]]) -> int:
heap = []
for cost in costs:
heapq.heappush(heap, (-abs(cost[0] - cost[1]), cost[0], cost[1]))
N = len(costs) / 2
total = 0
@phongngtuan
phongngtuan / binary-tree-pruning.py
Created September 16, 2019 15:23
binary-tree-pruning.py
class Solution:
def pruneTree(self, root: TreeNode) -> TreeNode:
if root == None:
return None
left = self.pruneTree(root.left)
right = self.pruneTree(root.right)
if root.val == 1 or left or right: # not prunable
root = TreeNode(root.val)
root.left = left
root.right = right
import cats._
import cats.implicits._
import cats.effect._
import scala.concurrent.duration._
object ProcessIO {
import scala.sys.process._
def exec[F[_]](commands: String)(implicit F: ConcurrentEffect[F], T: Timer[F]): F[Unit] = {
def acquire: F[Process] = F.delay(commands.run())
val doobieVersion = "0.6.0"
import $ivy.`org.tpolecat:doobie-core_2.12:0.6.0`
import $ivy.`org.tpolecat:doobie-postgres_2.12:0.6.0`
import $ivy.`org.tpolecat:doobie-specs2_2.12:0.6.0`
import doobie._
import doobie.implicits._
import cats._
import cats.data._
import cats.effect.IO
import cats.implicits._
public ListNode reverseList(ListNode head) {
if (head == null) return head;
ListNode curr = head.next;
head.next = null;
while (curr != null) {
ListNode next = curr.next;
curr.next = head;
head = curr;
curr = next;
@phongngtuan
phongngtuan / day-2.scala
Created October 4, 2018 13:07 — forked from calvinlfer/day-2.scala
Functional Scala: Toronto Edition
// Copyright(C) 2018 - John A. De Goes. All rights reserved.
package net.degoes.abstractions
import scalaz._
import Scalaz._
import scala.language.higherKinds
object algebra {
@phongngtuan
phongngtuan / day-1.scala
Created October 4, 2018 13:07 — forked from calvinlfer/day-1.scala
Functional Scala: Toronto edition
// Copyright(C) 2018 - John A. De Goes. All rights reserved.
package net.degoes.essentials
import java.time.{Instant, ZonedDateTime}
import scala.util.Try
object types {
type ??? = Nothing
@phongngtuan
phongngtuan / day-3.scala
Created October 4, 2018 13:06 — forked from calvinlfer/day-3.scala
Functional Scala: Toronto edition
// Copyright(C) 2018 - John A. De Goes. All rights reserved.
package net.degoes.effects
import scalaz.zio._
import scalaz.zio.console._
import scala.annotation.tailrec
import scala.concurrent.duration._