Участник:Taranov srg/Longest Happy String

Материал из DISCOPAL
Перейти к: навигация, поиск

https://leetcode.com/problems/longest-happy-string

Python3

class Solution:
    def longestDiverseString(self, a: int, b: int, c: int) -> str:
        import numpy as np
 
        s = 'abc'
        numbers = np.array([a,b,c])
        place = np.argmax(numbers)
        string = s[place]*min(numbers[place],2)
        numbers[place] -= min(numbers[place],2) 
        while numbers[(place+1)%3] > 0 or numbers[(place+2)%3] > 0:
            if numbers[(place+1)%3] > numbers[(place+2)%3]:
                place = (place+1)%3
            else:
                place = (place+2)%3
            string += s[place]*min(numbers[place],2) if np.argmax(numbers) == place else s[place]
            numbers[place] -= min(numbers[place],2) if np.argmax(numbers) == place else 1
        return string