Interviewstreet糖果挑战

时间:2012-07-18 05:22:56

标签: ruby algorithm puzzle

当我的代码给出错误答案时,有人可以给我测试用例吗?

问题在这里 - https://www.interviewstreet.com/challenges/dashboard/#problem/4fe12e4cbb829

我的解决方案

n=gets.to_i
marks=STDIN.readlines
#print marks
marks.map!{|x| x.chomp.strip}
marks.delete('')
marks.map! { |a| a.to_i}
#print marks
i=0
sum=0
len=marks.size
candies=[]
flag=-1
def min(a,b)
    return a if a<=b
    return b
end
change=[]
while i<len
    if i==0
        candies[i]=1
        i=i+1
        next
    end
    if marks[i]<marks[i-1]
        candies[i]=min(candies[i-1]-1,1)
        if candies[i]-candies[i-1]==-1
            flag=i-1 if flag==-1
        end
        if candies[i]-candies[i-1]<-1
            flag=i if flag==-1
        end
        i=i+1
        next
    end
    if marks[i-1]==marks[i]
        candies[i]=1
        i=i+1
        next
    end
    if marks[i]>marks[i-1]
        candies[i]=candies[i-1]+1
        i=i+1
        next
    end
end
#puts ''
#print candies
min_candies=candies.min
min_candies=(1 - min_candies)
i=flag
while i<len-1
    candies[i]=candies[i]+min_candies 
    i=i+1
end
candies[len-1]+=min_candies
#puts ''
#print candies
# x=candies.min
# puts x
#x=1-x
# candies.map!{ |a| a+x}
 candies.each{ |a| sum+=a}
# print candies
puts sum

它仅为十分之一的测试用户提供了成功,我无法找到任何失败的案例:(

0 个答案:

没有答案