Claire's Study Note

[Python ์ฝ”ํ…Œ ์ž…๋ฌธ] 3. (๋ฌธ์ œ 1๋ฒˆ) ๋นˆ๋„์ˆ˜(ver 1)

by Hi.Claire
๋ฐ˜์‘ํ˜•

๐Ÿ–ฅ๏ธ ์ž…๋ฌธ์ž๋ฅผ ์œ„ํ•œ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ํ•ต์‹ฌ - Python (๊น€ํƒœ์›, ์ธํ”„๋Ÿฐ)

 

์„น์…˜3. ํ•ด์‹œ(Hash)

 

(๋ฌธ์ œ 1๋ฒˆ) ๋นˆ๋„์ˆ˜(ver 1)

๋งค๊ฐœ๋ณ€์ˆ˜ nums์— ๊ธธ์ด๊ฐ€ n์ธ ์ˆ˜์—ด์ด ์ฃผ์–ด์ง€๋ฉด ์ˆ˜์—ด์˜ ์›์†Œ์ค‘์—์„œ ๋นˆ๋„์ˆ˜๊ฐ€ 1์ธ ๊ฐ€์žฅ ํฐ ์ˆซ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

๋นˆ๋„์ˆ˜ 1์ธ ์ˆซ์ž๊ฐ€ ์—†์„ ๊ฒฝ์šฐ -1์„ ๋ฐ˜ํ™˜ํ•˜์„ธ์š”.

 

์ž…์ถœ๋ ฅ ์˜ˆ

nums answer
[3, 9, 2, 12, 9, 12, 8, 7, 9, 12] 8
[2, 1, 3, 2, 1, 3, 4, 5, 4, 5, 6, 7, 6, 7, 8, 8] -1
[23, 56, 11, 67, 120, 560, 812, 960, 560, 777, 888, 960] 888
[11, 73, 156, 789, 345, 156, 789, 345, 678, 555, 678] 555
[1, 3, 1, 5, 7, 2, 3, 1, 5] 7

 

์ œํ•œ์‚ฌํ•ญ

  • nums์˜ ๊ธธ์ด : 3 <= n <= 10,000
  • ๋ฐฐ์—ด nums์˜ ์›์†Œ๋Š” ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค. 1 <= nums[i] <= 1,000

 

ํ’€์ด

def solution(nums):
  answer = -1
  dict1 = {}
  dict2 = []
  for num in nums:
    if num in dict1:
      dict1[num] += 1
    else:
      dict1[num] = 1
  for key, val in dict1.items():
    if val == 1:
      dict2.append(key)
  if len(dict2) == 0:
    return answer;
  else:
    return max(dict2);

print(solution([3, 9, 2, 12, 9, 12, 8, 7, 9, 12]));
print(solution([2, 1, 3, 2, 1, 3, 4, 5, 4, 5, 6, 7, 6 ,7, 8, 8]));
print(solution([23, 56, 11, 67, 120, 560, 812, 960, 560, 777, 888, 960]));
print(solution([11, 73, 156, 789, 345, 156, 789, 345, 678, 555, 678]));
print(solution([1, 3, 1, 5, 7, 2, 3, 1, 5]));

8

-1

888

555

7

 

์ •๋‹ต ํ’€์ด

๋ถ„์„

๋‘๋ฒˆ์งธ ์ œํ•œ์‚ฌํ•ญ์„ ๋ณด๋ฉด ๋ฐฐ์—ด nums์˜ ์›์†Œ์˜ ์ตœ๋Œ€๊ฐ’์€ 1000์ด๋‹ค.

์ด๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” direct address table ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด hash table์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ์†๋„๊ฐ€ ๋น ๋ฅผ ๊ฒƒ์ด๋ผ๊ณ  ํŒ๋‹จํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฐฐ์—ด์˜ ์ตœ๋Œ€ ๊ธธ์ด๊ฐ€ 1000๋ฐ–์— ์•ˆ ๋˜๋ฏ€๋กœ ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๋ฅผ ํ‚ค๋กœ ์‚ฌ์šฉํ•˜์—ฌ ๋นˆ๋„์ˆ˜๋ฅผ ์ฐพ๋Š” ๋ฐฉ์‹(๋ฐฐ์—ด์˜ ์ธ๋ฑ์‹ฑ์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(1)์ด๋‹ค.)์€ ๊ฐ„๋‹จํ•˜๋‹ค.

๋ฐ˜๋ฉดhash table(ํŒŒ์ด์ฌ์˜ dictionary)์€ hash ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ hash ๊ฐ’์— key, value(๋นˆ๋„์ˆ˜)๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐฉ์‹์ด๋ฏ€๋กœ ํ•ด๋‹น ์ˆซ์ž์˜ ๋นˆ๋„์ˆ˜๋ฅผ ์ฐพ์•„๊ฐ€๋Š” ๋ฐฉ์‹์ด ๋ฐฐ์—ด์˜ indexing์— ๋น„ํ•ด ์ข€ ๋” ๋ณต์žกํ•˜๋‹ค.

๋”ฐ๋ผ์„œ ์ด ๋ฌธ์ œ๋Š” direct address table ๋ฐฉ์‹์œผ๋กœ ํ’€์ดํ•œ๋‹ค.

 

ํ’€์ด

def solution(nums):
  answer = -1
  # index๋ฅผ 1000๊นŒ์ง€ ๊ฐ€์ง€๋Š” direct address table ์ƒ์„ฑ
  cnt = [0] * 1001
  # ์ž…๋ ฅ๋œ ์ˆ˜์—ด์˜ ์ˆซ์ž์™€ ์ผ์น˜ํ•˜๋Š” ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค ๊ฐ’์— ๋นˆ๋„์ˆ˜ ์ถ”๊ฐ€
  for num in nums:
    cnt[num] += 1
  # ๋นˆ๋„์ˆ˜๊ฐ€ 1์ธ ์ตœ๋Œ€๊ฐ’์„ ์ฐพ๊ธฐ ์œ„ํ•ด ๋ฐฐ์—ด์„ ๊ฑฐ๊พธ๋กœ ์ˆœ์ฐจํƒ์ƒ‰ํ•˜๋ฉฐ ๋นˆ๋„์ˆ˜๊ฐ€ 1์ธ ๊ฐ’์„ ์ฐพ์œผ๋ฉด ํ•ด๋‹น ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  for i in range(1000, 0, -1):
    if cnt[i] == 1:
      return i;

  return answer;

print(solution([3, 9, 2, 12, 9, 12, 8, 7, 9, 12]));
print(solution([2, 1, 3, 2, 1, 3, 4, 5, 4, 5, 6, 7, 6 ,7, 8, 8]));
print(solution([23, 56, 11, 67, 120, 560, 812, 960, 560, 777, 888, 960]));
print(solution([11, 73, 156, 789, 345, 156, 789, 345, 678, 555, 678]));
print(solution([1, 3, 1, 5, 7, 2, 3, 1, 5]));

8

-1

888

555

7

๋ฐ˜์‘ํ˜•

๋ธ”๋กœ๊ทธ์˜ ์ •๋ณด

Claire's Study Note

Hi.Claire

ํ™œ๋™ํ•˜๊ธฐ