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 Solution {
private:
void eraseIsland(vector<vector<char>>& grid, int x, int y) {
if (grid[y][x] == '0') {
return;
}
grid[y][x] = '0';
if (y > 0) eraseIsland(grid, x, y - 1);
if (y < grid.size() - 1) eraseIsland(grid, x, y + 1);
@vitkarpov
vitkarpov / divide-two-integers.js
Created October 3, 2019 07:50
Divide Two Integers (32 bits environment only, O(log n) time complexity)
/**
* @param {number} dividend
* @param {number} divisor
* @return {number}
*/
var divide = function(dividend, divisor) {
if (dividend == INT_MIN && divisor == -1) {
return INT_MAX;
}
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<pair<int, int>> numsNpos;
for (auto i = 0; i < nums.size(); i++) {
numsNpos.push_back({ nums[i], i });
}
sort(numsNpos.begin(), numsNpos.end());
@vitkarpov
vitkarpov / Computer-Science-Club-27-12-2018.md
Last active December 27, 2018 10:14
Computer Science Club, 27.12.2018 (Strings)

Computer Science Club, 27.12.2018

Here's a set of problems for the class. Exploiring strings.

Is Unique

Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures?

Hints:

@vitkarpov
vitkarpov / rm-element-from-the-stack.js
Created October 15, 2018 16:43
Removing an arbitrary element from the stack
function removeItemFromStack(stack, item) {
const tmp = stack.pop();
// if we've looked up the whole stack there's no item
// just return tmp element back to the top and we're done
if (stack.length === 0) {
stack.push(tmp);
return;
}
@vitkarpov
vitkarpov / extra-long-factorial.js
Created July 11, 2018 16:45
Extra Long Factorial
function mult(a, b) {
let carry = 0;
for (let i = 0; i < a.length || carry > 0; i++) {
if (i === a.length) {
a.push(0);
}
const curr = carry + a[i] * b;
a[i] = curr % 10;
carry = Math.floor(curr / 10);
@vitkarpov
vitkarpov / question.md
Created July 6, 2018 19:16
How do I make a tokenizer faster?

Hello software engineers and computer scientists! :)

I'm working on open source JavaScript project which has css parser as one if its modules. Node --perf shows me that the cricical part is REGEXP which is used to find the next word boundary.

const RE_WORD_END = /[ \n\t\r\f\(\)\{\}:;@!'"\\\]\[#]|\/(?=\*)/g;

// pos - current boundary
// next - next boundary which the code has to find
/my-website/
  | package.json
  | /src/
  | | /pages/
  | | | index.html
  | | | about.html
  | | /assets/
  | | | site.css
 | | | site.js
@vitkarpov
vitkarpov / code-comarison-in-errors.js
Created November 11, 2017 09:01
The right way to distinguish errors
if (err.code === 'ERR_HTTP_HEADERS_SENT') {
//do something with the error
}
@vitkarpov
vitkarpov / message-comparison-in-errors.js
Created November 11, 2017 08:56
Don't do this anymore, there're erorrs' codes since Node.js 9.0.0
if (err.message === 'Can\'t set headers after they are sent.') {
//do something with the error
}