This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def solution(A): | |
sum = 0 | |
N = len(A) | |
for val in A: | |
sum += val | |
expect = ( N + 1 ) * ( N + 2 ) // 2 | |
return expect - sum |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def solution(X, Y, D): | |
distance = Y - X | |
if distance % D == 0: | |
return distance // D | |
else: | |
return distance // D + 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def solution(A): | |
result = 0 | |
for number in A: | |
result ^= number | |
return result |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], |
NewerOlder