description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
An encoded string (s) is given, and the task is to decode it. The encoding pattern is that the occurrence of the string is given at the starting of the string and each string is enclosed by square brackets.
Note: The occurance of a single string is less than 1000.
Example 1:
Input: s = 1[b]
Output: b
Explaination: 'b' is present only one time.
Example 2:
Input: s = 3[b2[ca]]
Output: bcacabcacabcaca
Explaination: 2[ca] means 'ca' is repeated
twice which is 'caca' which concatenated with
'b' becomes 'bcaca'. This string repeated
thrice becomes the output.
Your Task:
You do not need to read input or print anything. Your task is to complete the function decodedString() which takes s as the input parameter and returns the decoded string.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{3} | class Solution:
def decodedString(self, s):
stk = []
ans = ""
i = 0
while i < len(s):
if s[i] == "]":
x = ""
while not stk[-1].isdigit():
x += stk.pop()
cnt = int(stk.pop())
j = 0
repeat = x
while j < cnt - 1:
x += repeat
j += 1
stk.append(x)
i += 1
elif s[i] != "[":
if s[i].isdigit():
d = ""
while s[i].isdigit():
d += s[i]
i += 1
stk.append(d)
else:
stk.append(s[i])
i += 1
else:
i += 1
while len(stk) > 0:
ans += stk.pop()
return ans[::-1] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR STRING WHILE FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR STRING IF FUNC_CALL VAR VAR ASSIGN VAR STRING WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR RETURN VAR NUMBER |
An encoded string (s) is given, and the task is to decode it. The encoding pattern is that the occurrence of the string is given at the starting of the string and each string is enclosed by square brackets.
Note: The occurance of a single string is less than 1000.
Example 1:
Input: s = 1[b]
Output: b
Explaination: 'b' is present only one time.
Example 2:
Input: s = 3[b2[ca]]
Output: bcacabcacabcaca
Explaination: 2[ca] means 'ca' is repeated
twice which is 'caca' which concatenated with
'b' becomes 'bcaca'. This string repeated
thrice becomes the output.
Your Task:
You do not need to read input or print anything. Your task is to complete the function decodedString() which takes s as the input parameter and returns the decoded string.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{3} | class Solution:
def decodedString(self, Str):
integerstack = []
stringstack = []
temp = ""
result = ""
i = 0
while i < len(Str):
count = 0
if Str[i] >= "0" and Str[i] <= "9":
while Str[i] >= "0" and Str[i] <= "9":
count = count * 10 + ord(Str[i]) - ord("0")
i += 1
i -= 1
integerstack.append(count)
elif Str[i] == "]":
temp = ""
count = 0
if len(integerstack) != 0:
count = integerstack[-1]
integerstack.pop()
while len(stringstack) != 0 and stringstack[-1] != "[":
temp = stringstack[-1] + temp
stringstack.pop()
if len(stringstack) != 0 and stringstack[-1] == "[":
stringstack.pop()
for j in range(count):
result = result + temp
for j in range(len(result)):
stringstack.append(result[j])
result = ""
elif Str[i] == "[":
if Str[i - 1] >= "0" and Str[i - 1] <= "9":
stringstack.append(Str[i])
else:
stringstack.append(Str[i])
integerstack.append(1)
else:
stringstack.append(Str[i])
i += 1
while len(stringstack) != 0:
result = stringstack[-1] + result
stringstack.pop()
return result | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR STRING VAR VAR STRING WHILE VAR VAR STRING VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR STRING IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR RETURN VAR |
An encoded string (s) is given, and the task is to decode it. The encoding pattern is that the occurrence of the string is given at the starting of the string and each string is enclosed by square brackets.
Note: The occurance of a single string is less than 1000.
Example 1:
Input: s = 1[b]
Output: b
Explaination: 'b' is present only one time.
Example 2:
Input: s = 3[b2[ca]]
Output: bcacabcacabcaca
Explaination: 2[ca] means 'ca' is repeated
twice which is 'caca' which concatenated with
'b' becomes 'bcaca'. This string repeated
thrice becomes the output.
Your Task:
You do not need to read input or print anything. Your task is to complete the function decodedString() which takes s as the input parameter and returns the decoded string.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{3} | class Solution:
def decodedString(self, s):
stack = []
digit = ""
for char in s:
if char == "]":
new_char = stack.pop()
new_string = ""
while new_char != "[":
new_string = new_char + new_string
new_char = stack.pop()
multipler = int(stack.pop())
stack.append(new_string * multipler)
elif char.isdigit():
digit += char
elif char == "[":
stack.append(digit)
stack.append("[")
digit = ""
else:
stack.append(char)
return "".join(stack) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING WHILE VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL STRING VAR |
An encoded string (s) is given, and the task is to decode it. The encoding pattern is that the occurrence of the string is given at the starting of the string and each string is enclosed by square brackets.
Note: The occurance of a single string is less than 1000.
Example 1:
Input: s = 1[b]
Output: b
Explaination: 'b' is present only one time.
Example 2:
Input: s = 3[b2[ca]]
Output: bcacabcacabcaca
Explaination: 2[ca] means 'ca' is repeated
twice which is 'caca' which concatenated with
'b' becomes 'bcaca'. This string repeated
thrice becomes the output.
Your Task:
You do not need to read input or print anything. Your task is to complete the function decodedString() which takes s as the input parameter and returns the decoded string.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{3} | class Solution:
def decodedString(self, s):
stack = []
curstr = ""
curnum = 0
for c in s:
if c.isdigit():
curnum = curnum * 10 + int(c)
elif c == "[":
stack.append([curnum, curstr])
curnum = 0
curstr = ""
elif c == "]":
pre_num, pre_str = stack.pop()
curstr = pre_str + curstr * pre_num
else:
curstr += c
return curstr | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING IF VAR STRING ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR RETURN VAR |
An encoded string (s) is given, and the task is to decode it. The encoding pattern is that the occurrence of the string is given at the starting of the string and each string is enclosed by square brackets.
Note: The occurance of a single string is less than 1000.
Example 1:
Input: s = 1[b]
Output: b
Explaination: 'b' is present only one time.
Example 2:
Input: s = 3[b2[ca]]
Output: bcacabcacabcaca
Explaination: 2[ca] means 'ca' is repeated
twice which is 'caca' which concatenated with
'b' becomes 'bcaca'. This string repeated
thrice becomes the output.
Your Task:
You do not need to read input or print anything. Your task is to complete the function decodedString() which takes s as the input parameter and returns the decoded string.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{3} | class Solution:
def decodedString(self, s):
string = ""
st = []
i = 0
while i < len(s):
if s[i] == "]":
currstring = ""
while len(st) > 0 and st[-1].isalpha():
currstring = st.pop() + currstring
currstring = int(st[-1]) * currstring
st.pop()
st.append(currstring)
elif s[i] != "[" and s[i] != "]" and s[i].isalpha():
st.append(s[i])
elif s[i] != "[" and s[i] != "]" and s[i].isalpha() == False:
digit = ""
while s[i].isdigit():
digit += s[i]
i += 1
i -= 1
st.append(digit)
i += 1
return "".join(st) | CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR STRING WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR STRING VAR VAR STRING FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN FUNC_CALL STRING VAR |
An encoded string (s) is given, and the task is to decode it. The encoding pattern is that the occurrence of the string is given at the starting of the string and each string is enclosed by square brackets.
Note: The occurance of a single string is less than 1000.
Example 1:
Input: s = 1[b]
Output: b
Explaination: 'b' is present only one time.
Example 2:
Input: s = 3[b2[ca]]
Output: bcacabcacabcaca
Explaination: 2[ca] means 'ca' is repeated
twice which is 'caca' which concatenated with
'b' becomes 'bcaca'. This string repeated
thrice becomes the output.
Your Task:
You do not need to read input or print anything. Your task is to complete the function decodedString() which takes s as the input parameter and returns the decoded string.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{3} | class Solution:
def decodedString(self, s):
n = len(s)
i = 0
chstack = []
intstack = []
res = ""
while i < n:
num = 0
if s[i].isnumeric():
while i < n and s[i].isnumeric():
num = num * 10 + int(s[i])
i += 1
intstack.append(num)
elif s[i] == "[":
if s[i - 1].isnumeric:
chstack.append(s[i])
else:
intstack.append(1)
chstack.append(s[i])
i += 1
elif s[i] == "]":
temp = ""
if len(intstack) != 0:
count = intstack[-1]
intstack.pop()
while len(chstack) != 0 and chstack[-1] != "[":
temp = chstack[-1] + temp
chstack.pop()
if len(chstack) != 0 and chstack[-1] == "[":
chstack.pop()
for j in range(count):
res = temp + res
for k in range(len(res)):
chstack.append(res[k])
res = ""
i += 1
else:
chstack.append(s[i])
i += 1
while len(chstack) != 0:
res = chstack[-1] + res
chstack.pop()
return res | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR STRING WHILE VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR WHILE VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR STRING IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR RETURN VAR |
An encoded string (s) is given, and the task is to decode it. The encoding pattern is that the occurrence of the string is given at the starting of the string and each string is enclosed by square brackets.
Note: The occurance of a single string is less than 1000.
Example 1:
Input: s = 1[b]
Output: b
Explaination: 'b' is present only one time.
Example 2:
Input: s = 3[b2[ca]]
Output: bcacabcacabcaca
Explaination: 2[ca] means 'ca' is repeated
twice which is 'caca' which concatenated with
'b' becomes 'bcaca'. This string repeated
thrice becomes the output.
Your Task:
You do not need to read input or print anything. Your task is to complete the function decodedString() which takes s as the input parameter and returns the decoded string.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{3} | class Solution:
def decodedString(self, s):
i = 0
n = len(s)
res = ""
while i < n:
if s[i].isdigit():
j = i
while s[j].isdigit():
j += 1
count = int(s[i:j])
k = j + 1
open_count = 1
while open_count > 0:
if s[k] == "[":
open_count += 1
elif s[k] == "]":
open_count -= 1
k += 1
substr = self.decodedString(s[j + 1 : k - 1])
res += count * substr
i = k
else:
res += s[i]
i += 1
return res | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING WHILE VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR |
An encoded string (s) is given, and the task is to decode it. The encoding pattern is that the occurrence of the string is given at the starting of the string and each string is enclosed by square brackets.
Note: The occurance of a single string is less than 1000.
Example 1:
Input: s = 1[b]
Output: b
Explaination: 'b' is present only one time.
Example 2:
Input: s = 3[b2[ca]]
Output: bcacabcacabcaca
Explaination: 2[ca] means 'ca' is repeated
twice which is 'caca' which concatenated with
'b' becomes 'bcaca'. This string repeated
thrice becomes the output.
Your Task:
You do not need to read input or print anything. Your task is to complete the function decodedString() which takes s as the input parameter and returns the decoded string.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{3} | class Solution:
def decodedString(self, s):
stack = []
i = 0
c, m = 0, 1
while i < len(s):
if ord("9") >= ord(s[i]) >= ord("0"):
c = c * m + int(s[i])
m = m * 10
elif s[i] == "[":
stack.append((c, i - len(str(c))))
stack.append((s[i], i))
c, m = 0, 1
elif s[i] == "]" and len(stack) > 0:
blank, stri = stack.pop()
totalCount, ci = stack.pop()
s = s[:ci] + totalCount * s[stri + 1 : i] + s[i + 1 :]
while ci < len(s):
if ord("9") >= ord(s[ci]) >= ord("0") or s[ci] == "]":
i = ci - 1
break
ci += 1
c, m = 0, 1
i += 1
return s | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR STRING FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER RETURN VAR |
An encoded string (s) is given, and the task is to decode it. The encoding pattern is that the occurrence of the string is given at the starting of the string and each string is enclosed by square brackets.
Note: The occurance of a single string is less than 1000.
Example 1:
Input: s = 1[b]
Output: b
Explaination: 'b' is present only one time.
Example 2:
Input: s = 3[b2[ca]]
Output: bcacabcacabcaca
Explaination: 2[ca] means 'ca' is repeated
twice which is 'caca' which concatenated with
'b' becomes 'bcaca'. This string repeated
thrice becomes the output.
Your Task:
You do not need to read input or print anything. Your task is to complete the function decodedString() which takes s as the input parameter and returns the decoded string.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{3} | class Solution:
def decodedString(self, s):
a = []
n = len(s)
i = 0
while i < n:
if s[i].isalpha():
a.append(s[i])
i += 1
elif s[i].isnumeric():
x = ""
while s[i].isnumeric():
x += s[i]
i += 1
a.append(x)
else:
if s[i] == "[":
a.append(s[i])
else:
x = ""
while a[-1] != "[":
x += a.pop(-1)
a.pop(-1)
a.append(int(a.pop(-1)) * x)
i += 1
x = ""
for i in a:
x += i
return x[::-1] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR STRING WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR STRING WHILE VAR NUMBER STRING VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER ASSIGN VAR STRING FOR VAR VAR VAR VAR RETURN VAR NUMBER |
An encoded string (s) is given, and the task is to decode it. The encoding pattern is that the occurrence of the string is given at the starting of the string and each string is enclosed by square brackets.
Note: The occurance of a single string is less than 1000.
Example 1:
Input: s = 1[b]
Output: b
Explaination: 'b' is present only one time.
Example 2:
Input: s = 3[b2[ca]]
Output: bcacabcacabcaca
Explaination: 2[ca] means 'ca' is repeated
twice which is 'caca' which concatenated with
'b' becomes 'bcaca'. This string repeated
thrice becomes the output.
Your Task:
You do not need to read input or print anything. Your task is to complete the function decodedString() which takes s as the input parameter and returns the decoded string.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{3} | class Solution:
def decodedString(self, s):
st = []
ans = ""
for i in range(len(s)):
if s[i] == "]":
x = ""
while st[-1] != "[":
x = st.pop() + x
st.pop()
k = ""
while st != [] and st[-1].isdigit():
k = st.pop() + k
y = int(k) * x
for i in y:
st.append(i)
else:
st.append(s[i])
while st != []:
ans = st.pop() + ans
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR STRING WHILE VAR NUMBER STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR STRING WHILE VAR LIST FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR WHILE VAR LIST ASSIGN VAR BIN_OP FUNC_CALL VAR VAR RETURN VAR |
An encoded string (s) is given, and the task is to decode it. The encoding pattern is that the occurrence of the string is given at the starting of the string and each string is enclosed by square brackets.
Note: The occurance of a single string is less than 1000.
Example 1:
Input: s = 1[b]
Output: b
Explaination: 'b' is present only one time.
Example 2:
Input: s = 3[b2[ca]]
Output: bcacabcacabcaca
Explaination: 2[ca] means 'ca' is repeated
twice which is 'caca' which concatenated with
'b' becomes 'bcaca'. This string repeated
thrice becomes the output.
Your Task:
You do not need to read input or print anything. Your task is to complete the function decodedString() which takes s as the input parameter and returns the decoded string.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{3} | def isNum(c):
return c >= "0" and c <= "9"
class Solution:
def decodedString(self, s):
ans = []
for i in range(len(s)):
c = s[i]
if c == "]":
curr = []
while len(ans) > 0 and ans[-1] != "[":
curr.append(ans[-1])
ans.pop()
ans.pop()
curr.reverse()
nums = ""
while len(ans) > 0 and isNum(ans[-1]):
nums += ans[-1]
ans.pop()
nums = int(nums[::-1])
ans.extend(curr * nums)
else:
ans.append(c)
return "".join(ans) | FUNC_DEF RETURN VAR STRING VAR STRING CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR STRING ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR STRING WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL STRING VAR |
An encoded string (s) is given, and the task is to decode it. The encoding pattern is that the occurrence of the string is given at the starting of the string and each string is enclosed by square brackets.
Note: The occurance of a single string is less than 1000.
Example 1:
Input: s = 1[b]
Output: b
Explaination: 'b' is present only one time.
Example 2:
Input: s = 3[b2[ca]]
Output: bcacabcacabcaca
Explaination: 2[ca] means 'ca' is repeated
twice which is 'caca' which concatenated with
'b' becomes 'bcaca'. This string repeated
thrice becomes the output.
Your Task:
You do not need to read input or print anything. Your task is to complete the function decodedString() which takes s as the input parameter and returns the decoded string.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{3} | class Solution:
def decodedString(self, s):
arr = []
i = len(s) - 1
while i >= 0:
if s[i] == "[":
ans = ""
while arr[-1] != "]":
ans += arr.pop()
arr.pop()
i -= 1
num = ""
while s[i].isdigit():
num += s[i]
i -= 1
arr.append(ans * int(num[::-1]))
else:
arr.append(s[i])
i -= 1
return "".join(arr) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING ASSIGN VAR STRING WHILE VAR NUMBER STRING VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN FUNC_CALL STRING VAR |
An encoded string (s) is given, and the task is to decode it. The encoding pattern is that the occurrence of the string is given at the starting of the string and each string is enclosed by square brackets.
Note: The occurance of a single string is less than 1000.
Example 1:
Input: s = 1[b]
Output: b
Explaination: 'b' is present only one time.
Example 2:
Input: s = 3[b2[ca]]
Output: bcacabcacabcaca
Explaination: 2[ca] means 'ca' is repeated
twice which is 'caca' which concatenated with
'b' becomes 'bcaca'. This string repeated
thrice becomes the output.
Your Task:
You do not need to read input or print anything. Your task is to complete the function decodedString() which takes s as the input parameter and returns the decoded string.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{3} | class Solution:
def decodedString(self, s):
stack_1 = []
for i in s:
if i != "]":
stack_1.append(i)
else:
temp = ""
while len(stack_1) > 0 and stack_1[-1] != "[":
temp = stack_1[-1] + temp
stack_1.pop()
stack_1.pop()
count = 0
power = 1
while len(stack_1) > 0:
num = stack_1[-1]
try:
num = int(stack_1[-1])
except:
break
num *= power
power *= 10
count += num
stack_1.pop()
while count > 0:
count -= 1
for c in temp:
stack_1.append(c)
ans1 = ""
while len(stack_1) > 0:
ans1 = stack_1[-1] + ans1
stack_1.pop()
return ans1 | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR RETURN VAR |
An encoded string (s) is given, and the task is to decode it. The encoding pattern is that the occurrence of the string is given at the starting of the string and each string is enclosed by square brackets.
Note: The occurance of a single string is less than 1000.
Example 1:
Input: s = 1[b]
Output: b
Explaination: 'b' is present only one time.
Example 2:
Input: s = 3[b2[ca]]
Output: bcacabcacabcaca
Explaination: 2[ca] means 'ca' is repeated
twice which is 'caca' which concatenated with
'b' becomes 'bcaca'. This string repeated
thrice becomes the output.
Your Task:
You do not need to read input or print anything. Your task is to complete the function decodedString() which takes s as the input parameter and returns the decoded string.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{3} | class Solution:
def decodedString(self, Str):
st = []
for i in range(len(s)):
if s[i] == "]":
temp = ""
while len(st) > 0 and st[-1] != "[":
temp = st[-1] + temp
st.pop()
st.pop()
num = ""
while len(st) > 0 and ord(st[-1]) >= 48 and ord(st[-1]) <= 57:
num = st[-1] + num
st.pop()
number = int(num)
repeat = ""
for j in range(number):
repeat += temp
st.extend(list(repeat))
else:
st.append(s[i])
return "".join(st) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR STRING WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR STRING WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL STRING VAR |
An encoded string (s) is given, and the task is to decode it. The encoding pattern is that the occurrence of the string is given at the starting of the string and each string is enclosed by square brackets.
Note: The occurance of a single string is less than 1000.
Example 1:
Input: s = 1[b]
Output: b
Explaination: 'b' is present only one time.
Example 2:
Input: s = 3[b2[ca]]
Output: bcacabcacabcaca
Explaination: 2[ca] means 'ca' is repeated
twice which is 'caca' which concatenated with
'b' becomes 'bcaca'. This string repeated
thrice becomes the output.
Your Task:
You do not need to read input or print anything. Your task is to complete the function decodedString() which takes s as the input parameter and returns the decoded string.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{3} | class Solution:
def decodedString(self, s):
res = ""
stack = []
num = 0
for c in s:
if c == "[":
stack.append((res, num))
num = 0
res = ""
elif c == "]":
n = stack[-1][1]
prev = stack[-1][0]
stack.pop()
res = prev + res * n
elif c.isdigit():
num = num * 10 + int(c)
else:
res += c
return res | CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING IF VAR STRING ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR RETURN VAR |
An encoded string (s) is given, and the task is to decode it. The encoding pattern is that the occurrence of the string is given at the starting of the string and each string is enclosed by square brackets.
Note: The occurance of a single string is less than 1000.
Example 1:
Input: s = 1[b]
Output: b
Explaination: 'b' is present only one time.
Example 2:
Input: s = 3[b2[ca]]
Output: bcacabcacabcaca
Explaination: 2[ca] means 'ca' is repeated
twice which is 'caca' which concatenated with
'b' becomes 'bcaca'. This string repeated
thrice becomes the output.
Your Task:
You do not need to read input or print anything. Your task is to complete the function decodedString() which takes s as the input parameter and returns the decoded string.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{3} | class Solution:
def decodedString(self, s):
f = []
c = 0
for i in range(len(s)):
if s[i] != "]":
f.append(s[i])
else:
flag = True
st = ""
while flag:
j = f.pop()
if j.isdigit() == True:
while len(f) != 0 and f[-1].isdigit():
j += f.pop()
st *= int(j[-1::-1])
flag = False
elif j != "[":
st += j
f.append(st)
res = ""
for i in f:
res += i[-1::-1]
return res | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING WHILE VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR STRING VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR VAR VAR VAR NUMBER NUMBER RETURN VAR |
An encoded string (s) is given, and the task is to decode it. The encoding pattern is that the occurrence of the string is given at the starting of the string and each string is enclosed by square brackets.
Note: The occurance of a single string is less than 1000.
Example 1:
Input: s = 1[b]
Output: b
Explaination: 'b' is present only one time.
Example 2:
Input: s = 3[b2[ca]]
Output: bcacabcacabcaca
Explaination: 2[ca] means 'ca' is repeated
twice which is 'caca' which concatenated with
'b' becomes 'bcaca'. This string repeated
thrice becomes the output.
Your Task:
You do not need to read input or print anything. Your task is to complete the function decodedString() which takes s as the input parameter and returns the decoded string.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{3} | class Solution:
def solve(self, prefix, s, i, n, repeat):
str = ""
str_start = i
while i < n:
if s[i].isdigit():
num = 0
prefix_ = i
while s[i].isdigit():
num += int(s[i])
num *= 10
i += 1
num //= 10
s, i = self.solve(prefix_, s, i + 1, n, num)
n = len(s)
elif s[i] == "]":
str = s[str_start:i]
str *= repeat
new_i = len(s[:prefix] + str)
s = s[:prefix] + str + s[i + 1 :]
return s, new_i
else:
i += 1
return s, i
def decodedString(self, s):
return self.solve(0, s, 0, len(s), 1)[0] | CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR VAR WHILE VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR NUMBER RETURN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER |
An encoded string (s) is given, and the task is to decode it. The encoding pattern is that the occurrence of the string is given at the starting of the string and each string is enclosed by square brackets.
Note: The occurance of a single string is less than 1000.
Example 1:
Input: s = 1[b]
Output: b
Explaination: 'b' is present only one time.
Example 2:
Input: s = 3[b2[ca]]
Output: bcacabcacabcaca
Explaination: 2[ca] means 'ca' is repeated
twice which is 'caca' which concatenated with
'b' becomes 'bcaca'. This string repeated
thrice becomes the output.
Your Task:
You do not need to read input or print anything. Your task is to complete the function decodedString() which takes s as the input parameter and returns the decoded string.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{3} | class Solution:
def decodedString(self, s):
stack = []
for i in s:
if i == "[":
stack.append(i)
elif i == "]":
sr = ""
nr = ""
while len(stack) != 0 and stack[-1] != "[":
sr = stack.pop() + sr
stack.pop()
while len(stack) != 0 and stack[-1].isnumeric() == True:
nr = stack.pop() + nr
sr = int(nr) * sr
stack.append(sr)
else:
stack.append(i)
return "".join(stack) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR EXPR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL STRING VAR |
An encoded string (s) is given, and the task is to decode it. The encoding pattern is that the occurrence of the string is given at the starting of the string and each string is enclosed by square brackets.
Note: The occurance of a single string is less than 1000.
Example 1:
Input: s = 1[b]
Output: b
Explaination: 'b' is present only one time.
Example 2:
Input: s = 3[b2[ca]]
Output: bcacabcacabcaca
Explaination: 2[ca] means 'ca' is repeated
twice which is 'caca' which concatenated with
'b' becomes 'bcaca'. This string repeated
thrice becomes the output.
Your Task:
You do not need to read input or print anything. Your task is to complete the function decodedString() which takes s as the input parameter and returns the decoded string.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{3} | class Solution:
def decodedString(self, s: str) -> str:
stack = []
for c in s:
if c == "]":
substr = ""
while stack[-1] != "[":
substr = stack.pop() + substr
stack.pop()
num = ""
while stack and stack[-1].isdigit():
num = stack.pop() + num
stack.append(int(num) * substr)
else:
stack.append(c)
return "".join(stack) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST FOR VAR VAR IF VAR STRING ASSIGN VAR STRING WHILE VAR NUMBER STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR STRING WHILE VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL STRING VAR VAR |
An encoded string (s) is given, and the task is to decode it. The encoding pattern is that the occurrence of the string is given at the starting of the string and each string is enclosed by square brackets.
Note: The occurance of a single string is less than 1000.
Example 1:
Input: s = 1[b]
Output: b
Explaination: 'b' is present only one time.
Example 2:
Input: s = 3[b2[ca]]
Output: bcacabcacabcaca
Explaination: 2[ca] means 'ca' is repeated
twice which is 'caca' which concatenated with
'b' becomes 'bcaca'. This string repeated
thrice becomes the output.
Your Task:
You do not need to read input or print anything. Your task is to complete the function decodedString() which takes s as the input parameter and returns the decoded string.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{3} | class Solution:
def decodedString(self, s):
num_stk = []
char_stk = []
num_str = ""
for i in s:
if i.isnumeric():
num_str += i
else:
if num_str:
num_stk.append(int(num_str))
num_str = ""
if i == "]":
temp_ans = ""
while char_stk[-1] != "[":
temp_ans = char_stk.pop(-1) + temp_ans
char_stk.pop(-1)
if len(char_stk) and char_stk[-1] != "[":
char_stk[-1] += temp_ans * num_stk.pop(-1)
else:
char_stk.append(temp_ans * num_stk.pop(-1))
else:
char_stk.append(i)
return char_stk[-1] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR VAR IF FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR STRING IF VAR STRING ASSIGN VAR STRING WHILE VAR NUMBER STRING ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER STRING VAR NUMBER BIN_OP VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER |
An encoded string (s) is given, and the task is to decode it. The encoding pattern is that the occurrence of the string is given at the starting of the string and each string is enclosed by square brackets.
Note: The occurance of a single string is less than 1000.
Example 1:
Input: s = 1[b]
Output: b
Explaination: 'b' is present only one time.
Example 2:
Input: s = 3[b2[ca]]
Output: bcacabcacabcaca
Explaination: 2[ca] means 'ca' is repeated
twice which is 'caca' which concatenated with
'b' becomes 'bcaca'. This string repeated
thrice becomes the output.
Your Task:
You do not need to read input or print anything. Your task is to complete the function decodedString() which takes s as the input parameter and returns the decoded string.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{3} | class Solution:
def decodedString(self, s):
val, res, stack = 0, "", []
for i in s:
if i.isdigit():
val = 10 * val + int(i)
elif i == "]":
res = stack.pop() + stack.pop() * res
elif i == "[":
stack.extend([val, res])
val = 0
res = ""
else:
res += i
return res | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER STRING LIST FOR VAR VAR IF FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING VAR VAR RETURN VAR |
An encoded string (s) is given, and the task is to decode it. The encoding pattern is that the occurrence of the string is given at the starting of the string and each string is enclosed by square brackets.
Note: The occurance of a single string is less than 1000.
Example 1:
Input: s = 1[b]
Output: b
Explaination: 'b' is present only one time.
Example 2:
Input: s = 3[b2[ca]]
Output: bcacabcacabcaca
Explaination: 2[ca] means 'ca' is repeated
twice which is 'caca' which concatenated with
'b' becomes 'bcaca'. This string repeated
thrice becomes the output.
Your Task:
You do not need to read input or print anything. Your task is to complete the function decodedString() which takes s as the input parameter and returns the decoded string.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{3} | class Solution:
def decodedString(self, s):
strings = []
nums = []
i = 0
while i < len(s):
num = ""
if ord(s[i]) >= ord("0") and ord(s[i]) <= ord("9"):
while ord(s[i]) >= ord("0") and ord(s[i]) <= ord("9"):
num += s[i]
i += 1
num = int(num)
nums.append(num)
if s[i] == "[" or s[i].isalpha():
strings.append(s[i])
if s[i] == "]":
number = 1
if len(nums) != 0:
number = nums.pop()
substr = ""
while strings[-1] != "[":
substr += strings.pop()
strings.pop()
strings.append(substr * number)
i += 1
res = ""
while len(strings) > 0:
res += strings.pop()
res = res[::-1]
return res | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR STRING IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING WHILE FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING WHILE VAR NUMBER STRING VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR STRING WHILE FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER RETURN VAR |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
output = array1 + array2
output.sort()
o = len(output)
if o % 2 == 1:
return output[o // 2]
else:
r = o // 2 - 1
ret1 = output[r]
ret2 = output[r + 1]
out = (ret2 + ret1) / 2
if out.is_integer():
return int(out)
else:
return out | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR RETURN FUNC_CALL VAR VAR RETURN VAR |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
arr3 = arr1 + arr2
arr3.sort()
l = len(arr3)
if len(arr3) % 2 == 1:
return arr3[l // 2]
else:
b = len(arr3) // 2
result = (arr3[b] + arr3[b - 1]) / 2
if result.is_integer():
return int(result)
else:
return result | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR RETURN FUNC_CALL VAR VAR RETURN VAR |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
m, n = len(array1), len(array2)
if m > n:
array1, array2, m, n = array2, array1, n, m
low, high = 0, m
while low <= high:
partitionX = (low + high) // 2
partitionY = (m + n + 1) // 2 - partitionX
maxLeftX = float("-inf") if partitionX == 0 else array1[partitionX - 1]
minRightX = float("inf") if partitionX == m else array1[partitionX]
maxLeftY = float("-inf") if partitionY == 0 else array2[partitionY - 1]
minRightY = float("inf") if partitionY == n else array2[partitionY]
if maxLeftX <= minRightY and maxLeftY <= minRightX:
if (m + n) % 2 == 0:
median = (max(maxLeftX, maxLeftY) + min(minRightX, minRightY)) / 2
return int(median) if median.is_integer() else median
else:
return max(maxLeftX, maxLeftY)
elif maxLeftX > minRightY:
high = partitionX - 1
else:
low = partitionX + 1 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR STRING VAR VAR IF VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, arr1, arr2):
n1, n2 = len(arr1), len(arr2)
if n2 < n1:
return self.MedianOfArrays(arr2, arr1)
low, high = 0, n1
while low <= high:
cut1 = (low + high) // 2
cut2 = (n1 + n2 + 1) // 2 - cut1
if cut1 == 0:
l1 = float("-inf")
else:
l1 = arr1[cut1 - 1]
if cut2 == 0:
l2 = float("-inf")
else:
l2 = arr2[cut2 - 1]
if cut1 == n1:
r1 = float("inf")
else:
r1 = arr1[cut1]
if cut2 == n2:
r2 = float("inf")
else:
r2 = arr2[cut2]
if l1 <= r2 and l2 <= r1:
if (n1 + n2) % 2 == 0:
sumi = max(l1, l2) + min(r1, r2)
if sumi % 2 == 0:
return sumi // 2
return sumi / 2
else:
return max(l1, l2)
elif l1 > r2:
high = cut1 - 1
else:
low = cut1 + 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR IF VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
array1.extend(array2)
array1.sort()
if len(array1) % 2 != 0:
return array1[len(array1) // 2]
elif (array1[len(array1) // 2] + array1[len(array1) // 2 - 1]) % 2 != 0:
return (array1[len(array1) // 2] + array1[len(array1) // 2 - 1]) / 2
else:
return (array1[len(array1) // 2] + array1[len(array1) // 2 - 1]) // 2 | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
if len(array1) > len(array2):
return self.MedianOfArrays(array2, array1)
m, n = len(array1), len(array2)
low, high = 0, m
while low <= high:
cut1 = (low + high) // 2
cut2 = (m + n + 1) // 2 - cut1
l1 = array1[cut1 - 1] if cut1 != 0 else -float("inf")
l2 = array2[cut2 - 1] if cut2 != 0 else -float("inf")
r1 = array1[cut1] if cut1 != m else float("inf")
r2 = array2[cut2] if cut2 != n else float("inf")
if l1 <= r2 and l2 <= r1:
if (n + m) % 2 == 0:
val = (max(l1, l2) + min(r1, r2)) / 2
if val == int(val):
return int(val)
return val
else:
return max(l1, l2)
elif l1 > r2:
high = cut1 - 1
else:
low = cut1 + 1
return 0 | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR STRING IF VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
i = 0
j = 0
arr = []
while i < len(array1) and j < len(array2):
if array1[i] < array2[j]:
arr.append(array1[i])
i += 1
else:
arr.append(array2[j])
j += 1
while i < len(array1):
arr.append(array1[i])
i += 1
while j < len(array2):
arr.append(array2[j])
j += 1
if len(arr) % 2 == 0:
a = (arr[len(arr) // 2] + arr[len(arr) // 2 - 1]) / 2
if a % 1 != 0:
pass
else:
a = int(a)
else:
a = arr[len(arr) // 2]
return a | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
array1[:] = array1[:] + array2[:]
array1.sort()
length = len(array1)
if length % 2 != 0:
return array1[len(array1) // 2]
else:
val = array1[length // 2 - 1] + array1[length // 2]
if val % 2 == 0:
return val // 2
else:
return val / 2 | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | import sys
class Solution:
def MedianOfArrays(self, a, b):
s = len(a) + len(b)
h = s // 2
if len(a) > len(b):
a, b = b, a
l = 0
r = len(a)
while l <= r:
mid1 = (l + r) // 2
mid2 = h - mid1
l1 = a[mid1 - 1] if mid1 - 1 >= 0 else -sys.maxsize
l2 = b[mid2 - 1] if mid2 - 1 >= 0 else -sys.maxsize
r1 = a[mid1] if mid1 < len(a) else sys.maxsize
r2 = b[mid2] if mid2 < len(b) else sys.maxsize
if l1 <= r2 and l2 <= r1:
if s % 2 == 0:
if (max(l1, l2) + min(r1, r2)) % 2 == 0:
return (max(l1, l2) + min(r1, r2)) // 2
return (max(l1, l2) + min(r1, r2)) / 2
return min(r1, r2)
if l1 > r2:
r = mid1 - 1
else:
l = mid1 + 1
return -1 | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
array1.extend(array2)
array1.sort()
if len(array1) % 2 == 0:
a = (array1[len(array1) // 2] + array1[len(array1) // 2 - 1]) / 2
if a == int(a):
return int(a)
return a
else:
return array1[len(array1) // 2] | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR RETURN VAR RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
m = len(array1)
n = len(array2)
if m > n:
return self.MedianOfArrays(array2, array1)
mini = -(2**31 - 1)
maxi = 2**31
k = m + n + 1 >> 1
lo = max(0, k - n)
hi = min(m, k)
while lo <= hi:
cut1 = lo + hi >> 1
cut2 = k - cut1
l1 = mini if cut1 == 0 else array1[cut1 - 1]
l2 = mini if cut2 == 0 else array2[cut2 - 1]
r1 = maxi if cut1 == m else array1[cut1]
r2 = maxi if cut2 == n else array2[cut2]
if l1 <= r2 and l2 <= r1:
if m + n & 1:
return max(l1, l2)
else:
temp = (max(l1, l2) + min(r1, r2)) / 2
if (max(l1, l2) + min(r1, r2)) % 2:
return temp
else:
return (max(l1, l2) + min(r1, r2)) // 2
elif l2 > r1:
lo = cut1 + 1
else:
hi = cut1 - 1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
array1.sort()
array2.sort()
arr = array1 + array2
arr.sort()
L = len(arr)
if L % 2 != 0:
i = L // 2
return arr[i]
else:
i = (arr[L // 2 - 1] + arr[L // 2]) / 2
if i - int(i) > 0:
return i
else:
return int(i) | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR FUNC_CALL VAR VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
x = array1 + array2
x.sort()
l = len(x)
if l % 2 == 0:
a = (x[l // 2] + x[l // 2 - 1]) / 2
if a % 1 != 0:
pass
else:
a = int(a)
else:
a = x[l // 2]
return a | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
for i in array2:
array1.append(i)
array1.sort()
c = len(array1)
if c % 2 != 0:
return array1[c // 2]
else:
m = array1[c // 2] + array1[c // 2 - 1]
if m % 2 == 0:
return m // 2
return m / 2 | CLASS_DEF FUNC_DEF FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, a1, a2):
a = sorted(a1 + a2)
n = len(a)
if n % 2 == 0:
return str((a[n // 2] + a[n // 2 - 1]) / 2).rstrip(".0")
else:
return a[n // 2] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER STRING RETURN VAR BIN_OP VAR NUMBER |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
arr = []
n = len(array1)
m = len(array2)
i, j = 0, 0
while i != n and j != m:
if array1[i] > array2[j]:
arr.append(array2[j])
j += 1
else:
arr.append(array1[i])
i += 1
if i == n:
while j != m:
arr.append(array2[j])
j += 1
else:
while i != n:
arr.append(array1[i])
i += 1
l = len(arr)
if l % 2 == 0:
a = arr[(l - 1) // 2]
b = arr[(l - 1) // 2 + 1]
if (a + b) / 2 == (a + b) // 2:
return (a + b) // 2
else:
return (a + b) / 2
else:
return arr[l // 2] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR BIN_OP VAR NUMBER |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
return (
sorted(array1 + array2)[len(array1 + array2) // 2]
if len(array1 + array2) & 1
else (
int(
(
sorted(array1 + array2)[len(array1 + array2) // 2 - 1]
+ sorted(array1 + array2)[len(array1 + array2) // 2]
)
/ 2
)
if (
sorted(array1 + array2)[len(array1 + array2) // 2 - 1]
+ sorted(array1 + array2)[len(array1 + array2) // 2]
)
/ 2
% 1
== 0
else (
sorted(array1 + array2)[len(array1 + array2) // 2 - 1]
+ sorted(array1 + array2)[len(array1 + array2) // 2]
)
/ 2
)
) | CLASS_DEF FUNC_DEF RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER NUMBER FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
arr = []
i = 0
j = 0
while i < len(array1) and j < len(array2):
if array1[i] <= array2[j]:
arr.append(array1[i])
i = i + 1
else:
arr.append(array2[j])
j = j + 1
while i < len(array1):
arr.append(array1[i])
i = i + 1
while j < len(array2):
arr.append(array2[j])
j = j + 1
if len(arr) % 2 != 0:
return arr[len(arr) // 2]
else:
m = len(arr) // 2
if (arr[m] + arr[m - 1]) % 2 == 0:
return (arr[m] + arr[m - 1]) // 2
else:
return (arr[m] + arr[m - 1]) / 2 | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
l = array1 + array2
l.sort()
n = len(l)
if n % 2 == 0:
x = l[int((n - 1) / 2)]
y = l[int(n / 2)]
if (x + y) % 2 == 0:
return (x + y) // 2
else:
return (x + y) / 2
else:
return l[n // 2] | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR BIN_OP VAR NUMBER |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
n = len(array1)
m = len(array2)
i = n - 1
j = 0
while i >= 0 and j < m:
if array1[i] >= array2[j]:
array1[i], array2[j] = array2[j], array1[i]
i -= 1
j += 1
else:
break
array1.sort()
array2.sort()
mid = int((n + m) / 2)
res = array1 + array2
if (m + n) % 2 == 0:
r = (res[mid - 1] + res[mid]) / 2
if r - int(r) == 0:
return int(r)
return r
return res[mid] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR RETURN VAR RETURN VAR VAR |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
newarr = array1 + array2
newarr.sort()
n = len(newarr)
if n % 2 != 0:
return newarr[int(n / 2)]
else:
v = (newarr[int(n / 2)] + newarr[int(n / 2) - 1]) / 2
k = int(v)
if v == k:
return k
else:
return v | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR RETURN VAR |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
new_arr = array1 + array2
new_arr.sort()
n = len(new_arr)
if n % 2 != 0:
left = 0
right = n
mid = (left + right) // 2
ans = new_arr[mid]
else:
left = 0
right = n // 2
ans = (new_arr[right] + new_arr[right - 1]) / 2
new_ans = int(ans)
if ans > new_ans:
return ans
return new_ans | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR RETURN VAR |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def median(self, merge, mid, even):
if even:
if (merge[mid] + merge[mid - 1]) % 2 == 0:
return (merge[mid] + merge[mid - 1]) // 2
else:
return (merge[mid] + merge[mid - 1]) / 2
else:
return merge[mid]
def MedianOfArrays(self, array1, array2):
merge = []
n = len(array1)
m = len(array2)
i, j = 0, 0
even = (n + m) % 2 == 0
mid = (n + m) // 2
k = 0
while i < n and j < m:
if array1[i] < array2[j]:
merge.append(array1[i])
i += 1
else:
merge.append(array2[j])
j += 1
k += 1
if k - 1 == mid:
return self.median(merge, k - 1, even)
while i < n:
merge.append(array1[i])
i += 1
k += 1
if k - 1 == mid:
return self.median(merge, k - 1, even)
while j < m:
merge.append(array2[j])
j += 1
k += 1
if k - 1 == mid:
return self.median(merge, k - 1, even) | CLASS_DEF FUNC_DEF IF VAR IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
i = 0
j = 0
k = 0
m = (len(array1) + len(array2)) // 2
na = []
if array1 == False:
return median(array2)
if array2 == False:
return median(array1)
while k <= m:
if i == len(array1):
na.append(array2[j])
j += 1
elif j == len(array2):
na.append(array1[i])
i += 1
elif array1[i] < array2[j]:
na.append(array1[i])
i += 1
else:
na.append(array2[j])
j += 1
k += 1
if (len(array1) + len(array2)) % 2 == 1:
return na[-1]
elif (na[-1] + na[-2]) % 2 == 0:
return (na[-1] + na[-2]) // 2
else:
return (na[-1] + na[-2]) / 2 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST IF VAR NUMBER RETURN FUNC_CALL VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR WHILE VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER RETURN BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER RETURN BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, arr1, arr2):
arr = arr1 + arr2
arr.sort()
if len(arr) % 2 != 0:
return arr[len(arr) // 2]
else:
res = (arr[len(arr) // 2] + arr[len(arr) // 2 - 1]) / 2
if int(res) == res:
return int(res)
return res | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR RETURN VAR |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
v = array1 + array2
v.sort()
l = len(v)
if l % 2 == 1:
return v[l // 2]
else:
if (v[l // 2] + v[l // 2 - 1]) % 2 == 0:
return (v[l // 2] + v[l // 2 - 1]) // 2
return (v[l // 2] + v[l // 2 - 1]) / 2 | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
arr = array1 + array2
arr.sort()
length = len(arr)
z = length // 2
if length % 2 == 1:
return arr[z]
elif length % 2 == 0:
xy = (arr[z - 1] + arr[z]) / 2
if xy.is_integer():
return int(xy)
else:
return xy | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR RETURN FUNC_CALL VAR VAR RETURN VAR |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, A, B):
n = len(A)
m = len(B)
if n > m:
return self.MedianOfArrays(B, A)
start = 0
end = n
realmidinmergedarray = (n + m + 1) // 2
while start <= end:
mid = (start + end) // 2
leftAsize = mid
leftBsize = realmidinmergedarray - mid
leftA = A[leftAsize - 1] if leftAsize > 0 else float("-inf")
leftB = B[leftBsize - 1] if leftBsize > 0 else float("-inf")
rightA = A[leftAsize] if leftAsize < n else float("inf")
rightB = B[leftBsize] if leftBsize < m else float("inf")
if leftA <= rightB and leftB <= rightA:
if (m + n) % 2 == 0:
median = (max(leftA, leftB) + min(rightA, rightB)) / 2.0
if median == int(median):
median = int(median)
return median
return max(leftA, leftB)
elif leftA > rightB:
end = mid - 1
else:
start = mid + 1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR STRING IF VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
n1, n2 = len(array1), len(array2)
total = n1 + n2
half = total // 2
if n2 < n1:
return self.MedianOfArrays(array2, array1)
l, r = 0, n1 - 1
while True:
i = (r + l) // 2
j = half - i - 2
left_1 = array1[i] if i >= 0 else float("-inf")
right_1 = array1[i + 1] if i + 1 < n1 else float("inf")
left_2 = array2[j] if j >= 0 else float("-inf")
right_2 = array2[j + 1] if j + 1 < n2 else float("inf")
if left_1 <= right_2 and left_2 <= right_1:
if total % 2 == 0:
sum = max(left_1, left_2) + min(right_1, right_2)
if sum % 2 == 0:
return sum // 2
return sum / 2
else:
return min(right_1, right_2)
elif left_1 > right_2:
r = i - 1
else:
l = i + 1 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING IF VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
merge = []
n = len(array1)
m = len(array2)
i, j = 0, 0
while i < n and j < m:
if array1[i] < array2[j]:
merge.append(array1[i])
i += 1
else:
merge.append(array2[j])
j += 1
while i < n:
merge.append(array1[i])
i += 1
while j < m:
merge.append(array2[j])
j += 1
if (n + m) % 2 == 0:
nm = (n + m) // 2
if (merge[nm] + merge[nm - 1]) % 2 == 0:
return (merge[nm] + merge[nm - 1]) // 2
else:
return (merge[nm] + merge[nm - 1]) / 2
else:
return merge[(n + m) // 2] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR BIN_OP BIN_OP VAR VAR NUMBER |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
arr = array1 + array2
arr.sort()
l = len(arr)
if l % 2 == 0:
med = arr[l // 2] + arr[l // 2 - 1]
if med / 2 > med // 2:
return med / 2
return med // 2
return arr[l // 2] | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN VAR BIN_OP VAR NUMBER |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
t = sorted(array1 + array2)
if len(t) % 2 == 0:
return str((t[len(t) // 2] + t[len(t) // 2 - 1]) / 2).rstrip(".0")
pass
else:
return t[len(t) // 2] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN FUNC_CALL FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER STRING RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def get_count(self, arrays, mid):
count = 0
for arr in arrays:
count += len([i for i in arr if i <= mid])
return count
def MedianOfArrays(self, array1, array2):
n1 = len(array1)
n2 = len(array2)
s = n1 + n2
pos = []
if s % 2 == 0:
pos.append(s // 2)
pos.append(s // 2 - 1)
else:
pos.append(s // 2)
ans = 0
for p in pos:
low = 0
high = 10**9
while low <= high:
mid = low + (high - low) // 2
count = self.get_count([array1, array2], mid)
if count <= p:
low = mid + 1
else:
high = mid - 1
ans += low
if s % 2 == 0:
ans = ans / 2
if ans == int(ans):
ans = int(ans)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
array1.extend(array2)
array1.sort()
if len(array1) % 2 != 0:
idx = len(array1) // 2
return array1[idx]
else:
idx = len(array1) // 2
ele = str((array1[idx - 1] + array1[idx]) / 2)
idx = ele.index(".")
if int(ele[idx + 1 :]) == 0:
return int(ele[:idx])
else:
return float(ele) | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
arr = [0] * (len(array1) + len(array2))
i, j, k = 0, 0, 0
while i < len(array1) and j < len(array2):
if array1[i] <= array2[j]:
arr[k] = array1[i]
i += 1
k += 1
else:
arr[k] = array2[j]
j += 1
k += 1
while i < len(array1):
arr[k] = array1[i]
k += 1
i += 1
while j < len(array2):
arr[k] = array2[j]
k += 1
j += 1
n = len(array1) + len(array2)
if n % 2 == 0:
k = (arr[n // 2 - 1] + arr[n // 2]) / 2
return int(k) if int(k) == k else k
else:
return arr[n // 2] | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR BIN_OP VAR NUMBER |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
if len(array1) > len(array2):
return self.MedianOfArrays(array2, array1)
n1 = len(array1)
n2 = len(array2)
midinmerged = (n1 + n2 + 1) // 2
low = 0
high = n1
while low <= high:
mid = (low + high) // 2
lcut1 = mid
lcut2 = midinmerged - mid
if lcut1 == 0:
l1 = -99989
else:
l1 = array1[lcut1 - 1]
if lcut2 == 0:
l2 = -99989
else:
l2 = array2[lcut2 - 1]
if lcut1 == n1:
r1 = 99989
else:
r1 = array1[lcut1]
if lcut2 == n2:
r2 = 99989
else:
r2 = array2[lcut2]
if l1 <= r2 and l2 <= r1:
if (n1 + n2) % 2 == 0:
median = (max(l1, l2) + min(r1, r2)) / 2
if median % 1 == 0:
return int(median)
else:
return median
else:
return max(l1, l2)
elif l1 > r2:
high = mid - 1
else:
low = mid + 1
return -1 | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
l1 = len(array1)
l2 = len(array2)
diff = abs(l2 - l1)
if diff % 2 == 0:
avg = True
else:
avg = False
if l1 == 1 and l2 == 1:
if (array1[0] + array2[0]) / 2 > (array1[0] + array2[0]) // 2:
return (array1[0] + array2[0]) / 2
return (array1[0] + array2[0]) // 2
i = 0
j = 0
First = True
count = -1
arr1 = []
cond = False
while i < l1 or j < l2:
if (cond and i < l1 or l2 == 0) or i < l1 and array1[i] < array2[j]:
arr1.append(array1[i])
i += 1
if i == l1:
cond = True
count += 1
elif l2 != 0:
if cond and j < l2 or j < l2:
arr1.append(array2[j])
j += 1
if j == l2:
cond = True
count += 1
if count == (l1 + l2) // 2:
break
if not avg:
return arr1[-1]
else:
idiv = (arr1[-1] + arr1[-2]) // 2
fdiv = (arr1[-1] + arr1[-2]) / 2
if fdiv > idiv:
return fdiv
return idiv | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER RETURN BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER RETURN BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR RETURN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER IF VAR VAR RETURN VAR RETURN VAR |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
a = array1 + array2
a = sorted(a)
n = len(a)
if n % 2 != 0:
d = n // 2
return a[d]
else:
b = n // 2
c = a[b] + a[b - 1]
if c % 2 == 0:
return c // 2
else:
return c / 2 | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
arr = array1 + array2
arr.sort()
a = len(arr)
if a % 2 != 0:
return arr[a // 2]
else:
k = (arr[a // 2 - 1] + arr[a // 2]) / 2
a = str(k)
b = a.index(".")
if a[b + 1] == "0":
return int(k)
else:
return k | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR BIN_OP VAR NUMBER STRING RETURN FUNC_CALL VAR VAR RETURN VAR |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
arr = sorted(array1) + sorted(array2)
arr.sort()
n = len(arr)
if n % 2 == 0:
z = n // 2
res = (arr[z] + arr[z - 1]) / 2
if int(res) == res:
return int(res)
else:
return res
return arr[n // 2] | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR RETURN VAR RETURN VAR BIN_OP VAR NUMBER |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
l = []
s = 0
for i in array1:
l.append(i)
for j in array2:
l.append(j)
l.sort()
n = len(l)
if n % 2 == 0:
d = n // 2
v = l[d] + l[d - 1]
s = v / 2
if s.is_integer():
return int(s)
else:
return s
else:
r = n // 2
t = l[r]
return t | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR RETURN FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, a, b):
ans = []
i = 0
j = 0
n = len(a)
m = len(b)
if n == 1 and m == 1:
return (a[0] + b[0]) // 2
while i < n and j < m:
if a[i] < b[j]:
ans.append(a[i])
i += 1
else:
ans.append(b[j])
j += 1
while i < n:
ans.append(a[i])
i += 1
while j < m:
ans.append(b[j])
j += 1
if (n + m) % 2 == 0:
x = (n + m) // 2 - 1
y = (n + m) // 2
z = (ans[x] + ans[y]) / 2
if z == int(z):
return int(z)
return z
else:
z = (m + n + 1) // 2 - 1
return ans[z] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER RETURN VAR VAR |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
b = []
for i in array1:
b.append(i)
for j in array2:
b.append(j)
b.sort()
n = len(b)
m = n // 2
med = (b[m] + b[m - 1]) / 2
if n % 2 == 0:
if med % 1 == 0:
med = int(med)
else:
med = float(med)
return med
else:
return int(b[m]) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
v = []
for i in array1:
v.append(i)
for i in array2:
v.append(i)
v.sort()
n = len(v)
if n % 2 == 0:
a = int(n / 2) - 1
k = (v[a] + v[a + 1]) / 2
if k - int(k) == 0:
return int(k)
else:
return k
else:
a = n
a = int((a - 1) / 2)
k = v[a]
if k - int(k) == 0:
return int(k)
else:
return k | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR RETURN VAR |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
if len(array2) < len(array1):
return self.MedianOfArrays(array2, array1)
low = 0
high = len(array1)
while low <= high:
cut1 = (low + high) // 2
cut2 = (len(array1) + len(array2) + 1) // 2 - cut1
if cut1 == 0:
left1 = -214787878
else:
left1 = array1[cut1 - 1]
if cut2 == 0:
left2 = -214787878
else:
left2 = array2[cut2 - 1]
if cut1 == len(array1):
right1 = 21478787807079
else:
right1 = array1[cut1]
if cut2 == len(array2):
right2 = 21478787807079
else:
right2 = array2[cut2]
if left1 <= right2 and left2 <= right1:
if (len(array1) + len(array2)) % 2 == 0:
if (max(left1, left2) + min(right1, right2)) % 2 == 0:
return (max(left1, left2) + min(right1, right2)) // 2
else:
return (max(left1, left2) + min(right1, right2)) / 2
else:
return max(left1, left2)
elif left1 > right2:
high = cut1 - 1
else:
low = cut1 + 1
return 0 | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR VAR IF BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, a1, a2):
x = a1 + a2
x.sort()
y = len(x)
if y % 2 != 0:
return x[y // 2]
else:
r = (x[y // 2] + x[y // 2 - 1]) / 2
r1 = (x[y // 2] + x[y // 2 - 1]) // 2
if r - r1 == 0.0:
return r1
else:
return r | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER RETURN VAR RETURN VAR |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
list1 = array1 + array2
list2 = sorted(list1)
if len(list2) % 2 != 0:
return list2[(len(list2) - 1) // 2]
m = (list2[(len(list2) - 1) // 2] + list2[(len(list2) - 1) // 2 + 1]) / 2
p = str(m)
list1 = p.split(".")
if list1[-1] == "0":
return int(list1[0])
return m | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER STRING RETURN FUNC_CALL VAR VAR NUMBER RETURN VAR |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
array1.extend(array2)
array1.sort()
x = len(array1)
y = x // 2
if x % 2 == 0:
z = array1[y - 1] + array1[y]
if z % 2 == 0:
k = z // 2
else:
k = z / 2
return k
else:
k = array1[y]
return k | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR VAR VAR RETURN VAR |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
array3 = []
array3 = array1 + array2
array3.sort()
a = len(array3)
if a % 2 == 0:
len1 = int((a - 1) / 2)
ans = (array3[len1] + array3[len1 + 1]) / 2
if int(ans) == ans:
return int(ans)
return ans
else:
len1 = int((a - 1) / 2)
return array3[len1] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN VAR VAR |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
for i in array2:
array1.append(i)
array1 = sorted(array1)
n = len(array1)
if n % 2 != 0:
return array1[n // 2]
else:
a = array1[n // 2 - 1]
b = array1[n // 2]
c = (a + b) / 2
if c == int(c):
return int(c)
else:
return c | CLASS_DEF FUNC_DEF FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR RETURN VAR |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | import sys
class Solution:
def MedianOfArrays(self, array1, array2):
n = len(array1)
m = len(array2)
if n > m:
return self.MedianOfArrays(array2, array1)
total_len = m + n
left = 0
right = n
result = 0
while left <= right:
cut1 = left + (right - left) // 2
cut2 = total_len // 2 - cut1
l1 = -sys.maxsize if cut1 == 0 else array1[cut1 - 1]
l2 = -sys.maxsize if cut2 == 0 else array2[cut2 - 1]
r1 = sys.maxsize if cut1 == n else array1[cut1]
r2 = sys.maxsize if cut2 == m else array2[cut2]
if l1 <= r2 and l2 <= r1:
if total_len % 2 != 0:
result = min(r1, r2)
break
else:
result = (max(l1, l2) + min(r1, r2)) / 2
if l1 > r2:
right = cut1 - 1
else:
left = cut1 + 1
return int(result) if result / int(result) == 1 else result | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR |
Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median.
Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints:
0 β€ m,n β€ 10^{6}
1 β€ array1[i], array2[i] β€ 10^{9} | class Solution:
def MedianOfArrays(self, array1, array2):
arr = array1 + array2
arr.sort()
m = len(arr) // 2
if len(arr) & 1:
return arr[m]
else:
s = arr[m - 1] + arr[m]
if s % 2:
return s / 2
else:
return s // 2 | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER |
Given a collection of distinct integers, return all possible permutations.
Example:
Input: [1,2,3]
Output:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
] | class Solution:
def permute(self, nums):
all_permutes = []
self.permute_nums(all_permutes, nums, [])
return all_permutes
def permute_nums(self, all_permutes, nums, cur_permute):
if len(nums) == 0:
all_permutes.append(cur_permute)
return
for i in range(len(nums)):
num = nums[i]
self.permute_nums(
all_permutes, nums[0:i] + nums[i + 1 : len(nums)], cur_permute + [num]
) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR LIST RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR LIST VAR |
Given a collection of distinct integers, return all possible permutations.
Example:
Input: [1,2,3]
Output:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
] | class Solution:
def permute(self, nums):
res = []
self.dfs(nums, [], res)
return res
def dfs(self, nums, path, res):
if not nums:
res.append(path)
for i in range(len(nums)):
self.dfs(nums[:i] + nums[i + 1 :], path + [nums[i]], res) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR LIST VAR RETURN VAR FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR VAR VAR |
Given a collection of distinct integers, return all possible permutations.
Example:
Input: [1,2,3]
Output:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
] | class Solution:
def permute(self, nums):
if not nums:
return []
nums.sort()
res = [nums[:]]
n = len(nums)
i = n - 1
while i > 0:
if nums[i - 1] < nums[i]:
j = n - 1
while nums[j] < nums[i - 1]:
j -= 1
nums[i - 1], nums[j] = nums[j], nums[i - 1]
nums[i:] = sorted(nums[i:])
res.append(nums[:])
i = n - 1
else:
i -= 1
return res | CLASS_DEF FUNC_DEF IF VAR RETURN LIST EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR |
Given a collection of distinct integers, return all possible permutations.
Example:
Input: [1,2,3]
Output:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
] | class Solution:
def permute(self, nums):
res = []
print(nums)
def swap(a, i, j):
temp = a[i]
a[i] = a[j]
a[j] = temp
def helper(index, path):
if index == len(nums) - 1:
res.append(path.copy())
for i in range(index, len(nums)):
swap(path, index, i)
helper(index + 1, path.copy())
helper(0, nums)
print(nums)
return res | CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Wet Shark once had 2 sequences:
{a_n}= {a_1, a_2, a_3, ... , a_(109)}
{b_n} = {b_1, b_2, b_3, ... , b_(109)}
However, he only kept one element from each sequence. Luckily, both the elements that Wet Shark kept have the same index in Wet Shark's sequences: that is, he took a_i and b_i for some 1 β€ i β€ 109.
Right after Wet Shark loses his sequences, he finds that he actually needs them to break the code of Cthulhu to escape a labyrinth. Cthulhu's code is a single floating point number Q. However, the code verifier is faulty. If Wet Shark enters any code c such that |c - Q| β€ 0.01 , Cthulhu's code checker will allow him to escape.
Wet Shark now starts to panic, and consults Dry Dolphin for help via ultrasonic waves. After the Dry Dolphin Sequence Processing Factory processes data of Wet Shark's sequences, the machines give Wet Shark the following 2 relations his sequences follow for all 1 β€ n < 109, where x = sqrt(2) and y = sqrt(3).
Wet Shark is now clueless on how to compute anything, and asks you for help.
Wet Shark has discovered that Cthulhu's code is actually defined as Q = (a_k + b_k) / (2^s), where s is a predetermined number, k is the index of another element in Wet Shark's sequence, and a_k, b_k are precisely the kth elements of Wet Shark's sequences {a_n} and {b_n}, respectively.
Given k, i, and the 2 elements of the arrays Wet Shark has lost, find any value of the code c that will allow Wet Shark to exit Cthulhu's labyrinth.
-----Input-----
The first line of input contains 3 space separated integers i, k, s β the common index of the two elements Wet Shark kept, the index of Wet Shark's array needed to break Cthulhu's code, and the number s described in the problem statement, respectively. It is guaranteed that Cthulhu's code, Q, is between -109 and 109 (both inclusive).
The second line of the input contains 2 space separated integers a_i and b_i, representing the ith element of sequence {a_n} and the ith element of sequence {b_n}, respectively.
-----Output-----
Output any number c that will crack Cthulhu's code. Recall that if Wet Shark enters any code c such that |c - Q| β€ 0.01 , Cthulhu's code checker will allow him to exit the labyrinth.
----- Constraints -----
- SUBTASK 1: 20 POINTS
- 1 β€ i β€ 103
- 1 β€ k β€ 103
- -103 β€ s β€ 103
- 1ββ€βa_i,βb_iββ€β103
- SUBTASK 2: 80 POINTS
- 1 β€ i β€ 1010
- 1 β€ k β€ 1010
- -1010 β€ s β€ 1010
- 1ββ€βa_i,βb_iββ€β1010
It is guaranteed that -1010 β€βQ β€β 1010.
-----Example-----
Input:
1 1 5
4 5
Output:
0.28125
-----Explanation-----
Example case 1. In this case, a_1 = 4, b_1 = 5, and s = 5. Cthulhu's code in this case is (a_1 + b_1) / (2s) = 9/32 = 0.28125. | X = pow(2, 0.5)
Y = pow(3, 0.5)
maxI = pow(10, 10)
maxK = pow(10, 10)
maxS = pow(10, 10)
def calculateCode(i, ai, bi, k, s):
c = 0
aii, bii = ai, bi
if (k - i) % 2 != 0:
aii, bii = X * (ai + bi) - X * Y * (ai - bi), X * (ai - bi) + X * Y * (ai + bi)
i += 1
exp = 2 * (k - i) - s
return (aii + bii) * pow(2, exp)
i, k, s = [int(x) for x in input().split()]
ai, bi = [int(x) for x in input().split()]
result = calculateCode(i, ai, bi, k, s)
print(result) | ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Wet Shark once had 2 sequences:
{a_n}= {a_1, a_2, a_3, ... , a_(109)}
{b_n} = {b_1, b_2, b_3, ... , b_(109)}
However, he only kept one element from each sequence. Luckily, both the elements that Wet Shark kept have the same index in Wet Shark's sequences: that is, he took a_i and b_i for some 1 β€ i β€ 109.
Right after Wet Shark loses his sequences, he finds that he actually needs them to break the code of Cthulhu to escape a labyrinth. Cthulhu's code is a single floating point number Q. However, the code verifier is faulty. If Wet Shark enters any code c such that |c - Q| β€ 0.01 , Cthulhu's code checker will allow him to escape.
Wet Shark now starts to panic, and consults Dry Dolphin for help via ultrasonic waves. After the Dry Dolphin Sequence Processing Factory processes data of Wet Shark's sequences, the machines give Wet Shark the following 2 relations his sequences follow for all 1 β€ n < 109, where x = sqrt(2) and y = sqrt(3).
Wet Shark is now clueless on how to compute anything, and asks you for help.
Wet Shark has discovered that Cthulhu's code is actually defined as Q = (a_k + b_k) / (2^s), where s is a predetermined number, k is the index of another element in Wet Shark's sequence, and a_k, b_k are precisely the kth elements of Wet Shark's sequences {a_n} and {b_n}, respectively.
Given k, i, and the 2 elements of the arrays Wet Shark has lost, find any value of the code c that will allow Wet Shark to exit Cthulhu's labyrinth.
-----Input-----
The first line of input contains 3 space separated integers i, k, s β the common index of the two elements Wet Shark kept, the index of Wet Shark's array needed to break Cthulhu's code, and the number s described in the problem statement, respectively. It is guaranteed that Cthulhu's code, Q, is between -109 and 109 (both inclusive).
The second line of the input contains 2 space separated integers a_i and b_i, representing the ith element of sequence {a_n} and the ith element of sequence {b_n}, respectively.
-----Output-----
Output any number c that will crack Cthulhu's code. Recall that if Wet Shark enters any code c such that |c - Q| β€ 0.01 , Cthulhu's code checker will allow him to exit the labyrinth.
----- Constraints -----
- SUBTASK 1: 20 POINTS
- 1 β€ i β€ 103
- 1 β€ k β€ 103
- -103 β€ s β€ 103
- 1ββ€βa_i,βb_iββ€β103
- SUBTASK 2: 80 POINTS
- 1 β€ i β€ 1010
- 1 β€ k β€ 1010
- -1010 β€ s β€ 1010
- 1ββ€βa_i,βb_iββ€β1010
It is guaranteed that -1010 β€βQ β€β 1010.
-----Example-----
Input:
1 1 5
4 5
Output:
0.28125
-----Explanation-----
Example case 1. In this case, a_1 = 4, b_1 = 5, and s = 5. Cthulhu's code in this case is (a_1 + b_1) / (2s) = 9/32 = 0.28125. | k, i, s = map(int, input().split())
ai, bi = map(int, input().split())
if i == k:
print((ai + bi) / pow(2, s))
else:
if (i - k) % 2 == 1:
si = (ai + 1.7320508075688772 * bi) / (4 * 1.4142135623730951)
i = int((i - k + 1) / 2)
else:
si = ai + bi
i = int((i - k) / 2)
print(si * pow(2, 4 * i - s)) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR VAR |
Wet Shark once had 2 sequences:
{a_n}= {a_1, a_2, a_3, ... , a_(109)}
{b_n} = {b_1, b_2, b_3, ... , b_(109)}
However, he only kept one element from each sequence. Luckily, both the elements that Wet Shark kept have the same index in Wet Shark's sequences: that is, he took a_i and b_i for some 1 β€ i β€ 109.
Right after Wet Shark loses his sequences, he finds that he actually needs them to break the code of Cthulhu to escape a labyrinth. Cthulhu's code is a single floating point number Q. However, the code verifier is faulty. If Wet Shark enters any code c such that |c - Q| β€ 0.01 , Cthulhu's code checker will allow him to escape.
Wet Shark now starts to panic, and consults Dry Dolphin for help via ultrasonic waves. After the Dry Dolphin Sequence Processing Factory processes data of Wet Shark's sequences, the machines give Wet Shark the following 2 relations his sequences follow for all 1 β€ n < 109, where x = sqrt(2) and y = sqrt(3).
Wet Shark is now clueless on how to compute anything, and asks you for help.
Wet Shark has discovered that Cthulhu's code is actually defined as Q = (a_k + b_k) / (2^s), where s is a predetermined number, k is the index of another element in Wet Shark's sequence, and a_k, b_k are precisely the kth elements of Wet Shark's sequences {a_n} and {b_n}, respectively.
Given k, i, and the 2 elements of the arrays Wet Shark has lost, find any value of the code c that will allow Wet Shark to exit Cthulhu's labyrinth.
-----Input-----
The first line of input contains 3 space separated integers i, k, s β the common index of the two elements Wet Shark kept, the index of Wet Shark's array needed to break Cthulhu's code, and the number s described in the problem statement, respectively. It is guaranteed that Cthulhu's code, Q, is between -109 and 109 (both inclusive).
The second line of the input contains 2 space separated integers a_i and b_i, representing the ith element of sequence {a_n} and the ith element of sequence {b_n}, respectively.
-----Output-----
Output any number c that will crack Cthulhu's code. Recall that if Wet Shark enters any code c such that |c - Q| β€ 0.01 , Cthulhu's code checker will allow him to exit the labyrinth.
----- Constraints -----
- SUBTASK 1: 20 POINTS
- 1 β€ i β€ 103
- 1 β€ k β€ 103
- -103 β€ s β€ 103
- 1ββ€βa_i,βb_iββ€β103
- SUBTASK 2: 80 POINTS
- 1 β€ i β€ 1010
- 1 β€ k β€ 1010
- -1010 β€ s β€ 1010
- 1ββ€βa_i,βb_iββ€β1010
It is guaranteed that -1010 β€βQ β€β 1010.
-----Example-----
Input:
1 1 5
4 5
Output:
0.28125
-----Explanation-----
Example case 1. In this case, a_1 = 4, b_1 = 5, and s = 5. Cthulhu's code in this case is (a_1 + b_1) / (2s) = 9/32 = 0.28125. | i_k_s = input().split()
i = int(i_k_s[0])
k = int(i_k_s[1])
s = int(i_k_s[2])
a_b = input().split()
a = int(a_b[0])
b = int(a_b[1])
if (k - i) % 2 == 0:
x = 2 ** (2 * (k - i) - s) * (a + b)
else:
a1 = 2 ** (1 / 2.0) * (a + b) - 6 ** (1 / 2.0) * (a - b)
b1 = 2 ** (1 / 2.0) * (a - b) + 6 ** (1 / 2.0) * (a + b)
x = 2 ** (2 * (k - i - 1) - s) * (a1 + b1)
print(x) | ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP BIN_OP NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER BIN_OP VAR VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER BIN_OP VAR VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Wet Shark once had 2 sequences:
{a_n}= {a_1, a_2, a_3, ... , a_(109)}
{b_n} = {b_1, b_2, b_3, ... , b_(109)}
However, he only kept one element from each sequence. Luckily, both the elements that Wet Shark kept have the same index in Wet Shark's sequences: that is, he took a_i and b_i for some 1 β€ i β€ 109.
Right after Wet Shark loses his sequences, he finds that he actually needs them to break the code of Cthulhu to escape a labyrinth. Cthulhu's code is a single floating point number Q. However, the code verifier is faulty. If Wet Shark enters any code c such that |c - Q| β€ 0.01 , Cthulhu's code checker will allow him to escape.
Wet Shark now starts to panic, and consults Dry Dolphin for help via ultrasonic waves. After the Dry Dolphin Sequence Processing Factory processes data of Wet Shark's sequences, the machines give Wet Shark the following 2 relations his sequences follow for all 1 β€ n < 109, where x = sqrt(2) and y = sqrt(3).
Wet Shark is now clueless on how to compute anything, and asks you for help.
Wet Shark has discovered that Cthulhu's code is actually defined as Q = (a_k + b_k) / (2^s), where s is a predetermined number, k is the index of another element in Wet Shark's sequence, and a_k, b_k are precisely the kth elements of Wet Shark's sequences {a_n} and {b_n}, respectively.
Given k, i, and the 2 elements of the arrays Wet Shark has lost, find any value of the code c that will allow Wet Shark to exit Cthulhu's labyrinth.
-----Input-----
The first line of input contains 3 space separated integers i, k, s β the common index of the two elements Wet Shark kept, the index of Wet Shark's array needed to break Cthulhu's code, and the number s described in the problem statement, respectively. It is guaranteed that Cthulhu's code, Q, is between -109 and 109 (both inclusive).
The second line of the input contains 2 space separated integers a_i and b_i, representing the ith element of sequence {a_n} and the ith element of sequence {b_n}, respectively.
-----Output-----
Output any number c that will crack Cthulhu's code. Recall that if Wet Shark enters any code c such that |c - Q| β€ 0.01 , Cthulhu's code checker will allow him to exit the labyrinth.
----- Constraints -----
- SUBTASK 1: 20 POINTS
- 1 β€ i β€ 103
- 1 β€ k β€ 103
- -103 β€ s β€ 103
- 1ββ€βa_i,βb_iββ€β103
- SUBTASK 2: 80 POINTS
- 1 β€ i β€ 1010
- 1 β€ k β€ 1010
- -1010 β€ s β€ 1010
- 1ββ€βa_i,βb_iββ€β1010
It is guaranteed that -1010 β€βQ β€β 1010.
-----Example-----
Input:
1 1 5
4 5
Output:
0.28125
-----Explanation-----
Example case 1. In this case, a_1 = 4, b_1 = 5, and s = 5. Cthulhu's code in this case is (a_1 + b_1) / (2s) = 9/32 = 0.28125. | x = pow(2, 0.5)
y = pow(3, 0.5)
i, k, s = [int(h) for h in input().split()]
a, b = [int(h) for h in input().split()]
a = a
b = b
if i == k:
c = (a + b) / pow(2, s)
elif i < k:
a1 = x * (a + b) - x * y * (a - b)
b1 = x * (a - b) + x * y * (a + b)
if (k - i) % 2 == 1:
l = 2 * (k - i - 1) - s
c = (a1 + b1) * pow(2, l)
else:
l = 2 * (k - i) - s
c = (a + b) * pow(2, l)
else:
d = 2 * x * (1 + y * y)
a1 = (a * (1 - y) + b * (1 + y)) / d
b1 = (a * (1 + y) + b * (y - 1)) / d
if (i - k) % 2 == 1:
l = 2 * (k - i + 1) - s
c = (a1 + b1) * pow(2, l)
else:
l = 2 * (k - i) - s
c = (a + b) * pow(2, l)
print(c * 1.0) | ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP VAR BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
Wet Shark once had 2 sequences:
{a_n}= {a_1, a_2, a_3, ... , a_(109)}
{b_n} = {b_1, b_2, b_3, ... , b_(109)}
However, he only kept one element from each sequence. Luckily, both the elements that Wet Shark kept have the same index in Wet Shark's sequences: that is, he took a_i and b_i for some 1 β€ i β€ 109.
Right after Wet Shark loses his sequences, he finds that he actually needs them to break the code of Cthulhu to escape a labyrinth. Cthulhu's code is a single floating point number Q. However, the code verifier is faulty. If Wet Shark enters any code c such that |c - Q| β€ 0.01 , Cthulhu's code checker will allow him to escape.
Wet Shark now starts to panic, and consults Dry Dolphin for help via ultrasonic waves. After the Dry Dolphin Sequence Processing Factory processes data of Wet Shark's sequences, the machines give Wet Shark the following 2 relations his sequences follow for all 1 β€ n < 109, where x = sqrt(2) and y = sqrt(3).
Wet Shark is now clueless on how to compute anything, and asks you for help.
Wet Shark has discovered that Cthulhu's code is actually defined as Q = (a_k + b_k) / (2^s), where s is a predetermined number, k is the index of another element in Wet Shark's sequence, and a_k, b_k are precisely the kth elements of Wet Shark's sequences {a_n} and {b_n}, respectively.
Given k, i, and the 2 elements of the arrays Wet Shark has lost, find any value of the code c that will allow Wet Shark to exit Cthulhu's labyrinth.
-----Input-----
The first line of input contains 3 space separated integers i, k, s β the common index of the two elements Wet Shark kept, the index of Wet Shark's array needed to break Cthulhu's code, and the number s described in the problem statement, respectively. It is guaranteed that Cthulhu's code, Q, is between -109 and 109 (both inclusive).
The second line of the input contains 2 space separated integers a_i and b_i, representing the ith element of sequence {a_n} and the ith element of sequence {b_n}, respectively.
-----Output-----
Output any number c that will crack Cthulhu's code. Recall that if Wet Shark enters any code c such that |c - Q| β€ 0.01 , Cthulhu's code checker will allow him to exit the labyrinth.
----- Constraints -----
- SUBTASK 1: 20 POINTS
- 1 β€ i β€ 103
- 1 β€ k β€ 103
- -103 β€ s β€ 103
- 1ββ€βa_i,βb_iββ€β103
- SUBTASK 2: 80 POINTS
- 1 β€ i β€ 1010
- 1 β€ k β€ 1010
- -1010 β€ s β€ 1010
- 1ββ€βa_i,βb_iββ€β1010
It is guaranteed that -1010 β€βQ β€β 1010.
-----Example-----
Input:
1 1 5
4 5
Output:
0.28125
-----Explanation-----
Example case 1. In this case, a_1 = 4, b_1 = 5, and s = 5. Cthulhu's code in this case is (a_1 + b_1) / (2s) = 9/32 = 0.28125. | def main():
line = list(map(int, input().split()))
i = line[0]
k = line[1]
s = line[2]
line = list(map(int, input().split()))
aI = line[0]
bI = line[1]
root2 = 2**0.5
root3 = 3**0.5
root6 = float(root2) * float(root3)
if i % 2 == 0:
powerOf2 = 2 * i
if k % 2 == 0:
powerOf2InQ = s - 2 * k + powerOf2
print("%.4f" % (float(aI + bI) / float(2**powerOf2InQ)))
else:
powerOf2InQ = s - 2 * (k - 1) + powerOf2
aPlusB = aI + bI
aMinusB = aI - bI
a1 = root2 * aPlusB - root6 * aMinusB
b1 = root2 * aMinusB + root6 * aPlusB
print("%.4f" % (float(a1 + b1) / float(2**powerOf2InQ)))
else:
powerOf2 = 2 * (i - 1)
if k % 2 == 1:
powerOf2InQ = s - 2 * (k - 1) + powerOf2
print("%.4f" % (float(aI + bI) / float(2**powerOf2InQ)))
else:
powerOf2InQ = s - 2 * k + powerOf2
a0 = ((2 + root3) * bI - aI) / (4 * root2 * (1 + root3))
b0 = (aI * (1 + root3) - bI * (1 - root3)) / (8 * root2)
print("%.4f" % (float(a0 + b0) / float(2**powerOf2InQ)))
return 0
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR RETURN NUMBER EXPR FUNC_CALL VAR |
Wet Shark once had 2 sequences:
{a_n}= {a_1, a_2, a_3, ... , a_(109)}
{b_n} = {b_1, b_2, b_3, ... , b_(109)}
However, he only kept one element from each sequence. Luckily, both the elements that Wet Shark kept have the same index in Wet Shark's sequences: that is, he took a_i and b_i for some 1 β€ i β€ 109.
Right after Wet Shark loses his sequences, he finds that he actually needs them to break the code of Cthulhu to escape a labyrinth. Cthulhu's code is a single floating point number Q. However, the code verifier is faulty. If Wet Shark enters any code c such that |c - Q| β€ 0.01 , Cthulhu's code checker will allow him to escape.
Wet Shark now starts to panic, and consults Dry Dolphin for help via ultrasonic waves. After the Dry Dolphin Sequence Processing Factory processes data of Wet Shark's sequences, the machines give Wet Shark the following 2 relations his sequences follow for all 1 β€ n < 109, where x = sqrt(2) and y = sqrt(3).
Wet Shark is now clueless on how to compute anything, and asks you for help.
Wet Shark has discovered that Cthulhu's code is actually defined as Q = (a_k + b_k) / (2^s), where s is a predetermined number, k is the index of another element in Wet Shark's sequence, and a_k, b_k are precisely the kth elements of Wet Shark's sequences {a_n} and {b_n}, respectively.
Given k, i, and the 2 elements of the arrays Wet Shark has lost, find any value of the code c that will allow Wet Shark to exit Cthulhu's labyrinth.
-----Input-----
The first line of input contains 3 space separated integers i, k, s β the common index of the two elements Wet Shark kept, the index of Wet Shark's array needed to break Cthulhu's code, and the number s described in the problem statement, respectively. It is guaranteed that Cthulhu's code, Q, is between -109 and 109 (both inclusive).
The second line of the input contains 2 space separated integers a_i and b_i, representing the ith element of sequence {a_n} and the ith element of sequence {b_n}, respectively.
-----Output-----
Output any number c that will crack Cthulhu's code. Recall that if Wet Shark enters any code c such that |c - Q| β€ 0.01 , Cthulhu's code checker will allow him to exit the labyrinth.
----- Constraints -----
- SUBTASK 1: 20 POINTS
- 1 β€ i β€ 103
- 1 β€ k β€ 103
- -103 β€ s β€ 103
- 1ββ€βa_i,βb_iββ€β103
- SUBTASK 2: 80 POINTS
- 1 β€ i β€ 1010
- 1 β€ k β€ 1010
- -1010 β€ s β€ 1010
- 1ββ€βa_i,βb_iββ€β1010
It is guaranteed that -1010 β€βQ β€β 1010.
-----Example-----
Input:
1 1 5
4 5
Output:
0.28125
-----Explanation-----
Example case 1. In this case, a_1 = 4, b_1 = 5, and s = 5. Cthulhu's code in this case is (a_1 + b_1) / (2s) = 9/32 = 0.28125. | def multiply22(x, y):
return [
[x[0][0] * y[0][0] + x[0][1] * y[1][0], x[0][0] * y[0][1] + x[0][1] * y[1][1]],
[x[1][0] * y[0][0] + x[1][1] * y[1][0], x[1][0] * y[0][1] + x[1][1] * y[1][1]],
]
def multiply21(x, y):
return [
[x[0][0] * y[0][0] + x[0][1] * y[1][0]],
[x[1][0] * y[0][0] + x[1][1] * y[1][0]],
]
def wpower(value, power):
if power & 1 == 0:
return [[1, 0], [0, 1]], 2 * power
return value, 2 * (power - 1)
fw = [
[-1.0352761804100832, 3.8637033051562737],
[3.8637033051562737, 1.0352761804100832],
]
bw = [
[-0.06470476127563018, 0.24148145657226705],
[0.24148145657226705, 0.06470476127563018],
]
i, k, s = map(int, input().split())
ai, bi = map(int, input().split())
if i == k:
print((ai + bi) / 2**s)
else:
mat = wpower(fw, k - i)
ans = multiply21(mat[0], [[ai], [bi]])
s -= mat[1]
print((ans[0][0] + ans[1][0]) / 2**s) | FUNC_DEF RETURN LIST LIST BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER LIST BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FUNC_DEF RETURN LIST LIST BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER LIST BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FUNC_DEF IF BIN_OP VAR NUMBER NUMBER RETURN LIST LIST NUMBER NUMBER LIST NUMBER NUMBER BIN_OP NUMBER VAR RETURN VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER LIST LIST VAR LIST VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP NUMBER VAR |
Wet Shark once had 2 sequences:
{a_n}= {a_1, a_2, a_3, ... , a_(109)}
{b_n} = {b_1, b_2, b_3, ... , b_(109)}
However, he only kept one element from each sequence. Luckily, both the elements that Wet Shark kept have the same index in Wet Shark's sequences: that is, he took a_i and b_i for some 1 β€ i β€ 109.
Right after Wet Shark loses his sequences, he finds that he actually needs them to break the code of Cthulhu to escape a labyrinth. Cthulhu's code is a single floating point number Q. However, the code verifier is faulty. If Wet Shark enters any code c such that |c - Q| β€ 0.01 , Cthulhu's code checker will allow him to escape.
Wet Shark now starts to panic, and consults Dry Dolphin for help via ultrasonic waves. After the Dry Dolphin Sequence Processing Factory processes data of Wet Shark's sequences, the machines give Wet Shark the following 2 relations his sequences follow for all 1 β€ n < 109, where x = sqrt(2) and y = sqrt(3).
Wet Shark is now clueless on how to compute anything, and asks you for help.
Wet Shark has discovered that Cthulhu's code is actually defined as Q = (a_k + b_k) / (2^s), where s is a predetermined number, k is the index of another element in Wet Shark's sequence, and a_k, b_k are precisely the kth elements of Wet Shark's sequences {a_n} and {b_n}, respectively.
Given k, i, and the 2 elements of the arrays Wet Shark has lost, find any value of the code c that will allow Wet Shark to exit Cthulhu's labyrinth.
-----Input-----
The first line of input contains 3 space separated integers i, k, s β the common index of the two elements Wet Shark kept, the index of Wet Shark's array needed to break Cthulhu's code, and the number s described in the problem statement, respectively. It is guaranteed that Cthulhu's code, Q, is between -109 and 109 (both inclusive).
The second line of the input contains 2 space separated integers a_i and b_i, representing the ith element of sequence {a_n} and the ith element of sequence {b_n}, respectively.
-----Output-----
Output any number c that will crack Cthulhu's code. Recall that if Wet Shark enters any code c such that |c - Q| β€ 0.01 , Cthulhu's code checker will allow him to exit the labyrinth.
----- Constraints -----
- SUBTASK 1: 20 POINTS
- 1 β€ i β€ 103
- 1 β€ k β€ 103
- -103 β€ s β€ 103
- 1ββ€βa_i,βb_iββ€β103
- SUBTASK 2: 80 POINTS
- 1 β€ i β€ 1010
- 1 β€ k β€ 1010
- -1010 β€ s β€ 1010
- 1ββ€βa_i,βb_iββ€β1010
It is guaranteed that -1010 β€βQ β€β 1010.
-----Example-----
Input:
1 1 5
4 5
Output:
0.28125
-----Explanation-----
Example case 1. In this case, a_1 = 4, b_1 = 5, and s = 5. Cthulhu's code in this case is (a_1 + b_1) / (2s) = 9/32 = 0.28125. | X = pow(2, 0.5)
Y = pow(3, 0.5)
def calculateCode(i, ai, bi, k, s):
if (k - i) % 2 != 0:
ai, bi = X * (ai + bi) - X * Y * (ai - bi), X * (ai - bi) + X * Y * (ai + bi)
i += 1
return (ai + bi) * pow(2, 2 * (k - i) - s)
i, k, s = [int(x) for x in input().split()]
ai, bi = [int(x) for x in input().split()]
print(calculateCode(i, ai, bi, k, s)) | ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER FUNC_DEF IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR |
Wet Shark once had 2 sequences:
{a_n}= {a_1, a_2, a_3, ... , a_(109)}
{b_n} = {b_1, b_2, b_3, ... , b_(109)}
However, he only kept one element from each sequence. Luckily, both the elements that Wet Shark kept have the same index in Wet Shark's sequences: that is, he took a_i and b_i for some 1 β€ i β€ 109.
Right after Wet Shark loses his sequences, he finds that he actually needs them to break the code of Cthulhu to escape a labyrinth. Cthulhu's code is a single floating point number Q. However, the code verifier is faulty. If Wet Shark enters any code c such that |c - Q| β€ 0.01 , Cthulhu's code checker will allow him to escape.
Wet Shark now starts to panic, and consults Dry Dolphin for help via ultrasonic waves. After the Dry Dolphin Sequence Processing Factory processes data of Wet Shark's sequences, the machines give Wet Shark the following 2 relations his sequences follow for all 1 β€ n < 109, where x = sqrt(2) and y = sqrt(3).
Wet Shark is now clueless on how to compute anything, and asks you for help.
Wet Shark has discovered that Cthulhu's code is actually defined as Q = (a_k + b_k) / (2^s), where s is a predetermined number, k is the index of another element in Wet Shark's sequence, and a_k, b_k are precisely the kth elements of Wet Shark's sequences {a_n} and {b_n}, respectively.
Given k, i, and the 2 elements of the arrays Wet Shark has lost, find any value of the code c that will allow Wet Shark to exit Cthulhu's labyrinth.
-----Input-----
The first line of input contains 3 space separated integers i, k, s β the common index of the two elements Wet Shark kept, the index of Wet Shark's array needed to break Cthulhu's code, and the number s described in the problem statement, respectively. It is guaranteed that Cthulhu's code, Q, is between -109 and 109 (both inclusive).
The second line of the input contains 2 space separated integers a_i and b_i, representing the ith element of sequence {a_n} and the ith element of sequence {b_n}, respectively.
-----Output-----
Output any number c that will crack Cthulhu's code. Recall that if Wet Shark enters any code c such that |c - Q| β€ 0.01 , Cthulhu's code checker will allow him to exit the labyrinth.
----- Constraints -----
- SUBTASK 1: 20 POINTS
- 1 β€ i β€ 103
- 1 β€ k β€ 103
- -103 β€ s β€ 103
- 1ββ€βa_i,βb_iββ€β103
- SUBTASK 2: 80 POINTS
- 1 β€ i β€ 1010
- 1 β€ k β€ 1010
- -1010 β€ s β€ 1010
- 1ββ€βa_i,βb_iββ€β1010
It is guaranteed that -1010 β€βQ β€β 1010.
-----Example-----
Input:
1 1 5
4 5
Output:
0.28125
-----Explanation-----
Example case 1. In this case, a_1 = 4, b_1 = 5, and s = 5. Cthulhu's code in this case is (a_1 + b_1) / (2s) = 9/32 = 0.28125. | x = 2**0.5
y = 3**0.5
def crack(i, k, s, a_i, b_i):
p = x * (1 - y)
q = x * (1 + y)
sq = p * p + q * q
d = k - i
if d % 2 != 0:
i = i + 1
a_i_1 = a_i
b_i_1 = b_i
a_i = p * a_i_1 + q * b_i_1
b_i = q * a_i_1 - p * b_i_1
d = k - i
multiplier = 2 ** (d / 2 * 4 - s)
a_k = a_i * multiplier
b_k = b_i * multiplier
return a_k, b_k
[i, k, s] = map(int, input().split())
[a_i, b_i] = map(int, input().split())
a_k, b_k = crack(i, k, s, a_i, b_i)
Q = a_k + b_k
print("%f" % Q) | ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR VAR ASSIGN LIST VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN LIST VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR |
Wet Shark once had 2 sequences:
{a_n}= {a_1, a_2, a_3, ... , a_(109)}
{b_n} = {b_1, b_2, b_3, ... , b_(109)}
However, he only kept one element from each sequence. Luckily, both the elements that Wet Shark kept have the same index in Wet Shark's sequences: that is, he took a_i and b_i for some 1 β€ i β€ 109.
Right after Wet Shark loses his sequences, he finds that he actually needs them to break the code of Cthulhu to escape a labyrinth. Cthulhu's code is a single floating point number Q. However, the code verifier is faulty. If Wet Shark enters any code c such that |c - Q| β€ 0.01 , Cthulhu's code checker will allow him to escape.
Wet Shark now starts to panic, and consults Dry Dolphin for help via ultrasonic waves. After the Dry Dolphin Sequence Processing Factory processes data of Wet Shark's sequences, the machines give Wet Shark the following 2 relations his sequences follow for all 1 β€ n < 109, where x = sqrt(2) and y = sqrt(3).
Wet Shark is now clueless on how to compute anything, and asks you for help.
Wet Shark has discovered that Cthulhu's code is actually defined as Q = (a_k + b_k) / (2^s), where s is a predetermined number, k is the index of another element in Wet Shark's sequence, and a_k, b_k are precisely the kth elements of Wet Shark's sequences {a_n} and {b_n}, respectively.
Given k, i, and the 2 elements of the arrays Wet Shark has lost, find any value of the code c that will allow Wet Shark to exit Cthulhu's labyrinth.
-----Input-----
The first line of input contains 3 space separated integers i, k, s β the common index of the two elements Wet Shark kept, the index of Wet Shark's array needed to break Cthulhu's code, and the number s described in the problem statement, respectively. It is guaranteed that Cthulhu's code, Q, is between -109 and 109 (both inclusive).
The second line of the input contains 2 space separated integers a_i and b_i, representing the ith element of sequence {a_n} and the ith element of sequence {b_n}, respectively.
-----Output-----
Output any number c that will crack Cthulhu's code. Recall that if Wet Shark enters any code c such that |c - Q| β€ 0.01 , Cthulhu's code checker will allow him to exit the labyrinth.
----- Constraints -----
- SUBTASK 1: 20 POINTS
- 1 β€ i β€ 103
- 1 β€ k β€ 103
- -103 β€ s β€ 103
- 1ββ€βa_i,βb_iββ€β103
- SUBTASK 2: 80 POINTS
- 1 β€ i β€ 1010
- 1 β€ k β€ 1010
- -1010 β€ s β€ 1010
- 1ββ€βa_i,βb_iββ€β1010
It is guaranteed that -1010 β€βQ β€β 1010.
-----Example-----
Input:
1 1 5
4 5
Output:
0.28125
-----Explanation-----
Example case 1. In this case, a_1 = 4, b_1 = 5, and s = 5. Cthulhu's code in this case is (a_1 + b_1) / (2s) = 9/32 = 0.28125. | def main():
h = list(map(int, input().split()))
i = h[0]
k = h[1]
s = h[2]
h = list(map(int, input().split()))
a = h[0]
b = h[1]
if k >= i:
x = k - i
if x % 2 == 0:
ans = float(a + b) / float(2 ** (s - 2 * x))
print(ans)
else:
ans = float(
2**0.5 * (3**0.5 - 1) * ((2 + 3**0.5) * (a + b) + (b - a))
) / float(2 ** (s - 2 * x + 2))
print(ans)
else:
x = i - k
if x % 2 == 0:
ans = float(a + b) / float(2 ** (s + 2 * x))
print(ans)
else:
ans = float(
2**0.5 * (3**0.5 - 1) * ((2 + 3**0.5) * (a + b) + (b - a))
) / float(2 ** (s + 2 * x + 2))
print(ans)
return 0
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER BIN_OP BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER BIN_OP BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR |
Wet Shark once had 2 sequences:
{a_n}= {a_1, a_2, a_3, ... , a_(109)}
{b_n} = {b_1, b_2, b_3, ... , b_(109)}
However, he only kept one element from each sequence. Luckily, both the elements that Wet Shark kept have the same index in Wet Shark's sequences: that is, he took a_i and b_i for some 1 β€ i β€ 109.
Right after Wet Shark loses his sequences, he finds that he actually needs them to break the code of Cthulhu to escape a labyrinth. Cthulhu's code is a single floating point number Q. However, the code verifier is faulty. If Wet Shark enters any code c such that |c - Q| β€ 0.01 , Cthulhu's code checker will allow him to escape.
Wet Shark now starts to panic, and consults Dry Dolphin for help via ultrasonic waves. After the Dry Dolphin Sequence Processing Factory processes data of Wet Shark's sequences, the machines give Wet Shark the following 2 relations his sequences follow for all 1 β€ n < 109, where x = sqrt(2) and y = sqrt(3).
Wet Shark is now clueless on how to compute anything, and asks you for help.
Wet Shark has discovered that Cthulhu's code is actually defined as Q = (a_k + b_k) / (2^s), where s is a predetermined number, k is the index of another element in Wet Shark's sequence, and a_k, b_k are precisely the kth elements of Wet Shark's sequences {a_n} and {b_n}, respectively.
Given k, i, and the 2 elements of the arrays Wet Shark has lost, find any value of the code c that will allow Wet Shark to exit Cthulhu's labyrinth.
-----Input-----
The first line of input contains 3 space separated integers i, k, s β the common index of the two elements Wet Shark kept, the index of Wet Shark's array needed to break Cthulhu's code, and the number s described in the problem statement, respectively. It is guaranteed that Cthulhu's code, Q, is between -109 and 109 (both inclusive).
The second line of the input contains 2 space separated integers a_i and b_i, representing the ith element of sequence {a_n} and the ith element of sequence {b_n}, respectively.
-----Output-----
Output any number c that will crack Cthulhu's code. Recall that if Wet Shark enters any code c such that |c - Q| β€ 0.01 , Cthulhu's code checker will allow him to exit the labyrinth.
----- Constraints -----
- SUBTASK 1: 20 POINTS
- 1 β€ i β€ 103
- 1 β€ k β€ 103
- -103 β€ s β€ 103
- 1ββ€βa_i,βb_iββ€β103
- SUBTASK 2: 80 POINTS
- 1 β€ i β€ 1010
- 1 β€ k β€ 1010
- -1010 β€ s β€ 1010
- 1ββ€βa_i,βb_iββ€β1010
It is guaranteed that -1010 β€βQ β€β 1010.
-----Example-----
Input:
1 1 5
4 5
Output:
0.28125
-----Explanation-----
Example case 1. In this case, a_1 = 4, b_1 = 5, and s = 5. Cthulhu's code in this case is (a_1 + b_1) / (2s) = 9/32 = 0.28125. | def recrel(k, i, ai, bi, s):
n = k - i
if n == 0:
return (ai + bi) / 2**s
elif n % 2 == 0:
return 2 ** (2 * n - s) * (ai + bi)
else:
return 2 ** (2 * n - (1 + s)) * (2**0.5 * ai + 6**0.5 * bi)
def recrel1(k, i, ai, bi, s):
n = i - k
if n == 0:
return ai + bi
elif n % 2 == 0:
return (ai + bi) / 2 ** (2 * n + s)
else:
return (2**0.5 * ai + 6**0.5 * bi) / 2 ** (2 * n + 1 + s)
i, k, s = input().split(" ")
i = int(i)
k = int(k)
s = float(s)
ai, bi = input().split(" ")
ai = int(ai)
bi = int(bi)
if k >= i:
q = recrel(k, i, ai, bi, s)
print(round(q, 5))
elif k < i:
q = recrel1(k, i, ai, bi, s)
print(round(q, 5)) | FUNC_DEF ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP NUMBER BIN_OP BIN_OP NUMBER VAR VAR BIN_OP VAR VAR RETURN BIN_OP BIN_OP NUMBER BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER RETURN BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP VAR VAR BIN_OP NUMBER BIN_OP BIN_OP NUMBER VAR VAR RETURN BIN_OP BIN_OP BIN_OP BIN_OP NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER VAR BIN_OP NUMBER BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER |
After rejecting 10^{100} data structure problems, Errorgorn is very angry at Anton and decided to kill him.
Anton's DNA can be represented as a string a which only contains the characters "ANTON" (there are only 4 distinct characters).
Errorgorn can change Anton's DNA into string b which must be a permutation of a. However, Anton's body can defend against this attack. In 1 second, his body can swap 2 adjacent characters of his DNA to transform it back to a. Anton's body is smart and will use the minimum number of moves.
To maximize the chance of Anton dying, Errorgorn wants to change Anton's DNA the string that maximizes the time for Anton's body to revert his DNA. But since Errorgorn is busy making more data structure problems, he needs your help to find the best string B. Can you help him?
Input
The first line of input contains a single integer t (1 β€ t β€ 100000) β the number of testcases.
The first and only line of each testcase contains 1 string a (1 β€ |a| β€ 100000). a consists of only the characters "A", "N", "O" and "T".
It is guaranteed that the sum of |a| over all testcases does not exceed 100000.
Output
For each testcase, print a single string, b. If there are multiple answers, you can output any one of them. b must be a permutation of the string a.
Example
Input
4
ANTON
NAAN
AAAAAA
OAANTTON
Output
NNOTA
AANN
AAAAAA
TNNTAOOA
Note
For the first testcase, it takes 7 seconds for Anton's body to transform NNOTA to ANTON:
NNOTA β NNOAT β NNAOT β NANOT β NANTO β ANNTO β ANTNO β ANTON.
Note that you cannot output strings such as AANTON, ANTONTRYGUB, AAAAA and anton as it is not a permutation of ANTON.
For the second testcase, it takes 2 seconds for Anton's body to transform AANN to NAAN. Note that other strings such as NNAA and ANNA will also be accepted. | import sys
from itertools import permutations
input = lambda: sys.stdin.readline().rstrip()
class BIT:
def __init__(self, init):
if type(init) == int:
self.n = init + 1
self.X = [0] * self.n
else:
self.n = len(init) + 1
self.X = [0] + init
for i in range(1, self.n):
if i + (i & -i) < self.n:
self.X[i + (i & -i)] += self.X[i]
def add(self, i, x=1):
i += 1
while i < self.n:
self.X[i] += x
i += i & -i
def getsum(self, i):
ret = 0
while i != 0:
ret += self.X[i]
i -= i & -i
return ret
def getrange(self, l, r):
return self.getsum(r) - self.getsum(l)
def inversion_number(L):
bit = BIT(len(L))
ret = 0
for i, a in enumerate(L):
ret += i - bit.getsum(a + 1)
bit.add(a)
return ret
def inversion_number(L):
ll = sorted(list(set(L)))
dd = {a: i for i, a in enumerate(ll)}
nL = [dd[l] for l in L]
bit = BIT(len(nL))
ret = 0
for i, a in enumerate(nL):
ret += i - bit.getsum(a + 1)
bit.add(a)
return ret
def calc(A):
n = len(A)
C = [0] * 4
for a in A:
C[a] += 1
ma = 0
pma = [0, 1, 2, 3]
for p in permutations([0, 1, 2, 3]):
D = [0] * 4
c = 0
for i, a in enumerate(p):
D[a] = c
c += C[a]
B = [0] * n
for i, a in enumerate(A):
B[i] = D[a]
D[a] += 1
ivn = inversion_number(B)
if ivn > ma:
ma = ivn
pma = p
re = []
for a in pma:
re.append("ANTO"[a] * C[a])
return "".join(re)
T = int(input())
for _ in range(T):
A = [(0 if a == "A" else 1 if a == "N" else 2 if a == "T" else 3) for a in input()]
print(calc(A)) | IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR FUNC_DEF NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR VAR RETURN FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR STRING NUMBER VAR STRING NUMBER VAR STRING NUMBER NUMBER VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
After rejecting 10^{100} data structure problems, Errorgorn is very angry at Anton and decided to kill him.
Anton's DNA can be represented as a string a which only contains the characters "ANTON" (there are only 4 distinct characters).
Errorgorn can change Anton's DNA into string b which must be a permutation of a. However, Anton's body can defend against this attack. In 1 second, his body can swap 2 adjacent characters of his DNA to transform it back to a. Anton's body is smart and will use the minimum number of moves.
To maximize the chance of Anton dying, Errorgorn wants to change Anton's DNA the string that maximizes the time for Anton's body to revert his DNA. But since Errorgorn is busy making more data structure problems, he needs your help to find the best string B. Can you help him?
Input
The first line of input contains a single integer t (1 β€ t β€ 100000) β the number of testcases.
The first and only line of each testcase contains 1 string a (1 β€ |a| β€ 100000). a consists of only the characters "A", "N", "O" and "T".
It is guaranteed that the sum of |a| over all testcases does not exceed 100000.
Output
For each testcase, print a single string, b. If there are multiple answers, you can output any one of them. b must be a permutation of the string a.
Example
Input
4
ANTON
NAAN
AAAAAA
OAANTTON
Output
NNOTA
AANN
AAAAAA
TNNTAOOA
Note
For the first testcase, it takes 7 seconds for Anton's body to transform NNOTA to ANTON:
NNOTA β NNOAT β NNAOT β NANOT β NANTO β ANNTO β ANTNO β ANTON.
Note that you cannot output strings such as AANTON, ANTONTRYGUB, AAAAA and anton as it is not a permutation of ANTON.
For the second testcase, it takes 2 seconds for Anton's body to transform AANN to NAAN. Note that other strings such as NNAA and ANNA will also be accepted. | import itertools
import sys
from sys import stdin
tt = int(stdin.readline())
alp = "ANTO"
dic = {}
dic["A"] = 0
dic["N"] = 1
dic["T"] = 2
dic["O"] = 3
ANS = []
for loop in range(tt):
s = stdin.readline()[:-1]
lis = [([0] * 4) for i in range(4)]
num = [0] * 4
for tmp in s:
i = dic[tmp]
for j in range(4):
if i != j:
lis[j][i] += num[j]
num[i] += 1
ans = -1
maxp = 0
for p in itertools.permutations((0, 1, 2, 3)):
np = list(p)
nans = 0
for ri in range(4):
for li in range(ri):
l = np[li]
r = np[ri]
nans += lis[r][l]
if ans < nans:
ans = nans
maxp = np
astr = []
for i in maxp:
astr.append(alp[i] * num[i])
ANS.append("".join(astr))
print("\n".join(ANS)) | IMPORT IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR DICT ASSIGN VAR STRING NUMBER ASSIGN VAR STRING NUMBER ASSIGN VAR STRING NUMBER ASSIGN VAR STRING NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
After rejecting 10^{100} data structure problems, Errorgorn is very angry at Anton and decided to kill him.
Anton's DNA can be represented as a string a which only contains the characters "ANTON" (there are only 4 distinct characters).
Errorgorn can change Anton's DNA into string b which must be a permutation of a. However, Anton's body can defend against this attack. In 1 second, his body can swap 2 adjacent characters of his DNA to transform it back to a. Anton's body is smart and will use the minimum number of moves.
To maximize the chance of Anton dying, Errorgorn wants to change Anton's DNA the string that maximizes the time for Anton's body to revert his DNA. But since Errorgorn is busy making more data structure problems, he needs your help to find the best string B. Can you help him?
Input
The first line of input contains a single integer t (1 β€ t β€ 100000) β the number of testcases.
The first and only line of each testcase contains 1 string a (1 β€ |a| β€ 100000). a consists of only the characters "A", "N", "O" and "T".
It is guaranteed that the sum of |a| over all testcases does not exceed 100000.
Output
For each testcase, print a single string, b. If there are multiple answers, you can output any one of them. b must be a permutation of the string a.
Example
Input
4
ANTON
NAAN
AAAAAA
OAANTTON
Output
NNOTA
AANN
AAAAAA
TNNTAOOA
Note
For the first testcase, it takes 7 seconds for Anton's body to transform NNOTA to ANTON:
NNOTA β NNOAT β NNAOT β NANOT β NANTO β ANNTO β ANTNO β ANTON.
Note that you cannot output strings such as AANTON, ANTONTRYGUB, AAAAA and anton as it is not a permutation of ANTON.
For the second testcase, it takes 2 seconds for Anton's body to transform AANN to NAAN. Note that other strings such as NNAA and ANNA will also be accepted. | import sys
from itertools import permutations
def I():
return int(sys.stdin.readline().rstrip())
def MI():
return map(int, sys.stdin.readline().rstrip().split())
def LI():
return list(map(int, sys.stdin.readline().rstrip().split()))
def LI2():
return list(map(int, sys.stdin.readline().rstrip()))
def S():
return sys.stdin.readline().rstrip()
def LS():
return list(sys.stdin.readline().rstrip().split())
def LS2():
return list(sys.stdin.readline().rstrip())
t = I()
C = ["A", "N", "T", "O"]
for _ in range(t):
X = S()
count1 = [0] * 4
count2 = [([0] * 4) for _ in range(4)]
for x in X:
if x == "A":
count1[0] += 1
for i in range(4):
if i == 0:
continue
count2[i][0] += count1[i]
elif x == "N":
count1[1] += 1
for i in range(4):
if i == 1:
continue
count2[i][1] += count1[i]
elif x == "T":
count1[2] += 1
for i in range(4):
if i == 2:
continue
count2[i][2] += count1[i]
else:
count1[3] += 1
for i in range(4):
if i == 3:
continue
count2[i][3] += count1[i]
M = -1
ans = 0, 0, 0, 0
for A in list(permutations([0, 1, 2, 3], 4)):
cnt = 0
for i in range(4):
a = A[i]
for j in range(i + 1, 4):
b = A[j]
cnt += count2[b][a]
if M < cnt:
M = cnt
ans = A
ANS = (
[C[ans[0]]] * count1[ans[0]]
+ [C[ans[1]]] * count1[ans[1]]
+ [C[ans[2]]] * count1[ans[2]]
+ [C[ans[3]]] * count1[ans[3]]
)
ANS = "".join(ANS)
print(ANS) | IMPORT FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR VAR IF VAR STRING VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR VAR IF VAR STRING VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP LIST VAR VAR NUMBER VAR VAR NUMBER BIN_OP LIST VAR VAR NUMBER VAR VAR NUMBER BIN_OP LIST VAR VAR NUMBER VAR VAR NUMBER BIN_OP LIST VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR |
After rejecting 10^{100} data structure problems, Errorgorn is very angry at Anton and decided to kill him.
Anton's DNA can be represented as a string a which only contains the characters "ANTON" (there are only 4 distinct characters).
Errorgorn can change Anton's DNA into string b which must be a permutation of a. However, Anton's body can defend against this attack. In 1 second, his body can swap 2 adjacent characters of his DNA to transform it back to a. Anton's body is smart and will use the minimum number of moves.
To maximize the chance of Anton dying, Errorgorn wants to change Anton's DNA the string that maximizes the time for Anton's body to revert his DNA. But since Errorgorn is busy making more data structure problems, he needs your help to find the best string B. Can you help him?
Input
The first line of input contains a single integer t (1 β€ t β€ 100000) β the number of testcases.
The first and only line of each testcase contains 1 string a (1 β€ |a| β€ 100000). a consists of only the characters "A", "N", "O" and "T".
It is guaranteed that the sum of |a| over all testcases does not exceed 100000.
Output
For each testcase, print a single string, b. If there are multiple answers, you can output any one of them. b must be a permutation of the string a.
Example
Input
4
ANTON
NAAN
AAAAAA
OAANTTON
Output
NNOTA
AANN
AAAAAA
TNNTAOOA
Note
For the first testcase, it takes 7 seconds for Anton's body to transform NNOTA to ANTON:
NNOTA β NNOAT β NNAOT β NANOT β NANTO β ANNTO β ANTNO β ANTON.
Note that you cannot output strings such as AANTON, ANTONTRYGUB, AAAAA and anton as it is not a permutation of ANTON.
For the second testcase, it takes 2 seconds for Anton's body to transform AANN to NAAN. Note that other strings such as NNAA and ANNA will also be accepted. | import sys
from itertools import permutations
input = sys.stdin.readline
class bit:
def __init__(self, n):
self.n = n + 1
self.a = [0] * self.n
def add(self, idx, val):
idx += 1
while idx < self.n:
self.a[idx] += val
idx += idx & -idx
def sum_prefix(self, idx):
tot = 0
idx += 1
while idx > 0:
tot += self.a[idx]
idx -= idx & -idx
return tot
def inversions(a):
tot = 0
loc = [0] * len(a)
for i in range(len(a)):
loc[a[i]] = i
bt = bit(len(a))
for i in range(len(a) - 1, -1, -1):
tot += bt.sum_prefix(loc[i])
bt.add(loc[i], 1)
return tot
def create_c(a, b):
c = [0] * len(a)
idx = {}
positions = {}
for i in range(len(b)):
if b[i] in positions:
positions[b[i]].append(i)
else:
positions[b[i]] = [i]
for i in range(len(a)):
if a[i] in idx:
idx[a[i]] += 1
else:
idx[a[i]] = 0
c[i] = positions[a[i]][idx[a[i]]]
return c
for _ in range(int(input())):
a = input().strip()
counts = dict(zip("ANTO", [0] * 4))
for i in a:
counts[i] += 1
best_perm = None
best_perm_cost = -1
for order in permutations("ANTO"):
final_perm = "".join([(char * counts[char]) for char in order])
cost = inversions(create_c(a, final_perm))
if cost > best_perm_cost:
best_perm = final_perm
best_perm_cost = cost
print(best_perm) | IMPORT ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL STRING BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
After rejecting 10^{100} data structure problems, Errorgorn is very angry at Anton and decided to kill him.
Anton's DNA can be represented as a string a which only contains the characters "ANTON" (there are only 4 distinct characters).
Errorgorn can change Anton's DNA into string b which must be a permutation of a. However, Anton's body can defend against this attack. In 1 second, his body can swap 2 adjacent characters of his DNA to transform it back to a. Anton's body is smart and will use the minimum number of moves.
To maximize the chance of Anton dying, Errorgorn wants to change Anton's DNA the string that maximizes the time for Anton's body to revert his DNA. But since Errorgorn is busy making more data structure problems, he needs your help to find the best string B. Can you help him?
Input
The first line of input contains a single integer t (1 β€ t β€ 100000) β the number of testcases.
The first and only line of each testcase contains 1 string a (1 β€ |a| β€ 100000). a consists of only the characters "A", "N", "O" and "T".
It is guaranteed that the sum of |a| over all testcases does not exceed 100000.
Output
For each testcase, print a single string, b. If there are multiple answers, you can output any one of them. b must be a permutation of the string a.
Example
Input
4
ANTON
NAAN
AAAAAA
OAANTTON
Output
NNOTA
AANN
AAAAAA
TNNTAOOA
Note
For the first testcase, it takes 7 seconds for Anton's body to transform NNOTA to ANTON:
NNOTA β NNOAT β NNAOT β NANOT β NANTO β ANNTO β ANTNO β ANTON.
Note that you cannot output strings such as AANTON, ANTONTRYGUB, AAAAA and anton as it is not a permutation of ANTON.
For the second testcase, it takes 2 seconds for Anton's body to transform AANN to NAAN. Note that other strings such as NNAA and ANNA will also be accepted. | import itertools
import sys
IS_INTERACTIVE = False
input = input
if not IS_INTERACTIVE:
(*data,) = sys.stdin.read().split("\n")[::-1]
def input():
return data.pop()
def fprint(*args, **kwargs):
print(*args, **kwargs, flush=True)
def eprint(*args, **kwargs):
print(*args, **kwargs, file=sys.stderr)
def mergeSort(arr, n):
temp_arr = [0] * n
return _mergeSort(arr, temp_arr, 0, n - 1)
def _mergeSort(arr, temp_arr, left, right):
inv_count = 0
if left < right:
mid = (left + right) // 2
inv_count += _mergeSort(arr, temp_arr, left, mid)
inv_count += _mergeSort(arr, temp_arr, mid + 1, right)
inv_count += merge(arr, temp_arr, left, mid, right)
return inv_count
def merge(arr, temp_arr, left, mid, right):
i = left
j = mid + 1
k = left
inv_count = 0
while i <= mid and j <= right:
if arr[i] <= arr[j]:
temp_arr[k] = arr[i]
k += 1
i += 1
else:
temp_arr[k] = arr[j]
inv_count += mid - i + 1
k += 1
j += 1
while i <= mid:
temp_arr[k] = arr[i]
k += 1
i += 1
while j <= right:
temp_arr[k] = arr[j]
k += 1
j += 1
for loop_var in range(left, right + 1):
arr[loop_var] = temp_arr[loop_var]
return inv_count
def calc_invs(a, priorities):
a = [priorities[ch] for ch in a]
return mergeSort(a, len(a))
for _ in range(int(input())):
s = input()
stat = {}
for ch in "ANTO":
stat[ch] = s.count(ch)
mx = 0
mx_ans = s
results = {}
for perm in itertools.permutations("ANTO"):
ans = []
for ch in perm:
ans.append(ch * stat[ch])
ans = "".join(ans)
invs = calc_invs(s, {ch: i for i, ch in enumerate(perm)})
if invs > mx:
mx_ans = ans
mx = invs
print(mx_ans) | IMPORT IMPORT ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
After rejecting 10^{100} data structure problems, Errorgorn is very angry at Anton and decided to kill him.
Anton's DNA can be represented as a string a which only contains the characters "ANTON" (there are only 4 distinct characters).
Errorgorn can change Anton's DNA into string b which must be a permutation of a. However, Anton's body can defend against this attack. In 1 second, his body can swap 2 adjacent characters of his DNA to transform it back to a. Anton's body is smart and will use the minimum number of moves.
To maximize the chance of Anton dying, Errorgorn wants to change Anton's DNA the string that maximizes the time for Anton's body to revert his DNA. But since Errorgorn is busy making more data structure problems, he needs your help to find the best string B. Can you help him?
Input
The first line of input contains a single integer t (1 β€ t β€ 100000) β the number of testcases.
The first and only line of each testcase contains 1 string a (1 β€ |a| β€ 100000). a consists of only the characters "A", "N", "O" and "T".
It is guaranteed that the sum of |a| over all testcases does not exceed 100000.
Output
For each testcase, print a single string, b. If there are multiple answers, you can output any one of them. b must be a permutation of the string a.
Example
Input
4
ANTON
NAAN
AAAAAA
OAANTTON
Output
NNOTA
AANN
AAAAAA
TNNTAOOA
Note
For the first testcase, it takes 7 seconds for Anton's body to transform NNOTA to ANTON:
NNOTA β NNOAT β NNAOT β NANOT β NANTO β ANNTO β ANTNO β ANTON.
Note that you cannot output strings such as AANTON, ANTONTRYGUB, AAAAA and anton as it is not a permutation of ANTON.
For the second testcase, it takes 2 seconds for Anton's body to transform AANN to NAAN. Note that other strings such as NNAA and ANNA will also be accepted. | from itertools import permutations
from sys import stdin
input = stdin.readline
global T, N
d = {"A": 0, "N": 1, "O": 2, "T": 3}
rd = {(0): "A", (1): "N", (2): "O", (3): "T"}
def read(index):
global T, N
s = 0
while index > 0:
s += T[index]
index -= index & -index
return s
def update(index, val):
global T, N
while index <= N:
T[index] += val
index += index & -index
for t in range(int(input())):
S = input().rstrip()
N = len(S)
groups = [0, 0, 0, 0]
A = [[] for _ in range(4)]
for i in range(N):
ind = d[S[i]]
A[ind].append(i + 1)
groups[ind] += 1
mx = -1
s = ""
for perm in permutations([0, 1, 2, 3]):
a, n, o, t = perm
S = ""
for i in perm:
S += rd[i] * groups[i]
inds = [(-1) for _ in range(4)]
T = [(0) for _ in range(N + 1)]
ans = 0
for i in range(N - 1, -1, -1):
idx = d[S[i]]
x = A[idx][inds[idx]]
inds[idx] -= 1
ans += i + 1 - x - read(x)
update(x + 1, -1)
if ans > mx:
s = S
mx = ans
print(s) | ASSIGN VAR VAR ASSIGN VAR DICT STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER STRING STRING STRING STRING FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR STRING FOR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
After rejecting 10^{100} data structure problems, Errorgorn is very angry at Anton and decided to kill him.
Anton's DNA can be represented as a string a which only contains the characters "ANTON" (there are only 4 distinct characters).
Errorgorn can change Anton's DNA into string b which must be a permutation of a. However, Anton's body can defend against this attack. In 1 second, his body can swap 2 adjacent characters of his DNA to transform it back to a. Anton's body is smart and will use the minimum number of moves.
To maximize the chance of Anton dying, Errorgorn wants to change Anton's DNA the string that maximizes the time for Anton's body to revert his DNA. But since Errorgorn is busy making more data structure problems, he needs your help to find the best string B. Can you help him?
Input
The first line of input contains a single integer t (1 β€ t β€ 100000) β the number of testcases.
The first and only line of each testcase contains 1 string a (1 β€ |a| β€ 100000). a consists of only the characters "A", "N", "O" and "T".
It is guaranteed that the sum of |a| over all testcases does not exceed 100000.
Output
For each testcase, print a single string, b. If there are multiple answers, you can output any one of them. b must be a permutation of the string a.
Example
Input
4
ANTON
NAAN
AAAAAA
OAANTTON
Output
NNOTA
AANN
AAAAAA
TNNTAOOA
Note
For the first testcase, it takes 7 seconds for Anton's body to transform NNOTA to ANTON:
NNOTA β NNOAT β NNAOT β NANOT β NANTO β ANNTO β ANTNO β ANTON.
Note that you cannot output strings such as AANTON, ANTONTRYGUB, AAAAA and anton as it is not a permutation of ANTON.
For the second testcase, it takes 2 seconds for Anton's body to transform AANN to NAAN. Note that other strings such as NNAA and ANNA will also be accepted. | from itertools import permutations
from sys import stdin, stdout
dic = {"A": 0, "N": 1, "O": 2, "T": 3}
dic2 = {(0): "A", (1): "N", (2): "O", (3): "T"}
def kill_anton(a):
global dic
cnta1 = [[(0) for _ in range(4)] for _ in range(4)]
cnta2 = [(0) for _ in range(4)]
for c in a:
for i in range(4):
cnta1[dic[c]][i] += cnta2[i]
cnta2[dic[c]] += 1
rmoves = 0
rperm = [0, 1, 2, 3]
for perm in permutations([0, 1, 2, 3]):
moves = 0
for i in range(4):
for j in range(i + 1, 4):
moves += cnta1[perm[i]][perm[j]]
if moves > rmoves:
rmoves = moves
rperm = perm
r = ""
for p in rperm:
r += dic2[p] * cnta2[p]
return r
t = int(stdin.readline())
for _ in range(t):
a = stdin.readline().strip()
r = kill_anton(a)
stdout.write(r + "\n") | ASSIGN VAR DICT STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER STRING STRING STRING STRING FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR STRING FOR VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR STRING |
After rejecting 10^{100} data structure problems, Errorgorn is very angry at Anton and decided to kill him.
Anton's DNA can be represented as a string a which only contains the characters "ANTON" (there are only 4 distinct characters).
Errorgorn can change Anton's DNA into string b which must be a permutation of a. However, Anton's body can defend against this attack. In 1 second, his body can swap 2 adjacent characters of his DNA to transform it back to a. Anton's body is smart and will use the minimum number of moves.
To maximize the chance of Anton dying, Errorgorn wants to change Anton's DNA the string that maximizes the time for Anton's body to revert his DNA. But since Errorgorn is busy making more data structure problems, he needs your help to find the best string B. Can you help him?
Input
The first line of input contains a single integer t (1 β€ t β€ 100000) β the number of testcases.
The first and only line of each testcase contains 1 string a (1 β€ |a| β€ 100000). a consists of only the characters "A", "N", "O" and "T".
It is guaranteed that the sum of |a| over all testcases does not exceed 100000.
Output
For each testcase, print a single string, b. If there are multiple answers, you can output any one of them. b must be a permutation of the string a.
Example
Input
4
ANTON
NAAN
AAAAAA
OAANTTON
Output
NNOTA
AANN
AAAAAA
TNNTAOOA
Note
For the first testcase, it takes 7 seconds for Anton's body to transform NNOTA to ANTON:
NNOTA β NNOAT β NNAOT β NANOT β NANTO β ANNTO β ANTNO β ANTON.
Note that you cannot output strings such as AANTON, ANTONTRYGUB, AAAAA and anton as it is not a permutation of ANTON.
For the second testcase, it takes 2 seconds for Anton's body to transform AANN to NAAN. Note that other strings such as NNAA and ANNA will also be accepted. | import sys
from itertools import permutations
def f(pos, k):
m = len(pos)
a1, a2 = [0], [0]
for i in range(m):
a1.append(a1[-1] + pos[i] - i)
for i in range(1, m + 1):
a2.append(a2[-1] + k - i - pos[m - i])
a2 = a2[::-1]
ind = max(range(m + 1), key=lambda i: a1[i] + a2[i])
return ind, a1[ind] + a2[ind]
def solve(s):
n = len(s)
ma = -1
ans = []
for p in permutations(["A", "N", "O", "T"]):
cur = 0
ans1, ans2 = [], []
s1 = s[:]
for c in p:
s2 = []
pos = []
for i in range(len(s1)):
if s1[i] == c:
pos.append(i)
else:
s2.append(s1[i])
x, y = f(pos, len(s1))
cur += y
ans1.extend([c] * x)
ans2.extend([c] * (len(pos) - x))
s1 = s2
ans1.extend(ans2[::-1])
if cur > ma:
ma = cur
ans = ans1
return "".join(ans)
input = lambda: sys.stdin.readline().rstrip()
t = int(input())
for i in range(t):
s = list(input())
print(solve(s)) | IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR RETURN VAR BIN_OP VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR LIST STRING STRING STRING STRING ASSIGN VAR NUMBER ASSIGN VAR VAR LIST LIST ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
After rejecting 10^{100} data structure problems, Errorgorn is very angry at Anton and decided to kill him.
Anton's DNA can be represented as a string a which only contains the characters "ANTON" (there are only 4 distinct characters).
Errorgorn can change Anton's DNA into string b which must be a permutation of a. However, Anton's body can defend against this attack. In 1 second, his body can swap 2 adjacent characters of his DNA to transform it back to a. Anton's body is smart and will use the minimum number of moves.
To maximize the chance of Anton dying, Errorgorn wants to change Anton's DNA the string that maximizes the time for Anton's body to revert his DNA. But since Errorgorn is busy making more data structure problems, he needs your help to find the best string B. Can you help him?
Input
The first line of input contains a single integer t (1 β€ t β€ 100000) β the number of testcases.
The first and only line of each testcase contains 1 string a (1 β€ |a| β€ 100000). a consists of only the characters "A", "N", "O" and "T".
It is guaranteed that the sum of |a| over all testcases does not exceed 100000.
Output
For each testcase, print a single string, b. If there are multiple answers, you can output any one of them. b must be a permutation of the string a.
Example
Input
4
ANTON
NAAN
AAAAAA
OAANTTON
Output
NNOTA
AANN
AAAAAA
TNNTAOOA
Note
For the first testcase, it takes 7 seconds for Anton's body to transform NNOTA to ANTON:
NNOTA β NNOAT β NNAOT β NANOT β NANTO β ANNTO β ANTNO β ANTON.
Note that you cannot output strings such as AANTON, ANTONTRYGUB, AAAAA and anton as it is not a permutation of ANTON.
For the second testcase, it takes 2 seconds for Anton's body to transform AANN to NAAN. Note that other strings such as NNAA and ANNA will also be accepted. | import itertools
import sys
TC = int(input())
m = {"A": 0, "N": 1, "O": 2, "T": 3}
st = "ANOT"
for _ in range(TC):
s = input()
a = []
for i in s:
a.append(m[i])
flips = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
cnt = [0, 0, 0, 0]
for i in a:
for j in range(4):
flips[j][i] += cnt[j]
cnt[i] += 1
best = []
mx = -1
for perm in itertools.permutations([0, 1, 2, 3]):
c = 0
for i in range(4):
for j in range(i + 1, 4):
c += flips[perm[j]][perm[i]]
if c > mx:
mx = c
best = perm
for i in range(4):
for j in range(cnt[best[i]]):
print(st[best[i]], end="")
print() | IMPORT IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR |
After rejecting 10^{100} data structure problems, Errorgorn is very angry at Anton and decided to kill him.
Anton's DNA can be represented as a string a which only contains the characters "ANTON" (there are only 4 distinct characters).
Errorgorn can change Anton's DNA into string b which must be a permutation of a. However, Anton's body can defend against this attack. In 1 second, his body can swap 2 adjacent characters of his DNA to transform it back to a. Anton's body is smart and will use the minimum number of moves.
To maximize the chance of Anton dying, Errorgorn wants to change Anton's DNA the string that maximizes the time for Anton's body to revert his DNA. But since Errorgorn is busy making more data structure problems, he needs your help to find the best string B. Can you help him?
Input
The first line of input contains a single integer t (1 β€ t β€ 100000) β the number of testcases.
The first and only line of each testcase contains 1 string a (1 β€ |a| β€ 100000). a consists of only the characters "A", "N", "O" and "T".
It is guaranteed that the sum of |a| over all testcases does not exceed 100000.
Output
For each testcase, print a single string, b. If there are multiple answers, you can output any one of them. b must be a permutation of the string a.
Example
Input
4
ANTON
NAAN
AAAAAA
OAANTTON
Output
NNOTA
AANN
AAAAAA
TNNTAOOA
Note
For the first testcase, it takes 7 seconds for Anton's body to transform NNOTA to ANTON:
NNOTA β NNOAT β NNAOT β NANOT β NANTO β ANNTO β ANTNO β ANTON.
Note that you cannot output strings such as AANTON, ANTONTRYGUB, AAAAA and anton as it is not a permutation of ANTON.
For the second testcase, it takes 2 seconds for Anton's body to transform AANN to NAAN. Note that other strings such as NNAA and ANNA will also be accepted. | import sys
from itertools import permutations
class SegmentTree:
def __init__(self, arr, func, initialRes=0):
self.f = func
self.N = len(arr)
self.tree = [(0) for _ in range(2 * self.N)]
self.initialRes = initialRes
for i in range(self.N):
self.tree[self.N + i] = arr[i]
for i in range(self.N - 1, 0, -1):
self.tree[i] = self.f(self.tree[i << 1], self.tree[i << 1 | 1])
def updateTreeNode(self, idx, value):
self.tree[idx + self.N] = value
idx += self.N
i = idx
while i > 1:
self.tree[i >> 1] = self.f(self.tree[i], self.tree[i ^ 1])
i >>= 1
def query(self, l, r):
r += 1
res = self.initialRes
l += self.N
r += self.N
while l < r:
if l & 1:
res = self.f(res, self.tree[l])
l += 1
if r & 1:
r -= 1
res = self.f(res, self.tree[r])
l >>= 1
r >>= 1
return res
def getMaxSegTree(arr):
return SegmentTree(arr, lambda a, b: max(a, b), initialRes=-float("inf"))
def getMinSegTree(arr):
return SegmentTree(arr, lambda a, b: min(a, b), initialRes=float("inf"))
def getSumSegTree(arr):
return SegmentTree(arr, lambda a, b: a + b, initialRes=0)
def getMinMoves(arr, s):
n = len(s)
charIndexes = dict()
for i in range(n - 1, -1, -1):
if s[i] not in charIndexes.keys():
charIndexes[s[i]] = []
charIndexes[s[i]].append(i)
elmCnts = getSumSegTree([1] * n)
nMoves = 0
for c in arr:
i = charIndexes[c].pop()
nMoves += elmCnts.query(0, i) - 1
elmCnts.updateTreeNode(i, 0)
return nMoves
def main():
n = int(input())
allans = []
for _ in range(n):
s = input()
chars = []
charCnts = dict()
for c in s:
if c not in charCnts.keys():
chars.append(c)
charCnts[c] = 0
charCnts[c] += 1
currMax = -1
ans = []
for p in permutations(chars):
arr = []
for c in p:
for __ in range(charCnts[c]):
arr.append(c)
nMoves = getMinMoves(arr, s)
if nMoves > currMax:
ans = arr
currMax = nMoves
allans.append("".join(ans))
multiLineArrayPrint(allans)
return
input = lambda: sys.stdin.readline().rstrip("\r\n")
def oneLineArrayPrint(arr):
print(" ".join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
print("\n".join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
print("\n".join([" ".join([str(x) for x in y]) for y in arr]))
def readIntArr():
return [int(x) for x in input().split()]
def makeArr(defaultValFactory, dimensionArr):
dv = defaultValFactory
da = dimensionArr
if len(da) == 1:
return [dv() for _ in range(da[0])]
else:
return [makeArr(dv, da[1:]) for _ in range(da[0])]
def queryInteractive(i, j):
print("? {} {}".format(i, j))
sys.stdout.flush()
return int(input())
def answerInteractive(ans):
print("! {}".format(" ".join([str(x) for x in ans])))
sys.stdout.flush()
inf = float("inf")
MOD = 10**9 + 7
for _abc in range(1):
main() | IMPORT CLASS_DEF FUNC_DEF NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR |
After rejecting 10^{100} data structure problems, Errorgorn is very angry at Anton and decided to kill him.
Anton's DNA can be represented as a string a which only contains the characters "ANTON" (there are only 4 distinct characters).
Errorgorn can change Anton's DNA into string b which must be a permutation of a. However, Anton's body can defend against this attack. In 1 second, his body can swap 2 adjacent characters of his DNA to transform it back to a. Anton's body is smart and will use the minimum number of moves.
To maximize the chance of Anton dying, Errorgorn wants to change Anton's DNA the string that maximizes the time for Anton's body to revert his DNA. But since Errorgorn is busy making more data structure problems, he needs your help to find the best string B. Can you help him?
Input
The first line of input contains a single integer t (1 β€ t β€ 100000) β the number of testcases.
The first and only line of each testcase contains 1 string a (1 β€ |a| β€ 100000). a consists of only the characters "A", "N", "O" and "T".
It is guaranteed that the sum of |a| over all testcases does not exceed 100000.
Output
For each testcase, print a single string, b. If there are multiple answers, you can output any one of them. b must be a permutation of the string a.
Example
Input
4
ANTON
NAAN
AAAAAA
OAANTTON
Output
NNOTA
AANN
AAAAAA
TNNTAOOA
Note
For the first testcase, it takes 7 seconds for Anton's body to transform NNOTA to ANTON:
NNOTA β NNOAT β NNAOT β NANOT β NANTO β ANNTO β ANTNO β ANTON.
Note that you cannot output strings such as AANTON, ANTONTRYGUB, AAAAA and anton as it is not a permutation of ANTON.
For the second testcase, it takes 2 seconds for Anton's body to transform AANN to NAAN. Note that other strings such as NNAA and ANNA will also be accepted. | import sys
from itertools import permutations
input = sys.stdin.readline
def mergeSort(arr, left, right):
inv_count = 0
if left < right:
mid = left + (right - left) // 2
inv_count += mergeSort(arr, left, mid)
inv_count += mergeSort(arr, mid + 1, right)
inv_count += merge(arr, left, mid, right)
return inv_count
def merge(arr, left, mid, right):
inv_count = 0
n1 = mid - left + 1
n2 = right - mid
l = [0] * n1
r = [0] * n2
for i in range(0, n1):
l[i] = arr[left + i]
for i in range(0, n2):
r[i] = arr[mid + i + 1]
i = 0
j = 0
k = left
while i < n1 and j < n2:
if l[i] < r[j]:
arr[k] = l[i]
i += 1
k += 1
else:
arr[k] = r[j]
j += 1
k += 1
inv_count += n1 - i
while i < n1:
arr[k] = l[i]
i += 1
k += 1
while j < n2:
arr[k] = r[j]
j += 1
k += 1
return inv_count
def create_c(a, b):
c = [0] * len(a)
idx = {}
positions = {}
for i in range(len(b)):
if b[i] in positions:
positions[b[i]].append(i)
else:
positions[b[i]] = [i]
for i in range(len(a)):
if a[i] in idx:
idx[a[i]] += 1
else:
idx[a[i]] = 0
c[i] = positions[a[i]][idx[a[i]]]
return c
for _ in range(int(input())):
a = input().strip()
if len(a) <= 2:
print(a[::-1])
continue
counts = dict(zip("ANTO", [0] * 4))
for i in a:
counts[i] += 1
best_perm = None
best_perm_cost = -1
for order in permutations("ANTO"):
final_perm = "".join([(char * counts[char]) for char in order])
final_arr = create_c(a, final_perm)
cost = mergeSort(final_arr, 0, len(final_arr) - 1)
if cost > best_perm_cost:
best_perm = final_perm
best_perm_cost = cost
print(best_perm) | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL STRING BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
After rejecting 10^{100} data structure problems, Errorgorn is very angry at Anton and decided to kill him.
Anton's DNA can be represented as a string a which only contains the characters "ANTON" (there are only 4 distinct characters).
Errorgorn can change Anton's DNA into string b which must be a permutation of a. However, Anton's body can defend against this attack. In 1 second, his body can swap 2 adjacent characters of his DNA to transform it back to a. Anton's body is smart and will use the minimum number of moves.
To maximize the chance of Anton dying, Errorgorn wants to change Anton's DNA the string that maximizes the time for Anton's body to revert his DNA. But since Errorgorn is busy making more data structure problems, he needs your help to find the best string B. Can you help him?
Input
The first line of input contains a single integer t (1 β€ t β€ 100000) β the number of testcases.
The first and only line of each testcase contains 1 string a (1 β€ |a| β€ 100000). a consists of only the characters "A", "N", "O" and "T".
It is guaranteed that the sum of |a| over all testcases does not exceed 100000.
Output
For each testcase, print a single string, b. If there are multiple answers, you can output any one of them. b must be a permutation of the string a.
Example
Input
4
ANTON
NAAN
AAAAAA
OAANTTON
Output
NNOTA
AANN
AAAAAA
TNNTAOOA
Note
For the first testcase, it takes 7 seconds for Anton's body to transform NNOTA to ANTON:
NNOTA β NNOAT β NNAOT β NANOT β NANTO β ANNTO β ANTNO β ANTON.
Note that you cannot output strings such as AANTON, ANTONTRYGUB, AAAAA and anton as it is not a permutation of ANTON.
For the second testcase, it takes 2 seconds for Anton's body to transform AANN to NAAN. Note that other strings such as NNAA and ANNA will also be accepted. | from itertools import permutations
t = int(input())
perms = list(permutations(range(4)))
def inversion(d, L):
if len(d) == 1:
return (g for g in d), 1, 0
a, A, b = inversion(d[: L // 2], L // 2)
c, C, e = inversion(d[L // 2 :], (L + 1) // 2)
k = 0
s = []
a_ = next(a)
c_ = next(c)
while a_ != -1 != c_:
if a_ < c_:
s.append(a_)
a_ = next(a, -1)
A -= 1
else:
s.append(c_)
c_ = next(c, -1)
k += A
while a_ != -1:
s.append(a_)
a_ = next(a, -1)
while c_ != -1:
s.append(c_)
c_ = next(c, -1)
return (g for g in s), L, b + e + k
for _ in range(t):
s = input().strip()
pos = [[], [], [], []]
for i, c in enumerate(s):
pos["ANTO".index(c)].append(i)
siz = [len(a) for a in pos]
m = 0
best = 0, 1, 2, 3
for k in perms:
d = []
for j in range(4):
d += pos[k[j]]
z = inversion(d, len(d))[2]
if m < z:
best = k
m = z
res = ""
for x in best:
res += "ANTO"[x] * siz[x]
print(res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST LIST LIST LIST LIST FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR STRING FOR VAR VAR VAR BIN_OP STRING VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.