description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that :
p[0] = start
p[i] and p[i+1] differ by only one bit in their binary representation.
p[0] and p[2^n -1] must also differ by only one bit in their binary representation.
Example 1:
Input: n = 2, start = 3
Output: [3... | class Solution:
def grayCode(self, n: int) -> List[int]:
if n == 0:
return [0]
prv_code = self.grayCode(n - 1)
return prv_code + [(2 ** (n - 1) + x) for x in reversed(prv_code)]
def circularPermutation(self, n: int, start: int) -> List[int]:
ind = self.grayCode(n).i... | CLASS_DEF FUNC_DEF VAR IF VAR NUMBER RETURN LIST NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR |
Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that :
p[0] = start
p[i] and p[i+1] differ by only one bit in their binary representation.
p[0] and p[2^n -1] must also differ by only one bit in their binary representation.
Example 1:
Input: n = 2, start = 3
Output: [3... | class Solution:
def circularPermutation(self, n: int, start: int) -> List[int]:
return self.solve(start, n)
def solve(self, num, length):
if length == 1:
if num == 1:
return [1, 0]
return [0, 1]
firstnum = 1
if num < 2 ** (length - 1):
... | CLASS_DEF FUNC_DEF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER IF VAR NUMBER RETURN LIST NUMBER NUMBER RETURN LIST NUMBER NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR NU... |
Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that :
p[0] = start
p[i] and p[i+1] differ by only one bit in their binary representation.
p[0] and p[2^n -1] must also differ by only one bit in their binary representation.
Example 1:
Input: n = 2, start = 3
Output: [3... | class Solution:
def circularPermutation(self, n: int, start: int) -> List[int]:
array = list(map(self.binaryToGray, range(0, 2**n)))
index = array.index(start)
return array[index:] + array[:index]
def binaryToGray(self, n: int) -> int:
return n ^ n >> 1 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR VAR VAR VAR VAR FUNC_DEF VAR RETURN BIN_OP VAR BIN_OP VAR NUMBER VAR |
Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that :
p[0] = start
p[i] and p[i+1] differ by only one bit in their binary representation.
p[0] and p[2^n -1] must also differ by only one bit in their binary representation.
Example 1:
Input: n = 2, start = 3
Output: [3... | class Solution:
def circularPermutation(self, n: int, start: int) -> List[int]:
r = [0, 1]
for i in range(1, n):
r += [(2**i + v) for v in r[::-1]]
idx = r.index(start)
return r[idx:] + r[:idx] | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR VAR VAR VAR VAR |
Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that :
p[0] = start
p[i] and p[i+1] differ by only one bit in their binary representation.
p[0] and p[2^n -1] must also differ by only one bit in their binary representation.
Example 1:
Input: n = 2, start = 3
Output: [3... | class Solution:
def circularPermutation(self, n: int, start: int) -> List[int]:
k = 1
ans = [0, 1]
while k <= n - 1:
l = len(ans)
for i in range(l - 1, -1, -1):
ans.append(2**k + ans[i])
k = k + 1
while 1:
if ans[0] == ... | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR FUN... |
Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that :
p[0] = start
p[i] and p[i+1] differ by only one bit in their binary representation.
p[0] and p[2^n -1] must also differ by only one bit in their binary representation.
Example 1:
Input: n = 2, start = 3
Output: [3... | class Solution:
def circularPermutation(self, n, start):
start = bin(start)[2:]
while len(start) != n:
start = "0" + start
nums = [start]
for i in range(n):
tmp = []
for j in nums[::-1]:
if j[i] == "1":
tmp.appe... | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR STRING VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VA... |
Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that :
p[0] = start
p[i] and p[i+1] differ by only one bit in their binary representation.
p[0] and p[2^n -1] must also differ by only one bit in their binary representation.
Example 1:
Input: n = 2, start = 3
Output: [3... | class Solution:
def circularPermutation(self, n: int, start: int) -> List[int]:
gray = []
for i in range(2**n):
gray.append(i ^ i >> 1)
if start == i ^ i >> 1:
index = i
return gray[index:] + gray[0:index] | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN BIN_OP VAR VAR VAR NUMBER VAR VAR VAR |
Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that :
p[0] = start
p[i] and p[i+1] differ by only one bit in their binary representation.
p[0] and p[2^n -1] must also differ by only one bit in their binary representation.
Example 1:
Input: n = 2, start = 3
Output: [3... | class Solution:
def circularPermutation(self, n: int, start: int) -> List[int]:
res, s = [], None
for i in range(1 << n):
tmp = i ^ i >> 1
res.append(i ^ i >> 1)
if tmp == start:
s = i
res += res
return res[s : s + len(res) // 2] | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR LIST NONE FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR |
Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that :
p[0] = start
p[i] and p[i+1] differ by only one bit in their binary representation.
p[0] and p[2^n -1] must also differ by only one bit in their binary representation.
Example 1:
Input: n = 2, start = 3
Output: [3... | class Solution:
def circularPermutation(self, n: int, start: int) -> List[int]:
res = [(i ^ i >> 1) for i in range(1 << n)]
idx = res.index(start)
return res[idx:] + res[:idx] | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR VAR VAR VAR VAR |
Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that :
p[0] = start
p[i] and p[i+1] differ by only one bit in their binary representation.
p[0] and p[2^n -1] must also differ by only one bit in their binary representation.
Example 1:
Input: n = 2, start = 3
Output: [3... | class Solution:
def circularPermutation(self, n: int, start: int) -> List[int]:
gc, bit = [0, 1], 1
while bit < n:
bit += 1
next_vals = []
for val in reversed(gc):
next_vals.append(val + 2 ** (bit - 1))
gc.extend(next_vals)
ind... | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR LIST NUMBER NUMBER NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR VAR VAR VAR VAR |
Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that :
p[0] = start
p[i] and p[i+1] differ by only one bit in their binary representation.
p[0] and p[2^n -1] must also differ by only one bit in their binary representation.
Example 1:
Input: n = 2, start = 3
Output: [3... | class Solution:
def helper(self, n):
if n == 1:
return ["0", "1"]
else:
res = self.helper(n - 1)
return [("0" + x) for x in res] + [("1" + x) for x in res[::-1]]
def circularPermutation(self, n: int, start: int) -> List[int]:
res = [int(n, 2) for n i... | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN LIST STRING STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP STRING VAR VAR VAR BIN_OP STRING VAR VAR VAR NUMBER FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR |
Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that :
p[0] = start
p[i] and p[i+1] differ by only one bit in their binary representation.
p[0] and p[2^n -1] must also differ by only one bit in their binary representation.
Example 1:
Input: n = 2, start = 3
Output: [3... | class Solution:
def circularPermutation(self, n: int, start: int) -> List[int]:
perms = [(2**i) for i in range(n - 1, -1, -1)]
end = start ^ 1
visited = set()
path = []
def dfs(node):
if node == end:
path.append(node)
return True
... | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR IF FUNC_CALL VAR BIN_OP VAR... |
Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that :
p[0] = start
p[i] and p[i+1] differ by only one bit in their binary representation.
p[0] and p[2^n -1] must also differ by only one bit in their binary representation.
Example 1:
Input: n = 2, start = 3
Output: [3... | class Solution:
def circularPermutation(self, n: int, start: int) -> List[int]:
a = ["0", "1"]
for i in range(n - 1):
a0 = [("0" + b) for b in a]
a1 = [("1" + b) for b in a]
a = a0 + a1[::-1]
intperm = [int(b, 2) for b in a]
idx = intperm.index(st... | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST STRING STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP STRING VAR VAR VAR ASSIGN VAR BIN_OP STRING VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR VAR VAR VAR VAR |
Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that :
p[0] = start
p[i] and p[i+1] differ by only one bit in their binary representation.
p[0] and p[2^n -1] must also differ by only one bit in their binary representation.
Example 1:
Input: n = 2, start = 3
Output: [3... | class Solution:
def circularPermutation(self, n: int, start: int) -> List[int]:
gray = []
for i in range(2**n):
gray.append(i ^ i >> 1)
idxStart = gray.index(start)
result = []
for i in range(len(gray)):
result.append(gray[(i + idxStart) % len(gray)])... | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR VAR |
Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that :
p[0] = start
p[i] and p[i+1] differ by only one bit in their binary representation.
p[0] and p[2^n -1] must also differ by only one bit in their binary representation.
Example 1:
Input: n = 2, start = 3
Output: [3... | class Solution:
def circularPermutation(self, n: int, start: int) -> List[int]:
def encode_as_gray(n):
return n ^ n >> 1
arr = [encode_as_gray(i) for i in range(0, 2**n)]
i = arr.index(start)
return arr[i:] + arr[:i] | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF RETURN BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR VAR VAR VAR VAR |
Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that :
p[0] = start
p[i] and p[i+1] differ by only one bit in their binary representation.
p[0] and p[2^n -1] must also differ by only one bit in their binary representation.
Example 1:
Input: n = 2, start = 3
Output: [3... | class Solution:
def circularPermutation(self, n: int, start: int) -> List[int]:
answer = [start]
visited = set(answer)
next = start
while next != None:
previous = next
next = None
for i in range(n):
newNum = previous ^ 1 << i
... | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR VAR VAR |
Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that :
p[0] = start
p[i] and p[i+1] differ by only one bit in their binary representation.
p[0] and p[2^n -1] must also differ by only one bit in their binary representation.
Example 1:
Input: n = 2, start = 3
Output: [3... | class Solution:
def circularPermutation(self, n: int, start: int) -> List[int]:
def f(n):
if n == 0:
return [0]
else:
return f(n - 1) + [(2 ** (n - 1) + v) for v in f(n - 1)[::-1]]
res = f(n)
i = res.index(start)
return res[i... | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR NUMBER RETURN LIST NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR VAR VAR VAR VAR |
Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that :
p[0] = start
p[i] and p[i+1] differ by only one bit in their binary representation.
p[0] and p[2^n -1] must also differ by only one bit in their binary representation.
Example 1:
Input: n = 2, start = 3
Output: [3... | class Solution:
def helper(self, n):
return n ^ n >> 1
def circularPermutation(self, n: int, start: int) -> List[int]:
solution = list()
for i in range(0, 2**n):
solution.append(self.helper(i))
index = -1
for i in range(0, len(solution)):
if solu... | CLASS_DEF FUNC_DEF RETURN BIN_OP VAR BIN_OP VAR NUMBER FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR VAR VAR VAR VAR VAR |
Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that :
p[0] = start
p[i] and p[i+1] differ by only one bit in their binary representation.
p[0] and p[2^n -1] must also differ by only one bit in their binary representation.
Example 1:
Input: n = 2, start = 3
Output: [3... | class Solution:
def backtracking(self, result, visited, n, found):
if len(result) == 2**n:
diff = result[-1] ^ result[0]
i = 0
is_one_bit = 0
while i < n:
if 1 << i == diff:
is_one_bit = 1
break
... | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER NUMBER RETURN IF VAR NUMBER NUMBER RETURN ASSIGN VAR VAR NUMBER FOR ... |
On the first row, we write a 0. Now in every subsequent row, we look at the previous row and replace each occurrence of 0 with 01, and each occurrence of 1 with 10.
Given row N and index K, return the K-th indexed symbol in row N. (The values of K are 1-indexed.) (1 indexed).
Examples:
Input: N = 1, K = 1
Output: 0
... | class Solution:
def maxChunksToSorted(self, arr):
res = 0
temp = sorted(arr)
sum1, sum2 = 0, 0
for i in range(0, len(arr)):
sum1 += arr[i]
sum2 += temp[i]
if sum1 == sum2:
res += 1
return res | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR |
On the first row, we write a 0. Now in every subsequent row, we look at the previous row and replace each occurrence of 0 with 01, and each occurrence of 1 with 10.
Given row N and index K, return the K-th indexed symbol in row N. (The values of K are 1-indexed.) (1 indexed).
Examples:
Input: N = 1, K = 1
Output: 0
... | class Solution:
def maxChunksToSorted(self, arr):
arr_index = sorted(list(range(len(arr))), key=arr.__getitem__)
ans = ma = 0
for i, x in enumerate(arr_index):
ma = max(ma, x)
if ma == i:
ans += 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR |
On the first row, we write a 0. Now in every subsequent row, we look at the previous row and replace each occurrence of 0 with 01, and each occurrence of 1 with 10.
Given row N and index K, return the K-th indexed symbol in row N. (The values of K are 1-indexed.) (1 indexed).
Examples:
Input: N = 1, K = 1
Output: 0
... | class Solution:
def maxChunksToSorted(self, arr):
dict = {}
sortedArr = sorted(arr)
for i in range(len(sortedArr)):
if sortedArr[i] not in dict:
dict[sortedArr[i]] = [i]
else:
dict[sortedArr[i]].append(i)
position = []
... | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASS... |
On the first row, we write a 0. Now in every subsequent row, we look at the previous row and replace each occurrence of 0 with 01, and each occurrence of 1 with 10.
Given row N and index K, return the K-th indexed symbol in row N. (The values of K are 1-indexed.) (1 indexed).
Examples:
Input: N = 1, K = 1
Output: 0
... | class Solution:
def maxChunksToSorted(self, arr):
stack = []
for num in arr:
if len(stack) == 0 or stack[-1] <= num:
stack.append(num)
else:
maxnum = stack.pop()
while len(stack) > 0 and stack[-1] > num:
sta... | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR |
On the first row, we write a 0. Now in every subsequent row, we look at the previous row and replace each occurrence of 0 with 01, and each occurrence of 1 with 10.
Given row N and index K, return the K-th indexed symbol in row N. (The values of K are 1-indexed.) (1 indexed).
Examples:
Input: N = 1, K = 1
Output: 0
... | class Solution:
def maxChunksToSorted(self, arr):
mxvs, mnvs = [-1] * len(arr), [10**8 + 1] * len(arr)
for i, v in enumerate(arr):
if i == 0:
mxvs[i] = arr[i]
else:
mxvs[i] = max(mxvs[i - 1], v)
for i in range(len(arr))[::-1]:
... | CLASS_DEF FUNC_DEF ASSIGN VAR VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR BIN_OP LIST BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP F... |
On the first row, we write a 0. Now in every subsequent row, we look at the previous row and replace each occurrence of 0 with 01, and each occurrence of 1 with 10.
Given row N and index K, return the K-th indexed symbol in row N. (The values of K are 1-indexed.) (1 indexed).
Examples:
Input: N = 1, K = 1
Output: 0
... | class Solution:
def maxChunksToSorted(self, arr):
if arr is None:
return 0
if len(arr) < 2:
return len(arr)
suffix_min = [float("inf")] * len(arr)
suffix_min[-1] = arr[-1]
suffix_max = [float("-inf")] * len(arr)
suffix_max[-1] = arr[-1]
... | CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL V... |
On the first row, we write a 0. Now in every subsequent row, we look at the previous row and replace each occurrence of 0 with 01, and each occurrence of 1 with 10.
Given row N and index K, return the K-th indexed symbol in row N. (The values of K are 1-indexed.) (1 indexed).
Examples:
Input: N = 1, K = 1
Output: 0
... | class Solution:
def maxChunksToSorted(self, arr):
t = arr[0]
left_max = []
for x in arr:
t = max(t, x)
left_max.append(t)
t = arr[-1]
right_min = []
for i in range(len(arr) - 1, -1, -1):
t = min(arr[i], t)
right_min.app... | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUN... |
On the first row, we write a 0. Now in every subsequent row, we look at the previous row and replace each occurrence of 0 with 01, and each occurrence of 1 with 10.
Given row N and index K, return the K-th indexed symbol in row N. (The values of K are 1-indexed.) (1 indexed).
Examples:
Input: N = 1, K = 1
Output: 0
... | class Solution:
def maxChunksToSorted(self, arr):
stack = []
for e in reversed(arr):
min_so_far = e
while True:
if len(stack) == 0 or e <= stack[-1][0]:
stack.append([min_so_far, e])
break
if e > stack[-... | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER NUMBER RETURN FUNC_C... |
On the first row, we write a 0. Now in every subsequent row, we look at the previous row and replace each occurrence of 0 with 01, and each occurrence of 1 with 10.
Given row N and index K, return the K-th indexed symbol in row N. (The values of K are 1-indexed.) (1 indexed).
Examples:
Input: N = 1, K = 1
Output: 0
... | class Solution:
def maxChunksToSorted(self, arr):
sarr = sorted(arr)
table = {}
for i, n in enumerate(sarr):
if n not in table:
table[n] = [i, i]
else:
table[n][1] += 1
c = -1
chunks = 0
for i, n in enumerate(ar... | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER NUMBER ... |
Let's call two numbers similar if their binary representations contain the same number of digits equal to 1. For example:
* 2 and 4 are similar (binary representations are 10 and 100);
* 1337 and 4213 are similar (binary representations are 10100111001 and 1000001110101);
* 3 and 2 are not similar (binary repr... | import sys
readline = sys.stdin.readline
readlines = sys.stdin.readlines
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prn = lambda x: print(*x, sep="\n")
def solve():
n = ni()
a = nl()
mask... | IMPORT ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VA... |
Let's call two numbers similar if their binary representations contain the same number of digits equal to 1. For example:
* 2 and 4 are similar (binary representations are 10 and 100);
* 1337 and 4213 are similar (binary representations are 10100111001 and 1000001110101);
* 3 and 2 are not similar (binary repr... | def bitcount(x):
return 0 if x == 0 else x % 2 + bitcount(x // 2)
def core():
_ = int(input())
A = [int(v) for v in input().split()]
Hmask = 1073709056
Lmask = 32767
H = [(a >> 15) for a in A]
L = [(a & Lmask) for a in A]
Hcnt = {}
Lcnt = {}
for x in range(2**15):
hkey ... | FUNC_DEF RETURN VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR DICT A... |
Let's call two numbers similar if their binary representations contain the same number of digits equal to 1. For example:
* 2 and 4 are similar (binary representations are 10 and 100);
* 1337 and 4213 are similar (binary representations are 10100111001 and 1000001110101);
* 3 and 2 are not similar (binary repr... | import sys
def main():
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
dic1 = {}
dic2 = {}
A1 = [0] * N
for i, a in enumerate(A):
A1[i] = a >> 15
for i in range(2**15):
tmp = [0] * N
for j, a in enumerate(A1):
... | IMPORT FUNC_DEF IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSI... |
Let's call two numbers similar if their binary representations contain the same number of digits equal to 1. For example:
* 2 and 4 are similar (binary representations are 10 and 100);
* 1337 and 4213 are similar (binary representations are 10100111001 and 1000001110101);
* 3 and 2 are not similar (binary repr... | n = int(input())
d = [int(i) for i in input().split()]
l1 = [([1] * n) for i in range(15)]
l2 = [([1] * n) for i in range(15)]
vec_const1 = [0] * n
vec_const2 = [0] * n
for j in range(n):
dd = d[j]
for i in range(30):
if dd & 1 << i:
if i < 15:
vec_const1[j] += 1
... | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR V... |
Let's call two numbers similar if their binary representations contain the same number of digits equal to 1. For example:
* 2 and 4 are similar (binary representations are 10 and 100);
* 1337 and 4213 are similar (binary representations are 10100111001 and 1000001110101);
* 3 and 2 are not similar (binary repr... | n = int(input())
arr = list(map(int, input().split()))
msbs_achieved_by = {
tuple(bin(num >> 15 ^ x).count("1") for num in arr): x for x in range(1 << 15)
}
def main():
for x in range(1 << 15):
counts = [bin(x ^ num & (1 << 15) - 1).count("1") for num in arr]
for common_count in range(max(coun... | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR STRING VAR VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL FUNC_... |
Let's call two numbers similar if their binary representations contain the same number of digits equal to 1. For example:
* 2 and 4 are similar (binary representations are 10 and 100);
* 1337 and 4213 are similar (binary representations are 10100111001 and 1000001110101);
* 3 and 2 are not similar (binary repr... | H = 15
n = int(input())
a = list(map(int, input().split()))
def pc(v):
v = v - (v >> 1 & 1431655765)
v = (v & 858993459) + (v >> 2 & 858993459)
return ((v + (v >> 4) & 252645135) * 16843009 & 4294967295) >> 24
mask = (1 << H) - 1
rec = {}
for x in range(1 << H):
r = [pc(ai & mask ^ x) for ai in a]
... | ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER N... |
Let's call two numbers similar if their binary representations contain the same number of digits equal to 1. For example:
* 2 and 4 are similar (binary representations are 10 and 100);
* 1337 and 4213 are similar (binary representations are 10100111001 and 1000001110101);
* 3 and 2 are not similar (binary repr... | H = 15
MASK = (1 << H) - 1
n = int(input())
a = list(map(int, input().split()))
rec = {}
for x in range(1 << H):
r = [bin(ai & MASK ^ x).count("1") for ai in a]
d = tuple(r[-1] - ri for ri in r[:-1])
rec.setdefault(d, x)
ans = None
for x in range(1 << H):
r = [bin(ai >> H ^ x).count("1") for ai in a]
... | ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR STRING VAR VAR ASSIGN VAR FUNC_CALL ... |
Chef's new friend hErd gave him two functions f and g.
The function f is defined over x (x≥ 1) as:
f(x) = \begin{cases}
0, & \text{if } x = 1 \\
f( \frac{x}{2} ) + 1, & \text{if } x \text{ is even} \\
f( \lfloor \frac{x}{2} \rfloor ), & \text{if } x \text{ is odd} \\
\end{cases}
The function g is defined over x (x... | def FF(x):
return format(x, "b").count("0")
def GG(x):
pwr2 = x.bit_length()
return p_of_2[pwr2] + p_of_2[pwr2 - 1] - x - 1
def F_G(x):
pwr2 = x.bit_length()
return format(x, "b").count("0") + p_of_2[pwr2] + p_of_2[pwr2 - 1] - x - 1
p_of_2 = {i: (2**i) for i in range(31)}
for _ in range(int(in... | FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR VAR STRING STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL FUNC_CALL VAR VAR STRING STRING VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN ... |
Chef's new friend hErd gave him two functions f and g.
The function f is defined over x (x≥ 1) as:
f(x) = \begin{cases}
0, & \text{if } x = 1 \\
f( \frac{x}{2} ) + 1, & \text{if } x \text{ is even} \\
f( \lfloor \frac{x}{2} \rfloor ), & \text{if } x \text{ is odd} \\
\end{cases}
The function g is defined over x (x... | f = lambda x: bin(x).count("0") - 1
g = lambda x: (~x & (1 << x.bit_length()) - 1) + (1 << x.bit_length() - 1)
def solve(l, r):
p = 1
best = None
while p <= r:
if p >= l:
best = p
p <<= 1
if best is not None:
return f(best) + g(best)
return max(f(k) + g(k) for k... | ASSIGN VAR BIN_OP FUNC_CALL FUNC_CALL VAR VAR STRING NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR NUMBER BIN_OP NUMBER BIN_OP FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NONE WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NONE RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL... |
Chef's new friend hErd gave him two functions f and g.
The function f is defined over x (x≥ 1) as:
f(x) = \begin{cases}
0, & \text{if } x = 1 \\
f( \frac{x}{2} ) + 1, & \text{if } x \text{ is even} \\
f( \lfloor \frac{x}{2} \rfloor ), & \text{if } x \text{ is odd} \\
\end{cases}
The function g is defined over x (x... | t = int(input())
while t:
a, b = input().split()
a, b = int(a), int(b)
n = bin(b)[2:]
l = len(n)
x = 2 ** (l - 1)
if x >= a:
print(2 * x - 1 + l - 1)
elif a > x:
k = 2 * x - 1
n2 = bin(x)[2:]
mx = 0
for j in range(a, min(a + 20, b) + 1):
n1... | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMB... |
Chef's new friend hErd gave him two functions f and g.
The function f is defined over x (x≥ 1) as:
f(x) = \begin{cases}
0, & \text{if } x = 1 \\
f( \frac{x}{2} ) + 1, & \text{if } x \text{ is even} \\
f( \lfloor \frac{x}{2} \rfloor ), & \text{if } x \text{ is odd} \\
\end{cases}
The function g is defined over x (x... | a = int(input())
for i in range(a):
L, R = map(int, input().split())
ans = 0
x = bin(L)
y = bin(R)
p = len(x) - 2
q = len(y) - 2
if q > p:
ans = 2**q + q - 2
elif q == p:
A = []
b = min(R - L, 30)
c = 2**p - 1
d = x.count("0") - 1
g_L = c -... | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP ... |
Chef's new friend hErd gave him two functions f and g.
The function f is defined over x (x≥ 1) as:
f(x) = \begin{cases}
0, & \text{if } x = 1 \\
f( \frac{x}{2} ) + 1, & \text{if } x \text{ is even} \\
f( \lfloor \frac{x}{2} \rfloor ), & \text{if } x \text{ is odd} \\
\end{cases}
The function g is defined over x (x... | def f(n):
ans = 0
b = bin(n)[2:]
for i in b[1:]:
if i == "0":
ans += 1
return ans
def g(n):
ans = 1
b = bin(n)[2:]
for i in b[1:]:
ans *= 2
if i == "0":
ans += 1
return ans
def solver(l, r):
p = 1 << int(len(bin(r)) - 3)
x = max... | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR NUMBER IF VAR STRING VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR NUMBER VAR NUMBER IF VAR STRING VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC... |
Chef's new friend hErd gave him two functions f and g.
The function f is defined over x (x≥ 1) as:
f(x) = \begin{cases}
0, & \text{if } x = 1 \\
f( \frac{x}{2} ) + 1, & \text{if } x \text{ is even} \\
f( \lfloor \frac{x}{2} \rfloor ), & \text{if } x \text{ is odd} \\
\end{cases}
The function g is defined over x (x... | t = int(input())
def f(x):
if x == 1:
return 0
elif not x & 1:
return f(x // 2) + 1
else:
return f((x - 1) // 2)
def g(x):
if x == 1:
return 1
elif not x & 1:
return 2 * g(x // 2) + 1
else:
return 2 * g((x - 1) // 2)
def go(x):
return g(x... | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER... |
Chef's new friend hErd gave him two functions f and g.
The function f is defined over x (x≥ 1) as:
f(x) = \begin{cases}
0, & \text{if } x = 1 \\
f( \frac{x}{2} ) + 1, & \text{if } x \text{ is even} \\
f( \lfloor \frac{x}{2} \rfloor ), & \text{if } x \text{ is odd} \\
\end{cases}
The function g is defined over x (x... | t = int(input())
def f(n):
if n == 1:
return 0
elif n % 2 == 0:
return f(n // 2) + 1
else:
return f(n // 2)
def g(n):
if n == 1:
return 1
elif n % 2 == 0:
return 2 * g(n // 2) + 1
else:
return 2 * g(n // 2)
for _ in range(t):
l, r = map(i... | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER... |
Chef's new friend hErd gave him two functions f and g.
The function f is defined over x (x≥ 1) as:
f(x) = \begin{cases}
0, & \text{if } x = 1 \\
f( \frac{x}{2} ) + 1, & \text{if } x \text{ is even} \\
f( \lfloor \frac{x}{2} \rfloor ), & \text{if } x \text{ is odd} \\
\end{cases}
The function g is defined over x (x... | def LII():
return [int(x) for x in input().split()]
def candidates(m, ln):
if m == 0:
return [m]
rv = []
while m < 2**ln:
rv.append(m)
b = bin(m)[2:]
i = b.rindex("1")
m += 2 ** (len(b) - 1 - i)
return rv
def vl(x):
b = bin(x)[2:]
r = len(b) - 1
... | FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN LIST VAR ASSIGN VAR LIST WHILE VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN VAR FUNC_DEF ASS... |
Chef's new friend hErd gave him two functions f and g.
The function f is defined over x (x≥ 1) as:
f(x) = \begin{cases}
0, & \text{if } x = 1 \\
f( \frac{x}{2} ) + 1, & \text{if } x \text{ is even} \\
f( \lfloor \frac{x}{2} \rfloor ), & \text{if } x \text{ is odd} \\
\end{cases}
The function g is defined over x (x... | def base_2(x):
return bin(x)[2:]
def f_plus_g(x):
b = base_2(x)
k = len(b) - 1
c = b.count("0")
return c + 2 ** (k + 1) + 2**k - 1 - x
for _ in range(int(input())):
l, r = [int(x) for x in input().split()]
assert l >= 1 and l <= r
k = len(base_2(r)) - 1
if 2**k >= l:
prin... | FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR F... |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def graycode(self, n):
def rec(i, n, res):
if i == n:
return res[-1]
row = []
temp = res[-1]
for j in range(len(temp)):
row.append("0" + str(temp[j]))
for j in range(len(temp) - 1, -1, -1):
... | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR RETURN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR VAR VAR EXPR FU... |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def graycode(self, n):
N = n
res = []
for i in range(2**N):
s = str((i & 1 << N - 1) >> N - 1)
for j in range(1, N):
p = (i & 1 << N - j - 1) >> N - j - 1
q = (i & 1 << N - j) >> N - j
s += str(p ^ q)
... | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASS... |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def graycode(self, n):
for i in range(1 << n):
val = i ^ i >> 1
s = bin(val)[2:]
print(s.zfill(n), end=" ")
return [] | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING RETURN LIST |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def gcode(self, temp1, temp2):
for i in range(len(temp1)):
temp1[i] = "0" + temp1[i]
for j in range(len(temp2)):
temp2[j] = "1" + temp2[j]
return temp1, temp2
def graycode(self, n):
temp1 = ["0"]
temp2 = ["1"]
for i in ran... | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP STRING VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP STRING VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR LIST STRING ASSIGN VAR LIST STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSI... |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def graycode(self, n):
patterns = []
i = 0
while i < 1 << n:
val = i ^ i >> 1
gray = bin(val).replace("0b", "").zfill(n)
patterns.append(gray)
i += 1
return patterns | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR STRING STRING VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def graycode(self, n):
l = [[0, 1]]
for i in range(2, n + 1):
modified = [("0" + str(i)) for i in l[-1]]
modified_rev = [("1" + str(i)) for i in l[-1][::-1]]
l.append(modified + modified_rev)
return l[-1] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP STRING FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP STRING FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR NUMBER |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def graycode(self, n):
ans = []
for i in range(1 << n):
val = i ^ i >> 1
string = ""
while val:
string += str(val % 2)
val //= 2
while len(string) < n:
string += "0"
ans.appen... | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR STRING WHILE VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def graycode(self, n):
ans = ["0", "1"]
if n == 1:
return ans
for i in range(1, n):
add0 = ans[:]
for j in range(len(ans)):
add0[j] = "0" + add0[j]
add1 = ans[::-1]
for j in range(len(ans)):
... | CLASS_DEF FUNC_DEF ASSIGN VAR LIST STRING STRING IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP STRING VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP STRING VAR VAR ASSIGN VAR BIN_OP VAR ... |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def graycode(self, n):
arr = []
for i in range(2**n):
x = bin(i).replace("0b", "").rjust(n, "0")
a = x[0]
for j in range(1, len(x)):
a += str(int(x[j - 1]) ^ int(x[j]))
arr.append(a)
return arr | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR STRING STRING VAR STRING ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ... |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def graycode(self, n):
ans = []
for i in range(1 << n):
s = ["0"] * n
k = 0
for j in range(n):
if i & 1 << j:
s[n - 1 - j] = "1"
s = "".join(s)
g = s[0]
for k in range(1, n):
... | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR STRING ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR I... |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def graycode(self, n):
if n == 1:
return ["0", "1"]
ans = []
temp = self.graycode(n - 1)
for i in range(0, len(temp)):
bitString = "0" + temp[i]
ans.append(bitString)
for j in range(len(temp) - 1, -1, -1):
bitSt... | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN LIST STRING STRING ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP STRING VAR VA... |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def gray(self, n):
if n == 1:
l = ["0", "1"]
return l
l = self.gray(n - 1)
res = []
for i in l:
res.append("0" + i)
for i in reversed(l):
res.append("1" + i)
return res
def graycode(self, n):
... | CLASS_DEF FUNC_DEF IF VAR NUMBER ASSIGN VAR LIST STRING STRING RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def graycode(self, n):
a = ["0", "1"]
len_a = 2
for i in range(1, n):
for j in range(len_a - 1, -1, -1):
a.append("1" + a[j])
for j in range(len_a):
a[j] = "0" + a[j]
len_a *= 2
return a | CLASS_DEF FUNC_DEF ASSIGN VAR LIST STRING STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP STRING VAR VAR VAR NUMBER RETURN VAR |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def graycode(self, n):
ans = ["0", "1"]
if n == 1:
return ans
for i in range(2, n + 1):
new_ans = []
for j in ans:
new_ans.append("0" + j)
for j in ans[::-1]:
new_ans.append("1" + j)
... | CLASS_DEF FUNC_DEF ASSIGN VAR LIST STRING STRING IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR FOR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR ASSIGN VAR VAR RETURN VAR |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def graycode(self, n):
def funk(n):
if n == 1:
return ["0", "1"]
b = funk(n - 1)
c = []
for r in range(2 ** (n - 1)):
c += ["0" + b[r]]
for rr in range(2 ** (n - 1) - 1, -1, -1):
c +... | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER RETURN LIST STRING STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR LIST BIN_OP STRING VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER NUMBER NUMBER VAR LIST BIN_OP STRING VAR... |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def graycode(self, n):
def rec(n):
if n == 1:
return ["0", "1"]
temp = rec(n - 1)
ans = []
for i in range(len(temp)):
ans.append("0" + temp[i])
for i in range(len(temp) - 1, -1, -1):
... | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER RETURN LIST STRING STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR RET... |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def graycode(self, n):
arr = []
arr.append("0")
arr.append("1")
i = 2
j = 0
while True:
if i >= 1 << n:
break
for j in range(i - 1, -1, -1):
arr.append("1" + arr[j])
for j in range(0,... | CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP STRING VA... |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def graycode(self, n):
grey = []
i = 0
while i < 1 << n:
val = i ^ i >> 1
s = ""
j = 0
while j < n:
if val & 1 << j != 0:
s = "1" + s
else:
s = "0" + s
... | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP STRING VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ... |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def graycode(self, n):
l = []
for i in range(0, pow(2, n)):
l.append(bin(i ^ i >> 1)[2:].zfill(n))
return l | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR RETURN VAR |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def graycode(self, n):
ans = []
for i in range(1 << n):
val = i ^ i >> 1
bitrep = format(val, "016b")
ans.append(bitrep[16 - n :])
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP NUMBER VAR RETURN VAR |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def graycode(self, n):
length = 1 << n
output = []
for i in range(length):
num = i ^ i >> 1
bin_num = bin(num)[2:].zfill(n)
output.append(bin_num)
return output | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def graycode(self, n):
N = 2**n
code = "0" * n
result = [code]
for i in range(1, N):
for flip_bit in range(n, -1, -1):
if i % 2**flip_bit == 0:
flip_bit = n - flip_bit - 1
break
code = (
... | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR STRING STRING STRING VAR BIN_O... |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def graycode(self, n):
def helper(n):
if n == 1:
return ["0", "1"]
x = helper(n - 1)
res = []
for i in range(len(x)):
s = x[i]
res.append("0" + s)
for i in range(len(x) - 1, -1, -1):... | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER RETURN LIST STRING STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_... |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def graycode(self, n):
if n == 1:
arr = []
arr.append("0")
arr.append("1")
return arr
l = self.graycode(n - 1)
ans = []
for i in range(len(l)):
ans.append("0" + l[i])
for i in range(len(l) - 1, -1, -... | CLASS_DEF FUNC_DEF IF VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ... |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def graycode_gen(self, n: int):
if n == 0:
return ["0"]
if n == 1:
return ["0", "1"]
arr = self.graycode_gen(n - 1)
res = []
for i in arr:
res.append("0" + i)
for i in reversed(arr):
res.append("1" + i)
... | CLASS_DEF FUNC_DEF VAR IF VAR NUMBER RETURN LIST STRING IF VAR NUMBER RETURN LIST STRING STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def graycode(self, n):
if n == 1:
return ["0", "1"]
temp = self.graycode(n - 1)
size = len(temp)
temp1 = []
for i in range(size):
temp1.append("0" + temp[i])
for j in range(size - 1, -1, -1):
temp1.append("1" + temp... | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN LIST STRING STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR RETURN VAR |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def graycode(self, n):
def convert(string):
list1 = []
list1[:0] = string
return list1
arr = ["0" * n]
for i in range(n - 1, -1, -1):
arrLen = len(arr) - 1
for j in range(arrLen, -1, -1):
strtoarr ... | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER VAR RETURN VAR ASSIGN VAR LIST BIN_OP STRING VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR FUNC_C... |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def graycode(self, n):
res = ["0", "1"]
x, y = "0", "1"
for j in range(n - 1):
cur = []
for i in range(len(res)):
cur.append(res[i] + x)
cur.append(res[i] + y)
x, y = y, x
res = cur
r... | CLASS_DEF FUNC_DEF ASSIGN VAR LIST STRING STRING ASSIGN VAR VAR STRING STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def graycode(self, n):
if n == 1:
return ["0", "1"]
l1 = self.graycode(n - 1)
l2 = reversed(l1)
modl1 = [("0" + i) for i in l1]
modl2 = [("1" + i) for i in l2]
return modl1 + modl2 | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN LIST STRING STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING VAR VAR VAR ASSIGN VAR BIN_OP STRING VAR VAR VAR RETURN BIN_OP VAR VAR |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def graycode(self, n):
myList = ["0", "1"]
for i in range(n - 1):
for j in range(len(myList) - 1, -1, -1):
myList.append(myList[j])
for k in range(len(myList) // 2):
myList[k] = "0" + myList[k]
for l in range(len(my... | CLASS_DEF FUNC_DEF ASSIGN VAR LIST STRING STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP STRING VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VA... |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def graycode(self, n):
g = [(x ^ x >> 1) for x in range(1 << n)]
return ["{:0{n}b}".format(x, n=n) for x in g] | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR RETURN FUNC_CALL STRING VAR VAR VAR VAR |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | from itertools import permutations
class Solution:
def graycode(self, n):
if n == 1:
return ["0", "1"]
temp = self.graycode(n - 1)
ans = []
for i in temp:
ans.append("0" + i)
for i in range(len(temp) - 1, -1, -1):
ans.append("1" + temp[i... | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN LIST STRING STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR RETURN VAR |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def graycode(self, n):
if n < 1:
return []
out = []
out.append(format(0, "0" + str(n) + "b"))
c = int(math.pow(2, n))
for i in range(1, c):
v = i ^ i >> 1
out.append(format(v, "0" + str(n) + "b"))
return out | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP STRING FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP... |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def graycode(self, n):
if n == 0:
res = []
res.append("")
return res
prev = self.graycode(n - 1)
res = []
if len(prev) == 0:
res.append("0")
res.append("1")
else:
for i in range(len(prev)... | CLASS_DEF FUNC_DEF IF VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR STRING RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP V... |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def graycode(self, n):
res = ["0", "1"]
if n == 1:
return res
while n > 1:
var = list()
for ele in res:
var.append("0" + ele)
for ele in reversed(res):
var.append("1" + ele)
res = var... | CLASS_DEF FUNC_DEF ASSIGN VAR LIST STRING STRING IF VAR NUMBER RETURN VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def util(self, n, res):
if n == 1:
return ["0", "1"]
vals = self.util(n - 1, res)
ans = list()
for i in range(len(vals)):
ans.append("0" + vals[i])
vals = vals[::-1]
for i in range(len(vals)):
ans.append("1" + vals[... | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN LIST STRING STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR RETUR... |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def graycode(self, n):
output = []
padding = "0{}b".format(n)
code = "0" * n
for i in range(1 << n):
output.append(format(i ^ i >> 1, padding))
return output | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN VAR |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def graycode(self, n):
res = [0, 1]
if n == 1:
return res
else:
for i in range(1, n):
val = pow(2, i)
cur = []
for j in range(len(res) - 1, -1, -1):
cur.append(val + res[j])
... | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR VAR A... |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def graycode_gen(self, n: int):
if n == 0:
return ["0"]
if n == 1:
return ["0", "1"]
arr = self.graycode_gen(n - 1)
res = []
L = len(arr)
for i in range(L):
res.append("0" + arr[i])
for i in range(L - 1, -1,... | CLASS_DEF FUNC_DEF VAR IF VAR NUMBER RETURN LIST STRING IF VAR NUMBER RETURN LIST STRING STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL ... |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def graycode(self, n):
if n == 1:
return ["0", "1"]
else:
a = self.graycode(n - 1)
Final = []
for i in range(len(a)):
Final.append("0" + a[i])
Second = []
for i in range(len(a)):
... | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN LIST STRING STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR ASSIGN VAR VAR NUMBER EX... |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | def bina(st, ind, res, n):
if ind == n:
res.append(st)
return
bina(st + "0", ind + 1, res, n)
bina(st + "1", ind + 1, res, n)
class Solution:
def graycode(self, n):
res = []
ans = []
bina("", 0, res, n)
for i in range(len(res)):
st = res[i][... | FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR BIN_OP VAR STRING BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR STRING BIN_OP VAR NUMBER VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR STRING NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR V... |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def graycode(self, n):
def dec2bin(d):
return bin(d).replace("0b", "").zfill(n)
patterns = ["0" * n]
for i in range(1, 2**n):
b = dec2bin(i)
g = b[0]
for j in range(1, n):
g += str(int(b[j]) ^ int(b[j - 1]))
... | CLASS_DEF FUNC_DEF FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR STRING STRING VAR ASSIGN VAR LIST BIN_OP STRING VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR... |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def graycode(self, n):
l = ["0", "1"]
while len(l[-1]) != n:
b = []
for i in l:
if i.count("1") % 2 == 0:
b.append(i + "0")
b.append(i + "1")
else:
b.append(i + "1")
... | CLASS_DEF FUNC_DEF ASSIGN VAR LIST STRING STRING WHILE FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR LIST FOR VAR VAR IF BIN_OP FUNC_CALL VAR STRING NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR STRING ASSIGN VAR VAR ... |
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.
A Gray code sequence must begin with 0.
Example 1:
Input:
N = 2
Output:
00 01 11 10
Explanation:
00 and 01 differ by one bit.
01 and 11 differ by one bit.
11 and 10 also differ by one bit.
Example 2:
Input:
N... | class Solution:
def graycode(self, n):
ii = 1
temp = ["0", "1"]
if n == 1:
return temp
while ii < n:
tempp = []
for i in range(len(temp)):
tempp.append("0" + temp[i])
for i in range(len(temp) - 1, -1, -1):
... | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST STRING STRING IF VAR NUMBER RETURN VAR WHILE VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR ASSIGN V... |
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
Example:
Input: [1,2,2]
Output:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
] | class Solution:
def subsetsWithDup(self, nums):
def dfs(idx, path):
subsets.append(path)
for i in range(idx, len(nums)):
if i > idx and nums[i] == nums[i - 1]:
continue
dfs(i + 1, path + [nums[i]])
nums.sort()
sub... | CLASS_DEF FUNC_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER LIST RETURN VAR |
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
Example:
Input: [1,2,2]
Output:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
] | class Solution:
def subsetsWithDup(self, nums):
def backtrack(nums, start, tmp, res):
res.append(tmp[:])
for i in range(start, len(nums)):
if i > start and nums[i] == nums[i - 1]:
continue
else:
tmp.append(nums... | CLASS_DEF FUNC_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER LIST VAR RETURN VAR |
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
Example:
Input: [1,2,2]
Output:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
] | class Solution:
def subsetsWithDup(self, nums):
nums = sorted(nums)
res = [[]]
self._helpFun(nums, res, [])
return res
def _helpFun(self, nums, res, curr):
if not nums:
if curr not in res:
res.append(curr)
return
for i in ... | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST LIST EXPR FUNC_CALL VAR VAR VAR LIST RETURN VAR FUNC_DEF IF VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR LIST VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER V... |
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
Example:
Input: [1,2,2]
Output:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
] | class Solution:
def subsetsWithDup(self, nums):
def dfs(depth, start, cur):
if cur not in res:
res.append(cur)
if depth == len(nums):
return
for i in range(start, len(nums)):
dfs(depth + 1, i + 1, cur + [nums[i]])
... | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR LIST VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER NUMBER LIST RETURN FUNC_CALL VAR VAR |
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
Example:
Input: [1,2,2]
Output:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
] | class Solution:
def subsetsWithDup(self, nums):
result = []
res = []
self.df(nums, 0, result, res)
return res
def df(self, nums, idx, result, res):
if idx > len(nums):
return
if result not in res:
res.append(result)
for i in range... | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER VAR VAR RETURN VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR LIST VAR VAR VAR |
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
Example:
Input: [1,2,2]
Output:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
] | class Solution:
def subsets(self, nums):
if len(nums) == 0:
return [[]]
ret = []
for i, n in enumerate(nums):
if i > 0 and n == nums[i - 1]:
continue
for s in self.subsets(nums[i + 1 :]):
ret.append([n] + s)
return ... | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN LIST LIST ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR RETURN BIN_OP LIST LIST VAR FUNC_DEF EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example ... | class Solution:
def maxLength(self, arr: List[str]) -> int:
def valid(w):
return all(v <= 1 for v in collections.Counter(w).values())
def dfs(i, cand):
if i < 0:
return 0
res = dfs(i - 1, cand)
if valid(arr[i]) and all(c in cand for ... | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CA... |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example ... | class Solution:
def maxLength(self, arr: List[str]) -> int:
if len(arr) == 1:
if len(set(arr[0])) == len(arr[0]):
return len(arr[0])
else:
return 0
new_arr = []
for s in arr:
if len(s) == len(set(s)):
new_ar... | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR RETURN NU... |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example ... | class Solution:
def maxLength(self, arr: List[str]) -> int:
return self.backtrack(set(), arr, 0)
def backtrack(self, curr, arr, index):
if index >= len(arr):
return len(curr)
max_len = 0
for i in range(index, len(arr)):
char_set = set(arr[i])
... | CLASS_DEF FUNC_DEF VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUN... |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example ... | class Solution:
def maxLength(self, arr: List[str]) -> int:
def dfs(start, seen):
nonlocal res
if start == len(arr):
res = max(res, sum(len(arr[i]) for i in seen))
else:
is_end = True
for i in graph[start]:
... | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CA... |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example ... | class Solution:
def maxLength(self, arr: List[str]) -> int:
arr = [w for w in arr if len(w) == len(set(w))]
hm = {}
res = 0
dp = {(0): 0}
for i, w in enumerate(arr):
bitmask = 0
for c in set(w):
bit_set = 1 << ord(c) - ord("a")
... | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR VAR ASS... |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example ... | class Solution:
def maxLength(self, l: List[str]) -> int:
selected = set()
def ok(word, s) -> bool:
return not any(v in s for v in word) and len(word) == len(set(word))
def backTrack(l: List[str], curr_index: int, selected: set) -> int:
maxLength = 0
if... | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL V... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.