Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions 2160. Minimum Sum of Four Digit Number After Splitting Digits
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Get all digits of num.
Sort them.
To get the minimum sum. We need to choose 1st and 2nd digits as tens digits and 3rd and 4th as ones digits.
As when the tens digits will be minimum, then only we will get minimum sum.
For example:
5643=>3,4,5,6
30+40+5+6=86(min)

Other possibilities:
30+50+4+6=90
30+60+4+5=101
40+50+4+6=100 and so on.

Method 1:
Time complexity: O(nlogn)
Use array to store the digits of num.
*/

class Solution {
public int minimumSum(int num) {
int[] res=new int[4];
int i=0;
while(num>0){
res[i]=num%10;
num/=10;
i++;
}
Arrays.sort(res)
return res[0]*10+res[2]+res[1]*10+res[3];
}
}





//method 2
/*
Method 2:
Use priorityqueue to store the digits of num.
Time complexity: O(n)
*/


class Solution {
public int minimumSum(int num) {
PriorityQueue<Integer>pq=new PriorityQueue<>();
//complexity to insert: O(1)
while(num>0){
pq.offer(num%10);
num/=10;
}
//complexity to remove: O(n)
int a=10*pq.remove();
int b=10*pq.remove();
int c=pq.remove();
int d=pq.remove();
return a+b+c+d;
}
}