Rod Cutting Problem in C#

 The rod-cutting problem is the classic dynamic programming problem. In this problem, we cut a rod of a given length into smaller pieces with certain prices for each piece.

As per this problem, we need to determine the maximum revenue profit that can be get by cutting up the rod and selling the pieces of rod.

Problem statement

Given a rod of length n and we need to cut the rod in such a way that we need to sell it for the maximum revenue profit. We also have the price table, which tells us the worth of the rod.

Length =12345
Price =156910

So, according to the above table, if we cut the rod of length 1 we can sell it for $1.

Similarly, if we cut the rod at length 2, we can sell it at $5 and if we cut the rod at length 3, we can sell it at $6.

According to the problem, we need to find a way, how to cut the rod so that we can get maximum profit.

Here rod length is 5, so if we give the whole rod, without cutting it so the profit is $10. But if we cut the rod at length 2 and at length 3 (so total rod size is 2+3 = 5), then the total profit will be 5 + 6 = 11. This gives us maximum profit.

Dynamic programming solution

1static void Main(string[] args) {
2 int[] price = new int[] {3 1,4 5,5 6,6 9,7 108 };
9 int length = price.Length;
10 Console.WriteLine("Total Profit is :- " + Class1.TotalProfitToCutRod(price, length));
11}
12public static int TotalProfitToCutRod(int[] p, int n) {
13 int[] r = new int[n + 1];
14 r[0] = 0;
15 for (int j = 1; j <= n; j++) {
16 int q = int.MinValue;
17 for (int i = 1; i <= j; i++) {
18 q = Math.Max(q, p[i - 1] + r[j - i]);
19 }
20 r[j] = q;
21 }
22 return r[n];
23}
C#

In this implementation, p is the array of prices for each piece length of rod and n is the length of the rod. This function returns the maximum profit that we get by cutting up the rod.

Naive recursive solution

1static void Main(string[] args) {
2 int[] price = new int[] {3 1,4 5,5 6,6 9,7 108 };
9 int length = price.Length;
10 Console.WriteLine("Total Profit is :- " + Class1.TotalProfitToCutRod(price, length));
11}
12public static int TotalProfitToCutRod(int[] p, int n) {
13 if (n <= 0) return 0;
14 int maximum_val = int.MinValue;
15 for (int i = 0; i < n; i++) maximum_val = Math.Max(maximum_val, p[i] + TotalProfitToCutRod(p, n - i - 1));
16 return maximum_val;
17}
C#



 

----------------------------------------------------------------

Microsoft extend partnership with OpenAI

Microsoft extend partnership with OpenAI


Today (23-Jan-23) Microsoft announced a long-term partnership with OpenAI, through a multiyear, and multibillion-dollar investment.

It was rumored that Microsoft is going to do investment in OpenAI to extend the AI to give its benefits to the world and today it is officially announced.

Microsoft extend partnership with OpenAI

ChatGPT (Generative Pre-trained Transformer) is the chatbot developed by OpenAI on November 30, 2022.

Previously we discussed OpenAI and we have seen how to create a ChatGTP application using OpenAI, 

For more please follow the below link 👇

Microsoft and OpenAI have had a partnership in the past, with Microsoft investing multibillion dollars in OpenAI to support the development of advanced AI technologies and to build large-scale AI supercomputing capabilities. The partnership aims to develop AI solutions that can help businesses and organizations achieve more, such as natural language processing, computer vision, and reinforcement learning.

This agreement between Microsoft and OpenAI follows their previous investments in 2019 and 2021. It extends their ongoing collaboration across AI supercomputing and research and enables each of us to independently commercialize the result of advanced AI technologies.

Supercomputing at scale 

Now Microsoft invest in OpenAI for enhancing the development and deployment of supercomputing systems to grow OpenAI, even they will build Azure’s enhancing AI infrastructure to help users to build and deploy their AI applications widely.

New AI-powered experiences 

Now Microsoft utilizes OpenAI to enhance user experience in many products like development and deployment etc.

Exclusive cloud provider 

As OpenAI is the exclusive cloud provider, Azure will power all OpenAI workloads across research, products, and API services.

“The past three years of our partnership have been great,” says OpenAI CEO Sam Altman. “Microsoft shares our values and we are excited to continue our independent research and work toward creating advanced AI that benefits everyone.”

Microsoft purchased the exclusive license for the underlying technology behind the GPT-3 in 2020 just after investing $1 billion in OpenAI in 2019. It has built a relationship with OpenAI and they are also planning to add the AI text-to-image model to Bing powered by OpenAI’s DALL-E 2.

Reference:- https://blogs.microsoft.com/blog/2023/01/23/microsoftandopenaiextendpartnership/

Find total number of ways to reach the n’th stair from the bottom in C#

  In this article, we will see how to find total ways to reach the n’th stair from the bottom in C#

Given a staircase, where we need to find the total number of ways to reach to the n'th stair from the bottom of the staircase.

A person can only climb either the 1 or 2 stairs at a time. 

To understand it let's take an example,

The total number of ways to reach the 4th stair is 5,
 
1 step + 1 step + 1 step + 1 step
2 steps + 1 step + 1 steps
1 step + 1 steps + 2 steps
1 step + 2 steps + 1 steps
2 steps + 2 steps

Problem

Let's understand T(n) is the total number of ways to reach the n'th stair from the bottom form staircase. Since a person is only allowed to climb either 1 or 2 stairs at a time, so the person can reach the n'th stair from either the (n-1)'th stair, (n-2)'th stair.

Considering this is the recurrence relation T(n) , can be written as:

T(n) = T(n-1) + T(n-2) , where n >= 0 and the
T(0) = 1, T(1) = 1

Problem-solving 

There are many ways to perform to count the number of ways to reach the nth stair in C#.

One common way to is the Bottom-up/ dynamic approach and another is the Top-up/Recursive approach.

Bottom-Up Approach/ dynamic approach

The bottom-up approach uses dynamic programming, where it starts by calculating the number of ways to reach the first few staircases (in this case, the first two stairs) and then uses these values to calculate the number of ways to reach higher stairs.

static void Main(string[] args) {
    Console.WriteLine("give number of stairs to reach");
    int number = Convert.ToInt16(Console.ReadLine());
    Console.WriteLine("Number of ways to reach:- " + Class1.CountStairWays(number));
}
public static int CountStairWays(int stairNumber) {
    if (stairNumber <= 0) {
        return 0;
    } else if (stairNumber == 1) {
        return 1;
    } else if (stairNumber == 2) {
        return 2;
    }
    int[] stairArray = new int[stairNumber + 1];
    stairArray[0] = 0;
    stairArray[1] = 1;
    stairArray[2] = 2;
    for (int i = 3; i <= stairNumber; i++) {
        stairArray[i] = stairArray[i - 1] + stairArray[i - 2];
    }
    return stairArray[stairNumber];
}
C#

The time complexity of the above solution is O(n), where n is the size of the input, And The space complexity of the above solution is O(n).

Top-up approach/ recursive function and memorization

This function uses the top-down approach, where it starts by trying to find the number of ways to reach the nth stair by breaking the problem down into smaller sub-problems (in this case, finding the number of ways to reach the n-1th and n-2nd stairs) and then combining the solutions to these subproblems to find the solution to the original problem.

Note that the above examples uses the Fibonacci sequence, where each number is the sum of the two preceding ones, starting from 0 and 1.

static void Main(string[] args) {
    Console.WriteLine("give number of stairs to reach");
    int number = Convert.ToInt16(Console.ReadLine());
    int[] stairArray = new int[number + 1];
    Console.WriteLine("Number of ways to reach:- " + Class1.CountStairWaysUsingRecursive(number, stairArray));
}
public static int CountStairWaysUsingRecursive(int stairNumber, int[] stairArray) {
    if (stairNumber <= 0) {
        return 0;
    } else if (stairNumber == 1) {
        return 1;
    } else if (stairNumber == 2) {
        return 2;
    }
    if (stairArray[stairNumber] == 0) {
        stairArray[stairNumber] = CountStairWaysUsingRecursive(stairNumber - 1, stairArray) + CountStairWaysUsingRecursive(stairNumber - 2, stairArray);
    }
    return stairArray[stairNumber];
}
C#

Output





----------------------------------------------