Skip to content

Instantly share code, notes, and snippets.

@v-odin
v-odin / Solution.h
Created October 24, 2020 06:18
Amaxon OA 3
struct Station {
Station(int capacity = 0, int car = 0): capacity(capacity), car(car) {}
int capacity;
int car;
};
bool pick(list<Station>& workers, list<int>& q) {
if (q.empty()) return false;
int car = q.front();
for (auto& station: workers) {
@v-odin
v-odin / Solution.h
Created October 21, 2020 17:19
Amazon OA 1
int toNum(vector<int>& digits) {
int result = 0;
for (int num: digits) {
result *= 10;
result += num;
}
return result;
}
vector<int> toVec(int num) {
@v-odin
v-odin / Solution.h
Created October 9, 2020 11:21
315. Count of Smaller Numbers After Self #cpp #leetcode
class Solution {
public:
struct TreeNode {
TreeNode(int val) : val(val) {}
int val = 0;
int depth = 0;
int lCount = 0;
int rCount = 0;
TreeNode* left = nullptr;
TreeNode* right = nullptr;
@v-odin
v-odin / Solution.h
Created October 6, 2020 08:26
1382. Balance a Binary Search Tree #leetcode #cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
@v-odin
v-odin / AVL.h
Created October 5, 2020 07:45
AVL tree basic implementation
struct Node {
Node* left = nullptr;
Node* right = nullptr;
int val = 0;
int height = 0;
}
int getHeight(Node *root) {
if (!root) return -1;
return height;
@v-odin
v-odin / Solution.h
Created August 10, 2020 06:55
1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit #cpp #leetcode
class MinMaxQueue {
public:
void push(int v) {
if (back.empty()) {
back.emplace(v, v, v);
} else {
back.emplace(v, std::min(v, back.top().min), std::max(v, back.top().max));
}
}
@v-odin
v-odin / Solution.h
Created July 27, 2020 17:30
460. LFU Cache #cpp #leetcode #design
struct DoubleLinkedList {
struct Node {
Node(){};
Node(int key, int value) : key(key), value(value) {};
Node* next = nullptr;
Node* prev = nullptr;
int key = -1;
int value = -1;
int rank = 1;
};
@v-odin
v-odin / ContactList.h
Last active July 23, 2020 18:41
ContaktList #practice #design
#include <iostream>
#include <memory>
#include <vector>
#include <string>
using namespace std;
struct Trie {
array<unique_ptr<Trie>, 26> nodes;
bool isEnd = false;
@v-odin
v-odin / Solution.h
Created July 2, 2020 18:49
Longest Increasing Subsequence N^2 #cpp #leetcode
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
int result = 0;
int N = nums.size();
vector<int> dp(N, 1);
for (int i = 0; i < N; ++i) {
for (int j = i - 1; j >= 0; --j) {
if (nums[j] < nums[i]) {
dp[i] = max(dp[i], dp[j] + 1);
@v-odin
v-odin / Solution.h
Created July 2, 2020 17:01
Longest Increasing Subsequence #cpp #leetcode
class Solution {
public:
int lowerBound(const vector<int>& nums, int value) {
int l = 0;
int r = nums.size();
while (l < r) {
int mid = (l+r) / 2;
if (nums[mid] < value) {
l = mid + 1;
} else {