Google foobar gearing_up_for_destruction

后端 未结 9 619
醉话见心
醉话见心 2020-12-29 11:12

I was doing the google foobar challenge but ran out of time on the following challenge i am trying to see what i did wrong.

Challenge

As

9条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-29 11:43

    from fractions import Fraction
    
    def answer(a):
      l = len(a)
      if(not a or l == 1): return [-1,-1]
      s = (a[l-1] - a[0]) if (l % 2 == 0) else (-a[l-1]-a[0]); 
      if(l > 2): 
          for i in range(1, l-1): s+= 2 * (-1)**(i+1) * a[i]
      v = Fraction(2*(float(s)/3 if (l%2==0) else float(s))).limit_denominator();
      c = v;
      for i in range(0, l-2):
        d = a[i+1] - a[i]
        n = d - c
        if(c < 1 or n < 1): return [-1,-1]
        else: c = n
      return [v.numerator, v.denominator];
    

提交回复
热议问题