Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1a4aa1f

Browse files
committed
add problems
1 parent e24eab6 commit 1a4aa1f

File tree

3 files changed

+379
-0
lines changed

3 files changed

+379
-0
lines changed

‎july/11.cpp

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
typedef long long int ll;
6+
typedef pair<int, int> ii;
7+
typedef vector<int> vi;
8+
typedef vector<string> vs;
9+
10+
#define len(container) int((container).size())
11+
#define all(c) (c).begin(), (c).end()
12+
13+
const int M = 1e9 + 7;
14+
15+
template <typename ITER>
16+
void show(ITER begin, ITER end) {
17+
for (int i = 1; begin != end; i++) {
18+
printf("%d ", *(begin++));
19+
if (i % 20 == 0 or begin == end) printf("\n");
20+
}
21+
};
22+
23+
template <typename T>
24+
void addMod(T& a, T b) {
25+
a = (a + b) % M;
26+
}
27+
28+
inline bool isValid(int x, int y, int R, int C) {
29+
return x >= 0 && x < R && y >= 0 && y < C;
30+
}
31+
32+
struct TreeNode {
33+
int val;
34+
TreeNode* left;
35+
TreeNode* right;
36+
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
37+
};
38+
39+
class Solution {
40+
public:
41+
double myPow(double x, int n) {
42+
if (n < 0) {
43+
if (n == INT_MIN) return abs(x) == 1.0 ? 1 : 0;
44+
return 1 / myPow(x, -n);
45+
}
46+
if (n == 0) return 1;
47+
if (n == 1) {
48+
return x;
49+
}
50+
if (x == 1.0) return 1;
51+
if (n % 2) {
52+
auto c = myPow(x, (n - 1) / 2);
53+
return x * c * c;
54+
}
55+
double c = myPow(x, n / 2);
56+
return c * c;
57+
/*
58+
if (n == std::numeric_limits<int>::lowest()) {
59+
return myPow(1 / x, -(n + 1)) / x;
60+
}
61+
if (n < 0) {
62+
return myPow(1 / x, -n);
63+
}
64+
double ans = 1;
65+
while (n) {
66+
if (n & 1 == 1) ans *= x;
67+
x *= x;
68+
n >>= 1;
69+
}
70+
return ans;*/
71+
}
72+
73+
// https://leetcode.com/problems/is-graph-bipartite/
74+
bool colorHelper(vector<vector<int>> g, vector<int>& colors, int v,
75+
int color) {
76+
if (colors[v] != -1 && colors[v] != color) return false;
77+
colors[v] = color;
78+
for (auto n : g[v]) {
79+
if (!colorHelper(g, colors, n, 1 - color)) return false;
80+
}
81+
82+
return true;
83+
}
84+
85+
bool isBipartite(vector<vector<int>>& graph) {
86+
const int N = graph.size();
87+
vector<int> colors(N, -1);
88+
for (int i = 0; i < N; ++i) {
89+
if (colors[i] == -1) {
90+
if (!colorHelper(graph, colors, i, 0)) return false;
91+
}
92+
}
93+
return true;
94+
}
95+
96+
// https://leetcode.com/problems/custom-sort-string/
97+
string customSortString(string S, string T) {
98+
int count[26] = {0};
99+
int i = 0;
100+
for (auto& c : S) {
101+
count[c - 'a'] = i++;
102+
}
103+
104+
auto cmp = [&count](const char& a, const char& b) {
105+
return count[a - 'a'] < count[b - 'a'];
106+
};
107+
std::sort(std::begin(T), std::end(T), cmp);
108+
return T;
109+
}
110+
111+
// https://leetcode.com/problems/task-scheduler/
112+
int leastInterval(vector<char>& tasks, int n) {
113+
int ans = 0;
114+
const int T = tasks.size();
115+
if (n == 0) {
116+
return T;
117+
}
118+
119+
int count[26] = {0};
120+
for (auto& c : tasks) {
121+
count[c - 'A']++;
122+
}
123+
124+
vector<ii> sch;
125+
for (int i = 0; i < 26; ++i) {
126+
if (count[i]) sch.emplace_back(count[i], i);
127+
}
128+
129+
std::sort(std::begin(sch), std::end(sch), std::greater<ii>());
130+
int lastSize = 0;
131+
while (!sch.empty()) {
132+
lastSize = sch.size();
133+
for (auto& s : sch) {
134+
s.second--;
135+
}
136+
ans += max(n, lastSize);
137+
for (int i = lastSize - 1; i >= 0; --i) {
138+
if (sch[i].second == 0) {
139+
sch.pop_back();
140+
} else
141+
break;
142+
}
143+
}
144+
return ans - lastSize;
145+
}
146+
// very hard greedy problem
147+
// int ans = (count-1)*(n+1);
148+
// for(auto e : mp) if(e.second == count) ans++;
149+
// return max((int)tasks.size(), ans);
150+
151+
// https://leetcode.com/problems/kth-smallest-element-in-a-bst/
152+
int kthSmallest(TreeNode* root, int k) {
153+
if (root == nullptr) return 0;
154+
155+
int v = getTreeSize(root->left);
156+
if (v >= k) {
157+
return kthSmallest(root->left, k);
158+
}
159+
if (v + 1 == k) {
160+
return root->val;
161+
}
162+
163+
return kthSmallest(root->right, k - v - 1);
164+
}
165+
// Other solutions:
166+
// - using stack for inorder traversal
167+
// - using recursion for inorder travesal
168+
// - using binary search like above.
169+
// Follow up:
170+
// - If tree has frequent insert/delete, then we could cache the getTreeSize
171+
172+
int getTreeSize(TreeNode* r) {
173+
if (r == NULL) {
174+
return 0;
175+
}
176+
return 1 + getTreeSize(r->left) + getTreeSize(r->right);
177+
}
178+
179+
// https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/
180+
string minRemoveToMakeValid(string s) {
181+
string ans;
182+
return ans;
183+
}
184+
};
185+
186+
int main(int argc, char* argv[]) {
187+
// Solution s = Solution();
188+
return 0;
189+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* @lc app=leetcode id=329 lang=cpp
3+
*
4+
* [329] Longest Increasing Path in a Matrix
5+
*
6+
* https://leetcode.com/problems/longest-increasing-path-in-a-matrix/description/
7+
*
8+
* algorithms
9+
* Hard (42.25%)
10+
* Likes: 1909
11+
* Dislikes: 36
12+
* Total Accepted: 146.4K
13+
* Total Submissions: 339.1K
14+
* Testcase Example: '[[9,9,4],[6,6,8],[2,1,1]]'
15+
*
16+
* Given an integer matrix, find the length of the longest increasing path.
17+
*
18+
* From each cell, you can either move to four directions: left, right, up or
19+
* down. You may NOT move diagonally or move outside of the boundary (i.e.
20+
* wrap-around is not allowed).
21+
*
22+
* Example 1:
23+
*
24+
*
25+
* Input: nums =
26+
* [
27+
* ⁠ [9,9,4],
28+
* ⁠ [6,6,8],
29+
* ⁠ [2,1,1]
30+
* ]
31+
* Output: 4
32+
* Explanation: The longest increasing path is [1, 2, 6, 9].
33+
*
34+
*
35+
* Example 2:
36+
*
37+
*
38+
* Input: nums =
39+
* [
40+
* ⁠ [3,4,5],
41+
* ⁠ [3,2,6],
42+
* ⁠ [2,2,1]
43+
* ]
44+
* Output: 4
45+
* Explanation: The longest increasing path is [3, 4, 5, 6]. Moving diagonally
46+
* is not allowed.
47+
*
48+
*
49+
*/
50+
51+
#include <vector>
52+
using namespace std;
53+
// @lc code=start
54+
class Solution {
55+
public:
56+
int dirs[5] = {1, 0, -1, 0, 1};
57+
int R, C;
58+
int dfs(vector<vector<int>>& scores, vector<vector<int>>& matrix, int a,
59+
int b) {
60+
if (scores[a][b] != -1) {
61+
return scores[a][b];
62+
}
63+
int score = -1;
64+
for (int i = 0; i < 4; ++i) {
65+
int x = a + dirs[i];
66+
int y = b + dirs[i + 1];
67+
if (x >= 0 && x < R && y >= 0 && y < C) {
68+
if (matrix[x][y] > matrix[a][b]) {
69+
score = max(score, 1 + dfs(scores, matrix, x, y));
70+
}
71+
}
72+
}
73+
scores[a][b] = score;
74+
return scores[a][b];
75+
}
76+
77+
int longestIncreasingPath(vector<vector<int>>& matrix) {
78+
R = matrix.size();
79+
if (R == 0) {
80+
return 0;
81+
}
82+
C = matrix[0].size();
83+
int ans = 0;
84+
vector<vector<int>> scores(R, vector<int>(C, -1));
85+
for (int i = 0; i < R; ++i) {
86+
for (int j = 0; j < C; ++j) {
87+
ans = max(ans, dfs(scores, matrix, i, j));
88+
}
89+
}
90+
91+
return ans;
92+
}
93+
};
94+
// @lc code=end
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* @lc app=leetcode id=714 lang=cpp
3+
*
4+
* [714] Best Time to Buy and Sell Stock with Transaction Fee
5+
*
6+
* https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/description/
7+
*
8+
* algorithms
9+
* Medium (53.15%)
10+
* Likes: 1516
11+
* Dislikes: 46
12+
* Total Accepted: 67.9K
13+
* Total Submissions: 125.1K
14+
* Testcase Example: '[1,3,2,8,4,9]\n2'
15+
*
16+
* Your are given an array of integers prices, for which the i-th element is
17+
* the price of a given stock on day i; and a non-negative integer fee
18+
* representing a transaction fee.
19+
* You may complete as many transactions as you like, but you need to pay the
20+
* transaction fee for each transaction. You may not buy more than 1 share of
21+
* a stock at a time (ie. you must sell the stock share before you buy again.)
22+
* Return the maximum profit you can make.
23+
*
24+
* Example 1:
25+
*
26+
* Input: prices = [1, 3, 2, 8, 4, 9], fee = 2
27+
* Output: 8
28+
* Explanation: The maximum profit can be achieved by:
29+
* Buying at prices[0] = 1Selling at prices[3] = 8Buying at prices[4] =
30+
* 4Selling at prices[5] = 9The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) =
31+
* 8.
32+
*
33+
*
34+
*
35+
* Note:
36+
* 0 < prices.length .
37+
* 0 < prices[i] < 50000.
38+
* 0 .
39+
*
40+
*/
41+
#include <bits/stdc++.h>
42+
43+
using namespace std;
44+
45+
typedef vector<int> vi;
46+
typedef pair<int, int> ii;
47+
typedef long long int ll;
48+
49+
#define len(container) int((container).size())
50+
#define all(c) (c).begin(), (c).end()
51+
52+
template <int group = 20, typename ITER>
53+
void show(const char* note, ITER begin, ITER end) {
54+
cout << note;
55+
for (int i = 1; begin != end; i++) {
56+
std::cout << *(begin++) << ' ';
57+
if (i % group == 0 or begin == end) cout << endl;
58+
}
59+
};
60+
61+
struct TreeNode {
62+
int val;
63+
TreeNode* left;
64+
TreeNode* right;
65+
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
66+
};
67+
68+
// @lc code=start
69+
class Solution {
70+
public:
71+
int maxProfit(vector<int>& prices, int fee) {
72+
const int N = prices.size();
73+
int k = N / 2;
74+
vector<vi> dp(k + 1, vector<int>(N, 0));
75+
int res = 0;
76+
77+
// sell at position i
78+
for (int i = 1; i <= k; i++) {
79+
for (int j = 1; j < N; j++) {
80+
dp[i][j] = dp[i - 1][j];
81+
int msf = 0;
82+
for (int l = 0; l < j; ++l) {
83+
if (l) msf = max(msf, dp[i - 1][l - 1]);
84+
if (prices[l] <= prices[j]) {
85+
dp[i][j] = max(dp[i][j], msf + prices[j] - prices[l] - fee);
86+
}
87+
}
88+
res = max(res, dp[i][j]);
89+
}
90+
}
91+
92+
return res;
93+
}
94+
};
95+
// @lc code=end
96+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /