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)
相关推荐
whitelbwwww4 天前
cuBLAS--CUDA高性能线性代数计算库--第一部分
c++·线性代数·gpu算力·cuda c++
JianZhen✓5 天前
电商 SKU 选择器:后端只给扁平列表,前端怎么反推规格矩阵并做到 O(1) 命中
前端·矩阵·状态模式
tellmewhoisi8 天前
深度学习0:数学知识:(雅可比矩阵)
人工智能·深度学习·矩阵
lisw0514 天前
Dayhoff的PAM替换矩阵理论评介!
线性代数·算法·矩阵
DFT计算杂谈15 天前
可DIY布里渊区图像绘制编辑器
人工智能·机器学习·语言模型·矩阵·编辑器
Pniubi15 天前
力扣54.螺旋矩阵
算法·leetcode·矩阵
Pniubi15 天前
力扣73 矩阵置零
算法·leetcode·矩阵
kingx316 天前
Bugku 逆向 MatrixHunter:黑盒 oracle + GF(2) 消元出唯一解
网络安全·矩阵·逆向·ctf
莱茶荼菜20 天前
【slam公式】
人工智能·线性代数·机器学习