Array of Doubled Pairs LT954
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:
0 <= A.length <= 30000A.lengthis even-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的更多相关文章
- 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 ...
- [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 ...
- 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 ...
- Array of Doubled Pairs
Given an array of integers A with even length, return true if and only if it is possible to reorder ...
- 【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 ...
- 【LeetCode】954. Array of Doubled Pairs 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 算法与数据结构基础 - 数组(Array)
数组基础 数组是最基础的数据结构,特点是O(1)时间读取任意下标元素,经常应用于排序(Sort).双指针(Two Pointers).二分查找(Binary Search).动态规划(DP)等算法.顺 ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- Weekly Contest 114
955. Delete Columns to Make Sorted II We are given an array A of N lowercase letter strings, all of ...
随机推荐
- Flask--(项目准备)--框架搭建,参数配置
项目准备: 配置参数 项目配置: 新建工程: 配置虚拟环境: 通过配置类添加配置参数: Debug配置, 初始化数据库对象, Mysql配置及数据库创建 redis配置: 端口6379和域名: 创建存 ...
- Xamarin+Prism开发详解七:Plugin开发与打包测试
有了上章[Xamarin+Prism开发详解六:DependencyService与IPlatformInitializer的关系]的基础,现在来理解Plugin开发就简单了. 本文实例代码地址:ht ...
- Linux操作redis 使用(VMwareWorkstation)
项目一般都部署到linux上面,记得刚出来的时候,第一家公司 服务器是windows系统,以后公司的项目都放在了linux上面,所以掌握linux的一些基本操作是一个程序员必备的知识,本次记录如何使用 ...
- python:推导式套路
推导式套路 列表推导式为例的推导式详细格式,同样适用于其他推导式 variable = [out_exp_res for out_exp in input_list if out_exp == 2] ...
- springboot 的war包在Tomcat中启动失败
springboot 默认是通常是打包成jar的,里面会内置一个tomcat容器 有时候我们需要使用以前打成war包的方式部署到对应的tomcat中, 具体springboot 怎么从jar改成war ...
- 安装ORACLE高可用RAC集群11g执行root脚本的输出信息
安装ORACLE高可用RAC集群11g执行root脚本的输出信息 作者:Eric 微信:loveoracle11g [root@node1 ~]# /u01/app/oraInventory/orai ...
- NodeJS学习之win10安装与sublime配置
Window 上安装Node.js Node.js安装包及源码下载地址为:https://nodejs.org/en/download/ 下载安装就行了,安装node会同时安装npm. sublime ...
- OpenStack Trove组件WSGI和RPC调用流程(参考调用的整个过程)
参考博文:https://blog.csdn.net/bill_xiang_/article/details/72909927
- Django文件存储(一)默认存储系统
Django默认使用的文件存储系统'django.core.files.storage.FileSystemStorage'是一个本地存储系统,由settings中的DEFAULT_FILE_STOR ...
- Linux权限管理之ACL权限
注:转载自:https://www.cnblogs.com/ysocean/p/7801329.html 目录 1.什么是 ACL 权限? 2.查看分区 ACL 权限是否开启:dump2fs ①.查看 ...