[Mock] Random 15



どちらも簡単~~

811. Subdomain Visit Count


A website domain like "discuss.leetcode.com"consists of various subdomains. At the top level, we have "com", at the next level, we have "leetcode.com", and at the lowest level, "discuss.leetcode.com". When we visit a domain like "discuss.leetcode.com", we will also visit the parent domains "leetcode.com"and "com"implicitly.
Now, call a "count-paired domain"to be a count (representing the number of visits this domain received), followed by a space, followed by the address. An example of a count-paired domain might be "9001 discuss.leetcode.com".
We are given a list cpdomains of count-paired domains. We would like a list of count-paired domains, (in the same format as the input, and in any order), that explicitly counts the number of visits to each subdomain.

My Answer 1: Accepted (Runtime: 80 ms - 5.53% / Memory Usage: 14.4 MB - 45.78%)

class Solution:
    def subdomainVisits(self, cpdomains: List[str]) -> List[str]:
        dic = {}
        
        for i in range(len(cpdomains)):
            tmp = cpdomains[i].split()
            time = int(tmp[0])
            if tmp[1] in dic:
                dic[tmp[1]] += time
            else:
                dic[tmp[1]] = time
                
            while "." in tmp[1]:
                idx = tmp[1].index(".")
                tmp[1] = tmp[1][idx+1:]
                
                if tmp[1] in dic:
                    dic[tmp[1]] += time
                else:
                    dic[tmp[1]] = time
        
        ans = []
        
        for key, val in dic.items():
            tmp = str(val) + " " + key
            ans.append(tmp)
        
        return ans
まずドメインを分割して時間とドメインを分離
time値はint、domainはdic(既に存在する場合はtimeのみ)
前の.による切り出しも1つのドメインであるため、도메인[idx+1:]で切り出し、同様にdicに更新するansにval値とキー値を付けて終了~
でも遅い...

Solution 1: Accepted (Runtime: 72 ms - 5.53% / Memory Usage: 14.4 MB - 45.78%)

class Solution:
    def subdomainVisits(self, cpdomains: List[str]) -> List[str]:
        ans = collections.Counter()
        for domain in cpdomains:
            count, domain = domain.split()
            count = int(count)
            frags = domain.split('.')
            for i in range(len(frags)):
                ans[".".join(frags[i:])] += count

        return ["{} {}".format(ct, dom) for dom, ct in ans.items()]
差が少ないdomain.split('.')以降".".join(frags[i:])でもいいです~"{} {}".format(ct, dom)=>count domain順に格納
解決策も遅い…^^

961. N-Repeated Element in Size 2N Array


In a array nums of size 2 * n, there are n + 1 unique elements, and exactly one of these elements is repeated n times.
Return the element repeated n times.

My Answer 1: Accepted (Runtime: 312 ms - 7.76% / Memory Usage: 15.7 MB - 33.80%)

class Solution:
    def repeatedNTimes(self, nums: List[int]) -> int:
        n = len(nums) // 2
        
        count = collections.Counter(nums)
        
        for key, time in count.items():
            if time == n:
                return key
私の愛カウンタを書いて、n+1個の値がそれぞれ何回現れるかを求めます.n回の値return keyが見つかりました
循環ドアはcount(값) == nで、だいぶ遅くなったこともあります.
ソリューションも似ていますが、nに比べて1より大きい場合は無条件に戻ります.

Solution 1: Accepted (Runtime: 228 ms - 17.58% / Memory Usage: 15.7 MB - 33.80%)

class Solution:
    def repeatedNTimes(self, nums: List[int]) -> int:
        for k in range(1, 4):
            for i in range(len(nums) - k):
                if nums[i] == nums[i+k]:
                    return nums[i]
4格前の隣人を見れば、繰り返されるかどうかが分かる.
もっと速く