Compare commits
10 Commits
f428f19329
...
18ecf286e4
| Author | SHA1 | Date |
|---|---|---|
|
|
18ecf286e4 | |
|
|
bb83b31326 | |
|
|
09ed316e00 | |
|
|
c2eb3ea45f | |
|
|
0b7cdf0450 | |
|
|
b2c871ce9a | |
|
|
807dc8da34 | |
|
|
eb1efa5f37 | |
|
|
5a3a8d32a4 | |
|
|
da6a7b2dac |
|
|
@ -0,0 +1,39 @@
|
|||
#include <unordered_set>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
/**
|
||||
* 523. Continuous Subarray Sum
|
||||
*
|
||||
* Given an integer array nums and an integer k, return true if nums has a continuous subarray of size at least two whose elements sum up to a multiple of k, or false otherwise.
|
||||
* An integer x is a multiple of k if there exists an integer n such that x = n * k. 0 is always a multiple of k.
|
||||
*/
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
static bool checkSubarraySum(const std::vector<int>& nums, int k) {
|
||||
int sum = nums[0] % k, n = nums.size(), prev = 0;
|
||||
if (n < 2)
|
||||
return false;
|
||||
if (k == 1)
|
||||
return true;
|
||||
if (n == 2)
|
||||
return (nums[0] + nums[1]) % k == 0;
|
||||
std::unordered_set<int> s{0, nums[0] % k};
|
||||
for (int i = 1; i < n; ++i) {
|
||||
if (!(nums[i] % k) && !(nums[i - 1] % k))
|
||||
return true;
|
||||
prev = sum;
|
||||
sum = (sum + nums[i]) % k;
|
||||
if (s.count(sum) && prev != sum)
|
||||
return true;
|
||||
s.insert(sum);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
std::cout << Solution::checkSubarraySum({1,2,12}, 6);
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
#include <vector>
|
||||
|
||||
/**
|
||||
* 1822. Sign of the Product of an Array
|
||||
*
|
||||
* There is a function signFunc(x) that returns:
|
||||
* - 1 if x is positive.
|
||||
* - -1 if x is negative.
|
||||
* - 0 if x is equal to 0.
|
||||
* You are given an integer array nums. Let product be the product of all values in the array nums.
|
||||
* Return signFunc(product).
|
||||
*/
|
||||
|
||||
class Solution {
|
||||
private:
|
||||
static constexpr int sgn(int n) {
|
||||
return n ? ((n >> 31) | 1) : 0;
|
||||
}
|
||||
|
||||
public:
|
||||
static int arraySign(const std::vector<int>& nums) {
|
||||
const int MASK = 0xFFFFFFFE, n = nums.size();
|
||||
int ans = sgn(nums[0]);
|
||||
if (!ans)
|
||||
return 0;
|
||||
for (int i = 1; i < n; ++i) {
|
||||
switch (sgn(nums[i])) {
|
||||
case 0:
|
||||
return 0;
|
||||
case -1:
|
||||
ans ^= MASK;
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
};
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
#include <vector>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
/**
|
||||
* 49. Group Anagrams
|
||||
*
|
||||
* Given an array of strings strs, group the anagrams together. You can return the answer in any order.
|
||||
* An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
|
||||
*/
|
||||
|
||||
class Solution {
|
||||
private:
|
||||
static inline long long h(const std::string& s) {
|
||||
long long ret = 0;
|
||||
int pos;
|
||||
for (char c : s) {
|
||||
pos = (c - 'a') << 1;
|
||||
ret = (ret & ~(3LL << pos)) | (((ret >> pos & 3) + 1) & 3) << pos;
|
||||
}
|
||||
return ret | s.length() << 52;
|
||||
}
|
||||
|
||||
public:
|
||||
static std::vector<std::vector<std::string>> groupAnagrams(const std::vector<std::string>& strs) {
|
||||
std::unordered_map<long long, std::vector<std::string>*> m;
|
||||
for (const std::string& s : strs) {
|
||||
auto hash = h(s);
|
||||
if (!m.count(hash))
|
||||
m[hash] = new std::vector<std::string>;
|
||||
m[hash]->push_back(s);
|
||||
}
|
||||
std::vector<std::vector<std::string>> ret;
|
||||
for (const auto& [hash, ptr] : m)
|
||||
ret.push_back(std::move(*ptr));
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
#include <vector>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
|
||||
/**
|
||||
* 1773. Count Items Matching a Rule
|
||||
*
|
||||
* You are given an array items, where each items[i] = [typei, colori, namei] describes the type, color, and name of the ith item. You are also given a rule represented by two strings, ruleKey and ruleValue.
|
||||
* The ith item is said to match the rule if one of the following is true:
|
||||
* - ruleKey == "type" and ruleValue == typei.
|
||||
* - ruleKey == "color" and ruleValue == colori.
|
||||
* - ruleKey == "name" and ruleValue == namei.
|
||||
* Return the number of items that match the given rule.
|
||||
*/
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
static int countMatches(const std::vector<std::vector<std::string>>& i, const std::string& k, const std::string& v) {
|
||||
return std::count_if(i.begin(), i.end(), [&](auto&& x) { return x[(4 - k[0]) & 3] == v; });
|
||||
}
|
||||
};
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
#include <vector>
|
||||
#include <set>
|
||||
#include <utility>
|
||||
#include <algorithm>
|
||||
|
||||
/**
|
||||
* 2136. Earliest Possible Day of Full Bloom
|
||||
*
|
||||
* You have n flower seeds. Every seed must be planted first before it can begin to grow, then bloom. Planting a seed takes time and so does the growth of a seed. You are given two 0-indexed integer arrays plantTime and growTime, of length n each:
|
||||
* plantTime[i] is the number of full days it takes you to plant the ith seed. Every day, you can work on planting exactly one seed. You do not have to work on planting the same seed on consecutive days, but the planting of a seed is not complete until you have worked plantTime[i] days on planting it in total.
|
||||
* - growTime[i] is the number of full days it takes the ith seed to grow after being completely planted. After the last day of its growth, the flower blooms and stays bloomed forever.
|
||||
* - From the beginning of day 0, you can plant the seeds in any order.
|
||||
* Return the earliest possible day where all seeds are blooming.
|
||||
*/
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
static int earliestFullBloom(const std::vector<int>& plantTime, const std::vector<int>& growTime) {
|
||||
std::multiset<std::pair<int, int>, std::greater<>> s;
|
||||
for (int i = 0; i < plantTime.size(); ++i)
|
||||
s.insert({growTime[i], plantTime[i]});
|
||||
int ans = 0, cur = 0;
|
||||
for (const auto& [gt, pt] : s)
|
||||
ans = std::max(ans, gt + (cur += pt));
|
||||
return ans;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
Solution::earliestFullBloom({1,2,3,2},{2,1,2,1});
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
#include <vector>
|
||||
#include <functional>
|
||||
#include <cctype>
|
||||
|
||||
/**
|
||||
* 784. Letter Case Permutation
|
||||
*
|
||||
* Given a string s, you can transform every letter individually to be lowercase or uppercase to create another string.
|
||||
* Return a list of all possible strings we could create. Return the output in any order.
|
||||
*/
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
std::vector<std::string> letterCasePermutation(std::string& s) {
|
||||
std::vector<std::string> ret;
|
||||
std::function<void(int)> f = [&](int pos) {
|
||||
if (pos >= s.length()) {
|
||||
ret.push_back(s);
|
||||
} else if (std::isalpha(s[pos])) {
|
||||
s[pos] = std::tolower(s[pos]);
|
||||
f(1 + pos);
|
||||
s[pos] = std::toupper(s[pos]);
|
||||
f(1 + pos);
|
||||
} else {
|
||||
f(1 + pos);
|
||||
}
|
||||
};
|
||||
f(0);
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
#include <vector>
|
||||
#include <queue>
|
||||
#include <tuple>
|
||||
#include <iostream>
|
||||
|
||||
/**
|
||||
* 1293. Shortest Path in a Grid with Obstacles Elimination
|
||||
*
|
||||
* You are given an m x n integer matrix grid where each cell is either 0 (empty) or 1 (obstacle). You can move up, down, left, or right from and to an empty cell in one step.
|
||||
* Return the minimum number of steps to walk from the upper left corner (0, 0) to the lower right corner (m - 1, n - 1) given that you can eliminate at most k obstacles. If it is not possible to find such walk return -1.
|
||||
*/
|
||||
|
||||
class Solution {
|
||||
private:
|
||||
static constexpr inline bool OK(int x, int y, int m, int n) {
|
||||
return x >= 0 && y >= 0 && x < m && y < n;
|
||||
}
|
||||
|
||||
static const inline int dX[] = {0, 1, 0, -1};
|
||||
static const inline int dY[] = {1, 0, -1, 0};
|
||||
|
||||
public:
|
||||
static int shortestPath(const std::vector<std::vector<int>>& G, const int k) {
|
||||
const int m = G.size(), n = G.front().size(), p = m * n;
|
||||
if (m == 1 && n == 1)
|
||||
return 0;
|
||||
|
||||
auto dp = new int[40][40][1600]{};
|
||||
std::queue<std::tuple<uint8_t, uint8_t, short, int>> q;
|
||||
q.push({m - 1, n - 1, 0, 0});
|
||||
dp[m - 1][n - 1][0] = -1;
|
||||
while (!q.empty()) {
|
||||
const auto [x, y, d, l] = q.front();
|
||||
q.pop();
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
int nx = x + dX[i], ny = y + dY[i];
|
||||
if (!OK(nx, ny, m, n) || dp[nx][ny][d + G[nx][ny]] || d + G[nx][ny] > k)
|
||||
continue;
|
||||
q.push({nx, ny, d + G[nx][ny], dp[nx][ny][d + G[nx][ny]] = 1 + l});
|
||||
}
|
||||
}
|
||||
int ans = 0x7FFFFFFF;
|
||||
for (int i = 0; i <= k; ++i)
|
||||
if (dp[0][0][i])
|
||||
ans = std::min(ans, dp[0][0][i]);
|
||||
delete[] dp;
|
||||
return ans == 0x7FFFFFFF ? -1 : ans;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
std::cout << Solution::shortestPath({{0,0,0},{1,1,0},{0,0,0},{0,1,1},{0,0,0}}, 1);
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
#include <bitset>
|
||||
#include <iostream>
|
||||
|
||||
/**
|
||||
* 481. Magical String
|
||||
*
|
||||
* A magical string s consists of only '1' and '2' and obeys the following rules:
|
||||
* - The string s is magical because concatenating the number of contiguous occurrences of characters '1' and '2' generates the string s itself.
|
||||
* The first few elements of s is s = "1221121221221121122……". If we group the consecutive 1's and 2's in s, it will be "1 22 11 2 1 22 1 22 11 2 11 22 ......" and the occurrences of 1's or 2's in each group are "1 2 2 1 1 2 1 2 2 1 2 2 ......". You can see that the occurrence sequence is s itself.
|
||||
* Given an integer n, return the number of 1's in the first n number in the magical string s.
|
||||
*/
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
static constexpr int magicalString(int n) {
|
||||
if (n <= 3) return 1;
|
||||
std::bitset<100000> s;
|
||||
int i = 2, c[2] = {1, 2};
|
||||
s[1] = s[2] = true;
|
||||
for (; c[0] + c[1] < n; ++i) {
|
||||
s[c[i & 1]++ + c[i & 1 ^ 1]] = i & 1;
|
||||
if (s[i])
|
||||
s[c[i & 1]++ + c[i & 1 ^ 1]] = i & 1;
|
||||
}
|
||||
return c[0] + (!(i & 1) ? 0 : (n - c[0] - c[1]));
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
std::cout << Solution::magicalString(4) << "\n";
|
||||
std::cout << Solution::magicalString(5) << "\n";
|
||||
std::cout << Solution::magicalString(6) << "\n";
|
||||
std::cout << Solution::magicalString(7) << "\n";
|
||||
std::cout << Solution::magicalString(8) << "\n";
|
||||
std::cout << Solution::magicalString(9) << "\n";
|
||||
std::cout << Solution::magicalString(10) << "\n";
|
||||
std::cout << Solution::magicalString(11) << "\n";
|
||||
std::cout << Solution::magicalString(12) << "\n";
|
||||
std::cout << Solution::magicalString(13) << "\n";
|
||||
std::cout << Solution::magicalString(14) << "\n";
|
||||
std::cout << Solution::magicalString(15) << "\n";
|
||||
std::cout << Solution::magicalString(16) << "\n";
|
||||
std::cout << Solution::magicalString(17) << "\n";
|
||||
std::cout << Solution::magicalString(18) << "\n";
|
||||
std::cout << Solution::magicalString(19) << "\n";
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
/**
|
||||
* 766. Toeplitz Matrix
|
||||
*
|
||||
* Given an m x n matrix, return true if the matrix is Toeplitz. Otherwise, return false.
|
||||
* A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same elements.
|
||||
*/
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
bool isToeplitzMatrix(const std::vector<std::vector<int>>& m) {
|
||||
for (int i = 1; i < m.size(); ++i)
|
||||
if (!std::equal(m[i].begin() + 1, m[i].end(), m[i - 1].begin()))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
|
@ -3,4 +3,4 @@ PROJECT(2210)
|
|||
|
||||
SET(CMAKE_CXX_STANDARD 23)
|
||||
|
||||
ADD_EXECUTABLE(2210 221026-CN.cpp)
|
||||
ADD_EXECUTABLE(2210 221031-CN.cpp)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,12 @@
|
|||
#include <string>
|
||||
#include <algorithm>
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
bool canConstruct(const std::string& r, const std::string& m) {
|
||||
for (char c = 'a'; c <= 'z'; ++c)
|
||||
if (std::count(r.begin(), r.end(), c) > std::count(m.begin(), m.end(), c))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
Loading…
Reference in New Issue