Skip to content

Commit 30a7a11

Browse files
committed
[Function add]
1. Add leetcode solutions.
1 parent 3dbb9cd commit 30a7a11

5 files changed

+295
-0
lines changed

leetcode/438. Find All Anagrams in a String.md

Whitespace-only changes.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
## 609. Find Duplicate File in System
2+
3+
### Question
4+
Given a list of directory info including directory path, and all the files with contents in this directory, you need to find out all the groups of duplicate files in the file system in terms of their paths.
5+
6+
A group of duplicate files consists of at least two files that have exactly the same content.
7+
8+
A single directory info string in the input list has the following format:
9+
10+
"root/d1/d2/.../dm f1.txt(f1_content) f2.txt(f2_content) ... fn.txt(fn_content)"
11+
12+
It means there are n files (f1.txt, f2.txt ... fn.txt with content f1_content, f2_content ... fn_content, respectively) in directory root/d1/d2/.../dm. Note that n >= 1 and m >= 0. If m = 0, it means the directory is just the root directory.
13+
14+
The output is a list of group of duplicate file paths. For each group, it contains all the file paths of the files that have the same content. A file path is a string that has the following format:
15+
16+
"directory_path/file_name.txt"
17+
18+
```
19+
Example 1:
20+
21+
Input:
22+
["root/a 1.txt(abcd) 2.txt(efgh)", "root/c 3.txt(abcd)", "root/c/d 4.txt(efgh)", "root 4.txt(efgh)"]
23+
Output:
24+
[["root/a/2.txt","root/c/d/4.txt","root/4.txt"],["root/a/1.txt","root/c/3.txt"]]
25+
```
26+
27+
Note:
28+
* No order is required for the final output.
29+
* You may assume the directory name, file name and file content only has letters and digits, and the length of file content is in the range of [1,50].
30+
* The number of files given is in the range of [1,20000].
31+
* You may assume no files or directories share the same name in the same directory.
32+
* You may assume each given directory info represents a unique directory. Directory path and file info are separated by a single blank space.
33+
34+
35+
Follow-up beyond contest:
36+
1. Imagine you are given a real file system, how will you search files? DFS or BFS?
37+
2. If the file content is very large (GB level), how will you modify your solution?
38+
3. If you can only read the file by 1kb each time, how will you modify your solution?
39+
4. What is the time complexity of your modified solution? What is the most time-consuming part and memory consuming part of it? How to optimize?
40+
5. How to make sure the duplicated files you find are not false positive?
41+
42+
43+
44+
### Thinking:
45+
* Method 1:
46+
```Java
47+
class Solution {
48+
public List<List<String>> findDuplicate(String[] paths) {
49+
Map<String, String> pathMap = new HashMap<>(); //key: content, value:path/filename;
50+
Map<String, List<String>> resMap = new HashMap<>(); //key: content, value: list of duplicate files.
51+
for(String path: paths){
52+
//Need to add them to the pathMap.
53+
String[] tokens = path.split(" ");
54+
String dir = tokens[0];
55+
for(int i = 1; i < tokens.length; i++){
56+
int index = tokens[i].indexOf("(");
57+
String filename = tokens[i].substring(0, index);
58+
String content = tokens[i].substring(index, tokens[i].length() - 1);
59+
if(pathMap.containsKey(content)){
60+
if(!resMap.containsKey(content)){
61+
List<String> dup = new ArrayList<>();
62+
dup.add(pathMap.get(content));
63+
resMap.put(content, dup);
64+
}
65+
List<String> dup = resMap.get(content);
66+
dup.add(dir + "/" + filename);
67+
resMap.put(content, dup);
68+
}else{
69+
pathMap.put(content, dir + "/" + filename);
70+
}
71+
}
72+
}
73+
List<List<String>> result = new ArrayList<>();
74+
for(List<String> dups : resMap.values()){
75+
result.add(dups);
76+
}
77+
return result;
78+
}
79+
}
80+
```

leetcode/729. My Calendar I.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
## 729. My Calendar I
2+
3+
### Question
4+
Implement a MyCalendar class to store your events. A new event can be added if adding the event will not cause a double booking.
5+
6+
Your class will have the method, book(int start, int end). Formally, this represents a booking on the half open interval [start, end), the range of real numbers x such that start <= x < end.
7+
8+
A double booking happens when two events have some non-empty intersection (ie., there is some time that is common to both events.)
9+
10+
For each call to the method MyCalendar.book, return true if the event can be added to the calendar successfully without causing a double booking. Otherwise, return false and do not add the event to the calendar.
11+
Your class will be called like this: MyCalendar cal = new MyCalendar(); MyCalendar.book(start, end)
12+
13+
```
14+
Example 1:
15+
16+
MyCalendar();
17+
MyCalendar.book(10, 20); // returns true
18+
MyCalendar.book(15, 25); // returns false
19+
MyCalendar.book(20, 30); // returns true
20+
Explanation:
21+
The first event can be booked. The second can't because time 15 is already booked by another event.
22+
The third event can be booked, as the first event takes every time less than 20, but not including 20.
23+
```
24+
25+
26+
Note:
27+
* The number of calls to MyCalendar.book per test case will be at most 1000.
28+
* In calls to MyCalendar.book(start, end), start and end are integers in the range [0, 10^9].
29+
30+
31+
32+
33+
### Thinking:
34+
* Method 1: TreeMap 25min
35+
```Java
36+
class MyCalendar {
37+
private TreeMap<Integer, Integer> map; //key is the start time and end is the end time
38+
public MyCalendar() {
39+
this.map = new TreeMap<>();
40+
}
41+
42+
public boolean book(int start, int end) {
43+
// Check the previous one.
44+
Map.Entry<Integer, Integer> ceiling = this.map.ceilingEntry(start);
45+
if(ceiling != null){
46+
if(end > ceiling.getKey()) return false;
47+
}
48+
// Check the next one.
49+
Map.Entry<Integer, Integer> floor = this.map.floorEntry(start);
50+
if(floor != null){
51+
if(start < floor.getValue()) return false;
52+
}
53+
this.map.put(start, end);
54+
return true;
55+
}
56+
}
57+
58+
/**
59+
* Your MyCalendar object will be instantiated and called as such:
60+
* MyCalendar obj = new MyCalendar();
61+
* boolean param_1 = obj.book(start,end);
62+
*/
63+
```
64+
65+
* Method 2: Brutal force
66+
```Java
67+
class MyCalendar {
68+
private List<int[]> calendar;
69+
public MyCalendar() {
70+
this.calendar = new ArrayList<>();
71+
}
72+
73+
public boolean book(int start, int end) {
74+
for(int[] book: calendar){
75+
if(Math.max(start, book[0]) < Math.min(book[1], end)) return false;
76+
}
77+
this.calendar.add(new int[]{start, end});
78+
return true;
79+
}
80+
}
81+
82+
/**
83+
* Your MyCalendar object will be instantiated and called as such:
84+
* MyCalendar obj = new MyCalendar();
85+
* boolean param_1 = obj.book(start,end);
86+
*/
87+
```

leetcode/731. My Calendar II.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
## 731. My Calendar II
2+
3+
### Question
4+
Implement a MyCalendarTwo class to store your events. A new event can be added if adding the event will not cause a triple booking.
5+
6+
Your class will have one method, book(int start, int end). Formally, this represents a booking on the half open interval [start, end), the range of real numbers x such that start <= x < end.
7+
8+
A triple booking happens when three events have some non-empty intersection (ie., there is some time that is common to all 3 events.)
9+
10+
For each call to the method MyCalendar.book, return true if the event can be added to the calendar successfully without causing a triple booking. Otherwise, return false and do not add the event to the calendar.
11+
Your class will be called like this: MyCalendar cal = new MyCalendar(); MyCalendar.book(start, end)
12+
13+
```
14+
Example 1:
15+
16+
MyCalendar();
17+
MyCalendar.book(10, 20); // returns true
18+
MyCalendar.book(50, 60); // returns true
19+
MyCalendar.book(10, 40); // returns true
20+
MyCalendar.book(5, 15); // returns false
21+
MyCalendar.book(5, 10); // returns true
22+
MyCalendar.book(25, 55); // returns true
23+
Explanation:
24+
The first two events can be booked. The third event can be double booked.
25+
The fourth event (5, 15) can't be booked, because it would result in a triple booking.
26+
The fifth event (5, 10) can be booked, as it does not use time 10 which is already double booked.
27+
The sixth event (25, 55) can be booked, as the time in [25, 40) will be double booked with the third event;
28+
the time [40, 50) will be single booked, and the time [50, 55) will be double booked with the second event.
29+
```
30+
31+
Note:
32+
* The number of calls to MyCalendar.book per test case will be at most 1000.
33+
* In calls to MyCalendar.book(start, end), start and end are integers in the range [0, 10^9].
34+
35+
36+
### Thinking:
37+
* Method 1: Brutal force
38+
```Java
39+
class MyCalendarTwo {
40+
private List<int[]> calendar;
41+
private List<int[]> overlap;
42+
public MyCalendarTwo() {
43+
this.calendar = new ArrayList<>();
44+
this.overlap = new ArrayList<>();
45+
}
46+
47+
public boolean book(int start, int end) {
48+
for(int[] o : overlap){
49+
if(Math.max(o[0], start) < Math.min(o[1], end)) return false;
50+
}
51+
for(int[] c : calendar){
52+
if(Math.max(c[0], start) < Math.min(c[1], end)){
53+
overlap.add(new int[]{Math.max(c[0], start), Math.min(c[1], end)});
54+
}
55+
}
56+
calendar.add(new int[]{start, end});
57+
return true;
58+
}
59+
}
60+
61+
/**
62+
* Your MyCalendarTwo object will be instantiated and called as such:
63+
* MyCalendarTwo obj = new MyCalendarTwo();
64+
* boolean param_1 = obj.book(start,end);
65+
*/
66+
```
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
## 957. Prison Cells After N Days
2+
3+
### Question
4+
There are 8 prison cells in a row, and each cell is either occupied or vacant.
5+
6+
Each day, whether the cell is occupied or vacant changes according to the following rules:
7+
8+
If a cell has two adjacent neighbors that are both occupied or both vacant, then the cell becomes occupied.
9+
Otherwise, it becomes vacant.
10+
11+
(Note that because the prison is a row, the first and the last cells in the row can't have two adjacent neighbors.)
12+
13+
We describe the current state of the prison in the following way: cells[i] == 1 if the i-th cell is occupied, else cells[i] == 0.
14+
15+
Given the initial state of the prison, return the state of the prison after N days (and N such changes described above.)
16+
17+
```
18+
Example 1:
19+
20+
Input: cells = [0,1,0,1,1,0,0,1], N = 7
21+
Output: [0,0,1,1,0,0,0,0]
22+
Explanation:
23+
The following table summarizes the state of the prison on each day:
24+
Day 0: [0, 1, 0, 1, 1, 0, 0, 1]
25+
Day 1: [0, 1, 1, 0, 0, 0, 0, 0]
26+
Day 2: [0, 0, 0, 0, 1, 1, 1, 0]
27+
Day 3: [0, 1, 1, 0, 0, 1, 0, 0]
28+
Day 4: [0, 0, 0, 0, 0, 1, 0, 0]
29+
Day 5: [0, 1, 1, 1, 0, 1, 0, 0]
30+
Day 6: [0, 0, 1, 0, 1, 1, 0, 0]
31+
Day 7: [0, 0, 1, 1, 0, 0, 0, 0]
32+
33+
Example 2:
34+
35+
Input: cells = [1,0,0,1,0,0,1,0], N = 1000000000
36+
Output: [0,0,1,1,1,1,1,0]
37+
```
38+
39+
Note:
40+
* cells.length == 8
41+
* cells[i] is in {0, 1}
42+
* 1 <= N <= 10^9
43+
44+
45+
46+
### Thinking:
47+
* Method 1:
48+
```Java
49+
class Solution {
50+
public int[] prisonAfterNDays(int[] cells, int N) {
51+
N = N % 14 == 0 ? 14: N % 14;
52+
for(int i = 0; i < N; i++){
53+
int[] next = new int[8];
54+
for(int j = 1; j < 7; j++){
55+
next[j] = cells[j - 1] == cells[j + 1] ? 1: 0;
56+
}
57+
cells = next;
58+
}
59+
return cells;
60+
}
61+
}
62+
```

0 commit comments

Comments
 (0)