sortByHeight codefights python đã xong, Cám ơn bạn lala
Code của mình:
[details=sortByHeight]
def sortByHeight(a):
position = []
for i in range(0, len(a)):
if a[i] == -1:
position.append(str(i))
num = a.count(-1)
for i in range(0, num):
a.remove(-1)
a.sort()
for i in position:
a.insert(int(i), -1)
return a[/details]
Mình gặp lỗi khi giải hidden test ở bài này areSimilar: https://codefights.com/arcade/intro/level-4/xYXfzQmnhBvEKJwXP
Two arrays are called similar if one can be obtained from another by swapping at most one pair of elements in one of the arrays.
Given two arrays a and b, check whether they are similar.
Example
For a = [1, 2, 3] and b = [1, 2, 3], the output should be
areSimilar(a, b) = true.
The arrays are equal, no need to swap any elements.
For a = [1, 2, 3] and b = [2, 1, 3], the output should be
areSimilar(a, b) = true.
We can obtain b from a by swapping 2 and 1 in b.
For a = [1, 2, 2] and b = [2, 1, 1], the output should be
areSimilar(a, b) = false.
Any swap of any two elements either in a or in b won’t make a and b equal.
Input/Output
[time limit] 4000ms (py)
[input] array.integer a
Array of integers.
Guaranteed constraints:
3 ≤ a.length ≤ 105,
1 ≤ a[i] ≤ 1000.
[input] array.integer b
Array of integers of the same length as a.
Guaranteed constraints:
b.length = a.length,
1 ≤ b[i] ≤ 1000.
[output] boolean
true if a and b are similar, false otherwise.
Code của mình: (giải được 14/20 test)
def areSimilar(a,b): for i in range(len(a)): for j in range(i,len(a)): equal = True tmp = a[i] a[i] = a[j] a[j] = tmp for k in range(len(a)): if a[k] != b[k]: equal = False break if equal: return True a[j] = a[i] a[i] = tmp return False
Xin mọi người giúp đỡ và đưa ra giải thuật tốt hơn