Advertisement
LikeRampage

C++/C#/Python leetcode 343. Integer Break

Nov 12th, 2024
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.99 KB | Source Code | 0 0
  1. Certainly! Below is a C++ solution for the LeetCode problem 343, "Integer Break". The problem requires you to maximize the product of integers that sum up to a given integer n by breaking it into at least two positive integers.
  2.  
  3. C++ Code:
  4. #include <iostream>
  5. #include <vector>
  6.  
  7. class Solution {
  8. public:
  9.     int integerBreak(int n) {
  10.         // Create a dp array to store the maximum product for each integer
  11.         std::vector<int> dp(n + 1, 0);
  12.        
  13.         // Base case: for integers 1 and 2
  14.         dp[1] = 1; // 1 cannot be broken down
  15.         dp[2] = 1; // 2 can only be broken into 1 + 1
  16.        
  17.         // Fill the dp array for all integers from 3 to n
  18.         for (int i = 3; i <= n; ++i) {
  19.             for (int j = 1; j < i; ++j) {
  20.                 // Maximize the product by considering breaking i into j and (i - j)
  21.                 dp[i] = std::max(dp[i], std::max(j * (i - j), j * dp[i - j]));
  22.             }
  23.         }
  24.        
  25.         return dp[n];
  26.     }
  27. };
  28.  
  29. int main() {
  30.     Solution solution;
  31.    
  32.     // Test with some examples
  33.     int n = 10; // Example input
  34.     std::cout << "Maximum product for integer break of " << n << " is: " << solution.integerBreak(n) << std::endl;
  35.  
  36.     return 0;
  37. }
  38. Explanation:
  39. Dynamic Programming Array: We create a dp array where dp[i] holds the maximum product obtainable by breaking the integer i.
  40. Base Cases:
  41. For n = 1, the only option is 1, hence dp[1] = 1.
  42. For n = 2, the only option is 1 + 1, hence dp[2] = 1.
  43. Filling the DP Array:
  44. For each integer i from 3 to n, we check all possible ways to break i into two parts j and i - j.
  45. We calculate the maximum product by comparing the product of j * (i - j) and j * dp[i - j] to ensure that we also consider previous results stored in the dp array.
  46. Return the Result: Finally, we return dp[n], which contains the maximum product for the integer n.
  47. Example Usage:
  48. You can change the value of n in the main function to test with different inputs. The output will show the maximum product for the integer break of the given n.
  49.  
  50. Sure! Below is a C# solution for the LeetCode problem 343, "Integer Break". This problem involves breaking an integer n into at least two positive integers such that their product is maximized.
  51.  
  52. C# Code:
  53. using System;
  54.  
  55. public class Solution {
  56.     public int IntegerBreak(int n) {
  57.         // Create a dp array to store the maximum product for each integer
  58.         int[] dp = new int[n + 1];
  59.  
  60.         // Base cases
  61.         dp[1] = 1; // 1 cannot be broken down
  62.         dp[2] = 1; // 2 can only be broken into 1 + 1
  63.  
  64.         // Fill the dp array for all integers from 3 to n
  65.         for (int i = 3; i <= n; i++) {
  66.             for (int j = 1; j < i; j++) {
  67.                 // Maximize the product by considering breaking i into j and (i - j)
  68.                 dp[i] = Math.Max(dp[i], Math.Max(j * (i - j), j * dp[i - j]));
  69.             }
  70.         }
  71.  
  72.         return dp[n];
  73.     }
  74. }
  75.  
  76. public class Program {
  77.     public static void Main() {
  78.         Solution solution = new Solution();
  79.        
  80.         // Test with some examples
  81.         int n = 10; // Example input
  82.         Console.WriteLine($"Maximum product for integer break of {n} is: {solution.IntegerBreak(n)}");
  83.     }
  84. }
  85. Explanation:
  86. Dynamic Programming Array: We create an array dp where dp[i] holds the maximum product obtainable by breaking the integer i.
  87. Base Cases:
  88. For n = 1, the only option is 1, hence dp[1] = 1.
  89. For n = 2, the only option is 1 + 1, hence dp[2] = 1.
  90. Filling the DP Array:
  91. For each integer i from 3 to n, we check all possible ways to break i into two parts j and i - j.
  92. We calculate the maximum product by comparing the product of j * (i - j) and j * dp[i - j] to ensure that we also consider previous results stored in the dp array.
  93. Return the Result: Finally, we return dp[n], which contains the maximum product for the integer n.
  94. Example Usage:
  95. You can change the value of n in the Main method to test with different inputs. The output will show the maximum product for the integer break of the given n. For example, for n = 10, the output will be 36, as the optimal break is 3 + 3 + 4.
  96.  
  97. Certainly! Below is a Python solution for the LeetCode problem 343, "Integer Break". The goal is to maximize the product of integers that sum up to a given integer n by breaking it into at least two positive integers.
  98.  
  99. Python Code:
  100. class Solution:
  101.     def integerBreak(self, n: int) -> int:
  102.         # Create a dp array to store the maximum product for each integer
  103.         dp = [0] * (n + 1)
  104.  
  105.         # Base cases
  106.         dp[1] = 1  # 1 cannot be broken down
  107.         dp[2] = 1  # 2 can only be broken into 1 + 1
  108.  
  109.         # Fill the dp array for all integers from 3 to n
  110.         for i in range(3, n + 1):
  111.             for j in range(1, i):
  112.                 # Maximize the product by considering breaking i into j and (i - j)
  113.                 dp[i] = max(dp[i], max(j * (i - j), j * dp[i - j]))
  114.  
  115.         return dp[n]
  116.  
  117. # Example usage
  118. if __name__ == "__main__":
  119.     solution = Solution()
  120.    
  121.     n = 10  # Example input
  122.     print(f"Maximum product for integer break of {n} is: {solution.integerBreak(n)}")
  123. Explanation:
  124. Dynamic Programming Array: We create a list dp where dp[i] holds the maximum product obtainable by breaking the integer i.
  125. Base Cases:
  126. For n = 1, the only option is 1, hence dp[1] = 1.
  127. For n = 2, the only option is 1 + 1, hence dp[2] = 1.
  128. Filling the DP Array:
  129. For each integer i from 3 to n, we check all possible ways to break i into two parts j and i - j.
  130. We calculate the maximum product by comparing the product of j * (i - j) and j * dp[i - j] to ensure that we also consider previous results stored in the dp array.
  131. Return the Result: Finally, we return dp[n], which contains the maximum product for the integer n.
  132. Example Usage:
  133. You can change the value of n in the if __name__ == "__main__": block to test with different inputs. The output will show the maximum product for the integer break of the given n. For example, for n = 10, the output will be 36, as the optimal break is 3 + 3 + 4.
  134.  
Tags: C++ C# python
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement