1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| ''' Author: NEFU AB-IN Date: 2024-08-17 22:12:44 FilePath: \LeetCode\CP137_2\c\c.py LastEditTime: 2024-08-17 23:11:38 '''
import random from collections import Counter, defaultdict, deque from datetime import datetime, timedelta from functools import lru_cache, reduce from heapq import heapify, heappop, heappush, nlargest, nsmallest from itertools import combinations, compress, permutations, starmap, tee from math import ceil, comb, fabs, floor, gcd, hypot, log, perm, sqrt from string import ascii_lowercase, ascii_uppercase from sys import exit, setrecursionlimit, stdin from typing import Any, Callable, Dict, List, Optional, Tuple, TypeVar, Union
TYPE = TypeVar('TYPE') N = int(2e5 + 10) M = int(20) INF = int(1e12) OFFSET = int(100) MOD = int(1e9 + 7)
setrecursionlimit(int(2e9))
class Arr: array = staticmethod(lambda x=0, size=N: [x() if callable(x) else x for _ in range(size)]) array2d = staticmethod(lambda x=0, rows=N, cols=M: [Arr.array(x, cols) for _ in range(rows)]) graph = staticmethod(lambda size=N: [[] for _ in range(size)])
class Math: max = staticmethod(lambda a, b: a if a > b else b) min = staticmethod(lambda a, b: a if a < b else b)
class IO: input = staticmethod(lambda: stdin.readline().strip()) read = staticmethod(lambda: map(int, IO.input().split())) read_list = staticmethod(lambda: list(IO.read())) read_mixed = staticmethod(lambda *types: [t(v) for t, v in zip(types, IO.input().split())])
class Std: pass
class Solution: def maximumValueSum(self, board: List[List[int]]) -> int: m, n = len(board), len(board[0]) row_dict = defaultdict(list) col_dict = defaultdict(list)
for i in range(m): for j in range(n): value = board[i][j]
if len(row_dict[i]) < 3: row_dict[i].append((value, i, j)) row_dict[i].sort(reverse=True, key=lambda x: x[0]) elif value > row_dict[i][-1][0]: row_dict[i][-1] = (value, i, j) row_dict[i].sort(reverse=True, key=lambda x: x[0])
if len(col_dict[j]) < 3: col_dict[j].append((value, i, j)) col_dict[j].sort(reverse=True, key=lambda x: x[0]) elif value > col_dict[j][-1][0]: col_dict[j][-1] = (value, i, j) col_dict[j].sort(reverse=True, key=lambda x: x[0])
row_max_set = set() for i in range(m): row_max_set.update(row_dict[i]) for j in range(n): row_max_set.update(col_dict[j])
row_max = list(row_max_set) row_max.sort(reverse=True, key=lambda x: x[0])
res = -INF for i in range(len(row_max)): v1, r1, c1 = row_max[i] for j in range(i + 1, len(row_max)): v2, r2, c2 = row_max[j] if c1 == c2 or r1 == r2: continue for k in range(j + 1, len(row_max)): v3, r3, c3 = row_max[k] if r2 != r3 and r1 != r3 and c2 != c3 and c1 != c3: res = Math.max(res, v1 + v2 + v3) break
return res
|