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

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

 

5883๋ฒˆ: ์•„์ดํฐ 9S

์‚ฌ๋žŒ 9๋ช…์ด ์ค„์„ ์„œ์žˆ๊ณ  ๊ฐ ์‚ฌ๋žŒ์ด ์›ํ•˜๋Š” ์šฉ๋Ÿ‰์˜ ํฌ๊ธฐ๋Š” 2, 7, 3, 7, 7, 3, 7, 5, 7 ์ด๋‹ค. ์šฉ๋Ÿ‰ 3์„ ์›ํ•˜๋Š” ์‚ฌ๋žŒ์„ ์ค„์—์„œ ๋นผ๋ฒ„๋ฆฌ๋ฉด, ์ค„์€ 2, 7, 7, 7, 7, 5, 7๊ฐ€ ๋˜๊ณ , 7์„ ์›ํ•˜๋Š” ์‚ฌ๋žŒ์ด 4๋ช…์ด ์—ฐ์†๋œ ๊ตฌ๊ฐ„์ด

www.acmicpc.net


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

๐ŸŽจ Go

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

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

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

	var n int
	fmt.Fscanln(reader, &n)
	var b = make([]int, n)

	keyMap := map[int]bool{}
	for i := 0; i < n; i++ {
		fmt.Fscanln(reader, &b[i])
		keyMap[b[i]] = true
	}

	var maxContinuousCount = 1
	for key := range keyMap {
		keyToDelete := key
		var newLine []int
		for i := 0; i < n; i++ {
			if b[i] != keyToDelete {
				newLine = append(newLine, b[i])
			}
		}
		if len(newLine) == 0 {
			maxContinuousCount = 0
			break
		}
		var prev = newLine[0]
		var continuousCount = 1
		for i := 1; i < len(newLine); i++ {
			var cur = newLine[i]
			if cur == prev {
				continuousCount++
				if maxContinuousCount < continuousCount {
					maxContinuousCount = continuousCount
				}
			} else {
				continuousCount = 1
			}
			prev = cur
		}
	}
	fmt.Fprintln(writer, maxContinuousCount)
}

๐ŸŽจ Python3

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

if __name__ == "__main__":
    n = int(sys.stdin.readline())
    b = []
    keys = {}
    for _ in range(n):
        input = int(sys.stdin.readline())
        b.append(input)
        keys[input] = True
    max_continuous_count = 1
    for key_to_delete in keys:
        new_line = []
        for i in range(n):
            if b[i] != key_to_delete:
                new_line.append(b[i])
        if len(new_line) == 0:
            max_continuous_count = 0
            break
        prev = new_line[0]
        continuous_count = 1
        for i in range(1, len(new_line)):
            cur = new_line[i]
            if cur == prev:
                continuous_count += 1
                max_continuous_count = max(max_continuous_count, continuous_count)
            else:
                continuous_count = 1
            prev = cur
    print(max_continuous_count)
728x90
๋Œ“๊ธ€