Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Must-know competitive programming problems with solutions and intuitive visualizations

NotificationsYou must be signed in to change notification settings

leduckhai/Awesome-Competitive-Programming

Repository files navigation

Please press ⭐ button if you like this repo. Thấy ngon thì nhấn star ⭐ ủng hộ mình nha các đồng râm

GitHub Trend

This repository contains my implementation ofuseful / well-known data structures, algorithms and solutions for awesome competitive programming problems inHackerrank, Project Euler and Leetcode

I create this for faster implementation and better preparation in interviews as well as programming contests:trollface::trollface:

⚠️ This repo is day-by-day updated. Please make sure you have the latest version!

🔥New updates today:Longest Substring Without Repeating Characters[Leetcode]

❓ How to use

Overview of the file:

🧨 Tips and Tricks

Here is some tips and tricks to ACE all competitive programming problems and interview questions:

If input array is sorted then    - Binary search    - Two pointersIf asked for all permutations/subsets then    - BacktrackingIf given a tree then    - DFS    - BFSIf given a graph then    - DFS    - BFSIf given a linked list then    - Two pointersIf recursion is banned then    - StackIf must solve in-place then    - Swap corresponding values    - Store one or more different values in the same pointerIf asked for maximum/minumum subarray/subset/options then    - Dynamic programmingIf asked for top/least K items then    - HeapIf asked for common strings then    - Map    - TrieElse    - Map/Set for O(1) time & O(n) space    - Sort input for O(nlogn) time and O(1) space

🍉 Data structures, Algorithms and Patterns

A) Data Structures Applications

🌴 Binary Search Tree Algorithm


B) String Algorithm

🌾 Suffix Tree - Suffix Array

    • Longest Palindromic Substring -O(n)
    • Pattern Search -O(log(n))

C) Searching and Graph Algorithms

❄️ Graph Theory

    • Graph Representation using Adjacency List: Weighted, Un-/Directed
    • Find All Nodes: Find All Nodes in the Unweighted Graph -O(V+E) for Adjacency List , V, E is the number of vertices and edges
    • Find All Edges

♻️ Detect Cycle

    • Detect Cycle: Disjoint Set: Detect Cycle in an Undirected Graph based on Disjoint Set (Union-Find) using "Union by Rank and Path Compression" -O(V)

✈️ Graph Traversal

    • Breadth-First Search: Find BFS Path from a Starting Node in Un-/Directed Graph -O(V+E) for Adjacency List; O(V2) for Adjacency Matrix , V, E is the number of vertices and edges
    • Depth-First Search: Find DFS Path from a Starting Node in Un-/Directed Graph -O(V+E) for Adjacency List; O(V2) for Adjacency Matrix , V, E is the number of vertices and edges

☘️ Minimum Spanning Tree (MST)

    • MST: Kruskal Algorithm[PDF]: Find Minimum Spanning Tree (MST) of an Undirected Graph using Kruskal Algorithm -O(E.log(E)) or O(E.log(V))

🛴 Shortest Path

Type of AlgorithmSubjects of ApplicationTime Complexity
Breadth-First SearchUnweighted, Un-/Directed GraphO(V+E) for Adjacency List
DijkstraNon-Negative Un-/Weighted Un-/Directed GraphO(E.log(V)) for Min-priority Queue
Bellman-Ford
Floyd-Warshall
    • Shortest Path: Breadth-First Search: Find the Shortest Path in a Unweighted Un-/Directed Graph based on BFS -O(V+E) for Adjacency List , V, E is the number of vertices and edges
    • Shortest Path: Bellman-Ford
    • Shortest Path: Floyd-Warshall

D) Greedy Algorithm

  1. Sherlock and The Beast
  • Find the "Decent Number" having n Digits ("Decent Number" has its digits to be only 3's and/or 5's; the number of 3's it contains is divisible by 5; the number of 5's it contains is divisible by 3; and it is the largest such number for its length)
  1. Largest Permutation
  • Swap 2 digits of a number k times to get largest number -O(n)

E) Dynamic Programming

Coin Change Algorithms: Given an array of choices, every choice is pickedunlimited times

Knapsack Problems: Given an array of choices, every choice is pickedonly once

💰 Coin Change Algorithms

    • Coin Change[PDF]: How many ways to pay V money using C coins [C1,C2,...Cn] -O(C.V)

👜 Knapsack Problems

    • Knapsack 0/1[Wiki]: Given a List of Weights associated with their Values, find the Founding Weights and Maximum Total Value attained with its Total Weight <= Given Total Weight, each Weight is onlypicked once (0/1 Rule) -O(N.W) , N, W is length of weights array and given total weight
    • Partition Problem: Subset Sum[Wiki]: Given an Array containing only Positive Integers, find if it can be Partitioned into 2 Subsets having Sum of elements in both subsets is Equal. -O(N.T) , N, T is the length of numbers array and the target sum (=sum/2)
    • Partition Problem: Multiway Number Partitioning[Wiki]:

📈 Path Sum Problems

Subsequence = Any subset of an array/string

Subarray = Contiguous subsequence of an array

Substring = Contiguous subsequence of a string

📅 Subarray Problems

🍡 Subsequence Problems

📃 Substring Problems


F) Two Pointers - Sliding Window

📖 Non-categorized


G) Mathematics

📘 Binomial Coefficient Problems

    • Pascal Triangle: Create Pascal Triangle (to Calculate Multiple Large-Number Combinations) -O(n2)

📕 Factors Problems

📗 Multiples Problems

📓 Permutation Problems

    • Permutation Check: Check if 2 Numbers/Strings are Permutations -O(n) , n = max(|a|,|b|)

📙 Primes Problems

📔 Primes-Factors Problems

    • Euler Totient Function (Number List): Find ALL Numbers of Coprimes < n based on Euler Totient Function -O((l) + m.loglogm + l.(logm + k)) , k is the number of prime factors of n; m and l is max value and length of the input number list

📒 Pythagorean Theorem

📖 Non-categorized


H) Linked List

🐍 Singly-linked List

Problem statement: Do something on a given singly-linked List

Hints:

  1. Simply convert linked list to 1D array and solve on the array using "ListNode_to_Array"
  2. If necessary, convert 1D array back to the linked list using "Array_to_ListNode"
    • List Node: Create a Singly-linked List using "Node class"
    • ListNode to Array: Convert ListNode into Array, could be used as print to "see" inside a ListNode -O(n)

I) Matrix

💻 Matrix x Simulation

Problem statement: Move current position within the matrix along diagonals, up-down-right-left, ...

Hints:

  1. Use variable flag = 'up' or 'down' ... to guide the current position to the next position
ifflag=='up':# Do something# Consider to change the flag = 'down' or 'right' or 'something'
  1. Pay attention to the matrix boundary when moving
definMatrix(r,c):# R, C is the matrix size# r, c is current positionifr>=0andc>=0andr<Randc<C:returnTrueelse:returnFalse
  1. Use variable seen = set() to not move to the seen position or to not start as a current position
if (r,c)notinseen:# Do something

Matrix x Graph


Recursion Algorithm

Backtracking

Divide and Conquer


[8]ページ先頭

©2009-2025 Movatter.jp