Skip to content

Commit d69094c

Browse files
authored
Update reduce-array-size-to-the-half.py
1 parent a3b9754 commit d69094c

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

Python/reduce-array-size-to-the-half.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ def minSetSize(self, arr):
1616
counting_sort[c-1] += 1
1717
result, total = 0, 0
1818
for c in reversed(xrange(len(arr))):
19-
for _ in xrange(counting_sort[c]):
20-
result += 1
21-
total += c+1
22-
if total*2 >= len(arr):
23-
return result
19+
if not counting_sort[c]:
20+
continue
21+
count = min(counting_sort[c],
22+
((len(arr)+1)//2 - total - 1)//(c+1) + 1)
23+
result += count
24+
total += count*(c+1)
25+
if total >= (len(arr)+1)//2:
26+
break
2427
return result

0 commit comments

Comments
 (0)