• Check if a given array contains duplicate elements within k distance from each other.


    package _interview_question
    
    /**
     * Check if a given array contains duplicate elements within k distance from each other.
     * Given an unsorted array that may contain duplicates. Also given a number k which is smaller than size of array.
     * Write a function that returns true if array contains duplicates within k distance.
     *
     * Input: k = 3, arr[] = {1, 2, 3, 4, 1, 2, 3, 4}
    Output: false
    All duplicates are more than k distance away.
    
    Input: k = 3, arr[] = {1, 2, 3, 1, 4, 5}
    Output: true
    1 is repeated at distance 3.
    
    Input: k = 3, arr[] = {1, 2, 3, 4, 5}
    Output: false
    
    Input: k = 3, arr[] = {1, 2, 3, 4, 4}
    Output: true
     * */
    class Solution11 {
        /*
        * solution 1: Time complexity:O(n*k), Space complexity:O(1)
        * solution 2: HashSet, Time complexity:O(n), Space complexity:O(n)
        * */
        fun checkDuplicate(nums: IntArray, k: Int): Boolean {
            /*for (i in nums.indices){
                for (j in i+1 until nums.size){
                    if (nums[i]==nums[j]){
                        println("i:$i")
                        println("j:$j")
                        if (j-i<=k){
                            return true
                        }
                    }
                }
            }
            return false*/
    
            val set = HashSet<Int>()
            for (i in nums.indices) {
                if (set.contains(nums[i])) {
                    return true
                }
                set.add(nums[i])
                //remove the k+1 distance item
                if (i >= k) {
                    set.remove(nums[i - k])
                }
            }
            return false
        }
    }
  • 相关阅读:
    day7
    11.3NOIP模拟赛
    codeforces 880E. Maximum Subsequence(折半搜索+双指针)
    11.2NOIP模拟赛
    bzoj1483: [HNOI2009]梦幻布丁(vector+启发式合并)
    day9
    codeforces 1006 F(折半搜索)
    codeforces 28D(dp)
    P2210 Haywire(A*)
    4800: [Ceoi2015]Ice Hockey World Championship(折半搜索)
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13326588.html
Copyright © 2020-2023  润新知