Skip to content

Instantly share code, notes, and snippets.

View vitkarpov's full-sized avatar
🔲
I help folks prepare for coding interviews

Viktor Karpov vitkarpov

🔲
I help folks prepare for coding interviews
View GitHub Profile
class Trie {
private:
struct Node {
vector<Node*> children = vector<Node*>(26);
bool is_word = false;
};
Node* root = new Node;
public:
/** Initialize your data structure here. */
Trie() {}
@vitkarpov
vitkarpov / divide-two-integers.cpp
Created March 16, 2020 03:46
29. Divide Two Integers
class Solution {
public:
int divide(int dividend, int divisor) {
int sign = (dividend < 0) ^ (divisor < 0) ? -1 : 1;
long long dvd = dividend;
long long dvs = divisor;
if (dvd == INT32_MIN && dvs == -1) {
return INT32_MAX;
}
@vitkarpov
vitkarpov / leetcode-valid-parentheses.cpp
Created March 2, 2020 14:01
Valid Parentheses LeetCode grader
#include <stdexcept>
#include <string>
#include <vector>
using namespace std;
class Solution {
public:
bool isValid(string s) {
// твой код здесь
}
@vitkarpov
vitkarpov / balanced-brackets.js
Last active January 31, 2021 15:24
Правильная скобочная последовательность (t.me/coding_interviews)
function isValid(s) {
const brackets = {
')': '(',
']': '[',
'}': '{'
};
// в качестве стека в JavaScript можно использовать обычный массив
//(если пользоваться только «разрешенными» методами push & pop)
const st = [];
for (let i = 0; i < s.length; i++) {
@vitkarpov
vitkarpov / trapping-rain-water.cpp
Created November 6, 2019 12:08
Trapping Rain Water (Dynamic Programming)
class Solution {
public:
int trap(const vector<int>& h) {
int n = h.size();
vector<int> left_max(n + 1);
vector<int> right_max(n + 1);
for (int i = 1; i <= n; i++) {
left_max[i] = max(left_max[i - 1], h[i - 1]);
}
@vitkarpov
vitkarpov / trapping-water.cpp
Created October 29, 2019 16:26
42. Trapping Rain Water
class Solution {
public:
int trap(const vector<int>& h) {
int n = h.size();
vector<int> left_max(n + 1);
vector<int> right_max(n + 1);
for (int i = 1; i <= n; i++) {
left_max[i] = max(left_max[i - 1], h[i - 1]);
}
// Time: O(n * log (n))
// Memory: O(log (n))
/**
* @param {number[]} heights
* @return {number}
*/
var largestRectangleArea = function(heights) {
if (!heights.length) {
return 0;
@vitkarpov
vitkarpov / valid-parenthesis-string.js
Last active October 20, 2019 06:20
678. Valid Parenthesis String (dynamic programming)
/**
* @param {string} s
* @return {boolean}
*/
var checkValidString = function(s) {
const n = s.length;
const dp = new Array(n).fill(0).map(() => new Array(n).fill(false));
for (let i = 0; i < n; i++) {
if (s[i] == "*") {
class LRUCache {
typedef pair<int, int> Node;
typedef list<Node>::iterator ref;
list<Node> q;
unordered_map<int, ref> m;
int capacity;
void insert(int key, int value) {
if (m.find(key) != m.end()) {
@vitkarpov
vitkarpov / sort-colors.cpp
Created October 15, 2019 08:09
Sort Colors
class Solution {
public:
void sortColors(vector<int>& nums) {
int p0 = 0;
int p1 = 0;
int p2 = nums.size() - 1;
while (p1 <= p2) {
if (nums[p1] == 1) {
p1++;