Skip to content

Instantly share code, notes, and snippets.

@junjiah
junjiah / WaterContainer.java
Last active December 30, 2015 17:19
solved 'Container With Most Water ' on LeetCode (ONE AC lol) http://oj.leetcode.com/problems/container-with-most-water/ [better O(n) algorithm is also attached - from Web]
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* Created with IntelliJ IDEA.
* User: EDFward
* Date: 12/8/13
* Time: 9:56 PM
"""
(C) Mathieu Blondel - 2010
License: BSD 3 clause
Implementation of the collapsed Gibbs sampler for
Latent Dirichlet Allocation, as described in
Finding scientifc topics (Griffiths and Steyvers)
"""
@junjiah
junjiah / ReorderList.java
Last active December 29, 2015 13:39
solved 'Reorder List' on LeetCode (ONE AC! oh yeah;) http://oj.leetcode.com/problems/reorder-list/
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
@junjiah
junjiah / interleaving_string.cpp
Last active December 29, 2015 08:08
solved 'Interleaving String' on LeetCode (6 submissions, fxxk!) http://oj.leetcode.com/problems/interleaving-string/
/*
1. first tried backtracking, TLE
2. 2-dimension DP, accepted. state transition is easy to derive
*/
class Solution {
public:
bool isInterleave(string s1, string s2, string s3) {
m = s1.size();
n = s2.size();
if (s3.size() != m+n) return 0;
/*
1. first using DP, O(n^2), TLE.
2. then try spliting arrays to intervals by
taking the maximum reach as the boundary.
*/
class Solution {
public:
int jump(int A[], int n) {
if (A[0] == 0 || n == 1) return 0;
int iter = 1, intervalEnd = A[0], res = 1;
@junjiah
junjiah / 0_reuse_code.js
Created November 17, 2013 04:10
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
import java.util.HashMap;
import java.util.Map;
/**
* Created with IntelliJ IDEA.
* User: EDFward
* Date: 11/17/13
* Time: 12:52 AM
* Happy coding!
*/
@junjiah
junjiah / fn_extract_cause.py
Last active December 22, 2015 23:49
extract causation frame's Cause and Effect role from FrameNet annotated corpus
from lxml import etree as ET
from itertools import chain
import sys
if len(sys.argv) > 1:
f = open(sys.argv[1])
else:
f = open('x.xml') # default xml file
# first, remove namespace
@junjiah
junjiah / lxml_tutorial.py
Last active December 22, 2015 23:49
A brief tutorial to lxml module
from lxml import etree
# parse
parser = etree.XMLParser(ns_clean=True) # support other arguments
tree = etree.parse(some_file_like_obj)
# could be file name/path, file-like object, http/ftp url. but
# name/path and url are faster. or from string as following
tree = etree.fromstring(string, parser)
tree = etree.XML('<root><a><b/></a></root>')