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)
相关推荐
余~~185381628007 小时前
矩阵碰一碰发视频的后端源码技术,支持OEM
线性代数·矩阵·音视频
CoderCodingNo8 小时前
【GESP】C++二级真题 luogu-b3924, [GESP202312 二级] 小杨的H字矩阵
java·c++·矩阵
@ V:ZwaitY098 小时前
如何打造TikTok矩阵:多账号管理与内容引流的高效策略
人工智能·矩阵·tiktok
_Itachi__8 小时前
LeetCode 热题 100 73. 矩阵置零
算法·leetcode·矩阵
01_9 小时前
力扣hot100 ——搜索二维矩阵 || m+n复杂度优化解法
算法·leetcode·矩阵
curemoon1 天前
理解都远正态分布中指数项的精度矩阵(协方差逆矩阵)
人工智能·算法·矩阵
和光同尘@1 天前
74. 搜索二维矩阵(LeetCode 热题 100)
数据结构·c++·线性代数·算法·leetcode·职场和发展·矩阵
Vacant Seat1 天前
矩阵-矩阵置零
java·矩阵·二维数组
跨境卫士小树1 天前
店铺矩阵崩塌前夜:跨境多账号运营的3个生死线
大数据·线性代数·矩阵
BingLin-Liu2 天前
蓝桥杯备考:贪心算法之矩阵消除游戏
游戏·贪心算法·矩阵