Skip to content

Instantly share code, notes, and snippets.

View lnikon's full-sized avatar

Vahag Bejanyan lnikon

View GitHub Profile
@lnikon
lnikon / optimalParents.cpp
Created March 22, 2019 16:50
Choose optimal way to multiply matrices.
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <utility>
#include <limits>
#include <cstdint>
using Matrix = std::vector<std::vector<int>>;
#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <utility>
template <class ForwardIterator, class Comp = std::less<>>
auto maxSum(ForwardIterator begin,
ForwardIterator end,
Comp comp = Comp()) {
@lnikon
lnikon / lcs.cpp
Last active March 22, 2019 16:53
Longest Common Substring for Two Strings
#include <iostream>
#include <string>
#include <algorithm>
#include <unordered_map>
#include <array>
int LCSNaive(std::string x, std::string y, int n, int m)
{
if (m == 0 || n == 0) {
return 0;
@lnikon
lnikon / lis.cpp
Last active March 22, 2019 13:54
Longest Increasing subsequence
#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>
template <class ForwardIterator, class Comp = std::less<>>
auto lis(ForwardIterator begin,
ForwardIterator end,
Comp comp = Comp()) {
#include <iostream>
#include <unordered_map>
#include <vector>
#include <string>
#include <algorithm>
template <class T>
auto&& minusKFromAll(std::vector<T> v, T k) {
std::transform(std::begin(v), std::end(v), std::begin(v), [](T i) { return i - 1; });
1) Find the last element of a list.
xs = [1, 2, 3, 4]
last xs
2) Find the last but one element of a list.
xs = [1, 2, 3, 4]
last (init xs)
3) Find the K'th element of a list. The first element in the list is number 1.
xs = [1, 2, 4, 3, 5, 6, 8, 7]
@lnikon
lnikon / counting_sort.cpp
Created August 9, 2018 20:17
Counting sort.
template <class T>
auto countingSort(const std::vector<T>& vec)
{
if(vec.size() <= 1) std::move(vec);
std::vector<T> counts(vec.size(), 0);
const auto size = vec.size();
for(size_t i = 0; i < size - 1; ++i)
{
@lnikon
lnikon / from2tonprimes.cpp
Created August 9, 2018 07:05
Prime numbers from 2 to N(O(N^2) worst case complexity)
#include <iostream>
#include <cmath>
#include <vector>
using ull = unsigned long long;
bool isPrime(int n)
{
for(int i = 2; i < n / 2; i++)
{
@lnikon
lnikon / fast_power.cpp
Created August 8, 2018 07:35
Binary Power
#include <vector>
#include <iostream>
using ull = unsigned long long;
ull fast_power(ull num, int power)
{
if(num < 0 || power < 0) {
std::cout << "invalid arguments specified\n";
}
@lnikon
lnikon / revwrd.cpp
Created July 29, 2018 13:53
Reverse words in a given string
#include <iostream>
#include <string>
#include <algorithm>
void solution(std::string str) {
str += " ";
auto size = str.size();
size_t start = 0;
for(size_t i = 0; i < size; i++) {
size_t end = str.find(' ', start);