leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE | |
commit | bbb2aa0798c50becb077e9f7be3322515800451e |
parent | f40d86e308ee006d1434917d4bc1bb5490a59208 |
author | Dimitrije Dobrota <mail@dimitrijedobrota.com> |
date | Wed, 20 Dec 2023 22:57:31 +0000 |
Daily Problem and 4 Random Problems
Diffstat:A | Problems/0030.cpp | | | ++++++++++++++++++++++++++++++++++++ |
A | Problems/0123.cpp | | | ++++++++++++++++ |
A | Problems/0183.sql | | | +++++ |
A | Problems/0188.cpp | | | +++++++++++++++ |
A | Problems/2706.cpp | | | ++++++++++++++++ |
M | README.md | | | +++++ |
6 files changed, 93 insertions(+), 0 deletions(-)
diff --git a/Problems/0030.cpp b/Problems/0030.cpp
@@ -0,0 +1,36 @@
static auto _ = []() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
return 0;
};
class Solution {
static bool check(const unordered_map<string, int> &count, const string &s, int len) {
static unordered_map<string, int> lcount;
lcount.clear();
for (int i = 0; i < s.size(); i += len) {
string word = s.substr(i, len);
const auto it = count.find(word);
if (it == count.end()) return false;
if (++lcount[word] > it->second) return false;
}
return true;
}
public:
vector<int> findSubstring(const string &s, const vector<string> &words) const {
const int n = words.size(), m = words[0].size();
unordered_map<string, int> count;
for (const string &word : words)
count[word]++;
vector<int> res;
for (int i = 0; i + m * n <= s.size(); i++) {
if (check(count, s.substr(i, m * n), m)) {
res.push_back(i);
}
}
return res;
}
};
diff --git a/Problems/0123.cpp b/Problems/0123.cpp
@@ -0,0 +1,16 @@
class Solution {
public:
int maxProfit(const vector<int> &prices) const {
int cost1 = INT_MAX, cost2 = INT_MAX;
int profit1 = 0, profit2 = 0;
for (const int price : prices) {
cost1 = min(cost1, price);
profit1 = max(profit1, price - cost1);
cost2 = min(cost2, price - profit1);
profit2 = max(profit2, price - cost2);
}
return profit2;
}
};
diff --git a/Problems/0183.sql b/Problems/0183.sql
@@ -0,0 +1,5 @@
SELECT C.Name as Customers
FROM Customers C
LEFT JOIN Orders O
ON C.Id = O.CustomerId
WHERE O.CustomerId is NULL
diff --git a/Problems/0188.cpp b/Problems/0188.cpp
@@ -0,0 +1,15 @@
class Solution {
public:
int maxProfit(const int k, const vector<int> &prices) const {
vector<int> cost(k + 1, INT_MAX), profit(k + 1, 0);
for (const int price : prices) {
for (int i = 1; i <= k; i++) {
cost[i] = min(cost[i], price - profit[i - 1]);
profit[i] = max(profit[i], price - cost[i]);
}
}
return profit[k];
}
};
diff --git a/Problems/2706.cpp b/Problems/2706.cpp
@@ -0,0 +1,16 @@
class Solution {
public:
int buyChoco(const vector<int> &prices, int money) const {
int a = prices[0], b = prices[1];
if (a > b) swap(a, b);
for (int i = 2; i < prices.size(); i++) {
if (a > prices[i]) {
b = a;
a = prices[i];
} else if (b > prices[i]) {
b = prices[i];
}
}
return a + b <= money ? money - (a + b) : money;
}
};
diff --git a/README.md b/README.md
@@ -53,6 +53,7 @@ for solving problems.
| 0027 | Easy | [Remove Element](Problems/0027.cpp) |
| 0028 | Medium | [Find the Index of the First Occurrence in a String](Problems/0028.cpp) |
| 0029 | Medium | [Divide Two Integers](Problems/0029.cpp) |
| 0030 | Hard | [Substring with Concatenation of All Words](Problems/0030.cpp) |
| 0031 | Medium | [Next Permutation](Problems/0031.cpp) |
| 0032 | Hard | [Longest Valid Parentheses](Problems/0032.cpp) |
| 0033 | Medium | [Search in Rotated Sorted Array](Problems/0033.cpp) |
@@ -142,6 +143,7 @@ for solving problems.
| 0120 | Medium | [Triangle](Problems/0120.cpp) |
| 0121 | Easy | [Best Time to Buy and Sell Stock](Problems/0121.cpp) |
| 0122 | Medium | [Best Time to Buy and Sell Stock II](Problems/0122.cpp) |
| 0123 | Hard | [Best Time to Buy and Sell Stock III](Problems/0123.cpp) |
| 0124 | Hard | [Binary Tree Maximum Path Sum](Problems/0124.cpp) |
| 0125 | Easy | [Valid Palindrome](Problems/0125.cpp) |
| 0127 | Hard | [Word Ladder](Problems/0127.cpp) |
@@ -187,9 +189,11 @@ for solving problems.
| 0180 | Medium | [Consecutive Numbers](Problems/0180.cpp) |
| 0181 | Easy | [Employees Earning More Than Their Managers](Problems/0181.cpp) |
| 0182 | Easy | [Duplicate Emails](Problems/0182.cpp) |
| 0183 | Easy | [Customers Who Never Order](Problems/0183.cpp) |
| 0184 | Medium | [Department Highest Salary](Problems/0184.cpp) |
| 0185 | Hard | [Department Top Three Salaries](Problems/0185.cpp) |
| 0187 | Medium | [Repeated DNA Sequences](Problems/0187.cpp) |
| 0188 | Hard | [Best Time to Buy and Sell Stock IV](Problems/0188.cpp) |
| 0189 | Medium | [Rotate Array](Problems/0189.cpp) |
| 0190 | Easy | [Reverse Bits](Problems/0190.cpp) |
| 0191 | Easy | [Number of 1 Bits](Problems/0191.cpp) |
@@ -1014,6 +1018,7 @@ for solving problems.
| 2683 | Medium | [Neighboring Bitwise XOR](Problems/2683.cpp) |
| 2685 | Medium | [Count the Number of Complete Components](Problems/2685.cpp) |
| 2698 | Medium | [Find the Punishment Number of an Integer](Problems/2698.cpp) |
| 2706 | Easy | [Buy Two Chocolates](Problems/2706.cpp) |
| 2707 | Medium | [Extra Characters in a String](Problems/2707.cpp) |
| 2711 | Medium | [Difference of Number of Distinct Values on Diagonals](Problems/2711.cpp) |
| 2740 | Medium | [Find the Value of the Partition](Problems/2740.cpp) |