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)
相关推荐
kingmax542120087 小时前
高中数学教师资格面试试讲稿:《直线的位置关系(例2)》
线性代数·算法·面试·矩阵·教师资格
小妖66610 小时前
力扣(LeetCode)- 74. 搜索二维矩阵
算法·leetcode·矩阵
Liangwei Lin10 小时前
洛谷 U311289 矩阵距离
线性代数·算法·矩阵
不穿格子的程序员1 天前
从零开始写算法——矩阵类题:矩阵置零 + 螺旋矩阵
线性代数·算法·矩阵
一水鉴天1 天前
专题讨论 类型理论和范畴理论之间的关系 之2 整体设计中的“闭” 解题和“位”问题 (ima.copilot)
线性代数·矩阵·mvc
Tezign_space1 天前
技术实战:Crocs如何构建AI驱动的智能内容矩阵,实现内容播放量提升470%?
大数据·人工智能·矩阵·aigc·内容运营·多智能体系统·智能内容矩阵
serve the people1 天前
TensorFlow 中雅可比矩阵计算方式
人工智能·矩阵·tensorflow
劈星斩月2 天前
线性代数-3Blue1Brown《线性代数的本质》矩阵与线性变换-三维空间(6)
线性代数·矩阵·三维空间线性变换
Rock_yzh2 天前
LeetCode算法刷题——54. 螺旋矩阵
数据结构·c++·学习·算法·leetcode·职场和发展·矩阵
凯子坚持 c3 天前
体系化AI开发方案:豆包新模型矩阵与PromptPilot自动化调优平台深度解析
人工智能·矩阵·自动化