mirror of
https://git.wolves.top/wolves/leetcode.git
synced 2025-11-04 17:26:32 +08:00
init
This commit is contained in:
26
23/10/2520.h
Normal file
26
23/10/2520.h
Normal file
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/26.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_2520_H
|
||||
#define LEECODE_C_2520_H
|
||||
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Q2520 {
|
||||
public:
|
||||
int countDigits(int num) {
|
||||
string str = to_string(num);
|
||||
int res = 0;
|
||||
for (auto &ch: str) {
|
||||
if (num % (ch - '0') == 0) {
|
||||
res++;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //LEECODE_C_2520_H
|
||||
49
23/10/901.h
Normal file
49
23/10/901.h
Normal file
@@ -0,0 +1,49 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/7.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_Q901_H
|
||||
#define LEECODE_C_Q901_H
|
||||
|
||||
#include <stack>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class trendSpanner {
|
||||
public:
|
||||
trendSpanner() {}
|
||||
|
||||
int next(int price) {
|
||||
count = 1;
|
||||
|
||||
if (trend.size() == 0 || price < trend.top()->value) {
|
||||
trend.push(new stock{price, 1});
|
||||
return count;
|
||||
}
|
||||
|
||||
while (!trend.empty() && price >= trend.top()->value) {
|
||||
count += trend.top()->value;
|
||||
stock *temp = trend.top();
|
||||
trend.pop();
|
||||
delete (temp);
|
||||
}
|
||||
|
||||
trend.push(new stock{price, count});
|
||||
return count;
|
||||
}
|
||||
|
||||
private:
|
||||
struct stock {
|
||||
int key;
|
||||
int value;
|
||||
};
|
||||
stack<stock *> trend;
|
||||
int count;
|
||||
};
|
||||
/**
|
||||
* Your trendSpanner object will be instantiated and called as such:
|
||||
* trendSpanner* obj = new trendSpanner();
|
||||
* int param_1 = obj->next(price);
|
||||
*/
|
||||
|
||||
#endif LEECODE_C_Q901_H
|
||||
25
23/10/Q1346.h
Normal file
25
23/10/Q1346.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef LEECODE_C_Q1346_H
|
||||
#define LEECODE_C_Q1346_H
|
||||
#include <map>
|
||||
using namespace std;
|
||||
|
||||
class Q1346 {
|
||||
public:
|
||||
bool checkIfExist(vector<int>& arr) {
|
||||
unordered_map<int,int> m;
|
||||
for(auto i:arr){
|
||||
m[i] = 1;
|
||||
}
|
||||
|
||||
for(auto [key,value]:m){
|
||||
if (m[key*2] == 1)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
#endif LEECODE_C_Q1346_H
|
||||
23
23/10/Q136.h
Normal file
23
23/10/Q136.h
Normal file
@@ -0,0 +1,23 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/14.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_Q136_H
|
||||
#define LEECODE_C_Q136_H
|
||||
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Q136 {
|
||||
public:
|
||||
int singleNumber(vector<int> &nums) {
|
||||
int count = 0;
|
||||
for (int i: nums) {
|
||||
count ^= i;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //LEECODE_C_Q136_H
|
||||
29
23/10/Q137.h
Normal file
29
23/10/Q137.h
Normal file
@@ -0,0 +1,29 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/16.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_Q137_H
|
||||
#define LEECODE_C_Q137_H
|
||||
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Q137 {
|
||||
public:
|
||||
int singleNumber(vector<int> &nums) {
|
||||
unordered_map<int, int> m;
|
||||
for (int i: nums) {
|
||||
m[i] = m[i] + 1;
|
||||
}
|
||||
for (auto [num, count]: m) {
|
||||
if (count == 1) {
|
||||
return num;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //LEECODE_C_Q137_H
|
||||
41
23/10/Q1465.h
Normal file
41
23/10/Q1465.h
Normal file
@@ -0,0 +1,41 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/27.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_Q1465_H
|
||||
#define LEECODE_C_Q1465_H
|
||||
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Q1465 {
|
||||
public:
|
||||
int maxArea(int h, int w, vector<int> &horizontalCuts, vector<int> &verticalCuts) {
|
||||
int maxHorizon;
|
||||
int maxVertical;
|
||||
|
||||
if (horizontalCuts.size()) {
|
||||
maxHorizon = max(horizontalCuts[0], h - horizontalCuts[horizontalCuts.size() - 1]);
|
||||
} else {
|
||||
maxHorizon = h;
|
||||
}
|
||||
if (verticalCuts.size()) {
|
||||
maxVertical = max(verticalCuts[0], w - verticalCuts[verticalCuts.size() - 1]);
|
||||
} else {
|
||||
maxVertical = w;
|
||||
}
|
||||
|
||||
for (int i = 1; i < horizontalCuts.size(); ++i) {
|
||||
maxHorizon = max(maxHorizon, horizontalCuts[i] - horizontalCuts[i - 1]);
|
||||
}
|
||||
|
||||
for (int i = 1; i < verticalCuts.size(); ++i) {
|
||||
maxVertical = max(maxVertical, verticalCuts[i] - verticalCuts[i - 1]);
|
||||
}
|
||||
|
||||
return (long long) maxHorizon * maxVertical % 1000000001;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //LEECODE_C_Q1465_H
|
||||
44
23/10/Q1488.h
Normal file
44
23/10/Q1488.h
Normal file
@@ -0,0 +1,44 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/13.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_Q1488_H
|
||||
#define LEECODE_C_Q1488_H
|
||||
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Q1488 {
|
||||
public:
|
||||
vector<int> avoidFlood(vector<int> &rains) {
|
||||
map<int, int> m;
|
||||
vector<int> ret;
|
||||
for (int i = 0; i < rains.size(); i++) {
|
||||
if (rains[i] == 0) {
|
||||
for (int j = i + 1; j < rains.size(); ++j) {
|
||||
if (j > 0) {
|
||||
if (m[rains[j]] == 1) {
|
||||
m.erase(rains[j]);
|
||||
ret.push_back(rains[j]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (j == rains.size() - 1) {
|
||||
ret.push_back(1);
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (m[rains[i]]) {
|
||||
return vector<int>();
|
||||
}
|
||||
m[rains[i]] = 1;
|
||||
ret.push_back(-1);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //LEECODE_C_Q1488_H
|
||||
77
23/10/Q1726.h
Normal file
77
23/10/Q1726.h
Normal file
@@ -0,0 +1,77 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/19.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_Q1726_H
|
||||
#define LEECODE_C_Q1726_H
|
||||
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <../../dataStruct/Combination.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int tupleSameProduct(vector<int> &nums) {
|
||||
int count = 0;
|
||||
unordered_map<int, int> m;
|
||||
for (int i = 0; i < nums.size(); ++i) {
|
||||
for (int j = i + 1; j < nums.size(); ++j) {
|
||||
m[nums[i] * nums[j]] = m[nums[i] * nums[j]] + 1;
|
||||
}
|
||||
}
|
||||
for (auto &pair: m) {
|
||||
if (pair.second > 1) {
|
||||
count += pair.second * (pair.second - 1) * 4;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
int tupleSameProduct2(vector<int> &nums) {
|
||||
int count = 0;
|
||||
vector<int> result;
|
||||
for (int i = 0; i < nums.size(); ++i) {
|
||||
for (int j = i + 1; j < nums.size(); ++j) {
|
||||
result.push_back(nums[i] * nums[j]);
|
||||
}
|
||||
}
|
||||
sort(result.begin(), result.end());
|
||||
int temp = 0;
|
||||
int num = nums[0];
|
||||
for (int i = 1; i < result.size(); ++i) {
|
||||
if (result[i] == num) {
|
||||
count += (temp++) * 8;
|
||||
} else {
|
||||
num = result[i];
|
||||
temp = 0;
|
||||
}
|
||||
if (result[i] == result[i - 1]) {
|
||||
count += 8;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
int tupleSameProduct3(vector<int> &nums) {
|
||||
int count = 0;
|
||||
vector<int> result;
|
||||
for (int i = 0; i < nums.size(); ++i) {
|
||||
for (int j = i + 1; j < nums.size(); ++j) {
|
||||
result.push_back(nums[i] * nums[j]);
|
||||
}
|
||||
}
|
||||
sort(result.begin(), result.end());
|
||||
int temp = 1;
|
||||
int num = nums[0];
|
||||
for (int i = 1; i < result.size(); ++i) {
|
||||
if (result[i] == num) {
|
||||
temp++;
|
||||
} else {
|
||||
count += temp * (temp - 1) * 4;
|
||||
num = result[i];
|
||||
temp = 1;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
#endif //LEECODE_C_Q1726_H
|
||||
29
23/10/Q2525.h
Normal file
29
23/10/Q2525.h
Normal file
@@ -0,0 +1,29 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/20.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_Q2525_H
|
||||
#define LEECODE_C_Q2525_H
|
||||
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Q2525 {
|
||||
public:
|
||||
string categorizeBox(int length, int width, int height, int mass) {
|
||||
if (length >= 10000 || width >= 10000 || height >= 10000 || (long long)length * width * height >= 1000000000) {
|
||||
if (mass >= 100) {
|
||||
return "Both";
|
||||
} else {
|
||||
return "Bulky";
|
||||
}
|
||||
}
|
||||
if (mass >= 100) {
|
||||
return "Heavy";
|
||||
}
|
||||
return "Neither";
|
||||
}
|
||||
};
|
||||
|
||||
#endif //LEECODE_C_Q2525_H
|
||||
33
23/10/Q2526.h
Normal file
33
23/10/Q2526.h
Normal file
@@ -0,0 +1,33 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/12.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_Q2526_H
|
||||
#define LEECODE_C_Q2526_H
|
||||
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Q2526 {
|
||||
public:
|
||||
long long findTheArrayConcVal(vector<int> &nums) {
|
||||
long long ans = 0;
|
||||
for (int i = 0, j = nums.size() - 1; i <= j; i++, j--) {
|
||||
if (i != j) {
|
||||
int temp = nums[j];
|
||||
int digit = 0;
|
||||
while (temp != 0) {
|
||||
digit++;
|
||||
temp /= 10;
|
||||
}
|
||||
ans += nums[j] + (long long) nums[i] * pow(10, digit);
|
||||
} else {
|
||||
ans += nums[i];
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //LEECODE_C_Q2526_H
|
||||
26
23/10/Q2530.h
Normal file
26
23/10/Q2530.h
Normal file
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/18.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_Q2530_H
|
||||
#define LEECODE_C_Q2530_H
|
||||
|
||||
#include <vector>
|
||||
#include <queue>
|
||||
|
||||
using namespace std;
|
||||
|
||||
long long maxKelements(vector<int> &nums, int k) {
|
||||
long long count = 0;
|
||||
priority_queue<int> Q(nums.begin(), nums.end());
|
||||
while (k) {
|
||||
auto top = Q.top();
|
||||
Q.pop();
|
||||
count += top;
|
||||
Q.push(ceil(top / 3.0));
|
||||
k--;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
#endif //LEECODE_C_Q2530_H
|
||||
35
23/10/Q260.h
Normal file
35
23/10/Q260.h
Normal file
@@ -0,0 +1,35 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/16.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_Q260_H
|
||||
#define LEECODE_C_Q260_H
|
||||
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Q260 {
|
||||
public:
|
||||
vector<int> singleNumber(vector<int> &nums) {
|
||||
unordered_map<int, int> m;
|
||||
for (int i: nums) {
|
||||
m[i] = m[i] + 1;
|
||||
}
|
||||
vector<int> ret;
|
||||
for (auto [num, count]: m) {
|
||||
if (count == 1) {
|
||||
if (ret.empty()) {
|
||||
ret.push_back(num);
|
||||
} else {
|
||||
ret.push_back(num);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //LEECODE_C_Q260_H
|
||||
19
23/10/Q2652.h
Normal file
19
23/10/Q2652.h
Normal file
@@ -0,0 +1,19 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/17.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_Q2652_H
|
||||
#define LEECODE_C_Q2652_H
|
||||
|
||||
class Q2652 {
|
||||
public:
|
||||
int f(int n, int m) {
|
||||
return (m + n / m * m) * (n / m) / 2;
|
||||
}
|
||||
|
||||
int sumOfMultiples(int n) {
|
||||
return f(n, 3) + f(n, 5) + f(n, 7) - f(n, 3 * 5) - f(n, 3 * 7) - f(n, 5 * 7) + f(n, 3 * 5 * 7);
|
||||
}
|
||||
};
|
||||
|
||||
#endif //LEECODE_C_Q2652_H
|
||||
26
23/10/Q2698.h
Normal file
26
23/10/Q2698.h
Normal file
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/25.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_Q2698_H
|
||||
#define LEECODE_C_Q2698_H
|
||||
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Q2698 {
|
||||
public:
|
||||
int punishmentNumber(int n) {
|
||||
auto all = vector<int>(
|
||||
{1, 9, 10, 36, 45, 55, 82, 91, 99, 100, 235, 297, 369, 370, 379, 414, 657, 675, 703, 756, 792, 909, 918,
|
||||
945, 964, 990, 991, 999, 1000});
|
||||
int count = 0;
|
||||
for (int i = 0; i < all.size() && all[i] <= n; ++i) {
|
||||
count += all[i] * all[i];
|
||||
}
|
||||
return count;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //LEECODE_C_Q2698_H
|
||||
30
23/10/Q274.h
Normal file
30
23/10/Q274.h
Normal file
@@ -0,0 +1,30 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/29.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_Q274_H
|
||||
#define LEECODE_C_Q274_H
|
||||
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Q274 {
|
||||
public:
|
||||
int hIndex(vector<int> &citations) {
|
||||
sort(citations.begin(), citations.end(), [](int a, int b) {
|
||||
return a > b;
|
||||
});
|
||||
for (int i = 1; i < citations.size(); ++i) {
|
||||
if (citations[i - 1] < i) {
|
||||
return i - 1;
|
||||
}
|
||||
if (i + 1 == citations.size() - 1) {
|
||||
return i + 1;
|
||||
}
|
||||
}
|
||||
return citations[0] == 0 ? 0 : 1;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //LEECODE_C_Q274_H
|
||||
29
23/10/Q275.h
Normal file
29
23/10/Q275.h
Normal file
@@ -0,0 +1,29 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/30.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_Q275_H
|
||||
#define LEECODE_C_Q275_H
|
||||
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Q275 {
|
||||
public:
|
||||
int hIndex(vector<int> &citations) {
|
||||
int n = citations.size();
|
||||
int left = 0, right = n - 1;
|
||||
while (left <= right) {
|
||||
int mid = left + (right - left) / 2;
|
||||
if (citations[mid] >= n - mid) {
|
||||
right = mid - 1;
|
||||
} else {
|
||||
left = mid + 1;
|
||||
}
|
||||
}
|
||||
return n - left;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //LEECODE_C_Q275_H
|
||||
57
23/10/T368.h
Normal file
57
23/10/T368.h
Normal file
@@ -0,0 +1,57 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/22.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_T368_H
|
||||
#define LEECODE_C_T368_H
|
||||
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int minimumSum(vector<int> &nums) {
|
||||
int left = 0;
|
||||
int right = nums.size() - 1;
|
||||
for (int i = 0; i < right; ++i) {
|
||||
if (nums[left] >= nums[i]) {
|
||||
left = i;
|
||||
}
|
||||
}
|
||||
for (int i = right; i > left; --i) {
|
||||
if (nums[right] >= nums[i]) {
|
||||
right = i;
|
||||
}
|
||||
}
|
||||
if (right - left == 1) {
|
||||
return -1;
|
||||
}
|
||||
int max = left + 1;
|
||||
for (int i = left + 1; i < right; ++i) {
|
||||
if (nums[max] >= nums[i]) {
|
||||
max = i;
|
||||
}
|
||||
}
|
||||
return nums[left] + nums[right] + nums[max];
|
||||
}
|
||||
|
||||
void runT368() {
|
||||
vector<int> arr; // [5,4,8,7,10,2] / [6,5,4,3,4,5]
|
||||
/* arr.push_back(5);
|
||||
arr.push_back(4);
|
||||
arr.push_back(8);
|
||||
arr.push_back(7);
|
||||
arr.push_back(10);
|
||||
arr.push_back(2);*/
|
||||
// arr.push_back(6);
|
||||
// arr.push_back(5);
|
||||
// arr.push_back(4);
|
||||
// arr.push_back(3);
|
||||
// arr.push_back(4);
|
||||
// arr.push_back(5);
|
||||
arr.push_back(50);
|
||||
arr.push_back(50);
|
||||
arr.push_back(50);
|
||||
auto result = minimumSum(arr);
|
||||
}
|
||||
|
||||
#endif //LEECODE_C_T368_H
|
||||
Reference in New Issue
Block a user