Dynamic Programming Dynamic Programming Heap Medium String

Longest Diverse Str

~2 mins read

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import heapq
from heapq import heappush, heappop, heapify

def longestDiverseString(a: int, b: int, c: int) -> str:
    
    ans = 0
    
    # get 2 from max, 1 from min 
    
    minh = [a,b,c]
    maxh = [a,b,c]

    heapify(minh)
    heapq._heapify_max(maxh)
    
    
    maxnum = heappop(maxh)
    minnum = heappop(minh)


    while maxnum or minnum:
        if maxnum:
            print("maxh",maxh)

            if maxnum > 1:
                heappush(maxh, maxnum-2)
                ans += 2
            

            else:
                ans += 1
            
            if maxh:
                maxnum = heappop(maxh)
                
        if minnum:
            print("minh",minh)
            if minnum > 0:
                ans += 1
                heappush(minh, minnum-1)

                minnum = heappop(minh)

    print(ans)
    return ans 


longestDiverseString(7,1,1)

🎰