【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)。
相關文章
- [LeetCode] 2406. Divide Intervals Into Minimum Number of GroupsLeetCodeIDE
- LeetCode 452. Minimum Number of Arrows to Burst Balloons Sort/MediumLeetCode
- 【Lintcode】970. Big Business(配數學證明)
- [LeetCode] 3239. Minimum Number of Flips to Make Binary Grid Palindromic ILeetCode
- Leetcode 1365. How Many Numbers Are Smaller Than the Current Number (cpp)LeetCode
- leetcode第九題Palindrome Number 驗證迴文數字LeetCode
- LeetCode2: Add two numbers(兩數相加)LeetCode
- CodeForces - 976A:Minimum Binary Number(水題)
- 數學證明 學習筆記筆記
- Fifth. LeetCode 2:Add Two Numbers 兩數之和LeetCode
- LeetCode 1326. Minimum Number of Taps to Open to Water a Garden 動態規劃 離散化 貪心LeetCode動態規劃
- HDU 1394 Minimum Inversion Number (暴力+線段樹)
- Leetcode 165 Compare Version NumbersLeetCode
- LeetCode 2 Add Two NumbersLeetCode
- Leetcode Number of islandsLeetCode
- [LeetCode] 727. Minimum Window SubsequenceLeetCode
- [每日一題]452. Minimum Number of Arrows to Burst Balloons每日一題
- LeetCode-2 Add Two NumbersLeetCode
- Leetcode 967 Numbers With Same Consecutive DifferencesLeetCode
- LeetCode 2. Add Two NumbersLeetCode
- leetcode學習筆記09 palindrome-numberLeetCode筆記
- Leetcode 447 Number of BoomerangsLeetCodeOOM
- [LeetCode] Third Maximum NumberLeetCode
- [LeetCode] Find the Duplicate NumberLeetCode
- Leetcode 9 Palindrome NumberLeetCode
- LeetCode65. Valid Number — 判斷合法數字LeetCode
- LeetCode 209. Minimum Size Subarray Sum ?LeetCode
- Leetcode 209. Minimum Size Subarray SumLeetCode
- LeetCode のminimum-depth-of-binary-treeLeetCode
- LeetCode 129. Sum Root to Leaf NumbersLeetCode
- leetcode 兩數相加(add two numbers) Python程式設計實現LeetCodePython程式設計
- Leetcode 933 Number of Recent CallsLeetCode
- [LeetCode] 9. Palindrome NumberLeetCode
- LeetCode之Fibonacci Number(Kotlin)LeetCodeKotlin
- [LeetCode] 3096. Minimum Levels to Gain More PointsLeetCodeAI
- LeetCode | 153. Find Minimum in Rotated Sorted ArrayLeetCode
- 【Leetcode】453. Minimum Moves to Equal Array ElementsLeetCode
- [LeetCode] 857. Minimum Cost to Hire K WorkersLeetCode