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 <= 30000
A.length
is 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 ...
随机推荐
- Struts1 不能进入Action,页面空白问题解决
http://blog.csdn.net/barry_liao/article/details/35592935 Struts1 不能进入Action,页面空白问题解决 标签: Struts1Acti ...
- keil5 MDK 链接报错 Error: L6410W 解决
keil5 MDK 报错 Build target 'Project' linking... .\Output\Project.axf: Warning: L6310W: Unable to find ...
- BZOJ 2173 luoguo P4451 [国家集训队]整数的lqp拆分
整数的lqp拆分 [问题描述] lqp在为出题而烦恼,他完全没有头绪,好烦啊… 他首先想到了整数拆分.整数拆分是个很有趣的问题.给你一个正整数N,对于N的一个整数拆分就是满足任意m>0,a1 , ...
- python使用xlrd读取excel数据时,整数变小数的解决办法
python使用xlrd读取excel数据时,整数变小数: 解决方法: 1.有个比较简单的就是在数字和日期的单元格内容前加上一个英文的逗号即可.如果数据比较多,也可以批量加英文逗号的前缀(网上都有方法 ...
- win10 安装 oracle 11g
在安装文件的/stage/cvu文件夹下面找到文件 cvu_prereq.xml文件 64位添加红色部分 32位添加蓝色部分 ............... </OPERATING_SYST ...
- 10. 批量插入List<String>
List<String> iscBusOrgIdList = getIscOrgIdList();List<Map<String, Object>> iscBusO ...
- Web框架本质及第一个Django实例 Web框架
Web框架本质及第一个Django实例 Web框架本质 我们可以这样理解:所有的Web应用本质上就是一个socket服务端,而用户的浏览器就是一个socket客户端. 这样我们就可以自己实现Web ...
- django AnonymousUser
AnonymousUser对象class models.AnonymousUser django.contrib.auth.models.AnonymousUser是一个实现django.contri ...
- 微信小程序自制提示框(具有输入文本功能)
https://blog.csdn.net/qq_41681675/article/details/81005561
- 20175314 《Java程序设计》第三周学习总结
20175314 <Java程序设计>第三周学习总结 教材学习内容总结 编程语言的发展事是从面向机器(汇编.机器)到面向过程(C)再到面向对象(Java) 成员变量: 1.成员变量定义在类 ...