一般编程题,如果用数组来处理,注意中间要忽略的城市
1 class FoxAndSightseeing: 2 def getMin(self, position): 3 minx = 999999999 4 for i in range(1, len(position)-1): 5 sumx = 0 6 for k in range(1, len(position)): 7 sumx += abs(position[k] - position[k-1]) 8 sumx -= abs(position[i] - position[i-1]) 9 sumx -= abs(position[i+1] - position[i]) 10 sumx += abs(position[i-1] - position[i+1]) 11 minx = min(sumx, minx) 12 13 return minx 14 15 # test 16 o = FoxAndSightseeing() 17 assert(o.getMin((1, 4, -1, 3)) == 4) 18 assert(o.getMin((-2, 4, 3)) == 5) 19 assert(o.getMin((100, -100, 99, -99)) == 199) 20 21 print('ok') 22 23 # test case