[Mock] Adobe 3


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に入れます.times[i] = 30であれば、60 - 30 = 3060を合わせた値が同じテーブルです.(30, 90, 150, 210, ...) <= 500 times[i] = 100なら120 - 100 = 20から~(20, 80, 140, 200, ...) <= 500最初は時間制限の仕方を考えていたので、頭が痛いです.

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
残りをディック郡に入れる