首页 > 其他分享 >1502. Can Make Arithmetic Progression From Sequence

1502. Can Make Arithmetic Progression From Sequence

时间:2023-06-06 18:47:50浏览次数:36  
标签:arr elements return progression Progression Make 1502 false arithmetic

/**
 * 1502. Can Make Arithmetic Progression From Sequence
 * https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/description/
 * 
A sequence of numbers is called an arithmetic progression if the difference between any two consecutive elements is the same.
Given an array of numbers arr, return true if the array can be rearranged to form an arithmetic progression. Otherwise, return false.

Example 1:
Input: arr = [3,5,1]
Output: true
Explanation: We can reorder the elements as [1,3,5] or [5,3,1] with differences 2 and -2 respectively, between each consecutive elements.

Example 2:
Input: arr = [1,2,4]
Output: false
Explanation: There is no way to reorder the elements to obtain an arithmetic progression.
 
Constraints:
2 <= arr.length <= 1000
-106 <= arr[i] <= 106
 * 
*/

/* 
Solution: keep checking diff of every two nums, Time complexity:O(nlogn), Space complexity:O(n)
*/
function canMakeArithmeticProgression(arr: number[]): boolean {
    arr.sort((a, b) => a - b);
    let diff = arr[1] - arr[0];
    for (let i = 2; i < arr.length; i++) {
        if ((arr[i] - arr[i - 1]) != diff) {
            return false;
        }
    }
    return true;
};

 

标签:arr,elements,return,progression,Progression,Make,1502,false,arithmetic
From: https://www.cnblogs.com/johnnyzhao/p/17461404.html

相关文章

  • [LeetCode] 1347. Minimum Number of Steps to Make Two Strings Anagram 制造字母异
    Youaregiventwostringsofthesamelength s and t.Inonestepyoucanchoose anycharacter of t andreplaceitwith anothercharacter.Return theminimumnumberofsteps tomake t ananagramof s.An Anagram ofastringisastringthatco......
  • CMakeLists.txt 编写模板
     新建文件  CMakeLists.txt #设置cmake的最低版本cmake_minimum_required(VERSION2.8)#指定为C++11版本set(CMAKE_CXX_STANDARD11)#设置工程名称project(wss)message(${PROJECT_SOURCE_DIR})set(SRC_LIST${PROJECT_SOURCE_DIR}/src/websocket_s......
  • 简单Makefile文件编写
    简单编写单个C文件的Makefile文件,文件名为demo.cdemo.c文件如下:#include<stdio.h>intmain(){printf("hello,world!\n");return0;}编写Makefile文件如下:demo:demo.ogccdemo.o-odemodemo.o:demo.cgcc-cdemo.c-odemo.oclean:rm-rf......
  • ubuntu 20.04安装(升级)cmake
    ubuntu20.04安装(升级)cmake-知乎(zhihu.com)    ......
  • Generative AI 新世界 | 大语言模型(LLMs)在 Amazon SageMaker 上的动手实践
    在上一篇《GenerativeAI新世界:大型语言模型(LLMs)概述》中,我们一起探讨了大型语言模型的发展历史、语料来源、数据预处理流程策略、训练使用的网络架构、最新研究方向分析(AmazonTitan、LLaMA、PaLM-E等),以及在亚马逊云科技上进行大型语言模型训练的一些最佳落地实践等。本期文章......
  • CMakeLists --- install和uninstall
    install假设生成了以下几个文件:静态库target1,动态库target2,可执行文件target3 1.安装文件至指定位置#只安装静态库install(TARGETStarget1LIBRARYDESTINATIONlib)#安装静态库,动态库,可执行文件install(TARGETStarget1target2target3LIBRARYDESTINATIONli......
  • LLMs As Tool Makers:LLM制造工具
    最近,来自GoogleDeepMind,普林斯顿和斯坦福的顶尖华人团队提出了一种全新的框架。现在,LLM可以像人类一样制作自己的工具了!ChatGPT等大语言模型诞生以来,凭着强大的语言理解能力、生成能力、逻辑推理能力等,已经被人类玩出了花。而OpenAI公开GPT-4后,最大的惊喜之一,莫过于插件模式的......
  • [转]关于Visual Studio:如何使用cmake检测64位MSVC?
    1、如何使用cmake检测64位MSVC?2、关于VisualStudio:如何使用cmake检测64位MSVC?......
  • can't not find Node.js binary ''path",make sure Node.js is installed and in your
    vscode中node执行debug报错报错信息如下 思路1:检查node是否安装成功win+R输入cmd  能够正常显示版本号,则证明没有问题,接着换个思路 思路2:根据提示打开图示的'launch.json'文件,在页面补充 runtimeExecutable具体补充什么内容呢?在overflow中找到了nginx中需要补......
  • CMake入门
    CMakecmake的定义是--高级编译配置工具当多人用不同的语言或者编译器开发一个项目,最终要输出一个可执行文件或者共享库(dll,so等等)这时候就需要用到------CMake所有操作都是通过编译CMakeLists.txt来完成的官方网站是www.cmake.org学习CMake的目的,为将来处理大型的C/C++......