【算法】Maximum Split of Positive Even Integers 拆分成最多数目的正偶数之和 -上篇 贪心模拟

这篇具有很好参考价值的文章主要介绍了【算法】Maximum Split of Positive Even Integers 拆分成最多数目的正偶数之和 -上篇 贪心模拟。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Maximum Split of Positive Even Integers 拆分成最多数目的正偶数之和

问题描述:

给你一个整数 finalSum 。请你将它拆分成若干个 互不相同 的正偶数之和,且拆分出来的正偶数数目 最多 。

比方说,给你 finalSum = 12 ,那么这些拆分是 符合要求 的(互不相同的正偶数且和为 finalSum):(2 + 10) ,(2 + 4 + 6) 和 (4 + 8) 。它们中,(2 + 4 + 6) 包含最多数目的整数。注意 finalSum 不能拆分成 (2 + 2 + 4 + 4) ,因为拆分出来的整数必须互不相同。
请你返回一个整数数组,表示将整数拆分成 最多 数目的正偶数数组。如果没有办法将 finalSum 进行拆分,请你返回一个 空 数组。你可以按 任意 顺序返回这些整数。

1 < = f i n a l S u m < = 1 0 10 1 <= finalSum <= 10^{10} 1<=finalSum<=1010

分析

很多题解上来就是贪心的具体操作,作为菜鸡的我,贪心一直不是我的首选。

贪心算法作为一种算法思路,有的人说很简单,有的人说很难。因为贪心算法本身并不存在一种固定的模式,它往往是以局部的最优解,从而得到整体的最优解的思路。
但是有些情况下,局部的最优解,并不能保证整体的最优,这种场景下,贪心就不行了。
所以使用贪心算法解决问题,对问题的分析力,有比较高的要求。

首先问题要将num拆成偶数之和,如果可以拆分,那么num必须是even
因为要求最终的结果中偶数不能重复使用,而且拆分的偶数要最多
把可能的偶数全部列出来 2 , 4 , 6 , 8... 2 34 2,4,6,8...2^{34} 2,4,6,8...234
log ⁡ 2 ( 1 0 10 ) ≈ 33.21 \log_2(10^{10})\approx33.21 log2(1010)33.21

要从34个数中,选择k个组成num,最长的k,问题就可以解决了。

最简单暴力的方式就是枚举选择的方案数,因为每个元素只能用一次,那么方案数就等于 2 n 2^n 2n,上限大概 1 0 10 10^{10} 1010.
这个数据规模,暴力是走不通的,但是思路没问题,毕竟一切恐惧源于火力不足。

暴力之所以耗时太多,还是由于计算了非常多的无效组合,但是只要时间给够,最终也是能出结果的。

暴力走不通,就想到了二分,二分同样也会走到暴力的上,在n个数字中找k个恰好等于num,此外由于k不一定连续,二分也没法用

能想到的方法,由于时间复杂度不能用,二分也不行。就只能找找规律了。

因为num是偶数,那么它一开始一定可以拆成2个偶数x,y。比如[2,0],[2,2],但是要求必须是正偶数,且不相等。
所以num如果是2,或者4,就无法继续拆了,又或者num拆出了2,a,b,c,与x,假设x比较大如果继续拆,会与已经拆出的数产生重复,那么x就不能拆了。

特点1:为了拆出最多的偶数,那么较小的偶数就应该占多数。

那么就从最小的偶数2开始拆。
num=2,只能拆成2,
num=4,只能拆成4。
num=6,只能拆成2,4。
num=8,只能拆成2,6,当拆出2时,还有6,如果拆4,就重复了,只能不动,长度为2。
num=10,先拆2,余8,此时8继续拆成4,不行,拆6也不行,所以8也不能再拆了,所以只能是2,8,长度2.

num=12,先拆2,余10,再拆4,余6,此时再拆就是6,可以不动,长度3。

似乎这个思路得到的方案,不会有其他比它更优秀的方案了

所以通过循环模拟这个过程。

代码

public List<Long> maximumEvenSplit(long finalSum) {
        List<Long> res = new ArrayList<>();
        if (finalSum % 2 == 1) return res;
        for (long i = 2; i <= finalSum; i += 2) {
            finalSum -= i;
            if(finalSum <=i) i+=finalSum;
            res.add(i);
        }
        if (finalSum!=0);
        return res;
    }

时间复杂度 O ( N ) O(\sqrt{N}) O(N )

空间复杂度 O ( 1 ) O(1) O(1)

另一个公式解法, 下篇

Tag

Greedy

Math文章来源地址https://www.toymoban.com/news/detail-542328.html

到了这里,关于【算法】Maximum Split of Positive Even Integers 拆分成最多数目的正偶数之和 -上篇 贪心模拟的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • leetcode - 2616. Minimize the Maximum Difference of Pairs

    You are given a 0-indexed integer array nums and an integer p. Find p pairs of indices of nums such that the maximum difference amongst all the pairs is minimized. Also, ensure no index appears more than once amongst the p pairs. Note that for a pair of elements at the index i and j, the difference of this pair is |nums[i] - nums[j]|, where |x| represents th

    2024年02月13日
    浏览(31)
  • LeetCode646. Maximum Length of Pair Chain——动态规划

    You are given an array of n pairs pairs where pairs[i] = [lefti, righti] and lefti righti. A pair p2 = [c, d] follows a pair p1 = [a, b] if b c. A chain of pairs can be formed in this fashion. Return the length longest chain which can be formed. You do not need to use up all the given intervals. You can select pairs in any order. Example 1: Input: pairs = [[

    2024年02月22日
    浏览(31)
  • LeetCode 1921. Eliminate Maximum Number of Monsters【贪心,计数排序】1527

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2024年02月09日
    浏览(39)
  • LeetCode //C - 1161. Maximum Level Sum of a Binary Tree

    Given the root of a binary tree, the level of its root is 1, the level of its children is 2, and so on. Return the smallest level x such that the sum of all the values of nodes at level x is maximal.   Example 1: Input: root = [1,7,0,7,-8,null,null] Output: 2 **Explanation: ** Level 1 sum = 1. Level 2 sum = 7 + 0 = 7. Level 3 sum = 7 + -8 = -1. So we return

    2024年01月23日
    浏览(39)
  • LeetCode //C - 2130. Maximum Twin Sum of a Linked List

    In a linked list of size n, where n is even, the i t h i^{th} i t h node (0-indexed) of the linked list is known as the twin of the ( n − 1 − i ) t h (n-1-i)^{th} ( n − 1 − i ) t h node, if 0 = i = (n / 2) - 1. For example, if n = 4, then node 0 is the twin of node 3, and node 1 is the twin of node 2. These are the only nodes with twins for n = 4. Th

    2024年01月17日
    浏览(38)
  • leetcode - 1751. Maximum Number of Events That Can Be Attended II

    You are given an array of events where events[i] = [startDayi, endDayi, valuei]. The ith event starts at startDayi and ends at endDayi, and if you attend this event, you will receive a value of valuei. You are also given an integer k which represents the maximum number of events you can attend. You can only attend one event at a time. If you choose to attend

    2024年02月16日
    浏览(70)
  • 【我的Android进阶之旅】解决:The currently selected variant “debug“ uses split APKs, but none of the 1 split...

    在Github下载了一份代码,在本地运行看看效果,直接运行失败,如下所示: 错误描述如下所示: 翻译过来就是: 当前选择的变体“ debug ”使用拆分 APK ,但1个拆分APK中没有一个与当前具有 ABI “ armeabi-v7a,armeabi ”的设备兼容。 我的设备只支持 armeabi-v7a 或者 armeabi 代码中

    2024年02月13日
    浏览(33)
  • 【异常】The field file exceeds its maximum permitted size of 1048576 bytes.

    本项目是个Springboot 项目,功能是要做一个文件上传,在测试时发现报错,上传的是一个 word 文件,大小是 1.25MB,报错内容如下: Caused by: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 1048576 bytes. 详细报错内容如下图

    2024年03月15日
    浏览(31)
  • LeetCode 2496. Maximum Value of a String in an Array【字符串,数组】简单

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2024年02月11日
    浏览(40)
  • IOT chip IPQ9554/IPQ9514 support mesh maximum transfer rate of 4800 Mbps

    Today,  introduce the differences between the two wifi7 chips Chip IPQ9514 IPQ9514 is a wireless network chip launched by Qualcomm, which is mainly used in home routers and small and medium-sized enterprise wireless network devices. Powered by Qualcomm\\\'s Krait architecture, the IPQ9514 features four processor cores with a dominant frequency of 1.4GHz, integ

    2023年04月10日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包