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)
相关推荐
撩妹小狗4 天前
科赫雪花--Python--数学原理--turtle绘图
python·线性代数·几何学
张3蜂4 天前
线性代数与AI的关系
人工智能·线性代数
汉克老师4 天前
GESP2023年12月认证C++二级( 第三部分编程题(2) 小杨的H字矩阵)
c++·算法·矩阵·循环结构·gesp二级·gesp2级
AI科技星4 天前
物理世界的几何建构:论统一场论的本体论革命与概念生成
人工智能·opencv·线性代数·算法·矩阵
没有bug.的程序员4 天前
订单系统重构史诗:从单体巨兽到微服务矩阵的演进、数据一致性内核与分布式事务
java·微服务·矩阵·重构·分布式事务·数据一致性·订单系统
super_lzb4 天前
【线性代数】矩阵第一讲:矩阵与矩阵的运算
线性代数·矩阵·考研数学·矩阵的计算
newbiai4 天前
TikTok矩阵账号引流怎么解决效率低成本高?
python·线性代数·矩阵
逆境不可逃4 天前
【从零入门23种设计模式08】结构型之组合模式(含电商业务场景)
线性代数·算法·设计模式·职场和发展·矩阵·组合模式
菜鸡儿齐5 天前
leetcode-搜索二维矩阵
算法·leetcode·矩阵
炽烈小老头5 天前
【每天学习一点算法 2026/02/24】矩阵置零
学习·算法·矩阵