Skip to content

Instantly share code, notes, and snippets.

@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):
@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, '')
#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>
using namespace std;
// From https://leetcode.com/discuss/36807/c-8-ms-kmp-based-o-n-time-%26-o-n-memory-solution?show=40650#a40650
// O(n^2), 4ms.
// May fail test cases such as "abababababababababababababababababababababab".
# Do binary search for each row. O(mlgn).
# Run time: 224 ms
class Solution:
# @param {integer[][]} matrix
# @param {integer} target
# @return {boolean}
def searchMatrix(self, matrix, target):
m, n = len(matrix), len(matrix[0])
# Edge case.
if m == 0 or n == 0:
# Maintain a deque where the first element is the max
# of the window.
class Solution:
# @param {integer[]} nums
# @param {integer} k
# @return {integer[]}
def maxSlidingWindow(self, nums, k):
results = []
# Element in the window should be (index, val).
window = collections.deque()
@junjiah
junjiah / ways_to_compute.cc
Created August 16, 2015 00:41
solved 'Different Ways to Add Parentheses' on LeetCode https://leetcode.com/problems/different-ways-to-add-parentheses/
// Simple recursive algorithm. Didn't use DP considering that
// it needs to store a list for every DP entry (n * n).
class Solution {
public:
vector<int> diffWaysToCompute(string input) {
tokens_ = parse(input);
return diffWaysToCompute(0, tokens_.size());
}
private:
@junjiah
junjiah / remove_nth_one_pass.cc
Last active August 29, 2015 14:26
solved 'Remove Nth Node From End of List' on LeetCode https://leetcode.com/problems/remove-nth-node-from-end-of-list/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
// Very clever algorithm, from
@junjiah
junjiah / wormhole.cc
Last active August 29, 2015 14:26
POJ 3259 - Wormholes: Bellman-Form algorithm to detect negative cycles
#include <iostream>
#include <vector>
using namespace std;
// Given the info from the problem statement.
const int kInf = 10001;
struct Edge {
int src_;
@junjiah
junjiah / gift_min_price.cc
Last active August 29, 2015 14:26
POJ 1062 - 昂贵的聘礼: typical Dijkstra algorithm problem
#include <cstdio>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
/*
Note all operations on arrays/vectors start from index 1.
*/
@junjiah
junjiah / rob_ii.cc
Last active August 29, 2015 14:25
solved 'House Robber II' on leetcode https://leetcode.com/problems/house-robber-ii/
class Solution {
public:
int rob(vector<int>& nums) {
int nums_len = nums.size();
if (nums_len == 0) {
return 0;
}
else if (nums_len == 1) {
return nums[0];
}