首页 > 其他分享 >leetcode-1646-easy

leetcode-1646-easy

时间:2023-12-06 09:03:12浏览次数:44  
标签:1646 nums int easy integer leetcode

Get Maximum in Generated Array

You are given an integer n. A 0-indexed integer array nums of length n + 1 is generated in the following way:

nums[0] = 0
nums[1] = 1
nums[2 * i] = nums[i] when 2 <= 2 * i <= n
nums[2 * i + 1] = nums[i] + nums[i + 1] when 2 <= 2 * i + 1 <= n
Return the maximum integer in the array nums​​​.

Example 1:

Input: n = 7
Output: 3
Explanation: According to the given rules:
  nums[0] = 0
  nums[1] = 1
  nums[(1 * 2) = 2] = nums[1] = 1
  nums[(1 * 2) + 1 = 3] = nums[1] + nums[2] = 1 + 1 = 2
  nums[(2 * 2) = 4] = nums[2] = 1
  nums[(2 * 2) + 1 = 5] = nums[2] + nums[3] = 1 + 2 = 3
  nums[(3 * 2) = 6] = nums[3] = 2
  nums[(3 * 2) + 1 = 7] = nums[3] + nums[4] = 2 + 1 = 3
Hence, nums = [0,1,1,2,1,3,2,3], and the maximum is max(0,1,1,2,1,3,2,3) = 3.
Example 2:

Input: n = 2
Output: 1
Explanation: According to the given rules, nums = [0,1,1]. The maximum is max(0,1,1) = 1.
Example 3:

Input: n = 3
Output: 2
Explanation: According to the given rules, nums = [0,1,1,2]. The maximum is max(0,1,1,2) = 2.
Constraints:

0 <= n <= 100

思路一:遍历

    public int getMaximumGenerated(int n) {
        if (n < 2) {
            return n;
        }
        
        int[] nums = new int[n + 1];

        nums[0] = 0;
        nums[1] = 1;
        for (int i = 2; i <= n; i++) {
            if ((i & 1) == 1) {
                int x = (i - 1) / 2;
                nums[i] = nums[x] + nums[x + 1];
            } else {
                nums[i] = nums[i / 2];
            }
        }

        int max = 0;
        for (int num : nums) {
            max = Math.max(max, num);
        }

        return max;
    }

标签:1646,nums,int,easy,integer,leetcode
From: https://www.cnblogs.com/iyiluo/p/17878736.html

相关文章

  • leetcode-1464-easy
    MaximumProductofTwoElementsinanArrayGiventhearrayofintegersnums,youwillchoosetwodifferentindicesiandjofthatarray.Returnthemaximumvalueof(nums[i]-1)*(nums[j]-1).Example1:Input:nums=[3,4,5,2]Output:12Explanation:I......
  • leetcode-1455-easy
    CheckIfaWordOccursAsaPrefixofAnyWordinaSentenceGivenasentencethatconsistsofsomewordsseparatedbyasinglespace,andasearchWord,checkifsearchWordisaprefixofanywordinsentence.Returntheindexofthewordinsentence(1-......
  • leetcode-2180-easy
    CountIntegersWithEvenDigitSumGivenapositiveintegernum,returnthenumberofpositiveintegerslessthanorequaltonumwhosedigitsumsareeven.Thedigitsumofapositiveintegeristhesumofallitsdigits.Example1:Input:num=4Ou......
  • leetcode-1572-easy
    MatrixDiagonalSumGivenasquarematrixmat,returnthesumofthematrixdiagonals.Onlyincludethesumofalltheelementsontheprimarydiagonalandalltheelementsonthesecondarydiagonalthatarenotpartoftheprimarydiagonal.Example1:......
  • leetcode-1550-easy
    ThreeConsecutiveOddsGivenanintegerarrayarr,returntrueiftherearethreeconsecutiveoddnumbersinthearray.Otherwise,returnfalse.Example1:Input:arr=[2,6,4,1]Output:falseExplanation:Therearenothreeconsecutiveodds.Example2:......
  • leetcode-1512-easy
    NumberofGoodPairsGivenanarrayofintegersnums,returnthenumberofgoodpairs.Apair(i,j)iscalledgoodifnums[i]==nums[j]andi<j.Example1:Input:nums=[1,2,3,1,1,3]Output:4Explanation:Thereare4goodpairs(0,3),(0,4),(......
  • CF1824B1 LuoTianyi and the Floating Islands (Easy Version) 题解
    题意:思路:由于$k∈[1,3]$,分类讨论:当$k=1$时,有人结点自身即为好结点,每种情况的期望为$\frac{1}{n}$,$n$种情况的期望和为$1$。最终答案即为$1$。当$k=2$时,$2$个有人结点之间的路径上的结点即为好结点,那么问题转化为:树上所有路径的结点......
  • C1. Good Subarrays (Easy Version)
    思路:我们枚举每一个左端点,对于每一个左端点,寻找最长的满足条件的区间,这个区间长度就是左端点对答案的贡献,可以发现具有单调性,右端点只会前进不会倒退。所以我们两个指针各扫一遍区间就可以。#include<bits/stdc++.h>#definelsp<<1#definersp<<1|1#definePIIpair<int,......
  • [LeetCode Hot 100] LeetCode19. 删除链表的倒数第N个结点
    题目描述思路一:采用两次遍历第一遍遍历先获取链表的长度length第二次从dummy节点开始走length-n步然后将该节点指向下下个节点思路二:采用一次遍历设置虚拟节点dummyHead指向head设定双指针p和q,初始都指向虚拟节点dummyHead移动q,直到p与q之间相隔的元素个数为n(即q走......
  • [LeetCode Hot 100] LeetCode21. 合并两个有序链表
    题目描述思路:新建dummy去"穿针引线"新建一个dummy节点去"穿针引线"注意最后返回的是dummy.next方法一:/***Definitionforsingly-linkedlist.*publicclassListNode{*intval;*ListNodenext;*ListNode(){}*ListNode(intval){this......