๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Algorithm/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ํฌ์ผ“๋ชฌ - Python

1. ๋ฌธ์ œ ์„ค๋ช…

  • ์ž…๋ ฅ nums : ํฌ์ผ“๋ชฌ ์ข…๋ฅ˜๋ฅผ ๋‹ด์€ ๋ฐฐ์—ด
  • ์ถœ๋ ฅ ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ๊ธธ์ด์˜ ์ ˆ๋ฐ˜๋งŒํผ ์ˆ˜๋ฅผ ์„ ํƒํ•  ์ˆ˜ ์žˆ๊ณ , ์ตœ๋Œ€ํ•œ ๋‹ค์–‘ํ•œ ์ข…๋ฅ˜์˜ ์ˆ˜๋ฅผ ์„ ํƒํ•ด์•ผํ•  ๋•Œ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€๊ฐ’์„ return
nums result
[3,1,2,3]  2
[3,3,3,2,2,4] 3
[3,3,3,2,2,2] 2

2. ์ฝ”๋“œ

def solution(nums):
    answer = 0
    max_num = len(nums)/2
    temp = [] #์ง€๊ธˆ๊นŒ์ง€ ๋‚˜์˜จ ํฌ์ผ“๋ชฌ ์ €์žฅ
    for i in range(len(nums)):
        if nums[i] not in temp:
            temp.append(nums[i])
        if len(temp) == max_num:
            break
    return len(temp)

3. ์–ด๋ ค์› ๊ฑฐ๋‚˜ ๋ฐฐ์šด ์ 

not in ํ™œ์šฉ

not in์„ ์“ฐ์ง€ ์•Š๋Š”๋‹ค๋ฉด ์ฝ”๋“œ๊ฐ€ ๊ธธ์–ด์งˆ ๊ฒƒ

ํŠน์ • ๊ฐ’์ด ๋ฆฌ์ŠคํŠธ์— ์กด์žฌํ•˜๋Š”์ง€ ๊ฒ€์‚ฌํ•  ๋•Œ, in ๋ฟ๋งŒ์•„๋‹ˆ๋ผ not in ๋„ ๊ธฐ์–ตํ•  ๊ฒƒ