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 fc81942

Browse files
committed
finish problem 639
1 parent e78eb88 commit fc81942

File tree

5 files changed

+203
-33
lines changed

5 files changed

+203
-33
lines changed

‎Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ CXX = g++
22
CPPFLAGS = -Wall -std=c++11 -O2
33
LD_FLAGS =
44

5-
FILE = algoexpert/03
5+
FILE = algoexpert/05-jul
66
# FILE = contest/contest-195
77
SOURCES = $(FILE).cpp
88
OBJECTS = $(SOURCES:.cpp=.o)

‎july/01.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
int arrangeCoins(int n) { return (round(sqrt(8 * n + 1)) - 1) / 2; }
42+
// return (int)(Math.sqrt(2 * (long)n + 0.25) - 0.5);
43+
};
44+
45+
int main(int argc, char* argv[]) {
46+
// Solution s = Solution();
47+
return 0;
48+
}

‎july/template.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ typedef vector<string> vs;
1010
#define len(container) int((container).size())
1111
#define all(c) (c).begin(), (c).end()
1212

13+
const int M = 1e9 + 7;
14+
1315
template <typename ITER>
1416
void show(ITER begin, ITER end) {
1517
for (int i = 1; begin != end; i++) {
@@ -18,6 +20,11 @@ void show(ITER begin, ITER end) {
1820
}
1921
};
2022

23+
template <typename T>
24+
void addMod(T& a, T b) {
25+
a = (a + b) % M;
26+
}
27+
2128
inline bool isValid(int x, int y, int R, int C) {
2229
return x >= 0 && x < R && y >= 0 && y < C;
2330
}

‎problems/146.lru-cache.cpp

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* @lc app=leetcode id=146 lang=cpp
3+
*
4+
* [146] LRU Cache
5+
*
6+
* https://leetcode.com/problems/lru-cache/description/
7+
*
8+
* algorithms
9+
* Medium (30.13%)
10+
* Likes: 5789
11+
* Dislikes: 259
12+
* Total Accepted: 552.4K
13+
* Total Submissions: 1.7M
14+
* Testcase Example:
15+
'["LRUCache","put","put","get","put","get","put","get","get","get"]\n' +
16+
'[[2],[1,1],[2,2],[1],[3,3],[2],[4,4],[1],[3],[4]]'
17+
*
18+
* Design and implement a data structure for Least Recently Used (LRU) cache.
19+
* It should support the following operations: get and put.
20+
*
21+
* get(key) - Get the value (will always be positive) of the key if the key
22+
* exists in the cache, otherwise return -1.
23+
* put(key, value) - Set or insert the value if the key is not already present.
24+
* When the cache reached its capacity, it should invalidate the least recently
25+
* used item before inserting a new item.
26+
*
27+
* The cache is initialized with a positive capacity.
28+
*
29+
* Follow up:
30+
* Could you do both operations in O(1) time complexity?
31+
*
32+
* Example:
33+
*
34+
*
35+
* LRUCache cache = new LRUCache( 2 capacity );
36+
**cache.put(1, 1);
37+
*cache.put(2, 2);
38+
*cache.get(1); // returns 1
39+
*cache.put(3, 3); // evicts key 2
40+
*cache.get(2); // returns -1 (not found)
41+
*cache.put(4, 4); // evicts key 1
42+
*cache.get(1); // returns -1 (not found)
43+
*cache.get(3); // returns 3
44+
*cache.get(4); // returns 4
45+
*****/
46+
#include <bits/stdc++.h>
47+
48+
using namespace std;
49+
// @lc code=start
50+
class LRUCache {
51+
private:
52+
struct KVPair {
53+
int k, v;
54+
KVPair(int k, int v) : k(k), v(v) {}
55+
};
56+
unordered_map<int, list<KVPair>::iterator> m;
57+
list<KVPair> l;
58+
int cap;
59+
60+
public:
61+
LRUCache(int capacity) : cap(capacity) {}
62+
63+
int get(int key) {
64+
if (m.count(key)) {
65+
int val = m[key]->v;
66+
l.erase(m[key]);
67+
KVPair kv = KVPair(key, val); // OR kv = *m[key];
68+
l.push_front(kv); // emplace_front(k,v)
69+
m[key] = l.begin();
70+
return val;
71+
} else
72+
return -1;
73+
}
74+
75+
void put(int key, int value) {
76+
if (m.count(key)) {
77+
l.erase(m[key]);
78+
KVPair kv = KVPair(key, value);
79+
l.push_front(kv);
80+
m[key] = l.begin();
81+
} else {
82+
KVPair kv = KVPair(key, value);
83+
l.push_front(kv);
84+
m[key] = l.begin();
85+
}
86+
87+
if (m.size() > cap) {
88+
KVPair p = l.back();
89+
l.pop_back();
90+
m.erase(p.k);
91+
}
92+
}
93+
};
94+
95+
/**
96+
* Your LRUCache object will be instantiated and called as such:
97+
* LRUCache* obj = new LRUCache(capacity);
98+
* int param_1 = obj->get(key);
99+
* obj->put(key,value);
100+
*/
101+
// @lc code=end

‎problems/639.decode-ways-ii.cpp

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -79,42 +79,56 @@ typedef vector<string> vs;
7979
// @lc code=start
8080
class Solution {
8181
public:
82-
void dfsDecoding(string s, int st, int cur, int &res) {
83-
if (st == s.length()) {
84-
// cout << cur << endl;
85-
res += cur;
86-
return;
87-
}
88-
89-
switch (s[st]) {
90-
case '*': { //
91-
} break;
92-
case '1': { //
93-
} break;
94-
case '2': { //
95-
} break;
96-
if (st + 1 < s.length()) {
97-
cur += s[st];
98-
dfsDecoding(s, st + 1, cur, res);
99-
if (s[st + 1] < '7')
100-
dfsDecoding(s, st + 2, cur, res);
101-
else if (s[st + 1] == '*') {
102-
dfsDecoding(s, st + 2, 3 * cur, res);
103-
dfsDecoding(s, st + 2, 6 * cur, res);
104-
}
105-
}
106-
default: {
107-
cur += s[st];
108-
dfsDecoding(s, st + 1, cur, res);
109-
} break;
110-
}
111-
}
82+
const int M = 1e9 + 7;
11283

84+
void addMod(long &a, long b) { a = (a + b) % M; }
11385
int numDecodings(string s) {
11486
const int N = s.length();
87+
if (N == 0) {
88+
return 0;
89+
}
11590
int res = 0;
116-
dfsDecoding(s, 0, "", res);
117-
return res;
91+
vector<long> nums(N + 1, 0);
92+
reverse(s.begin(), s.end());
93+
nums[0] = 1;
94+
nums[1] = s[0] == '*' ? 9 : (s[0] == '0' ? 0 : 1);
95+
for (int i = 2; i <= N; ++i) {
96+
long tmp = nums[i - 1];
97+
switch (s[i - 1]) {
98+
case '0':
99+
tmp = 0;
100+
break;
101+
102+
case '*': {
103+
addMod(tmp, 8 * nums[i - 1]);
104+
105+
if (s[i - 2] == '*') {
106+
addMod(tmp, 15 * nums[i - 2]);
107+
} else if (s[i - 2] < '7') {
108+
addMod(tmp, 2 * nums[i - 2]);
109+
} else {
110+
addMod(tmp, nums[i - 2]);
111+
}
112+
} break;
113+
114+
case '1': {
115+
addMod(tmp, (s[i - 2] == '*' ? 9 : 1) * nums[i - 2]);
116+
} break;
117+
118+
case '2': {
119+
if (s[i - 2] == '*') {
120+
addMod(tmp, 6 * nums[i - 2]);
121+
} else if (s[i - 2] < '7') {
122+
addMod(tmp, nums[i - 2]);
123+
}
124+
} break;
125+
126+
default:
127+
break;
128+
}
129+
nums[i] = tmp % M;
130+
}
131+
return nums[N];
118132
}
119133
};
120134
// @lc code=end

0 commit comments

Comments
(0)

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