码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 1201. Ugly Number III && 264. Ugly Number ll


    First, we can enumerate different numbers as a candidate answer as long as there are n - 1's ugly numbers before it.

    So, right now we have three conditions here, the first one is all the ugly numbers should be smaller than the answer. The second condition is all the ugly numbers should be divisible by a, b, c. The third condition is there are total of n - 1's ugly numbers before answer.

    THe first condition is easy to solve.

    The second condition is easy to solve too.

    The only annoying condition is the last condition. How to count how many ugly number are smaller than the current number?

    for example, we have 3 6 9 12

    let say a is 3.

    how many ugly number that is divisible by 3 and smaller than 12? the answer is 12/ 3.

    as 6 9 12 are all the multiple of 3, we can ensure how many times 3 is able to multiply itself.

    But there are a, b, c, there will occur some duplicate cases.

    ans/a + ans/b + ans/c - ans/lcm(a,b) - ans/lcm(a,c) - ans/lcm(b, c) + ans/lcm(a,b,c)

    1. typedef long long ll;
    2. class Solution {
    3. public:
    4. ll gcd(ll a, ll b){
    5. if(!b)return a;
    6. return gcd(b, a % b);
    7. }
    8. ll lcm(ll a, ll b){
    9. return a * b / gcd(a, b);
    10. }
    11. ll lcm2(ll a, ll b, ll z){
    12. int res = lcm(a, b);
    13. return lcm(res, z);
    14. }
    15. ll nthUglyNumber(int n, int a, int b, int c) {
    16. ll l = 0, r = INT_MAX;
    17. while(l <= r){
    18. ll mid = (l + r) / 2;
    19. ll cnt = mid/a + mid/b + mid/c - mid/(lcm(a,b)) - mid/(lcm(a,c)) - mid/(lcm(b,c)) + mid/lcm2(a,b,c);
    20. if(cnt < n){
    21. l = mid + 1;
    22. }else{
    23. r = mid - 1;
    24. }
    25. }
    26. return l;
    27. }
    28. };

    264. Ugly Number ll

    Every time, we generate a new number by multiplying a number by 2/ 3/ 5, we will always make sure the number is minimum, and if we used a 2/3/5, we have to increase the pointer by 1, it will naturally create a sorted array which all the number only have 2/3/5 factors.

    1. class Solution {
    2. public:
    3. int nthUglyNumber(int n) {
    4. vector<int> v(n + 1, 1);
    5. int t1 = 0, t2 = 0, t3 = 0;
    6. for(int i = 1; i <= n; i++){
    7. v[i] = min(v[t1]*2, min(v[t2]*3, v[t3]*5));
    8. if(v[i] == v[t1]*2) t1++;
    9. if(v[i] == v[t2]*3) t2++;
    10. if(v[i] == v[t3]*5) t3++;
    11. }
    12. return v[n - 1];
    13. }
    14. };

  • 相关阅读:
    LeetCode693. 交替位二进制数
    微信小程序向公众号推送模板消息(根据用户登录小程序openid实现向同一主体下对应公众号推送模板消息)
    提示3D标题编辑器仍在运行怎么解决,以及3D标题编辑器怎么使用
    第 125 场 LeetCode 双周赛题解
    如何下载MySQL的JDBC驱动包
    基于微信小程序校园二手交易市场(springboot+ mybatis-plus+mysql+原生微信小程序)
    Vue2源码学习笔记 - 14.响应式原理—核心本质
    在同一台机器上部署OGG并测试
    无需root,删除安卓内置应用
    Android 缓存清理实现工具类
  • 原文地址:https://blog.csdn.net/weixin_58286631/article/details/125430284
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号