Skip to content

Commit f231ead

Browse files
author
Liang Wang
committed
update leetcode
1 parent 1344d4f commit f231ead

File tree

6 files changed

+288
-0
lines changed

6 files changed

+288
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
class Solution {
11+
public:
12+
vector<double> averageOfLevels(TreeNode* root) {
13+
vector<double> avg, sm;
14+
vector<int> cnt;
15+
queue<pair<TreeNode*, int> > q;
16+
q.push({root, 0});
17+
while (!q.empty()) {
18+
pair<TreeNode*, int> ptl = q.front();
19+
q.pop();
20+
TreeNode* node = ptl.first;
21+
int level = ptl.second;
22+
if (level == sm.size()) {
23+
sm.push_back(0.0);
24+
cnt.push_back(0);
25+
}
26+
sm[level] += node->val;
27+
++cnt[level];
28+
if (NULL != node->left) {
29+
q.push({node->left, level+1});
30+
}
31+
if (NULL != node->right) {
32+
q.push({node->right, level+1});
33+
}
34+
}
35+
for (int i = 0; i < cnt.size(); ++i) {
36+
avg.push_back(sm[i] / cnt[i]);
37+
}
38+
return avg;
39+
}
40+
};

leetcode/DesignCircularDeque.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
class MyCircularDeque {
2+
private:
3+
vector<int> data;
4+
int front, length;
5+
public:
6+
/** Initialize your data structure here. Set the size of the deque to be k. */
7+
MyCircularDeque(int k) {
8+
data.resize(k);
9+
front = length = 0;
10+
}
11+
12+
/** Adds an item at the front of Deque. Return true if the operation is successful. */
13+
bool insertFront(int value) {
14+
if (isFull()) return false;
15+
if (!isEmpty()) {
16+
front = (front - 1 + data.size()) % data.size();
17+
}
18+
data[front] = value;
19+
++length;
20+
return true;
21+
}
22+
23+
/** Adds an item at the rear of Deque. Return true if the operation is successful. */
24+
bool insertLast(int value) {
25+
if (isFull()) return false;
26+
int rear = isEmpty() ? front : (front + length) % data.size();
27+
data[rear] = value;
28+
++length;
29+
return true;
30+
}
31+
32+
/** Deletes an item from the front of Deque. Return true if the operation is successful. */
33+
bool deleteFront() {
34+
if (isEmpty()) return false;
35+
front = (front + 1) % data.size();
36+
--length;
37+
return true;
38+
}
39+
40+
/** Deletes an item from the rear of Deque. Return true if the operation is successful. */
41+
bool deleteLast() {
42+
if (isEmpty()) return false;
43+
--length;
44+
return true;
45+
}
46+
47+
/** Get the front item from the deque. */
48+
int getFront() {
49+
if (isEmpty()) return -1;
50+
return data[front];
51+
}
52+
53+
/** Get the last item from the deque. */
54+
int getRear() {
55+
if (isEmpty()) return -1;
56+
int rear = (front + length - 1 + data.size()) % data.size();
57+
return data[rear];
58+
}
59+
60+
/** Checks whether the circular deque is empty or not. */
61+
bool isEmpty() {
62+
return length == 0;
63+
}
64+
65+
/** Checks whether the circular deque is full or not. */
66+
bool isFull() {
67+
return length == data.size();
68+
}
69+
};
70+
71+
/**
72+
* Your MyCircularDeque object will be instantiated and called as such:
73+
* MyCircularDeque* obj = new MyCircularDeque(k);
74+
* bool param_1 = obj->insertFront(value);
75+
* bool param_2 = obj->insertLast(value);
76+
* bool param_3 = obj->deleteFront();
77+
* bool param_4 = obj->deleteLast();
78+
* int param_5 = obj->getFront();
79+
* int param_6 = obj->getRear();
80+
* bool param_7 = obj->isEmpty();
81+
* bool param_8 = obj->isFull();
82+
*/
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def exclusiveTime(self, n: int, logs: List[str]) -> List[int]:
3+
res = [0 for _ in range(n)]
4+
stack = []
5+
log_tp = []
6+
for log in logs:
7+
idx, cmd, tm = log.split(':')
8+
idx, tm = int(idx), int(tm)
9+
if cmd == 'end':
10+
tm += 1
11+
log_tp.append((idx, cmd, tm))
12+
for i, (idx, cmd, tm) in enumerate(log_tp):
13+
if cmd == 'start':
14+
if stack:
15+
res[stack[-1]] += tm - log_tp[i-1][2]
16+
stack.append(idx)
17+
elif cmd == 'end':
18+
res[stack[-1]] += tm - log_tp[i-1][2]
19+
stack.pop()
20+
return res
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
double findMaxAverage(vector<int>& nums, int k) {
4+
int mx_sm = accumulate(nums.begin(), nums.begin() + k, 0);
5+
int sm = mx_sm;
6+
for (int i = k; i < nums.size(); ++i) {
7+
sm += nums[i];
8+
sm -= nums[i - k];
9+
mx_sm = max(mx_sm, sm);
10+
}
11+
return 1.0 * mx_sm / k;
12+
}
13+
};

leetcode/ShopingOffers.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
class Solution {
2+
private:
3+
vector<int> price;
4+
vector<vector<int>> special;
5+
map<int, int> dp;
6+
7+
int solve(vector<int>& cur_needs) {
8+
int key = to_key(cur_needs);
9+
if (dp.find(key) != dp.end()) {
10+
return dp[key];
11+
}
12+
int val = 0;
13+
for (int i = 0; i < price.size(); ++i) {
14+
val += cur_needs[i] * price[i];
15+
}
16+
for (auto& s : special) {
17+
if (ok(cur_needs, s)) {
18+
for (int i = 0; i < cur_needs.size(); ++i) {
19+
cur_needs[i] -= s[i];
20+
}
21+
int cost = solve(cur_needs) + s.back();
22+
val = min(cost, val);
23+
for (int i = 0; i < cur_needs.size(); ++i) {
24+
cur_needs[i] += s[i];
25+
}
26+
}
27+
}
28+
dp[key] = val;
29+
return val;
30+
}
31+
32+
int to_key(vector<int>& v) {
33+
int ret = 0;
34+
for (int i = 0; i < v.size(); ++i) {
35+
ret = ret * 10 + v[i];
36+
}
37+
return ret;
38+
}
39+
bool ok(vector<int>& needs, vector<int>& s) {
40+
for (int i = 0; i < needs.size(); ++i) {
41+
if (s[i] > needs[i]) {
42+
return false;
43+
}
44+
}
45+
return true;
46+
}
47+
public:
48+
int shoppingOffers(vector<int>& price, vector<vector<int>>& special, vector<int>& needs) {
49+
this->price = price;
50+
this->special = special;
51+
dp.clear();
52+
dp[0] = 0;
53+
return solve(needs);
54+
}
55+
};

leetcode/SolveTheEquation.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
#define FOR(i, n) for (int i = 0; i < (int)n; ++i)
4+
typedef long long ll;
5+
typedef pair<int, int> pii;
6+
7+
struct Term {
8+
bool positive;
9+
int coef;
10+
bool has_x;
11+
bool left;
12+
bool has_digit;
13+
Term(bool _left): positive(true), left(_left), has_x(false), coef(0), has_digit(false) {}
14+
};
15+
16+
class Solution {
17+
public:
18+
string solveEquation(string equation) {
19+
bool left = true;
20+
vector<Term> terms;
21+
Term term(left);
22+
FOR(i, equation.size()) {
23+
char c = equation[i];
24+
if (c == 'x') {
25+
term.has_x = true;
26+
} else if (c <= '9' && c >= '0') {
27+
term.coef = term.coef * 10 + (c - '0');
28+
term.has_digit = true;
29+
} else if (c == '+' || c == '-') {
30+
if (i > 0 && equation[i - 1] != '=') {
31+
terms.push_back(term);
32+
}
33+
term = Term(left);
34+
term.positive = (c == '+');
35+
} else if (c == '=') {
36+
terms.push_back(term);
37+
left = false;
38+
term = Term(left);
39+
}
40+
}
41+
terms.push_back(term);
42+
43+
int x_coef = 0, num_coef = 0;
44+
for (auto& t : terms) {
45+
if (!t.has_digit) {
46+
t.coef = 1;
47+
}
48+
if (!t.positive) {
49+
t.coef = -t.coef;
50+
t.positive = true;
51+
}
52+
if (t.has_x) {
53+
x_coef += t.left ? t.coef : -t.coef;
54+
} else {
55+
num_coef += t.left ? -t.coef : t.coef;
56+
}
57+
}
58+
if (x_coef == 0 && num_coef == 0) {
59+
return "Infinite solutions";
60+
} else if (x_coef == 0 && num_coef != 0) {
61+
return "No solution";
62+
} else {
63+
return "x=" + to_string(num_coef / x_coef);
64+
}
65+
}
66+
};
67+
68+
int main() {
69+
Solution solution;
70+
cout << solution.solveEquation("-x=1") << endl;
71+
cout << solution.solveEquation("-x=-1") << endl;
72+
cout << solution.solveEquation("x+5-3+x=6+x-2") << endl;
73+
cout << solution.solveEquation("x=x") << endl;
74+
cout << solution.solveEquation("2x=x") << endl;
75+
cout << solution.solveEquation("2x+3x-6x=x+2") << endl;
76+
cout << solution.solveEquation("x=x+2") << endl;
77+
return 0;
78+
}

0 commit comments

Comments
 (0)