zoukankan      html  css  js  c++  java
  • Count subsets

    package _interview_question
    
    /**
     * https://leetcode.com/discuss/interview-question/275785/facebook-phone-screen-count-subsets
     *
     * Input:
    Given an array A of
    -positive
    -sorted
    -no duplicate
    -integer
    A positive integer k
    
    Output:
    Count of all such subsets of A,
    Such that for any such subset S,
    Min(S) + Max(S) = k
    subset should contain atleast two elements
    
    input: {1,2,3,4,5}
     * */
    class Solution {
    
        private val map = HashMap<Int, Int>()
        private var result = 0.0
    
        fun subSetMinMaxEqualK(nums: IntArray, k: Int):Int {
            //1.backtracking, Time complexity: O(2^n), Space complexity:O(n)
            //dfs(0, nums, ArrayList<Int>(), k)
    
            //2.use map, just like 2 sum
            //Time complexity: O(nlogn), Space complexity:O(n)
            nums.sort()
            for (i in nums.indices) {
                val needFind = k - nums[i]
                if (map.containsKey(needFind)) {
                    val value = i-map.get(needFind)!!-1
                    //1,2,3,4,5; k = 5, {1 4} there are 4 cases 2^2
                    result += Math.pow(2.0,value.toDouble())
                }
                map.put(nums[i], i)
            }
            return result.toInt()
        }
    
        private fun dfs(n: Int, nums: IntArray, cur: ArrayList<Int>, target: Int) {
            val temp = ArrayList<Int>(cur)
            val min = temp.min() ?: 0
            val max = temp.max() ?: 0
            val value = min + max
            if (value == target) {
                result++
            }
            for (i in n until nums.size) {
                cur.add(nums[i])
                dfs(i + 1, nums, cur, target)
                cur.removeAt(cur.size - 1)
            }
        }
    
    }
  • 相关阅读:
    《网络对抗技术》Exp6 MSF应用基础
    用Onenote写博客日志 
    C语言文法
    0909
    使用jQuery解决溢出文本省略
    几种流行的AJAX框架jQuery,Mootools,Dojo,Ext JS的对比
    jQuery实现动态加载大尺寸图片
    常用jQuery插件推荐
    使用不带单位的lineheight
    JavaScript懒加载技术 lazyload
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/12953647.html
Copyright © 2011-2022 走看看