Skip to content

jerkdavi/LeetCode-Problem-1672-Solution-CSharp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SOLUTION TO LEETCODE PROBLEM 1672 - C#

This is my solution to LeetCode Problem 1672 - Richest Customer Wealth - in C# and a brief explanation

Link to problem on LeetCode -> https://leetcode.com/problems/richest-customer-wealth/

PROJECT EXPLANATION

I have created a new project in Visual Studio 2022 Community Edition, selected the Console App project template, named the project RichestCustomerWealth, selected .NET Core 3.1 (Out of support) Framework.

I have two classes, Program class and Solution class.

CODE EXPLANATION

Program class

I am using the automatically generated Program class and Main function.

internal class Program
{
    static void Main(string[] args)
    {

Firstly, the accounts numeric (int) 2D array (int[][]) is initialized by using basic 2D array initialization. This part of code is commented out currently.

int[][] accounts =
{
    new int[] { 1, 2, 3 },
    new int[] { 3, 2, 1 }
};

Currently, the nums numeric (int) 2D array is initialized by using next structure:

  • Console Write -> where it displays that the number of accounts needs to be inputted [1. dimension].
  • int input1 = int Parse Console ReadLine -> where user input is stored into a numeric input (int).
  • Console Write -> where it displays that the number of values needs to be inputted [2. dimension].
  • int input2 = int Parse Console ReadLine -> where user input is stored into a numeric input (int).
  • int[][] array = new int[input1][] -> where new 2D array is initialized and instantiated with the length specified in input1.
  • for loop -> which iterates from 0 to input1.
  • array[i] = new int[input2] -> where new 1D array is initialized and instantiated with the length specified in input1.
  • for loop -> which iterates from 0 to input2.
  • Console Write -> where it displays which array member's value needs to be inputted.
  • array[i][j] = int Parse Console ReadLine -> where user input is stored into the array.
Console.Write("What is the number of accounts: ");
int accountsLength = int.Parse(Console.ReadLine());
Console.Write("What is the number of values: ");
int valuesLength = int.Parse(Console.ReadLine());
int[][] accounts = new int[accountsLength][];
for (int i = 0; i < accountsLength; i++)
{
    accounts[i] = new int[valuesLength];
    for (int j = 0; j < valuesLength; j++)
    {
        Console.Write($"Enter account[{i + 1}][{j + 1}] value: ");
        accounts[i][j] = int.Parse(Console.ReadLine());
    }
}

CODE EXPLAINED IN GREATER DETAIL
- When initializing a 2D array and at once filling it with values, notice that array Length does not need to be specified. This is because the Length of the array is implied by the number of array submembers and their values provided. Also, array does not need to be specifically instantiated (new int[input][]). This is because instantiation is implied.

- When initializing a 2D array first before filling it with values, array Length needs to be specified and array needs to be specifically instantiated (new int[input1][]).

- When initializing each of the array submembers first before filling it with values, array Length needs to be specified and array needs to be specifically instantiated (new int[input2]).

- Notice that you cannot at the start instantiate the 2D array by providing both Length values (new int[input1][input2]). You need to instantiate array submembers one at a time, after instantiating the 2D array itself.

- Console.ReadLine always takes user input in a text format (string) . To convert the text input (string) into a numeric input (int) -> int Parse needs to be used.

- 2D array submembers can be accessed by using array[i]. 2D array values can be accessed by using array[i][j].

- Notice that you could have 1D arrays with different number of values stored inside.

Next, the MaximumWealth function from Solution class is called. It takes a 2D array (int[][] array) and returns an int (int maxAccountSum). The returned int is stored into a new int named maxAccountSum. The returned int is then printed out to the console.

int maxAccountSum = Solution.MaximumWealth(accounts);
Console.WriteLine(maxAccountSum);

At the end of the Program class there is next structure:

  • Console WriteLine -> where it displays that the code is done processing.
  • Console ReadLine -> where it waits for user input.

WHY DOES CONSOLE READLINE WAIT FOR USER INPUT?
To stop the code from automatically completing and exiting and us not being able to check the result of the code.

Console.WriteLine("Done processing");
Console.ReadLine();

Solution class

I have created a new class and named it Solution. I have made it public. I have created the MaximumWealth function and mirrored the example provided in the LeetCode problem. I have made it static.

public class Solution
{
    public static int MaximumWealth(int[][] accounts)
    {

Firstly, the maxAccountSum numeric input (int) is initialized by using basic int initialization. It is set to 0. This numeric input (int) will be used to store the biggest account sum into and return it at the end of the function.

int maxAccountSum = 0;

Secondly, the for loop is initialized that iterates from 0 to array Length.

for (int i = 0; i < accounts.Length; i++)
{

The currentAccountSum numeric input (int) is initialized by using basic int initialization. It is set to 0. This numeric input (int) will be used to store current account sum into and to check if it is greater then the maxAccountSum.


WHY IS IT INITIALIZED HERE AND NOT BEFORE THE FOR LOOP?
Because it needs to be set to 0 at every for loop iteration to correctly sum all of current account values.

int currentAccountSum = 0;

Inside the for loop, there is another for loop initialized that iterates from 0 to array[i] Length.

for (int j = 0; j < accounts[i].Length; j++)
{

Every value inside the array[i][j] is being summed into the currentAccountSum numeric input (int).

currentAccountSum += accounts[i][j];

Outside the second for loop, but inside the first one is the if statement. It checks if currentAccountSum is greater than the maxAccountSum. If it is greater, it stores the value of currentAccountSum into the maxAccountSum.

if (currentAccountSum > maxAccountSum)
{
    maxAccountSum = currentAccountSum;
}

At the end of the Solution class, the maxAccountSum numeric input (int) is returned.

return maxAccountSum;

POTENTIAL MISTAKES (I HAVE MADE)

None that I am aware of.

Any advice regarding making the code MORE TIME AND SPACE EFFICIENT is MORE THAN WELCOME.

RUNNING THE CODE - RESULT

image image image

About

Solution to LeetCode Problem 1672 - C#

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages