Given a sorted array of integers a, find an integer x from a such that the value of

  1. abs(a[0] - x) + abs(a[1] - x) + ... + abs(a[a.length - 1] - x)

is the smallest possible (here abs denotes the absolute value).
If there are several possible answers, output the smallest one.

Example

For a = [2, 4, 7], the output should be
absoluteValuesSumMinimization(a) = 4.

我的解答:

  1. def absoluteValuesSumMinimization(a):
  2. return a[len(a)//2] if len(a) % 2 == 1 else a[len(a)//2-1]
  1. def absoluteValuesSumMinimization(A):
  2. return A[(len(A)-1)//2]

膜拜大佬

Code Signal_练习题_absoluteValuesSumMinimization的更多相关文章

  1. Code Signal_练习题_digitDegree

    Let's define digit degree of some positive integer as the number of times we need to replace this nu ...

  2. Code Signal_练习题_Knapsack Light

    You found two items in a treasure chest! The first item weighs weight1 and is worth value1, and the ...

  3. Code Signal_练习题_growingPlant

    Each day a plant is growing by upSpeed meters. Each night that plant's height decreases by downSpeed ...

  4. Code Signal_练习题_arrayMaxConsecutiveSum

    Given array of integers, find the maximal possible sum of some of its k consecutive elements. Exampl ...

  5. Code Signal_练习题_differentSymbolsNaive

    Given a string, find the number of different characters in it. Example For s = "cabca", th ...

  6. Code Signal_练习题_firstDigit

    Find the leftmost digit that occurs in a given string. Example For inputString = "var_1__Int&qu ...

  7. Code Signal_练习题_extractEachKth

    Given array of integers, remove each kth element from it. Example For inputArray = [1, 2, 3, 4, 5, 6 ...

  8. Code Signal_练习题_stringsRearrangement

    Given an array of equal-length strings, check if it is possible to rearrange the strings in such a w ...

  9. Code Signal_练习题_depositProfit

    You have deposited a specific amount of money into your bank account. Each year your balance increas ...

随机推荐

  1. Iframe高度自适应(兼容IE/Firefox、同域/跨域)

    在实际的项目进行中,很多地方可能由于历史原因不得不去使用iframe,包括目前正火热的应用开发也是如此. 随之而来的就是在实际使用iframe中,会遇到iframe高度的问题,由于被嵌套的页面长度不固 ...

  2. Redis---List(链表)

    1. 基本结构 Redis 早期版本存储 list 列表数据结构使用的是压缩列表 ziplist 和普通的双向链表 linkedlist,也就是元素少时用 ziplist,元素多时用 linkedli ...

  3. Html 常见meta

    html 的meta标签对网页渲染及SEO搜索引擎起着不可忽视的作用.详细的写法一段时间不写,容易忘,所以整理了一下,方便需要时查看. <!DOCTYPE html> <!-- 使用 ...

  4. python批量拷贝文件

    普通批量拷贝文件 import os import shutil import logging from logging import handlers from colorama import Fo ...

  5. cFSMN和FSMN参数规模对比分析

    1. FSMN参数规模分析        (1)分析前提: 假设隐藏层单元规模都为n 只分析前向t个时刻的结构,即暂时不考虑双向的结构 只分析向量系数编码,即vFSMN,暂时不考虑sFSMN     ...

  6. Django 模版过滤器

    模版常用过滤器 在模版中,有时候需要对一些数据进行处理以后才能使用.一般在Python中我们是通过函数的形式来完成的.而在模版中,则是通过过滤器来实现的.过滤器使用的是|来使用.比如使用add过滤器, ...

  7. fastjson的JSONArray转化为泛型列表

    背景:一个复杂结构体内部可能有array的数据,例如:{name:"test",cities:[{name:"shanghai",area:1,code:200 ...

  8. c++拷贝构造函数(深拷贝、浅拷贝)——转

    拷贝构造函数: 拷贝构造函数是一种特殊的构造函数,函数的名称必须和类名称一致,它的唯一的一个参数是本类的一个引用变量,该参数是const类型,不可变的.例如:类A的拷贝构造函数的形式为A(A& ...

  9. 在Ubuntu Server上安装Postgresql

    首先更新一下源: sudo apt-get update 如果你不知道Postgresql具体的包的名称,可以使用一下语句进行查找: apt-cache search ^Postgresql 使用上述 ...

  10. php完美匹配邮箱、链接地址和电话号码

    php完美匹配邮箱.链接地址和电话号码 写了好一会有问题,朋友这边很好功能,借用了.嘎嘎 //31日 更新: 匹配手机以及电话号码 重新修改,可支持18开头的手机号,并修改bug,可匹配出字符串中所有 ...