๐Ÿง ๋ฐฑ์ค€ 1427 (๋‹จ์ผ ์ˆซ์ž ๋ถ„๋ฆฌํ•˜๊ธฐ)

๐Ÿคซ  solution_1427

num_str_list = list(input())  # ['5', '0', '0', '6', '1', '3', '0', '0', '9']
print(*sorted(num_str_list, reverse = True), sep = "")

2143 => 1234์ฒ˜๋Ÿผ ๋งŒ๋“ค์–ด์•ผ ํ•˜๋ฏ€๋กœ ๋จผ์ € str๋กœ ๋ฐ›๊ณ  ์›์†Œ ๊ฐ๊ฐ์„ ๋น„๊ตํ•˜๋ฉด ๋œ๋‹ค.

 

๐Ÿง ๋ฐฑ์ค€ 2108 (ํ†ต๊ณ„ํ•™ _ statistics ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ด์šฉ)

๐Ÿคซ ํ•ด๊ฒฐ์˜ ์‹ค๋งˆ๋ฆฌ 

๐Ÿค” ์ˆ˜ํ•™ ํ†ต๊ณ„ ๋‚ด์žฅํ•จ์ˆ˜ statistics (https://python.flowdas.com/library/statistics.html)
์ด ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ, ์ตœ๋นˆ๊ฐ’์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด countํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋  ๊ฒฝ์šฐ, ์ด๋กœ ์ธํ•ด ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๊ฑธ๋ฆฌ๊ฒŒ ๋œ๋‹ค.

๋”ฐ๋ผ์„œ ์•„๋ž˜์˜ ํ†ต๊ณ„ํ•™๊ด€๋ จ statistics ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ multimode()ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ์ตœ๋นˆ๊ฐ’์„ ๊ตฌํ•ด์ค€๋‹ค.



๐Ÿคซ  solution_2108

import statistics as st

num = [int(input()) for _ in range(int(input()))]

mode =st.multimode(num)
if len(mode) > 1:
    mode.remove(min(mode))

print(int(sum(num)/len(num)))
print(round(st.median(num)))
print(min(mode))
print(max(num) - min(num))

 

 

 

๐Ÿง ๋ฐฑ์ค€ 10814,11650, 11651 (C++์˜ pair์™€ ๊ฐ™์€ ์ž๋ฃŒ๊ตฌ์กฐ๊ฐ€ ํ•„์š”ํ•ด!โ˜…โ˜…โ˜…โ˜…โ˜…

๐Ÿคซ ํ•ด๊ฒฐ์˜ ์‹ค๋งˆ๋ฆฌ 1. python์˜ sort๋Š” stable ์ •๋ ฌ์„ ํ•œ๋‹ค.

ํŒŒ์ด์ฌ์€ ๊ธฐ๋ณธ์ ์œผ๋กœ stable_sort์ด๊ธฐ์— ๋‚˜์ด ๊ธฐ์ค€ ์ •๋ ฌ์„ ํ•ด๋„ ๊ฐ€์ž…ํ•œ ์ˆœ์„œ๋Š” ๊ทธ๋Œ€๋กœ!

๐Ÿคซ ํ•ด๊ฒฐ์˜ ์‹ค๋งˆ๋ฆฌ 2.  2์ฐจ์› ๋ฆฌ์ŠคํŠธ์˜ ๊ฒฝ์šฐ, ํ•˜๋‚˜์˜ for๋ฌธ์œผ๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค! 

ํŒŒ์ด์ฌ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด 2์ฐจ์› ๋ฐฐ์—ด์˜ ๊ฒฝ์šฐ, ํ•œ๋ฒˆ์— ์š”์†Œ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.
arr = list(input().split() for _ in range(int(input())))

for x, y in arr:
    print(x, y)  => arr[0][0], arr[0][1]


๐Ÿค” Algorithm ๊ณผ์ • 
๊ตณ์ด int๋กœ ๋ฐ›์ง€ ์•Š์•„๋„ ์–ด์ฐจํ”ผ '20' < '21' ์ด๋ฏ€๋กœ ์ •๋ ฌํ•œ person๋ฆฌ์ŠคํŠธ๋ฅผ for๋ฌธ์„ ์ด์šฉํ•ด ์ ‘๊ทผํ•œ๋‹ค.
์ด๋•Œ, ์ •๋ ฌํ•  ๋•Œ, key = lambda์‹์„ ์ด์šฉํ•ด a[0], ์ฆ‰ ๋‚˜์ด๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•œ๋‹ค.
 

๐Ÿคซ  solution_10814

# ํŒŒ์ด์ฌ์˜ sortํ•จ์ˆ˜๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ stable์ •๋ ฌ์„ ํ•œ๋‹ค.

person = list(input().split() for _ in range(int(input())))

# str๋กœ ๋ฐ›์•„๋„ ์›์†Œ๊ฐ’์„ int๋กœ ๋ฐ›์•„ ๋น„๊ต๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.
for x, y in sorted(person, key = lambda a: int(a[0])):
    print(x, y)
10814์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ•ด๊ฒฐ 

๐Ÿคซ  solution_11650

num = [list(map(int, input().split())) for _ in range(int(input()))]
# num์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ์ €์žฅ๋จ
# [[3, 4], [1, 1], [1, -1], [2, 2], [3, 3]]

# for๋ฌธ์œผ๋กœ 2์ฐจ์› ๋ฆฌ์ŠคํŠธ ์š”์†Œ ์ถœ๋ ฅ
# [1, -1] [1, 1] [2, 2] [3, 3] [3, 4]
for x, y in sorted(num):
    print(x, y)

๐Ÿคซ  solution_11651

num = [list(map(int, input().split())) for _ in range(int(input()))]

# ๋งจ ๋’ค์˜ ์›์†Œ a[1]์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ ํ›„ a[0]์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ
for x, y in sorted(num, key = lambda a: (a[1], a[0])):
    print(x, y)

๋‹จ, ์ด ๋ฌธ์ œ์—์„œ ์œ ์˜ํ•  ์ ์€ python์€ ์ •๋ ฌ์—์„œ ๊ธฐ์ค€ ์—ฌ๋Ÿฌ ๊ฐœ ์„ธ์šธ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ด๋‹ค. ์œ„์—์„œ ์ฒ˜๋Ÿผ ๋ง์ด๋‹ค.

# ๋งจ ๋’ค์˜ ์›์†Œ a[1]์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ ํ›„ a[0]์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ
sorted(num, key = lambda a: (a[1], a[0])):

 

๐Ÿง ๋ฐฑ์ค€ 18870 (๋ฉ”๋ชจ๋ฆฌ์— ๋Œ€ํ•œ ์ ‘๊ทผ)

๐Ÿคซ ํ•ด๊ฒฐ์˜ ์‹ค๋งˆ๋ฆฌ 1. ๋ฉ”๋ชจ๋ฆฌ ์ œํ•œ์ด ์—ฌ์œ ๋กญ๋‹ค??

๐Ÿค” ์ด ๋ฌธ์ œ๋Š” ๋ฉ”๋ชจ๋ฆฌ ์ œํ•œ์ด 512MB๋กœ ์—ฌ์œ ๋กœ์šด๋ฐ, ์ด์— ๋Œ€ํ•ด ์‚ดํŽด๋ณผ ํ•„์š”๊ฐ€ ์žˆ๋‹ค.
-
๋ฉ”๋ชจ๋ฆฌ ์ œํ•œ์ด ๋งŽ์Œ = ์ˆœ์„œ๋ฅผ ๋”ฐ๋กœ ์ €์žฅ(hash)ํ•˜์ง€ ์•Š์œผ๋ฉด ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒƒ์ด๋ผ ์œ ์ถ”
- ์ˆœ์„œ๋งŒ ๊ฐ„๋‹จํžˆ ๋น„๊ตํ•˜๋ฉด ๋˜๊ธฐ์— set์œผ๋กœ ์ค‘๋ณต์„ ์—†์• ๊ณ  sort๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด ๋” ๊ฐ„๋‹จํžˆ ํ•ด๊ฒฐ ๊ฐ€๋Šฅ.
๐Ÿค”์‚ฌ์‹ค ๋ฌธ์ œ์—์„œ ๋งํ•˜๋Š” ๋ฐ”๋Š” ๋“ฑ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ๊ณผ ๋™์ผ!

๐Ÿคซ ํ•ด๊ฒฐ์˜ ์‹ค๋งˆ๋ฆฌ 2. ๋”•์…”๋„ˆ๋ฆฌ ์ž๋ฃŒ๊ตฌ์กฐ

๐Ÿค” Dictionary.
- 
key-value ํ˜•ํƒœ๋กœ ์ด๋ฃจ์–ด์ง„ ์ž๋ฃŒ๊ตฌ์กฐ๋กœ ๊ฐ’๊ณผ ๋ฐ์ดํ„ฐ์˜ ๋Œ€์‘๊ด€๊ณ„๋ฅผ ํ‘œํ˜„ํ•  ๋•Œ, powerfulํ•˜๋‹ค.
- ์ด๋•Œ, ํ•ต์‹ฌ์€ key๊ฐ’์€ ์ค‘๋ณต๋  ์ˆ˜ ์—†๋‹ค๋Š” ์ ์ด๋ฉฐ ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ์˜ˆ์‹œ๋กœ ๋“ค์–ด ๋ณด๊ฒ ๋‹ค.
# x_set์—๋Š” [-10, -9, 2, 4] ์›์†Œ๊ฐ€ ๋“ค์–ด๊ฐ€์žˆ๋Š” set๋ฆฌ์ŠคํŠธ์ด๋‹ค.

x_dict = {x_set[i] : i for i in range(len(x_set))}

#x_dict = { -10: 0, -9: 1, 2: 2, 4: 3 }
# print(x_dict[-9])  # key๊ฐ’์„ ์ด์šฉํ•œ value, 1 ์ถœ๋ ฅ




๐Ÿค” Algorithm ๊ณผ์ • 
1. ์ž…๋ ฅ๊ฐ’์„ ์ €์žฅํ•˜๋Š” x๋ฆฌ์ŠคํŠธ์™€
2. x๋ฆฌ์ŠคํŠธ์˜ ์›์†Œ๋“ค์ด ๊ฒน์น˜์ง€ ์•Š๋„๋ก set์„ ๋งŒ๋“ค์–ด ์ •๋ ฌ์‹œ์ผœ์ค€๋‹ค.
3. ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ์ด ๋œ set์— index๋ฅผ ๋งตํ•‘ํ•ด์ฃผ๋ฉด ๊ทธ๊ฒƒ์ด ๋น„๋กœ์†Œ ์ˆœ์œ„๊ฐ€ ๋˜๋ฏ€๋กœ
4. set_element  :  index์˜ ๋งตํ•‘์„ ์œ„ํ•ด ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด์ค€๋‹ค.
5. ๊ทธ ํ›„ x๋ฆฌ์ŠคํŠธ๋ฅผ ๋Œ๋ฉด์„œ ํ•ด๋‹น ์›์†Œ์™€ ์ผ์น˜ํ•˜๋Š” ๊ฐ’๊ณผ ๋งตํ•‘ํ•œ ๋”•์…”๋„ˆ๋ฆฌ์˜ value๊ฐ’(์ธ๋ฑ์Šค, ์ˆœ์œ„ ๊ฐ’)์„ ์ถœ๋ ฅ

 

๐Ÿคซ  solution_18870 (์‹œ๊ฐ„์ดˆ๊ณผ)

N = int(input())
x = list(map(int, input().split()))
x_prime = []

for i in x:
    cnt = 0
    for j in set(sorted(x)):
        if i > j:
            cnt += 1
    x_prime.append(cnt)
print(*x_prime)

์ฒ˜์Œ ์ง  ์ฝ”๋“œ๋กœ ๋ฌธ์ œ์ ์€ ์ด์ค‘ for๋ฌธ์„ ์ด์šฉํ•˜์—ฌ if๋ฌธ์„ 2๋ฒˆ ๋Œ๋ฆฌ๋ฉด ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ O(n^2)์ด๋‹ค.
์ด ๋ฌธํ•ญ์˜ ๊ฒฝ์šฐ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ O(nlog n)์˜ ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ํ•„์š”ํ•˜๋‹ค.


๐Ÿคซ  solution_18870

N = int(input())
x = list(map(int, input().split()))
x_set = sorted(list(set(x)))
x_prime = []

# x_set์˜ [-10, -9, 2, 4]์— index๋ฅผ ๋งตํ•‘ํ•˜์—ฌ ๋”•์…”๋„ˆ๋ฆฌ๋กœ(์ˆœ์œ„)
# cf. ๋”•์…”๋„ˆ๋ฆฌ๋Š” ํ•ด์‹œ๋ฅผ ์ด์šฉํ•ด ๋ฐ์ดํ„ฐ ์ €์žฅ
x_dict = {x_set[i] : i for i in range(len(x_set))}
#x_dict = {-10: 0, -9: 1, 2: 2, 4: 3}
# print(x_dict[-9])  # 1 ์ถœ๋ ฅ


for i in x:
    print(x_dict[i], end = " ")
 

+ Recent posts