|
4 | 4 | import math |
5 | 5 | import heapq |
6 | 6 | from heapq import heappop, heapify, heappush, heappushpop, heapreplace |
7 | | - |
8 | | - |
| 7 | +from collections import Counter |
9 | 8 |
|
10 | 9 | class Solution: |
11 | 10 |
|
@@ -386,9 +385,101 @@ def evaluate(self, s: str) -> int: |
386 | 385 |
|
387 | 386 | return res |
388 | 387 |
|
| 388 | + def getMinDistSum(self, positions: List[List[int]]) -> float: |
| 389 | + xs = [i[0] for i in positions] |
| 390 | + ys = [i[1] for i in positions] |
| 391 | + minx = min(xs) |
| 392 | + maxx = max(xs) |
| 393 | + miny = min(ys) |
| 394 | + maxy = max(ys) |
| 395 | + ans = 999999999999999 |
| 396 | + record = [] |
| 397 | + for i in range(minx, maxx+1): |
| 398 | + for j in range(miny, maxy+1): |
| 399 | + temp_xs = [(i - ti)**2 for ti in xs] |
| 400 | + temp_ys = [(j - tj)**2 for tj in ys] |
| 401 | + t = math.sqrt(sum([temp_xs[i] + temp_ys[i] for i in range(len(positions))])) |
| 402 | + if t < ans: |
| 403 | + ans = t |
| 404 | + record = [i, j] |
| 405 | + print(record) |
| 406 | + return ans |
389 | 407 |
|
| 408 | + def minOperations(self, nums: List[int]) -> int: |
| 409 | + def dfs(s:str): |
| 410 | + if len(s) == 0: |
| 411 | + return 0 |
| 412 | + elif len(s) == 1: |
| 413 | + return 1 |
| 414 | + s_list = s.split("0") |
| 415 | + ans = 0 |
| 416 | + for s in s_list: |
| 417 | + if len(s) == 0: |
| 418 | + continue |
| 419 | + c = Counter(s) |
| 420 | + char = c.most_common(1)[0][0] |
| 421 | + ans += 1 |
| 422 | + lister = [ i for i in s.split(char) if i] |
| 423 | + for new_s in lister: |
| 424 | + ans += dfs(new_s) |
| 425 | + return ans |
| 426 | + return dfs("".join([str(i) for i in nums])) |
| 427 | + |
| 428 | + def maxDistance(self, s: str, k: int) -> int: |
| 429 | + x = [0,0] |
| 430 | + y = [0,0] |
| 431 | + ans = 0 |
| 432 | + for idx, c in enumerate(s): |
| 433 | + if c == "N": |
| 434 | + y[0] += 1 |
| 435 | + elif c == 'S': |
| 436 | + y[1] += 1 |
| 437 | + elif c == "W": |
| 438 | + x[1] += 1 |
| 439 | + else: |
| 440 | + x[0] += 1 |
| 441 | + minnum = min(x) + min(y) |
| 442 | + if k >= minnum: |
| 443 | + ans = max(ans, max(x) + max(y) + minnum) |
| 444 | + else: |
| 445 | + ans = max(ans, max(x) + max(y) +2*k - minnum) |
| 446 | + return ans |
390 | 447 |
|
| 448 | + def uniquePaths(self, grid: List[List[int]]) -> int: |
| 449 | + m = len(grid) |
| 450 | + n = len(grid[0]) |
| 451 | + if grid[m-1][n-1] == 1: |
| 452 | + return 0 |
| 453 | + dp = [[0 for _ in range(n)] for _ in range(m)] |
| 454 | + dp[0][0] = 1 |
391 | 455 |
|
| 456 | + for i in range(m): |
| 457 | + for j in range(n): |
| 458 | + if i + j == 0: |
| 459 | + continue |
| 460 | + else: |
| 461 | + if i == 0: |
| 462 | + if grid[i][j] == 0: |
| 463 | + dp[i][j] += dp[i][j-1] |
| 464 | + else: |
| 465 | + if i + 1 < m: |
| 466 | + dp[i+1][j] += dp[i][j-1] |
| 467 | + elif j == 0: |
| 468 | + if grid[i][j] == 0: |
| 469 | + dp[i][j] += dp[i-1][j] |
| 470 | + else: |
| 471 | + if j + 1< n: |
| 472 | + dp[i][j+1] += dp[i-1][j] |
| 473 | + else: |
| 474 | + if grid[i][j] == 0: |
| 475 | + dp[i][j] += dp[i-1][j] + dp[i][j-1] |
| 476 | + else: |
| 477 | + if i+1 < m: |
| 478 | + #左边来的会变到下面 |
| 479 | + dp[i+1][j] += dp[i][j-1] |
| 480 | + if j+1 < n: |
| 481 | + dp[i][j+1] += dp[i-1][j] |
| 482 | + return dp[m-1][n-1] |
392 | 483 | s = Solution() |
393 | 484 | print(s.minCost(maxTime = 29, edges = [[0,1,10],[1,2,10],[2,5,10],[0,3,1],[3,4,10],[4,5,15]], passingFees = [5,1,2,20,20,3])) |
394 | 485 |
|
0 commit comments