## Monthly Archives: July 2023

## C# || Fruit Into Baskets – How To Find The Maximum Number Of Fruits Using C#

The following is a module with functions which demonstrates how to find the maximum number of fruits using C#.

1. Total Fruit – Problem Statement

You are visiting a farm that has a single row of fruit trees arranged from left to right. The trees are represented by an integer array **fruits** where **fruits[i]** is the **type** of fruit the **i ^{th}** tree produces.

You want to collect as much fruit as possible. However, the owner has some strict rules that you must follow:

- You only have
**two**baskets, and each basket can only hold a**single type**of fruit. There is no limit on the amount of fruit each basket can hold. - Starting from any tree of your choice, you must pick
**exactly one fruit**from**every**tree (including the start tree) while moving to the right. The picked fruits must fit in one of your baskets. - Once you reach a tree with fruit that cannot fit in your baskets, you must stop.

Given the integer array **fruits**, return *the maximum number of fruits you can pick*.

**Example 1:**

Input:fruits = [1,2,1]

Output:3

Explanation:We can pick from all 3 trees.

**Example 2:**

Input:fruits = [0,1,2,2]

Output:3

Explanation:We can pick from trees [1,2,2].

If we had started at the first tree, we would only pick from trees [0,1].

**Example 3:**

Input:fruits = [1,2,3,2,2]

Output:4

Explanation:We can pick from trees [2,3,2,2].

If we had started at the first tree, we would only pick from trees [1,2].

2. Total Fruit – Solution

The following is a solution which demonstrates how to find the maximum number of fruits.

```
```
12345678910111213141516171819202122232425262728293031323334353637
// ============================================================================// Author: Kenneth Perkins// Date: Jul 28, 2023// Taken From: http://programmingnotes.org/// File: Solution.cs// Description: Demonstrates how to find the maximum number of fruits// ============================================================================public class Solution { public int TotalFruit(int[] fruits) { // We use a hash map 'basket' to store the number of each type of fruit. var basket = new Dictionary<int, int>(); int left = 0; int maxPicked = 0; // Add fruit from the right index (right) of the window. for (int right = 0; right < fruits.Length; ++right) { basket[fruits[right]] = basket.GetValueOrDefault(fruits[right], 0) + 1; // If the current window has more than 2 types of fruit, // we remove fruit from the left index (left) of the window, // until the window has only 2 types of fruit. while (basket.Count > 2) { basket[fruits[left]] = basket[fruits[left]] - 1; if (basket[fruits[left]] == 0) { basket.Remove(fruits[left]); } ++left; } // Update maxPicked. maxPicked = Math.Max(maxPicked, right - left + 1); } // Return maxPicked as the maximum number of fruits we can collect. return maxPicked; }}// http://programmingnotes.org/

**QUICK NOTES**:

The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output for the example cases:

3

3

4

## C# || How To Find The Minimum Rounds To Complete All Tasks Using C#

The following is a module with functions which demonstrates how to find the minimum rounds to complete all tasks using C#.

1. Minimum Rounds – Problem Statement

You are given a **0-indexed** integer array **tasks**, where **tasks[i]** represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the **same difficulty level**.

Return *the minimum rounds required to complete all the tasks, or *

**-1**

*if it is not possible to complete all the tasks.*

**Example 1:**

Input:tasks = [2,2,3,3,2,4,4,4,4,4]

Output:4

Explanation:To complete all the tasks, a possible plan is:

- In the first round, you complete 3 tasks of difficulty level 2.

- In the second round, you complete 2 tasks of difficulty level 3.

- In the third round, you complete 3 tasks of difficulty level 4.

- In the fourth round, you complete 2 tasks of difficulty level 4.

It can be shown that all the tasks cannot be completed in fewer than 4 rounds, so the answer is 4.

**Example 2:**

Input:tasks = [2,3,3]

Output:-1

Explanation:There is only 1 task of difficulty level 2, but in each round, you can only complete either 2 or 3 tasks of the same difficulty level. Hence, you cannot complete all the tasks, and the answer is -1.

2. Minimum Rounds – Solution

The following is a solution which demonstrates how to find the minimum rounds to complete all tasks.

```
```
123456789101112131415161718192021222324252627282930313233343536
// ============================================================================// Author: Kenneth Perkins// Date: Jul 1, 2023// Taken From: http://programmingnotes.org/// File: Solution.cs// Description: Demonstrates how to find minimum rounds to complete tasks// ============================================================================public class Solution { public int MinimumRounds(int[] tasks) { var freq = new Dictionary<int, int>(); // Store the frequencies in the map. foreach (var task in tasks) { freq[task] = freq.GetValueOrDefault(task, 0) + 1; } int minimumRounds = 0; // Iterate over the task's frequencies. foreach (int count in freq.Values) { // If the frequency is 1, it's not possible to complete tasks. if (count == 1) { return - 1; } if (count % 3 == 0) { // Group all the task in triplets. minimumRounds += count / 3; } else { // If count % 3 = 1; (count / 3 - 1) groups of triplets and 2 pairs. // If count % 3 = 2; (count / 3) groups of triplets and 1 pair. minimumRounds += (count / 3) + 1; } } return minimumRounds; }}// http://programmingnotes.org/

**QUICK NOTES**:

The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output for the example cases:

4

-1