当前位置:网站首页>Leetcode week 4: maximum sum of arrays (shape pressing DP bit operation)
Leetcode week 4: maximum sum of arrays (shape pressing DP bit operation)
2022-07-03 22:16:00 【White speed Dragon King's review】
Ideas :
1. Use bit operation to compress the state of basket vacancy
2. Use dp, To represent in a given state , front i Maximum and sum of
src:
class Solution:
def maximumANDSum(self, nums: List[int], numSlots: int) -> int:
# f(i, mask) Show consideration nums In front of i It's an integer , Basket availability status is mask
@lru_cache(None)
def f(i, mask):
if i < 0:
return 0
t, w, res = mask, 1, 0
# Traverse the basket
for k in range(1, numSlots + 1):
# Ruodi k Baskets are still available
if t % 3:
# Choose him , And the previous results
res = max(res, f(i - 1, mask - w) + (k & nums[i]))
# Consider the next basket
t, w = t // 3, w * 3
return res
return f(len(nums) - 1, 3 ** numSlots - 1)
summary :
[email protected]_cache Memory search
2. use n Binary to compress the state (n States )
3. Traverse all possible , Take out the biggest
边栏推荐
- Team collaborative combat penetration tool CS artifact cobalt strike
- Harbor integrated LDAP authentication
- Electronic tube: Literature Research on basic characteristics of 6j1
- Cesium terrain clipping draw polygon clipping
- UC Berkeley proposes a multitask framework slip
- How PHP adds two numbers
- WFC900M-Network_ Card/Qualcomm-Atheros-AR9582-2T-2R-MIMO-802.11-N-900M-high-power-Mini-PCIe-Wi-Fi-Mod
- 十大券商开户注册安全靠谱吗?有没有风险的?
- What is the difference between res.send() and res.end() in the node express framework
- Rest reference
猜你喜欢
Buuctf, misc: n solutions
2022 safety officer-b certificate examination summary and safety officer-b certificate simulation test questions
Summary of basic knowledge of exception handling
Bluebridge cup Guoxin Changtian single chip microcomputer -- detailed explanation of schematic diagram (IV)
Data consistency between redis and database
Teach you how to install aidlux (1 installation)
Blue Bridge Cup Guoxin Changtian MCU -- program download (III)
Functions and differences between static and Const
Correlation
Station B, dark horse programmer, employee management system, access conflict related (there is an unhandled exception at 0x00007ff633a4c54d (in employee management system.Exe): 0xc0000005: read locat
随机推荐
SDNU_ ACM_ ICPC_ 2022_ Winter_ Practice_ 4th [individual]
[sg function] 2021 Niuke winter vacation training camp 6 h. winter messenger 2
IPhone development swift foundation 09 assets
1 Introduction to spark Foundation
Why should enterprises do more application activities?
使用dnSpy對無源碼EXE或DLL進行反編譯並且修改
The White House held an open source security summit, attended by many technology giants
China's Call Center Industry 14th five year plan direction and operation analysis report Ⓔ 2022 ~ 2028
js demo 計算本年度還剩下多少天
Unique in China! Alibaba cloud container service enters the Forrester leader quadrant
English topic assignment (28)
[dynamic programming] Jisuan Ke: Jumping stake (variant of the longest increasing subsequence)
Netfilter ARP log
How to obtain opensea data through opensea JS
Rest参考
Buuctf, misc: sniffed traffic
Introduction to kubernetes
Redis single thread and multi thread
What indicators should be paid attention to in current limit monitoring?
2 spark environment setup local