-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
68a7aef
commit 0b726b1
Showing
9 changed files
with
330 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Given a list paths of directory info, including the directory path, and all the files with contents in this directory, return all the duplicate files in the file system in terms of their paths. You may return the answer in any order. | ||
# A group of duplicate files consists of at least two files that have the same content. | ||
# A single directory info string in the input list has the following format: | ||
# - "root/d1/d2/.../dm f1.txt(f1_content) f2.txt(f2_content) ... fn.txt(fn_content)" | ||
# It means there are n files (f1.txt, f2.txt ... fn.txt) with content (f1_content, f2_content ... fn_content) respectively in the directory "root/d1/d2/.../dm". Note that n >= 1 and m >= 0. If m = 0, it means the directory is just the root directory. | ||
# The output is a list of groups 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: | ||
# - "directory_path/file_name.txt" | ||
# | ||
# Example 1: | ||
# | ||
# Input: paths = ["root/a 1.txt(abcd) 2.txt(efgh)","root/c 3.txt(abcd)","root/c/d 4.txt(efgh)","root 4.txt(efgh)"] | ||
# Output: [["root/a/2.txt","root/c/d/4.txt","root/4.txt"],["root/a/1.txt","root/c/3.txt"]] | ||
# | ||
# Example 2: | ||
# | ||
# Input: paths = ["root/a 1.txt(abcd) 2.txt(efgh)","root/c 3.txt(abcd)","root/c/d 4.txt(efgh)"] | ||
# Output: [["root/a/2.txt","root/c/d/4.txt"],["root/a/1.txt","root/c/3.txt"]] | ||
# | ||
# Constraints: | ||
# | ||
# 1 <= paths.length <= 2 * 10^4 | ||
# 1 <= paths[i].length <= 3000 | ||
# 1 <= sum(paths[i].length) <= 5 * 10^5 | ||
# paths[i] consist of English letters, digits, '/', '.', '(', ')', and ' '. | ||
# You may assume no files or directories share the same name in the same directory. | ||
# You may assume each given directory info represents a unique directory. A single blank space separates the directory path and file info. | ||
# | ||
# Follow up: | ||
# | ||
# Imagine you are given a real file system, how will you search files? DFS or BFS? | ||
# If the file content is very large (GB level), how will you modify your solution? | ||
# If you can only read the file by 1kb each time, how will you modify your solution? | ||
# 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? | ||
# How to make sure the duplicated files you find are not false positive? | ||
|
||
from collections import defaultdict | ||
|
||
|
||
class Solution: | ||
def findDuplicate(self, paths: List[str]) -> List[List[str]]: | ||
groups = defaultdict(list) | ||
for path in paths: | ||
dir, *files = path.split() | ||
for f in files: | ||
name, content = f.split("(") | ||
content = content[:-1] | ||
path = dir + "/" + name | ||
groups[content].append(path) | ||
return [path for path in groups.values() if len(path) > 1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# We are given a list schedule of employees, which represents the working time for each employee. | ||
# Each employee has a list of non-overlapping Intervals, and these intervals are in sorted order. | ||
# Return the list of finite intervals representing common, positive-length free time for all employees, also in sorted order. | ||
# (Even though we are representing Intervals in the form [x, y], the objects inside are Intervals, not lists or arrays. For example, schedule[0][0].start = 1, schedule[0][0].end = 2, and schedule[0][0][0] is not defined). Also, we wouldn't include intervals like [5, 5] in our answer, as they have zero length. | ||
# | ||
# Example 1: | ||
# | ||
# Input: schedule = [[[1,2],[5,6]],[[1,3]],[[4,10]]] | ||
# Output: [[3,4]] | ||
# Explanation: There are a total of three employees, and all common | ||
# free time intervals would be [-inf, 1], [3, 4], [10, inf]. | ||
# We discard any intervals that contain inf as they aren't finite. | ||
# | ||
# Example 2: | ||
# | ||
# Input: schedule = [[[1,3],[6,7]],[[2,4]],[[2,5],[9,12]]] | ||
# Output: [[5,6],[7,9]] | ||
# | ||
# Constraints: | ||
# | ||
# 1 <= schedule.length , schedule[i].length <= 50 | ||
# 0 <= schedule[i].start < schedule[i].end <= 10^8 | ||
|
||
|
||
""" | ||
# Definition for an Interval. | ||
class Interval: | ||
def __init__(self, start: int = None, end: int = None): | ||
self.start = start | ||
self.end = end | ||
""" | ||
|
||
|
||
# Similar to 56. Merge Intervals | ||
# It doesn't matter which employee an interval belongs to, so just flatten | ||
class Solution: | ||
def employeeFreeTime(self, schedule: "[[Interval]]") -> "[Interval]": | ||
intervals = sorted([i for s in schedule for i in s], key=lambda i: i.start) | ||
res = [] | ||
end = intervals[0].end | ||
for i in intervals[1:]: | ||
if i.start > end: | ||
res.append(Interval(end, i.start)) | ||
end = max(end, i.end) | ||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# You are given a series of video clips from a sporting event that lasted time seconds. These video clips can be overlapping with each other and have varying lengths. | ||
# Each video clip is described by an array clips where clips[i] = [start_i, end_i] indicates that the ith clip started at start_i and ended at end_i. | ||
# We can cut these clips into segments freely. | ||
# - For example, a clip [0, 7] can be cut into segments [0, 1] + [1, 3] + [3, 7]. | ||
# Return the minimum number of clips needed so that we can cut the clips into segments that cover the entire sporting event [0, time]. If the task is impossible, return -1. | ||
# | ||
# Example 1: | ||
# | ||
# Input: clips = [[0,2],[4,6],[8,10],[1,9],[1,5],[5,9]], time = 10 | ||
# Output: 3 | ||
# Explanation: We take the clips [0,2], [8,10], [1,9]; a total of 3 clips. | ||
# Then, we can reconstruct the sporting event as follows: | ||
# We cut [1,9] into segments [1,2] + [2,8] + [8,9]. | ||
# Now we have segments [0,2] + [2,8] + [8,10] which cover the sporting event [0, 10]. | ||
# | ||
# Example 2: | ||
# | ||
# Input: clips = [[0,1],[1,2]], time = 5 | ||
# Output: -1 | ||
# Explanation: We cannot cover [0,5] with only [0,1] and [1,2]. | ||
# | ||
# Example 3: | ||
# | ||
# Input: clips = [[0,1],[6,8],[0,2],[5,6],[0,4],[0,3],[6,7],[1,3],[4,7],[1,4],[2,5],[2,6],[3,4],[4,5],[5,7],[6,9]], time = 9 | ||
# Output: 3 | ||
# Explanation: We can take clips [0,4], [4,7], and [6,9]. | ||
# | ||
# Constraints: | ||
# | ||
# 1 <= clips.length <= 100 | ||
# 0 <= start_i <= end_i <= 100 | ||
# 1 <= time <= 100 | ||
|
||
# https://leetcode.com/problems/video-stitching/discuss/484877/Python-(24-ms-beats-99)-Jump-Game-II-O(N)-time-O(1)-memory | ||
# Idea | ||
# Convert clips to the furthest point you can jump from each point. O(N) | ||
# Do a jump game O(N). | ||
# | ||
# e.g. clips = [[0,2],[4,6],[8,10],[1,9],[1,5],[5,9]], time = 10 | ||
# max_jumps = [2, 9, 0, 0, 6, 9, 0, 0, 10, 0, 0] | ||
# lo hi | ||
# 0 0 | ||
# 0 2 | ||
# 2 9 | ||
class Solution: | ||
def videoStitching(self, clips: List[List[int]], time: int) -> int: | ||
max_jumps = [0] * 101 | ||
for l, r in clips: | ||
max_jumps[l] = max(max_jumps[l], r) | ||
# print(max_jumps) | ||
|
||
# It is then a jump game | ||
count = 0 | ||
lo = hi = 0 | ||
while hi < time: | ||
# print(lo, hi) | ||
lo, hi = hi, max(max_jumps[lo : hi + 1]) | ||
if hi <= lo: | ||
return -1 | ||
count += 1 | ||
return count |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.