6323. Distribute Money to Maximum Children
You are given an integer money denoting the amount of money (in dollars) that you have and another integer children denoting the number of children that you must distribute the money to.
You have to distribute the money according to the following rules:
- All money must be distributed.
- Everyone must receive at least
1dollar. - Nobody receives
4dollars.
Return the maximum number of children who may receive exactly 8 dollars if you distribute the money according to the aforementioned rules. If there is no way to distribute the money, return -1.
Example 1:
Input: money = 20, children = 3
Output: 1
Explanation:
The maximum number of children with 8 dollars will be 1. One of the ways to distribute the money is:
- 8 dollars to the first child.
- 9 dollars to the second child.
- 3 dollars to the third child.
It can be proven that no distribution exists such that number of children getting 8 dollars is greater than 1.
Example 2:
Input: money = 16, children = 2
Output: 2
Explanation: Each child can be given 8 dollars.
Constraints:
1 <= money <= 2002 <= children <= 30
CODE:
class Solution {
public:
int distMoney(int money, int children) {
// vector<int> child;
// child[0] = 0;
if(money < children)
{
return -1;
}
if(money < 8)
{
return 0;
}
int a = money/8;
int b = money%8;
if(a == children && b == 0)
{
return children;
}
if(a >= children)
{
return children - 1;
}
if(b == 4 && children - a == 1)
{
return children - 2;
}
if(b >= (children-a))
{
return a;
}
int count = children - a;
while(b < count)
{
count++;
b = b+8;
a--;
}
return a;
}
};
6351. Find Score of an Array After Marking All Elements
You are given an array nums consisting of positive integers.
Starting with score = 0, apply the following algorithm:
- Choose the smallest integer of the array that is not marked. If there is a tie, choose the one with the smallest index.
- Add the value of the chosen integer to
score. - Mark the chosen element and its two adjacent elements if they exist.
- Repeat until all the array elements are marked.
Return the score you get after applying the above algorithm.
Example 1:
Input: nums = [2,1,3,4,5,2]
Output: 7
Explanation: We mark the elements as follows:
- 1 is the smallest unmarked element, so we mark it and its two adjacent elements: [2,1,3,4,5,2].
- 2 is the smallest unmarked element, so we mark it and its left adjacent element: [2,1,3,4,5,2].
- 4 is the only remaining unmarked element, so we mark it: [2,1,3,4,5,2].
Our score is 1 + 2 + 4 = 7.
Example 2:
Input: nums = [2,3,5,1,3,2]
Output: 5
Explanation: We mark the elements as follows:
- 1 is the smallest unmarked element, so we mark it and its two adjacent elements: [2,3,5,1,3,2].
- 2 is the smallest unmarked element, since there are two of them, we choose the left-most one, so we mark the one at index 0 and its right adjacent element: [2,3,5,1,3,2].
- 2 is the only remaining unmarked element, so we mark it: [2,3,5,1,3,2].
Our score is 1 + 2 + 2 = 5.
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 106
Code:[Time Limit Exceeded]
class Solution {
public:
// void print(vector<int> & nums)
// {
// for(int i=0; i<nums.size(); i++)
// {
// cout << nums[i] << " ";
// }
// cout << "\n";
// }
long long minimum(vector<int>& nums, long long min)
{
for(int i=0; i<nums.size(); i++)
{
if(nums[i] > 0)
{
min = nums[i];
break;
}
}
for(int i=0; i<nums.size(); i++)
{
if(min > nums[i] && nums[i] > 0)
{
min = nums[i];
}
}
// cout << min << endl;
return min;
}
long long findScore(vector<int>& nums) {
long long score = 0;
long long min = 0;
long long min_a;
int count = 0;
for(int k=0; k<nums.size(); k++)
{
min_a = minimum(nums, min);
for(int i=0; i<nums.size(); i++)
{
if(nums[i] == min_a)
{
score = score + min_a;
// cout << "Score" << score << "\n";
if(i > 0 && i < nums.size()-1)
{
nums[i-1] = -1;
nums[i+1] = -1;
nums[i] = -1;
}
else if(i > 0 && i == nums.size()-1)
{
nums[i] = -1;
nums[i-1] = -1;
}
else if(i == 0 && i < nums.size()-1)
{
nums[i] = -1;
nums[i+1] = -1;
}
}
// print(nums);
for(int j=0; j<nums.size(); j++)
{
if(nums[j] == -1)
{
count++;
}
}
if(count == nums.size())
{
break;
}
}
}
return score;
}
};

Leave a comment