{"id":22180484,"url":"https://github.com/levelopers/algorithm","last_synced_at":"2025-03-24T18:45:53.247Z","repository":{"id":102422972,"uuid":"466075392","full_name":"levelopers/algorithm","owner":"levelopers","description":null,"archived":false,"fork":false,"pushed_at":"2022-06-12T17:27:24.000Z","size":23,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-01-29T23:29:46.437Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/levelopers.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-03-04T10:13:38.000Z","updated_at":"2022-03-05T17:03:22.000Z","dependencies_parsed_at":null,"dependency_job_id":"b37999fe-ca36-4c3e-9de9-7ae182524a95","html_url":"https://github.com/levelopers/algorithm","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/levelopers%2Falgorithm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/levelopers%2Falgorithm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/levelopers%2Falgorithm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/levelopers%2Falgorithm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/levelopers","download_url":"https://codeload.github.com/levelopers/algorithm/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245331891,"owners_count":20598060,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":[],"created_at":"2024-12-02T09:18:24.903Z","updated_at":"2025-03-24T18:45:53.239Z","avatar_url":"https://github.com/levelopers.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# common algorithm \u0026 data structures\n\n## data structures\n---\n\n[list](./data-structure/linkedList)\n- [linked list](./data-structure/linkedList/LinkedList.js)\n- [doubley linked list](./data-structure/linkedList/DoublyLinkedList.js)\n\n[queue](./data-structure/queue/Queue.js)\n- [priority queue](./data-structure/queue/PriorityQueue.js)\n- [mono queue](./data-structure/queue/MonoQueue.js)\n\n[stack](./data-structure/stack/Stack.js)\n\n[heap](./data-structure/heap/Heap.js)\n\n[tree](./data-structure/tree)\n- [binary tree](./data-structure/tree/binaryTree/BinaryTreeNode.js)\n- [trie](./data-structure/tree/trie)\n\n[graph](./data-structure/graph)\n\n\n\n## algorithm\n---\n\n### sorting\n| type | average time complexity | worst case time complexity | best case time complexity | space complexity |\n| --- | --- | --- | --- | --- |\n| bubbleSort | O(n^2) | O(n^2) | O(n) | O(1) |\n| selectionSort | O(n^2) | O(n^2) | O(n^2) | O(1) |\n| insertionSort | O(n^2) | O(n^2) | O(n) | O(1) |\n| quickSort | O(nlogn) | O(n^2) | O(nlogn) | O(nlogn) |\n| mergeSort | O(nlogn) | O(nlogn) | O(nlogn) | O(n) |\n\n### searching\n| type | average time complexity | worst case time complexity | best case time complexity | space complexity |\n| --- | --- | --- | --- | --- |\n| binarySearch | O(logn) | O(logn) | O(1) | O(1) |\n\n### array \n- two pointers\n- sliding window\n- binary search\n\n### string\n- rotate\n- kmp\n\n### linked list \n- two pointers\n- fast slow pointers\n\n### tree\n- binary search\n\n## common algorithm problems\n\n### array\n- [remove element（移除元素）](#27)\n- [binary search（二分查找）](#704)\n- [squares of a sorted array（有序数组的平方）](#977)\n- [minimum size subarray sum（长度最小子数组）](#209)\n\n### linked list\n- [implement linked list（实现链表及操作）](./data-structure/linkedList/LinkedList.js)\n- [reverse linked list（反转链表）](#206)\n- [remove nth from end（删除倒数第n个节点）](#19)\n- [cycle list（环形链表）](#142)\n  \n### string\n- [reverse string（反转字符串）](#344)\n- [replace space（替换空格）](#tihuankongge)\n- [reverse words（反转字符串里的单词）](#151)\n- [rotate string（左旋字符串）](#zuoxuanzifuchuan)\n- [kmp](#28)\n\n### two pointer\n- [3sum（三数之和）](#15)\n\n### queue\n- [implement stack using queues（用队列实现栈）](#225)\n- [sliding window maximum(滑动窗口最大值)](#239)\n- [top k frequent elements(前 K 个高频元素)](#347)\n\n\n### stack\n- [implement queue using stacks（用栈实现队列）](#232)\n- [valid parentheses(有效的括号)](#20)\n- [evaluate reverse polish notation(逆波兰表达式求值)](#150)\n\n### tree\n- [traverse in-order, pre-order, post-order(前，中，后序遍历)](./data-structure/tree/binaryTree/BinaryTreeNode.js)\n- [BFS, DFS（广度，深度搜索）](./algorithm/searching/search.js)\n- [迷宫](#1091)\n- [invert binary tree(翻转二叉树)](#226)\n\n## algorithm categories (types)\n\n1. Recursive Algorithm: it’s an Algorithm that calls itself repeatedly until the problem is solved.\n    - fibonacci\n\n2. Divide and Conquer Algorithm: A divide-and-conquer algorithm recursively breaks down a problem into two or more sub-problems of the same or related type, until these become simple enough to be solved directly. \n    - merge sort\n\n3. Dynamic Programming Algorithm: a dynamic programming algorithm solves complex problems by breaking them into multiple simple subproblems and then it solves each of them once and then stores them for future use.\n    - climb stairs (fibonacci)\n\n4. Greedy Algorithm: These algorithms are used for solving optimization problems. In this algorithm, we find a locally optimum solution (without any regard for any consequence in future) and hope to find the optimal solution at the global level.\n    - Kruskal's algorithm\n5. Brute Force Algorithm: This is one of the simplest algorithms in the concept. A brute force algorithm blindly iterates all possible solutions to search one or more than one solution that may solve a function.\n\n6. Backtracking Algorithm: It solves problems recursively and tries to solve a problem by solving one piece of the problem at a time. If one of the solutions fail, we remove it and backtrack to find another solution.\n    - N Queens problem\n\n\n\n## leetcode question cheat sheet\n\n```\nIf input array is sorted then\n    - Binary search\n    - Two pointers\n\nIf asked for all permutations/subsets then\n    - Backtracking\n\nIf given a tree then\n    - DFS\n    - BFS\n\nIf given a graph then\n    - DFS\n    - BFS\n\nIf given a linked list then\n    - Two pointers\n\nIf recursion is banned then\n    - Stack\n\nIf must solve in-place then\n    - Swap corresponding values\n    - Store one or more different values in the same pointer\n\nIf asked for maximum/minimum subarray/subset/options then\n    - Dynamic programming\n\nIf asked for top/least K items then\n    - Heap\n\nIf asked for common strings then\n    - Map\n    - Trie\n\nElse\n    - Map/Set for O(1) time \u0026 O(n) space\n    - Sort input for O(nlogn) time and O(1) space\n```\n\n## 回溯(backtracking)\n\n```\nvoid backtracking(参数) {\n    if (终止条件) {\n        存放结果;\n        return;\n    }\n\n    for (选择：本层集合中元素（树中节点孩子的数量就是集合的大小）) {\n        处理节点;\n        backtracking(路径，选择列表); // 递归\n        回溯，撤销处理结果\n    }\n}\n```\n\n## leetcode patterns\n\n\n| question | question link | category | description |\n| --- | --- | --- | --- |\n| 268. 丢失的数字 | https://leetcode-cn.com/problems/missing-number/ | Array | sort array, if(nums[i]!==i) return i |\n|136. 只出现一次的数字|https://leetcode-cn.com/problems/single-number/|Array| sort array, step in 2, if adjacent values are in pair |\n|70. 爬楼梯|https://leetcode-cn.com/problems/climbing-stairs/| dynamic programming| f(n) = f(n-1) + f(n-2), memo store f(n) value\n|121. 买卖股票的最佳时机|https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/| greedy | tracking and compare minPrice and maxProfit in one iteration|\n|53. 最大子数组和|https://leetcode-cn.com/problems/maximum-subarray/|dynamic programming| max sum of subArray end with nums[i], clear sum when sum \u003c 0\n|141. 环形链表|https://leetcode-cn.com/problems/linked-list-cycle/| hashTable / slow\u0026fast pointer | track head and comparing of exsistence / slow pointer will meet fast pointer if there is a loop (fast two steps, slow one step, break loop when fast || fast.next == null)\n|203. 移除链表元素|https://leetcode-cn.com/problems/remove-linked-list-elements/| slow\u0026fast pointer| dummy.next = head, iterate if node.next.val = val -\u003e node.next = node.next.next\n|100. 相同的树|https://leetcode-cn.com/problems/same-tree/|DFS| two stack dfs two trees compare each node\n|53. 最大子数组和|https://leetcode-cn.com/problems/maximum-subarray/|dynamic programming| dp[i]=max{nums[i],dp[i−1]+nums[i]}|\n|\u003cspan id=\"704\"\u003e704. 二分查找\u003c/span\u003e|https://leetcode-cn.com/problems/binary-search/|binary search| binary search sorted array|\n|\u003cspan id=\"27\"\u003e27. 移除元素\u003c/span\u003e|https://leetcode-cn.com/problems/remove-element/|two pointer| remove and shift others upwards|\n|\u003cspan id=\"977\"\u003e977. 有序数组的平方\u003c/span\u003e|https://leetcode-cn.com/problems/squares-of-a-sorted-array/|two pointer| two pointers from first and last then compare|\n|\u003cspan id=\"209\"\u003e209. 长度最小的子数组\u003c/span\u003e|https://leetcode-cn.com/problems/minimum-size-subarray-sum/|sliding window| window on sub-array|\n|\u003cspan id=\"203\"\u003e203. 移除链表元素\u003c/span\u003e|https://leetcode-cn.com/problems/remove-linked-list-elements/|linkedlist| loop node.next and compare node.next.val==target|\n|\u003cspan id=\"206\"\u003e206. 反转链表\u003c/span\u003e|https://leetcode-cn.com/problems/reverse-linked-list/|linkedlist|  let temp = cur.next；cur.next = pre；pre = cur；cur = temp|\n|\u003cspan id=\"19\"\u003e19. 删除链表的倒数第 N 个结点\u003c/span\u003e|https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/|fast\u0026slow pointer| fast n steps forward slow pointer,return slow when fast reach the end|\n|\u003cspan id=\"142\"\u003e142. 环形链表 II\u003c/span\u003e|https://leetcode-cn.com/problems/linked-list-cycle-ii/|fast\u0026slow pointer| fast 2 steps, slow 1 step|\n|\u003cspan id=\"344\"\u003e344. 反转字符串\u003c/span\u003e|https://leetcode-cn.com/problems/reverse-string/|two pointers| point to first and last element|\n|\u003cspan id=\"tihuankongge\"\u003e剑指 Offer 05. 替换空格\u003c/span\u003e|https://leetcode-cn.com/problems/ti-huan-kong-ge-lcof/|two pointers| increase size to result, move two pinters|\n|\u003cspan id=\"151\"\u003e151. 颠倒字符串中的单词\u003c/span\u003e|https://leetcode-cn.com/problems/reverse-words-in-a-string/|two pointers| remove trailing space, reverse string, reverse each word|\n|\u003cspan id=\"zuoxuanzifuchuan\"\u003e剑指 Offer 58 - II. 左旋转字符串\u003c/span\u003e|https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/|array | reverse first k, reverse rest, reverse whole string|\n|\u003cspan id=\"28\"\u003e28. 实现 strStr()\u003c/span\u003e|https://leetcode-cn.com/problems/implement-strstr/|string kmp | kmp|\n|\u003cspan id=\"15\"\u003e15. 三数之和\u003c/span\u003e|https://leetcode-cn.com/problems/3sum/|two pointers | for loop, sum=nums[i]+nums[left]+nums[right] |\n|\u003cspan id=\"232\"\u003e232. 用栈实现队列\u003c/span\u003e|https://leetcode-cn.com/problems/implement-queue-using-stacks/|stack | use two stacks |\n|\u003cspan id=\"225\"\u003e225. 用队列实现栈\u003c/span\u003e|https://leetcode-cn.com/problems/implement-stack-using-queues/|queue | use two queue, leave 1 element in main queue1  |\n|\u003cspan id=\"20\"\u003e20. 有效的括号\u003c/span\u003e|https://leetcode-cn.com/problems/valid-parentheses/|stack | push open brackets to stack, return stack.length  |\n|\u003cspan id=\"150\"\u003e150. 逆波兰表达式求值\u003c/span\u003e|https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/|stack | push numbers to stack, calculate when math operations, push result to stack  |\n|\u003cspan id=\"239\"\u003e239. 滑动窗口最大值\u003c/span\u003e|https://leetcode-cn.com/problems/sliding-window-maximum/| queue | [mono queue](./data-structure/queue/MonoQueue.js)  |\n|\u003cspan id=\"347\"\u003e347. 前 K 个高频元素\u003c/span\u003e|https://leetcode-cn.com/problems/top-k-frequent-elements/| queue | [priority queue](./data-structure/queue/PriorityQueue.js)  |\n|\u003cspan id=\"226\"\u003e226. 翻转二叉树\u003c/span\u003e|https://leetcode-cn.com/problems/invert-binary-tree/| recursive | swap node.left, node.right |\n|\u003cspan id=\"1091\"\u003e1091. 二进制矩阵中的最短路径\u003c/span\u003e|https://leetcode.cn/problems/shortest-path-in-binary-matrix/| bfs | queue structured bfs, shortest path is the first reach the end in tree |","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flevelopers%2Falgorithm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flevelopers%2Falgorithm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flevelopers%2Falgorithm/lists"}