Skip to content

Instantly share code, notes, and snippets.

@junjiah
junjiah / paren_gen.py
Created August 27, 2015 06:16
solved 'Generate Parentheses' on LeetCode https://leetcode.com/problems/generate-parentheses/
class Solution(object):
def generateParenthesis(self, n):
"""
:type n: int
:rtype: List[str]
"""
self.valid_parens = []
self.n = n
# 0 open, 0 closed, recursively build valid strings.
self.buildParenString(0, 0, '')
@junjiah
junjiah / candies.py
Last active August 27, 2015 08:46
solved 'Candies' on hackerrank https://www.hackerrank.com/challenges/candies
import sys
def find_min_candies(ratings):
"""ratings: A list of ratings"""
sz = len(ratings)
# Records number of continuous elements less than
# the current rating from left and right.
less_from_left, less_from_right = [0] * sz, [0] * sz
for i in range(1, sz):
def max_score(bricks):
"""bricks: A list of bricks."""
sz = len(bricks)
# DP algorithm. See the editorial.
# https://www.hackerrank.com/challenges/play-game/editorial
max_score_from = [0] * sz
max_score_from[-1] = bricks[-1]
max_score_from[-2] = max_score_from[-1] + bricks[-2]
max_score_from[-3] = max_score_from[-2] + bricks[-3]
@junjiah
junjiah / same_tree.cpp
Last active August 29, 2015 13:57
solved "Same Tree" and "Symmetric Tree" on LeetCode (same idea) http://oj.leetcode.com/problems/same-tree/ http://oj.leetcode.com/problems/symmetric-tree/
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
class Solution {
public:
void merge(int A[], int m, int B[], int n) {
vector<int> sorted;
int i=0, j=0;
while (i < m && j < n) {
if (A[i] < B[j])
sorted.push_back(A[i++]);
else
sorted.push_back(B[j++]);
@junjiah
junjiah / restore_ip.cpp
Last active August 29, 2015 13:57
solved "Restore IP Addresses" on LeetCode (my algorithm using python is brute and bad, the cpp version is better) http://oj.leetcode.com/problems/restore-ip-addresses/
/****
* from leetcode discussion on http://oj.leetcode.com/discuss/77/restore-ip-addresses?show=295#a295
* answered by mitchelllc
* using 3 for loops
****/
vector<string> restoreIpAddress(string s) {
vector<string> res;
if (s.size() > 12 || s.size() < 4)
return res;
/**
* AC in one try!
**/
class Solution {
public:
vector<vector<string>> partition(string s) {
str = s;
result.clear();
sz = s.size();
class Solution:
# @param num, a list of integer
# @return an integer
def init(self):
self.endDict = {}
def combine(self, left, right):
leftEnd = self.endDict[left]
rightEnd = self.endDict[right]
self.endDict[left], self.endDict[right] = left, right
@junjiah
junjiah / pow.cpp
Created March 24, 2014 01:50
solved 'Pow(x, n)' on LeetCode http://oj.leetcode.com/problems/powx-n/
class Solution {
public:
double pow(double x, int n) {
double result = 1;
if (n < 0) {
x = 1/x;
result *= x;
n = -(n+1);
}
if (n == 0)
@junjiah
junjiah / btree_max_pathsum.java
Last active August 29, 2015 13:57
solved 'Binary Tree Maximum Path Sum' on LeetCode (C++ before, Java now) http://oj.leetcode.com/problems/binary-tree-maximum-path-sum/
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {