Given a non-negative integer N, find the largest number that is less than or equal to N with monotone increasing digits.

(Recall that an integer has monotone increasing digits if and only if each pair of adjacent digits x and y satisfy x <= y.)

Example 1:

Input: N = 10
Output: 9

Example 2:

Input: N = 1234
Output: 1234

Example 3:

Input: N = 332
Output: 299

Note: N is an integer in the range [0, 10^9].

思路:

The idea is to go from the LSB to MSB and find the last digit, where an inversion happens.
There are 2 cases to consider:

case 1:
In 14267 , we see that inversion happens at 4. In this case, then answer is obtained by reducing 4 to 3, and changing all the following digits to 9.
=> 13999

case 2:
1444267, here eventhough the last inversion happens at the last 4 in 1444, if we reduce it to 3, then that itself breaks the rule. So once we find the last digit where inversion happens, if that digit is repeated, then we have to find the last position of that digit. After that it is same as case1, where we reduce it by 1 and set the remaining digits to 9.
=> 1399999

The steps are:

    1. Convert n into num array in reverse order
    2. Find the leftmost position that is inverted and if the inverted character repeats itself, find the leftmost repeated digit.
    3. Fill the digits after inversion as 9
    4. Reduce the digit that caused the inversion by -1
    5. Reverse back the num array and convert to int
int monotoneIncreasingDigits(int N) {
string n_str = to_string(N); int marker = n_str.size();
for(int i = n_str.size()-; i > ; i --) {
if(n_str[i] < n_str[i-]) {
marker = i;
n_str[i-] = n_str[i-]-;
}
} for(int i = marker; i < n_str.size(); i ++) n_str[i] = ''; return stoi(n_str);
}

参考:

https://leetcode.com/problems/monotone-increasing-digits/discuss/109811

https://leetcode.com/problems/monotone-increasing-digits/solution/

https://leetcode.com/problems/monotone-increasing-digits/discuss/109794

[leetcode-738-Monotone Increasing Digits]的更多相关文章

  1. [LeetCode] 738. Monotone Increasing Digits 单调递增数字

    Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...

  2. 【LeetCode】738. Monotone Increasing Digits 解题报告(Python)

    [LeetCode]738. Monotone Increasing Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...

  3. 402. Remove K Digits/738.Monotone Increasing Digits/321. Create Maximum Number

    Given a non-negative integer num represented as a string, remove k digits from the number so that th ...

  4. 738. Monotone Increasing Digits 单调递增的最接近数字

    [抄题]: Given a non-negative integer N, find the largest number that is less than or equal to N with m ...

  5. 738. Monotone Increasing Digits

    Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...

  6. LC 738. Monotone Increasing Digits

    Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...

  7. 【leetcode】Monotone Increasing Digits

    Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...

  8. [LeetCode] Monotone Increasing Digits 单调递增数字

    Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...

  9. [Swift]LeetCode738. 单调递增的数字 | Monotone Increasing Digits

    Given a non-negative integer N, find the largest number that is less than or equal to Nwith monotone ...

随机推荐

  1. 『ACM C++』 PTA 天梯赛练习集L1 | 042-43

    记录刷题情况 ------------------------------------------------L1-042--------------------------------------- ...

  2. Virtualization state: Optimized (version 7.4 installed)

    [Virtualization state: Optimized (version 7.4 installed)] [root@localhost ~]# cd /mnt/ [root@localho ...

  3. es6 Proxy对象详解

    Proxy用于修改某些操作的默认行为,也可以理解为在目标对象之前架设一层拦截,外部所有的访问都必须先通过这层拦截,因此提供了一种机制,可以对外部的访问进行过滤和修改.这个词的原理为代理,在这里可以表示 ...

  4. 【转】Red5流服务器搭建(实现在线直播,流媒体视频播放和在线视频会议)

    来自:http://blog.csdn.net/sunroyi666/article/details/52981639 一. 先介绍一下流媒体技术:所谓流媒体技术,是指将连续的影像和声音信息经过压缩处 ...

  5. A64 I2S调试

    通过A64 的I2S总线与回音消除模块连接,在A64中需要使能并配置daudio功能. Daudio 为A64 的数字音频接口,可配置成i2s/pcm格式标准音频接口. 内核配置在lichee/lin ...

  6. kubernetes理论基础#开始入坑啊!

    什么是kubernetes 首先,他是一个全新的基于容器技术的分布式架构领先方案.Kubernetes(k8s)是Google开源的容器集群管理系统(谷歌内部:Borg).在Docker技术的基础上, ...

  7. PAT A1060 (Advanced Level) Practice

    If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered ...

  8. 理解golang中的channel

    channel是goroutine之间的通信机制.可以类比线程间的通信,线程间的通信有多种方式,比如线程上下文.共享内存.IPC通信.socket实现不同机器间的通信. channel用起来很简单,绑 ...

  9. APP支付 + 退款(JAVA实现)

    首先,你得先有微信开发平台账号密码还需要开通应用,然后还有微信服务商平台商户版账号(这些我都是给产品经理拿的) 其次我认为你先去看一看微信开发平台的文档!  https://pay.weixin.qq ...

  10. 【BZOJ1564】【NOI2009】二叉查找树(动态规划)

    [BZOJ1564][NOI2009]二叉查找树(动态规划) 题面 BZOJ 洛谷 题目描述 已知一棵特殊的二叉查找树.根据定义,该二叉查找树中每个结点的数据值都比它左儿子结点的数据值大,而比它右儿子 ...