Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.

 

Example 1:

Input: [3,1,3,6]
Output: false

Example 2:

Input: [2,1,2,6]
Output: false

Example 3:

Input: [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].

Example 4:

Input: [1,2,4,16,8,4]
Output: false

Note:

  1. 0 <= A.length <= 30000
  2. A.length is even
  3. -100000 <= A[i] <= 100000

Idea 1. Kind of counting sort + hashMap

Time complexity: O(n + m) where m = max(A), n = A.length

Space complexity: O(m)

 class Solution {
public boolean canReorderDoubled(int[] A) {
int n = 20000;
int[] negatives = new int[n+1];
int[] positives = new int[n+1]; for(int a: A) {
if(a < 0) {
++negatives[-a];
}
else {
++positives[a];
}
} for(int i = 0; i <= n; ++i) {
if(negatives[i] != 0) {
if(negatives[2*i] < negatives[i]) {
return false;
}
else {
negatives[2*i] -= negatives[i];
}
} if(positives[i] != 0) {
if(positives[2*i] < positives[i]) {
return false;
}
else {
positives[2*i] -= positives[i];
}
}
} return true;
}
}

Idea 1.a TreeMap + absoluate value as comparator, no need to deal with /2 for negative values

Time complexity: O(nlogn)

Space complexity: O(n)

 class Solution {
public boolean canReorderDoubled(int[] A) {
Comparator<Integer> comparator = (Integer a, Integer b) -> {
int absEqual = Integer.compare(Math.abs(a), Math.abs(b));
if(absEqual == 0 && a != b) {
return Integer.compare(a, b);
}
return absEqual;
}; Map<Integer, Integer> count = new TreeMap<>(comparator); for(int a : A) {
count.put(a, count.getOrDefault(a, 0) + 1);
} for(int key: count.keySet()) {
int want = count.getOrDefault(2*key, 0);
if(count.get(key) > want) {
return false;
}
else if(count.containsKey(2*key)) {
count.put(2*key, want - count.get(key));
}
} return true;
}
}

Idea1.c normal treeMap with both positive + negatives

 class Solution {
public boolean canReorderDoubled(int[] A) {
Map<Integer, Integer> count = new TreeMap<>(); for(int a : A) {
count.put(a, count.getOrDefault(a, 0) + 1);
} for(int key: count.keySet()) {
if(count.get(key) == 0) {
continue;
} int next = key < 0? key/2 : key*2;
int want = count.getOrDefault(next, 0);
if(count.get(key) > want) {
return false;
} count.put(next, want - count.get(key)); } return true;
}
}

Array of Doubled Pairs LT954的更多相关文章

  1. LC 954. Array of Doubled Pairs

    Given an array of integers A with even length, return true if and only if it is possible to reorder ...

  2. [Swift]LeetCode954. 二倍数对数组 | Array of Doubled Pairs

    Given an array of integers A with even length, return true if and only if it is possible to reorder ...

  3. 114th LeetCode Weekly Contest Array of Doubled Pairs

    Given an array of integers A with even length, return true if and only if it is possible to reorder ...

  4. Array of Doubled Pairs

    Given an array of integers A with even length, return true if and only if it is possible to reorder ...

  5. 【leetcode】954. Array of Doubled Pairs

    题目如下: Given an array of integers A with even length, return true if and only if it is possible to re ...

  6. 【LeetCode】954. Array of Doubled Pairs 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. 算法与数据结构基础 - 数组(Array)

    数组基础 数组是最基础的数据结构,特点是O(1)时间读取任意下标元素,经常应用于排序(Sort).双指针(Two Pointers).二分查找(Binary Search).动态规划(DP)等算法.顺 ...

  8. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  9. Weekly Contest 114

    955. Delete Columns to Make Sorted II We are given an array A of N lowercase letter strings, all of ...

随机推荐

  1. DataSource - 常用数据库连接池 (DBCP、c3p0、Druid) 配置说明

    1. 引言 1.1 定义 数据库连接是一种关键的有限的昂贵的资源,这一点在多用户的网页应用程序中体现得尤为突出.对数据库连接的管理能显著影响到整个应用程序的伸缩性和健壮性,影响到程序的性能指标.数据库 ...

  2. python3学习笔记四(列表1)

    参考http://www.runoob.com/python3/python3-list.html 序列 python包含6种内建的序列:列表,元组,字符串,Unicode字符串,buffer对象和x ...

  3. redis高可用(主从复制)

    熟练掌握redis需要从 reids如何操作5种基本数据类型,redis如何集群,reids主从复制,redis哨兵机制redis持久化 reids主从复制 的作用可以:实现数据备份,读写分离,集群, ...

  4. Qt中三种解析xml的方式

    在下面的随笔中,我会根据xml的结构,给出Qt中解析这个xml的三种方式的代码.虽然,这个代码时通过调用Qt的函数实现的,但是,很多开源的C++解析xml的库,甚至很多其他语言解析xml的库,都和下面 ...

  5. 手动卸载CAD 删除残留文件 清理遗留的文件

    手动卸载基于 AutoCAD 产品的文件,从而删除所有残留文件. 清理安装失败所遗留的文件. 一.解决方案: 通过"控制面板"卸载该程序. 删除以下位置残留的 AutoCAD 文件 ...

  6. 源码小结:Java 集合ArrayList,LinkedList 源码

    现在这篇主要讲List集合的三个子类: ArrayList 底层数据结构是数组.线程不安全 LinkedList 底层数据结构是链表.线程不安全 Vector 底层数据结构是数组.线程安全 Array ...

  7. Vue非父子组件之间的传值

    1.新建一个js文件  然后引入vue 实例化vue 最后暴露这个实例:实例化Vue对象的时候名称要小写,大写控制台报错,我也不知道什么原因: 2.在要广播的地方引入刚才定义的实例: 3通过VueEm ...

  8. 【HQL】小技巧

    case1.a与b匹配表保留一条匹配关系 背景:匹配b,b匹配a在同一张表: match_table表为: uid,m_uid 111,222 222,111 需求:只保留一条匹配关系. 结果为: u ...

  9. css:清楚html所有标签自带属性

    相信如果您动手写过网页的话,应该体会到有些标签会自带一些默认的样式,而这些样式或许又是我们不想要的,所以我们可以用以下代码清除所有标签的默认样式   html, body, div, span, ap ...

  10. win7频繁提示资源管理器已停止工作解决办法

    方法一,重新启动资源管理器,右键点击桌面下方的“任务栏”空白区,在弹出的菜单栏中选择“任务管理器”.   进入任务管理器,点击上方的“文件”,选择新建任务.   在弹出的对话框中,输入explorer ...