Skip to content

Instantly share code, notes, and snippets.

@m00nlight
m00nlight / target_sum.kt
Last active June 22, 2021 19:02
Leetcode Target sum DP solution in Kotlin
import kotlin.math.abs
// https://leetcode.com/problems/target-sum/
// dp function: dp[i][j] = dp[i - 1][j - nums[i]] + dp[i - 1][j + nums[i]]
class TargetSum {
fun findTargetSumWays(nums: IntArray, target: Int): Int {
val dp = Array(nums.size + 1) { mutableMapOf<Int, Int>().withDefault { 0 } }
dp[0][0] = 1
for (i in 1..nums.size) {
@m00nlight
m00nlight / banan.bmp
Last active September 14, 2016 19:28 — forked from skuro/nonograms-dojo.clj
(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1)
(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1)
(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1 1)
(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1 1)
(1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 0 1 1 1 0)
(0 0 0 1 1 0 1 1 1 1 1 1 1 1 0 1 1 1 1 1)
(1 1 1 1 1 1 0 0 0 0 0 1 0 1 1 1 1 1 1 1)
(1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1)
(0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1)
(1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0)
@m00nlight
m00nlight / table.md
Created January 29, 2016 08:48
test for markdown

Input

#+begin_src

.NET Data type F# Declaration
Int let i = 0 or let i = 0l
Uint let i = 1u or let i = 1ul
Decimal let d = 1m or let d = 1M
Short let c = 2s
Long let l = 5L
@m00nlight
m00nlight / binary_search.py
Last active September 23, 2015 02:11
various binary search algorithm including lower_bound upper_bound in python
from __future__ import division
def binary_search(xs, target):
"""
type : Listof[Val] * Val -> Int
input : xs :: Sorted list
target :: Search target
desp : Return the index of the element in the list, -1 if the
element is not in the list
@m00nlight
m00nlight / list.py
Created August 28, 2015 05:25
Python list function
class Solution(object):
def addTwoNumbers(self, l1, l2):
ll1 = self.list2naive(l1)
ll2 = self.list2naive(l2)
a = 0 if ll1 == [] else int(''.join(map(str, ll1))[::-1])
b = 0 if ll2 == [] else int(''.join(map(str, ll2))[::-1])
res = a + b
return self.naive2list(map(int, list(str(res)[::-1])))
def list2naive(self, l):
@m00nlight
m00nlight / gist:51171c9ba28b979fffd2
Created July 6, 2015 09:38
Ruby decorator pattern
module Decorator
def initialize(decorated)
@decorated = decorated
end
def method_missing(method, *args)
args.empty? ? @decorated.send(method) : @decorated.send(method, args)
end
end
@m00nlight
m00nlight / gist:ddb333a6d6b10a5bac38
Created June 28, 2015 09:32
Basic python graph algorithm library
from __future__ import division
from collections import defaultdict, deque
from heapq import heappush, heappop
from sys import stdin
class UnionFindSet:
def __init__(self, nodes):
self.fa = {}
for n in nodes:
self.fa[n] = n
@m00nlight
m00nlight / gist:5b6eeebc28ab15a35b10
Last active September 27, 2022 23:24
Haskell segment tree with lazy propagation
import Control.Applicative
import Control.Monad
import qualified Data.ByteString.Char8 as BS
import Data.List
import Data.Maybe
import qualified Data.Vector as V
data SegTree a =
Node {
val :: a
@m00nlight
m00nlight / gist:cf89e14d93ed69c204f8
Last active June 30, 2019 06:10
python binary index tree
from __future__ import division
class BinaryIndexTree:
def __init__(self, n):
self.sz = n
self.vals = [0] * (n + 1)
def update(self, idx, delta):
"add c to the value at index idx"
@m00nlight
m00nlight / gist:4c5d3e44f38298714092
Created April 13, 2015 03:19
The while language parser in haskell
import System.IO
import Control.Monad
import Text.ParserCombinators.Parsec
import Text.ParserCombinators.Parsec.Expr
import Text.ParserCombinators.Parsec.Language
import qualified Text.ParserCombinators.Parsec.Token as Token
data BExpr = BoolConst Bool
| Not BExpr