[Mock] Apple 2

shsh·2021년 4월 5일
0

Mock

목록 보기
24/93

937. Reorder Data in Log Files

You are given an array of logs. Each log is a space-delimited string of words, where the first word is the identifier.

There are two types of logs:

  • Letter-logs: All words (except the identifier) consist of lowercase English letters.
  • Digit-logs: All words (except the identifier) consist of digits.
    Reorder these logs so that:
  1. The letter-logs come before all digit-logs.
  2. The letter-logs are sorted lexicographically by their contents. If their contents are the same, then sort them lexicographically by their identifiers.
  3. The digit-logs maintain their relative ordering.

Return the final order of the logs.

Mt Answer 1: Accepted (Runtime: 40 ms - 34.94% / Memory Usage: 14.3 MB - 86.72%)

class Solution:
    def reorderLogFiles(self, logs: List[str]) -> List[str]:
        # 이거 easy 맞나요...?
        
        result = []	# 식별자 제외한 문자 값들만 저장
        nums = []	# 식별자 포함한 숫자 값들만 저장
        dic = {}	# [식별자: logs 값]
        
        for i in range(len(logs)):
            tmp = logs[i].split()
            # 문자로 구성됐으면 dic, result 에 넣어줌
            if tmp[1].isalpha() and tmp[0] not in dic:
                dic[tmp[0]] = [" ".join(tmp[1:])]
                result.append(" ".join(tmp[1:]))
            # 식별자가 중복되는 경우... 원래 있는 dic 값에 플러스
            elif tmp[1].isalpha() and tmp[0] in dic:
                dic[tmp[0]] += [" ".join(tmp[1:])]
                result.append(" ".join(tmp[1:]))
            # 숫자로 구성된 건 nums 에 넣어줌
            else:
                nums.append(logs[i])
        
        # 문자들 기준으로 sort
        result.sort()
        # 식별자 기준으로 sort
        sdic = sorted(dic.items())
        
        for key, val in sdic:
            for v in val:	# 식별자가 중복된 경우 여러개의 값이므로...
                for i in range(len(result)):
                    # result 값들의 key 찾아주기
                    if v == result[i]:
                        result[i] = key + " " + result[i]
                        break
        
        # 숫자는 정렬 필요없으니까 뒤에 붙여주기
        result += nums
        
        return result

정말.. 구질구질하게 풀었읍니다

애플아 너 왜 이딴 문제 내... 우리 좋았잖아...^^


22. Generate Parentheses

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

My Answer 1: Accpeted (Runtime: 36 ms - 60.65% / Memory Usage: 14.4 MB - 96.65%)

class Solution:
    def generateParenthesis(self, n: int) -> List[str]:
        
        def func(o, c, s):
            if c == n:
                self.result.add(s)
                return s
            
            if o == c or o < n:
                func(o+1, c, s + "(")
            
            if o > c and c < n:
                func(o, c+1, s + ")")
            
            return s
        
        self.result = set()
        func(0, 0, "")
        
        return self.result

o(pen) c(lose) 써서 조합 찾기

재귀는 아직도 어렵네요...ㅎ

profile
Hello, World!

0개의 댓글

Powered by GraphCDN, the GraphQL CDN