Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Найти К максимальных элементов
- func kMostLargest(_ numbers: [Int], k: Int) {
- var max = numbers.max()!
- var indexMax = numbers.firstIndex(of: max)! as Int
- var result = [max]
- var i = 0
- var currentMax = -Int.max
- var currentMaxIndex = -1
- var sameMaxIsFound = false
- while i < numbers.count && result.count < k {
- while i < numbers.count {
- if numbers[i] > currentMax && numbers[i] < max {
- print(numbers[i])
- currentMax = numbers[i]
- currentMaxIndex = i
- }
- if numbers[i] == max && i > indexMax {
- currentMax = numbers[i]
- currentMaxIndex = i
- break
- }
- i += 1
- }
- result.append(currentMax)
- indexMax = currentMaxIndex
- max = currentMax
- i = 0
- currentMax = numbers[i]
- }
- print(result)
- }
- kMostLargest([10, 5, 8, 6, 8, 8, 8, 7, 7, 7, 7, 8, 9, 9], k: 5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement