fork(2) download
  1. def sj_raw(jails, unique=None):
  2. unique = unique or set(jails)
  3. if set(jails) == unique and len(jails) == len(unique):
  4. return jails
  5. else:
  6. lsub = unique.issubset(set(jails[:-1])) and sj_raw(jails[:-1], unique)
  7. rsub = unique.issubset(set(jails[1:])) and sj_raw(jails[1:], unique)
  8. if lsub and rsub:
  9. if len(lsub) > len(rsub):
  10. return rsub
  11. else:
  12. return lsub
  13. else:
  14. return lsub or rsub
  15. sjail = lambda jails: len(sj_raw(jails))
  16. while True:
  17. try:
  18. inp = str(input())
  19. if inp == '':
  20. break
  21. print(sjail(inp.split(' ')))
  22. except (EOFError):
  23. break
Success #stdin #stdout 0.02s 9984KB
stdin
7 3 7 2 1 3 4 1
1 2 3 1 2 1
stdout
5
3