Daily Archives: October 13, 2021

C# || How To Multiply Two Strings Using C#

The following is a module with functions which demonstrates how to multiply two strings together using C#.


1. Multiply Strings – Problem Statement

Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.

Note: You must not use any built-in BigInteger library or convert the inputs to integer directly.

Example 1:


Input: num1 = "2", num2 = "3"
Output: "6"

Example 2:


Input: num1 = "123", num2 = "456"
Output: "56088"


2. Multiply Strings – Solution

The following is a solution which demonstrates how to multiply two strings together.

This solution starts from the end of both strings and multiplies each individual number, keeping track of any carryovers. The result of each operation is stored in a ‘solution’ array, and when the operation is complete, the result is returned as a string.

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:


"6"
"56088"

C# || How To Add Two Binary Strings Using C#

The following is a module with functions which demonstrates how to add two binary strings together using C#.


1. Add Binary – Problem Statement

Given two binary strings a and b, return their sum as a binary string.

Example 1:


Input: a = "11", b = "1"
Output: "100"

Example 2:


Input: a = "1010", b = "1011"
Output: "10101"


2. Add Binary – Solution

The following is a solution which demonstrates how to add two binary strings together.

In this solution, we start at the end of both strings, and perform basic math on each number, adding them together. If any mathematical carry over is required, that is added to the next loop iteration.

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:


"100"
"10101"

C# || How To Traverse A Binary Tree Postorder Using C#

The following is a module with functions which demonstrates how to traverse a binary tree post order using C#.


1. Binary Tree Traversal – Problem Statement

Given the root of a binary tree, return the postorder traversal of its nodes’ values.

Example 1:

Example 1


Input: root = [1,null,2,3]
Output: [3,2,1]

Example 2:


Input: root = []
Output: []

Example 3:


Input: root = [1]
Output: [1]

Example 4:

Example 4


Input: root = [1,2]
Output: [2,1]

Example 5:

Example 5


Input: root = [1,null,2]
Output: [2,1]


2. Binary Tree Traversal – Solution

The following is a solution which demonstrates how to traverse a binary tree post order.

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,2,1]
[]
[1]
[2,1]
[2,1]

C# || How To Traverse A Binary Tree Preorder Using C#

The following is a module with functions which demonstrates how to traverse a binary tree pre order using C#.


1. Binary Tree Traversal – Problem Statement

Given the root of a binary tree, return the preorder traversal of its nodes’ values.

Example 1:

Example 1


Input: root = [1,null,2,3]
Output: [1,2,3]

Example 2:


Input: root = []
Output: []

Example 3:


Input: root = [1]
Output: [1]

Example 4:

Example 4


Input: root = [1,2]
Output: [1,2]

Example 5:

Example 5


Input: root = [1,null,2]
Output: [1,2]


2. Binary Tree Traversal – Solution

The following is a solution which demonstrates how to traverse a binary tree pre order.

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:


[1,2,3]
[]
[1]
[1,2]
[1,2]

C# || How To Traverse A Binary Tree Inorder Using C#

The following is a module with functions which demonstrates how to traverse a binary tree in order using C#.


1. Binary Tree Traversal – Problem Statement

Given the root of a binary tree, return the inorder traversal of its nodes’ values.

Example 1:

Example 1


Input: root = [1,null,2,3]
Output: [1,3,2]

Example 2:


Input: root = []
Output: []

Example 3:


Input: root = [1]
Output: [1]

Example 4:

Example 4


Input: root = [1,2]
Output: [2,1]

Example 5:

Example 5


Input: root = [1,null,2]
Output: [1,2]


2. Binary Tree Traversal – Solution

The following is a solution which demonstrates how to traverse a binary tree in order.

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:


[1,3,2]
[]
[1]
[2,1]
[1,2]

C# || How To Determine The Maximum Units On A Truck Using C#

The following is a module with functions which demonstrates how to determine the maximum units on a truck using C#.


1. Maximum Units – Problem Statement

You are assigned to put some amount of boxes onto one truck. You are given a 2D array boxTypes, where boxTypes[i] = [numberOfBoxes, numberOfUnitsPerBox]:


numberOfBoxes is the number of boxes of type i.
numberOfUnitsPerBox is the number of units in each box of the type i

You are also given an integer truckSize, which is the maximum number of boxes that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed truckSize.

Return the maximum total number of units that can be put on the truck.

Example 1:


Input: boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
Output: 8
Explanation: There are:
- 1 box of the first type that contains 3 units.
- 2 boxes of the second type that contain 2 units each.
- 3 boxes of the third type that contain 1 unit each.
You can take all the boxes of the first and second types, and one box of the third type.
The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8.

Example 2:


Input: boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
Output: 91


2. Maximum Units – Solution

The following is a solution which demonstrates how to determine the maximum units on a truck.

In this solution, we start with the box with the most units. The box types are sorted by the number of units per box in descending order. Then, the box types are iterated over, taking from each type as many as possible.

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:


8
91