[Mock] Adobe 3

shsh·2021년 7월 9일
0

Mock

목록 보기
80/93

709. To Lower Case

Given a string s, return the string after replacing every uppercase letter with the same lowercase letter.

My Answer 1: Accepted (Runtime: 32 ms - 44.29% / Memory Usage: 14.2 MB - 32.66%)

class Solution:
    def toLowerCase(self, s: str) -> str:
        return s.lower()

아스키로 계산하려 했는데 그거나 이거나..^^


1010. Pairs of Songs With Total Durations Divisible by 60

You are given a list of songs where the ith song has a duration of time[i] seconds.

Return the number of pairs of songs for which their total duration in seconds is divisible by 60. Formally, we want the number of indices i, j such that i < j with (time[i] + time[j]) % 60 == 0.

My Answer 1: Accepted (Runtime: 424 ms - 7.10% / Memory Usage: 17.7 MB - 75.14%)

class Solution:
    def numPairsDivisibleBy60(self, time: List[int]) -> int:
        ans = 0
        time.sort()
        pairs = collections.defaultdict(int)
        for i in range(len(time)):
            tmp = (time[i] // 60) * 60 + 60 - time[i]
            if time[i] in pairs:
                ans += pairs[time[i]]
            while tmp <= 500:
                pairs[tmp] += 1
                tmp += 60
                
        return ans

우선 time sort 해주고 pairs 라는 딕셔너리 만들기

times 값이 최대 500 이므로 적은편이라 생각해서
times[i] 의 가능한 짝꿍들을 모두 pairs 에 넣어주기

ex) times[i] = 30 이라면 60 - 30 = 30 부터 60 을 더한 값들이 짝꿍이 됨
(30, 90, 150, 210, ...) <= 500
ex) times[i] = 100 이라면 120 - 100 = 20 부터 시작~
(20, 80, 140, 200, ...) <= 500

첨엔 계속 timelimit 걸리는 방식만 생각나서 골 때렸네요..

Solution 1: Accepted (Runtime: 296 ms - 19.20% / Memory Usage: 17.8 MB - 66.45%)

class Solution:
    def numPairsDivisibleBy60(self, time: List[int]) -> int:
        remainders = collections.defaultdict(int)
        ret = 0
        for t in time:
            if t % 60 == 0: # check if a%60==0 && b%60==0
                ret += remainders[0]
            else: # check if a%60+b%60==60
                ret += remainders[60-t%60]
            remainders[t % 60] += 1 # remember to update the remainders
        return ret

나머지를 딕셔너리에 넣어주기

profile
Hello, World!

0개의 댓글