擅长:python、mysql、java
<p>首先,对列表进行排序,但要跟踪原始索引。在下面的解决方案中,我使用元组</p>
<p>然后,在排序列表上向后移动,如果该值不是<strong>valueToIgnore</strong>,则将该标记附加到<em>res</em>,直到<em>res</em>的长度为<strong>n</strong></p>
<pre><code>n = 10
valueToIgnore = 100
array = [ 10, 10, 11, 11, 10, 10, 12, 12, 10, 10, 10, 13, 14,
14, 15, 100, 15, 12, 13, 11, 10, 12, 14, 14, 100, 100,
100, 12, 13, 10, 10, 11, 13, 100, 100, 13, 14, 13, 12,
10, 10, 11, 10, 100, 100, 100, 12, 13, 12, 13, 10, 10,
10, 15, 100, 14, 14, 11, 12, 12, 10, 10, 10, 15, 15,
14, 10, 10, 10, 11, 10, 10, 10, 12, 11, 11, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10]
array = [(i, val) for i,val in enumerate(array)]
array.sort(key= lambda x: x[1])
res = []
for i in range(len(array)-1, -1, -1):
if len(res) == n: break
if array[i][1] != valueToIgnore:
res.append(array[i][0])
print(sorted(res))
# This will print [14, 16, 23, 36, 53, 55, 56, 63, 64, 65]
</code></pre>