leecode 解题总结:313. Super Ugly Number

#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
using namespace std;
/*
问题:
Write a program to find the nth super ugly number.

Super ugly numbers are positive numbers whose all prime factors are in the given prime list
primes of size k. For example, [1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] is the sequence 
of the first 12 super ugly numbers given primes = [2, 7, 13, 19] of size 4.

Note:
(1) 1 is a super ugly number for any given primes.
(2) The given numbers in primes are in ascending order.
(3) 0 < k ≤ 100, 0 < n ≤ 106, 0 < primes[i] < 1000.
(4) The nth super ugly number is guaranteed to fit in a 32-bit signed integer.

分析:题目给定了一个长度为k的丑数列表,计算第n个丑数。
之前计算{2,3,5}的丑数是采用:设定3个指针分别先指向候选丑数列表{mul2,mul3,mul5}.
从中每次选择最小的作为丑数cand,
然后更新候选丑数列表,用2*mul2 <= cand,就累加指向候选丑数mul2的下标;
同理对3*mul3 <= cand,5*mul5 <= cand做同样处理。
对于此题,同理设定长度为k的指针,分别指向候选丑数列表,但是每一次比较就是
从k个里面选择一个最小的数作为候选数,然后对k个指针进行遍历和处理。
每一次时间复杂度为O(k),总的时间复杂度为O(n*k)

输入:
4(给定的质数数组长度)  12(命令个数每个命令表示想要查询的第几个丑数)
2 7 13 19
1 2 3 4 5 6 7 8 9 10 11 12
输出:
1 2 4 7 8 13 14 16 19 26 28 32

关键:
1 之前计算{2,3,5}的丑数是采用:设定3个指针分别先指向候选丑数列表{mul2,mul3,mul5}.
从中每次选择最小的作为丑数cand,
然后更新候选丑数列表,用2*mul2 <= cand,就累加指向候选丑数mul2的下标;
同理对3*mul3 <= cand,5*mul5 <= cand做同样处理。
对于此题,同理设定长度为k的指针,分别指向候选丑数列表,但是每一次比较就是
从k个里面选择一个最小的数作为候选数,然后对k个指针进行遍历和处理。
*/

class Solution {
public:
	//从候选丑数列表中选择最小的丑数:indexs候选丑数列表的下标,primes:质数列表,uglyNumbers:已经生成的丑数列表
	int minNum(vector<int>& uglyNumbers ,  vector<int>& primes , vector<int>& indexs)
	{
		if(uglyNumbers.empty() || indexs.empty() || primes.empty() || indexs.size() != primes.size())
		{
			return 0;
		}
		int minValue = INT_MAX;
		int size = primes.size();
		for(int i = 0 ; i < size ; i++)
		{
			int index = indexs.at(i);
			if(0 <= index && index < uglyNumbers.size())
			{
				minValue = min(minValue , primes.at(i) * uglyNumbers.at(index) );
			}
		}
		return minValue;
	}

    int nthSuperUglyNumber(int n, vector<int>& primes) {
		if(n < 1 || primes.empty())
		{
			return 0;
		}
        vector<int> uglyNumbers(1,1);//初始化候选丑数列表
		int size = primes.size();
		//初始化长度为size的候选丑数下标数组,初始化均为0
		vector<int> indexs(size , 0);
		int count = 1;
		while(count < n)
		{
			int uglyNumber = minNum(uglyNumbers,primes , indexs);
			uglyNumbers.push_back(uglyNumber);
			//更新每一个候选丑数下标
			for(int i = 0 ; i < size ;  i++)
			{
				while(primes.at(i) * uglyNumbers.at( indexs.at(i) ) <= uglyNumber)
				{
					indexs.at(i)++;
				}
			}
			count++;
		}
		return uglyNumbers.at(n - 1);
    }
};


void process()
{
	 vector<int> nums;
	 int value;
	 int num;
	 int commandNum;
	 Solution solution;
	 int nTh;
	 while(cin >> num  >> commandNum)
	 {
		 nums.clear();
		 for(int i = 0 ; i < num ; i++)
		 {
			 cin >> value;
			 nums.push_back(value);
		 }
		 for(int i = 0 ; i < commandNum ; i++)
		 {
			 cin >> nTh;
			 int result = solution.nthSuperUglyNumber(nTh , nums);
			 cout << result << endl;
		 }
	 }
}

int main(int argc , char* argv[])
{
	process();
	getchar();
	return 0;
}


(1)普通用户端(全平台) 音乐播放核心体验: 个性化首页:基于 “听歌历史 + 收藏偏好” 展示 “推荐歌单(每日 30 首)、新歌速递、相似曲风推荐”,支持按 “场景(通勤 / 学习 / 运动)” 切换推荐维度。 播放页功能:支持 “无损音质切换、倍速播放(0.5x-2.0x)、定时关闭、歌词逐句滚动”,提供 “沉浸式全屏模式”(隐藏冗余控件,突出歌词与专辑封面)。 多端同步:自动同步 “播放进度、收藏列表、歌单” 至所有登录设备(如手机暂停后,电脑端打开可继续播放)。 音乐发现与管理: 智能搜索:支持 “歌曲名 / 歌手 / 歌词片段” 搜索,提供 “模糊匹配(如输入‘晴天’联想‘周杰伦 - 晴天’)、热门搜索词推荐”,结果按 “热度 / 匹配度” 排序。 歌单管理:创建 “公开 / 私有 / 加密” 歌单,支持 “批量添加歌曲、拖拽排序、一键分享到社交平台”,系统自动生成 “歌单封面(基于歌曲风格配色)”。 音乐分类浏览:按 “曲风(流行 / 摇滚 / 古典)、语言(国语 / 英语 / 日语)、年代(80 后经典 / 2023 新歌)” 分层浏览,每个分类页展示 “TOP50 榜单”。 社交互动功能: 动态广场:查看 “关注的用户 / 音乐人发布的动态(如‘分享新歌感受’)、好友正在听的歌曲”,支持 “点赞 / 评论 / 转发”,可直接点击动态中的歌曲播放。 听歌排行:个人页展示 “本周听歌 TOP10、累计听歌时长”,平台定期生成 “全球 / 好友榜”(如 “好友中你本周听歌时长排名第 3”)。 音乐圈:加入 “特定曲风圈子(如‘古典音乐爱好者’)”,参与 “话题讨论(如‘你心中最经典的钢琴曲’)、线上歌单共创”。 (2)音乐人端(创作者中心) 作品管理: 音乐上传:支持 “无损音频(FLAC/WAV)+ 歌词文件(LRC)+ 专辑封面” 上传,填写 “歌曲信息
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值