由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 一道面试改错题,求答案
相关主题
An online coding test problemc++ primer求助
发个G店面的题目问一个graph题
MS面试题Leetcode online judge的word search是不是用dp?
有最简法code么InterviewStreet problem - Meeting Point
how to solve this google interview question求解一道算法题
问一道data structure的面试题Leetcode triangle 题目clarification
一道google interview的题目坐cruise去巴哈马影响date of last entry into u.s.么
问一道数据结构题topo sort莫非用matrix list表示graph更方便?
相关话题的讨论汇总
话题: count话题: int话题: coins话题: adjacency话题: coin
进入JobHunting版参与讨论
1 (共1页)
c**********t
发帖数: 8
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;
}
w****k
发帖数: 755
2
题目太长了,没读完,貌似还很难懂,估计这个版没人原因浪费这个时间。
y*****n
发帖数: 1235
3
Int r=-1就可以了。
有可能全是正面。然后必须flip一个。选头尾的就行。
1 (共1页)
进入JobHunting版参与讨论
相关主题
topo sort莫非用matrix list表示graph更方便?how to solve this google interview question
一般社交网站的"friend"是怎么存储的呢?问一道data structure的面试题
贡献A家面经一道google interview的题目
一道linkedin的graph题问一道数据结构题
An online coding test problemc++ primer求助
发个G店面的题目问一个graph题
MS面试题Leetcode online judge的word search是不是用dp?
有最简法code么InterviewStreet problem - Meeting Point
相关话题的讨论汇总
话题: count话题: int话题: coins话题: adjacency话题: coin