Skip to content

Instantly share code, notes, and snippets.

@KirillLykov
KirillLykov / lc_421.cpp
Last active December 5, 2016 15:31
Problem 421 from leetcode, O(n) solution using Trie
class Solution {
public:
struct Node {
Node(): left(nullptr), right(nullptr) {}
shared_ptr<Node> left, right;
};
typedef shared_ptr<Node> NodePtr;
shared_ptr<Node> root;
@KirillLykov
KirillLykov / topcoder_701_1.cpp
Last active December 5, 2016 15:32
Topcoder 701, 1, div2
#include <bits/stdc++.h>
using namespace std;
class SquareFreeString {
public:
string isSquareFree(string s) {
if (s.length() < 2)
return "square-free";
for (int l = 2; l <= s.length(); l += 2) {
for (int shift = 0; shift < s.length() - l + 1; ++shift) {
@KirillLykov
KirillLykov / sync_shoping.cpp
Created December 5, 2016 09:41
Synchronous Shopping solution
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <queue>
#include <list>
#include <bitset>
using namespace std;
@KirillLykov
KirillLykov / connectedSticks.cpp
Last active December 11, 2016 22:07
Solution for a programming problem from unknown contest
#include <bits/stdc++.h>
using namespace std;
// Assume that you have N sticks with different lengths
// You can connect these sticks to get a composed stick of some length
// You are given a targetLenth, is it possible to get a composed stick of this length?
// For this solution I represent problem a s graph where a vertex is marked by the mask (every bit says which of the sticks
// we have already chosen) and the length of edges are the length of the corresponding sticks.
// For instance, if we have lengths=[2,3,5], than 0th vertex corresponds to the case when we haven't picked up a stick yet,
/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
class SummaryRanges {
@KirillLykov
KirillLykov / partitions-qsort-kthstat.cpp
Last active May 31, 2018 15:54
Partitions, k-th statistic, quicksort , merge sort
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef vector<int>::iterator Iterator;
void merge_sort(Iterator b, Iterator e) {
if (e - b <= 1) return;
auto m = b + (e - b)/2;
// Solution for grafixMask from TC SRM211
#include <bits/stdc++.h>
using namespace std;
// using DFS approach
class grafixMask
{
public:
vector<int> sortedAreas(vector <string> rectangles)
{
@KirillLykov
KirillLykov / RoadReconstruction.cpp
Created February 15, 2017 15:54
Solution for tc srm 356, div2 lvl3
#include <bits/stdc++.h>
using namespace std;
struct Road {
string id;
int x,y;
int cost;
Road(string a, int b, int c, int d) : id(a), x(b), y(c), cost(d) {}
};
@KirillLykov
KirillLykov / codeforces_401_C.cpp
Created February 24, 2017 16:53
Solution for codeforces round 401, C
#include <bits/stdc++.h>
using namespace std;
int main(int, char**)
{
int n,m;
cin >> n >> m;
vector<vector<int>> matrix(n, vector<int>(m,0));
for(int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
@KirillLykov
KirillLykov / codeforces_403_3.cpp
Created March 5, 2017 20:14
Solution for codeforces 403, div2C
#include <bits/stdc++.h>
using namespace std;
void dfs(vector<int>& colors, vector< list<int> >& adjlist, int cur, int parent) {
int c = 1;
for (int a : adjlist[cur]) {
if (a != parent) {
while (c == colors[cur] || c == colors[parent])
++c;
colors[a] = c;