Skip to content

Commit

Permalink
Merge branch '7oSkaaa:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedGamal2212 authored Jun 25, 2023
2 parents fda090b + 1bbe50a commit f044ca6
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Author: Ahmed Hossam

class Solution {
public:

int n; // Number of rods
static constexpr int MAXN = 5000, INF = 1e9; // Constants for array size and infinite value
vector < int > rods; // Vector to store the lengths of the rods
vector < vector < int > > dp; // Dynamic programming array

// Function to calculate the maximum score
int max_score(int idx, int diff) {
// Base case: If all rods have been processed, return 0 if the difference is 0, or -INF otherwise
if (idx == n) return (!diff ? 0 : -INF);

// Reference to the maximum score at current index and difference
int& ret = dp[idx][diff + MAXN];

// If the maximum score has already been calculated, return it
if (~ret) return ret;

// Scenario 1: Exclude the current rod
ret = max_score(idx + 1, diff);

// Scenario 2: Include the current rod as positive difference
ret = max(ret, rods[idx] + max_score(idx + 1, diff + rods[idx]));

// Scenario 3: Include the current rod as negative difference
ret = max(ret, max_score(idx + 1, diff - rods[idx]));

// Return the maximum score
return ret;
}

int tallestBillboard(vector<int>& rods) {
this -> rods = rods; // Assign the input rods to the member variable
this -> n = rods.size(); // Set the number of rods

// Initialize the dynamic programming array with -1
this -> dp = vector < vector < int > > (n, vector < int > (2 * MAXN + 5, -1));

// Return the maximum score starting from index 0 and with 0 difference
return max_score(0, 0);
}
};
63 changes: 63 additions & 0 deletions 06- June/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
1. **[Minimum Cost to Make Array Equal](#21--minimum-cost-to-make-array-equal)**
1. **[Best Time to Buy and Sell Stock with Transaction Fee](#22--best-time-to-buy-and-sell-stock-with-transaction-fee)**
1. **[Longest Arithmetic Subsequence](#23--longest-arithmetic-subsequence)**
1. **[Tallest Billboard](#24--tallest-billboard)**

<hr>
<br><br>
Expand Down Expand Up @@ -1380,4 +1381,66 @@ public:
}
};
```

<hr>
<br><br>

## 24) [Tallest Billboard](https://leetcode.com/problems/tallest-billboard/)

### Difficulty

![](https://img.shields.io/badge/Hard-red?style=for-the-badge)

### Related Topic

`Array` `Dynamic Programming`

### Code


```cpp
class Solution {
public:

int n; // Number of rods
static constexpr int MAXN = 5000, INF = 1e9; // Constants for array size and infinite value
vector < int > rods; // Vector to store the lengths of the rods
vector < vector < int > > dp; // Dynamic programming array

// Function to calculate the maximum score
int max_score(int idx, int diff) {
// Base case: If all rods have been processed, return 0 if the difference is 0, or -INF otherwise
if (idx == n) return (!diff ? 0 : -INF);
// Reference to the maximum score at current index and difference
int& ret = dp[idx][diff + MAXN];
// If the maximum score has already been calculated, return it
if (~ret) return ret;

// Scenario 1: Exclude the current rod
ret = max_score(idx + 1, diff);
// Scenario 2: Include the current rod as positive difference
ret = max(ret, rods[idx] + max_score(idx + 1, diff + rods[idx]));
// Scenario 3: Include the current rod as negative difference
ret = max(ret, max_score(idx + 1, diff - rods[idx]));

// Return the maximum score
return ret;
}

int tallestBillboard(vector<int>& rods) {
this -> rods = rods; // Assign the input rods to the member variable
this -> n = rods.size(); // Set the number of rods

// Initialize the dynamic programming array with -1
this -> dp = vector < vector < int > > (n, vector < int > (2 * MAXN + 5, -1));
// Return the maximum score starting from index 0 and with 0 difference
return max_score(0, 0);
}
};
```

0 comments on commit f044ca6

Please sign in to comment.