Search This Blog

Tuesday, November 14, 2017

Degree of an Array

Task

Given an array of integers. Degree of this array is defined as the maximum frequency of any element in the array. For example, the array [1, 2, 3, 4, 2, 2, 3] has a degree 3 because the number 2 occurs three times (more than any other numbers). Need to define size of the smallest subArray with degree equals to initial array's degree. For current array smallest subArray with degree 3 will be [2, 3, 4, 2, 2] which size is 5. So answer is 5.

Algorithm


  1. Create three dictionary: 
    • first for stored count of each number in array (countTable dictionary), 
    • second for storing first index of each number (left dictionary) 
    • and third for storing last index of each number (right dictionary)
  2. For each number in array
    • If for this number already exists value in countTable then increase this value by 1
    • If for this number do not exist value in countTable then assign it with value of 1 and also we need to store index of first time we see this value in left dictionary
    • For right dictionary update value each iteration of loop
    • Find max of two values: current degree of array and count of current value
  3. For each key in countTable
    1.  Check if count for this value is equal to degree of array then
      1. For current key (which is int number) we need to calculate length of subArray by following formula: [right index] - [left index] + 1
      2. Get min value from length of initial array and length of subArray (this min value is our answer) 

Full Code

func findMinSubArrayWithDegree(_ array: [Int]) -> Int {
    var degree = 0
    let n = array.count
    var countTable = [Int: Int]()
    var left = [Int: Int]()
    var right = [Int: Int]()
    for i in 0..<n {
        let value = array[i]
        if let count = countTable[value] {
            countTable[value] = count + 1
        } else {
            countTable[value] = 1
            left[value] = i
        }
        right[value] = i
        degree = max(degree, countTable[value]!)
    }
    var result = n
    for key in countTable.keys {
        if degree == countTable[key] {
            let subArrayCount = right[key]! - left[key]! + 1
            result = min(result, subArrayCount)
        }
    }
    return result
}

findMinSubArrayWithDegree([1, 2, 1, 3, 1, 2, 2, 1, 2, 2]) // 9
findMinSubArrayWithDegree([1, 2, 2, 3, 1]) // 2
findMinSubArrayWithDegree([1, 2, 3, 4, 2, 2, 3]) // 5

6 comments:

  1. After a long time i found a unique and on purpose information about Custom Designed Websites. Can't wait to have more from you.

    ReplyDelete
  2. Thank you for sharing this important and useful information in this article. Best Custom Websites

    ReplyDelete
  3. Great content. I was looking for this kind of content. It saved my time to search further.
    You must provide such type of content constantly. I appreciate your willingness to provide readers with a good article.
    Custom Design Websites

    ReplyDelete
  4. Your article is unbelievable with precise knowledge. I can see a lot of research behind that and that is beyond my expectations.
    Create Your Own Website \

    ReplyDelete
  5. This is a great article! Thank you very much. I really need these suggestions. An in-depth explanation is of great benefit. Incredibly inspiring to see how you write. Custom Website

    ReplyDelete
  6. This is a fantastic article. Your essay is quite simple to comprehend. Thank you for sharing this informative information with us.
    Search Engine Marketing Agency

    ReplyDelete