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

leetcode-1464-easy

时间:2023-12-06 09:02:48浏览次数:31  
标签:12 nums int Example Output easy Input 1464 leetcode

Maximum Product of Two Elements in an Array
Given the array of integers nums, you will choose two different indices i and j of that array. Return the maximum value of (nums[i]-1)*(nums[j]-1).

Example 1:

Input: nums = [3,4,5,2]
Output: 12 
Explanation: If you choose the indices i=1 and j=2 (indexed from 0), you will get the maximum value, that is, (nums[1]-1)*(nums[2]-1) = (4-1)*(5-1) = 3*4 = 12. 
Example 2:

Input: nums = [1,5,4,5]
Output: 16
Explanation: Choosing the indices i=1 and j=3 (indexed from 0), you will get the maximum value of (5-1)*(5-1) = 16.
Example 3:

Input: nums = [3,7]
Output: 12
Constraints:

2 <= nums.length <= 500
1 <= nums[i] <= 10^3

思路一:冒泡排序,整个数组只排两次

    public int maxProduct(int[] nums) {
        for (int i = 0; i < 2; i++) {
            for (int j = 1; j < nums.length; j++) {
                if (nums[j] < nums[j - 1]) {
                    int temp = nums[j];
                    nums[j] = nums[j - 1];
                    nums[j - 1] = temp;
                }
            }
        }

        return (nums[nums.length - 1] - 1) * (nums[nums.length - 2] - 1);
    }

标签:12,nums,int,Example,Output,easy,Input,1464,leetcode
From: https://www.cnblogs.com/iyiluo/p/17878742.html

相关文章

  • 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......
  • 视频汇聚/音视频流媒体视频平台/视频监控EasyCVR分享页面无法播放,该如何解决?
    国标GB28181安防视频监控/视频集中存储/云存储EasyCVR平台可拓展性强、视频能力灵活、部署轻快,可支持的主流标准协议有国标GB28181、RTSP/Onvif、RTMP等,以及支持厂家私有协议与SDK接入,包括海康Ehome、海大宇等设备的SDK等。平台既具备传统安防视频监控的能力,也具备接入AI智能分析的......