首页 > 其他分享 >LeetCode 2597. The Number of Beautiful Subsets

LeetCode 2597. The Number of Beautiful Subsets

时间:2024-05-06 11:25:01浏览次数:20  
标签:Beautiful beautiful 2597 nums int number num array LeetCode

原题链接在这里:https://leetcode.com/problems/the-number-of-beautiful-subsets/description/

题目:

You are given an array nums of positive integers and a positive integer k.

A subset of nums is beautiful if it does not contain two integers with an absolute difference equal to k.

Return the number of non-empty beautiful subsets of the array nums.

A subset of nums is an array that can be obtained by deleting some (possibly none) elements from nums. Two subsets are different if and only if the chosen indices to delete are different.

Example 1:

Input: nums = [2,4,6], k = 2
Output: 4
Explanation: The beautiful subsets of the array nums are: [2], [4], [6], [2, 6].
It can be proved that there are only 4 beautiful subsets in the array [2,4,6].

Example 2:

Input: nums = [1], k = 1
Output: 1
Explanation: The beautiful subset of the array nums is [1].
It can be proved that there is only 1 beautiful subset in the array [1].

Constraints:

  • 1 <= nums.length <= 20
  • 1 <= nums[i], k <= 1000

题解:

The question asks for number of subsets without two nums having an absolute difference equal to k.

We can group the nums by num % k, then only nums in one group can have absolute difference equal to k.

The nubmers in different group can't have difference equal to k.

Within one group, we need to sort them.

For the previous number and current number, if the difference is equal to k.

Then taking the current number can't take the previous number. 
If difference != k, then it doesn't matter.

The final result minus 1 to exlude the empty subset.

Time Complexity: O(nlogn + k).

AC Java:

 1 class Solution {
 2     public int beautifulSubsets(int[] nums, int k) {
 3         HashMap<Integer, TreeMap<Integer, Integer>> hm = new HashMap<>();
 4         for(int num : nums){
 5             hm.putIfAbsent(num % k, new TreeMap<>());
 6             TreeMap<Integer, Integer> tm = hm.get(num % k);
 7             tm.put(num, tm.getOrDefault(num, 0) + 1);
 8         }
 9 
10         int res = 1;
11         for(int key = 0; key < k; key++){
12             int pre = 0;
13             int exclude = 1;
14             int include = 0;
15             if(hm.containsKey(key)){
16                 for(int num : hm.get(key).keySet()){
17                     int v = (1 << hm.get(key).get(num)) - 1; // count of non-empty subsets
18                     if(pre + k == num){
19                         int ex = exclude;
20                         exclude = include + exclude;
21                         include = ex * v;
22                     }else{
23                         int ex = exclude;
24                         exclude = include + exclude;
25                         include = (ex + include) * v;
26                     }
27 
28                     pre = num;
29                 }
30 
31                 res *= (exclude + include);
32             }
33         }
34 
35         return res - 1; // minus 1 to exclude the empty subset.
36     }
37 }

类似House Robber.

标签:Beautiful,beautiful,2597,nums,int,number,num,array,LeetCode
From: https://www.cnblogs.com/Dylan-Java-NYC/p/18174646

相关文章

  • LeetCode 1373. Maximum Sum BST in Binary Tree
    原题链接在这里:https://leetcode.com/problems/maximum-sum-bst-in-binary-tree/description/题目:Givena binarytree root,return themaximumsumofallkeysof any sub-treewhichisalsoaBinarySearchTree(BST).AssumeaBSTisdefinedasfollows:Thel......
  • 【LeetCode 1235】规划兼职工作
    题目描述原题链接:LeetCode.1235规划兼职工作解题思路想到了按照结束时间排序后用动态规划来处理,但是又局限在了以结束时间为维度进行递推,又卡在了时间不连续无法高效计算到最晚结束时间范围内所有时间对应值这一问题上,看了题解才知道用排序后的兼职工作数量为维度去递推......
  • 106. 从中序与后序遍历序列构造二叉树(leetcode)
    https://leetcode.cn/problems/construct-binary-tree-from-inorder-and-postorder-traversal/要点是明白中序和后序如何构造二叉树的,并且需要理清当前递归函数的语义,不要一开始就陷入细节,而是思考整棵树与其左右子树的关系,语义是:即构造当前节点,给当前节点左右子树赋值,明......
  • [leetcode 87 扰乱字符串] [剪枝搜索]
    importjava.util.HashMap;importjava.util.Map;classSolution{publicstaticvoidmain(String[]args){Solutionsolution=newSolution();booleanres=solution.isScramble("eebaacbcbcadaaedceaaacadccd","eadcaacabad......
  • leetcode算法热题--最长连续序列
    题目给定一个未排序的整数数组nums,找出数字连续的最长序列(不要求序列元素在原数组中连续)的长度。请你设计并实现时间复杂度为O(n)的算法解决此问题。示例1:输入:nums=[100,4,200,1,3,2]输出:4解释:最长数字连续序列是[1,2,3,4]。它的长度为4。示例2:输入:nums=......
  • leetcode算法热题--最长连续序列
    题目给定一个未排序的整数数组nums,找出数字连续的最长序列(不要求序列元素在原数组中连续)的长度。请你设计并实现时间复杂度为O(n)的算法解决此问题。示例1:输入:nums=[100,4,200,1,3,2]输出:4解释:最长数字连续序列是[1,2,3,4]。它的长度为4。示例2:输入:nums=......
  • leetcode算法热题--盛最多水的容器
    题目给定一个长度为n的整数数组height。有n条垂线,第i条线的两个端点是(i,0)和(i,height[i])。找出其中的两条线,使得它们与x轴共同构成的容器可以容纳最多的水。返回容器可以储存的最大水量。说明:你不能倾斜容器。示例1:输入:[1,8,6,2,5,4,8,3,7]输出:49解释......
  • leetcode算法热题-爬楼梯
    题目假设你正在爬楼梯。需要n阶你才能到达楼顶。每次你可以爬1或2个台阶。你有多少种不同的方法可以爬到楼顶呢?示例1:输入:n=2输出:2解释:有两种方法可以爬到楼顶。1阶+1阶2阶示例2:输入:n=3输出:3解释:有三种方法可以爬到楼顶。1阶+1阶+1阶1......
  • 347. 前 K 个高频元素(leetcode)
    https://leetcode.cn/problems/top-k-frequent-elements/description/可以考虑使用HashMap+排序,不过直接使用优先队列也挺不错,可以使用大顶堆或小顶堆classSolution{publicint[]topKFrequent(int[]nums,intk){Map<Integer,Integer>map=newHas......
  • 239. 滑动窗口最大值(leetcode)
    https://leetcode.cn/problems/sliding-window-maximum/简单的滑动窗口,但是与ACM模式的维护数组不同,在leetcode定义单调队列类更加方便classMyQueue{//单调队列实现,递减Deque<Integer>deque=newLinkedList<>();voidpoll(intval){if(!deque......