Redian新闻
>
天天渴望阳光的微笑
avatar
天天渴望阳光的微笑# Piebridge - 鹊桥
c*t
1
纠结它只让改三行code, 毫无头绪,求解
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 of face.

You are giving an implementation of a function:
int solution(int A[], int N);
that, given a non-empty zero-idexed array A consisting of N integers
representing the coins, returns 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.

Unfortunately, despite the fact that the funciton may return expected
result for hte example input, there is a bug in the implementation, which
may produce incorrect results for other inputs. Find the bug and correct it.
You should modify at most three lines of code.

N is an integer within the range of [1....1'000]. And it has to be done
as O(N) time and O(1) space. Elements of input arrays can be modified.
int solution(int *A, int A_length) {
int n = A_length;
int result = 0;
int i;
for (i = 0; i < n - 1; i++) {
if (A[i] == A[i + 1])
result = result + 1;
}
int r = 0;
for (i = 0; i < n; i++) {
int count = 0;
if (i > 0) {
if (A[i - 1] != A[i])
count = count + 1;
else
count = count - 1;
}
if (i < n - 1) {
if (A[i + 1] != A[i])
count = count + 1;
else
count = count - 1;
}
if (count > r)
r = count;
}
return result + r;
}
avatar
h*y
2
★性别: 女
★出生年份: 1975
★所在地(起码给出state): 加州
★职业情况(学生还是工作):工作
★简单的物理参数(身高/体重):160cm/110 lbs
★当前婚姻状态(从没结过婚/曾婚/丧偶): 从没结过婚
★联系方式(email/IM/站内):c********[email protected]
★血型、星座(optional):
★是否希望版务对本帖锁贴并禁止讨论:是
我从小接受中国传统教育,读了理科,在美国博士毕业,来到北加州工作。性格比较温和
愉快, 有点shy, 很enjoy与熟悉亲密的朋友畅谈。 对很多事物都有好奇心,既喜欢自
然科学也喜欢文学艺术。 兴趣爱好广泛:喜欢耐久又看似有点枯燥的运动,跑步,游
泳,形体锻炼;喜欢听音乐会, 特别是钢琴演奏;喜欢参观各种博物馆, 欣赏意义深远
的电影, 歌剧, 歌曲, 中国传统舞蹈;看电视会常选择看documentary film, 历史片,
常听National Public Radio。喜欢自然科学,尤其需要动手实验的学科;最喜欢读各
种书,特别是偏医学类的。喜欢在温
avatar
w*k
3
题目太长了,没读完,貌似还很难懂,估计这个版没人原因浪费这个时间。
avatar
y*n
4
Int r=-1就可以了。
有可能全是正面。然后必须flip一个。选头尾的就行。
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。