Skip to content

Instantly share code, notes, and snippets.

#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;
#include <stdexcept>
#include <iostream>
#include <memory>
#include <cstring>
#include <cmath>
using namespace std;
template<class T>
class RingBuffer;
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
// returns true if A is a fading palindome
bool ifp(char* A) {
int i, n = strlen(A);
for (i = 0; i < n/2; ++i) {
// we need to check the palindome invariant A[i] == A[n-i-1] and
@dgodfrey206
dgodfrey206 / stairbowtie.cpp
Last active November 30, 2024 02:41
Making stairs and a bow tie
#include <iostream>
using namespace std;
void makeBowTie(int h) {
int i, j, k, s;
for (i = 1; i <= h; ++i) {
s = 2*i-1;
s = h - abs(h - s);
for (j = 0; j < s; ++j) cout << '*';
for (j = 0; j < 2*h - 2*s; ++j) cout << ' ';
@dgodfrey206
dgodfrey206 / oddsumsub.cpp
Last active September 6, 2016 20:54
Write a recursive function that takes an integer n as a parameter and returns the following sum/subtraction of odd numbers 1 - 3 + 5 - 7 + 9....+/- n (if n is even, the last term is n-1)
#include <iostream>
#include <cmath>
using namespace std;
// 1 - 3 + 5 - 7 + 9 ... +/- n
// recursive
int f1(int n) {
if (n == 1) return 1;
if (n % 2 == 0) n--;