This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include<numeric> | |
#include<climits> | |
#include <algorithm> | |
#include<utility> | |
#include<iostream> | |
using namespace std; | |
#define check(n,lim)(0<=(n)&&(n)<=(lim)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <map> | |
#include <utility> | |
#include <queue> | |
#include <algorithm> | |
using namespace std; | |
template<class T> | |
class MaxHeap { | |
public: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <map> | |
#include <utility> | |
#include <queue> | |
#include <algorithm> | |
using namespace std; | |
class PriorityVenues { | |
public: | |
PriorityVenues()=default; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |