Problem:
Score : 300 points

Problem Statement

There are N stones arranged in a row. Every stone is painted white or black.
A string S represents the color of the stones. The i-th stone from the left is white if the i-th character of S is ., and the stone is black if the character is #.

Takahashi wants to change the colors of some stones to black or white so that there will be no white stone immediately to the right of a black stone.
Find the minimum number of stones that needs to be recolored.

Constraints

1 \leq N \leq 2\times 10^5

S is a string of length N consisting of . and #.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the minimum number of stones that needs to be recolored.

Sample Input 1

3
#.#

Sample Output 1

1

It is enough to change the color of the first stone to white.

Sample Input 2

5
#.##.

Sample Output 2

2

Sample Input 3

9
.........

Sample Output 3

0

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

public class Main {
	private static final int MOD = 1000000007;

	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		String S = sc.next();
		char[] s = S.toCharArray();

		int sumWhite = 0; // .
		for (char c : s) {
			if (c == '.') {
				sumWhite++;
			}
		}

		int min = sumWhite;
		int white = 0;
		int black = 0;
		for (int i = 0; i < s.length; i++) {
			if (s[i] == '.') {
				white++;
			} else {
				black++;
			}

			min = Math.min(min, black + (sumWhite - white));
		}

		System.out.println(min);
	}

	private static long calc1(String S) {
		char[] s = S.toCharArray();
		// # をすべて . にする
		long x = 0;
		char[] xx = Arrays.copyOf(s, s.length);
		for (int i = 0; i < s.length; i++) {
			if (s[i] == '#') {
				x++;
				xx[i] = '.';
			}
		}

		// 最初の # 以降を . → #
		int state = 0;
		long y = 0;
		char[] yy = Arrays.copyOf(s, s.length);
		for (int i = 0; i < s.length; i++) {
			if (state == 0) {
				if (s[i] == '#') {
					state = 1;
				}
			} else {
				if (s[i] == '.') {
					y++;
					yy[i] = '#';
				}
			}
		}

		// 最後の # より左は # → . 、最後の # より右は . → #
		long z = 0;
		char[] zz = Arrays.copyOf(s, s.length);
		int index = S.lastIndexOf('#');
		for (int i = 0; i < s.length; i++) {
			if (i < index) {
				if (s[i] == '#') {
					z++;
					zz[i] = '.';
				}
			} else {
				if (s[i] == '.') {
					z++;
					zz[i] = '#';
				}
			}
		}

		long min = Math.min(Math.min(x, y), z);
		return min;
	}

	private static boolean isDebug = System.getProperty("sun.desktop") != null;

	private static void debug(Object... o) {
		if (isDebug) {
			System.err.println(Arrays.deepToString(o));
		}
	}

	public static class Scanner {
		private BufferedInputStream inputStream;

		public Scanner(InputStream in) {
			inputStream = new BufferedInputStream(in);
		}

		public char nextChar() throws IOException {
			return (char) skip();
		}

		public int nextInt() throws IOException {
			int num = 0;

			int read = skip();
			do {
				num = num * 10 + (read - 0x30);
			} while ((read = inputStream.read()) > 0x20);

			return num;
		}

		public void fill(int[] a) throws IOException {
			for (int i = 0; i < a.length; i++) {
				a[i] = nextInt();
			}
		}

		public void fill(int[] a, int[] b) throws IOException {
			if (a.length != b.length) {
				throw new IllegalArgumentException();
			}

			for (int i = 0; i < a.length; i++) {
				a[i] = nextInt();
				b[i] = nextInt();
			}
		}

		public long nextLong() throws IOException {
			long num = 0;

			int read = skip();
			do {
				num = num * 10 + (read - 0x30);
			} while ((read = inputStream.read()) > 0x20);

			return num;
		}

		public void fill(long[] a) throws IOException {
			for (int i = 0; i < a.length; i++) {
				a[i] = nextLong();
			}
		}

		public void fill(long[] a, long[] b) throws IOException {
			if (a.length != b.length) {
				throw new IllegalArgumentException();
			}

			for (int i = 0; i < a.length; i++) {
				a[i] = nextLong();
				b[i] = nextLong();
			}
		}

		public long[] nextLong(int n) throws IOException {
			long[] array = new long[n];
			for (int i = 0; i < n; i++) {
				array[i] = nextLong();
			}

			return array;
		}

		public String next() throws IOException {
			StringBuilder builder = new StringBuilder();

			int read = skip();
			do {
				builder.append((char) read);
			} while ((read = inputStream.read()) > 0x20);

			return builder.toString();
		}

		private int skip() throws IOException {
			int read;
			while ((read = inputStream.read()) <= 0x20)
				;

			return read;
		}
	}
}