Skip to content
This repository was archived by the owner on May 16, 2021. It is now read-only.

Commit 275ba44

Browse files
committed
add 6 problems
1 parent 5652f77 commit 275ba44

6 files changed

+376
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
#include <queue>
11+
#include <utility>
12+
using std::make_pair;
13+
using std::queue;
14+
using std::pair;
15+
16+
class Solution {
17+
public:
18+
vector<double> averageOfLevels(TreeNode* root) {
19+
queue<pair<int, TreeNode *>> q;
20+
vector<double> res;
21+
vector<int> c;
22+
23+
if (root == NULL) {
24+
return res;
25+
}
26+
27+
pair<int, TreeNode *> p;
28+
int dep;
29+
TreeNode *node;
30+
31+
q.push(make_pair(0, root));
32+
while (!q.empty()) {
33+
p = q.front();
34+
q.pop();
35+
36+
dep = p.first;
37+
node = p.second;
38+
while (res.size() <= dep) {
39+
res.push_back(0);
40+
c.push_back(0);
41+
}
42+
res[dep] += node->val;
43+
c[dep] += 1;
44+
45+
if (node->left != NULL) {
46+
q.push(make_pair(dep + 1, node->left));
47+
}
48+
if (node->right != NULL) {
49+
q.push(make_pair(dep + 1, node->right));
50+
}
51+
}
52+
53+
int n = res.size();
54+
int i;
55+
for (i = 0; i < n; ++i) {
56+
res[i] /= c[i];
57+
}
58+
c.clear();
59+
60+
return res;
61+
}
62+
};

decode-ways-ii_1_AC.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Too ugly
2+
#include <cstdint>
3+
#include <string>
4+
#include <vector>
5+
using std::string;
6+
using std::vector;
7+
8+
class Solution {
9+
public:
10+
int numDecodings(string s) {
11+
int ls = s.size();
12+
if (ls == 0) {
13+
return 0;
14+
}
15+
16+
vector<int64_t> v1(11, 0), v2(11, 0), v3(11, 0);
17+
v1[10] = 1;
18+
19+
int i;
20+
char c1_start, c1_end, c1;
21+
if (s[0] == '*') {
22+
c1_start = '1';
23+
c1_end = '9';
24+
} else {
25+
c1_start = c1_end = s[0];
26+
}
27+
for (c1 = c1_start; c1 > '0' && c1 <= c1_end; ++c1) {
28+
v2[c1 - '0'] = 1 % MOD;
29+
v2[10] = (v2[10] + 1) % MOD;
30+
}
31+
if (ls == 1) {
32+
return v2[10];
33+
}
34+
35+
int j;
36+
char c2_start, c2_end, c2;
37+
for (i = 1; i < ls; ++i) {
38+
c1 = s[i];
39+
if (c1 == '*') {
40+
c1_start = '1';
41+
c1_end = '9';
42+
} else {
43+
c1_start = c1_end = c1;
44+
}
45+
46+
c2 = (i > 0 ? s[i - 1] : '0');
47+
if (c2 == '*') {
48+
c2_start = '1';
49+
c2_end = '9';
50+
} else {
51+
c2_start = c2_end = c2;
52+
}
53+
54+
for (j = 0; j <= 10; ++j) {
55+
v3[j] = 0;
56+
}
57+
for (c1 = c1_start; c1 <= c1_end; ++c1) {
58+
for (c2 = c2_start; c2 <= c2_end; ++c2) {
59+
calc(v1, v2, v3, c1 - '0', c2 - '0');
60+
}
61+
}
62+
v1 = v2;
63+
v2 = v3;
64+
}
65+
66+
int64_t res = v3[10];
67+
return res;
68+
}
69+
private:
70+
static const int64_t MOD = 1000000007;
71+
static const int64_t LOW = 1;
72+
static const int64_t HIGH = 26;
73+
74+
void calc(vector<int64_t> &v1, vector<int64_t> &v2, vector<int64_t> &v3, int d1, int d2) {
75+
int d = d1;
76+
if (d >= LOW && d <= HIGH) {
77+
v3[d1] = (v3[d1] + v2[d2]) % MOD;
78+
v3[10] = (v3[10] + v2[d2]) % MOD;
79+
}
80+
81+
if (d2 != 0) {
82+
d = d2 * 10 + d1;
83+
if (d >= LOW && d <= HIGH) {
84+
v3[d1] = (v3[d1] + v1[10]) % MOD;
85+
v3[10] = (v3[10] + v1[10]) % MOD;
86+
}
87+
}
88+
}
89+
};

largest-palindrome-product_1_AC.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
// Calculate palindrome product for 2 n-digit numbers.
3+
#include <cmath>
4+
#include <cstdint>
5+
#include <iostream>
6+
using std::cin;
7+
using std::cout;
8+
using std::endl;
9+
using std::pow;
10+
11+
int64_t getPal(int64_t n)
12+
{
13+
const int R = 10;
14+
15+
if (n <= 0) {
16+
return 0;
17+
}
18+
int64_t n0 = n;
19+
int64_t n1 = 0;
20+
while (n > 0) {
21+
n1 = n1 * R + (n % R);
22+
n0 *= R;
23+
n /= R;
24+
}
25+
return n0 + n1;
26+
}
27+
28+
int64_t solve(int n)
29+
{
30+
if (n == 1) {
31+
return 9;
32+
}
33+
int64_t low, high;
34+
int64_t num;
35+
int64_t pal;
36+
int64_t i;
37+
38+
low = (int64_t)pow(10, n - 1);
39+
high = (int64_t)pow(10, n) - 1;
40+
for (num = high; num >= low; --num) {
41+
pal = getPal(num);
42+
for (i = high; pal / i <= high && i >= low; --i) {
43+
if (pal % i == 0) {
44+
// cout << "(" << i << ", " << pal / i << ")";
45+
return pal;
46+
}
47+
}
48+
}
49+
return 0;
50+
}
51+
52+
int main()
53+
{
54+
int n;
55+
int64_t pal;
56+
57+
for (n = 1; n <= 8; ++n) {
58+
pal = solve(n);
59+
cout << pal << ", ";
60+
}
61+
cout << endl;
62+
63+
return 0;
64+
}
65+
*/
66+
#include <cstdint>
67+
68+
int64_t a[] = {9, 9009, 906609, 99000099, 9966006699, 999000000999, 99956644665999, 9999000000009999};
69+
70+
class Solution {
71+
public:
72+
int largestPalindrome(int n) {
73+
return a[n - 1] % 1337;
74+
}
75+
};

shopping-offers_1_AC.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <vector>
2+
using std::vector;
3+
4+
class Solution {
5+
public:
6+
int shoppingOffers(vector<int>& price, vector<vector<int>>& special, vector<int>& needs) {
7+
int n = price.size();
8+
int m = 1;
9+
int i;
10+
for (i = 0; i < n; ++i) {
11+
m *= (needs[i] + 1);
12+
}
13+
14+
vector<int> dp(m, -1);
15+
dp[0] = 0;
16+
17+
vector<int> normal(n, 0);
18+
vector<int> v(n);
19+
for (i = 0; i < n; ++i) {
20+
normal[i] = 1;
21+
knapsack(0, v, normal, needs, dp, price[i]);
22+
normal[i] = 0;
23+
}
24+
25+
int ns = special.size();
26+
for (i = 0; i < ns; ++i) {
27+
knapsack(0, v, special[i], needs, dp, special[i][n]);
28+
}
29+
30+
int res = dp[m - 1];
31+
dp.clear();
32+
v.clear();
33+
34+
return res;
35+
}
36+
private:
37+
void knapsack(int cnt, vector<int> &v, const vector<int> &offer, const vector<int> &needs, vector<int> &dp, int pr) {
38+
if (cnt == needs.size()) {
39+
int idx = vtoi(v, needs);
40+
int idx1 = idx - vtoi(offer, needs);
41+
42+
if (dp[idx1] != -1 && (dp[idx] == -1 || dp[idx] > dp[idx1] + pr)) {
43+
dp[idx] = dp[idx1] + pr;
44+
}
45+
return;
46+
}
47+
48+
int i;
49+
for (i = offer[cnt]; i <= needs[cnt]; ++i) {
50+
v[cnt] = i;
51+
knapsack(cnt + 1, v, offer, needs, dp, pr);
52+
v[cnt] = 0;
53+
}
54+
}
55+
56+
int vtoi(const vector<int> &v, const vector<int> &needs) {
57+
int n = needs.size();
58+
int i;
59+
int idx = 0;
60+
for (i = 0; i < n; ++i) {
61+
idx = idx * (needs[i] + 1) + v[i];
62+
}
63+
return idx;
64+
}
65+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <vector>
2+
using std::vector;
3+
4+
class Solution {
5+
public:
6+
int singleNonDuplicate(vector<int>& nums) {
7+
auto &a = nums;
8+
int n = a.size();
9+
10+
int ll = 0;
11+
int rr = n - 1;
12+
int mm;
13+
14+
while (ll <= rr) {
15+
mm = ll + (rr - ll >> 2 << 1);
16+
if (mm + 1 <= n - 1 && a[mm] == a[mm + 1]) {
17+
ll = mm + 2;
18+
} else if (mm - 1 >= 0 && a[mm] == a[mm - 1]) {
19+
rr = mm - 2;
20+
} else {
21+
return a[mm];
22+
}
23+
}
24+
// not supposed to be here
25+
return -1;
26+
}
27+
};

solve-the-equation_1_AC.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <string>
2+
using std::string;
3+
4+
class Solution {
5+
public:
6+
string solveEquation(string equation) {
7+
auto &s = equation;
8+
int ls = s.size();
9+
10+
int pe = s.find('=');
11+
string s1 = s.substr(0, pe);
12+
string s2 = s.substr(pe + 1, ls - pe - 1);
13+
14+
int a1, b1;
15+
parse(s1, a1, b1);
16+
int a2, b2;
17+
parse(s2, a2, b2);
18+
19+
if (a1 != a2) {
20+
return "x=" + to_string((b2 - b1) / (a1 - a2));
21+
} else if (b1 == b2) {
22+
return string("Infinite solutions");
23+
} else {
24+
return string("No solution");
25+
}
26+
}
27+
private:
28+
void parse(string s, int &a, int &b) {
29+
if (s[0] != '+' && s[0] != '-') {
30+
s = '+' + s;
31+
}
32+
33+
string ss = "";
34+
int ls = s.size();
35+
int i;
36+
int sign;
37+
int coef;
38+
39+
i = 0;
40+
a = 0;
41+
b = 0;
42+
while (i < ls) {
43+
sign = (s[i++] == '-' ? -1 : +1);
44+
while (i < ls && s[i] != '+' && s[i] != '-') {
45+
ss.push_back(s[i++]);
46+
}
47+
if (sscanf(ss.data(), "%d", &coef) != 1) {
48+
coef = 1;
49+
}
50+
if (ss.back() == 'x') {
51+
a += sign * coef;
52+
} else {
53+
b += sign * coef;
54+
}
55+
ss.clear();
56+
}
57+
}
58+
};

0 commit comments

Comments
 (0)