Skip to content

Instantly share code, notes, and snippets.

@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;
@dgodfrey206
dgodfrey206 / removeloop.cpp
Last active March 26, 2022 16:11
Remove a loop from a linked list
bool has_cycle(node* head) {
if (!head || !head->next) return false;
node* slow, *fast;
slow = fast = head;
while (true) {
slow = slow->next;
if (fast->next)
fast = fast->next->next;
if (slow == nullptr || fast == nullptr)
return false;
@dgodfrey206
dgodfrey206 / deleteBetween.cpp
Last active March 26, 2022 16:11
Delete a linked list between two nodes (singly and doubly)
void deleteList(node* cur);
// Doubly linked list
node* deleteBetween(node* head, node* x, node* y) {
if (x->prev)
x->prev->next = y->next;
else
head = y->next;
if (y->next)
y->next->prev = x->prev;