Skip to content

Instantly share code, notes, and snippets.

View sundeepblue's full-sized avatar
🎯
Focusing

Protoss In Matrix sundeepblue

🎯
Focusing
View GitHub Profile
@karpathy
karpathy / pg-pong.py
Created May 30, 2016 22:50
Training a Neural Network ATARI Pong agent with Policy Gradients from raw pixels
""" 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
@karpathy
karpathy / min-char-rnn.py
Last active September 27, 2025 03:29
Minimal character-level language model with a Vanilla Recurrent Neural Network, in Python/numpy
"""
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)
@sundeepblue
sundeepblue / find the minimal difference between two sorted arrays elements
Last active December 1, 2019 01:53
find the minimal difference between two sorted arrays elements
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;
@sundeepblue
sundeepblue / Kth maximal element of sum of two arrays
Last active August 29, 2015 14:01
Kth maximal element of sum of two arrays
// 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
#define MAXV 1000
bool processed[MAXV+1];
bool discovered[MAXV+1];
int parent[MAXV+1];
struct edgenode {
int y;
int weight;
edgenode *next;
@sundeepblue
sundeepblue / quicksort.cp
Last active August 29, 2015 14:01
quicksort, quickselect, partition
// 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:
@sundeepblue
sundeepblue / select_activities
Created May 11, 2014 01:35
greedy, schedule movie problem, activity selection problem
/*
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.
*/
@sundeepblue
sundeepblue / search_matrix
Created May 10, 2014 20:53
Search a 2D Matrix
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++;
}
@sundeepblue
sundeepblue / pre_order
Created May 3, 2014 18:54
[tree] preorder binary tree traversal
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;
@sundeepblue
sundeepblue / word_break.cpp
Last active August 29, 2015 14:00
[dp] [dfs] [word break] [string] Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words
// 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;
}