인생은 고통의 연속

Rearrange Spaces Between Words 본문

프로그래밍/알고리즘

Rearrange Spaces Between Words

gnidoc 2021. 4. 26. 23:14
    반응형

    leetcode.com/problems/rearrange-spaces-between-words/

     

    Rearrange Spaces Between Words - LeetCode

    Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

    leetcode.com

    오늘의 문제

    난이도 easy 답게 한번에 성공 ㅎㅎ

     

    솔직히 영어만 보고 이해하고 풀었다기보단

    예제에 설명을 보고 이해했다... ㅋㅋ(영어 넘나 어려운 것)

    Input: text = "  this   is  a sentence "
    Output: "this   is   a   sentence"
    Explanation: There are a total of 9 spaces and 4 words. We can evenly divide the 9 spaces between the words: 9 / (4-1) = 3 spaces.

     

    난 아래처럼 좀 귀찮게 풀긴했는데

    class Solution:
        def reorderSpaces(self, text: str) -> str:
            space_count = 0
            for c in text:
                if c is ' ':
                    space_count = space_count + 1
                    
            words = list(filter(lambda x: x is not '', text.split(' ')))
            
            adjacent_space_count = len(words) - 1        
            if adjacent_space_count is 0:
                return words[0] + ' ' * space_count
            
            # print(f'{space_count} / {adjacent_space_count} / ', words)
            redistribution_space_count = space_count // adjacent_space_count
            extra_space_count = space_count % adjacent_space_count
            # print(f'{redistribution_space_count} / {extra_space_count}')
            redistribution_space = ' ' * redistribution_space_count
            return redistribution_space.join(words) + ' ' * extra_space_count
    

     

    어차피 for문은 1번이니깐 O(n)로 될거 같고

    공간복잡도는 뭐 게임최적화도 아니고... 그냥 변수는 넉넉하게 썼다

    이런 코드일수록 오히려 메모리 최적화보다는 명확한 변수명이 중요하다고 생각하기 때문이다

    이걸 만약에 한달 뒤에 고쳐야되는 상황이 왔는데 a, b, c, d 이렇게 되있으면 진짜 ㅋㅋㅋ

     

    근데 참 변수명 짓는게 참 어려웠는데...

    • 단어 사이의 공백의 숫자 = adjacent_space_count
    • 공백 재분배 후의 공백 묶음의 숫자 = redistribution_space_count
    • 재분배 후 남은 공백의 숫자 = extra_space_count

    일단 길어도 시스템상 문제없으면 길게 쓰는게 내 지론이기 때문에 무려 26자짜리 변수가 나왔다.... ㅋㅋㅋㅋㅋㅋㅋ

    요즘은 뭐 툴이 워낙 좋으니깐 알아서 자동완성해서 쓰겠지 뭐

    예전엔 나도 count = cnt, space = s 이런 식으로 줄여썼는데

    이게 뭔가 규칙적으로 줄여서 쓰면 모르겠지만 지금 회사에서 이런걸 당한 뒤로는 풀어서 쓰고 있다 ㅋㅋㅋㅋ

    • CSTDY_PD = custody period : 보관 기간
    • LDADNG_CYCLE = loading cycle : 적재 주기
    • THEMA_RELM = thema realm : 주제영역

    하... 영어 못하는건 나도 그러니 이해라도 가는데 CSTDY는 진짜 상상도 못했다

    이게 웃자고 이러는게 아니고 진짜 DB컬럼명을 저따구로 지어놨다

    실화냐

    보통 storage period 정도로 생각할거 같은데

    음... 잘모르겠다...

    그래 그럴 수 있어....

    그럴 수 있지...

    근데 회사에서 그러면 안되지???

    반응형

    '프로그래밍 > 알고리즘' 카테고리의 다른 글

    Statistics from a Large Sample  (0) 2022.01.17
    Merge Two Sorted Lists  (0) 2022.01.11
    two sum  (0) 2021.04.22
    11일차 리뷰  (0) 2019.02.13
    10일차 리뷰  (0) 2019.02.12
    Comments