diff options
author | Jack Sangdahl <[email protected]> | 2024-07-09 18:22:48 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2024-07-09 18:22:48 -0600 |
commit | fc22e6f09ee7fe386c2d95b599649666df926f18 (patch) | |
tree | 112bf99c9e9ef9e4e1546ff1398afc34ba5c7d41 | |
parent | 3da2313c973529e042dbaf522166e10c3f3a94ae (diff) | |
download | leetcode-fc22e6f09ee7fe386c2d95b599649666df926f18.tar.gz leetcode-fc22e6f09ee7fe386c2d95b599649666df926f18.zip |
1598. crawler log folder
-rw-r--r-- | crawler_log_folder.c | 33 | ||||
-rw-r--r-- | crawler_log_folder.py | 28 |
2 files changed, 61 insertions, 0 deletions
diff --git a/crawler_log_folder.c b/crawler_log_folder.c new file mode 100644 index 0000000..b24fda4 --- /dev/null +++ b/crawler_log_folder.c @@ -0,0 +1,33 @@ +// 1598. Crawler Log Folder +#include "leetcode.h" + +/* + * the leetcode file system keeps a log each time some user performs a change + * folder operation. the operations are described below. "../" move to the + * parent folder of the current folder. if you are already in the main folder, + * remain in the same folder. "./" remain in the same folder. "x/" move to the + * child folder named 'x' which is guaranteed to exist. you are given a list of + * strings 'logs' where 'logs[i]' is the operation performed by the user at the + * i'th step. + */ + +int minOperations(char **logs, int logsSize) { + int steps = 0; + char *parent = "../", *curr = "./"; + for (int i = 0; i < logsSize; i++) { + if (logs[i] == parent) + steps = fmax(steps - 1, 0); + else if (logs[i] != curr) + steps++; + } + return steps; +} + +int main() { + char *l1[] = {"d1/", "d2/", "../", "d21/", "./"}, + *l2[] = {"d1/", "d2/", "./", "d3/", "../", "d31/"}, + *l3[] = {"d1/", "../", "../", "../"}; + printf("%d\n", minOperations((char **)l1, ARRAY_SIZE(l1))); // execpt: 2 + printf("%d\n", minOperations((char **)l2, ARRAY_SIZE(l2))); // execpt: 3 + printf("%d\n", minOperations((char **)l3, ARRAY_SIZE(l3))); // execpt: 0 +} diff --git a/crawler_log_folder.py b/crawler_log_folder.py new file mode 100644 index 0000000..da1f69a --- /dev/null +++ b/crawler_log_folder.py @@ -0,0 +1,28 @@ +# 1598. Crawler Log Folder +from functools import reduce + +""" +the leetcode file system keeps a log each time some user performs a change +folder operation. the operations are described below. "../" move to the +parent folder of the current folder. if you are already in the main folder, +remain in the same folder. "./" remain in the same folder. "x/" move to the +child folder named 'x' which is guaranteed to exist. you are given a list of +strings 'logs' where 'logs[i]' is the operation performed by the user at the +i'th step. +""" + + +class Solution(object): + def minOperations(self, logs): + """ + :type logs: List[str] + :rtype: int + """ + return reduce(lambda depth, folder: max(0, depth - folder.rfind(".")), logs, 0) + + +if __name__ == "__main__": + obj = Solution() + print(obj.minOperations(logs=["d1/", "d2/", "../", "d21/", "./"])) + print(obj.minOperations(logs=["d1/", "d2/", "./", "d3/", "../", "d31/"])) + print(obj.minOperations(logs=["d1/", "../", "../", "../"])) |