A decimal number is called deci-binary if each of its digits is either
Given a string
0
or 1
without any leading zeros. For example, 101
and 1100
are deci-binary, while 112
and 3001
are not.Given a string
n
that represents a positive decimal integer, return the minimum number of positive deci-binary numbers needed so that they sum up to n
.Example 1:
Input: n = "32"
Output: 3
Explanation: 10 + 11 + 11 = 32
Approach
Java
public class MiniNumOfDecBin {public static void main(String[] args) {String n = "32";System.out.println(minPartitions(n));}static int minPartitions(String n) {int ans = 0;// iterate till length of string and// find the maximum digitsfor (int i = 0; i < n.length(); i++)ans = Math.max(ans, n.charAt(i) - '0');return ans;}}
C++
#include <bits/stdc++.h>using namespace std;int minPartitions(string n){int ans=0;//iterate till length of string and//find the maximum digitsfor(int i=0;i<n.size();i++)ans=max(ans,n[i]-'0');return ans;}int main(){string n="32";cout<<minPartitions(n);return 0;}
No comments:
Post a Comment