본문 바로가기
공부/Algorithm

[HackerRank] Equal stacks

by 김샤랑 2022. 3. 8.

[HackerRank] Equal stacks


 

 

  • 문제 링크

https://www.hackerrank.com/challenges/equal-stacks/problem?isFullScreen=true 

 


시간복잡도: O(n)

 

  • 코드
def equalStacks(h1, h2, h3):

    sumH1 = sum(h1)
    sumH2 = sum(h2)
    sumH3 = sum(h3)
      
    while 1:

        minNum=min(sumH1, sumH2, sumH3)
        
        if(sumH1==sumH2==sumH3): #equal?
            break

        
        if(sumH1 > minNum): #sum > min?
            sumH1 -= h1.pop(0) 
            
        if(sumH2 > minNum):
            sumH2 -= h2.pop(0)
            
        if(sumH3 > minNum):
            sumH3 -= h3.pop(0)
 
    return minNum

 

 

'공부 > Algorithm' 카테고리의 다른 글

[LeetCode] Implement Trie  (0) 2022.03.21
[LeetCode] Two sum  (0) 2022.03.15
[HackerRank] Balanced Brackets  (0) 2022.03.11
[HackerRank] Jesse and Cookies  (0) 2022.03.09
[HackerRank] Merge two sorted linked lists  (0) 2022.03.07

댓글