description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position).
Inserting an element in the same position he was erased from is also considered moving.
Can Vasya divide the array after choosing the right element to move and its new position?
-----Input-----
The first line contains single integer n (1 ≤ n ≤ 100000) — the size of the array.
The second line contains n integers a_1, a_2... a_{n} (1 ≤ a_{i} ≤ 10^9) — the elements of the array.
-----Output-----
Print YES if Vasya can divide the array after moving one element. Otherwise print NO.
-----Examples-----
Input
3
1 3 2
Output
YES
Input
5
1 2 3 4 5
Output
NO
Input
5
2 2 3 4 5
Output
YES
-----Note-----
In the first example Vasya can move the second element to the end of the array.
In the second example no move can make the division possible.
In the third example Vasya can move the fourth element by one position to the left. | def find(A):
total = sum(A)
if total % 2 == 1:
return "NO"
need = total // 2
dic = {}
temp = 0
for i in range(len(A)):
temp += A[i]
if temp == need:
return "YES"
if temp > need:
if temp - need in dic:
return "YES"
dic[A[i]] = 1
A = A[::-1]
dic = {}
temp = 0
for i in range(len(A)):
temp += A[i]
if temp == need:
return "YES"
if temp > need:
if temp - need in dic:
return "YES"
dic[A[i]] = 1
return "NO"
input()
print(find(list(map(int, input().strip().split(" "))))) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR RETURN STRING IF VAR VAR IF BIN_OP VAR VAR VAR RETURN STRING ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR RETURN STRING IF VAR VAR IF BIN_OP VAR VAR VAR RETURN STRING ASSIGN VAR VAR VAR NUMBER RETURN STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING |
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position).
Inserting an element in the same position he was erased from is also considered moving.
Can Vasya divide the array after choosing the right element to move and its new position?
-----Input-----
The first line contains single integer n (1 ≤ n ≤ 100000) — the size of the array.
The second line contains n integers a_1, a_2... a_{n} (1 ≤ a_{i} ≤ 10^9) — the elements of the array.
-----Output-----
Print YES if Vasya can divide the array after moving one element. Otherwise print NO.
-----Examples-----
Input
3
1 3 2
Output
YES
Input
5
1 2 3 4 5
Output
NO
Input
5
2 2 3 4 5
Output
YES
-----Note-----
In the first example Vasya can move the second element to the end of the array.
In the second example no move can make the division possible.
In the third example Vasya can move the fourth element by one position to the left. | def bs(a, be, en, v):
if be + 1 >= en:
return False
while be + 1 < en:
mid = (be + en) // 2
if a[mid] == v:
return True
elif a[mid] < v:
be = mid
else:
en = mid
if a[be] == v:
return True
return False
def solve(inp, ps, n):
su = ps[-1]
if su % 2:
return False
su //= 2
if bs(ps, 0, n, su):
return True
for i in range(n):
if bs(ps, i, n, su + inp[i]):
return True
for i in range(n):
if bs(ps, 0, i, su - inp[i]):
return True
return False
n = int(input())
inp = list(map(int, input().split()))
ps = [0] * n
for i in range(n):
ps[i] = ps[i - 1] + inp[i]
print("YES" if solve(inp, ps, n) else "NO") | FUNC_DEF IF BIN_OP VAR NUMBER VAR RETURN NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER RETURN NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR STRING STRING |
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position).
Inserting an element in the same position he was erased from is also considered moving.
Can Vasya divide the array after choosing the right element to move and its new position?
-----Input-----
The first line contains single integer n (1 ≤ n ≤ 100000) — the size of the array.
The second line contains n integers a_1, a_2... a_{n} (1 ≤ a_{i} ≤ 10^9) — the elements of the array.
-----Output-----
Print YES if Vasya can divide the array after moving one element. Otherwise print NO.
-----Examples-----
Input
3
1 3 2
Output
YES
Input
5
1 2 3 4 5
Output
NO
Input
5
2 2 3 4 5
Output
YES
-----Note-----
In the first example Vasya can move the second element to the end of the array.
In the second example no move can make the division possible.
In the third example Vasya can move the fourth element by one position to the left. | n = int(input())
a = list(map(int, input().split()))
sum = [0] * n
sum[0] = a[0]
for i in range(1, n):
sum[i] = sum[i - 1] + a[i]
s = sum[n - 1]
found = 0
if s % 2 == 0:
half = s // 2
u = 0
l = n - 1
while u <= l:
mid = (u + l) // 2
if sum[mid] == half:
print("YES")
exit()
elif sum[mid] > half:
l = mid - 1
else:
u = mid + 1
for i in range(0, n):
u = i
l = n - 1
if (2 * a[i] + s) % 2 == 0:
check = (s + 2 * a[i]) // 2
while u <= l:
mid = (u + l) // 2
if sum[mid] == check:
found = 1
break
elif sum[mid] > check:
l = mid - 1
else:
u = mid + 1
if found == 1:
break
if found == 0 and n < 10000:
for i in range(n - 1, -1, -1):
u = 0
l = i - 1
if (s - 2 * a[i]) % 2 == 0 and s - 2 * a[i] > 0:
check = (s - 2 * a[i]) // 2
while u <= l:
mid = (u + l) // 2
if sum[mid] == check:
found = 1
break
elif sum[mid] > check:
l = mid - 1
else:
u = mid + 1
if found == 1:
break
if found == 1:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER NUMBER BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | t = int(input())
for _ in range(t):
n, m = map(int, input().split())
s = ""
a = []
for i in range(n):
x = input()
c = x.count("1")
a.append([c, x])
a.sort()
for i in range(n):
s = s + a[i][1]
z = 0
ans = 0
for i in range(len(s) - 1, -1, -1):
if s[i] == "0":
z = z + 1
else:
ans = ans + z
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | t = int(input())
def comp(x):
count1 = 0
for i in x:
if i == "1":
count1 += 1
return count1
while t > 0:
t -= 1
n, m = list(map(int, input().split(" ")))
nums = list()
while n > 0:
n -= 1
t1 = str(input())
nums.append(t1)
nums.sort(key=lambda x: comp(x))
a = "".join(nums)
inversion_count = 0
one_count = 0
for i in a:
if i == "1":
one_count += 1
else:
inversion_count += one_count
print(inversion_count) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER RETURN VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | class String:
def __init__(self, a=None, b=0):
self.s = a
self.ones = 0
def calculate(self):
for i in self.s:
if i == "1":
self.ones = self.ones + 1
def __lt__(self, other):
if self.ones < other.ones:
return True
elif self.ones > other.ones:
return False
elif self.s < other.s:
return True
else:
return False
T = int(input())
for _ in range(0, T):
objects = []
N, M = map(int, input().split())
for i in range(0, N):
objects.append(String())
objects[i].s = input()
objects[i].calculate()
sorted_objects = sorted(objects)
string = ""
for i in sorted_objects:
string = string + i.s
ones_count = 0
inversions = 0
for i in string:
if i == "1":
ones_count = ones_count + 1
else:
inversions = inversions + ones_count
print(inversions) | CLASS_DEF FUNC_DEF NONE NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FUNC_DEF FOR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | for _ in range(int(input())):
n, m = map(int, input().split())
l = []
l1 = []
inv = 0
for i in range(n):
st = input()
l.append(st)
co = 0
for j in range(m):
if st[j] == "1":
co = co + 1
else:
inv = inv + co
l1.append(co)
l1.sort()
prev1 = 0
for i in range(1, n):
prev1 = prev1 + l1[i - 1]
inv = inv + (m - l1[i]) * prev1
print(inv) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | def inver(st):
cnt_one = 0
ans = 0
for ch in st:
if ch == "1":
cnt_one += 1
else:
ans += cnt_one
return ans
for _ in range(int(input())):
n, m = map(int, input().split())
l = [input() for _ in range(n)]
l.sort(key=lambda s: s.count("1"))
bigstr = "".join(l)
print(inver(bigstr)) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | def ff(x):
su = 0
z = 0
for i in reversed(range(len(x))):
if x[i] == "0":
z += 1
else:
su += z
return len(x) - z, su
t = int(input())
while t:
t -= 1
a = list(map(int, input().split()))
ti = 0
one = []
for i in range(a[0]):
x = input()
on, inv = ff(x)
ti += inv
one.append(on)
one.sort()
su = 0
for i in reversed(range(len(one))):
su += one[i]
for i in range(len(one)):
ti += one[i] * ((len(one) - i - 1) * a[1] - (su - one[i]))
su -= one[i]
print(ti) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | t = int(input())
for _ in range(t):
n, m = map(int, input().split())
l = []
for i in range(n):
s = input()
count1 = 0
count0 = 0
for i in s:
if i == "1":
count1 += 1
else:
count0 += 1
l.append((count1, s))
l.sort()
a = ""
b = 0
for i, j in l:
a += j
count = 0
for i in a:
if i == "1":
count += 1
else:
b += count
print(b) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | T = int(input())
for j in range(1, T + 1):
n, m = map(int, input().split())
a = [input() for i in range(n)]
a.sort(key=lambda x: x.count("1"))
invs = one = 0
for i in "".join(a):
if i == "1":
one += 1
else:
invs += one
print(invs) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL STRING VAR IF VAR STRING VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | 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
t = int(input())
while t > 0:
nm = list(map(int, input().strip().split()))
n, m = nm[0], nm[1]
li = []
while n > 0:
s = input()
li.append(s)
n -= 1
li1 = sorted(li, key=lambda x: x.count("1"))
li2 = []
for i in li1:
j = list(i)
li2.extend(j)
li3 = list(map(int, li2))
ans = mergeSort(li3, len(li3))
print(ans)
t -= 1 | 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | t = int(input())
for i in range(t):
m, n = map(int, input().split())
S = []
for i in range(m):
s = input()
S.append(s)
S = [(int(s, 2), s, i) for i, s in enumerate(S)]
S.sort(key=lambda x: x[1].count("1"))
s_joined = "".join(s for v, s, i in S)
pref_s = [0]
ans = 0
for i in range(len(s_joined) - 1, -1, -1):
if s_joined[i] == "0":
pref_s.append(pref_s[-1] + 1)
else:
pref_s.append(pref_s[-1])
pref_s.pop(0)
pref_s.reverse()
for i in range(len(s_joined)):
if s_joined[i] == "1":
ans += pref_s[i]
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER STRING ASSIGN VAR FUNC_CALL STRING VAR VAR VAR VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | for _ in range(int(input())):
n, m = map(int, input().split())
b = [input() for i in range(n)]
b.sort(key=lambda x: x.count("1"))
inversion = 0
cnt = 0
for i in "".join(b):
if i == "1":
cnt += 1
else:
inversion = inversion + cnt
print(inversion) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL STRING VAR IF VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | for _ in range(int(input())):
N, M = map(int, input().split())
A = []
for i in range(N):
k = input()
A.append(k)
A.sort(key=lambda x: x.count("1"))
ans = "".join(A)
z = ans.count("0")
c = 0
for i in range(len(ans)):
if ans[i] == "0":
z -= 1
else:
c += z
print(c) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | def sortkey(a):
return a[1]
def inversion(string):
left1s = 0
count = 0
for ele in string:
if ele == "1":
left1s += 1
else:
count += left1s
return count
t = int(input())
for _ in range(t):
n, m = map(int, input().split())
a = []
while n != 0:
s = input()
count = 0
for ele in s:
if ele == "0":
count += 1
a.append([s, count])
n -= 1
a.sort(key=sortkey, reverse=True)
string = ""
for val in a:
string += val[0]
res = inversion(string)
print(res) | FUNC_DEF RETURN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING FOR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | for _ in range(int(input())):
n, m = map(int, input().split())
res = []
for i in range(n):
k = input()
res.append([k.count("0"), k])
res.sort(key=lambda x: x[0], reverse=True)
t = ""
for i in res:
t += i[1]
m = t.count("0")
ans = 0
for i in t:
if i == "0":
m -= 1
else:
ans += m
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | def count(s):
ones1 = 0
for ele in s:
if ele == "1":
ones1 += 1
return ones1
t = int(input())
while t > 0:
sizes = input().split(" ")
n = int(sizes[0])
m = int(sizes[1])
arr = [0] * n
for i in range(0, n):
st = input()
arr[i] = st
marr = []
for item in arr:
cnt = count(item)
marr.append((cnt, item))
marr.sort()
res = ""
for item in marr:
res += item[1]
tsize = n * m
onecnt = 0
inv = 0
for i in range(0, tsize):
if res[i] == "1":
onecnt += 1
continue
else:
inv += onecnt
print(inv)
t -= 1 | FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | for i in range(int(input())):
n, m = map(int, input().split())
ones = 0
res = 0
s = [input() for i in range(n)]
s.sort(key=lambda x: x.count("1"))
for i in "".join(s):
if i == "1":
ones += 1
else:
res += ones
print(res) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL STRING VAR IF VAR STRING VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | for _ in range(int(input())):
N, M = input().split()
N = int(N)
M = int(M)
strings = list()
for _ in range(N):
strings.append(input())
strings = sorted(strings, key=lambda s: s.count("1"))
ones = 0
inv = 0
for c in "".join(strings):
if c == "1":
ones += 1
else:
inv += ones
print(inv) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL STRING VAR IF VAR STRING VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | def count(x):
ans = 0
for i in x:
if i == "1":
ans += 1
return ans
def mgc(n, a):
a.sort()
x = ""
for i in a:
x += i[1]
ans = 0
zeroes = 0
for i in x:
if i == "0":
zeroes += 1
for i in range(len(x)):
if x[i] == "1":
ans += zeroes
else:
zeroes -= 1
return ans
for _ in range(int(input())):
n, m = map(int, input().split())
arr = []
for i in range(n):
temp = input()
arr.append([count(temp), temp])
print(mgc(n, arr)) | FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | for _ in range(int(input())):
n, m = map(int, input().split())
l = []
for i in range(n):
l.append(input())
l.sort(key=lambda x: x.count("1"))
s = ""
for i in l:
s += i
ones = 0
ans = 0
for i in s:
if i == "0":
ans += ones
else:
ones += 1
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR STRING FOR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | def solve(N, M, A):
A.sort(key=lambda x: x.count("1"))
a = "".join(A)
ans = 0
zero = 0
for x in reversed(a):
if x == "0":
zero += 1
else:
ans += zero
return ans
T = int(input())
for _ in range(T):
N, M = map(int, input().split())
A = []
for _ in range(N):
A.append(input().strip())
print(solve(N, M, A)) | FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR STRING VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | def check(a):
return a.count("1")
t = int(input())
for _ in range(t):
n, m = map(int, input().split())
data = [input() for i in range(n)]
data.sort(key=lambda x: x.count("1"))
s = ""
s = s.join(data)
total = s.count("0")
sharma = 0
for i in s:
if i == "0":
total -= 1
else:
sharma += total
print(sharma) | FUNC_DEF RETURN FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | for i in range(int(input())):
n, m = map(int, input().split())
l = []
L = []
for j in range(n):
s = input()
o = s.count("1")
l.append([o, j])
L.append(s)
l.sort()
p = []
for k in range(len(l)):
v = l[k]
d = v[1]
p.append(L[d])
v = "".join(map(str, p))
a = v.count("0")
b = m * n - a
c = 0
for j in range(len(v)):
if v[j] == "0":
a -= 1
else:
c += a
print(c) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | def numberOfInversions(s: str, l: int) -> int:
ans = 0
num_ones = 0
for i in range(l):
if s[i] == "1":
num_ones += 1
else:
ans += num_ones
return ans
t = int(input().strip())
result = []
for _ in range(t):
n, m = list(map(int, input().rstrip().split()))
arr = [input().strip() for i in range(n)]
arr.sort(key=lambda s: s.count("1"))
ans_str = "".join(arr)
result.append(numberOfInversions(ans_str, n * m))
for r in result:
print(r) | FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | def func(x):
ones = 0
for ch in x:
if ch == "1":
ones += 1
return ones
t = int(input())
for T in range(t):
n, m = map(int, input().split())
strings = []
for i in range(n):
strings.append(input())
strings = sorted(strings, key=func)
concat_str = "".join(strings)
ones = 0
ans = 0
for ch in concat_str:
if ch == "0":
ans += ones
else:
ones += 1
print(ans) | FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | def func(N, M, arr):
i = 0
dp = [[] for i in range(N)]
d = {}
for i in range(N):
c = 0
for j in arr[i]:
if j == "1":
c += 1
d[i] = c
d = dict(sorted(d.items(), key=lambda x: x[1]))
pos = 0
for i, j in d.items():
dp[pos] = arr[i]
pos += 1
ans = 0
s = ""
inver = 0
for i in dp:
for j in range(M):
if i[j] == "1":
ans += 1
else:
inver += ans
return inver
T = int(input())
for i in range(T):
N, M = map(int, input().split())
arr = []
for j in range(N):
x = list(input())
arr.append(x)
print(func(N, M, arr)) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | def king_coding():
n, m = map(int, input().split())
L = []
for i in range(n):
L.append(input())
L.sort(key=lambda b: b.count("1"))
c, s = 0, 0
for i in range(1, n + 1):
for j in range(1, m + 1):
if L[n - i][m - j] == "0":
c += 1
else:
s += c
print(s)
for _ in range(int(input())):
king_coding() | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR BIN_OP VAR VAR STRING VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | for _ in range(int(input())):
n, m = map(int, input().split())
l = []
for i in range(n):
r = input()
l.append(r)
l.sort(key=lambda x: x.count("1"))
s = ""
for i in range(len(l)):
s = s + l[i]
d = []
s = list(char for char in s)
for i in range(len(s)):
if s[i] == "0":
d.append(i)
su = 0
for i in range(0, len(d)):
if i == 0:
su = su + d[0] * len(d)
else:
su = su + (d[i] - d[i - 1] - 1) * (len(d) - i)
print(su) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | test = int(input())
for i in range(0, test):
n, v = map(int, input().split())
list1 = []
list2 = []
for z in range(n):
b = input()
list1.append(b)
list1.sort(key=lambda i: i.count("1"))
ans = ""
for p in range(n):
ans = ans + list1[p]
one = 0
cost = 0
for f in range(0, len(ans)):
if ans[f] == "1":
one += 1
else:
cost += one
print(cost) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | t = int(input())
for _ in range(t):
n, m = map(int, input().split())
A = [0] * n
for i in range(n):
A[i] = input()
A.sort(key=lambda i: i.count("1"))
ones, count = 0, 0
for i in "".join(A):
if i == "1":
ones += 1
else:
count += ones
print(count) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL STRING VAR IF VAR STRING VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | for i in range(int(input())):
n, m = map(int, input().split())
c = 0
l = []
for j in range(n):
s = str(input())
c0, c1 = 0, 0
for k in range(m):
if s[m - 1 - k] == "1":
c += c0
c1 += 1
else:
c0 += 1
l.append(c0)
l.sort(reverse=True)
ss = sum(l)
for j in range(n):
c += (m - l[j]) * (ss - l[j])
ss -= l[j]
print(c) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER VAR STRING VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | t = int(input())
for i in range(t):
n, m = map(int, input().split())
l = []
for j in range(n):
s = input()
c = 0
p = 0
for k in range(m):
if s[k] == "1":
c += 1
if s[k] == "0":
p += c
l.append([c, p, m - c, s])
l = sorted(l)
val = l[0][0]
ans = l[0][1]
for ii in range(1, n):
ans += val * l[ii][2] + l[ii][1]
val += l[ii][0]
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | for _ in range(int(input())):
l = []
x, y = map(int, input().split())
for i in range(x):
s = str(input())
l.append(s)
l.sort(key=lambda x: x.count("1"))
ans = 0
c = 0
s = str("")
for i in range(x):
s += l[i]
for i in range(len(s)):
if s[i] == str(1):
c += 1
elif s[i] == str(0):
ans += c
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | for _ in range(int(input())):
n, m = map(int, input().split())
l = []
for i in range(n):
l.append(input())
l.sort(key=lambda b: b.count("1"))
p = ""
for i in l:
p = p + i
z = 0
ans = 0
for i in range(len(p) - 1, -1, -1):
if p[i] == "0":
z = z + 1
elif p[i] == "1":
ans = ans + z
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | def process(List):
Min, one = 0, 0
List.sort(key=lambda item: item.count("1"))
val = "".join(List)
for i in val:
if i == "0":
Min += one
if i == "1":
one += 1
return Min
for _ in range(int(input())):
List = []
N, M = map(int, input().split())
count = 0
for i in range(N):
List.append(input())
print(process(List)) | FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL STRING VAR FOR VAR VAR IF VAR STRING VAR VAR IF VAR STRING VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | tc = int(input())
for _ in range(tc):
c, bitc = map(int, input().split())
map_ = {}
for i in range(c):
t = input()
tc = t.count("1")
if tc not in map_:
map_[tc] = []
map_[tc].append(t)
keys = sorted(map_.keys(), reverse=True)
for key in map_:
map_[key].sort(key=lambda x: int(x, 2))
text = ""
for key in keys:
text = "".join(map_[key]) + text
text = text[::-1]
zc = 0
res = 0
for c in text:
if c == "0":
zc += 1
else:
res += zc
print(res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL STRING VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | def inversion(string):
ones, count = 0, 0
for i in string:
if i == "1":
ones += 1
else:
count += ones
print(count)
for i in range(int(input())):
N, M = map(int, input().split(" "))
bin = []
for s in range(N):
K = input()
bin.append(K)
bin.sort(key=lambda val: val.count("1"))
string = "".join(bin)
inversion(string) | FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | def cont(ele):
flag = 0
for i in ele:
if i == "0":
flag += 1
return flag
t = int(input())
for tt in range(t):
n, m = map(int, input().split())
zer = []
one = []
zersum = [0] * n
u = 0
for nn in range(n):
ele = input()
r = cont(ele)
zer.append(r)
c = r
for y in ele:
if y == "1":
u += c
else:
c -= 1
zer.sort(reverse=True)
p = 0
for el in zer:
one.append(m - el)
for w in range(n - 1, -1, -1):
zersum[w] = p + zer[w]
p = zersum[w]
for y in range(n - 1):
u += one[y] * zersum[y + 1]
print(u) | FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR VAR IF VAR STRING VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | def SieveOfEratosthenes(n):
prime = [(True) for i in range(n + 1)]
p = 2
while p * p <= n:
if prime[p] == True:
for i in range(p**2, n + 1, p):
prime[i] = False
p += 1
prime[0] = False
prime[1] = False
for p in range(n + 1):
if prime[p]:
print(p)
for _ in range(int(input())):
n, m = map(int, input().split())
a = [input() for i in range(n)]
a.sort(key=lambda x: x.count("1"))
one = 0
inv = 0
for i in "".join(a):
if i == "1":
one += 1
else:
inv += one
print(inv) | FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL STRING VAR IF VAR STRING VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | from sys import stdin
t = int(stdin.readline().rstrip())
while t > 0:
n, m = map(int, stdin.readline().rstrip().split(" "))
nums = []
for i in range(n):
s = stdin.readline().strip()
nums.append(s)
nums.sort(key=lambda i: i.count("1"))
nums = "".join(nums)
ones = 0
inversions = 0
for i in nums:
if i == "1":
ones += 1
else:
inversions += ones
print(inversions)
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | def solve():
n, m = map(int, input().split())
l = []
for i in range(n):
s = input()
l.append((s, s.count("0")))
l = sorted(l, key=lambda x: x[1], reverse=True)
ans = ""
for i in l:
ans += i[0]
count_0 = 0
answer = 0
for i in ans[::-1]:
if i == "1":
answer += count_0
else:
count_0 += 1
print(answer)
t = int(input())
while t != 0:
solve()
t -= 1 | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR STRING VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | t = int(input())
while t:
n, m = map(int, input().split())
arr = ["" for i in range(n)]
for i in range(n):
arr[i] = input()
arr.sort(key=lambda x: x.count("1"))
st = "".join(arr)
cnt_z = 0
cnt_o = 0
for i in range(len(st) - 1, -1, -1):
if st[i] == "0":
cnt_z += 1
else:
cnt_o += cnt_z
print(cnt_o)
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$2 ≤ N \cdot M ≤ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions. | def countinverse(no):
count = 0
val = 0
for x in reversed(no):
if x == "0":
val += 1
if x == "1":
count += val
return count
for _ in range(int(input())):
arr = []
string, length = map(int, input().split())
for s in range(string):
arr.append(input())
arr.sort(key=lambda x: x.count("1"))
arr = map(str, arr)
no = "".join(arr)
print(countinverse(no)) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
heap = []
for x, y in points:
if len(heap) < K:
heapq.heappush(heap, [-(x * x + y * y), [x, y]])
else:
heapq.heappushpop(heap, [-(x * x + y * y), [x, y]])
return [pair for value, pair in heap] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR LIST BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR LIST BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR LIST VAR VAR RETURN VAR VAR VAR VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
dist = lambda x: math.sqrt(points[x][0] ** 2 + points[x][1] ** 2)
def dc(i, j, K):
if i >= j:
return
oi, oj = i, j
i += 1
pivot = dist(oi)
while True:
while i < j and dist(i) < pivot:
i += 1
while i <= j and dist(j) >= pivot:
j -= 1
if i >= j:
break
points[i], points[j] = points[j], points[i]
points[oi], points[j] = points[j], points[oi]
if K - 1 == j:
return
elif K - 1 < j:
dc(oi, j, K)
else:
dc(j + 1, oj, K)
dc(0, len(points) - 1, K)
return points[:K] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE NUMBER WHILE VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR RETURN IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN VAR VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
pointDistances = [
[point, self.calculateDistanceToOrigin(point)] for point in points
]
sortedPoints = sorted(pointDistances, key=lambda pd: pd[1])
return [pd[0] for pd in sortedPoints[:K]]
def calculateDistanceToOrigin(self, point):
return point[0] * point[0] + point[1] * point[1] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR NUMBER VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
def norm(point):
return point[0] ** 2 + point[1] ** 2
dis = []
for point in points:
dis.append(norm(point))
dis, points = list(zip(*sorted(zip(dis, points))))
res = []
for i in range(K):
res.append(points[i])
return res | CLASS_DEF FUNC_DEF VAR VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
def edistance(point):
d = sqrt(point[0] ** 2 + point[1] ** 2)
return d
result = []
stk = []
for point in points:
dis = edistance(point)
heapq.heappush(stk, [dis, point])
while K > 0:
result.append(heapq.heappop(stk)[1])
K -= 1
return result | CLASS_DEF FUNC_DEF VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER RETURN VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR LIST VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
li = []
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def distance_square(self):
return self.x * self.x + self.y * self.y
def __lt__(self, other):
return self.distance_square() < other.distance_square()
for p in points:
x = p[0]
y = p[1]
pt = Point(x, y)
heapq.heappush(li, pt)
result_p = [heapq.heappop(li) for i in range(K)]
result = [[p.x, p.y] for p in result_p]
return result | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR VAR VAR VAR RETURN VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
left = 0
right = len(points) - 1
target = K - 1
while left <= right:
rand_pivot = random.randint(left, right)
pivot = self.partition(left, right, rand_pivot, points)
if pivot == target:
return points[0 : pivot + 1]
elif pivot < target:
left = pivot + 1
else:
right = pivot - 1
def partition(self, i, j, pivot, arr):
pivot_element = self.distFromOrigin(arr[pivot])
self.swap(pivot, j, arr)
result = i
for x in range(i, j):
if self.distFromOrigin(arr[x]) < pivot_element:
self.swap(x, result, arr)
result += 1
self.swap(result, j, arr)
return result
def swap(self, i, j, arr):
temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
def distFromOrigin(self, point):
return math.sqrt(point[0] ** 2 + point[1] ** 2) | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR RETURN VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
heap = []
indices = {}
for i in range(len(points)):
curr_point = points[i]
distance = math.sqrt(curr_point[0] ** 2 + curr_point[1] ** 2)
heapq.heappush(heap, distance)
if distance in list(indices.keys()):
indices[distance].append(i)
else:
indices[distance] = [i]
result = []
i = 0
while i < K:
curr_distance = heapq.heappop(heap)
curr_indices = indices[curr_distance]
for index in curr_indices:
result.append(points[index])
i += 1
return result | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
def distance(point):
return (point[0] ** 2 + point[1] ** 2) ** 0.5
def partition(left, right, pivot_index):
pivot = points[pivot_index]
points[pivot_index], points[right] = points[right], points[pivot_index]
store_index = left
for i in range(left, right):
if distance(points[i]) < distance(pivot):
points[store_index], points[i] = points[i], points[store_index]
store_index += 1
points[store_index], points[right] = points[right], points[store_index]
return store_index
left, right = 0, len(points) - 1
while True:
pivot_index = random.randint(left, right)
M = partition(left, right, pivot_index)
if M == K - 1:
break
elif M < K - 1:
left = M + 1
else:
right = M - 1
return points[:K] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
heap = []
for x, y in points:
dist = -(x**2 + y**2)
if len(heap) >= K:
heapq.heappushpop(heap, (dist, x, y))
else:
heapq.heappush(heap, (dist, x, y))
return [[x, y] for dist, x, y in heap] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN LIST VAR VAR VAR VAR VAR VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
heap = []
for i, p in enumerate(points):
x, y = p
dist = x**2 + y**2
heapq.heappush(heap, (-dist, i))
if len(heap) > K:
heapq.heappop(heap)
res = []
for _, i in heap:
res.append(points[i])
return res | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points, K: int):
return [
points[i]
for d, i in sorted(
[[x[0] ** 2 + x[1] ** 2, i] for i, x in enumerate(points)]
)[0:K]
] | CLASS_DEF FUNC_DEF VAR RETURN VAR VAR VAR VAR FUNC_CALL VAR LIST BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
self.qkSel(points, 0, len(points) - 1, K)
return points[:K]
def qkSel(self, points, l, r, K):
if l > r:
return
idx = random.randint(l, r)
points[idx], points[r] = points[r], points[idx]
i = l
for j in range(l, r):
if self.cmp(points[j], points[r]):
points[i], points[j] = points[j], points[i]
i += 1
points[i], points[r] = points[r], points[i]
if i == K:
return
elif i < K:
return self.qkSel(points, i + 1, r, K)
elif i > K:
return self.qkSel(points, l, i - 1, K)
def cmp(self, p1, p2):
return p1[0] ** 2 + p1[1] ** 2 - (p2[0] ** 2 + p2[1] ** 2) < 0 | CLASS_DEF FUNC_DEF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN VAR VAR VAR VAR VAR FUNC_DEF IF VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR RETURN IF VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
return sorted(
points, key=lambda point: point[0] * point[0] + point[1] * point[1]
)[:K] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
def parition(st, end):
lo, i, j = st, st, end
while True:
while i < end and arr[i][0] <= arr[lo][0]:
i += 1
while j > st and arr[lo][0] <= arr[j][0]:
j -= 1
if i >= j:
break
arr[i], arr[j] = arr[j], arr[i]
arr[lo], arr[j] = arr[j], arr[lo]
return j
def quick_select(st, end):
if st > end:
return
par = parition(st, end)
if par == K:
return
if par > K:
quick_select(st, par - 1)
else:
quick_select(par + 1, end)
return
arr = []
for pt in points:
arr.append([math.sqrt(sum([(x**2) for x in pt])), pt])
random.shuffle(arr)
quick_select(0, len(arr) - 1)
return [x[1] for x in arr[:K]] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR WHILE NUMBER WHILE VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR LIST FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER VAR VAR VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
shuffle(points)
distance_dict = dict()
def distance(xy):
xy = tuple(xy)
if xy in distance_dict:
return distance_dict[xy]
ans = xy[0] ** 2 + xy[1] ** 2
distance_dict[xy] = ans
return ans
def _quicksort(points, left, right):
if left > right:
return
pivot = right
lower = left
for i in range(left, right):
if distance(points[i]) <= distance(points[pivot]):
points[lower], points[i] = points[i], points[lower]
lower = lower + 1
pivot = lower
points[pivot], points[right] = points[right], points[pivot]
if K - 1 <= pivot:
_quicksort(points, left, pivot - 1)
else:
_quicksort(points, left, pivot - 1)
_quicksort(points, pivot + 1, right)
return
_quicksort(points, 0, len(points) - 1)
return points[:K] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN EXPR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
def get_distance(point):
return point[0] ** 2 + point[1] ** 2
def helper(i, j, K):
if i >= j:
return
pivot_index = random.randint(i, j)
points[j], points[pivot_index] = points[pivot_index], points[j]
mid = partition(i, j)
if mid == K - 1:
return
elif mid < K - 1:
helper(mid + 1, j, K)
else:
helper(i, mid - 1, K)
def partition(i, j):
left, right = i, j
pivot_distance = get_distance(points[j])
for k in range(left, right):
if get_distance(points[k]) < pivot_distance:
points[i], points[k] = points[k], points[i]
i += 1
points[i], points[j] = points[j], points[i]
return i
helper(0, len(points) - 1, K)
return points[:K] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER RETURN IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN VAR VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
for i in range(len(points)):
points[i].insert(0, (points[i][0] ** 2 + points[i][1] ** 2) ** 0.5)
points.sort()
ans = []
for i in range(K):
ans.append(points[i][1:])
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
dist = lambda i: points[i][0] ** 2 + points[i][1] ** 2
def quicksort(points, K):
print(points, K)
if not points:
return
pivot = points[-1]
l, r, m = [], [], []
for i in points:
if dist(i) == dist(pivot):
m.append(i)
elif dist(i) < dist(pivot):
l.append(i)
else:
r.append(i)
if len(l) >= K:
return quicksort(l, K)
elif len(l) < K <= len(l) + len(m):
return l + m[: K - len(l)]
else:
return l + m + quicksort(r, K - len(l) - len(m))
return [points[i] for i in quicksort([i for i in range(len(points))], K)] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR VAR IF VAR RETURN ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR LIST LIST LIST FOR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR BIN_OP VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
n1 = len(points)
newList = [0] * n1
for i in range(n1):
pointX = points[i][0]
pointY = points[i][1]
d2 = pointX * pointX + pointY * pointY
newList[i] = d2
newList.sort()
res = newList[K - 1]
i = 0
t = 0
ans = []
while i < n1 and t < K:
if res >= points[i][0] * points[i][0] + points[i][1] * points[i][1]:
ans = ans + [points[i]]
t += 1
i += 1
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR LIST VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
distances = []
for idx, point in enumerate(points):
eucliDist = point[0] ** 2 + point[1] ** 2
heapq.heappush(distances, (eucliDist, point))
distances = heapq.nsmallest(K, distances)
result = []
for res in distances:
result.append(res[1])
return result | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
pq = []
for x, y in points:
if len(pq) < K:
heapq.heappush(pq, (-x * x - y * y, [x, y]))
else:
small_distance, small_coordinate = heapq.heappop(pq)
if -x * x - y * y > small_distance:
heapq.heappush(pq, (-x * x - y * y, [x, y]))
else:
heapq.heappush(pq, (small_distance, small_coordinate))
return [_[1] for _ in pq] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR LIST VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR NUMBER VAR VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
nums = []
heapq.heapify(nums)
def dist(x, y):
return (x**2 + y**2) ** 0.5
for x, y in points:
heapq.heappush(nums, (-dist(x, y), [x, y]))
if len(nums) > K:
heapq.heappop(nums)
return [k[1] for k in nums] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR LIST VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER VAR VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def calculateEuclidean(self, point):
return point[0] ** 2 + point[1] ** 2
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
heap = []
for point in points:
heapq.heappush(heap, (-self.calculateEuclidean(point), point))
if len(heap) > K:
heapq.heappop(heap)
return [point[1] for point in heap] | CLASS_DEF FUNC_DEF RETURN BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER VAR VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
if not points:
return []
def distance(x, y):
return ((x - 0) ** 2 + (y - 0) ** 2) ** 0.5
def binsea_insert(dist, point):
if not nearest:
nearest.append((dist, point))
return
left, right = 0, len(nearest) - 1
while left <= right:
d, coor = nearest[right]
if dist >= d:
nearest.insert(right + 1, (dist, point))
return
d, coor = nearest[left]
if dist <= d:
nearest.insert(left, (dist, point))
return
mid = left + (right - left) // 2
d, coor = nearest[mid]
if dist < d:
right = mid - 1
else:
left = mid + 1
nearest = []
for point in points:
x, y = point
binsea_insert(distance(x, y), point)
print(nearest)
results = []
for k in range(K):
if k < len(nearest):
d, point = nearest[k]
results.append(point)
return results | CLASS_DEF FUNC_DEF VAR VAR VAR VAR IF VAR RETURN LIST FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR VAR RETURN ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN ASSIGN VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
dist = lambda i: points[i][0] ** 2 + points[i][1] ** 2
def sort(i, j, K):
if i >= j:
return
k = random.randint(i, j)
points[i], points[k] = points[k], points[i]
mid = partition(i, j)
if K < mid - i + 1:
sort(i, mid - 1, K)
elif K > mid - i + 1:
sort(mid + 1, j, K - (mid - i + 1))
def partition(i, j):
oi = i
pivot = dist(i)
i += 1
while True:
while i < j and dist(i) < pivot:
i += 1
while i <= j and dist(j) >= pivot:
j -= 1
if i >= j:
break
points[i], points[j] = points[j], points[i]
points[oi], points[j] = points[j], points[oi]
return j
sort(0, len(points) - 1, K)
return points[:K] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER WHILE NUMBER WHILE VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN VAR VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
heap = [(float("-Inf"), 0, 0)] * K
for x, y in points:
heapq.heappushpop(heap, (-(x**2) - y**2, x, y))
res = []
while heap:
val, row, col = heapq.heappop(heap)
res.append([row, col])
return res | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING NUMBER NUMBER VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]:
def distance(point):
return point[0] ** 2 + point[1] ** 2
def find_pivot_index(low, high, p_index):
ppoint = points[p_index]
points[p_index], points[high] = points[high], points[p_index]
i = low
j = low
while j < high:
if distance(points[j]) < distance(points[high]):
points[i], points[j] = points[j], points[i]
i += 1
j += 1
points[i], points[high] = points[high], points[i]
return i
def recursion(low, high):
pivot_index = random.randint(low, high)
p = find_pivot_index(low, high, pivot_index)
if p == k - 1:
return points[: p + 1]
elif p < k - 1:
return recursion(p + 1, high)
else:
return recursion(low, p - 1)
return recursion(0, len(points) - 1) | CLASS_DEF FUNC_DEF VAR VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER RETURN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
distance = lambda x, y: sqrt(x**2 + y**2)
k = 0
output = []
for s in sorted(points, key=lambda x: distance(x[0], x[1])):
if k < K:
output.append(s)
else:
break
k += 1
return output | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
self.countK(points, 0, len(points) - 1, K)
return points[:K]
def countK(self, points, l, r, k):
if l < r:
p = self.createPartition(points, l, r, k)
if p == k:
return
elif p < k:
self.countK(points, p + 1, r, k)
else:
self.countK(points, l, p - 1, k)
def createPartition(self, points, l, r, k):
pivot = points[r]
count = l
for i in range(l, r):
if points[i][0] ** 2 + points[i][1] ** 2 <= pivot[0] ** 2 + pivot[1] ** 2:
points[i], points[count] = points[count], points[i]
count += 1
points[count], points[r] = points[r], points[count]
return count | CLASS_DEF FUNC_DEF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN VAR VAR VAR VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
distance = [
((x[0] ** 2 + x[1] ** 2) ** (1 / 2), i) for i, x in enumerate(points)
]
heapq.heapify(distance)
ans = []
while K > 0:
element = heapq.heappop(distance)
ans.append(points[element[1]])
K -= 1
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER NUMBER VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
pq = []
def distance(point):
return point[0] * point[0] + point[1] * point[1]
for index, point in enumerate(points):
heapq.heappush(pq, (distance(point), index, point))
ans = []
k = 0
while pq and k < K:
_, _, point = heapq.heappop(pq)
ans.append(point)
k += 1
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST FUNC_DEF RETURN BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
distant = [[point, point[0] ** 2 + point[1] ** 2] for point in points]
distant.sort(key=itemgetter(1))
res = [distant[i][0] for i in range(K)]
return res | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
def distance(point):
x, y = point
return x**2 + y**2
distances = [(distance(p), p) for p in points]
heapq.heapify(distances)
for point in points:
heapq.heappushpop(distances, (-distance(point), point))
return [p for _, p in heapq.nsmallest(K, distances)] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR RETURN BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Node:
def __init__(self, x, y, d=None):
self.x = x
self.y = y
if d:
self.distance = d
else:
self.distance = x * x + y * y
def parent(i):
return (i - 1) // 2
def left(i):
return 2 * i + 1
def right(i):
return 2 * i + 2
def swap(heap, i, j):
t = heap[i]
heap[i] = heap[j]
heap[j] = t
def heapify(heap):
n = len(heap)
for i in range(1, n):
p = parent(i)
while i > 0 and heap[p].distance < heap[i].distance:
swap(heap, i, p)
i = p
p = parent(i)
def replace_top(heap, node):
t = heap[0]
heap[0] = node
n = len(heap)
i = 0
l = left(i)
r = right(i)
while i < n and not (
(l >= n or heap[l].distance <= heap[i].distance)
and (r >= n or heap[r].distance <= heap[i].distance)
):
if l < n and r < n:
if heap[l].distance < heap[r].distance:
swap(heap, i, r)
i = r
else:
swap(heap, i, l)
i = l
elif l < n:
swap(heap, i, l)
i = l
else:
swap(heap, i, r)
i = r
l = left(i)
r = right(i)
class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
n = len(points)
heap = []
for i in range(K):
p = points[i]
node = Node(p[0], p[1])
heap.append(node)
heapify(heap)
top = 0
for i in range(K, n):
p = points[i]
distance = p[0] * p[0] + p[1] * p[1]
if heap[top].distance > distance:
node = Node(p[0], p[1], distance)
replace_top(heap, node)
return [[heap[i].x, heap[i].y] for i in range(K)] | CLASS_DEF FUNC_DEF NONE ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF RETURN BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR RETURN LIST VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
def partition(start, end):
pivot = start
left = start + 1
right = end
while True:
while left < right and not comparer(points[left], points[pivot]):
left += 1
while left <= right and comparer(points[right], points[pivot]):
right -= 1
if left >= right:
break
points[left], points[right] = points[right], points[left]
points[pivot], points[right] = points[right], points[pivot]
return right
def comparer(point1, point2):
return point1[0] ** 2 + point1[1] ** 2 >= point2[0] ** 2 + point2[1] ** 2
left, right, mid = 0, len(points) - 1, 0
while left <= right:
mid = partition(left, right)
if mid == K - 1:
break
elif mid > K - 1:
right = mid - 1
else:
left = mid + 1
return points[:K] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE NUMBER WHILE VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Node:
def __init__(self, c, sum):
self.sum = sum
self.c = c
class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
if not points:
return []
heap = []
ans = []
for i in points:
heap.append(Node(i, i[0] ** 2 + i[1] ** 2))
n = len(heap)
def heapify(arr, i, n):
smallest = i
left = i * 2 + 1
right = i * 2 + 2
if left < n and arr[i].sum > arr[left].sum:
smallest = left
if right < n and arr[right].sum < arr[smallest].sum:
smallest = right
if smallest != i:
arr[smallest], arr[i] = arr[i], arr[smallest]
heapify(arr, smallest, n)
for i in range((n - 1) // 2, -1, -1):
heapify(heap, i, n)
while K > 0 and len(ans) < len(points):
h = heap[0]
ans.append(h.c)
heap[0].sum = sys.maxsize
heapify(heap, 0, len(heap))
K -= 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR CLASS_DEF FUNC_DEF VAR VAR VAR VAR IF VAR RETURN LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR WHILE VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def _getDistance(self, point):
dis = math.sqrt(point[0] ** 2 + point[1] ** 2)
return dis
def _partition(self, start, end, points):
target = points[start]
while start < end:
while start < end and self._getDistance(points[end]) >= self._getDistance(
target
):
end -= 1
points[start] = points[end]
while start < end and self._getDistance(points[start]) <= self._getDistance(
target
):
start += 1
points[end] = points[start]
points[start] = target
return start
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
if not points:
return []
start = 0
end = len(points) - 1
while start <= end:
mid = self._partition(start, end, points)
if mid == K:
return points[:K]
elif mid > K:
end = mid - 1
else:
start = mid + 1
return points[:K] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR WHILE VAR VAR WHILE VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR WHILE VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF VAR VAR VAR VAR IF VAR RETURN LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
heap = []
for x, y in points:
dis2 = x**2 + y**2
if len(heap) < K:
heapq.heappush(heap, (-dis2, (x, y)))
elif -dis2 > heap[0][0]:
heapq.heappop(heap)
heapq.heappush(heap, (-dis2, (x, y)))
ans = []
for i in range(K):
x, y = heapq.heappop(heap)[1]
ans = [[x, y]] + ans
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST LIST VAR VAR VAR RETURN VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
h = []
for point in points:
d = 0
for cor in point:
d += cor**2
h.append((d, point))
ksmallests = heapq.nsmallest(K, h)
ret = []
for val, point in ksmallests:
ret.append(point)
return ret | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
res = []
def insert(d, pos):
l = 0
r = len(res)
while l < r:
mid = (l + r) // 2
if res[mid][0] > d:
r = mid
else:
l = mid + 1
res.insert(l, [d, pos])
Max = res[-1][0]
Max = float("inf")
for i in range(len(points)):
dis = points[i][0] ** 2 + points[i][1] ** 2
print(len(res))
if i > K:
res.pop()
insert(dis, points[i])
ans = []
for i in range(K):
ans.append(res[i][1])
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR LIST VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
length = len(points)
if length <= K:
return points
distances: Dict[int, List[int]] = defaultdict(list)
for i, li in enumerate(points):
distance = (li[0] ** 2 + li[1] ** 2) ** 0.5
distances[distance].append(i)
order = OrderedDict(sorted(distances.items()))
ans = []
for i, (key, value) in enumerate(order.items()):
for j in value:
ans.append(points[j])
if len(ans) >= K:
break
return ans[:K] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class myObj:
def __init__(self, val, p):
self.val = val
self.p = p
def __lt__(self, other):
return self.val < other.val
class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
def partition(arr, left, right):
i = left
for j in range(left, right):
if arr[j] < arr[right]:
arr[i], arr[j] = arr[j], arr[i]
i += 1
arr[i], arr[right] = arr[right], arr[i]
return i
def sort(arr, left, right, K):
if left < right:
p = partition(arr, left, right)
if p == K:
return
elif p < K:
sort(arr, p + 1, right, K)
else:
sort(arr, left, p - 1, K)
dis = [0] * len(points)
for i in range(len(points)):
dis[i] = myObj(
points[i][0] * points[i][0] + points[i][1] * points[i][1], points[i]
)
sort(dis, 0, len(points) - 1, K)
ans = []
for i in range(K):
ans.append(dis[i].p)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF RETURN VAR VAR CLASS_DEF FUNC_DEF VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
points = [[i**2 + j**2, i, j] for i, j in points]
def quickSelect(l, r):
p = l
for i in range(l, r):
if points[i][0] < points[r][0]:
points[p], points[i] = points[i], points[p]
p += 1
points[r], points[p] = points[p], points[r]
if p == K - 1:
return
elif p > K - 1:
quickSelect(l, p - 1)
else:
quickSelect(p + 1, r)
quickSelect(0, len(points) - 1)
return [[i, j] for _, i, j in points[:K]] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER RETURN IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER RETURN LIST VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
squared = [round((x[0] ** 2 + x[1] ** 2) ** (1 / 2), 2) for x in points]
self.heapSort(points, squared)
result = []
for i in range(K):
result.append(points[i])
return result
def heapify(self, points, squared, n, i):
largest = i
l = 2 * i + 1
r = 2 * i + 2
if l < n and squared[l] > squared[largest]:
largest = l
if r < n and squared[r] > squared[largest]:
largest = r
if largest != i:
squared[largest], squared[i] = squared[i], squared[largest]
points[largest], points[i] = points[i], points[largest]
self.heapify(points, squared, n, largest)
def heapSort(self, points, squared):
n = len(squared)
for i in range(n // 2 - 1, -1, -1):
self.heapify(points, squared, n, i)
for i in range(n - 1, 0, -1):
squared[i], squared[0] = squared[0], squared[i]
points[i], points[0] = points[0], points[i]
self.heapify(points, squared, i, 0) | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
if not points or len(points) < 1:
return []
distances = []
for point in points:
dist = point[0] * point[0] + point[1] * point[1]
if len(distances) < K:
heapq.heappush(distances, (-dist, point))
elif -dist > distances[0][0]:
heapq.heappushpop(distances, (-dist, point))
return [point for _, point in distances] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR NUMBER RETURN LIST ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
dic = collections.defaultdict(list)
for p in points:
d = p[0] ** 2 + p[1] ** 2
dic[d].append(p)
dic2 = list(dic.keys())
print(dic2)
heapq.heapify(dic2)
res = []
count = 0
while count < K:
res.extend(dic[heapq.heappop(dic2)])
count = len(res)
return res | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
start, end = 0, len(points) - 1
pivot = -1
while start <= end:
pivot = self.partition(points, start, end)
if pivot == K:
return points[0:pivot]
elif pivot > K:
end = pivot - 1
else:
start = pivot + 1
return points
def partition(self, points, start, end):
pivot = start
for i in range(start, end + 1):
if self.dist(points, i) < self.dist(points, end):
points[i], points[pivot] = points[pivot], points[i]
pivot += 1
points[end], points[pivot] = points[pivot], points[end]
return pivot
def dist(self, points, i):
return points[i][0] ** 2 + points[i][1] ** 2 | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN VAR NUMBER VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
K -= 1
start, end = 0, len(points) - 1
while start <= end:
mid = self.__partition(points, start, end)
if mid == K:
break
elif mid < K:
start = mid + 1
else:
end = mid - 1
return points[: K + 1]
def __partition(self, points, lo, hi):
__dist = lambda points, i: points[i][0] ** 2 + points[i][1] ** 2
d = __dist(points, lo)
i, j = lo, hi + 1
while True:
while i < hi and __dist(points, i + 1) < d:
i += 1
while j > lo and __dist(points, j - 1) > d:
j -= 1
i, j = i + 1, j - 1
if i >= j:
break
points[i], points[j] = points[j], points[i]
points[lo], points[j] = points[j], points[lo]
return j | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER WHILE NUMBER WHILE VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
if not points or not K:
return []
distances = []
disMap = defaultdict(list)
maxKDis = float("-inf")
for point in points:
dis = (point[0] ** 2 + point[1] ** 2) ** 0.5
disMap[dis].append(point)
if len(distances) < K:
distances.append(dis)
maxKDis = max(maxKDis, dis)
elif len(distances) >= K and dis < maxKDis:
distances.sort()
distances.pop()
distances.append(dis)
maxKDis = max(distances[-2:])
res = []
for dis in distances:
res.extend(disMap[dis])
return res[:K] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR IF VAR VAR RETURN LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
dictionary = {}
for i in points:
dictionary[i[0], i[1]] = math.sqrt((i[0] - 0) ** 2 + (i[1] - 0) ** 2)
heap = []
heapq.heapify(heap)
for i in list(dictionary.keys()):
heapq.heappush(heap, (dictionary[i], i))
result = []
for i in range(K):
result.append(heapq.heappop(heap)[1])
return result | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Point:
def __init__(self, pt):
self.distance = -1 * sqrt(pt[0] ** 2 + pt[1] ** 2)
self.pt = pt
def __lt__(self, other):
return self.distance < other.distance
def __eq__(self, other):
return self.distance == other.distance
class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
heap = []
heapq.heapify(heap)
for pt in points:
curr_pt = Point(pt)
if len(heap) < K:
heapq.heappush(heap, curr_pt)
else:
heapq.heappushpop(heap, curr_pt)
return [pt.pt for pt in heap] | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_DEF RETURN VAR VAR FUNC_DEF RETURN VAR VAR CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
def compare(p1, p2):
return p1[0] ** 2 + p1[1] ** 2 - (p2[0] ** 2 + p2[1] ** 2)
def quickSelect(l, r):
pivot = points[r]
j = l
for i in range(l, r):
if compare(points[i], pivot) <= 0:
points[i], points[j] = points[j], points[i]
j += 1
points[r], points[j] = points[j], points[r]
return j
l, r = 0, len(points) - 1
while l <= r:
pivot = quickSelect(l, r)
if pivot == K:
break
elif pivot < K:
l = pivot + 1
else:
r = pivot - 1
return points[:K] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
point_distance = dict()
distance_list = []
for x, y in points:
distance = sqrt(x**2 + y**2)
heappush(distance_list, distance)
if distance in list(point_distance.keys()):
point_distance[distance].append([x, y])
else:
point_distance[distance] = [[x, y]]
result = []
while K > 0:
tmp = point_distance[heappop(distance_list)]
K -= len(tmp)
result = result + tmp
return result | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR LIST VAR VAR ASSIGN VAR VAR LIST LIST VAR VAR ASSIGN VAR LIST WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
distanceList = []
for x, y in points:
distanceList.append((x, y, self.distanceFromOrigin(x, y)))
return [
[p[0], p[1]] for p in heapq.nsmallest(K, distanceList, key=lambda x: x[2])
]
def distanceFromOrigin(self, x, y):
return sqrt(pow(x - 0, 2) + pow(y - 0, 2)) | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN LIST VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.