{"id":30779407,"url":"https://github.com/tuang3142/1337","last_synced_at":"2026-04-16T12:01:32.847Z","repository":{"id":49126267,"uuid":"363940955","full_name":"tuang3142/1337","owner":"tuang3142","description":"python/ruby solutions for leetcode problems","archived":false,"fork":false,"pushed_at":"2021-08-31T02:04:52.000Z","size":200,"stargazers_count":0,"open_issues_count":4,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-09-05T06:19:33.224Z","etag":null,"topics":["effort","leetcode","python","ruby"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tuang3142.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-05-03T13:35:40.000Z","updated_at":"2021-08-14T08:16:24.000Z","dependencies_parsed_at":"2022-09-05T02:50:47.398Z","dependency_job_id":null,"html_url":"https://github.com/tuang3142/1337","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/tuang3142/1337","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tuang3142%2F1337","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tuang3142%2F1337/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tuang3142%2F1337/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tuang3142%2F1337/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tuang3142","download_url":"https://codeload.github.com/tuang3142/1337/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tuang3142%2F1337/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31884929,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-16T11:36:10.202Z","status":"ssl_error","status_checked_at":"2026-04-16T11:36:09.652Z","response_time":69,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["effort","leetcode","python","ruby"],"created_at":"2025-09-05T06:11:13.931Z","updated_at":"2026-04-16T12:01:32.830Z","avatar_url":"https://github.com/tuang3142.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Why\n\nThis repo contains my solutions for [leetcode](http://leetcode.com/) problems. Occasionally, I'll write algorithms and data structures from scratch. The purpose is to practice and showcase clean code with explanation and complexity analysis. I primarily use Python over Ruby (which I use professionally) because Python supports a wider range of libraries for solving algorithmic problems.  \nEach solution comes with an explanation and test suites. I'll keep them brief as it takes great effort to write an \"easy-to-understand\" explanation and an equal effort to understand it. Again, the punch line is only short and clean code.\n\n# How to test\n\nUsing Vim (which you should) with the `vim-test` plugin, open a test file and run `:TestFile`. At the moment, I'm only writing python test.\n\n# Examples\n\n## 239. Sliding Window Maximum\n\n\n### tl;dr\n\n[Link to the original problem.](https://leetcode.com/problems/sliding-window-maximum/)  \nGiven an array of integers `A` and a window of size `k`, return the largest number in the window when moving it from left to right one index at a time.\n```\nInput: A = [1,3,-1,-3,5,3,6,7], k = 3\nOutput: [3,3,5,5,6,7]\n\nWindow position                Max\n----------------------------------\n[1  3  -1] -3  5  3  6  7       3\n 1 [3  -1  -3] 5  3  6  7       3\n 1  3 [-1  -3  5] 3  6  7       5\n 1  3  -1 [-3  5  3] 6  7       5\n 1  3  -1  -3 [5  3  6] 7       6\n 1  3  -1  -3  5 [3  6  7]      7\n```\n\n### Idea\n\nThis problem is more commonly known as [monotonic queue](http://www.algorithmsandme.com/monotonic-queue/). The idea is to create a queue and make sure the order is decreasing so that the first number in q is always the largest.\n\n### Complexity\n\nWith `n = len(A)`:\n- time: `O(n)`\n- space: `O(n)`\n\n\n### Code\n\n`solution.py`\n\n```python\nfrom collections import deque\n\nclass Solution:\n    def maxSlidingWindow(self, A, k):\n        qu = deque()\n        ret = []\n        for i in range(len(A)):\n            while qu and A[i] \u003e= A[qu[-1]]: # keep the qu in decreasing order\n                qu.pop()                    # make sure the first number in qu is the largest\n            qu.append(i)\n            if i \u003c k - 1:                   # appending the first k numbers to qu\n                continue\n            while qu and i - qu[0] + 1 \u003e k: # pop the first in qu if it is out of window range\n                qu.popleft()\n            ret.append(A[qu[0]])\n        return ret\n```\n\n`test.py`\n\n```python\nimport unittest\nfrom solution import Solution\n\nclass Test(unittest.TestCase):\n    def test_general_cases(self):\n        A = [1,3,-1,-3,5,3,6,7]\n        k = 3\n        self.assertEqual(Solution().maxSlidingWindow(A, k), [3,3,5,5,6,7])\n\n        A = [7,2,4]\n        k = 2\n        self.assertEqual(Solution().maxSlidingWindow(A, k), [7, 4])\n\n    def test_k_equal_1(self):\n        A = [-1, 2, 3]\n        k = 1\n        self.assertEqual(Solution().maxSlidingWindow(A, k), [-1, 2, 3])\n\n    def test_k_equal_array_length(self):\n        A = [-1, 2, 3]\n        k = 3\n        self.assertEqual(Solution().maxSlidingWindow(A, k), [3])\n\nif __name__ == '__main__':\n    unittest.main()\n```\n\n## 1367. Linked List in Binary Tree\n\n\n### tl;dr\n\n[Link to the problem statement.](https://leetcode.com/problems/linked-list-in-binary-tree/)  \nGiven the root of a binary tree `root` and the head of a linked list `head`, check if all the nodes in the linked list starting from the head correspond to some downward path connected in the tree.\n```\nInput: head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]\nOutput: true\nExplanation: blue nodes form a path in the tree that is \"equal\" to the linked list.\n```\n![visualized tree](https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png \"image from leetcode.com\")\n### Idea\n\nThis problem is similar to finding a pattern in a string. The brute force approach is acceptable. However, we can use [KMP algorithm](https://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm) to produce a much faster solution. The idea is when checking if a tree node is in the linked list, we don't need to start all over from beginning of the list.\n\n\n### Complexity\n\nwith `n = number_of_tree_nodes` and `k = number_of_list_nodes`:\n- time: `O(n + k)`\n- space: `O(k)` (because we need to transform the list into a k-sized array, which makes me wonder why they gave the list at the first place but well, hate the game dont hate the player)\n\n### Code\n\nThe python implementation for `Tree` and `LinkedList` can be found under `data_structure/`.  \n\n`solution.py`\n\n```python\nclass Solution:\n    def isSubPath(self, head, root):\n        A, lps = self.convert_to_array(head) # lps = longest \"prefix which is also suffix\"\n        def visit(node, i):\n            if i \u003e= len(A):\n                return True\n            if not node:\n                return False\n            if node.val == A[i]:\n                return(visit(node.left, i + 1) or visit(node.right, i + 1))\n            if i \u003e 0:\n                return visit(node, lps[i - 1])\n            return visit(node.left, 0) or visit(node.right, 0)\n\n        return visit(root, 0)\n\n    def convert_to_array(self, node):\n        A = []\n        while node:\n            A.append(node.val)\n            node = node.next\n        lps = [0] * len(A)\n        i, j = 1, 0\n        while i \u003c len(A):\n            if A[i] == A[j]:\n                lps[i] = j + 1\n                i, j = i + 1, j + 1\n            else:\n                if j \u003e 0: j = lps[j - 1]\n                else: i += 1\n        return A, lps\n```\n\n`test.py`\n\n```python\nimport sys, os\nsys.path.append(os.path.abspath(\"./data_structure\"))\nimport unittest\nfrom linked_list import LinkedList\nfrom tree import Tree\nfrom solution import Solution\n\nclass Test(unittest.TestCase):\n    def setUp(self):\n        self.isSubPath = Solution().isSubPath\n\n    def test_general(self):\n        head = LinkedList.convert_array([4,2,8])\n        root = Tree.convert_array([1,4,4,None,2,2,None,1,None,6,8,None,None,None,None,1,3])\n        self.assertEqual(self.isSubPath(head, root), True)\n\n        head = LinkedList.convert_array([1,4,2,6])\n        root = Tree.convert_array([1,4,4,None,2,2,None,1,None,6,8,None,None,None,None,1,3])\n        self.assertEqual(self.isSubPath(head, root), True)\n\n    def test_len_head_is_1(self):\n        head = LinkedList(7)\n        root = Tree.convert_array([1, 7])\n        self.assertEqual(self.isSubPath(head, root), True)\n\n    def test_head_is_longer_than_tree_depth(self):\n        head = LinkedList([1, 2, 3, 4, 5])\n        root = Tree.convert_array([1, 2])\n        self.assertEqual(self.isSubPath(head, root), False)\n\nif __name__ == '__main__':\n    unittest.main()\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftuang3142%2F1337","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftuang3142%2F1337","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftuang3142%2F1337/lists"}