[Programmers] ํŠœํ”Œ

Updated:

ํŠœํ”Œ

ํŠœํ”Œ ์„ ํด๋ฆญํ•˜๋ฉด ๋ฐ”๋กœ ์ด๋™ํ•œ๋‹ค.

์ด ๋ฌธ์ œ๋Š” ๋‘ ๊ฐ€์ง€๋ฅผ ํ•ด๊ฒฐํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

์ฒซ ๋ฒˆ์งธ๋Š” ๋ฌธ์ž์—ด๋กœ ์ฃผ์–ด์ง„ ์ง‘ํ•ฉ์„ ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ์ž๋ฃŒํ˜•์œผ๋กœ ๋ฐ”๊พธ๋Š” ๊ฒƒ!

๋‘ ๋ฒˆ์งธ๋Š” ์›ํ•˜๋Š” ์ž๋ฃŒํ˜•์œผ๋กœ ๋ฐ”๊พธ๊ณ  ๋‚œ ๋’ค ๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š” ๋‹ต์„ ๋„์ถœํ•ด ๋‚ผ ๊ฒƒ!


๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง€๋ฉด ๋‚˜๋Š” ๊ทธ๊ฒƒ์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ”๊พธ๊ธธ ์›ํ–ˆ๋‹ค.

๊ทธ๋ž˜์„œ ์ฒ˜์Œ ์ƒ๊ฐํ•œ ๋ฐฉ๋ฒ•์€ s[1:-1] ์„ ํ†ตํ•ด ๋งจ ์ฒ˜์Œ๊ณผ ๋งˆ์ง€๋ง‰ ๊ด„ํ˜ธ๋Š” ๋–ผ๊ณ  ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฆฌ๋ฉด์„œ โ€˜{โ€˜ ๊ฐ€ ๋‚˜์˜ค๋ฉด ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ 

โ€™}โ€™ ๊ฐ€ ๋‚˜์˜ค๋ฉด ์ €์žฅ ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ์˜ฎ๊ธฐ๋Š” ์ž‘์—…์„ ํ–ˆ๋‹ค.

[[1, 2, 3], [2, 1], [1, 2, 4, 3], [2]] ๋กœ ๋งŒ๋“  ๋’ค ํ•ด๋‹น ์ด์ฐจ์› ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ธธ์ด ์ˆœ์œผ๋กœ ์ •๋ ฌ์„ ํ–ˆ๋‹ค.

[[2], [2, 1], [1, 2, 3], [1, 2, 4, 3]] ์ด ๋˜๋Š”๋ฐ ์ด์ œ ์ฒซ ๋ฒˆ์งธ ๋ฆฌ์ŠคํŠธ ๋ถ€ํ„ฐ ๋Œ๋ฉด์„œ ์ค‘๋ณต ๊ฐ’์„ ์ œ์™ธํ•œ ๊ฐ’๋“ค์„ ๋ฝ‘์•„๋‚ด๋ฉด ๋œ๋‹ค.

[2, 1, 3, 4] ๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค.

def solution(s):
	string = s[1:-1]
	sort_box = list()
	box = list()
	answer = list()
	input_num = ''

	for cur in string:
		if cur == '{':
			switch = True
			continue
		elif cur == '}':
			switch = False
			box.append(int(input_num))
			sort_box.append(box.copy())
			box.clear()
			input_num = ''

		if switch and cur != ',':
			input_num += cur

		if switch and cur == ',':
			box.append(int(input_num))
			input_num = ''

	sort_box.sort(key=lambda x: len(x))
	answer.append(sort_box[0][0])

	for val in sort_box:
		answer += set(val) - set(answer)

	return answer

๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ”๊พธ๋Š” ๋” ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์ด ์žˆ์—ˆ๋‹ค.

๋‚˜๋„ ์ฒ˜์Œ์—” split ์„ ์‚ฌ์šฉ ํ•ด ๋ณผ๊นŒ ํ–ˆ๋Š”๋ฐ ์ ๋‹นํ•˜๊ฒŒ ๋‚˜๋ˆŒ ๊ธฐ์ค€์ด ์•ˆ๋ณด์—ฌ ํฌ๊ธฐํ–ˆ์—ˆ๋‹ค.

ํ•˜์ง€๋งŒโ€ฆ ์กฐ๊ธˆ๋งŒ ๋” ๊ผผ๊ผผํžˆ ๋ณผ ๊ป„ ๊ธฐ์ค€์ด ์žˆ์—ˆ๋‹ค !!

๋งจ ์™ผ์ชฝ ๊ด„ํ˜ธ ๋‘ ๊ฐœ , ๋งจ ์˜ค๋ฅธ์ชฝ ๊ด„ํ˜ธ ๋‘ ๊ฐœ๋ฅผ ์ง€์šด ๋’ค โ€˜},{โ€˜ ๋กœ split ํ•˜๋ฉด ์ˆซ์ž๋งŒ ๋ฝ‘์•„๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

def solution(s):
	split_str = s[2:-2].split('},{')
	sort_box = []
	box = []
	answer = []
	input_num = ''

	for cur in split_str:
		for val in cur:
			if val != ',':
				input_num += val
			else:
				box.append(int(input_num))
				input_num = ''

		box.append(int(input_num))
		input_num = ''
		sort_box.append(box.copy())
		box.clear()

	sort_box.sort(key=lambda x: len(x))

	for val in sort_box:
		answer += set(val) - set(answer)

	return answer

Categories:

Updated:

Leave a comment