1460. Make Two Arrays Equal by Reversing Sub-arrays

复制代码
package LeetCode_1460

/**
 * 1460. Make Two Arrays Equal by Reversing Sub-arrays
 * https://leetcode.com/problems/make-two-arrays-equal-by-reversing-sub-arrays/
 *
 * Given two integer arrays of equal length target and arr.
In one step, you can select any non-empty sub-array of arr and reverse it.
You are allowed to make any number of steps.
Return True if you can make arr equal to target, or False otherwise.

Example 1:
Input: target = [1,2,3,4], arr = [2,4,1,3]
Output: true
Explanation: You can follow the next steps to convert arr to target:
1- Reverse sub-array [2,4,1], arr becomes [1,4,2,3]
2- Reverse sub-array [4,2], arr becomes [1,2,4,3]
3- Reverse sub-array [4,3], arr becomes [1,2,3,4]
There are multiple ways to convert arr to target, this is not the only way to do so.

Constraints:
1. target.length == arr.length
2. 1 <= target.length <= 1000
3. 1 <= target[i] <= 1000
4. 1 <= arr[i] <= 1000
 * */
class Solution {
    /*
    * solution: use array to check if all element if equals,
    * Time:O(1001), Space:O(1001)
    * */
    fun canBeEqual(target: IntArray, arr: IntArray): Boolean {
        val array1 = IntArray(1001)
        val array2 = IntArray(1001)
        //calculate number of appearance for each element
        for (item in target) {
            array1[item]++
        }
        for (item in arr) {
            array2[item]++
        }
        //check the result
        for (i in array1.indices) {
            if (array1[i] != array2[i]) {
                return false
            }
        }
        return true
    }
}
复制代码

 

posted @   johnny_zhao  阅读(70)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示