Skip to content

Commit 030bdfa

Browse files
committed
Add 1471
1 parent b22df18 commit 030bdfa

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

Readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
| 1460 | Make Two Arrays Equal by Reversing Sub-arrays | Easy | O(N) | O(1) | Array | | |
6868
| 1464 | Maximum Product of Two Elements in an Array | Easy | O(N) | O(1) | Array | | |
6969
| 1470 | Shuffle the Array | Easy | O(N) | O(N) | Array | There is a better solution | |
70+
| 1471 | The k Strongest Values in an Array | Medium | O(NlogN) | O(N) | Array, Sort | There is a better solution | |
7071
| | | | | | | | |
7172

7273
For premium problems, question description are available as screenshots [here](./premium-questions)

inputs/1471

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[1,2,3,4,5]
2+
2
3+
[1,1,3,5,5]
4+
2
5+
[6,7,11,7,6,8]
6+
5
7+
[6,-3,7,2,11]
8+
3
9+
[-7,22,17,3]
10+
2
11+
[-46, 11, -85, 83, 22, 69, -35, 4, -39, 76, 94, -5, -91, 0, -80, 22, -22, -62, -59, -65, 7, -54, 88, -75, 73, 67, -74, -91, 9, 24, 32, -57, -43, -75, 54, 57, 10, 27, 96, -17, 26, 61, 36, 22, 85, -67, -55, 74, 80, -74, -22, -40, 91, -35, -1, -18, 59, 10, -35, -52, 29, 24, -41, 75, -15, 91, -12, -14, 17, -90, -43, -79, 80, -36, 91, 62, 94, 11, -69, -80, -36, 80, 0, -65, 52, 51, 54, 27, -31, -98, 91, -100, 54, -74, 0, -69, 91, -67, 94, 39, -68, -82, 7, 39, -71, -71, 10, -17, -44, 18, -2, 15, -12, 91, -58, 36, 29, 42, 35, -25, -3, 98, 87, 53, -100, 80, -9, 50, 79, -91, -26, -84, 63, 72, 88, 33, -21, 38, 56, -42, -19, -48, -9, -16, -28, 93, 78, -96, -20, -76, -45, 40, -72, 97, 18, 71, -89, -64, -96, 77, -1, 56, 71, -26, -36, 92, 85, -61, -97, -69, -25, 68, 29, -25, 67, -48, -17, -37, -67, 68, 61, -91, -13, -92, 48, 50, 64, 50, -30, 20, -65, -95, 11, 59, -46, -12, 66, 78, -36, -21, 79, 81, 3, -12, -67, -90, -51, 24, 44, 46, 77, -56, 18, 57, 96, 10, 98, 72, 86, 18, -50, -63, -61, 7, -81, 94, -23, 57, 50, -3, -47, 61, -75, -12, 12, -40, -91, -62, 98, -38, -2, 44, -6, -47, 16, -82, -52, 54, -40, 21, 66, -43, -69, -19, 20, -74, -92, -9, 45, 43, -4, 54, 63, 83, -64, -24, -25, -12, 64, -40, 53, -26, -75, 32, 73, 78, -20, -88, -89, 87, 82, -39, -39, 17, 75, 66, -92, -3, 49, 81, 14, 48, -68, 3, 53, 8, -1, 9, 77, 66]
12+
10
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#
2+
# @lc app=leetcode id=1471 lang=python3
3+
#
4+
# [1471] The k Strongest Values in an Array
5+
#
6+
7+
# @lc code=start
8+
# TAGS: Sort, Array
9+
class Solution:
10+
"""
11+
There is a better solution using Quick Select
12+
https://leetcode.com/problems/the-k-strongest-values-in-an-array/discuss/674346/Learn-randomized-algorithm-today-which-solves-this-problem-in-O(n).
13+
"""
14+
# 1116 ms, 86.47%. Custom Sort. O(NlogN). Space: O(1) best case, O(N) worst case due to sorting.
15+
def getStrongest(self, arr: List[int], k: int) -> List[int]:
16+
def custom_sort(a):
17+
diff_a = abs(a - median)
18+
return (diff_a, a)
19+
20+
arr.sort()
21+
median = arr[(len(arr) - 1) // 2]
22+
23+
arr.sort(key=custom_sort)
24+
return arr[-k:]
25+
26+
# @lc code=end
27+

0 commit comments

Comments
 (0)