Skip to content

Instantly share code, notes, and snippets.

@dgodfrey206
dgodfrey206 / mop.txt
Created January 3, 2018 03:11
Math Olympiad Problem
July 14
July 16
August 14
////August 15////
////August 17////
////May 15////
////May 16////
////June 17////
@dgodfrey206
dgodfrey206 / zeroOutMatrix.cpp
Last active November 18, 2017 01:56
Write an algorithm such that if an element in an MxN matrix is 0, its entire row and columns are set to 0
#include<vector>
#include<iostream>
using namespace std;
#define N 5
#define M 5
void zeroOutMatrix(int mat[N][M]) {
bool rowVisited[N] = {};
bool columnVisited[M] = {};
@dgodfrey206
dgodfrey206 / ab.txt
Created October 30, 2017 02:13
Thought process for AB problem on Topcoder.
Topcoder AB problem
Inputs: N,K
Problem: Generate a string of length N that consits of only letters A and B and contains K pairs (i,j) such that s[i]='A' and s[j]='B'
I'm going to assume K is at most N/2
Trying to simplify the problem. What if we have a string of length two (N=2) and we want to make a string with 1 ab-pair (K=1). The result will be either "ab" or "ba".
N=10
K=12
@dgodfrey206
dgodfrey206 / threeSum.cpp
Created October 28, 2017 02:12
Returns three numbers from an array that add up to 0
#include<iostream>
#include<vector>
#include<unordered_set>
#include<unordered_map>
using namespace std;
class Solution {
public:
vector<int> threeSum(vector<int>& nums) {
unordered_map<int, int> ht;
@dgodfrey206
dgodfrey206 / remove_line_dups.cpp
Created October 25, 2017 01:59
Removes duplicates from lines
#include <iostream>
#include <sstream>
#include <set>
#include <unordered_set>
#include<vector>
using namespace std;
int main() {
#ifdef TESTING
stringstream ss("10 yards of white lace burlap table runner\n"
class StringIterator {
public:
StringIterator(string compressedString) : compressedString(compressedString) {
size_t i = 0;
while (i < compressedString.size()) {
freqTable.push_back(make_pair(compressedString[i++], 0));
int freq = 0;
while (i < compressedString.size() && isdigit(compressedString[i])) {
freq = (freq * 10) + (compressedString[i] - '0');
i++;
#include <vector>
#include<numeric>
#include<climits>
#include <algorithm>
#include<utility>
#include<iostream>
using namespace std;
#define check(n,lim)(0<=(n)&&(n)<=(lim))
@dgodfrey206
dgodfrey206 / maxheap.cpp
Created November 2, 2016 19:51
Max heap
#include <iostream>
#include <map>
#include <utility>
#include <queue>
#include <algorithm>
using namespace std;
template<class T>
class MaxHeap {
public:
@dgodfrey206
dgodfrey206 / height.cpp
Created November 2, 2016 02:41
Height of binary tree
int height(node* root) {
if (!root) return 0;
int leftHeight = 0, rightHeight = 0;
if (root->left)
leftHeight = 1 + height(root->left);
if (root->right)
rightHeight = 1 + height(root->right);
return max(leftHeight, rightHeight);
}
@dgodfrey206
dgodfrey206 / priorityvenues.cpp
Last active March 26, 2022 16:10
PriorityVeneues using a Heap
#include <iostream>
#include <map>
#include <utility>
#include <queue>
#include <algorithm>
using namespace std;
class PriorityVenues {
public:
PriorityVenues()=default;