leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE |

commit985ea2690da12d511963bdec17486d86773e9c81
parent9fe29dc153950a30adae9baca066f11ac2bac0b4
authorDimitrije Dobrota <mail@dimitrijedobrota.com>
dateTue, 19 Sep 2023 17:11:28 +0000

5 Random Problems

Diffstat:
AProblems/1072.cpp|++++++++++++++++
AProblems/1344.cpp|+++++++++++
AProblems/1371.cpp|+++++++++++++++++++++++
AProblems/1734.cpp|++++++++++++++++++
AProblems/2799.cpp|++++++++++++++++++++
MREADME.md|+++++

6 files changed, 93 insertions(+), 0 deletions(-)


diff --git a/Problems/1072.cpp b/Problems/1072.cpp

@@ -0,0 +1,16 @@

class Solution {
typedef bitset<512> bs_t;
public:
int maxEqualRowsAfterFlips(const vector<vector<int>> &matrix) {
int n = matrix.size(), m = matrix[0].size(), res = 0;
unordered_map<bs_t, int> um;
for (int i = 0; i < n; i++) {
bs_t crnt;
for (int j = 0; j < m; j++)
crnt[j] = matrix[i][j] != matrix[i][0];
res = max(res, ++um[crnt]);
}
return res;
}
};

diff --git a/Problems/1344.cpp b/Problems/1344.cpp

@@ -0,0 +1,11 @@

class Solution {
public:
double angleClock(int hour, int minutes) {
double m = (360.0 / 60.0 * minutes);
double h = (360.0 / 12.0 * hour) + (30.0 / 60.0 * minutes);
if (h >= 360) h -= 360;
double res = max(m, h) - min(m, h);
if (res >= 180) res = 360 - res;
return res;
}
};

diff --git a/Problems/1371.cpp b/Problems/1371.cpp

@@ -0,0 +1,23 @@

class Solution {
static const uint8_t map[27];
public:
int findTheLongestSubstring(const string &s) {
static int um[1 << 5];
memset(um, 0xFF, sizeof(um));
int res = 0, crnt = 0;
for (int i = 0; i < s.size(); i++) {
crnt ^= (1 << map[s[i] & 0x1F]) >> 1;
if (!crnt)
res = max(res, i + 1);
else if (um[crnt] == -1)
um[crnt] = i;
else
res = max(res, i - um[crnt]);
}
return res;
}
};
const uint8_t Solution::map[27] = {0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 0,
0, 4, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0};

diff --git a/Problems/1734.cpp b/Problems/1734.cpp

@@ -0,0 +1,18 @@

class Solution {
public:
vector<int> decode(const vector<int> &encoded) {
const int n = encoded.size();
vector<int> res(n + 1);
res[0] = n + 1;
for (int i = 0; i < n; i++) {
if (i & 1) res[0] ^= encoded[i];
res[0] ^= i + 1;
}
for (int i = 1; i <= n; i++)
res[i] = res[i - 1] ^ encoded[i - 1];
return res;
}
};

diff --git a/Problems/2799.cpp b/Problems/2799.cpp

@@ -0,0 +1,20 @@

class Solution {
public:
int countCompleteSubarrays(const vector<int> &nums) {
static int count[10001];
memset(count, 0x00, sizeof(count));
int uniq = 0, res = 0, cnt = 0;
;
for (const int n : nums)
if (!count[n]) count[n] = 1, uniq++;
for (int i = 0, left = 0; i < nums.size(); i++) {
if (count[nums[i]]++ == 1) cnt++;
while (cnt == uniq) {
res += nums.size() - i;
if (--count[nums[left++]] == 1) cnt--;
}
}
return res;
}
};

diff --git a/README.md b/README.md

@@ -484,6 +484,7 @@ for solving problems.

| 1051 | Easy | [Height Checker](Problems/1051.cpp) |
| 1061 | Medium | [Lexicographically Smallest Equivalent String](Problems/1061.cpp) |
| 1071 | Easy | [Greatest Common Divisor of Strings](Problems/1071.cpp) |
| 1072 | Medium | [Flip Columns For Maximum Number of Equal Rows](Problems/1072.cpp) |
| 1079 | Medium | [Letter Tile Possibilities](Problems/1079.cpp) |
| 1089 | Easy | [Duplicate Zeros](Problems/1089.cpp) |
| 1091 | Medium | [Shortest Path in Binary Matrix](Problems/1091.cpp) |

@@ -543,6 +544,7 @@ for solving problems.

| 1339 | Medium | [Maximum Product of Splitted Binary Tree](Problems/1339.cpp) |
| 1342 | Easy | [Number of Steps to Reduce a Number to Zero](Problems/1342.cpp) |
| 1343 | Medium | [Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold](Problems/1343.cpp) |
| 1344 | Medium | [Angle Between Hands of a Clock](Problems/1344.cpp) |
| 1345 | Hard | [Jump Game IV](Problems/1345.cpp) |
| 1346 | Easy | [Check if N and Its Double Exist](Problems/1346.cpp) |
| 1347 | Medium | [Minimum Number of Steps to Make Two Strings Anagram](Problems/1347.cpp) |

@@ -552,6 +554,7 @@ for solving problems.

| 1359 | Hard | [Count All Valid Pickup and Delivery Options](Problems/1359.cpp) |
| 1361 | Medium | [Validate Binary Tree Nodes](Problems/1361.cpp) |
| 1367 | Medium | [Linked List in Binary Tree ](Problems/1367.cpp) |
| 1371 | Medium | [Find the Longest Substring Containing Vowels in Even Counts](Problems/1371.cpp) |
| 1372 | Medium | [Longest ZigZag Path in a Binary Tree](Problems/1372.cpp) |
| 1373 | Hard | [Maximum Sum BST in Binary Tree](Problems/1373.cpp) |
| 1375 | Medium | [Number of Times Binary String Is Prefix-Aligned](Problems/1375.cpp) |

@@ -642,6 +645,7 @@ for solving problems.

| 1721 | Medium | [Swapping Nodes in a Linked List](Problems/1721.cpp) |
| 1722 | Medium | [Minimize Hamming Distance After Swap Operations](Problems/1722.cpp) |
| 1732 | Easy | [Find the Highest Altitude](Problems/1732.cpp) |
| 1734 | Medium | [Decode XORed Permutation](Problems/1734.cpp) |
| 1743 | Medium | [Restore the Array From Adjacent Pairs](Problems/1743.cpp) |
| 1751 | Hard | [Maximum Number of Events That Can Be Attended II](Problems/1751.cpp) |
| 1753 | Medium | [Maximum Score From Removing Stones](Problems/1753.cpp) |

@@ -801,4 +805,5 @@ for solving problems.

| 2711 | Medium | [Difference of Number of Distinct Values on Diagonals](Problems/2711.cpp) |
| 2740 | Medium | [Find the Value of the Partition](Problems/2740.cpp) |
| 2785 | Medium | [Sort Vowels in a String](Problems/2785.cpp) |
| 2799 | Medium | [Count Complete Subarrays in an Array](Problems/2799.cpp) |
| 2807 | Medium | [Insert Greatest Common Divisors in Linked List](Problems/2807.cpp) |