当前位置:网站首页>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
边栏推荐
- Control loop of program (while loop)
- Why use pycharm to run the use case successfully but cannot exit?
- What is the content of the securities practice examination?
- Yyds dry goods inventory Spring Festival "make" your own fireworks
- The White House held an open source security summit, attended by many technology giants
- Report on the development status and investment planning trends of China's data center industry Ⓡ 2022 ~ 2028
- JS notes (III)
- IPhone development swift foundation 08 encryption and security
- Common problems in multi-threaded learning (I) ArrayList under high concurrency and weird hasmap under concurrency
- [sg function] 2021 Niuke winter vacation training camp 6 h. winter messenger 2
猜你喜欢

Dahua series books

Minio deployment

The latest analysis of R1 quick opening pressure vessel operation in 2022 and the examination question bank of R1 quick opening pressure vessel operation

4 environment construction -standalone ha
![[flax high frequency question] leetcode 426 Convert binary search tree to sorted double linked list](/img/db/b992d2b461ca17652518a1511b4947.gif)
[flax high frequency question] leetcode 426 Convert binary search tree to sorted double linked list
![[actual combat record] record the whole process of the server being attacked (redis vulnerability)](/img/9c/34b916aca2f9270ec4cf4651f0de7e.jpg)
[actual combat record] record the whole process of the server being attacked (redis vulnerability)

string

1068. Consolidation of ring stones (ring, interval DP)

Décompiler et modifier un exe ou une DLL non source en utilisant dnspy

320. Energy Necklace (ring, interval DP)
随机推荐
使用dnSpy對無源碼EXE或DLL進行反編譯並且修改
Compréhension de la technologie gslb (Global Server load balance)
[dynamic planning] counting garlic customers: the log of garlic King (the longest increasing public subsequence)
Implementation principle of inheritance, encapsulation and polymorphism
[secretly kill little partner pytorch20 days] - [day3] - [example of text data modeling process]
English topic assignment (28)
Cesium terrain clipping draw polygon clipping
The White House held an open source security summit, attended by many technology giants
Sow of PMP
Asynchronous artifact: implementation principle and usage scenario of completable future
Rest参考
Common problems in multi-threaded learning (I) ArrayList under high concurrency and weird hasmap under concurrency
Morning flowers and evening flowers
UC Berkeley proposes a multitask framework slip
Kali2021.4a build PWN environment
Buuctf, web:[geek challenge 2019] buyflag
Base ring tree Cartesian tree
Cognitive fallacy: Wittgenstein's ruler
Mysql database - Advanced SQL statement (I)
Exclusive interview with the person in charge of openkruise: to what extent has cloud native application automation developed now?