๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 1439_๋’ค์ง‘๊ธฐ

๋ฟŒ์•ผ._. 2023. 9. 13. 21:18

Silver V

๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/1439)

< ๋’ค์ง‘๊ธฐ >

 

๋ฌธ์ œ ํ’€์ด 

 

์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์„ ๊ฐ™์€ ์ˆซ์ž๋กœ ๋‹ค ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ๋ฌธ์ œ์ด๊ธฐ ๋•Œ๋ฌธ์— ์—ฐ์†๋œ 0์˜ ๊ตฌ๊ฐ„ ๊ฐœ์ˆ˜์™€ ์—ฐ์†๋œ 1์˜ ๊ตฌ๊ฐ„ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•ด์„œ ๋” ์ ์€ ๊ฐœ์ˆ˜๋ฅผ ์„ ํƒํ•˜๋ฉด ๋œ๋‹ค.

 

 

 my solution (Java)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class _1439_ { // ๋’ค์ง‘๊ธฐ

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));

		String str = bf.readLine();

		int zero = 0, one = 0;

		if (str.charAt(0) == '0') {
			zero += 1;
		} else {
			one += 1;
		}

		for (int i = 1; i < str.length(); i++) {
			if (str.charAt(i - 1) != str.charAt(i)) {
				if (str.charAt(i) == '0')
					zero += 1;
				else
					one += 1;
			}
		}

		int result = (zero > one) ? one : zero;
		System.out.println(result);

	}
}

 

Main

๋ณ€์ˆ˜)
str : ๋ฌธ์ž์—ด
zero, one : 0๊ตฌ๊ฐ„, 1๊ตฌ๊ฐ„ ๊ฐœ์ˆ˜
result : ์ตœ์†Œ ํšŸ์ˆ˜

 

- ๋ฌธ์ž์—ด(str) ์ž…๋ ฅ

- ์ฒซ ๋ฒˆ์งธ ๊ฐ’์ด 0์ธ์ง€ 1์ธ์ง€ ํ™•์ธ ํ›„ ๊ฐ’ ํ‘œ์‹œ

- ๋ฌธ์ž์—ด์„ ํƒ์ƒ‰ ํ›„ ์ „๊ณผ ๋น„๊ตํ•ด์„œ ๊ฐ™์€ ๊ฐ’์ด ์•„๋‹ˆ๋ผ๋ฉด ๋‹ค๋ฅธ ๊ตฌ๊ฐ„ ์ด๋ฏ€๋กœ 0์ธ์ง€ 1์ธ์ง€ ํ™•์ธ ํ›„ ๊ฐ’ ํ‘œ์‹œ

- 0 ๊ตฌ๊ฐ„๊ณผ 1 ๊ตฌ๊ฐ„ ๊ฐœ์ˆ˜๋ฅผ ๋น„๊ต ํ›„ ๋” ์ ์€ ๊ณณ์„ ๋ฐ”๊พธ๋Š” ๊ฒƒ์ด ์ตœ์†Œ ํšŸ์ˆ˜ ์ด๋ฏ€๋กœ ์ถœ๋ ฅ