当前位置:网站首页>1388.3n pizza dynamic planning
1388.3n pizza dynamic planning
2022-07-18 16:04:00 【Empress Yu】
Difficulty 104
Here is a pizza for you , It consists of 3n Blocks are made up of parts of different sizes , Now you and your friends need to divide pizza according to the following rules :
- You choose arbitrarily A piece of pizza .
- Alice You will choose the next pizza in the counterclockwise direction of the pizza you choose .
- Bob You will choose the pizza you choose, and the next pizza will be clockwise .
- Repeat the above process until there is no pizza left .
The size of each pizza is determined by a circular array in a clockwise direction
slicesExpress .Please return the maximum total pizza size you can get .
Example 1:
Input :slices = [1,2,3,4,5,6] Output :10 explain : Select a size of 4 Of pizza ,Alice and Bob Choose the size of 3 and 5 Of pizza . Then you choose the size 6 Of pizza ,Alice and Bob Choose the size of 2 and 1 Of pizza . The total size of pizza you get is 4 + 6 = 10 .Example 2:
Input :slices = [8,9,8,6,1,1] Output :16 explain : The size of both rounds is 8 Of pizza . If you choose the size 9 Of pizza , Your friends will choose the size 8 Of pizza , In this case, your total is not the largest .Tips :
1 <= slices.length <= 500slices.length % 3 == 01 <= slices[i] <= 1000
N
failed , Only after watching it ... I've thought of removing the head and tail twice , however x Choose from y I don't remember how to write T T. Press completely raid homes and plunder houses II If it comes to calculation , altogether 6 block , choose 1 3 5 You can't , Only choose 2 block .
Method : Dynamic programming
- Because the ring , You can't choose both ends , Take out the head and tail and calculate them once
- and raid homes and plunder houses II The difference is , There is a limit on the number of choices , You need to count one more layer . At present, we can start from quantity -1 Add the current , Or directly choose the same number as the previous
- For the first block , Choose a few are their own
class Solution {
public int maxSizeSlices(int[] slices) {
int n = slices.length;
return Math.max(find(slices,0,n-1),find(slices,1,n));
}
private int find(int[] slices, int start, int end){
int n = slices.length;
int v = n/3;
int[][] dp = new int[n][v+1];
for(int i = start; i < end; i++){
for(int j = 1; j <= v; j++){
dp[i][j] = Math.max(i==0?0:dp[i-1][j],slices[i]+(i>=2?dp[i-2][j-1]:0));
}
}
return Math.max(dp[n-1][v],dp[n-2][v]);
}
} Time complexity :O(n^2)
Spatial complexity :O(n^2)
边栏推荐
- 使用 tcpkill 阻断指定 TCP 连接的数据包
- Zhongguancun e valley · Su Gaoxin undertakes 2022 Suzhou China Japan South Korea high-level talent project roadshow competition
- ARTS_202207W1
- Big guy said * computing talk Club | Sanxingdui fantasy trip: an experience that only cloud computing can bring
- [training Day1] spy dispatch [minimum spanning tree]
- 1388. 3n 块披萨 动态规划
- Autojs learning TTS grabs voice red envelopes
- Solution to slow running card of VMware virtual machine
- uniapp扫码原生插件(Google MLKit、zxing;支持同时扫多个码)
- Start with a notice to prevent phishing emails
猜你喜欢

Uniapp code scanning native plug-ins (Google mlkit, zxing; support scanning multiple codes at the same time)

使用IDEA搭建WebService服务

可再生金融ReFi:提供对地球有利的技术和金融系统

What is the composition of CPU

Autojs learning - Application List

3个云渲染平台的价格体系,哪个最合适(一)

为健康增值,为时代赋能|仙乐健康发布年度可持续发展报告

面试秘籍大放送,编测编学独家秘籍遭外泄?!

Software testing interview: please talk about the most valuable bugs you found in your work?

cpu由什么组成
随机推荐
Kubedm install kubernetes 1.15 best practices
ARTS_202207W1
Graphic array calculation module numpy (trigonometric function, rounding function, converting radian to angle, statistical analysis function, median, array sorting, argsort(), lexport())
Do you know the meaning of call concurrency in okcc?
Power buckle ----- gemstones and stones
9. 说说hashCode() 和 equals() 之间的关系?
Uniapp code scanning native plug-ins (Google mlkit, zxing; support scanning multiple codes at the same time)
牛客网——华为题库(81~90)
数据库系统原理与应用教程(020)—— 登录 MySQL
Apache stress testing tool AB, with post parameter and token request
Excellent open source attack and defense weapon project of the whole network
ELK集群部署(十)之ELK服务化
#kubeadm安装Kubernetes 1.15最佳实践#
《名侦探柯南》1049话作画大崩坏 角色频频“变脸”
JS计算精度问题和及数据格式
RSS上手教程:聚合自己的信息收集渠道,RSSHub、FreshRSS、NetNewsWire
ValueError: The number of FixedLocator locations (7), usually from a call to set_ ticks, does not mat
CompressAI:基于pytorch的图像压缩框架使用
NFT players' consensus segmentation: money, community and culture
C language under custom type (enumeration, Union)

