[Mock] Google 4

shsh·2021년 3월 14일
0

Mock

목록 보기
9/93

1051. Height Checker

Students are asked to stand in non-decreasing order of heights for an annual photo.

Return the minimum number of students that must move in order for all students to be standing in non-decreasing order of height.

Notice that when a group of students is selected they can reorder in any possible way between themselves and the non selected students remain on their seats.

My Answer 1: Accepted (Runtime: 36 ms - 68.91% / Memory Usage: 14.2 MB - 74.91%)

class Solution:
    def heightChecker(self, heights: List[int]) -> int:
        # 정렬을 하라는 것인가요
        target = sorted(heights)
        
        result = 0
        
        for i in range(len(heights)):
            if heights[i] == target[i]:
                continue
            else:
                result += 1
        
        return result

if heights[i] != target[i]: 경우에만 result + 1


925. Long Pressed Name

Your friend is typing his name into a keyboard. Sometimes, when typing a character c, the key might get long pressed, and the character will be typed 1 or more times.

You examine the typed characters of the keyboard. Return True if it is possible that it was your friends name, with some characters (possibly none) being long pressed.

My Answer 1: Accepted (Runtime: 20 ms - 99.47% / Memory Usage: 14.1 MB - 86.32%)

class Solution:
    def isLongPressedName(self, name: str, typed: str) -> bool:
        # 가망 X
        if len(name) > len(typed):
            return False
        
        last = name[0]
        j = 0           # name index
        
        for i in range(len(typed)):
            if typed[i] == name[j]:
                # last 한테 지금의 문자를 넘겨주고 name 은 한칸 더 앞서 가있기
                # name = "leelee", typed = "lleeelee" 의 "ee" 같은 중복 문자를 한번에 확인하는거나 마찬가지
                last = name[j]
                if j < len(name) - 1:
                    j += 1
            
            # 그 다음부터는 last 랑 같은지만 계속 확인
            elif last == typed[i]:
                continue
                
            # name[j] 이랑도 다르고 last 랑도 다르면 완전 틀린 거니까 False
            else:
                return False
        
        # last 가 name 의 끝까지 안갔을 때
        if last != name[-1] or j != len(name) - 1:
            return False
        
        return True

이거 설명하기가 좀 어려운데...

첨엔 내사랑 collections.Counter 쓰려다가 오바인거 같아서 바꿨읍니다

profile
Hello, World!

0개의 댓글

관련 채용 정보

Powered by GraphCDN, the GraphQL CDN