diff = [0] * 300 for x, y in nums: diff[x] += 1 diff[y + 1] -= 1 ans = 0 for i inrange(1, 120): diff[i] += diff[i - 1] if diff[i] >= 1: ans += 1 return ans
classSolution: defminimumMoves(self, grid: List[List[int]]) -> int: n = 3 s = [] e = [] mp = [[0]*n for _ inrange(n)] for i inrange(n): for j inrange(n): if grid[i][j]>1: s.append((i,j)) mp[i][j] = grid[i][j]-1 elif grid[i][j]<1: e.append((i,j)) ans = inf defdfs(i,cnt): if i==len(e): nonlocal ans ans = min(ans,cnt) return for x,y in s: if mp[x][y]>0: mp[x][y]-=1 le = abs(x-e[i][0])+abs(y-e[i][1]) dfs(i+1,cnt+le) mp[x][y]+=1 dfs(0,0)
s1, s2 = '', '' for i inrange(3): for j inrange(3): if grid[i][j] == 0: s1 += str(3 * i + j) elif grid[i][j] > 1: s2 += str(3 * i + j) + str(grid[i][j]) k = len(s1) res = '' for _ inrange(k): res += '-'
defdo(s: str, idx: int) -> str: lis = list(s) lis[idx] = '-' return''.join(c for c in lis)
defdo1(ss: str, idx: int) -> str: lis = list(ss) lis[idx] = str(int(lis[idx]) - 1) return''.join(c for c in lis)
@cache defdfs(s: str, ss: str) -> int: if s == res: return0 ans = 10 ** 9 for idx, c inenumerate(s): if c == '-': continue l = len(ss) for t inrange(0, l, 2): c1, v = int(ss[t]), int(ss[t + 1]) if v == 1: continue ans = min(ans, dfs(do(s, idx), do1(ss, t + 1)) + get(int(c), c1)) return ans