Skip to content

Commit 67d8f5d

Browse files
committed
Adds 5 POTD questions
1 parent c2647ed commit 67d8f5d

File tree

18 files changed

+566
-0
lines changed

18 files changed

+566
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Final Prices With a Special Discount in a Shop
2+
3+
**Question ID**: 1475
4+
**Language**: C++
5+
**Status**: Accepted
6+
**Runtime**: 0 ms
7+
**Memory**: 13.94 MB
8+
9+
## Solution Code
10+
11+
```cpp
12+
class Solution {
13+
public:
14+
vector<int> finalPrices(vector<int>& prices) {
15+
16+
17+
18+
for(int i=0;i<prices.size();i++){
19+
for(int j=i+1;j<prices.size();j++){
20+
if(prices[j]<=prices[i]){
21+
prices[i]-=prices[j];
22+
break;
23+
}
24+
}
25+
}
26+
27+
28+
return prices;
29+
}
30+
};
31+
//https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop
32+
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"id": 1482239245,
3+
"question_id": 1475,
4+
"lang": "cpp",
5+
"lang_name": "C++",
6+
"timestamp": 1734616130,
7+
"status": 10,
8+
"status_display": "Accepted",
9+
"runtime": "0 ms",
10+
"url": "https://leetcode.com/submissions/detail/1482239245/",
11+
"is_pending": "Not Pending",
12+
"title": "Final Prices With a Special Discount in a Shop",
13+
"memory": "13.94 MB",
14+
"title_slug": "final-prices-with-a-special-discount-in-a-shop",
15+
"has_notes": false,
16+
"flag_type": 1
17+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
// https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop
5+
6+
class Solution
7+
{
8+
public:
9+
vector<int> finalPrices(vector<int> &prices)
10+
{
11+
12+
for (int i = 0; i < prices.size(); i++)
13+
{
14+
for (int j = i + 1; j < prices.size(); j++)
15+
{
16+
if (prices[j] <= prices[i])
17+
{
18+
prices[i] -= prices[j];
19+
break;
20+
}
21+
}
22+
}
23+
24+
return prices;
25+
}
26+
};
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Maximum Average Pass Ratio
2+
3+
**Question ID**: 1792
4+
**Language**: C++
5+
**Status**: Accepted
6+
**Runtime**: 307 ms
7+
**Memory**: 97.8 MB
8+
9+
## Solution Code
10+
```cpp
11+
//https://leetcode.com/problems/maximum-average-pass-ratio
12+
class Solution {
13+
public:
14+
double maxAverageRatio(vector<vector<int>>& classes, int extraStudents) {
15+
// Lambda to calculate the gain of adding an extra student
16+
17+
auto calculateGain = [](int passes, int totalStudents) {
18+
return (double)(passes + 1) / (totalStudents + 1) -
19+
(double)passes / totalStudents;
20+
};
21+
22+
23+
priority_queue<pair<double, pair<int, int>>> maxHeap;
24+
for (const auto& singleClass : classes) {
25+
maxHeap.push({calculateGain(singleClass[0], singleClass[1]),
26+
{singleClass[0], singleClass[1]}});
27+
}
28+
29+
// Distribute extra students
30+
31+
while (extraStudents--) {
32+
auto [currentGain, classInfo] = maxHeap.top();
33+
maxHeap.pop();
34+
int passes = classInfo.first;
35+
int totalStudents = classInfo.second;
36+
maxHeap.push({calculateGain(passes + 1, totalStudents + 1),
37+
{passes + 1, totalStudents + 1}});
38+
}
39+
40+
// Calculate the final average pass ratio
41+
double totalPassRatio = 0;
42+
while (!maxHeap.empty()) {
43+
auto [_, classInfo] = maxHeap.top();
44+
maxHeap.pop();
45+
totalPassRatio += (double)classInfo.first / classInfo.second;
46+
}
47+
48+
return totalPassRatio / classes.size();
49+
}
50+
};
51+
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"id": 1479691576,
3+
"question_id": 1792,
4+
"lang": "cpp",
5+
"lang_name": "C++",
6+
"timestamp": 1734615754,
7+
"status": 10,
8+
"status_display": "Accepted",
9+
"runtime": "307 ms",
10+
"url": "https://leetcode.com/submissions/detail/1479691576/",
11+
"is_pending": "Not Pending",
12+
"title": "Maximum Average Pass Ratio",
13+
"memory": "97.8 MB",
14+
"title_slug": "maximum-average-pass-ratio",
15+
"has_notes": false,
16+
"flag_type": 1
17+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
// https://leetcode.com/problems/maximum-average-pass-ratio
5+
6+
class Solution
7+
{
8+
public:
9+
double maxAverageRatio(vector<vector<int>> &classes, int extraStudents)
10+
{
11+
// Lambda to calculate the gain of adding an extra student
12+
13+
auto calculateGain = [](int passes, int totalStudents)
14+
{
15+
return (double)(passes + 1) / (totalStudents + 1) -
16+
(double)passes / totalStudents;
17+
};
18+
19+
priority_queue<pair<double, pair<int, int>>> maxHeap;
20+
for (const auto &singleClass : classes)
21+
{
22+
maxHeap.push({calculateGain(singleClass[0], singleClass[1]),
23+
{singleClass[0], singleClass[1]}});
24+
}
25+
26+
// Distribute extra students
27+
28+
while (extraStudents--)
29+
{
30+
auto [currentGain, classInfo] = maxHeap.top();
31+
maxHeap.pop();
32+
int passes = classInfo.first;
33+
int totalStudents = classInfo.second;
34+
maxHeap.push({calculateGain(passes + 1, totalStudents + 1),
35+
{passes + 1, totalStudents + 1}});
36+
}
37+
38+
// Calculate the final average pass ratio
39+
double totalPassRatio = 0;
40+
while (!maxHeap.empty())
41+
{
42+
auto [_, classInfo] = maxHeap.top();
43+
maxHeap.pop();
44+
totalPassRatio += (double)classInfo.first / classInfo.second;
45+
}
46+
47+
return totalPassRatio / classes.size();
48+
}
49+
};
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Construct String With Repeat Limit
2+
3+
**Question ID**: 2182
4+
**Language**: C++
5+
**Status**: Accepted
6+
**Runtime**: 52 ms
7+
**Memory**: 28.24 MB
8+
9+
## Solution Code
10+
11+
```cpp
12+
//https://leetcode.com/problems/construct-string-with-repeat-limit
13+
class Solution {
14+
public:
15+
string repeatLimitedString(string s, int repeatLimit) {
16+
unordered_map<char, int> freq;
17+
for (char ch : s) {
18+
freq[ch]++;
19+
}
20+
21+
priority_queue<char> maxHeap;
22+
for (auto& [ch, count] : freq) {
23+
maxHeap.push(ch);
24+
}
25+
26+
string result;
27+
28+
while (!maxHeap.empty()) {
29+
char ch = maxHeap.top();
30+
maxHeap.pop();
31+
int count = freq[ch];
32+
33+
int use = min(count, repeatLimit);
34+
result.append(use, ch);
35+
36+
freq[ch] -= use;
37+
38+
if (freq[ch] > 0 && !maxHeap.empty()) {
39+
char nextCh = maxHeap.top();
40+
maxHeap.pop();
41+
42+
result.push_back(nextCh);
43+
freq[nextCh]--;
44+
45+
if (freq[nextCh] > 0) {
46+
maxHeap.push(nextCh);
47+
}
48+
49+
maxHeap.push(ch);
50+
}
51+
}
52+
53+
return result;
54+
}
55+
};
56+
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"id": 1481462255,
3+
"question_id": 2182,
4+
"lang": "cpp",
5+
"lang_name": "C++",
6+
"timestamp": 1734616040,
7+
"status": 10,
8+
"status_display": "Accepted",
9+
"runtime": "52 ms",
10+
"url": "https://leetcode.com/submissions/detail/1481462255/",
11+
"is_pending": "Not Pending",
12+
"title": "Construct String With Repeat Limit",
13+
"memory": "28.24 MB",
14+
"title_slug": "construct-string-with-repeat-limit",
15+
"has_notes": false,
16+
"flag_type": 1
17+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
//https://leetcode.com/problems/construct-string-with-repeat-limit
5+
class Solution {
6+
public:
7+
string repeatLimitedString(string s, int repeatLimit) {
8+
unordered_map<char, int> freq;
9+
for (char ch : s) {
10+
freq[ch]++;
11+
}
12+
13+
priority_queue<char> maxHeap;
14+
for (auto& [ch, count] : freq) {
15+
maxHeap.push(ch);
16+
}
17+
18+
string result;
19+
20+
while (!maxHeap.empty()) {
21+
char ch = maxHeap.top();
22+
maxHeap.pop();
23+
int count = freq[ch];
24+
25+
int use = min(count, repeatLimit);
26+
result.append(use, ch);
27+
28+
freq[ch] -= use;
29+
30+
if (freq[ch] > 0 && !maxHeap.empty()) {
31+
char nextCh = maxHeap.top();
32+
maxHeap.pop();
33+
34+
result.push_back(nextCh);
35+
freq[nextCh]--;
36+
37+
if (freq[nextCh] > 0) {
38+
maxHeap.push(nextCh);
39+
}
40+
41+
maxHeap.push(ch);
42+
}
43+
}
44+
45+
return result;
46+
}
47+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Sum of Squares of Special Elements
2+
3+
**Question ID**: 2778
4+
**Language**: C++
5+
**Status**: Accepted
6+
**Runtime**: 0 ms
7+
**Memory**: 40.07 MB
8+
9+
## Solution Code
10+
11+
```cpp
12+
//https://leetcode.com/problems/sum-of-squares-of-special-elements
13+
class Solution {
14+
public:
15+
int sumOfSquares(vector<int>& nums) {
16+
17+
int sum=0;
18+
19+
int n=nums.size();
20+
for(int i=0;i<nums.size();i++){
21+
if(n%(i+1)==0){
22+
23+
sum+= (nums[i]*nums[i]);
24+
}
25+
}
26+
27+
return sum;
28+
}
29+
};
30+
```

0 commit comments

Comments
 (0)