Datasets:
Upload folder using huggingface_hub
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .gitattributes +1 -0
- HiBench/.gitattributes +59 -0
- HiBench/Code/c++Selected/1.cpp +35 -0
- HiBench/Code/c++Selected/10.cpp +16 -0
- HiBench/Code/c++Selected/100.cpp +10 -0
- HiBench/Code/c++Selected/11.cpp +20 -0
- HiBench/Code/c++Selected/12.cpp +38 -0
- HiBench/Code/c++Selected/13.cpp +6 -0
- HiBench/Code/c++Selected/14.cpp +30 -0
- HiBench/Code/c++Selected/15.cpp +6 -0
- HiBench/Code/c++Selected/16.cpp +60 -0
- HiBench/Code/c++Selected/17.cpp +23 -0
- HiBench/Code/c++Selected/18.cpp +23 -0
- HiBench/Code/c++Selected/19.cpp +6 -0
- HiBench/Code/c++Selected/2.cpp +11 -0
- HiBench/Code/c++Selected/20.cpp +33 -0
- HiBench/Code/c++Selected/21.cpp +16 -0
- HiBench/Code/c++Selected/22.cpp +15 -0
- HiBench/Code/c++Selected/23.cpp +22 -0
- HiBench/Code/c++Selected/24.cpp +22 -0
- HiBench/Code/c++Selected/25.cpp +6 -0
- HiBench/Code/c++Selected/26.cpp +40 -0
- HiBench/Code/c++Selected/27.cpp +20 -0
- HiBench/Code/c++Selected/28.cpp +28 -0
- HiBench/Code/c++Selected/29.cpp +15 -0
- HiBench/Code/c++Selected/3.cpp +17 -0
- HiBench/Code/c++Selected/30.cpp +22 -0
- HiBench/Code/c++Selected/31.cpp +49 -0
- HiBench/Code/c++Selected/32.cpp +17 -0
- HiBench/Code/c++Selected/33.cpp +17 -0
- HiBench/Code/c++Selected/34.cpp +32 -0
- HiBench/Code/c++Selected/35.cpp +6 -0
- HiBench/Code/c++Selected/36.cpp +27 -0
- HiBench/Code/c++Selected/37.cpp +6 -0
- HiBench/Code/c++Selected/38.cpp +62 -0
- HiBench/Code/c++Selected/39.cpp +34 -0
- HiBench/Code/c++Selected/4.cpp +14 -0
- HiBench/Code/c++Selected/40.cpp +18 -0
- HiBench/Code/c++Selected/41.cpp +55 -0
- HiBench/Code/c++Selected/42.cpp +37 -0
- HiBench/Code/c++Selected/43.cpp +40 -0
- HiBench/Code/c++Selected/44.cpp +24 -0
- HiBench/Code/c++Selected/45.cpp +27 -0
- HiBench/Code/c++Selected/46.cpp +28 -0
- HiBench/Code/c++Selected/47.cpp +17 -0
- HiBench/Code/c++Selected/48.cpp +21 -0
- HiBench/Code/c++Selected/49.cpp +11 -0
- HiBench/Code/c++Selected/5.cpp +150 -0
- HiBench/Code/c++Selected/50.cpp +17 -0
- HiBench/Code/c++Selected/51.cpp +15 -0
.gitattributes
CHANGED
|
@@ -57,3 +57,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 57 |
# Video files - compressed
|
| 58 |
*.mp4 filter=lfs diff=lfs merge=lfs -text
|
| 59 |
*.webm filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 57 |
# Video files - compressed
|
| 58 |
*.mp4 filter=lfs diff=lfs merge=lfs -text
|
| 59 |
*.webm filter=lfs diff=lfs merge=lfs -text
|
| 60 |
+
HiBench/Paper/contextual_qa/train.json filter=lfs diff=lfs merge=lfs -text
|
HiBench/.gitattributes
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.lz4 filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.mds filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
# Audio files - uncompressed
|
| 39 |
+
*.pcm filter=lfs diff=lfs merge=lfs -text
|
| 40 |
+
*.sam filter=lfs diff=lfs merge=lfs -text
|
| 41 |
+
*.raw filter=lfs diff=lfs merge=lfs -text
|
| 42 |
+
# Audio files - compressed
|
| 43 |
+
*.aac filter=lfs diff=lfs merge=lfs -text
|
| 44 |
+
*.flac filter=lfs diff=lfs merge=lfs -text
|
| 45 |
+
*.mp3 filter=lfs diff=lfs merge=lfs -text
|
| 46 |
+
*.ogg filter=lfs diff=lfs merge=lfs -text
|
| 47 |
+
*.wav filter=lfs diff=lfs merge=lfs -text
|
| 48 |
+
# Image files - uncompressed
|
| 49 |
+
*.bmp filter=lfs diff=lfs merge=lfs -text
|
| 50 |
+
*.gif filter=lfs diff=lfs merge=lfs -text
|
| 51 |
+
*.png filter=lfs diff=lfs merge=lfs -text
|
| 52 |
+
*.tiff filter=lfs diff=lfs merge=lfs -text
|
| 53 |
+
# Image files - compressed
|
| 54 |
+
*.jpg filter=lfs diff=lfs merge=lfs -text
|
| 55 |
+
*.jpeg filter=lfs diff=lfs merge=lfs -text
|
| 56 |
+
*.webp filter=lfs diff=lfs merge=lfs -text
|
| 57 |
+
# Video files - compressed
|
| 58 |
+
*.mp4 filter=lfs diff=lfs merge=lfs -text
|
| 59 |
+
*.webm filter=lfs diff=lfs merge=lfs -text
|
HiBench/Code/c++Selected/1.cpp
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) {
|
| 4 |
+
for (int i = 0; i < matrix.size(); ++i) {
|
| 5 |
+
for (int j = 0; j < matrix[i].size(); ++j) {
|
| 6 |
+
if (!matrix[i][j]) {
|
| 7 |
+
continue;
|
| 8 |
+
}
|
| 9 |
+
matrix[i][j] = numeric_limits<int>::max();
|
| 10 |
+
if (i > 0 && matrix[i - 1][j] != numeric_limits<int>::max()) {
|
| 11 |
+
matrix[i][j] = min(matrix[i][j], matrix[i - 1][j] + 1);
|
| 12 |
+
}
|
| 13 |
+
if (j > 0 && matrix[i][j - 1] != numeric_limits<int>::max()) {
|
| 14 |
+
matrix[i][j] = min(matrix[i][j], matrix[i][j - 1] + 1);
|
| 15 |
+
}
|
| 16 |
+
}
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
for (int i = matrix.size() - 1; i >= 0; --i) {
|
| 20 |
+
for (int j = matrix[i].size() - 1; j >= 0; --j) {
|
| 21 |
+
if (!matrix[i][j]) {
|
| 22 |
+
continue;
|
| 23 |
+
}
|
| 24 |
+
if (i < matrix.size() - 1 && matrix[i + 1][j] != numeric_limits<int>::max()) {
|
| 25 |
+
matrix[i][j] = min(matrix[i][j], matrix[i + 1][j] + 1);
|
| 26 |
+
}
|
| 27 |
+
if (j < matrix[i].size() - 1 && matrix[i][j + 1] != numeric_limits<int>::max()) {
|
| 28 |
+
matrix[i][j] = min(matrix[i][j], matrix[i][j + 1] + 1);
|
| 29 |
+
}
|
| 30 |
+
}
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
return matrix;
|
| 34 |
+
}
|
| 35 |
+
};
|
HiBench/Code/c++Selected/10.cpp
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
int maxA(int N) {
|
| 4 |
+
if (N < 7) {
|
| 5 |
+
return N;
|
| 6 |
+
}
|
| 7 |
+
if (N == 10) { // the following rule doesn't hold when N = 10
|
| 8 |
+
return 20;
|
| 9 |
+
}
|
| 10 |
+
auto n = N / 5 + 1; // n3 + n4 increases one every 5 keys
|
| 11 |
+
auto n3 = 5 * n - N - 1;
|
| 12 |
+
auto n4 = n - n3;
|
| 13 |
+
return pow(3, n3) * pow(4, n4);
|
| 14 |
+
}
|
| 15 |
+
};
|
| 16 |
+
|
HiBench/Code/c++Selected/100.cpp
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
vector<int> maxDepthAfterSplit(string seq) {
|
| 4 |
+
vector<int> result(seq.length());
|
| 5 |
+
for (int i = 0; i < seq.length(); ++i) {
|
| 6 |
+
result[i] = (i & 1) ^ (seq[i] == '(');
|
| 7 |
+
}
|
| 8 |
+
return result;
|
| 9 |
+
}
|
| 10 |
+
};
|
HiBench/Code/c++Selected/11.cpp
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
|
| 4 |
+
unordered_map<int, int> A_B_sum;
|
| 5 |
+
for (const auto& a : A) {
|
| 6 |
+
for (const auto& b : B) {
|
| 7 |
+
++A_B_sum[a + b];
|
| 8 |
+
}
|
| 9 |
+
}
|
| 10 |
+
int result = 0;
|
| 11 |
+
for (const auto& c : C) {
|
| 12 |
+
for (const auto& d : D) {
|
| 13 |
+
if (A_B_sum.find(-c - d) != A_B_sum.end()) {
|
| 14 |
+
result += A_B_sum[-c - d];
|
| 15 |
+
}
|
| 16 |
+
}
|
| 17 |
+
}
|
| 18 |
+
return result;
|
| 19 |
+
}
|
| 20 |
+
};
|
HiBench/Code/c++Selected/12.cpp
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
vector<vector<int> > fourSum(vector<int> &nums, int target) {
|
| 4 |
+
sort(begin(nums), end(nums));
|
| 5 |
+
vector<vector<int>> result;
|
| 6 |
+
for (int i = 0; i + 3 < size(nums); ++i) {
|
| 7 |
+
if (i && nums[i] == nums[i - 1]) {
|
| 8 |
+
continue;
|
| 9 |
+
}
|
| 10 |
+
for (int j = i + 1; j + 2 < size(nums); ++j) {
|
| 11 |
+
if (j != i + 1 && nums[j] == nums[j - 1]) {
|
| 12 |
+
continue;
|
| 13 |
+
}
|
| 14 |
+
const auto& total = target - nums[i] - nums[j];
|
| 15 |
+
int left = j + 1, right = size(nums) - 1;
|
| 16 |
+
while (left < right) {
|
| 17 |
+
if (nums[left] + nums[right] == total) {
|
| 18 |
+
result.push_back({nums[i], nums[j], nums[left], nums[right]});
|
| 19 |
+
++left, --right;
|
| 20 |
+
while (left < right && nums[left] == nums[left - 1]) {
|
| 21 |
+
++left;
|
| 22 |
+
}
|
| 23 |
+
while (left < right && nums[right] == nums[right + 1]) {
|
| 24 |
+
--right;
|
| 25 |
+
}
|
| 26 |
+
} else {
|
| 27 |
+
if (nums[left] + nums[right] > total) {
|
| 28 |
+
--right;
|
| 29 |
+
} else {
|
| 30 |
+
++left;
|
| 31 |
+
}
|
| 32 |
+
}
|
| 33 |
+
}
|
| 34 |
+
}
|
| 35 |
+
}
|
| 36 |
+
return result;
|
| 37 |
+
}
|
| 38 |
+
};
|
HiBench/Code/c++Selected/13.cpp
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
bool isSameAfterReversals(int num) {
|
| 4 |
+
return (num == 0 || num % 10);
|
| 5 |
+
}
|
| 6 |
+
};
|
HiBench/Code/c++Selected/14.cpp
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
string abbreviateProduct(int left, int right) {
|
| 4 |
+
static const int PREFIX_LEN = 5;
|
| 5 |
+
static const int SUFFIX_LEN = 5;
|
| 6 |
+
static const int64_t MOD = pow(10, PREFIX_LEN + SUFFIX_LEN);
|
| 7 |
+
|
| 8 |
+
int64_t curr = 1, zeros = 0;
|
| 9 |
+
bool abbr = false;
|
| 10 |
+
double decimal = 0.0;
|
| 11 |
+
for (int64_t i = left; i <= right; ++i, curr %= MOD) {
|
| 12 |
+
curr *= i;
|
| 13 |
+
decimal += log10(i);
|
| 14 |
+
while (curr % 10 == 0) {
|
| 15 |
+
curr /= 10;
|
| 16 |
+
++zeros;
|
| 17 |
+
}
|
| 18 |
+
if (curr >= MOD) {
|
| 19 |
+
abbr = true;
|
| 20 |
+
}
|
| 21 |
+
}
|
| 22 |
+
if (!abbr) {
|
| 23 |
+
return to_string(curr) + "e" + to_string(zeros);
|
| 24 |
+
}
|
| 25 |
+
decimal -= int(decimal);
|
| 26 |
+
const auto& prefix = to_string(static_cast<int>(pow(10, decimal + (PREFIX_LEN - 1))));
|
| 27 |
+
const auto& suffix = to_string(curr % static_cast<int>(pow(10, SUFFIX_LEN)));
|
| 28 |
+
return prefix + "..." + string(SUFFIX_LEN - size(suffix), '0') + suffix + "e" + to_string(zeros);
|
| 29 |
+
}
|
| 30 |
+
};
|
HiBench/Code/c++Selected/15.cpp
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
int accountBalanceAfterPurchase(int purchaseAmount) {
|
| 4 |
+
return 100 - (purchaseAmount + 5) / 10 * 10;
|
| 5 |
+
}
|
| 6 |
+
};
|
HiBench/Code/c++Selected/16.cpp
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
vector<vector<string>> accountsMerge(vector<vector<string>>& accounts) {
|
| 4 |
+
UnionFind union_find;
|
| 5 |
+
unordered_map<string, string> email_to_name;
|
| 6 |
+
unordered_map<string, int> email_to_id;
|
| 7 |
+
for (const auto& account : accounts) {
|
| 8 |
+
const auto& name = account[0];
|
| 9 |
+
for (int i = 1; i < account.size(); ++i) {
|
| 10 |
+
if (!email_to_id.count(account[i])) {
|
| 11 |
+
email_to_name[account[i]] = name;
|
| 12 |
+
email_to_id[account[i]] = union_find.get_id();
|
| 13 |
+
}
|
| 14 |
+
union_find.union_set(email_to_id[account[1]], email_to_id[account[i]]);
|
| 15 |
+
}
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
unordered_map<int, set<string>> lookup;
|
| 19 |
+
for (const auto& kvp : email_to_name) {
|
| 20 |
+
const auto& email = kvp.first;
|
| 21 |
+
lookup[union_find.find_set(email_to_id[email])].emplace(email);
|
| 22 |
+
}
|
| 23 |
+
vector<vector<string>> result;
|
| 24 |
+
for (const auto& kvp : lookup) {
|
| 25 |
+
const auto& emails = kvp.second;
|
| 26 |
+
vector<string> tmp{email_to_name[*emails.begin()]};
|
| 27 |
+
for (const auto& email : emails) {
|
| 28 |
+
tmp.emplace_back(email);
|
| 29 |
+
}
|
| 30 |
+
result.emplace_back(move(tmp));
|
| 31 |
+
}
|
| 32 |
+
return result;
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
private:
|
| 36 |
+
class UnionFind {
|
| 37 |
+
public:
|
| 38 |
+
int get_id() {
|
| 39 |
+
set_.emplace_back(set_.size());
|
| 40 |
+
return set_.size() - 1;
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
int find_set(const int x) {
|
| 44 |
+
if (set_[x] != x) {
|
| 45 |
+
set_[x] = find_set(set_[x]); // Path compression.
|
| 46 |
+
}
|
| 47 |
+
return set_[x];
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
void union_set(const int x, const int y) {
|
| 51 |
+
int x_root = find_set(x), y_root = find_set(y);
|
| 52 |
+
if (x_root != y_root) {
|
| 53 |
+
set_[min(x_root, y_root)] = max(x_root, y_root);
|
| 54 |
+
}
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
private:
|
| 58 |
+
vector<int> set_;
|
| 59 |
+
};
|
| 60 |
+
};
|
HiBench/Code/c++Selected/17.cpp
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
string addBinary(string a, string b) {
|
| 4 |
+
string res;
|
| 5 |
+
size_t res_len = max(a.length(), b.length()) ;
|
| 6 |
+
|
| 7 |
+
size_t carry = 0;
|
| 8 |
+
for (int i = 0; i < res_len; ++i) {
|
| 9 |
+
const size_t a_bit_i = i < a.length() ? a[a.length() - 1 - i] - '0' : 0;
|
| 10 |
+
const size_t b_bit_i = i < b.length() ? b[b.length() - 1 - i] - '0' : 0;
|
| 11 |
+
size_t sum = carry + a_bit_i + b_bit_i;
|
| 12 |
+
carry = sum / 2;
|
| 13 |
+
sum %= 2;
|
| 14 |
+
res.push_back('0' + sum);
|
| 15 |
+
}
|
| 16 |
+
if (carry) {
|
| 17 |
+
res.push_back('0' + carry);
|
| 18 |
+
}
|
| 19 |
+
reverse(res.begin(), res.end());
|
| 20 |
+
|
| 21 |
+
return res;
|
| 22 |
+
}
|
| 23 |
+
};
|
HiBench/Code/c++Selected/18.cpp
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
string addBoldTag(string s, vector<string>& dict) {
|
| 4 |
+
vector<bool> lookup(s.length());
|
| 5 |
+
for (const auto& d: dict) {
|
| 6 |
+
auto pos = -1;
|
| 7 |
+
while ((pos = s.find(d, pos + 1)) != string::npos) {
|
| 8 |
+
fill(lookup.begin() + pos, lookup.begin() + pos + d.length(), true);
|
| 9 |
+
}
|
| 10 |
+
}
|
| 11 |
+
string result;
|
| 12 |
+
for (int i = 0; i < s.length(); ++i) {
|
| 13 |
+
if (lookup[i] && (i == 0 || !lookup[i - 1])) {
|
| 14 |
+
result += "<b>";
|
| 15 |
+
}
|
| 16 |
+
result.push_back(s[i]);
|
| 17 |
+
if (lookup[i] && (i == (s.length() - 1) || !lookup[i + 1])) {
|
| 18 |
+
result += "</b>";
|
| 19 |
+
}
|
| 20 |
+
}
|
| 21 |
+
return result;
|
| 22 |
+
}
|
| 23 |
+
};
|
HiBench/Code/c++Selected/19.cpp
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
int addDigits(int num) {
|
| 4 |
+
return (num - 1) % 9 + 1;
|
| 5 |
+
}
|
| 6 |
+
};
|
HiBench/Code/c++Selected/2.cpp
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
bool isOneBitCharacter(vector<int>& bits) {
|
| 4 |
+
auto parity = 0;
|
| 5 |
+
for (int i = static_cast<int>(bits.size()) - 2;
|
| 6 |
+
i >= 0 && bits[i]; --i) {
|
| 7 |
+
parity ^= bits[i];
|
| 8 |
+
}
|
| 9 |
+
return parity == 0;
|
| 10 |
+
}
|
| 11 |
+
};
|
HiBench/Code/c++Selected/20.cpp
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
bool isPossible(int n, vector<vector<int>>& edges) {
|
| 4 |
+
vector<unordered_set<int>> adj(n);
|
| 5 |
+
for (const auto& e : edges) {
|
| 6 |
+
adj[e[0] - 1].emplace(e[1] - 1);
|
| 7 |
+
adj[e[1] - 1].emplace(e[0] - 1);
|
| 8 |
+
}
|
| 9 |
+
vector<int> odds;
|
| 10 |
+
for (int u = 0; u < n; ++u) {
|
| 11 |
+
if (size(adj[u]) % 2) {
|
| 12 |
+
odds.emplace_back(u);
|
| 13 |
+
}
|
| 14 |
+
}
|
| 15 |
+
if (size(odds) == 0) {
|
| 16 |
+
return true;
|
| 17 |
+
}
|
| 18 |
+
if (size(odds) == 2) {
|
| 19 |
+
for (int u = 0; u < n; ++u) {
|
| 20 |
+
if (!adj[u].count(odds[0]) && !adj[u].count(odds[1])) {
|
| 21 |
+
return true;
|
| 22 |
+
}
|
| 23 |
+
}
|
| 24 |
+
return false;
|
| 25 |
+
}
|
| 26 |
+
if (size(odds) == 4) {
|
| 27 |
+
return ((!adj[odds[1]].count(odds[0]) && !adj[odds[3]].count(odds[2])) ||
|
| 28 |
+
(!adj[odds[2]].count(odds[0]) && !adj[odds[3]].count(odds[1])) ||
|
| 29 |
+
(!adj[odds[3]].count(odds[0]) && !adj[odds[2]].count(odds[1])));
|
| 30 |
+
}
|
| 31 |
+
return false;
|
| 32 |
+
}
|
| 33 |
+
};
|
HiBench/Code/c++Selected/21.cpp
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
int addRungs(vector<int>& rungs, int dist) {
|
| 4 |
+
int result = 0, prev = 0;
|
| 5 |
+
for (const auto& curr : rungs) {
|
| 6 |
+
result += ceil_divide(curr - prev, dist) - 1;
|
| 7 |
+
prev = curr;
|
| 8 |
+
}
|
| 9 |
+
return result;
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
private:
|
| 13 |
+
int ceil_divide(int a, int b) {
|
| 14 |
+
return (a + (b - 1)) / b;
|
| 15 |
+
}
|
| 16 |
+
};
|
HiBench/Code/c++Selected/22.cpp
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
TreeNode* addOneRow(TreeNode* root, int v, int d) {
|
| 4 |
+
if (d == 0 || d == 1) {
|
| 5 |
+
auto node = new TreeNode(v);
|
| 6 |
+
(d == 1 ? node->left : node->right) = root;
|
| 7 |
+
return node;
|
| 8 |
+
}
|
| 9 |
+
if (root && d >= 2) {
|
| 10 |
+
root->left = addOneRow(root->left, v, d > 2 ? d - 1 : 1);
|
| 11 |
+
root->right = addOneRow(root->right, v, d > 2 ? d - 1 : 0);
|
| 12 |
+
}
|
| 13 |
+
return root;
|
| 14 |
+
}
|
| 15 |
+
};
|
HiBench/Code/c++Selected/23.cpp
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
string addStrings(string num1, string num2) {
|
| 4 |
+
string result;
|
| 5 |
+
|
| 6 |
+
for (int i = num1.size() - 1, j = num2.size() - 1, carry = 0;
|
| 7 |
+
i >= 0 || j >= 0 || carry;
|
| 8 |
+
carry /= 10) {
|
| 9 |
+
|
| 10 |
+
if (i >= 0) {
|
| 11 |
+
carry += num1[i--] - '0';
|
| 12 |
+
}
|
| 13 |
+
if (j >= 0) {
|
| 14 |
+
carry += num2[j--] - '0';
|
| 15 |
+
}
|
| 16 |
+
result += to_string(carry % 10);
|
| 17 |
+
}
|
| 18 |
+
reverse(result.begin(), result.end());
|
| 19 |
+
|
| 20 |
+
return result;
|
| 21 |
+
}
|
| 22 |
+
};
|
HiBench/Code/c++Selected/24.cpp
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
vector<int> addToArrayForm(vector<int>& A, int K) {
|
| 4 |
+
reverse(A.begin(), A.end());
|
| 5 |
+
int carry = K, i = 0;
|
| 6 |
+
A[i] += carry;
|
| 7 |
+
carry = A[i] / 10;
|
| 8 |
+
A[i] %= 10;
|
| 9 |
+
while (carry) {
|
| 10 |
+
++i;
|
| 11 |
+
if (i < A.size()) {
|
| 12 |
+
A[i] += carry;
|
| 13 |
+
} else {
|
| 14 |
+
A.emplace_back(carry);
|
| 15 |
+
}
|
| 16 |
+
carry = A[i] / 10;
|
| 17 |
+
A[i] %= 10;
|
| 18 |
+
}
|
| 19 |
+
reverse(A.begin(), A.end());
|
| 20 |
+
return A;
|
| 21 |
+
}
|
| 22 |
+
};
|
HiBench/Code/c++Selected/25.cpp
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
int sum(int num1, int num2) {
|
| 4 |
+
return num1 + num2;
|
| 5 |
+
}
|
| 6 |
+
};
|
HiBench/Code/c++Selected/26.cpp
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
|
| 4 |
+
stack<int> stk1, stk2;
|
| 5 |
+
while (l1) {
|
| 6 |
+
stk1.emplace(l1->val);
|
| 7 |
+
l1 = l1->next;
|
| 8 |
+
}
|
| 9 |
+
while (l2) {
|
| 10 |
+
stk2.emplace(l2->val);
|
| 11 |
+
l2 = l2->next;
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
ListNode *prev = nullptr, *head = nullptr;
|
| 15 |
+
int sum = 0;
|
| 16 |
+
while (!stk1.empty() || !stk2.empty()) {
|
| 17 |
+
sum /= 10;
|
| 18 |
+
if (!stk1.empty()) {
|
| 19 |
+
sum += stk1.top();
|
| 20 |
+
stk1.pop();
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
if (!stk2.empty()) {
|
| 24 |
+
sum += stk2.top();
|
| 25 |
+
stk2.pop();
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
head = new ListNode(sum % 10);
|
| 29 |
+
head->next = prev;
|
| 30 |
+
prev = head;
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
if (sum >= 10) {
|
| 34 |
+
head = new ListNode(sum / 10);
|
| 35 |
+
head->next = prev;
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
return head;
|
| 39 |
+
}
|
| 40 |
+
};
|
HiBench/Code/c++Selected/27.cpp
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
|
| 4 |
+
ListNode dummy{0};
|
| 5 |
+
auto curr = &dummy;
|
| 6 |
+
|
| 7 |
+
auto carry = 0;
|
| 8 |
+
while (l1 || l2 || carry) {
|
| 9 |
+
auto a = l1? l1->val : 0, b = l2? l2->val : 0;
|
| 10 |
+
auto val = carry + a + b;
|
| 11 |
+
curr->next = new ListNode(val % 10);
|
| 12 |
+
carry = val / 10;
|
| 13 |
+
l1 = l1 ? l1->next : nullptr;
|
| 14 |
+
l2 = l2 ? l2->next : nullptr;
|
| 15 |
+
curr = curr->next;
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
return dummy.next;
|
| 19 |
+
}
|
| 20 |
+
};
|
HiBench/Code/c++Selected/28.cpp
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
PolyNode* addPoly(PolyNode* poly1, PolyNode* poly2) {
|
| 4 |
+
PolyNode dummy;
|
| 5 |
+
auto curr = &dummy;
|
| 6 |
+
while (poly1 && poly2) {
|
| 7 |
+
if (poly1->power > poly2->power) {
|
| 8 |
+
curr->next = poly1;
|
| 9 |
+
curr = curr->next;
|
| 10 |
+
poly1 = poly1->next;
|
| 11 |
+
} else if (poly1->power < poly2->power) {
|
| 12 |
+
curr->next = poly2;
|
| 13 |
+
curr = curr->next;
|
| 14 |
+
poly2 = poly2->next;
|
| 15 |
+
} else {
|
| 16 |
+
auto coef = poly1->coefficient + poly2->coefficient;
|
| 17 |
+
if (coef) {
|
| 18 |
+
curr->next = new PolyNode(coef, poly1->power);
|
| 19 |
+
curr = curr->next;
|
| 20 |
+
}
|
| 21 |
+
poly1 = poly1->next;
|
| 22 |
+
poly2 = poly2->next;
|
| 23 |
+
}
|
| 24 |
+
}
|
| 25 |
+
curr->next = poly1 ? poly1 : poly2;
|
| 26 |
+
return dummy.next;
|
| 27 |
+
}
|
| 28 |
+
};
|
HiBench/Code/c++Selected/29.cpp
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
string addSpaces(string s, vector<int>& spaces) {
|
| 4 |
+
int prev = size(s);
|
| 5 |
+
s.resize(size(s) + size(spaces));
|
| 6 |
+
for(int i = size(spaces) - 1; i >= 0; --i) {
|
| 7 |
+
for (int j = prev - 1; j >= spaces[i]; --j) {
|
| 8 |
+
s[j + 1 + i] = s[j];
|
| 9 |
+
}
|
| 10 |
+
s[spaces[i] + i] = ' ';
|
| 11 |
+
prev = spaces[i];
|
| 12 |
+
}
|
| 13 |
+
return s;
|
| 14 |
+
}
|
| 15 |
+
};
|
HiBench/Code/c++Selected/3.cpp
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
bool find132pattern(vector<int>& nums) {
|
| 4 |
+
int ak = numeric_limits<int>::min();
|
| 5 |
+
vector<int> stk;
|
| 6 |
+
for (int i = size(nums) - 1; i >= 0; --i) {
|
| 7 |
+
if (nums[i] < ak) {
|
| 8 |
+
return true;
|
| 9 |
+
}
|
| 10 |
+
while (!empty(stk) && stk.back() < nums[i]) {
|
| 11 |
+
ak = stk.back(), stk.pop_back();
|
| 12 |
+
}
|
| 13 |
+
stk.emplace_back(nums[i]);
|
| 14 |
+
}
|
| 15 |
+
return false;
|
| 16 |
+
}
|
| 17 |
+
};
|
HiBench/Code/c++Selected/30.cpp
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
vector<int> addNegabinary(vector<int>& arr1, vector<int>& arr2) {
|
| 4 |
+
vector<int> result;
|
| 5 |
+
int carry = 0, i = arr1.size() - 1, j = arr2.size() - 1;
|
| 6 |
+
while (i >= 0 || j >= 0 || carry) {
|
| 7 |
+
if (i >= 0) {
|
| 8 |
+
carry += arr1[i--];
|
| 9 |
+
}
|
| 10 |
+
if (j >= 0) {
|
| 11 |
+
carry += arr2[j--];
|
| 12 |
+
}
|
| 13 |
+
result.emplace_back(carry & 1);
|
| 14 |
+
carry = -(carry >> 1);
|
| 15 |
+
}
|
| 16 |
+
while (result.size() > 1 && result.back() == 0) {
|
| 17 |
+
result.pop_back();
|
| 18 |
+
}
|
| 19 |
+
reverse(result.begin(), result.end());
|
| 20 |
+
return result;
|
| 21 |
+
}
|
| 22 |
+
};
|
HiBench/Code/c++Selected/31.cpp
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
bool isAdditiveNumber(string num) {
|
| 4 |
+
for (int i = 1; i < num.length(); ++i) {
|
| 5 |
+
for (int j = i + 1; j < num.length(); ++j) {
|
| 6 |
+
string s1 = num.substr(0, i), s2 = num.substr(i, j - i);
|
| 7 |
+
if ((s1.length() > 1 && s1[0] == '0') ||
|
| 8 |
+
(s2.length() > 1 && s2[0] == '0')) {
|
| 9 |
+
continue;
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
string next = add(s1, s2);
|
| 13 |
+
string cur = s1 + s2 + next;
|
| 14 |
+
while (cur.length() < num.length()) {
|
| 15 |
+
s1 = s2;
|
| 16 |
+
s2 = next;
|
| 17 |
+
next = add(s1, s2);
|
| 18 |
+
cur += next;
|
| 19 |
+
}
|
| 20 |
+
if (cur == num) {
|
| 21 |
+
return true;
|
| 22 |
+
}
|
| 23 |
+
}
|
| 24 |
+
}
|
| 25 |
+
return false;
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
private:
|
| 29 |
+
string add(const string& m, const string& n) {
|
| 30 |
+
string res;
|
| 31 |
+
int res_length = max(m.length(), n.length()) ;
|
| 32 |
+
|
| 33 |
+
int carry = 0;
|
| 34 |
+
for (int i = 0; i < res_length; ++i) {
|
| 35 |
+
int m_digit_i = i < m.length() ? m[m.length() - 1 - i] - '0' : 0;
|
| 36 |
+
int n_digit_i = i < n.length() ? n[n.length() - 1 - i] - '0' : 0;
|
| 37 |
+
int sum = carry + m_digit_i + n_digit_i;
|
| 38 |
+
carry = sum / 10;
|
| 39 |
+
sum %= 10;
|
| 40 |
+
res.push_back('0' + sum);
|
| 41 |
+
}
|
| 42 |
+
if (carry) {
|
| 43 |
+
res.push_back('0' + carry);
|
| 44 |
+
}
|
| 45 |
+
reverse(res.begin(), res.end());
|
| 46 |
+
|
| 47 |
+
return res;
|
| 48 |
+
}
|
| 49 |
+
};
|
HiBench/Code/c++Selected/32.cpp
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
bool hasIncreasingSubarrays(vector<int>& nums, int k) {
|
| 4 |
+
int result = 0;
|
| 5 |
+
int curr = 1, prev = 0;
|
| 6 |
+
for (int i = 0; i + 1 < size(nums); ++i) {
|
| 7 |
+
if (nums[i] < nums[i + 1]) {
|
| 8 |
+
++curr;
|
| 9 |
+
} else {
|
| 10 |
+
prev = curr;
|
| 11 |
+
curr = 1;
|
| 12 |
+
}
|
| 13 |
+
result = max({result, curr / 2, min(prev, curr)});
|
| 14 |
+
}
|
| 15 |
+
return result >= k;
|
| 16 |
+
}
|
| 17 |
+
};
|
HiBench/Code/c++Selected/33.cpp
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
int maxIncreasingSubarrays(vector<int>& nums) {
|
| 4 |
+
int result = 0;
|
| 5 |
+
int curr = 1, prev = 0;
|
| 6 |
+
for (int i = 0; i + 1 < size(nums); ++i) {
|
| 7 |
+
if (nums[i] < nums[i + 1]) {
|
| 8 |
+
++curr;
|
| 9 |
+
} else {
|
| 10 |
+
prev = curr;
|
| 11 |
+
curr = 1;
|
| 12 |
+
}
|
| 13 |
+
result = max({result, curr / 2, min(prev, curr)});
|
| 14 |
+
}
|
| 15 |
+
return result;
|
| 16 |
+
}
|
| 17 |
+
};
|
HiBench/Code/c++Selected/34.cpp
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
vector<int> advantageCount(vector<int>& A, vector<int>& B) {
|
| 4 |
+
vector<int> sortedA(A.cbegin(), A.cend());
|
| 5 |
+
sort(sortedA.begin(), sortedA.end());
|
| 6 |
+
vector<int> sortedB(B.cbegin(), B.cend());
|
| 7 |
+
sort(sortedB.begin(), sortedB.end());
|
| 8 |
+
|
| 9 |
+
unordered_map<int, vector<int>> candidates;
|
| 10 |
+
vector<int> others;
|
| 11 |
+
int j = 0;
|
| 12 |
+
for (const auto& a : sortedA) {
|
| 13 |
+
if (a > sortedB[j]) {
|
| 14 |
+
candidates[sortedB[j]].emplace_back(a);
|
| 15 |
+
++j;
|
| 16 |
+
} else {
|
| 17 |
+
others.emplace_back(a);
|
| 18 |
+
}
|
| 19 |
+
}
|
| 20 |
+
vector<int> result;
|
| 21 |
+
for (const auto& b : B) {
|
| 22 |
+
if (!candidates[b].empty()) {
|
| 23 |
+
result.emplace_back(candidates[b].back());
|
| 24 |
+
candidates[b].pop_back();
|
| 25 |
+
} else {
|
| 26 |
+
result.emplace_back(others.back());
|
| 27 |
+
others.pop_back();
|
| 28 |
+
}
|
| 29 |
+
}
|
| 30 |
+
return result;
|
| 31 |
+
}
|
| 32 |
+
};
|
HiBench/Code/c++Selected/35.cpp
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
double nthPersonGetsNthSeat(int n) {
|
| 4 |
+
return n != 1 ? 0.5 : 1.0;
|
| 5 |
+
}
|
| 6 |
+
};
|
HiBench/Code/c++Selected/36.cpp
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
vector<string> alertNames(vector<string>& keyName, vector<string>& keyTime) {
|
| 4 |
+
static const int THRESHOLD = 3;
|
| 5 |
+
unordered_map<string, vector<int>> name_to_times;
|
| 6 |
+
for (int i = 0; i < size(keyName); ++i) {
|
| 7 |
+
int hour = stoi(keyTime[i].substr(0, 2));
|
| 8 |
+
int minute = stoi(keyTime[i].substr(3));
|
| 9 |
+
name_to_times[keyName[i]].emplace_back(hour * 60 + minute);
|
| 10 |
+
}
|
| 11 |
+
vector<string> result;
|
| 12 |
+
for (auto& [name, times] : name_to_times) {
|
| 13 |
+
sort(begin(times), end(times));
|
| 14 |
+
for (int left = 0, right = 0; right < size(times); ++right) {
|
| 15 |
+
while (times[right] - times[left] > 60) {
|
| 16 |
+
++left;
|
| 17 |
+
}
|
| 18 |
+
if (right - left + 1 >= THRESHOLD) {
|
| 19 |
+
result.emplace_back(name);
|
| 20 |
+
break;
|
| 21 |
+
}
|
| 22 |
+
}
|
| 23 |
+
}
|
| 24 |
+
sort(begin(result), end(result));
|
| 25 |
+
return result;
|
| 26 |
+
}
|
| 27 |
+
};
|
HiBench/Code/c++Selected/37.cpp
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
long long flowerGame(int n, int m) {
|
| 4 |
+
return (1ll * n * m) / 2;
|
| 5 |
+
}
|
| 6 |
+
};
|
HiBench/Code/c++Selected/38.cpp
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
string alienOrder(vector<string>& words) {
|
| 4 |
+
unordered_set<char> nodes;
|
| 5 |
+
unordered_map<char, unordered_set<char>> in_degree, out_degree;
|
| 6 |
+
queue<char> zero_in_degree_queue;
|
| 7 |
+
for (const auto& word : words) {
|
| 8 |
+
for (const auto& c : word) {
|
| 9 |
+
nodes.emplace(c);
|
| 10 |
+
}
|
| 11 |
+
}
|
| 12 |
+
for (int i = 1; i < words.size(); ++i) {
|
| 13 |
+
if (words[i - 1].length() > words[i].length() &&
|
| 14 |
+
words[i - 1].substr(0, words[i].length()) == words[i]) {
|
| 15 |
+
return "";
|
| 16 |
+
}
|
| 17 |
+
findEdges(words[i - 1], words[i], &in_degree, &out_degree);
|
| 18 |
+
}
|
| 19 |
+
for (const auto& node : nodes) {
|
| 20 |
+
if (in_degree.find(node) == in_degree.end()) {
|
| 21 |
+
zero_in_degree_queue.emplace(node);
|
| 22 |
+
}
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
string result;
|
| 26 |
+
while (!zero_in_degree_queue.empty()) {
|
| 27 |
+
const auto& precedence = zero_in_degree_queue.front();
|
| 28 |
+
zero_in_degree_queue.pop();
|
| 29 |
+
result.push_back(precedence);
|
| 30 |
+
|
| 31 |
+
if (out_degree.find(precedence) != out_degree.end()) {
|
| 32 |
+
for (const auto& c : out_degree[precedence]) {
|
| 33 |
+
in_degree[c].erase(precedence);
|
| 34 |
+
if (in_degree[c].empty()) {
|
| 35 |
+
zero_in_degree_queue.emplace(c);
|
| 36 |
+
}
|
| 37 |
+
}
|
| 38 |
+
out_degree.erase(precedence);
|
| 39 |
+
}
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
if (!out_degree.empty()) {
|
| 43 |
+
return "";
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
return result;
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
private:
|
| 50 |
+
void findEdges(const string &word1, const string &word2,
|
| 51 |
+
unordered_map<char, unordered_set<char>> *in_degree,
|
| 52 |
+
unordered_map<char, unordered_set<char>> *out_degree) {
|
| 53 |
+
const int len = min(word1.length(), word2.length());
|
| 54 |
+
for (int i = 0; i < len; ++i) {
|
| 55 |
+
if (word1[i] != word2[i]) {
|
| 56 |
+
(*in_degree)[word2[i]].emplace(word1[i]);
|
| 57 |
+
(*out_degree)[word1[i]].emplace(word2[i]);
|
| 58 |
+
break;
|
| 59 |
+
}
|
| 60 |
+
}
|
| 61 |
+
}
|
| 62 |
+
};
|
HiBench/Code/c++Selected/39.cpp
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {
|
| 4 |
+
vector<vector<int>> adj(n);
|
| 5 |
+
for (const auto& e : edges) {
|
| 6 |
+
adj[e[0]].emplace_back(e[1]);
|
| 7 |
+
}
|
| 8 |
+
vector<vector<int>> result(n);
|
| 9 |
+
for (int u = 0; u < n; ++u) {
|
| 10 |
+
iter_dfs(adj, u, &result);
|
| 11 |
+
}
|
| 12 |
+
return result;
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
private:
|
| 16 |
+
void iter_dfs(const vector<vector<int>>& adj,
|
| 17 |
+
int i,
|
| 18 |
+
vector<vector<int>> *result) {
|
| 19 |
+
|
| 20 |
+
vector<bool> lookup(size(adj));
|
| 21 |
+
vector<int> stk = {i};
|
| 22 |
+
while (!empty(stk)) {
|
| 23 |
+
const auto u = stk.back(); stk.pop_back();
|
| 24 |
+
for (const auto& v: adj[u]) {
|
| 25 |
+
if (lookup[v]) {
|
| 26 |
+
continue;
|
| 27 |
+
}
|
| 28 |
+
lookup[v] = true;
|
| 29 |
+
stk.emplace_back(v);
|
| 30 |
+
(*result)[v].emplace_back(i);
|
| 31 |
+
}
|
| 32 |
+
}
|
| 33 |
+
}
|
| 34 |
+
};
|
HiBench/Code/c++Selected/4.cpp
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
int minSteps(int n) {
|
| 4 |
+
auto result = 0;
|
| 5 |
+
for (auto p = 2 ; p * p <= n; ++p) {
|
| 6 |
+
while (n % p == 0) {
|
| 7 |
+
result += p;
|
| 8 |
+
n /= p;
|
| 9 |
+
}
|
| 10 |
+
}
|
| 11 |
+
result += (n > 1) ? n : 0;
|
| 12 |
+
return result;
|
| 13 |
+
}
|
| 14 |
+
};
|
HiBench/Code/c++Selected/40.cpp
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
vector<int> maxScoreIndices(vector<int>& nums) {
|
| 4 |
+
const int total = accumulate(cbegin(nums), cend(nums), 0);
|
| 5 |
+
vector<int> result;
|
| 6 |
+
for (int i = 0, zeros = 0, mx = 0; i <= size(nums); ++i) {
|
| 7 |
+
zeros += static_cast<int>(((i - 1 >= 0) ? nums[i - 1] : 0) == 0);
|
| 8 |
+
if (zeros + (total - (i - zeros)) > mx) {
|
| 9 |
+
mx = zeros + (total - (i - zeros));
|
| 10 |
+
result.clear();
|
| 11 |
+
}
|
| 12 |
+
if (zeros + (total - (i - zeros)) == mx) {
|
| 13 |
+
result.emplace_back(i);
|
| 14 |
+
}
|
| 15 |
+
}
|
| 16 |
+
return result;
|
| 17 |
+
}
|
| 18 |
+
};
|
HiBench/Code/c++Selected/41.cpp
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {
|
| 4 |
+
vector<int> result;
|
| 5 |
+
Iterator<TreeNode*> left(root1), right(root2);
|
| 6 |
+
while (*left || *right) {
|
| 7 |
+
if (!*right || ((*left) && (*left)->val < (*right)->val)) {
|
| 8 |
+
result.emplace_back((*left)->val);
|
| 9 |
+
++left;
|
| 10 |
+
} else {
|
| 11 |
+
result.emplace_back((*right)->val);
|
| 12 |
+
++right;
|
| 13 |
+
}
|
| 14 |
+
}
|
| 15 |
+
return result;
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
private:
|
| 19 |
+
template<typename T>
|
| 20 |
+
class Iterator {
|
| 21 |
+
public:
|
| 22 |
+
Iterator(T root)
|
| 23 |
+
: stack_{{root, false}}
|
| 24 |
+
, curr_{} {
|
| 25 |
+
++(*this);
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
Iterator& operator++() {
|
| 29 |
+
while (!stack_.empty()) {
|
| 30 |
+
T root; bool is_visited;
|
| 31 |
+
tie(root, is_visited) = stack_.back(); stack_.pop_back();
|
| 32 |
+
if (!root) {
|
| 33 |
+
continue;
|
| 34 |
+
}
|
| 35 |
+
if (is_visited) {
|
| 36 |
+
curr_ = root;
|
| 37 |
+
return *this;
|
| 38 |
+
}
|
| 39 |
+
stack_.emplace_back(root->right, false);
|
| 40 |
+
stack_.emplace_back(root, true);
|
| 41 |
+
stack_.emplace_back(root->left, false);
|
| 42 |
+
}
|
| 43 |
+
curr_ = T{};
|
| 44 |
+
return *this;
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
const T& operator*() const {
|
| 48 |
+
return curr_;
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
private:
|
| 52 |
+
vector<pair<T, bool>> stack_;
|
| 53 |
+
T curr_;
|
| 54 |
+
};
|
| 55 |
+
};
|
HiBench/Code/c++Selected/42.cpp
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
vector<int> distanceK(TreeNode* root, TreeNode* target, int K) {
|
| 4 |
+
unordered_map<int, vector<int>> neighbors;
|
| 5 |
+
dfs(nullptr, root, &neighbors);
|
| 6 |
+
|
| 7 |
+
vector<int> bfs{target->val};
|
| 8 |
+
unordered_set<int> lookup{target->val};
|
| 9 |
+
for (int i = 0; i < K; ++i) {
|
| 10 |
+
vector<int> curr;
|
| 11 |
+
for (const auto& node : bfs) {
|
| 12 |
+
for (const auto& nei : neighbors[node]) {
|
| 13 |
+
if (!lookup.count(nei)) {
|
| 14 |
+
curr.emplace_back(nei);
|
| 15 |
+
lookup.emplace(nei);
|
| 16 |
+
}
|
| 17 |
+
}
|
| 18 |
+
}
|
| 19 |
+
swap(bfs, curr);
|
| 20 |
+
}
|
| 21 |
+
return bfs;
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
private:
|
| 25 |
+
void dfs(TreeNode *parent, TreeNode *child,
|
| 26 |
+
unordered_map<int, vector<int>> *neighbors) {
|
| 27 |
+
if (!child) {
|
| 28 |
+
return;
|
| 29 |
+
}
|
| 30 |
+
if (parent) {
|
| 31 |
+
(*neighbors)[parent->val].emplace_back(child->val);
|
| 32 |
+
(*neighbors)[child->val].emplace_back(parent->val);
|
| 33 |
+
}
|
| 34 |
+
dfs(child, child->left, neighbors);
|
| 35 |
+
dfs(child, child->right, neighbors);
|
| 36 |
+
}
|
| 37 |
+
};
|
HiBench/Code/c++Selected/43.cpp
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
enum Status { UNVISITED, VISITING, DONE };
|
| 4 |
+
|
| 5 |
+
bool leadsToDestination(int n, vector<vector<int>>& edges, int source, int destination) {
|
| 6 |
+
unordered_map<int, vector<int>> children;
|
| 7 |
+
for (const auto& edge : edges) {
|
| 8 |
+
children[edge[0]].emplace_back(edge[1]);
|
| 9 |
+
}
|
| 10 |
+
vector<Status> status(n);
|
| 11 |
+
return dfs(children, source, destination, &status);
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
private:
|
| 15 |
+
bool dfs(const unordered_map<int, vector<int>>& children,
|
| 16 |
+
int node,
|
| 17 |
+
int destination,
|
| 18 |
+
vector<Status> *status) {
|
| 19 |
+
|
| 20 |
+
if ((*status)[node] == DONE) {
|
| 21 |
+
return true;
|
| 22 |
+
}
|
| 23 |
+
if ((*status)[node] == VISITING) {
|
| 24 |
+
return false;
|
| 25 |
+
}
|
| 26 |
+
(*status)[node] = VISITING;
|
| 27 |
+
if (!children.count(node) && node != destination) {
|
| 28 |
+
return false;
|
| 29 |
+
}
|
| 30 |
+
if (children.count(node)) {
|
| 31 |
+
for (const auto& child : children.at(node)) {
|
| 32 |
+
if (!dfs(children, child, destination, status)) {
|
| 33 |
+
return false;
|
| 34 |
+
}
|
| 35 |
+
}
|
| 36 |
+
}
|
| 37 |
+
(*status)[node] = DONE;
|
| 38 |
+
return true;
|
| 39 |
+
}
|
| 40 |
+
};
|
HiBench/Code/c++Selected/44.cpp
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
|
| 4 |
+
vector<vector<int>> result;
|
| 5 |
+
vector<int> path{0};
|
| 6 |
+
dfs(graph, 0, &path, &result);
|
| 7 |
+
return result;
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
private:
|
| 11 |
+
void dfs(const vector<vector<int>>& graph,
|
| 12 |
+
int curr, vector<int> *path,
|
| 13 |
+
vector<vector<int>> *result) {
|
| 14 |
+
if (curr == graph.size() - 1) {
|
| 15 |
+
result->emplace_back(*path);
|
| 16 |
+
return;
|
| 17 |
+
}
|
| 18 |
+
for (const auto& node: graph[curr]) {
|
| 19 |
+
path->emplace_back(node);
|
| 20 |
+
dfs(graph, node, path, result);
|
| 21 |
+
path->pop_back();
|
| 22 |
+
}
|
| 23 |
+
}
|
| 24 |
+
};
|
HiBench/Code/c++Selected/45.cpp
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
vector<TreeNode*> allPossibleFBT(int N) {
|
| 4 |
+
if (!memo_.count(N)) {
|
| 5 |
+
vector<TreeNode*> result;
|
| 6 |
+
if (N == 1) {
|
| 7 |
+
result.emplace_back(new TreeNode(0));
|
| 8 |
+
} else if (N % 2 == 1) {
|
| 9 |
+
for (int i = 0; i < N; ++i) {
|
| 10 |
+
for (const auto& left: allPossibleFBT(i)) {
|
| 11 |
+
for (const auto& right: allPossibleFBT(N - 1 - i)) {
|
| 12 |
+
auto node = new TreeNode(0);
|
| 13 |
+
node->left = left;
|
| 14 |
+
node->right = right;
|
| 15 |
+
result.emplace_back(node);
|
| 16 |
+
}
|
| 17 |
+
}
|
| 18 |
+
}
|
| 19 |
+
}
|
| 20 |
+
memo_[N] = move(result);
|
| 21 |
+
}
|
| 22 |
+
return memo_[N];
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
private:
|
| 26 |
+
unordered_map<int, vector<TreeNode*>> memo_;
|
| 27 |
+
};
|
HiBench/Code/c++Selected/46.cpp
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
int minDistance(vector<int>& houses, int k) {
|
| 4 |
+
sort(begin(houses), end(houses));
|
| 5 |
+
vector<int> prefix(houses.size() + 1);
|
| 6 |
+
for (int i = 0; i < houses.size(); ++i) {
|
| 7 |
+
prefix[i + 1] = prefix[i] + houses[i];
|
| 8 |
+
}
|
| 9 |
+
vector<int> dp(houses.size());
|
| 10 |
+
for (int j = 0; j < houses.size(); ++j) {
|
| 11 |
+
dp[j] = cost(prefix, 0, j);
|
| 12 |
+
}
|
| 13 |
+
for (int m = 1; m < k; ++m) {
|
| 14 |
+
for (int j = houses.size() - 1; j >= 0; --j) {
|
| 15 |
+
for (int i = m; i <= j; ++i) {
|
| 16 |
+
dp[j] = min(dp[j], dp[i - 1] + cost(prefix, i, j));
|
| 17 |
+
}
|
| 18 |
+
}
|
| 19 |
+
}
|
| 20 |
+
return dp.back();
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
private:
|
| 24 |
+
int cost(const vector<int>& prefix, int i, int j) {
|
| 25 |
+
return (prefix[j + 1] - prefix[(i + j + 1) / 2]) -
|
| 26 |
+
(prefix[(i + j) / 2 + 1] - prefix[i]);
|
| 27 |
+
}
|
| 28 |
+
};
|
HiBench/Code/c++Selected/47.cpp
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
string alphabetBoardPath(string target) {
|
| 4 |
+
string result;
|
| 5 |
+
int x = 0, y = 0;
|
| 6 |
+
for (const auto& c : target) {
|
| 7 |
+
int x1 = (c - 'a') % 5, y1 = (c - 'a') / 5;
|
| 8 |
+
result += string(max(y - y1, 0), 'U');
|
| 9 |
+
result += string(max(x - x1, 0), 'L');
|
| 10 |
+
result += string(max(x1 - x, 0), 'R');
|
| 11 |
+
result += string(max(y1 - y, 0), 'D');
|
| 12 |
+
result += "!";
|
| 13 |
+
x = x1, y = y1;
|
| 14 |
+
}
|
| 15 |
+
return result;
|
| 16 |
+
}
|
| 17 |
+
};
|
HiBench/Code/c++Selected/48.cpp
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
vector<int> simulationResult(vector<int>& windows, vector<int>& queries) {
|
| 4 |
+
vector<bool> lookup(size(windows));
|
| 5 |
+
vector<int> result;
|
| 6 |
+
for (int i = size(queries) - 1; i >= 0; --i) {
|
| 7 |
+
if (lookup[queries[i]]) {
|
| 8 |
+
continue;
|
| 9 |
+
}
|
| 10 |
+
lookup[queries[i]] = true;
|
| 11 |
+
result.emplace_back(queries[i]);
|
| 12 |
+
}
|
| 13 |
+
for (const auto& x : windows) {
|
| 14 |
+
if (lookup[x]) {
|
| 15 |
+
continue;
|
| 16 |
+
}
|
| 17 |
+
result.emplace_back(x);
|
| 18 |
+
}
|
| 19 |
+
return result;
|
| 20 |
+
}
|
| 21 |
+
};
|
HiBench/Code/c++Selected/49.cpp
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
int alternateDigitSum(int n) {
|
| 4 |
+
int result = 0, sign = 1;
|
| 5 |
+
for (; n; n /= 10) {
|
| 6 |
+
sign *= -1;
|
| 7 |
+
result += sign * (n % 10);
|
| 8 |
+
}
|
| 9 |
+
return sign * result;
|
| 10 |
+
}
|
| 11 |
+
};
|
HiBench/Code/c++Selected/5.cpp
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
bool judgePoint24(vector<int>& nums) {
|
| 4 |
+
vector<double> doubles;
|
| 5 |
+
std::transform(nums.begin(), nums.end(), std::back_inserter(doubles),
|
| 6 |
+
[](const int num) { return double(num); });
|
| 7 |
+
return dfs(doubles);
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
private:
|
| 11 |
+
bool dfs(const vector<double>& nums) {
|
| 12 |
+
if (nums.size() == 1) {
|
| 13 |
+
return fabs(nums[0] - 24) < 1e-6;
|
| 14 |
+
}
|
| 15 |
+
static unordered_map<char, std::function<double(double, double)>> ops =
|
| 16 |
+
{
|
| 17 |
+
{'+', std::plus<double>()},
|
| 18 |
+
{'-', std::minus<double>()},
|
| 19 |
+
{'*', std::multiplies<double>()},
|
| 20 |
+
{'/', std::divides<double>()},
|
| 21 |
+
};
|
| 22 |
+
for (int i = 0; i < nums.size(); ++i) {
|
| 23 |
+
for (int j = 0; j < nums.size(); ++j) {
|
| 24 |
+
if (i == j) {
|
| 25 |
+
continue;
|
| 26 |
+
}
|
| 27 |
+
vector<double> next_nums;
|
| 28 |
+
for (int k = 0; k < nums.size(); ++k) {
|
| 29 |
+
if (k == i || k == j) {
|
| 30 |
+
continue;
|
| 31 |
+
}
|
| 32 |
+
next_nums.emplace_back(nums[k]);
|
| 33 |
+
}
|
| 34 |
+
for (const auto& op : ops) {
|
| 35 |
+
if (((op.first == '+' || op.first == '*') && i > j) ||
|
| 36 |
+
(op.first == '/' && nums[j] == 0)) {
|
| 37 |
+
continue;
|
| 38 |
+
}
|
| 39 |
+
next_nums.emplace_back(op.second(nums[i], nums[j]));
|
| 40 |
+
if (dfs(next_nums)) {
|
| 41 |
+
return true;
|
| 42 |
+
}
|
| 43 |
+
next_nums.pop_back();
|
| 44 |
+
}
|
| 45 |
+
}
|
| 46 |
+
}
|
| 47 |
+
return false;
|
| 48 |
+
}
|
| 49 |
+
};
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
class Fraction {
|
| 53 |
+
public:
|
| 54 |
+
Fraction() = default;
|
| 55 |
+
Fraction(int n)
|
| 56 |
+
: Fraction(n, 1)
|
| 57 |
+
{
|
| 58 |
+
}
|
| 59 |
+
Fraction(int n, int d)
|
| 60 |
+
: numerator_(n)
|
| 61 |
+
, denominator_(d)
|
| 62 |
+
{
|
| 63 |
+
}
|
| 64 |
+
~Fraction() = default;
|
| 65 |
+
|
| 66 |
+
void set_num(int value) { numerator_ = value; }
|
| 67 |
+
void set_den(int value) { denominator_ = value; }
|
| 68 |
+
int get_num() const { return numerator_; }
|
| 69 |
+
int get_den() const { return denominator_; }
|
| 70 |
+
void reduce();
|
| 71 |
+
int calculate_gcd(int, int) const;
|
| 72 |
+
private:
|
| 73 |
+
int numerator_, denominator_;
|
| 74 |
+
};
|
| 75 |
+
|
| 76 |
+
void Fraction::reduce()
|
| 77 |
+
{
|
| 78 |
+
const auto gcd = calculate_gcd(numerator_, denominator_);
|
| 79 |
+
numerator_ = numerator_ / gcd;
|
| 80 |
+
denominator_ = denominator_ / gcd;
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
int Fraction::calculate_gcd(int a, int b) const
|
| 84 |
+
{
|
| 85 |
+
a = std::abs(a);
|
| 86 |
+
b = std::abs(b);
|
| 87 |
+
while (b != 0) {
|
| 88 |
+
int tmp = b;
|
| 89 |
+
b = a % b;
|
| 90 |
+
a = tmp;
|
| 91 |
+
}
|
| 92 |
+
return a;
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
Fraction operator+(const Fraction& lhs, const Fraction& rhs)
|
| 96 |
+
{
|
| 97 |
+
Fraction result{};
|
| 98 |
+
|
| 99 |
+
result.set_num((lhs.get_num() * rhs.get_den()) + (lhs.get_den() * rhs.get_num()));
|
| 100 |
+
result.set_den(lhs.get_den() * rhs.get_den());
|
| 101 |
+
|
| 102 |
+
result.reduce();
|
| 103 |
+
|
| 104 |
+
return result;
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
Fraction operator-(const Fraction& lhs, const Fraction& rhs)
|
| 108 |
+
{
|
| 109 |
+
Fraction result{};
|
| 110 |
+
|
| 111 |
+
result.set_num((lhs.get_num() * rhs.get_den()) - (lhs.get_den() * rhs.get_num()));
|
| 112 |
+
result.set_den(lhs.get_den() * rhs.get_den());
|
| 113 |
+
|
| 114 |
+
result.reduce();
|
| 115 |
+
|
| 116 |
+
return result;
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
Fraction operator*(const Fraction& lhs, const Fraction& rhs)
|
| 120 |
+
{
|
| 121 |
+
Fraction result{};
|
| 122 |
+
|
| 123 |
+
result.set_num(lhs.get_num() * rhs.get_num());
|
| 124 |
+
result.set_den(lhs.get_den() * rhs.get_den());
|
| 125 |
+
|
| 126 |
+
result.reduce();
|
| 127 |
+
|
| 128 |
+
return result;
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
Fraction operator/(const Fraction& lhs, const Fraction& rhs)
|
| 132 |
+
{
|
| 133 |
+
Fraction result{};
|
| 134 |
+
|
| 135 |
+
result.set_num(lhs.get_num() * rhs.get_den());
|
| 136 |
+
result.set_den(lhs.get_den() * rhs.get_num());
|
| 137 |
+
|
| 138 |
+
result.reduce();
|
| 139 |
+
|
| 140 |
+
return result;
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
bool operator==(const Fraction &lhs, const Fraction &rhs) {
|
| 144 |
+
return (((lhs.get_num() * rhs.get_den()) - (rhs.get_num() * lhs.get_den())) == 0);
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
std::ostream &operator<<(std::ostream &os, const Fraction &value) {
|
| 148 |
+
os << value.get_num() << "/" << value.get_den();
|
| 149 |
+
return os;
|
| 150 |
+
}
|
HiBench/Code/c++Selected/50.cpp
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
int numberOfAlternatingGroups(vector<int>& colors) {
|
| 4 |
+
static const int k = 3;
|
| 5 |
+
|
| 6 |
+
int result = 0;
|
| 7 |
+
for (int right = 0, left = 0, curr = 0; right < size(colors) + k - 1; ++right) {
|
| 8 |
+
if (right - left + 1 == k) {
|
| 9 |
+
result += curr == k - 1 ? 1 : 0;
|
| 10 |
+
curr -= colors[left] != colors[(left + 1) % size(colors)] ? 1 : 0;
|
| 11 |
+
++left;
|
| 12 |
+
}
|
| 13 |
+
curr += colors[right % size(colors)] != colors[(right + 1) % size(colors)] ? 1 : 0;
|
| 14 |
+
}
|
| 15 |
+
return result;
|
| 16 |
+
}
|
| 17 |
+
};
|
HiBench/Code/c++Selected/51.cpp
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Solution {
|
| 2 |
+
public:
|
| 3 |
+
int numberOfAlternatingGroups(vector<int>& colors, int k) {
|
| 4 |
+
int result = 0;
|
| 5 |
+
for (int right = 0, left = 0, curr = 0; right < size(colors) + k - 1; ++right) {
|
| 6 |
+
if (right - left + 1 == k) {
|
| 7 |
+
result += curr == k - 1 ? 1 : 0;
|
| 8 |
+
curr -= colors[left] != colors[(left + 1) % size(colors)] ? 1 : 0;
|
| 9 |
+
++left;
|
| 10 |
+
}
|
| 11 |
+
curr += colors[right % size(colors)] != colors[(right + 1) % size(colors)] ? 1 : 0;
|
| 12 |
+
}
|
| 13 |
+
return result;
|
| 14 |
+
}
|
| 15 |
+
};
|