Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
You are given a positive integer num
consisting of exactly four digits. Split num
into two new integers new1
and new2
by using the digits found in num
. Leading zeros are allowed in new1
and new2
, and all the digits found in num
must be used.
num = 2932
, you have the following digits: two 2
‘s, one 9
and one 3
. Some of the possible pairs [new1, new2]
are [22, 93]
, [23, 92]
, [223, 9]
and [2, 329]
.Return the minimum possible sum of new1
and new2
.
Example 1:
Input: num = 2932
Output: 52
Explanation: Some possible pairs [new1, new2] are [29, 23], [223, 9], etc.
The minimum sum can be obtained by the pair [29, 23]: 29 + 23 = 52.
Example 2:
Input: num = 4009
Output: 13
Explanation: Some possible pairs [new1, new2] are [0, 49], [490, 0], etc.
The minimum sum can be obtained by the pair [4, 9]: 4 + 9 = 13.
Constraints:
1000 <= num <= 9999
class Solution{
public:
int minimumSum(int num){
string s = to_string(num);
sort(s.begin(), s.end());
int res = (s[0] - '0' + s[1] - '0') * 10 + s[2] - '0' + s[3] - '0';
return res;
}
};
class Solution
{
public int minimumSum(int num)
{
int[] dig = new int[4]; // For each digit
int cur = 0;
while(num > 0) // Getting each digit
{
dig[cur++] = num % 10;
num /= 10;
}
Arrays.sort(dig); // Ascending order
int num1 = dig[0] * 10 + dig[2]; // 1st and 3rd digit
int num2 = dig[1] * 10 + dig[3]; // 2nd and 4th digit
return num1 + num2;
}
}
class Solution:
def minimumSum(self, num: int) -> int:
s = list(str(num))
s.sort()
s1 = s[0]+s[2]
s2 = s[1]+s[3]
sum = int(s1)+int(s2)
return sum
You are given a 0-indexed integer array nums
and an integer pivot
. Rearrange nums
such that the following conditions are satisfied:
pivot
appears before every element greater than pivot
.pivot
appears in between the elements less than and greater than pivot
.pivot
and the elements greater than pivot
is maintained.pi
, pj
where pi
is the new position of the ith
element and pj
is the new position of the jth
element. For elements less than pivot
, if i < j
and nums[i] < pivot
and nums[j] < pivot
, then pi < pj
. Similarly for elements greater than pivot
, if i < j
and nums[i] > pivot
and nums[j] > pivot
, then pi < pj
.Return nums
after the rearrangement.
Example 1:
Input: nums = [9,12,5,10,14,3,10], pivot = 10
Output: [9,5,3,10,10,12,14]
Explanation:
The elements 9, 5, and 3 are less than the pivot so they are on the left side of the array.
The elements 12 and 14 are greater than the pivot so they are on the right side of the array.
The relative ordering of the elements less than and greater than pivot is also maintained. [9, 5, 3] and [12, 14] are the respective orderings.
Example 2:
Input: nums = [-3,4,3,2], pivot = 2
Output: [-3,2,4,3]
Explanation:
The element -3 is less than the pivot so it is on the left side of the array.
The elements 4 and 3 are greater than the pivot so they are on the right side of the array.
The relative ordering of the elements less than and greater than pivot is also maintained. [-3] and [4, 3] are the respective orderings.
Constraints:
1 <= nums.length <= 105
-106 <= nums[i] <= 106
pivot
equals to an element of nums
.class Solution
{
public:
vector<int> pivotArray(vector<int> &nums, int pivot)
{
int low = 0;
int same = 0;
int high;
for (auto n : nums)
{
if (n < pivot)
{
++low;
}
else if (n == pivot)
{
++same;
}
}
vector<int> res(nums.size());
high = same + low;
same = low;
low = 0;
for (auto n : nums)
{
if (n < pivot)
{
res[low++] = n;
}
else if (n == pivot)
{
res[same++] = n;
}
else
{
res[high++] = n;
}
}
return res;
}
};
public int[] pivotArray(int[] nums, int pivot) {
int n = nums.length, pivotFreq = 0, i = 0;
int[] ans = new int[n];
for (int num : nums) {
if (num < pivot) {
ans[i++] = num;
}else if (num == pivot) {
++pivotFreq;
}
}
while (pivotFreq-- > 0) {
ans[i++] = pivot;
}
for (int num : nums) {
if (num > pivot) {
ans[i++] = num;
}
}
return ans;
}
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
pivotFreq, ans = 0, []
for num in nums:
if num < pivot:
ans.append(num)
elif num == pivot:
pivotFreq += 1
ans.extend([pivot] * pivotFreq)
for num in nums:
if num > pivot:
ans.append(num)
return ans
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
ans=[]
nums.remove(pivot)
i=0
ans.append(pivot)
for j in nums:
if j<pivot:
ans.insert(i,j)
i=i+1
elif j==pivot:
ans.insert(i+1,j)
else:
ans.append(j)
return ans
A generic microwave supports cooking times for:
1
second.99
minutes and 99
seconds.To set the cooking time, you push at most four digits. The microwave normalizes what you push as four digits by prepending zeroes. It interprets the first two digits as the minutes and the last two digits as the seconds. It then adds them up as the cooking time. For example,
9
5
4
(three digits). It is normalized as 0954
and interpreted as 9
minutes and 54
seconds.0
0
0
8
(four digits). It is interpreted as 0
minutes and 8
seconds.8
0
9
0
. It is interpreted as 80
minutes and 90
seconds.8
1
3
0
. It is interpreted as 81
minutes and 30
seconds.You are given integers startAt
, moveCost
, pushCost
, and targetSeconds
. Initially, your finger is on the digit startAt
. Moving the finger above any specific digit costs moveCost
units of fatigue. Pushing the digit below the finger once costs pushCost
units of fatigue.
There can be multiple ways to set the microwave to cook for targetSeconds
seconds but you are interested in the way with the minimum cost.
Return the minimum cost to set targetSeconds
seconds of cooking time.
Remember that one minute consists of 60
seconds.
Example 1:
Input: startAt = 1, moveCost = 2, pushCost = 1, targetSeconds = 600
Output: 6
Explanation: The following are the possible ways to set the cooking time.
- 1 0 0 0, interpreted as 10 minutes and 0 seconds.
The finger is already on digit 1, pushes 1 (with cost 1), moves to 0 (with cost 2), pushes 0 (with cost 1), pushes 0 (with cost 1), and pushes 0 (with cost 1).
The cost is: 1 + 2 + 1 + 1 + 1 = 6. This is the minimum cost.
- 0 9 6 0, interpreted as 9 minutes and 60 seconds. That is also 600 seconds.
The finger moves to 0 (with cost 2), pushes 0 (with cost 1), moves to 9 (with cost 2), pushes 9 (with cost 1), moves to 6 (with cost 2), pushes 6 (with cost 1), moves to 0 (with cost 2), and pushes 0 (with cost 1).
The cost is: 2 + 1 + 2 + 1 + 2 + 1 + 2 + 1 = 12.
- 9 6 0, normalized as 0960 and interpreted as 9 minutes and 60 seconds.
The finger moves to 9 (with cost 2), pushes 9 (with cost 1), moves to 6 (with cost 2), pushes 6 (with cost 1), moves to 0 (with cost 2), and pushes 0 (with cost 1).
The cost is: 2 + 1 + 2 + 1 + 2 + 1 = 9.
Example 2:
Input: startAt = 0, moveCost = 1, pushCost = 2, targetSeconds = 76
Output: 6
Explanation: The optimal way is to push two digits: 7 6, interpreted as 76 seconds.
The finger moves to 7 (with cost 1), pushes 7 (with cost 2), moves to 6 (with cost 1), and pushes 6 (with cost 2). The total cost is: 1 + 2 + 1 + 2 = 6
Note other possible ways are 0076, 076, 0116, and 116, but none of them produces the minimum cost.
Constraints:
0 <= startAt <= 9
1 <= moveCost, pushCost <= 105
1 <= targetSeconds <= 6039
int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {
auto cost = [&](int pos, int m, int s){
if (min(m, s) < 0 || max(m, s) > 99)
return INT_MAX;
int res = 0;
for (auto digit : to_string(m * 100 + s)) {
res += pushCost + (pos == digit - '0' ? 0 : moveCost);
pos = digit - '0';
}
return res;
};
int m = targetSeconds / 60, s = targetSeconds % 60;
return min(cost(startAt, m, s), cost(startAt, m - 1, s + 60));
}
class Solution {
public int minCostSetTime(int startAt, int moveCost, int pushCost, int tar) {
int min=tar/60, sec=tar%60, minCost=(moveCost+pushCost)*4;
if(min>99) { min--; sec+=60; } // this is required to do because if tar>=6000 then min is 100 which is not possible as it atmost can be 99mins
while(min>=0&&sec<=99) { // this while loop will work for atmost 2 iterations
tar=min*100+sec;
char arr[]=(""+tar).toCharArray();
int sameMove=0;
for(int i=0;i<arr.length-1;i++)
if(arr[i]==arr[i+1])
sameMove++;
if(startAt==arr[0]-'0')
minCost=Math.min(minCost,pushCost*arr.length+moveCost*(arr.length-1-sameMove));
else
minCost=Math.min(minCost,pushCost*arr.length+moveCost*(arr.length-sameMove));
min--; sec+=60;
}
return minCost;
}
}
def minCostSetTime(self, start: int, mC: int, pC: int, TS: int) -> int:
def helper(mins, secs):
pre, cost, ss = str(start), 0, str(secs)
if mins != 0:
if len(ss) < 2:
ss = "0" + ss
ss = str(mins) + ss
for ch in ss:
if ch != pre:
cost += mC
cost += pC
pre = ch
return cost
mins, secs = divmod(TS, 60)
ans = math.inf
if mins < 100:
ans = min(ans, helper(mins, secs))
if secs < 40:
ans = min(ans, helper(mins - 1, secs + 60))
return ans
You are given a 0-indexed integer array nums
consisting of 3 * n
elements.
You are allowed to remove any subsequence of elements of size exactly n
from nums
. The remaining 2 * n
elements will be divided into two equal parts:
n
elements belonging to the first part and their sum is sumfirst
.n
elements belonging to the second part and their sum is sumsecond
.The difference in sums of the two parts is denoted as sumfirst - sumsecond
.
sumfirst = 3
and sumsecond = 2
, their difference is 1
.sumfirst = 2
and sumsecond = 3
, their difference is -1
.Return the minimum difference possible between the sums of the two parts after the removal of n
elements.
Example 1:
Input: nums = [3,1,2]
Output: -1
Explanation: Here, nums has 3 elements, so n = 1.
Thus we have to remove 1 element from nums and divide the array into two equal parts.
- If we remove nums[0] = 3, the array will be [1,2]. The difference in sums of the two parts will be 1 - 2 = -1.
- If we remove nums[1] = 1, the array will be [3,2]. The difference in sums of the two parts will be 3 - 2 = 1.
- If we remove nums[2] = 2, the array will be [3,1]. The difference in sums of the two parts will be 3 - 1 = 2.
The minimum difference between sums of the two parts is min(-1,1,2) = -1.
Example 2:
Input: nums = [7,9,5,8,1,3]
Output: 1
Explanation: Here n = 2. So we must remove 2 elements and divide the remaining array into two parts containing two elements each.
If we remove nums[2] = 5 and nums[3] = 8, the resultant array will be [7,9,1,3]. The difference in sums will be (7+9) - (1+3) = 12.
To obtain the minimum difference, we should remove nums[1] = 9 and nums[4] = 1. The resultant array becomes [7,5,8,3]. The difference in sums of the two parts is (7+5) - (8+3) = 1.
It can be shown that it is not possible to obtain a difference smaller than 1.
Constraints:
nums.length == 3 * n
1 <= n <= 105
1 <= nums[i] <= 105
def minimumDifference(self, A: List[int]) -> int:
n = len(A) // 3
# Build pre_min using min-heap.
pre_min, cur_min = [sum(A[:n])], sum(A[:n])
pre_hp = [-x for x in A[:n]]
heapq.heapify(pre_hp)
for i in range(n, 2 * n):
cur_pop = -heapq.heappop(pre_hp)
cur_min -= cur_pop
cur_min += min(cur_pop, A[i])
pre_min.append(cur_min)
heapq.heappush(pre_hp, -min(cur_pop, A[i]))
# Build suf_max.
suf_max, cur_max = [sum(A[2*n:])], sum(A[2*n:])
suf_hp = [x for x in A[2*n:]]
heapq.heapify(suf_hp)
for i in range(2 * n - 1, n - 1, -1):
cur_pop = heapq.heappop(suf_hp)
cur_max -= cur_pop
cur_max += max(cur_pop, A[i])
suf_max.append(cur_max)
heapq.heappush(suf_hp, max(cur_pop, A[i]))
suf_max = suf_max[::-1]
# Iterate over pre_min and suf_max and get the minimum difference.
ans = math.inf
for a, b in zip(pre_min, suf_max):
ans = min(ans, a - b)
return ans
class Solution {
public:
long long minimumDifference(vector<int>& A) {
priority_queue<int> L; // storing the smallest N digits in the first part
priority_queue<int,vector<int>, greater<>> R; // storing the greatest N digits in the right part
long N = A.size() / 3, left = 0, right = 0, ans = LLONG_MAX;
vector<long> tmp(A.size());
for (int i = A.size() - 1; i >= N; --i) { // calculate the greatest N digits in the right part
R.push(A[i]);
right += A[i];
if (R.size() > N) {
right -= R.top();
R.pop();
}
if (R.size() == N) tmp[i] = right; // `tmp[i]` is the maximum sum of `N` digits in `A[i:]`
}
for (int i = 0; i < A.size() - N; ++i) {
L.push(A[i]);
left += A[i];
if (L.size() > N) {
left -= L.top();
L.pop();
}
if (L.size() == N) ans = min(ans, left - tmp[i + 1]);
}
return ans;
}
};
class Solution {
public long minimumDifference(int[] nums) {
int n = nums.length / 3;
PriorityQueue<Integer> left = new PriorityQueue<>((a, b) -> b - a);
PriorityQueue<Integer> right = new PriorityQueue<>();
long[] minLeft = new long[3 * n];
long leftSum = 0;
long rightSum = 0;
for (int i = 0; i < n; i++) {
leftSum += nums[i];
left.add(nums[i]);
}
minLeft[n-1] = leftSum;
for (int i = n; i < 2 * n; i++) {
leftSum += nums[i];
left.add(nums[i]);
leftSum -= left.poll();
minLeft[i] = leftSum;
}
for (int i = 3 * n - 1; i >= 2 * n; i--) {
rightSum += nums[i];
right.add(nums[i]);
}
long res = minLeft[2 * n - 1] - rightSum;
for (int i = 2 * n - 1; i >= n; i--) {
rightSum += nums[i];
right.add(nums[i]);
rightSum -= right.poll();
res = Math.min(res, minLeft[i - 1] - rightSum);
}
return res;
}
}
def minimumDifference(self, nums: List[int]) -> int:
n = len(nums) // 3
# two heap queues, we keep n smallest elements in first, and n greatest elements in second
first, second = list(), list()
sum_1, sum_2 = 0, 0
for i in range(n):
heapq.heappush(first, -nums[i])
sum_1 += nums[i]
for i in range(2 * n, 3 * n):
heapq.heappush(second, nums[i])
sum_2 += nums[i]
# divide nums into two parts: nums[:i] & nums[i + 1:]
# i can only be in range(n + 1, 2 * n)
sum_first = [sum_1] # sum_first stores sum of n smallest elements for i in range(n + 1, 2 * n)
sum_second = [sum_2] # sum_second stores sum of n greatest elements for i in range(2 * n - 1, n, -1)
middle = nums[n : 2 * n]
for i in middle:
rep = heapq.heappushpop(first, -i)
sum_1 += i + rep
sum_first.append(sum_1)
for i in middle[::-1]:
rep = heapq.heappushpop(second, i)
sum_2 += i - rep
sum_second.append(sum_2)
# then sum_first[k] & sum_second[n - 1 - k] will be a pair for the same i
return min(i - j for i, j in zip(sum_first, sum_second[::-1]))
public class Solution {
public long MinimumDifference(int[] nums) {
PriorityQueue<long,long> left = new PriorityQueue<long,long>();
PriorityQueue<long,long> right = new PriorityQueue<long,long>();
int n = nums.Length/3;
long[] sumLeft = new long[n+1];
long[] sumRight = new long[n+1];
long tLeft = 0;
long tRight = 0;
for(int i=0;i<n;i++){
tLeft += nums[i];
left.Enqueue(nums[i],-nums[i]);
tRight += nums[nums.Length-1-i];
right.Enqueue(nums[nums.Length-1-i],nums[nums.Length-1-i]);
}
sumLeft[0] = tLeft;
sumRight[0]= tRight;
for(int i=1;i<n+1;i++){
long t = left.Dequeue();
if(t>nums[n-1+i]){
sumLeft[i] = sumLeft[i-1]-t+nums[n-1+i];
left.Enqueue(nums[n-1+i],-nums[n-1+i]);
}else{
left.Enqueue(t,-t);
sumLeft[i] = sumLeft[i-1];
}
}
for(int i=1;i<n+1;i++){
long t = right.Dequeue();
if(t<nums[2*n-i]){
sumRight[i] = sumRight[i-1]-t+nums[2*n-i];
right.Enqueue(nums[2*n-i],nums[2*n-i]);
}else{
right.Enqueue(t,t);
sumRight[i] = sumRight[i-1];
}
}
long res = sumLeft[0];
for(int i=0;i<n+1;i++)
res = Math.Min(res,sumLeft[i]-sumRight[n-i]);
return res;
}
}
In our experience, we suggest you solve this Biweekly Contest 71 LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.
If you are stuck anywhere between any coding problem, just visit Queslers to get the Biweekly Contest 71 LeetCode Solution
I hope this Biweekly Contest 71 LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.
This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.
Keep Learning!
More Coding Solutions >>