ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ๋ฌธ์ œ

 

1302๋ฒˆ: ๋ฒ ์ŠคํŠธ์…€๋Ÿฌ

์ฒซ์งธ ์ค„์— ์˜ค๋Š˜ ํ•˜๋ฃจ ๋™์•ˆ ํŒ”๋ฆฐ ์ฑ…์˜ ๊ฐœ์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค. ์ด ๊ฐ’์€ 1,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค. ๋‘˜์งธ๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ์ฑ…์˜ ์ œ๋ชฉ์ด ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜จ๋‹ค. ์ฑ…์˜ ์ œ๋ชฉ์˜ ๊ธธ์ด๋Š” 50๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๊ณ 

www.acmicpc.net


โœ๐Ÿป ํ’€์ด

๐ŸŽจ Go

// https://www.acmicpc.net/problem/1302
package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n int
	fmt.Fscanln(reader, &n)

	var sold = map[string]int{}
	for i := 0; i < n; i++ {
		var title string
		fmt.Fscanln(reader, &title)
		sold[title]++
	}

	counts := []soldCount{}
	for k, v := range sold {
		counts = append(counts, soldCount{k, v})
	}
	sort.Slice(counts, func(i, j int) bool {
		if counts[i].count > counts[j].count {
			return true
		} else if counts[i].count == counts[j].count {
			return counts[i].title < counts[j].title
		}
		return false
	})
	fmt.Fprintln(writer, counts[0].title)
}

type soldCount struct {
	title string
	count int
}

๐ŸŽจ Python3

# https://www.acmicpc.net/problem/1302
import sys

if __name__ == "__main__":
    n = int(sys.stdin.readline())
    sold = {}
    for i in range(n):
        title = sys.stdin.readline().rstrip()
        if sold.get(title):
            sold[title] += 1
        else:
            sold[title] = 1

    counts = [(key, sold[key]) for _, key in enumerate(sold)]
    counts = sorted(counts, key=lambda x: (-x[1], x[0]))
    print(counts[0][0])
728x90
๋Œ“๊ธ€