Consider N coins aligned in a row.

Consider N coins aligned in a row. Each coin is showing either heads or tails. The adjacency of these coins is the number of adjacent pairs of coins with the same side facing up.

It must return the maximum possible adjacency that can be obtained by reversing exactly one coin (that is, one of the coins must be reversed). Consecutive elements of array A represent consecutive coins in the row. Array A contains only 0s and/or 1s: 0 represents
a coin with heads facing up; 1 represents a coin with tails facing up. For example, given array A consisting of six numbers, such that:

A[0] = 1

A[1] = 1

A[2] = 0

A[3] = 1

A[4] = 0

A[5] = 0

the function should return 4. The initial adjacency is 2, as there are two pairs of adjacent coins with the same side facing up, namely (0, 1) and (4, 5). After reversing the coin represented by A[2], the adjacency equals 4, as there are four pairs of adjacent
coins with the same side facing up, namely: (0, 1), (1, 2), (2, 3) and (4, 5), and it is not possible to obtain a higher adjacency. The same adjacency can be obtained by reversing the coin represented by A[3].

I should mention here that number of elements can be from 1....10‘000. And it has to be done as O(N)

public class FlipCoin {
	public static void main(String[] args) {
		int[] A = {1, 1, 0, 1, 0, 0, 1, 1};
		System.out.println(new FlipCoin().solution(A) );
	}

	public int solution(int[] A) {
		boolean flipped = false;
		int adjacency = 0;
		for(int i=1; i<A.length-1; i++) {
			if(A[i] == A[i-1]) adjacency++;
			else if(A[i-1] == A[i+1] && !flipped) {
				flipped = true;
				adjacency += 2;
				i++;
			}
		}

		if(flipped ) {
			if(A[A.length-1] == A[A.length-2]) adjacency++;
		} else {
			if(A[A.length-1] != A[A.length-2]) adjacency++;
                        else if(A[0] != A[1] ) adjacency+=2;
                }

		return adjacency;
	}
}
时间: 2024-10-12 06:33:30

Consider N coins aligned in a row.的相关文章

Codeforces1111D Destroy the Colony 退背包+组合数

Codeforces1111D 退背包+组合数 D. Destroy the Colony Description: There is a colony of villains with several holes aligned in a row, where each hole contains exactly one villain. Each colony arrangement can be expressed as a string of even length, where the

Codeforces1138-A(D题)Sushi for Two

Arkady invited Anna for a dinner to a sushi restaurant. The restaurant is a bit unusual: it offers nn pieces of sushi aligned in a row, and a customer has to choose a continuous subsegment of these sushi to buy. The pieces of sushi are of two types:

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars There are n pillars aligned in a row and numbered from 1 to n. Initially each pillar contains exactly one disk. The i-th pillar contains a disk having radius ai. You can move these disks

441. 硬币楼梯 Arranging Coins

You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins. Given n, find the total number of full staircase rows that can be formed. n is a non-negative integer and fits within the range of

LeetCode 441. Arranging Coins

You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins. Given n, find the total number of full staircase rows that can be formed. n is a non-negative integer and fits within the range of

LeetCode Arranging Coins

原题链接在这里:https://leetcode.com/problems/arranging-coins/ 题目: You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins. Given n, find the total number of full staircase rows that can be forme

[LeetCode] Arranging Coins 排列硬币

You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins. Given n, find the total number of full staircase rows that can be formed. n is a non-negative integer and fits within the range of

[Leetcode + Lintcode] 441. Arranging Coins

You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins. Given n, find the total number of full staircase rows that can be formed. n is a non-negative integer and fits within the range of

441. Arranging Coins

You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins. Given n, find the total number of full staircase rows that can be formed. n is a non-negative integer and fits within the range of