zoukankan      html  css  js  c++  java
  • Find the most frequent element in all intervals

    package _interview_question
    
    /**
     * Problem: Find the most frequent element in all intervals
    Given an unsorted list of start and end time ( a range basically), find any number within all the ranges that occurs in maximum number of intervals.
    Example: [[1,4],[3,5],[4,6]].
    Answer should be 4 because it occurs in all the interval ranges.
     * */
    class Solution6 {
        /*
        * solution 1:brute force, Time complexity:O(n*n), Space complexity:O(n)
        * */
        fun findMostFrequent(intervals: Array<IntArray>): Int {
            val map = HashMap<Int, Int>()
            for (interval in intervals) {
                for (item in interval) {
                    map.put(item, map.getOrDefault(item, 0) + 1)
                }
            }
            var maxValue = Int.MIN_VALUE
            for (item in map) {
                maxValue = Math.max(maxValue, item.value)
            }
            var result = 0
            for (item in map) {
                if (maxValue == item.value) {
                    result = item.key
                }
            }
            println(map)
            return result
        }
    
        /*
        * solution 2:Time complexity:O(n), Space complexity:O(n);
          1.Initialize a freq array with 0, let the size of the array be 10^6 as this is the maximum possible.
          2.Increase the freq[l] by 1, for every starting index of the given range.
          3.Decrease the freq[r+1] by 1 for every ending index of the given range.
          4.Iterate from the minimum Left to the maximum Right and add the frequencies by freq[i] += freq[i-1].
          5.The index with the maximum value of freq[i] will be the answer.
        * */
        fun findMostFrequent2(intervals: Array<IntArray>): Int {
            val freq = IntArray(1000000)
            var first = 0
            var last = 0
            /*
            * eg: { {1, 6}, {2, 3}, {2, 5}, {3, 8} }
                {
                0: 0
                1: 1
                2: 2
                3: 1
                4: -1
                7: -1
                6: -1
                9: -1
                }
                change into:
                 1 -> 0 + 1
                 2 -> 1 + 2
                 3 -> 4
                 4 -> 3
                 5 -> 3
                 6 -> 2
                 7 -> 1
                 8 -> 1
            * */
            for (i in intervals.indices) {
                val left = intervals[i][0]
                val right = intervals[i][1]
                freq[left]++
                freq[right + 1]--
                first = Math.min(left, first)
                last = Math.max(right, last)
            }
            var maxFrequent = 0
            var num = 0
            //check for most frequent element
            for (i in first + 1..last) {
                //increase the frequency
                freq[i] = freq[i - 1] + freq[i]
                if (freq[i] > maxFrequent) {
                    maxFrequent = freq[i]
                    num = i
                }
            }
            return num
        }
    
    }
  • 相关阅读:
    逆变电路技术研究!
    GOOGLE日历(管理自己的日常事务!)
    MATLAB使用的几个小问题(随笔记录下,用作以后参考!)
    ASP.NET截取字符串
    ASP.NET以及JS获取URL和IP地址
    Jvascript 做IE功能按钮,打开、另存为。属性、打印、收藏夹等js按钮
    C# winform 动态添加控件 以及 事件
    VS2008简体中文正式版序列号
    js 获取日期
    ASP.NET读取XML某节点所有数据返回DataTable实例
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13049112.html
Copyright © 2011-2022 走看看