๋‚ด ์ธ์ƒ์—์„œ ๋ฏฟ์„ ๊ฑด ์˜ค์ง ๋‚˜ ์ž์‹ ๋ฟ!

The only one you can truly trust is yourself.

๊ฒŒ์ž„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ/Python ํ”„๋กœ๊ทธ๋ž˜๋ฐ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ˆซ์ž ์นด๋“œ ๋‚˜๋ˆ„๊ธฐ

๐ŸŽฎinspirer9 2023. 2. 15. 20:09
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

๋ฌธ์ œ ์„ค๋ช…

์ฒ ์ˆ˜์™€ ์˜ํฌ๋Š” ์„ ์ƒ๋‹˜์œผ๋กœ๋ถ€ํ„ฐ ์ˆซ์ž๊ฐ€ ํ•˜๋‚˜์”ฉ ์ ํžŒ ์นด๋“œ๋“ค์„ ์ ˆ๋ฐ˜์”ฉ ๋‚˜๋ˆ ์„œ ๊ฐ€์ง„ ํ›„, ๋‹ค์Œ ๋‘ ์กฐ๊ฑด ์ค‘ ํ•˜๋‚˜๋ฅผ ๋งŒ์กฑํ•˜๋Š” ๊ฐ€์žฅ ํฐ ์–‘์˜ ์ •์ˆ˜ a์˜ ๊ฐ’์„ ๊ตฌํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
  1. ์ฒ ์ˆ˜๊ฐ€ ๊ฐ€์ง„ ์นด๋“œ๋“ค์— ์ ํžŒ ๋ชจ๋“  ์ˆซ์ž๋ฅผ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๊ณ  ์˜ํฌ๊ฐ€ ๊ฐ€์ง„ ์นด๋“œ๋“ค์— ์ ํžŒ ๋ชจ๋“  ์ˆซ์ž๋“ค ์ค‘ ํ•˜๋‚˜๋„ ๋‚˜๋ˆŒ ์ˆ˜ ์—†๋Š” ์–‘์˜ ์ •์ˆ˜ a
  2. ์˜ํฌ๊ฐ€ ๊ฐ€์ง„ ์นด๋“œ๋“ค์— ์ ํžŒ ๋ชจ๋“  ์ˆซ์ž๋ฅผ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๊ณ , ์ฒ ์ˆ˜๊ฐ€ ๊ฐ€์ง„ ์นด๋“œ๋“ค์— ์ ํžŒ ๋ชจ๋“  ์ˆซ์ž๋“ค ์ค‘ ํ•˜๋‚˜๋„ ๋‚˜๋ˆŒ ์ˆ˜ ์—†๋Š” ์–‘์˜ ์ •์ˆ˜ a

์˜ˆ๋ฅผ ๋“ค์–ด, ์นด๋“œ๋“ค์— 10, 5, 20, 17์ด ์ ํ˜€ ์žˆ๋Š” ๊ฒฝ์šฐ์— ๋Œ€ํ•ด ์ƒ๊ฐํ•ด ๋ด…์‹œ๋‹ค. ๋งŒ์•ฝ, ์ฒ ์ˆ˜๊ฐ€ [10, 17]์ด ์ ํžŒ ์นด๋“œ๋ฅผ ๊ฐ–๊ณ , ์˜ํฌ๊ฐ€ [5, 20]์ด ์ ํžŒ ์นด๋“œ๋ฅผ ๊ฐ–๋Š”๋‹ค๋ฉด ๋‘ ์กฐ๊ฑด ์ค‘ ํ•˜๋‚˜๋ฅผ ๋งŒ์กฑํ•˜๋Š” ์–‘์˜ ์ •์ˆ˜ a๋Š” ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ์ฒ ์ˆ˜๊ฐ€ [10, 20]์ด ์ ํžŒ ์นด๋“œ๋ฅผ ๊ฐ–๊ณ , ์˜ํฌ๊ฐ€ [5, 17]์ด ์ ํžŒ ์นด๋“œ๋ฅผ ๊ฐ–๋Š”๋‹ค๋ฉด, ์ฒ ์ˆ˜๊ฐ€ ๊ฐ€์ง„ ์นด๋“œ๋“ค์˜ ์ˆซ์ž๋Š” ๋ชจ๋‘ 10์œผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๊ณ , ์˜ํฌ๊ฐ€ ๊ฐ€์ง„ ์นด๋“œ๋“ค์˜ ์ˆซ์ž๋Š” ๋ชจ๋‘ 10์œผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ฒ ์ˆ˜์™€ ์˜ํฌ๋Š” ๊ฐ๊ฐ [10, 20]์ด ์ ํžŒ ์นด๋“œ, [5, 17]์ด ์ ํžŒ ์นด๋“œ๋กœ ๋‚˜๋ˆ  ๊ฐ€์กŒ๋‹ค๋ฉด ์กฐ๊ฑด์— ํ•ด๋‹นํ•˜๋Š” ์–‘์˜ ์ •์ˆ˜ a๋Š” 10์ด ๋ฉ๋‹ˆ๋‹ค.

์ฒ ์ˆ˜๊ฐ€ ๊ฐ€์ง„ ์นด๋“œ์— ์ ํžŒ ์ˆซ์ž๋“ค์„ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜ ๋ฐฐ์—ด arrayA์™€ ์˜ํฌ๊ฐ€ ๊ฐ€์ง„ ์นด๋“œ์— ์ ํžŒ ์ˆซ์ž๋“ค์„ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜ ๋ฐฐ์—ด arrayB๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ฃผ์–ด์ง„ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ฐ€์žฅ ํฐ ์–‘์˜ ์ •์ˆ˜ a๋ฅผ returnํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด ์ฃผ์„ธ์š”. ๋งŒ์•ฝ, ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” a๊ฐ€ ์—†๋‹ค๋ฉด, 0์„ return ํ•ด ์ฃผ์„ธ์š”.

 

์ œํ•œ์‚ฌํ•ญ

  • 1 ≤ arrayA์˜ ๊ธธ์ด = arrayB์˜ ๊ธธ์ด ≤ 500,000
  • 1 ≤ arrayA์˜ ์›์†Œ, arrayB์˜ ์›์†Œ ≤ 100,000,000
  • arrayA์™€ arrayB์—๋Š” ์ค‘๋ณต๋œ ์›์†Œ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

arrayA arrayB result
[10, 17] [5, 20] 0
[10, 20] [5, 17] 10
[14, 35, 119] [18, 30, 102] 7

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ #1

  • ๋ฌธ์ œ ์˜ˆ์‹œ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #2

  • ๋ฌธ์ œ ์˜ˆ์‹œ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #3

  • ์ฒ ์ˆ˜๊ฐ€ ๊ฐ€์ง„ ์นด๋“œ์— ์ ํžŒ ์ˆซ์ž๋“ค์€ ๋ชจ๋‘ 3์œผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์—†๊ณ , ์˜ํฌ๊ฐ€ ๊ฐ€์ง„ ์นด๋“œ์— ์ ํžŒ ์ˆซ์ž๋Š” ๋ชจ๋‘ 3์œผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 3์€ ์กฐ๊ฑด์— ํ•ด๋‹นํ•˜๋Š” ์–‘์˜ ์ •์ˆ˜์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ์ฒ ์ˆ˜๊ฐ€ ๊ฐ€์ง„ ์นด๋“œ๋“ค์— ์ ํžŒ ์ˆซ์ž๋“ค์€ ๋ชจ๋‘ 7๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๊ณ , ์˜ํฌ๊ฐ€ ๊ฐ€์ง„ ์นด๋“œ๋“ค์— ์ ํžŒ ์ˆซ์ž๋Š” ๋ชจ๋‘ 7๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ตœ๋Œ€๊ฐ’์ธ 7์„ return ํ•ฉ๋‹ˆ๋‹ค.

๋ฌธ์ œํ’€์ด

  • ๋ฌด์ง€์„ฑ ์ฝ”๋”ฉ
def solution(arrayA, arrayB):
    arrayA = list(set(arrayA))
    arrayB = list(set(arrayB))
    arrayA.sort()
    arrayB.sort()
    arrayAlen = len(arrayA)
    arrayBlen = len(arrayB)
    divisorsA = set()
    divisorsB = set()
    
    n = max(arrayA[0]+1, int(arrayA[-1]**0.5)+1)
    for i in range(2, n):
        is_break = False
        for j in arrayA:
            if j % i != 0:
                is_break = True
                break
        if is_break == False:
            divisorsA.add(i)

    n = max(arrayB[0]+1, int(arrayB[-1]**0.5)+1)
    for i in range(2, n):
        is_break = False
        for j in arrayB:
            if j % i != 0:
                is_break = True
                break
        if is_break == False:
            divisorsB.add(i)
    
    divisorsA = list(divisorsA)
    divisorsB = list(divisorsB)
    divisorsA.sort(reverse=True)
    divisorsB.sort(reverse=True)
    
    print(divisorsA, divisorsB)
    
    if len(divisorsA) == 0 and len(divisorsB) == 0:
        return 0
    elif len(divisorsA) == 0:
        # divisorsB์— ์žˆ๋Š” ๊ฑธ๋กœ arrayA ๋‚˜๋ˆ ๋ณด๊ธฐ
        for i in divisorsB:
            is_break = False
            for j in arrayA:
                print(j,i)
                if j % i == 0:
                    is_break = True
                    break
            if is_break == False:
                return i
        return 0
    elif len(divisorsB) == 0:
        # divisorsA์— ์žˆ๋Š” ๊ฑธ๋กœ arrayB ๋‚˜๋ˆ ๋ณด๊ธฐ
        for i in divisorsA:
            is_break = False
            for j in arrayB:
                print(j,i)
                if j % i == 0:
                    is_break = True
                    break
            if is_break == False:
                return i
    else:
        A_used = True
        B_used = True
        a = 0
        b = 0
        _t = 0
        while len(divisorsA) > 0 or len(divisorsB) > 0:
            if len(divisorsA) != 0 and A_used == True:
                A_used = False
                a = divisorsA.pop(0)
            if len(divisorsB) != 0 and B_used == True:
                B_used = False
                b = divisorsB.pop(0)
            
            if a == b:
                A_used = True
                B_used = True
                b = 0
                a = 0
            elif a > b:
                A_used = True
                is_break = False
                for j in arrayB:
                    if j % a == 0:
                        is_break = True
                        break
                if is_break == False:
                    return a
                a = 0
            else:
                B_used = True
                is_break = False
                for j in arrayA:
                    if j % b == 0:
                        is_break = True
                        break
                if is_break == False:
                    return b
                b = 0
        return 0
    
    return 0
  • ๋‹น์—ฐํ•˜์ง€๋งŒ ์ฝ”๋“œ๊ฐ€ ๊ธธ๋ฉด ํŒŒ์ด์ฌ์€ ์‹œ๊ฐ„ ์ดˆ๊ณผ๋จ
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (250.26ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (222.42ms, 10.4MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (284.40ms, 10.4MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (210.04ms, 13.3MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (203.27ms, 11.3MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (205.46ms, 11.8MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (203.34ms, 10.4MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (212.16ms, 10.4MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (297.72ms, 11.2MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (217.82ms, 10.5MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
ํ…Œ์ŠคํŠธ 12 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
ํ…Œ์ŠคํŠธ 13 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
ํ…Œ์ŠคํŠธ 14 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
ํ…Œ์ŠคํŠธ 15 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
ํ…Œ์ŠคํŠธ 16 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
ํ…Œ์ŠคํŠธ 17 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
ํ…Œ์ŠคํŠธ 18 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.4MB)
ํ…Œ์ŠคํŠธ 21 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 22 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.3MB)
ํ…Œ์ŠคํŠธ 23 ใ€‰	ํ†ต๊ณผ (4.82ms, 10.2MB)
ํ…Œ์ŠคํŠธ 24 ใ€‰	ํ†ต๊ณผ (323.12ms, 10.3MB)
ํ…Œ์ŠคํŠธ 25 ใ€‰	ํ†ต๊ณผ (9741.68ms, 10.3MB)
ํ…Œ์ŠคํŠธ 26 ใ€‰	ํ†ต๊ณผ (5803.71ms, 10.5MB)
ํ…Œ์ŠคํŠธ 27 ใ€‰	ํ†ต๊ณผ (0.35ms, 10.3MB)
ํ…Œ์ŠคํŠธ 28 ใ€‰	ํ†ต๊ณผ (0.58ms, 10.5MB)
ํ…Œ์ŠคํŠธ 29 ใ€‰	ํ†ต๊ณผ (0.60ms, 10.3MB)
ํ…Œ์ŠคํŠธ 30 ใ€‰	ํ†ต๊ณผ (0.65ms, 10.3MB)
ํ…Œ์ŠคํŠธ 31 ใ€‰	ํ†ต๊ณผ (0.91ms, 10.5MB)
ํ…Œ์ŠคํŠธ 32 ใ€‰	ํ†ต๊ณผ (1.17ms, 10.4MB)
ํ…Œ์ŠคํŠธ 33 ใ€‰	ํ†ต๊ณผ (177.98ms, 10.3MB)
ํ…Œ์ŠคํŠธ 34 ใ€‰	ํ†ต๊ณผ (0.15ms, 10.3MB)
ํ…Œ์ŠคํŠธ 35 ใ€‰	ํ†ต๊ณผ (0.09ms, 10.4MB)
ํ…Œ์ŠคํŠธ 36 ใ€‰	ํ†ต๊ณผ (0.15ms, 10.4MB)
  • ๋‚˜์˜ ๋จธ๋ฆฌ๊ฐ€ ๋ฉ์ฒญ์ด์˜€๋‹ค. ํœด๋จผ.
  • ๋ฌธ์ œ ์ž์ฒด๊ฐ€ ํ•จ์ •์ด์—ˆ๋‹ค. ํœด๋จผ.
    • ๋ฆฌ์ŠคํŠธ์—์„œ ์ตœ๋Œ€ ๊ณต์•ฝ ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ณ , ๊ทธ๊ฑธ๋กœ ์ƒ๋Œ€ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋‚˜๋ˆ ์„œ 0์ด ๋‚˜์˜ค๋ฉด ๋” ๋ณผ ํ•„์š”๊ฐ€ ์—†์ด ๋๋‚œ๊ฑฐ๋‹ค.
    • ์•ž์— ์ง  ์†Œ์Šค๋Š” ๋ชจ๋“  ์•ฝ์ˆ˜๋ฅผ ๋‹ค ๊ณ„์‚ฐํ•˜๊ณ  ์žˆ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚ฌ๋˜ ๊ฒƒ...
import math

def solution(arrayA, arrayB):
    arrayA = list(set(arrayA))
    len_arrayA = len(arrayA)
    gcdA = arrayA[0]
    if len_arrayA != 1:
        for i in range(1,len_arrayA):
            gcdA = math.gcd(gcdA, arrayA[i])
            
    arrayB = list(set(arrayB))
    len_arrayB = len(arrayB)
    gcdB = arrayB[0]
    if len_arrayB != 1:
        for i in range(1,len_arrayB):
            gcdB = math.gcd(gcdB, arrayB[i])
    
    for i in arrayA:
        if i % gcdB == 0:
            gcdB = 1    
            break
    
    for i in arrayB:
        if i % gcdA == 0:
            gcdA = 1
            break
    
    if gcdA == 1:
        if gcdB == 1:
            return 0
        else:
            return gcdB
    else:
        if gcdB == 1:
            return gcdA
        else:
            return max(gcdA, gcdB)
            
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.29ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (1.18ms, 10.5MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.16ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (7.02ms, 13.4MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (2.84ms, 11MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (4.73ms, 11.7MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.69ms, 10.6MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.38ms, 10.5MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (2.87ms, 11.2MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.16ms, 10.2MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (228.88ms, 82.9MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (271.21ms, 82.8MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (225.80ms, 82.8MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (216.91ms, 82.8MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (220.48ms, 82.8MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (215.80ms, 82.9MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (189.72ms, 83MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (222.93ms, 82.9MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.2MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.4MB)
ํ…Œ์ŠคํŠธ 21 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 22 ใ€‰	ํ†ต๊ณผ (0.01ms, 10MB)
ํ…Œ์ŠคํŠธ 23 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 24 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.4MB)
ํ…Œ์ŠคํŠธ 25 ใ€‰	ํ†ต๊ณผ (0.01ms, 10MB)
ํ…Œ์ŠคํŠธ 26 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 27 ใ€‰	ํ†ต๊ณผ (0.07ms, 10.4MB)
ํ…Œ์ŠคํŠธ 28 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.1MB)
ํ…Œ์ŠคํŠธ 29 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.4MB)
ํ…Œ์ŠคํŠธ 30 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 31 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 32 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 33 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 34 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 35 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 36 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
  • ๊ทธ๋ฆฌ๊ณ  ๋ฐฐ์—ด ๊ธธ์ด๋„ ๊ฐ™๋„ค?
  • ์ฝ”๋“œ ๋” ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.
import math
from functools import reduce

def solution(arrayA, arrayB):
    len_arrayA = len(arrayA)
    gcdA = arrayA[0]
    gcdB = arrayB[0]
    for i in range(1,len_arrayA):
        gcdA = math.gcd(gcdA, arrayA[i])
        gcdB = math.gcd(gcdB, arrayB[i])
    
    for i in arrayA:
        if i % gcdB == 0:
            gcdB = 1    
            break
    
    for i in arrayB:
        if i % gcdA == 0:
            gcdA = 1
            break
    
    if gcdA == 1:
        if gcdB == 1:
            return 0
        else:
            return gcdB
    else:
        if gcdB == 1:
            return gcdA
        else:
            return max(gcdA, gcdB)
            
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.21ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.87ms, 10.2MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.14ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (10.44ms, 12.9MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (3.44ms, 10.4MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (4.58ms, 11.1MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.39ms, 10.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.56ms, 10.2MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (1.84ms, 10.6MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.21ms, 10.2MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (143.83ms, 53.3MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (174.65ms, 53.2MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (183.44ms, 53.2MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (217.14ms, 53.2MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (150.58ms, 53.2MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (144.12ms, 53.2MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (212.05ms, 53.1MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (139.89ms, 53.5MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.2MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 21 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 22 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 23 ใ€‰	ํ†ต๊ณผ (0.01ms, 10MB)
ํ…Œ์ŠคํŠธ 24 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 25 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 26 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 27 ใ€‰	ํ†ต๊ณผ (0.03ms, 10MB)
ํ…Œ์ŠคํŠธ 28 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.2MB)
ํ…Œ์ŠคํŠธ 29 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.2MB)
ํ…Œ์ŠคํŠธ 30 ใ€‰	ํ†ต๊ณผ (0.00ms, 10MB)
ํ…Œ์ŠคํŠธ 31 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 32 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.2MB)
ํ…Œ์ŠคํŠธ 33 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.4MB)
ํ…Œ์ŠคํŠธ 34 ใ€‰	ํ†ต๊ณผ (0.01ms, 9.97MB)
ํ…Œ์ŠคํŠธ 35 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 36 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
  • ์ฝ”๋“œ ์ค„์ด๊ธฐ
import math
from functools import reduce

def solution(arrayA, arrayB):
    len_arrayA = len(arrayA)
    gcdA = arrayA[0]
    gcdB = arrayB[0]
    
    for i in range(1,len_arrayA):
        gcdA = math.gcd(gcdA, arrayA[i])
        gcdB = math.gcd(gcdB, arrayB[i])
    
    for i in reversed(arrayA):
        if i % gcdB == 0:
            gcdB = 1    
            break
    
    for i in reversed(arrayB):
        if i % gcdA == 0:
            gcdA = 1
            break
    
    if gcdA == gcdB:
        return 0
    else:
        return max(gcdA, gcdB)
        
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.37ms, 10.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (1.54ms, 10.1MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.08ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (10.02ms, 12.8MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (2.36ms, 10.2MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (5.05ms, 11MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.35ms, 10.1MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.32ms, 10.2MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (3.44ms, 10.5MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.11ms, 10.1MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (147.29ms, 53.2MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (148.26ms, 53.4MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (159.85ms, 53.3MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (174.96ms, 53.4MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (169.69ms, 53.2MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (171.18ms, 53.2MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (136.89ms, 53.4MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (168.21ms, 53.3MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.1MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 21 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 22 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.1MB)
ํ…Œ์ŠคํŠธ 23 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 24 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 25 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 26 ใ€‰	ํ†ต๊ณผ (0.01ms, 10MB)
ํ…Œ์ŠคํŠธ 27 ใ€‰	ํ†ต๊ณผ (0.06ms, 10MB)
ํ…Œ์ŠคํŠธ 28 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.1MB)
ํ…Œ์ŠคํŠธ 29 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 30 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 31 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.2MB)
ํ…Œ์ŠคํŠธ 32 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 33 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 34 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 35 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 36 ใ€‰	ํ†ต๊ณผ (0.01ms, 10MB)

๋‹ค ํ’€๊ณ ๋‚˜๋ฉด ์‰ฌ์šด๋ฐ, ํ’€๊ธฐ์ „๊นŒ์ง€๋Š” ์ฐธ ์–ด๋ ต๋„ค... 400์˜ ๋ฒฝ์„ ๋šซ๊ธฐ๊ฐ€ ์ด๋ ‡๊ฒŒ ํž˜๋“œ๋‚˜?

์•„์ฐธ ๊ณ ์ˆ˜์˜ ํ’€์ด ๋ฆฌ๋“€์Šค๋ฅผ ์ด๋ ‡๊ฒŒ ์“ฐ๋Š” ๊ฑฐ์˜€๊ตฌ๋‚˜. reduce ์จ๋ณด๋ ค๋‹ค๊ฐ€ ๊ณต๋ถ€๊ฐ€ ๋œ๋˜์„œ ํฌ๊ธฐํ–ˆ๋Š”๋ฐ ใ…Žใ…Žใ…Ž

from math import gcd
from functools import reduce

def check(arrayA, arrayB):
    gcd_A = reduce(gcd, arrayA, arrayA[0])
    factors = [i for i in range(1, gcd_A//2+1) if not gcd_A % i]
    factors.append(gcd_A)
    for factor in factors[::-1]:
        if all(i % factor for i in arrayB):
            return gcd_A
    return 0

def solution(arrayA, arrayB):
    return max(check(arrayA, arrayB), check(arrayB, arrayA))
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.42ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.76ms, 10.3MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.59ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (8.17ms, 12.9MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (2.45ms, 10.5MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (5.01ms, 11.2MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.50ms, 10.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (1.00ms, 10.3MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (2.36ms, 10.6MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.53ms, 10.2MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (95.66ms, 53.4MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (117.54ms, 53.5MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (84.06ms, 53.4MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (89.90ms, 53.4MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (110.30ms, 53.5MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (89.00ms, 53.5MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (82.23ms, 53.5MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (141.29ms, 53.3MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 21 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 22 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 23 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 24 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.3MB)
ํ…Œ์ŠคํŠธ 25 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.4MB)
ํ…Œ์ŠคํŠธ 26 ใ€‰	ํ†ต๊ณผ (0.20ms, 9.96MB)
ํ…Œ์ŠคํŠธ 27 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.4MB)
ํ…Œ์ŠคํŠธ 28 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.2MB)
ํ…Œ์ŠคํŠธ 29 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 30 ใ€‰	ํ†ต๊ณผ (0.41ms, 10.3MB)
ํ…Œ์ŠคํŠธ 31 ใ€‰	ํ†ต๊ณผ (0.39ms, 10.2MB)
ํ…Œ์ŠคํŠธ 32 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.4MB)
ํ…Œ์ŠคํŠธ 33 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 34 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.4MB)
ํ…Œ์ŠคํŠธ 35 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.3MB)
ํ…Œ์ŠคํŠธ 36 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)

 

728x90
๋ฐ˜์‘ํ˜•