2017年2月16日 星期四

【Python】Codility in Python : Lesson 4 - Counting Elements【FrogRiverOne】

Counting Elements 練習題的第三題題目是【FrogRiverOne】
Find the earliest time when a frog can jump to the other side of a river.

方法一:使用 set 的簡易方式

def solution(X, A):
  S = set()
  count = len(A)
  for i in range(0, count):
    S.add(A[i])
    if(len(S) == X):
      return i
  return -1

完整練習題 source code 請參閱:github

沒有留言:

張貼留言