Skip to content

Instantly share code, notes, and snippets.

View ericprom's full-sized avatar

ericprom

View GitHub Profile
@ericprom
ericprom / MaxCounters.py
Last active May 6, 2021 02:27
You are given N counters, initially set to 0, and you have two possible operations on them
def solution(N, A):
counters = [0] * N
maxV, minV = 0, 0
for x in A:
if 1 <= x <= N:
counters[x-1] = max(counters[x-1], minV) + 1
maxV = max(maxV, counters[x-1])
else: minV = maxV
for i in range(N):
counters[i] = max(counters[i], minV)
@ericprom
ericprom / TapeEquilibrium.py
Created May 5, 2021 12:09
The absolute difference between the sum of the first part and the sum of the second part.
A = [3,1,2,4,3]
left, right = 0, sum(A)
m = float('inf')
for a in A[:-1]:
left += a
right -= a
m = min(m, abs(left - right))
print(m)
@ericprom
ericprom / smallFrog.py
Created May 5, 2021 12:08
A small frog wants to get to the other side of a river. The frog is initially located on one bank of the river (position 0) and wants to get to the opposite bank (position X+1). Leaves fall from a tree onto the surface of the river.
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(X, A):
i = 0
dict_temp = {}
while i < len(A):
dict_temp[A[i]] = i
if len(dict_temp) == X:
return i
@ericprom
ericprom / dice.py
Last active May 5, 2021 10:25
Total S of N Dice with F faces
import itertools
num_of_face = 4
num_of_dice = 2
final_sum = 7
rolls = 0
dices = [num for num in range(1, num_of_face+1)]
sets = [dices] * num_of_dice
for values in itertools.product(*sets):
@ericprom
ericprom / PermMissingElem.py
Created April 30, 2021 03:46
Find that missing element in array
def solution(A):
sum = 0
N = len(A)
for val in A:
sum += val
expect = ( N + 1 ) * ( N + 2 ) // 2
return expect - sum
@ericprom
ericprom / FrogJmp.py
Created April 30, 2021 03:30
Count the minimal number of jumps that the small frog must perform to reach its target.
def solution(X, Y, D):
distance = Y - X
if distance % D == 0:
return distance // D
else:
return distance // D + 1
@ericprom
ericprom / OddOccurrencesInArray.py
Created April 29, 2021 10:44
A non-empty array A consisting of N integers is given. The array contains an odd number of elements, and each element of the array can be paired with another element that has the same value, except for one element that is left unpaired.
def solution(A):
result = 0
for number in A:
result ^= number
return result
@ericprom
ericprom / rotateArray.py
Created April 29, 2021 10:18
Rotate array A K times; that is, each element of A will be shifted to the right K times.
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A, K):
if len(A) == 0:
return A
K = K % len(A)
return (A[-K:] + A[:-K])
@ericprom
ericprom / binaryGab.py
Created April 29, 2021 09:54
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.
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
import re
def solution(N):
if N < 0:
return 0
binNum = "{0:b}".format(N)
strBinNum = str(binNum)
largest = 0
@ericprom
ericprom / maze.js
Created March 18, 2018 14:56
Solve A 2D Array Maze Using Recursion And JavaScript
var maze = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 0, 0, 0, 0, 0],
[1, 1, 1, 0, 1, 0, 0, 1, 1, 1],
[0, 1, 0, 0, 1, 0, 0, 1, 0, 0],
[0, 1, 1, 1, 1, 0, 1, 1, 1, 0],
[0, 1, 0, 0, 1, 0, 1, 0, 1, 0],
[0, 0, 0, 0, 1, 1, 1, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 2],