【Leetcode】1689. Partitioning Into Minimum Number Of Deci-Binary Numbers(配數學證明)
題目地址:
https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/
如果一個十進位制整數的每一位都是 0 0 0或 1 1 1並且不以 1 1 1開頭,則稱其是Deci-Binary的。給定一個十進位制數 n n n以字串表示,問其最少能由多少個Deci-Binary的數之和表示。
設 n n n的每位的最大值是 x x x,我們證明 x x x就是答案。
演算法正確性證明:
首先我們證明
x
x
x是上界,先掃描
n
n
n,得到除
0
0
0之外的每位的最小值
m
m
m之後,讓
n
n
n減去
m
m
m個形如這樣的Deci-Binary數,
n
n
n是
0
0
0的地方它也是
0
0
0,
n
n
n不是
0
0
0的地方它是
1
1
1,減完之後重複上面的操作。我們發現
n
n
n的每位最大值
x
x
x被減了
x
x
x次,所以
x
x
x是個上界;
x
x
x也是下界的原因是,
x
x
x那一位必須通過至少減去
x
x
x個Deci-Binary的數才能變成
0
0
0。所以演算法正確。
程式碼如下:
public class Solution {
public int minPartitions(String n) {
char ch = '0';
for (int i = 0; i < n.length(); i++) {
ch = (char) Math.max(ch, n.charAt(i));
}
return ch - '0';
}
}
時間複雜度 O ( l n ) O(l_n) O(ln),空間 O ( 1 ) O(1) O(1)。
相關文章
- 【Lintcode】970. Big Business(配數學證明)
- LeetCode 452. Minimum Number of Arrows to Burst Balloons Sort/MediumLeetCode
- Leetcode 1365. How Many Numbers Are Smaller Than the Current Number (cpp)LeetCode
- [LeetCode] Palindrome Number 驗證迴文數字LeetCode
- [LeetCode] 2406. Divide Intervals Into Minimum Number of GroupsLeetCodeIDE
- 數學證明 學習筆記筆記
- LeetCode解題報告 452. Minimum Number of Arrows to Burst Balloons [medium]LeetCode
- HDU 1394 Minimum Inversion Number (樹狀陣列求逆序數)陣列
- leetcode第九題Palindrome Number 驗證迴文數字LeetCode
- [LeetCode] Ugly Number 醜陋數LeetCode
- LeetCode2: Add two numbers(兩數相加)LeetCode
- [LeetCode] Add Two Numbers 兩個數字相加LeetCode
- Leetcode - Bitwise AND of Numbers RangeLeetCode
- LeetCode-Lexicographical NumbersLeetCode
- [LeetCode] 3239. Minimum Number of Flips to Make Binary Grid Palindromic ILeetCode
- Leetcode: Palindrome Partitioning IILeetCode
- Palindrome Partitioning leetcode javaLeetCodeJava
- Fifth. LeetCode 2:Add Two Numbers 兩數之和LeetCode
- LeetCode (39) Ugly Number I II (醜數)LeetCode
- Leetcode Minimum Path SumLeetCode
- What are number-of-subpartitions of composite range-hash partitioning tables
- LeetCode-Bitwise AND of Numbers RangeLeetCode
- Leetcode Sum Root to Leaf NumbersLeetCode
- Leetcode Add Two NumbersLeetCode
- 無需語言的數學證明,值得收藏!
- [每日一題]452. Minimum Number of Arrows to Burst Balloons每日一題
- hdu 1394 Minimum Inversion Number 【線段樹查詢】
- Leetcode-Palindrome Partitioning IILeetCode
- Palindrome Partitioning II Leetcode javaLeetCodeJava
- Leetcode-Minimum Path SumLeetCode
- [LeetCode] Minimum Size Subarray SumLeetCode
- Leetcode Minimum Window SubstringLeetCode
- Minimum Path Sum leetcode javaLeetCodeJava
- LeetCode 2 Add Two NumbersLeetCode
- Leetcode-Add Two NumbersLeetCode
- Sum Root to Leaf Numbers leetcode javaLeetCodeJava
- Add Two Numbers leetcode javaLeetCodeJava
- CodeForces - 976A:Minimum Binary Number(水題)