Skip to content

Commit 225539a

Browse files
committed
update 388
1 parent 20a47a9 commit 225539a

8 files changed

+181
-224
lines changed

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,20 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
4040
| [44%] | [leetcode-google.md](./list/leetcode-google.md) | 209/471 | 1 vip |
4141
| [50%] | [leetcode-topics.md](./list/leetcode-topics.md) | 72/144 | - |
4242
| [86%] | [leetcode-top-interview-150.md](./list/leetcode-top-interview-150.md) | 130/150 | - |
43-
| [89%] | [neetcode150.md](./list/neetcode150.md) | 134/150 | - |
44-
| [92%] | [grind169.md](./list/grind169.md) | 156/169 | 2 vips |
43+
| [88%] | [neetcode150.md](./list/neetcode150.md) | 133/150 | 1 vip |
44+
| [91%] | [grind169.md](./list/grind169.md) | 155/169 | 3 vips |
4545
| [] | [9c-advanced.md](./list/9c-advanced.md) | 75/93 | 18 vips |
4646
| [] | [9c-dp.md](./list/9c-dp.md) | 42/45 | 3 vips |
47-
| [] | [blind75.md](./list/blind75.md) | 77/77 | - |
47+
| [] | [blind75.md](./list/blind75.md) | 76/77 | 1 vip |
4848
| [] | [geekbang.md](./list/geekbang.md) | 55/55 | - |
4949
| [] | [grind102.md](./list/grind102.md) | 101/102 | 1 vip |
5050
| [] | [grind75.md](./list/grind75.md) | 75/75 | - |
5151
| [] | [leetcode-top-100-liked.md](./list/leetcode-top-100-liked.md) | 100/100 | - |
5252
| [] | [leetcode101.md](./list/leetcode101.md) | 183/184 | 1 vip |
5353
| [🔲] | [9c-basic.md](./list/9c-basic.md) | 14/129 | 3 vips |
54-
| [🔲] | [9c-top.md](./list/9c-top.md) | 14/52 | 1 vip |
54+
| [🔲] | [9c-top.md](./list/9c-top.md) | 16/52 | 1 vip |
5555

56-
**Solved**: 482 problems
56+
**Solved**: 481 problems
5757

5858
## 类型/Category
5959

@@ -462,7 +462,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
462462

463463
## Stack
464464

465-
| Link | Problem(21) | Solution | Tag | Time | Space | Ref |
465+
| Link | Problem(22) | Solution | Tag | Time | Space | Ref |
466466
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
467467
| [Leetcode-224](https://leetcode.com/problems/basic-calculator/) | Basic Calculator | [c++](./leetcode/224.basic-calculator.cpp), [python3](./leetcode/224.basic-calculator.py) | Stack | O\(N\) | O\(N\) | - |
468468
| [Leetcode-227](https://leetcode.com/problems/basic-calculator-ii/) | Basic Calculator II | [c++](./leetcode/227.basic-calculator-ii.cpp), [python3](./leetcode/227.basic-calculator-ii.py) | Stack | O\(N\) | O\(N\) | - |
@@ -477,6 +477,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
477477
| [Leetcode-232](https://leetcode.com/problems/implement-queue-using-stacks/) | Implement Queue Using Stacks | [c++](./leetcode/232.implement-queue-using-stacks.cpp), [python3](./leetcode/232.implement-queue-using-stacks.py) | Stack | O\(\*1\) | O\(N\) | - |
478478
| [Leetcode-225](https://leetcode.com/problems/implement-stack-using-queues/) | Implement Stack Using Queues | [c++](./leetcode/225.implement-stack-using-queues.cpp), [python3](./leetcode/225.implement-stack-using-queues.py) | Stack | O\(N\) | O\(N\) | - |
479479
| [Leetcode-897](https://leetcode.com/problems/increasing-order-search-tree/) | Increasing Order Search Tree | [c++](./leetcode/897.increasing-order-search-tree.cpp), [python3](./leetcode/897.increasing-order-search-tree.py) | Stack | O\(N\) | O\(H\) | - |
480+
| [Leetcode-388](https://leetcode.com/problems/longest-absolute-file-path/) | Longest Absolute File Path | [c++](./leetcode/388.longest-absolute-file-path.cpp), [python3](./leetcode/388.longest-absolute-file-path.py) | Stack | O\(N\) | O\(N\) | - |
480481
| [Leetcode-32](https://leetcode.com/problems/longest-valid-parentheses/) | Longest Valid Parentheses | [c++](./leetcode/32.longest-valid-parentheses.cpp), [python3](./leetcode/32.longest-valid-parentheses.py) | Stack | O\(N\) | O\(N\) | - |
481482
| [Leetcode-895](https://leetcode.com/problems/maximum-frequency-stack/) | Maximum Frequency Stack | [c++](./leetcode/895.maximum-frequency-stack.cpp), [python3](./leetcode/895.maximum-frequency-stack.py) | Stack | O\(1\) | O\(N\) | - |
482483
| [Leetcode-155](https://leetcode.com/problems/min-stack/) | Min Stack | [c++](./leetcode/155.min-stack.cpp), [python3](./leetcode/155.min-stack.py) | Stack | O\(1\) | O\(N\) | - |
@@ -719,7 +720,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
719720

720721
## Depth-First Search
721722

722-
| Link | Problem(48) | Solution | Tag | Time | Space | Ref |
723+
| Link | Problem(49) | Solution | Tag | Time | Space | Ref |
723724
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
724725
| [Leetcode-721](https://leetcode.com/problems/accounts-merge/) | Accounts Merge | [c++](./leetcode/721.accounts-merge.cpp), [python3](./leetcode/721.accounts-merge.py) | Depth-First Search | O\(MlogM\) | O\(N\) | - |
725726
| [Leetcode-637](https://leetcode.com/problems/average-of-levels-in-binary-tree/) | Average Of Levels In Binary Tree | [c++](./leetcode/637.average-of-levels-in-binary-tree.cpp), [python3](./leetcode/637.average-of-levels-in-binary-tree.py) | Depth-First Search | O\(N\) | O\(N\) | - |
@@ -741,6 +742,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
741742
| [Leetcode-226](https://leetcode.com/problems/invert-binary-tree/) | Invert Binary Tree | [c++](./leetcode/226.invert-binary-tree.cpp), [python3](./leetcode/226.invert-binary-tree.py) | Depth-First Search | O\(N\) | O\(H\) | - |
742743
| [Leetcode-785](https://leetcode.com/problems/is-graph-bipartite/) | Is Graph Bipartite | [c++](./leetcode/785.is-graph-bipartite.cpp), [python3](./leetcode/785.is-graph-bipartite.py) | Depth-First Search | O\(E\+V\) | O\(W\) | - |
743744
| [Leetcode-230](https://leetcode.com/problems/kth-smallest-element-in-a-bst/) | Kth Smallest Element In A Bst | [c++](./leetcode/230.kth-smallest-element-in-a-bst.cpp), [python3](./leetcode/230.kth-smallest-element-in-a-bst.py) | Depth-First Search | O\(K\) | O\(H\) | - |
745+
| [Leetcode-388](https://leetcode.com/problems/longest-absolute-file-path/) | Longest Absolute File Path | [c++](./leetcode/388.longest-absolute-file-path.cpp), [python3](./leetcode/388.longest-absolute-file-path.py) | Depth-First Search | O\(N\) | O\(N\) | - |
744746
| [Leetcode-235](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/) | Lowest Common Ancestor Of A Binary Search Tree | [c++](./leetcode/235.lowest-common-ancestor-of-a-binary-search-tree.cpp), [python3](./leetcode/235.lowest-common-ancestor-of-a-binary-search-tree.py) | Depth-First Search | O\(N\) | O\(H\) | - |
745747
| [Leetcode-236](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/) | Lowest Common Ancestor Of A Binary Tree | [c++](./leetcode/236.lowest-common-ancestor-of-a-binary-tree.cpp), [python3](./leetcode/236.lowest-common-ancestor-of-a-binary-tree.py) | Depth-First Search | O\(N\) | O\(H\) | - |
746748
| [Leetcode-695](https://leetcode.com/problems/max-area-of-island/) | Max Area Of Island | [c++](./leetcode/695.max-area-of-island.cpp), [python3](./leetcode/695.max-area-of-island.py) | Depth-First Search | O\(MN\) | O\(MN\) | - |
@@ -919,7 +921,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
919921

920922
## Other
921923

922-
| Link | Problem(128) | Solution | Tag | Time | Space | Ref |
924+
| Link | Problem(126) | Solution | Tag | Time | Space | Ref |
923925
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
924926
| [Leetcode-16](https://leetcode.com/problems/3sum-closest/) | 3Sum Closest | [c++](./leetcode/16.3sum-closest.cpp), [python3](./leetcode/16.3sum-closest.py) | Other | \- | \- | - |
925927
| [Leetcode-454](https://leetcode.com/problems/4sum-ii/) | 4Sum II | [c++](./leetcode/454.4sum-ii.cpp) | Other | \- | \- | - |
@@ -948,7 +950,6 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
948950
| [Leetcode-237](https://leetcode.com/problems/delete-node-in-a-linked-list/) | Delete Node In A Linked List | [python3](./leetcode/237.delete-node-in-a-linked-list.py) | Other | \- | \- | - |
949951
| [Leetcode-498](https://leetcode.com/problems/diagonal-traverse/) | Diagonal Traverse | [c++](./leetcode/498.diagonal-traverse.cpp) | Other | \- | \- | - |
950952
| [Leetcode-29](https://leetcode.com/problems/divide-two-integers/) | Divide Two Integers | [c++](./leetcode/29.divide-two-integers.cpp), [python3](./leetcode/29.divide-two-integers.py) | Other | \- | \- | - |
951-
| [Leetcode-271](https://leetcode.com/problems/encode-and-decode-strings/) | Encode And Decode Strings | [c++](./leetcode/271.encode-and-decode-strings.cpp), [python3](./leetcode/271.encode-and-decode-strings.py) | Other | \- | \- | - |
952953
| [Leetcode-535](https://leetcode.com/problems/encode-and-decode-tinyurl/) | Encode And Decode Tinyurl | [python3](./leetcode/535.encode-and-decode-tinyurl.py) | Other | \- | \- | - |
953954
| [Leetcode-282](https://leetcode.com/problems/expression-add-operators/) | Expression Add Operators | [c++](./leetcode/282.expression-add-operators.cpp), [python3](./leetcode/282.expression-add-operators.py) | Other | \- | \- | - |
954955
| [Leetcode-760](https://leetcode.com/problems/find-anagram-mappings/) | Find Anagram Mappings | [python3](./leetcode/760.find-anagram-mappings.py) | Other | \- | \- | - |
@@ -977,7 +978,6 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
977978
| [Leetcode-747](https://leetcode.com/problems/largest-number-at-least-twice-of-others/) | Largest Number At Least Twice Of Others | [c++](./leetcode/747.largest-number-at-least-twice-of-others.cpp) | Other | \- | \- | - |
978979
| [Leetcode-17](https://leetcode.com/problems/letter-combinations-of-a-phone-number/) | Letter Combinations Of A Phone Number | [c++](./leetcode/17.letter-combinations-of-a-phone-number.cpp), [python3](./leetcode/17.letter-combinations-of-a-phone-number.py) | Other | \- | \- | - |
979980
| [Leetcode-359](https://leetcode.com/problems/logger-rate-limiter/) | Logger Rate Limiter | [c++](./leetcode/359.logger-rate-limiter.cpp) | Other | \- | \- | - |
980-
| [Leetcode-388](https://leetcode.com/problems/longest-absolute-file-path/) | Longest Absolute File Path | [c++](./leetcode/388.longest-absolute-file-path.cpp), [python3](./leetcode/388.longest-absolute-file-path.py) | Other | \- | \- | - |
981981
| [Leetcode-14](https://leetcode.com/problems/longest-common-prefix/) | Longest Common Prefix | [c++](./leetcode/14.longest-common-prefix.cpp), [python3](./leetcode/14.longest-common-prefix.py) | Other | \- | \- | - |
982982
| [Leetcode-687](https://leetcode.com/problems/longest-univalue-path/) | Longest Univalue Path | [c++](./leetcode/687.longest-univalue-path.cpp), [python3](./leetcode/687.longest-univalue-path.py) | Other | \- | \- | - |
983983
| [Leetcode-229](https://leetcode.com/problems/majority-element-ii/) | Majority Element II | [c++](./leetcode/229.majority-element-ii.cpp), [python3](./leetcode/229.majority-element-ii.py) | Other | \- | \- | - |

leetcode/271.encode-and-decode-strings.cpp

Lines changed: 0 additions & 65 deletions
This file was deleted.

leetcode/271.encode-and-decode-strings.py

Lines changed: 0 additions & 48 deletions
This file was deleted.

leetcode/271.encode-and-decode-strings.vip

Whitespace-only changes.
Lines changed: 80 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,88 @@
1+
// Tag: String, Stack, Depth-First Search
2+
// Time: O(N)
3+
// Space: O(N)
4+
// Ref: -
5+
// Note: -
6+
7+
// Suppose we have a file system that stores both files and directories. An example of one system is represented in the following picture:
8+
//
9+
// Here, we have dir as the only directory in the root. dir contains two subdirectories, subdir1 and subdir2. subdir1 contains a file file1.ext and subdirectory subsubdir1. subdir2 contains a subdirectory subsubdir2, which contains a file file2.ext.
10+
// In text form, it looks like this (with ⟶ representing the tab character):
11+
//
12+
// dir
13+
// ⟶ subdir1
14+
// ⟶ ⟶ file1.ext
15+
// ⟶ ⟶ subsubdir1
16+
// ⟶ subdir2
17+
// ⟶ ⟶ subsubdir2
18+
// ⟶ ⟶ ⟶ file2.ext
19+
//
20+
// If we were to write this representation in code, it will look like this: "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext". Note that the '\n' and '\t' are the new-line and tab characters.
21+
// Every file and directory has a unique absolute path in the file system, which is the order of directories that must be opened to reach the file/directory itself, all concatenated by '/'s. Using the above example, the absolute path to file2.ext is "dir/subdir2/subsubdir2/file2.ext". Each directory name consists of letters, digits, and/or spaces. Each file name is of the form name.extension, where name and extension consist of letters, digits, and/or spaces.
22+
// Given a string input representing the file system in the explained format, return the length of the longest absolute path to a file in the abstracted file system. If there is no file in the system, return 0.
23+
// Note that the testcases are generated such that the file system is valid and no file or directory name has length 0.
24+
//  
25+
// Example 1:
26+
//
27+
//
28+
// Input: input = "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext"
29+
// Output: 20
30+
// Explanation: We have only one file, and the absolute path is "dir/subdir2/file.ext" of length 20.
31+
//
32+
// Example 2:
33+
//
34+
//
35+
// Input: input = "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext"
36+
// Output: 32
37+
// Explanation: We have two files:
38+
// "dir/subdir1/file1.ext" of length 21
39+
// "dir/subdir2/subsubdir2/file2.ext" of length 32.
40+
// We return 32 since it is the longest absolute path to a file.
41+
//
42+
// Example 3:
43+
//
44+
// Input: input = "a"
45+
// Output: 0
46+
// Explanation: We do not have any files, just a single directory named "a".
47+
//
48+
//  
49+
// Constraints:
50+
//
51+
// 1 <= input.length <= 104
52+
// input may contain lowercase or uppercase English letters, a new line character '\n', a tab character '\t', a dot '.', a space ' ', and digits.
53+
// All file and directory names have positive length.
54+
//
55+
//
56+
157
class Solution {
2-
private:
3-
vector<string> split(string& line, char seporator)
4-
{
5-
vector<string> res;
6-
string word = "";
7-
8-
for(auto i = 0; i < line.size(); ++i)
9-
{
10-
if (line[i] == seporator)
11-
{
12-
res.push_back(word);
13-
word = "";
14-
}
15-
else
16-
{
17-
word += line[i];
18-
}
19-
}
20-
21-
if (word.size() > 0)
22-
{
23-
res.push_back(word);
24-
}
25-
26-
return res;
27-
}
2858
public:
2959
int lengthLongestPath(string input) {
30-
vector<string> lines = split(input, '\n');
31-
unordered_map<int, int> table;
32-
int longest = 0;
33-
34-
for (auto i = 0; i < lines.size(); ++i)
35-
{
36-
string l = lines[i];
37-
int level = l.find_last_of('\t') + 1;
38-
int size = l.substr(level).size();
39-
40-
if (l.find('.') == -1)
41-
{
42-
table[level] = table[level - 1] + size + 1; // append len of "/" at the end
60+
vector<string> lines;
61+
int i = 0;
62+
int j = 0;
63+
while (j < input.size()) {
64+
if (input[j] == '\n') {
65+
lines.push_back(input.substr(i, j - i));
66+
i = j + 1;
67+
}
68+
j += 1;
69+
}
70+
lines.push_back(input.substr(i, j - i));
71+
unordered_map<int, int> prefix;
72+
int res = 0;
73+
for (auto &l: lines) {
74+
int tab = 0;
75+
while (l[tab] == '\t') {
76+
tab += 1;
4377
}
44-
else
45-
{
46-
longest = max(longest, table[level - 1] + size);
78+
79+
int path_size = l.substr(tab).size();
80+
if (l.find('.') != string::npos) {
81+
res = max(res, prefix[tab - 1] + path_size);
82+
} else {
83+
prefix[tab] = prefix[tab - 1] + path_size + 1;
4784
}
4885
}
49-
50-
return longest;
86+
return res;
5187
}
5288
};

0 commit comments

Comments
 (0)