3276. 选择矩阵中单元格的最大得分

Powered by:NEFU AB-IN

Link

文章目录

  • [3276. 选择矩阵中单元格的最大得分](#3276. 选择矩阵中单元格的最大得分)

3276. 选择矩阵中单元格的最大得分

题意

给你一个由正整数构成的二维矩阵 grid。

你需要从矩阵中选择 一个或多个 单元格,选中的单元格应满足以下条件:

所选单元格中的任意两个单元格都不会处于矩阵的 同一行。

所选单元格的值 互不相同。

你的得分为所选单元格值的总和。

返回你能获得的 最大 得分。

思路

值域dp + 状压

换角度 ,因为数是1到100,状态就是 2 100 2^{100} 2100太多了,所以换集合的状态:

每一排选哪个数字 ,到我这个数字从哪些排里选,这样状态依赖于排的数量

所以dfs的状态为:

dfs(i, j) = max(dfs(i - 1, j), dfs(i - 1, j | {k}) + i)

i表示从[1, i]数字中选,j表示一个集合,这个集合中的行不能选

代码

python 复制代码
'''
Author: NEFU AB-IN
Date: 2024-09-01 10:10:28
FilePath: \LeetCode\CP413\c\c.py
LastEditTime: 2024-09-02 11:46:36
'''
import random
from collections import Counter, defaultdict, deque
# 3.8.9 import
from curses.ascii import SO
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

# Constants
TYPE = TypeVar('TYPE')
N = int(2e5 + 10)
M = int(20)
INF = int(1e12)
OFFSET = int(100)
MOD = int(1e9 + 7)

# Set recursion limit
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 Std:
    pass

# --------------------------------------------------------------- Division line ------------------------------------------------------------------


class Solution:
    def maxScore(self, grid: List[List[int]]) -> int:
        n, m = len(grid), len(grid[0])

        mx = 0
        cnt_ = defaultdict(set)
        for i, row in enumerate(grid):
            for j, num in enumerate(row):
                cnt_[num].add(i)
                mx = Math.max(mx, num)

        # 在 [1,i] 中选数,已选的行号集合为 j
        @lru_cache(None)
        def dfs(i: int, j: int):
            if i == 0:
                return 0
            ans = dfs(i - 1, j)

            for num in cnt_.keys():
                if num > i:
                    continue
                for row in cnt_[num]:
                    if not (j >> row & 1):
                        ans = Math.max(ans, dfs(num - 1, j | (1 << row)) + num)
            return ans

        return dfs(mx, 0)
相关推荐
POLITE31 小时前
Leetcode 54.螺旋矩阵 JavaScript (Day 8)
javascript·leetcode·矩阵
鲨莎分不晴2 小时前
从“像素对”到“纹理感”:深度解析灰度共生矩阵 (GLCM)
线性代数·矩阵
轻微的风格艾丝凡2 小时前
模型拆解--Variable Inductance Modeling
线性代数·simulink
雾喔4 小时前
1970. 你能穿过矩阵的最后一天 + 今年总结
线性代数·算法·矩阵
AI科技星15 小时前
张祥前统一场论宇宙大统一方程的求导验证
服务器·人工智能·科技·线性代数·算法·生活
JinSu_1 天前
【学习体会】Eigen和GLM在矩阵初始化和底层数据存储的差异
线性代数·矩阵
寂寞恋上夜1 天前
PRD权限矩阵怎么写:RBAC模型+5个真实案例
数据库·人工智能·矩阵·deepseek ai·markdown转xmind·ai思维导图生成器
wa的一声哭了1 天前
赋范空间 赋范空间的完备性
python·线性代数·算法·机器学习·数学建模·矩阵·django
短视频矩阵源码定制1 天前
专业的矩阵系统哪家强
线性代数·矩阵
Tisfy1 天前
LeetCode 840.矩阵中的幻方:模拟(+小小位运算)
算法·leetcode·矩阵