This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" Trains an agent with (stochastic) Policy Gradients on Pong. Uses OpenAI Gym. """ | |
import numpy as np | |
import cPickle as pickle | |
import gym | |
# hyperparameters | |
H = 200 # number of hidden layer neurons | |
batch_size = 10 # every how many episodes to do a param update? | |
learning_rate = 1e-4 | |
gamma = 0.99 # discount factor for reward |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Minimal character-level Vanilla RNN model. Written by Andrej Karpathy (@karpathy) | |
BSD License | |
""" | |
import numpy as np | |
# data I/O | |
data = open('input.txt', 'r').read() # should be simple plain text file | |
chars = list(set(data)) | |
data_size, vocab_size = len(data), len(chars) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int find_min_diff(vector<int>& A, vector<int>& B) { | |
if(A.empty() || B.empty()) return -1; | |
int m = A.size(), n = B.size(); | |
int i = 0, j = 0; | |
int min_diff = INT_MAX; | |
while(i < m && j < n) { | |
int diff = abs(A[i] - B[j]); | |
min_diff = min(min_diff, diff); | |
if(min_diff == 0) return 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://stackoverflow.com/questions/5212037/find-the-pair-across-2-arrays-with-kth-largest-sum | |
// result may not be correct!!!!!!!!!!!!!!!!! | |
// 6/6/2015. attempted to calculate time complexity seems to be: | |
// log1 + log2 + ... + logk = logk! = O(klogk) | |
// here is how to calculate logk! | |
// http://stackoverflow.com/questions/2095395/is-logn-%CE%98n-logn | |
// http://ballinger.disted.camosun.bc.ca/math126/lognfactorial.pdf |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define MAXV 1000 | |
bool processed[MAXV+1]; | |
bool discovered[MAXV+1]; | |
int parent[MAXV+1]; | |
struct edgenode { | |
int y; | |
int weight; | |
edgenode *next; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// various ways of partition function | |
// see: http://www.liacs.nl/~graaf/STUDENTENSEMINARIUM/quicksorthistorical.pdf | |
// read the chapter 3 in book: Beautiful Code. It shows how to simplify quicksort to count average comparison numbers. (9/2/2014) | |
#include <iostream> | |
using namespace std; | |
/* | |
Given an array nums of integers and an int k, partition the array (i.e move the elements in "nums") such that: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
http://www.geeksforgeeks.org/greedy-algorithms-set-1-activity-selection-problem/ | |
You are given n activities with their start and finish times. Select the maximum number of activities that can be performed by a single person, assuming that a person can only work on a single activity at a time. | |
1) Sort the activities according to their finishing time | |
2) Select the first activity from the sorted array and print it. | |
3) Do following for remaining activities in the sorted array. | |
…….a) If the start time of this activity is greater than the finish time of previously selected activity then select this activity and print it. | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool search_matrix(vector<vector<int>> &matrix, int target) { | |
if(matrix.empty()) return false; | |
int m = matrix.size(), n = matrix[0].size(); | |
if(target < matrix[0][0] || target > matrix[m-1][n-1]) return false; | |
int r = 0, c = n-1; | |
while(r < m && c >= 0) { | |
if(target == matrix[r][c]) return true; | |
else if(target < matrix[r][c]) c--; | |
else r++; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct treenode { | |
int val; | |
treenode *left, *right; | |
treenode(int v) : val(v), left(NULL), right(NULL) {} | |
}; | |
void pre_order(treenode *r) { | |
if(!r) return; | |
stack<treenode*> stk; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// dp solution | |
bool word_break(const string& s, unordered_set<string>& dict) { | |
if(s.empty() || dict.empty()) return true; | |
vector<bool> dp(s.size()+1, false); // <---- why define a 'dp' of length N+1? because dp[i] means whether a string of length i can be segmented using dict | |
dp[0] = true; // cannot forget! | |
for(int i=1; i<=s.size(); i++) { | |
for(int j=0; j<i; j++) { // looping j from 0 to i-1 seems better | |
dp[i] = dp[j] && dict.find(s.substr(j+1, i-j)) != dict.end(); | |
if(dp[i] == true) break; | |
} |
NewerOlder