[JavaScript 刷题] Code Signal - 相似数组(Are Similar?)
[JavaScript 刷题] Code Signal - 相似数组(Are Similar?)
题目地址:Are Similar?
题目
如下:
Two arrays are called similar if one can be obtained from another by swapping at most one pair of elements in one of the arrays.
Given two arrays a
and b
, check whether they are similar.
Example:
For
a = [1, 2, 3]
andb = [1, 2, 3]
, the output should beareSimilar(a, b) = true
.The arrays are equal, no need to swap any elements.
For
a = [1, 2, 3]
andb = [2, 1, 3]
, the output should beareSimilar(a, b) = true
.We can obtain
b
froma
by swapping2
and1
inb
.For
a = [1, 2, 2]
andb = [2, 1, 1]
, the output should beareSimilar(a, b) = false
.Any swap of any two elements either in
a
or inb
won’t makea
andb
equal.
Input/Output:
[execution time limit] 4 seconds (js)
[input] array.integer a
Array of integers.
Guaranteed constraints:
3 ≤ a.length ≤ 105
,1 ≤ a[i] ≤ 1000
.[input] array.integer b
Array of integers of the same length as a.
Guaranteed constraints:
b.length = a.length
,1 ≤ b[i] ≤ 1000
.[output] boolean
true
ifa
andb
are similar,false
otherwise.
解题思路
解题思路和 近乎增长序列(almostIncreasingSequence) 很相似,不过有点稍微变种。
因为先决条件最多只能调换 一个数组中 的 一对元素,所以二者之间的不同只能为 0(不需要调换),或者 2(调换一对)
当最大不同为 0 时,代表数组完全相等,返回
true
即可当最大不同为 2 时,因为只需要交换一个数组中的两个元素,所以只要满足下列条件即可:
a [ y ] = b [ x ] , a [ x ] = b [ y ] a[y] = b[x], a[x] = b[y] a[y]=b[x],a[x]=b[y]
这样跑下来的时间复杂度是 O ( n ) O(n) O(n),需要遍历一次所有的数组,空间复杂度为 O ( n ) O(n) O(n),需要保存所有不同数。当然,后者也可以被优化到 O ( 1 ) O(1) O(1),只需要当 包含所有不同的数组 长度超过 2 时直接返回即可,这样最大也就需要 3 个额外空间去进行存储。
使用 JavaScript 解题
// swapping at most one pair of elements in one of the arrays.
function areSimilar(a, b) {const maxDiff = maxDiffs(a, b);if (maxDiff.length > 2 || maxDiff.length === 1) return false;if (maxDiff.length === 0) return true; // identicalconst [diff1, diff2] = maxDiff;return a[diff1] === b[diff2] && a[diff2] === b[diff1];
}const maxDiffs = (a, b) => {let maxDiff = [];for (let i = 0; i < a.length; i++) {if (a[i] !== b[i]) {maxDiff.push(i);}}return maxDiff;
};console.log(areSimilar([1, 2, 3], [1, 2, 3]));
[JavaScript 刷题] Code Signal - 相似数组(Are Similar?)相关推荐
- [JavaScript 刷题] 搜索 - 腐烂的橘子, leetcode 994
[JavaScript 刷题] 搜索 - 腐烂的橘子, leetcode 994 唉--之前写过笔记总结的问题,还是又卡住了. 自挂东南枝-- 题目地址: Rotting Oranges 题目如下: ...
- [JavaScript 刷题] 树 - 完全二叉树的节点个数, leetcode 222
[JavaScript 刷题] 树 - 完全二叉树的节点个数, leetcode 222 github repo 地址: https://github.com/GoldenaArcher/js_lee ...
- .net 遍历数组找重复值写入一个新数组_第二轮 Python 刷题笔记一:数组
经过四十多天缓慢的刷题,现在进度大概是刷了八十多道 LeetCode 题,最近也在吸取过来人的经验,仍然需要对刷题计划进行调整. 首先明确一下目标,我是有些 Python 基础,想通过刷题掌握更多算法 ...
- Javascript刷题 》 查找数组元素位置
找出元素 item 在给定数组 arr 中的位置 输出描述: function indexOf(arr, item) {..... } 如果数组中存在 item,则返回元素在数组中的位置,否则返回 - ...
- 剑指offer刷题(java)|二维数组中的查找|替换空格|leetcode刷题
文章目录 前言 一.二维数组中的查找 题目 题解一 题解二 题解三 二.替换空格 题目 题解一 题解二 题解三 前言 本文主要是写了我做算法题的思路以及对其他优秀题解的自我理解. 一.二维数组中的查找 ...
- Leetcode刷题 34.在排序数组中查找元素的第一个和最后一个位置
解法1: class Solution { public:vector<int> searchRange(vector<int>& nums, int target) ...
- C#LeetCode刷题之#34-在排序数组中查找元素的第一个和最后一个位置(Find First and Last Position of Element in Sorted Array)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4970 访问. 给定一个按照升序排列的整数数组 nums,和一个目 ...
- C#LeetCode刷题之#350-两个数组的交集 II(Intersection of Two Arrays II)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4044 访问. 给定两个数组,编写一个函数来计算它们的交集. 输入 ...
- C#LeetCode刷题之#349-两个数组的交集(Intersection of Two Arrays)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4042 访问. 给定两个数组,编写一个函数来计算它们的交集. 输入 ...
最新文章
- Go 学习笔记(78)— Go 标准库 net/http 创建服务端(接收 GET、POST 请求)
- 蚂蚁王旭:开源协作如何提升业界的安全?
- BeanFactory 和ApplicationContext 有什么区别?
- dom 无法找到 body节点问题
- win7系统怎么用计算机,win7电脑配置怎么查看_win7系统查看电脑配置的方法
- 阿里云RPA(机器人流程自动化)— 码栈应用教程,让一切变得自动化
- App隐私合规注意事项和相关材料
- 家用计算机音效部件图示,唱吧新版自定义音效设置方法(附上最佳音效设置参数图)...
- 基于微型计算机系统的报警器设计,防盗报警器的设计毕业设计分析.doc
- 微信小程序扫描程序码携带参数
- vue单页面怎么做SEO优化
- php curl post 很慢,php的curl函数模拟post数据提交,首次速度非常慢的处理办法 | 学步园...
- 年纪大的程序员慢慢都流向什么地方去了?
- 【活动推荐】美团外卖两千万日订单背后的客户端技术架构
- px、em、rem单位间的区别
- SFP(Soft Filter Pruning)笔记
- OpenGL之三维GIS
- AD账号属性的 PwdLastSet 和 PasswordLastSet 有什么区别?
- 移动端黑马面面案例(技术方案,代码规范,目录规范,蓝湖/摹客协作平台,适配方案简介,初始化文件,swiper插件使用,index.html,index.less,index.css)
- CSS .class .class与.class.class区别
热门文章
- 玩家类pk{游戏}测试类
- 自学web前端觉得好难,可能你遇到了这些困境
- css直角线_直角符号和垂直符号 过射线的端点和刚作的点,画射线
- 计算机中丢失crashrpt,修复crashrpt.dll
- python 贝塞尔曲线,贝塞尔曲线原理分析及其Android的实现
- 使用 nodejs 和 ElasticSearch 快速搭建全文检索
- 旧版本CorelDRAW图件换成最新版本字体不识别解决办法
- arduino编乐谱_Arduino教程——手动添加库并使用
- Python,双色球模拟改进版
- 最好的html和css编辑器,10款顶级CSS编辑器