Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View barrysteyn's full-sized avatar

Barry Steyn barrysteyn

View GitHub Profile
@barrysteyn
barrysteyn / bubblesort.py
Created November 16, 2012 22:31
Bubble sort algorithm in pythong
for i in range(len(array_to_sort),0,-1):
for j in range(i-1):
if (array_to_sort[j] > array_to_sort[j+1]):
temp = array_to_sort[j];
array_to_sort[j] = array_to_sort[j+1];
array_to_sort[j+1] = temp;
@barrysteyn
barrysteyn / Node_Upstart_NVM
Created January 6, 2013 14:27
Running a Node process under NVM that uses upstart.
start on runlevel [2345]
stop on runlevel [06]
setuid devuser #don't run the process as root. You are asking for trouble if you do
setgid devuser
env NODEFOLDER=/home/dev/node-script
script
chdir $NODEFOLDER
@barrysteyn
barrysteyn / Python_VirutalEnv_With_Upstart
Created January 6, 2013 18:33
Using Python with a VirtualEnv in Upstart
start on runlevel [2345]
stop on runlevel [06]
setuid devuser
setgid devuser
exec /usr/local/bin/uwsgi --master --socket 127.0.0.1:3031 --pythonpath "/home/dev/python-script" --file "/home/dev/python-script/script.py" --callable app --processes 20 --virtualenv "/home/dev/.virtualenvs/python-script" --enable-threads
start on startup
@barrysteyn
barrysteyn / gist:5413430
Created April 18, 2013 15:03
Fibonacci - Recursive
void fib(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fib(n-1) + fib(n-2);
}
@barrysteyn
barrysteyn / rotate_list.cpp
Created June 25, 2013 19:27
Rotate List, from LeetCode
/**
* In an interview, the following questions should be asked:
* (1) What happens if k is longer than the list size
* (2) Edge cases (if head is NULL and k may be zero)
*
* Analysis: (where n is the number of nodes in the list)
* Time: O(n)
* Space: O(1)
*/
@barrysteyn
barrysteyn / hasPathSum.cpp
Created June 29, 2013 21:42
LeetCode: HasPathSum
/**
* Things to ask in an interview: Can sum ever be zero
* Time: O(n) (n = number of nodes)
*/
class Solution {
public:
bool hasPathSum(TreeNode *root, int sum) {
if (!root) {
return false;
@barrysteyn
barrysteyn / recover_binary_tree.cpp
Created June 30, 2013 15:47
Leetcode problem: Recover the binary tree that has had two nodes swapped/
/* This is an easy problem, but made difficult because you can only use O(1) space
* Traverse the tree use inorder. Its easy to spot nodes that are out of place: They will not be in increasing order
* In order to accomplish this, we need to do an in order traverse and keep track of the previous node
* The first time previous is larger than the current node (root), both previous and root must be added because both could
* potentially be in violation. If however we find another violation (i.e. previous is greater than current), then we can replace
* the last value of the node array with the current (root) node. Why? Because two nodes have been swapped. We find the larger of
* the two first (because of the in order traversal), so the next node must be smaller of the two.
*
* Space: O(1) - only an array of size 2
* Time Complexity: O(n), where n is the number of nodes (i.e. complexity of in-order traversal)
@barrysteyn
barrysteyn / Leetcode: Search In A Sorted Array.md
Last active December 19, 2015 05:09
Leetcode problem: Search in a rotated array (classic problem)

Leetcode: Search In A Sorted Array

This is a classic binary search problem, which I always struggle to get after not looking at it for some time.

There are many ways to accomplish, I will present 5 ways:

  1. The standard way without knowledge of abstract binary search theory (both iterative and recursive)
  2. Evaluation the following predicate: p(x): A[x] >= target
  3. Evaluation of the following predicate: p(x): A[x] > target
@barrysteyn
barrysteyn / duplicate_rotated_array_search.cpp
Last active December 19, 2015 05:19
Leetcode: Search in a sorted array that can have duplicates
/*
* This is the classic problem, but made slightly more difficult by allowing duplicates. If duplicates are allowed,
* then we can get into a pickle: When both the first and last element are the same. In this case, we do not know whether to go
* forwards or backwards. What do we know: That both first and last are the same (duh)! So that basically means we carry
* on either ignoring the first element or the last element (they are the same after all, so it does not matter).
*
* Time complexity = O(N) (bye bye logn binary search). This is because in the worst case, line 28 comes down to
* a linear search.
*/
@barrysteyn
barrysteyn / maxValidParenthesis.cpp
Created July 2, 2013 21:08
Leetcode: Max valid parenthesis
/*
* I found this one tough. I figured out a solution that used O(n) space almost immediately, but this
* solution is much more elegant. The problem states counting the max size of the valid parenthesis.
* For example, (()() would be four. The data structure to use here is a stack, but there is a trick.
* The trick is to note that if the stack is empty, and a right bracket comes along ')', then the
* size calculation must start from 0 again. This is because that is invalid and will never
* produce a valid parenthesis pair. So we initialise a variable called lastRight, which is set at -1
* When the stack is empty, this is the variable that will be used to calculate the size. When a ')'
* comes along, then this variable is updated. When the stack is not empty, then we just substract the
* the current increment value from whatever is on the top of the stack.