Skip to content

Instantly share code, notes, and snippets.

View andreytwostep's full-sized avatar

Andrey Svyachenovskiy andreytwostep

View GitHub Profile
# A binary gap within a positive integer N is any maximal sequence of consecutive zeros
# that is surrounded by ones at both ends in the binary representation of N.
# For example, number 9 has binary representation 1001 and contains a binary gap of length 2.
# The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of length 3.
# The number 20 has binary representation 10100 and contains one binary gap of length 1.
# The number 15 has binary representation 1111 and has no binary gaps. that, given a positive integer N,
# returns the length of its longest binary gap.
# The function should return 0 if N doesn't contain a binary gap.
# For example, given N = 1041 the function should return 5, because N has binary representation 10000010001 and so its longest
@andreytwostep
andreytwostep / twoSumBruteforce.js
Last active September 25, 2017 05:45
JS and Python implementations
function twoSum(data) {
var count = 0;
for (var i = 0; i < data.length; i++) {
for (var j = i+1; j < data.length; j++) {
if (data[i] + data[j] === 0) {
count++;
}
}
}
return count;
@andreytwostep
andreytwostep / 3SumBruteforce.php
Last active September 25, 2017 16:06
3Sum problem (bruteforce)
<?php
function readFileContent($filename) {
$result = FALSE;
if (file_exists($filename)) {
$result = file($filename, FILE_SKIP_EMPTY_LINES);
}
return $result;
}
var array = [1, 3, 5, 7, 9, 10, 22, 33, 45, 67, 88, 90];
function binarySearch(array, num) {
if (array.length < 1) {
return -1;
}
var low = 0;
var hi = array.length-1;
while (hi >= low) {
var mid = Math.floor((low + hi) / 2);
function binarySearchRecursiveImpl(array $array, $find, $left, $right) {
if (count($array) < 1 || $left > $right) {
return -1;
}
$mid = floor(($left + $right) / 2);
if ($find == $array[$mid]) {
return $mid;
}
return $find > $array[$mid] ?
binarySearchRecursiveImpl($array, $find, $mid+1, $right) :
function arrayPairSum(nums) {
var sum = 0;
if (nums !== null && nums.length > 1) {
nums.sort();
for (var i = 0; i < nums.length; i += 2) {
sum += nums[i];
}
}
return sum;
}
/**
* https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/
*/
var removeDuplicates = function(nums) {
var n = nums.length;
if (n <= 1) {
return n;
}
var unique = 0;
Access (By index) - O(n)
Search (By value) - O(n)
Insertion - O(1)
Deletion - O(1)
import time
f = open('./integers/1000.txt', 'r')
x = map(int, f.read().splitlines())
def solution(nums):
n = len(nums)
res = 0
for i in range(n):
for j in range(i + 1, n):
class Stack:
def __init__(self):
self.items = []
def isEmpty(self):
return []
def push(self, item):
self.items.append([item])