Convert Figma logo to code with AI

EndlessCheng logocodeforces-go

算法竞赛模板库 by 灵茶山艾府 💭💡🎈

6,917
705
6,917
8

Top Related Projects

🌍 针对小白的算法训练 | 包括四部分:①.大厂面经 ②.力扣图解 ③.千本开源电子书 ④.百张技术思维导图(项目花了上百小时,希望可以点 star 支持,🌹感谢~)推荐免费ChatGPT使用网站

✅ Solutions to LeetCode by Go, 100% test coverage, runtime beats 100% / LeetCode 题解

142,599

A curated list of awesome Go frameworks, libraries and software

算法模板,最科学的刷题方式,最快速的刷题路径,你值得拥有~

:memo: LeetCode of algorithms with golang solution(updating).

【未来服务器端编程语言】最全空降golang资料补给包(满血战斗),包含文章,书籍,作者论文,理论分析,开源框架,云原生,大佬视频,大厂实战分享ppt

Quick Overview

EndlessCheng/codeforces-go is a GitHub repository containing Go solutions for Codeforces problems. It provides a comprehensive collection of algorithmic solutions implemented in Go, serving as a valuable resource for competitive programmers and those preparing for coding interviews.

Pros

  • Extensive collection of Go solutions for Codeforces problems
  • Well-organized structure with solutions categorized by contest and problem number
  • Includes explanations and comments for many solutions, aiding in understanding
  • Regularly updated with new solutions and improvements

Cons

  • May not cover all Codeforces problems
  • Solutions might not always be the most optimal or efficient
  • Primarily focused on Go, limiting usefulness for users of other programming languages
  • Reliance on external solutions may hinder personal problem-solving skills development

Code Examples

  1. Binary Search implementation:
func binarySearch(arr []int, target int) int {
    left, right := 0, len(arr)-1
    for left <= right {
        mid := left + (right-left)/2
        if arr[mid] == target {
            return mid
        } else if arr[mid] < target {
            left = mid + 1
        } else {
            right = mid - 1
        }
    }
    return -1
}
  1. Depth-First Search (DFS) on a graph:
func dfs(graph map[int][]int, start int, visited map[int]bool) {
    visited[start] = true
    fmt.Printf("%d ", start)
    for _, neighbor := range graph[start] {
        if !visited[neighbor] {
            dfs(graph, neighbor, visited)
        }
    }
}
  1. Sieve of Eratosthenes for finding prime numbers:
func sieveOfEratosthenes(n int) []bool {
    isPrime := make([]bool, n+1)
    for i := 2; i <= n; i++ {
        isPrime[i] = true
    }
    for i := 2; i*i <= n; i++ {
        if isPrime[i] {
            for j := i * i; j <= n; j += i {
                isPrime[j] = false
            }
        }
    }
    return isPrime
}

Getting Started

To use the solutions from this repository:

  1. Clone the repository:

    git clone https://github.com/EndlessCheng/codeforces-go.git
    
  2. Navigate to the desired problem's solution:

    cd codeforces-go/problemset
    
  3. Run the Go file for the specific problem:

    go run problem_number.go
    

Remember to replace problem_number with the actual problem number you're interested in.

Competitor Comparisons

🌍 针对小白的算法训练 | 包括四部分:①.大厂面经 ②.力扣图解 ③.千本开源电子书 ④.百张技术思维导图(项目花了上百小时,希望可以点 star 支持,🌹感谢~)推荐免费ChatGPT使用网站

Pros of hello-algorithm

  • More comprehensive coverage of algorithms and data structures
  • Includes visual explanations and diagrams for better understanding
  • Offers content in multiple languages (Chinese and English)

Cons of hello-algorithm

  • Less focused on competitive programming compared to codeforces-go
  • May not have as many advanced or specialized algorithms
  • Updates less frequently than codeforces-go

Code Comparison

hello-algorithm (Python):

def binary_search(arr, target):
    left, right = 0, len(arr) - 1
    while left <= right:
        mid = (left + right) // 2
        if arr[mid] == target:
            return mid
        elif arr[mid] < target:
            left = mid + 1
        else:
            right = mid - 1
    return -1

codeforces-go (Go):

func binarySearch(a []int, x int) int {
    l, r := 0, len(a)
    for l < r {
        m := (l + r) >> 1
        if a[m] >= x {
            r = m
        } else {
            l = m + 1
        }
    }
    return l
}

Both repositories provide implementations of common algorithms, but codeforces-go focuses more on competitive programming solutions, while hello-algorithm offers a broader range of algorithms and data structures with explanations. The code comparison shows similar implementations of binary search in different languages, reflecting the repositories' focus on algorithm implementations.

✅ Solutions to LeetCode by Go, 100% test coverage, runtime beats 100% / LeetCode 题解

Pros of LeetCode-Go

  • More comprehensive coverage of LeetCode problems
  • Better organization with problems categorized by difficulty and topics
  • Includes detailed explanations and time/space complexity analyses

Cons of LeetCode-Go

  • Focuses solely on LeetCode, limiting its scope compared to codeforces-go
  • May not include as many advanced algorithmic techniques used in competitive programming

Code Comparison

LeetCode-Go:

func twoSum(nums []int, target int) []int {
    m := make(map[int]int)
    for i, num := range nums {
        if j, ok := m[target-num]; ok {
            return []int{j, i}
        }
        m[num] = i
    }
    return nil
}

codeforces-go:

func solve(n int, a []int) int {
    sort.Ints(a)
    ans := 0
    for i := 0; i < n; i++ {
        ans += abs(a[i] - (i + 1))
    }
    return ans
}

The code examples showcase different problem-solving approaches. LeetCode-Go focuses on common interview questions, while codeforces-go addresses more competitive programming-style problems. Both repositories provide valuable resources for Go developers looking to improve their algorithmic skills, with LeetCode-Go being more suitable for interview preparation and codeforces-go for competitive programming practice.

142,599

A curated list of awesome Go frameworks, libraries and software

Pros of awesome-go

  • Comprehensive curated list of Go libraries, tools, and resources
  • Regularly updated with community contributions
  • Well-organized into categories for easy navigation

Cons of awesome-go

  • Not focused on competitive programming or algorithm solutions
  • Lacks actual code implementations or examples
  • May be overwhelming for beginners due to the sheer volume of resources

Code comparison

codeforces-go:

func solve(n int, a []int) int {
    sort.Ints(a)
    ans := 0
    for i := 0; i < n; i++ {
        ans += abs(a[i] - (i + 1))
    }
    return ans
}

awesome-go:

No direct code comparison available, as awesome-go is a curated list of resources rather than a code repository.

Summary

codeforces-go is a repository focused on competitive programming solutions in Go, providing implementations for various algorithms and data structures. On the other hand, awesome-go is a curated list of Go libraries, tools, and resources, serving as a comprehensive reference for Go developers. While codeforces-go offers practical code examples for algorithmic problems, awesome-go provides a broader overview of the Go ecosystem without specific code implementations.

算法模板,最科学的刷题方式,最快速的刷题路径,你值得拥有~

Pros of algorithm-pattern

  • Focuses on algorithm patterns and provides a structured learning approach
  • Covers a wide range of common algorithms and data structures
  • Includes explanations and examples in multiple programming languages

Cons of algorithm-pattern

  • Less comprehensive in terms of problem-solving techniques specific to competitive programming
  • May not be as frequently updated with new problems and solutions
  • Lacks the extensive collection of Codeforces problem solutions

Code Comparison

algorithm-pattern (Go implementation of binary search):

func binarySearch(nums []int, target int) int {
    left, right := 0, len(nums)-1
    for left <= right {
        mid := left + (right-left)>>1
        if nums[mid] == target {
            return mid
        } else if nums[mid] < target {
            left = mid + 1
        } else {
            right = mid - 1
        }
    }
    return -1
}

codeforces-go (Binary search implementation from a Codeforces problem):

func f(m int) bool {
    // Problem-specific condition
}

func binarySearch() int {
    l, r := 0, 1e9
    for l < r {
        m := (l + r) >> 1
        if f(m) {
            r = m
        } else {
            l = m + 1
        }
    }
    return l
}

The code comparison shows that codeforces-go focuses more on competitive programming-specific implementations, while algorithm-pattern provides a more general approach to algorithms.

:memo: LeetCode of algorithms with golang solution(updating).

Pros of awesome-golang-algorithm

  • Broader coverage of algorithms and data structures beyond competitive programming
  • More organized structure with categorized algorithms
  • Includes explanations and comments for better understanding

Cons of awesome-golang-algorithm

  • Less focused on specific competitive programming platforms
  • May not cover as many advanced or niche algorithms used in competitions
  • Updates might be less frequent compared to codeforces-go

Code Comparison

codeforces-go:

func solve(n int, a []int) int {
    sort.Ints(a)
    return a[n/2]
}

awesome-golang-algorithm:

func BinarySearch(arr []int, target int) int {
    left, right := 0, len(arr)-1
    for left <= right {
        mid := left + (right-left)/2
        if arr[mid] == target {
            return mid
        } else if arr[mid] < target {
            left = mid + 1
        } else {
            right = mid - 1
        }
    }
    return -1
}

The codeforces-go example shows a concise solution for a specific problem, while the awesome-golang-algorithm example demonstrates a more general-purpose implementation of a common algorithm with detailed steps.

【未来服务器端编程语言】最全空降golang资料补给包(满血战斗),包含文章,书籍,作者论文,理论分析,开源框架,云原生,大佬视频,大厂实战分享ppt

Pros of Introduction-to-Golang

  • Comprehensive learning resource for Go beginners
  • Covers a wide range of Go topics and concepts
  • Includes practical examples and exercises

Cons of Introduction-to-Golang

  • Less focused on competitive programming
  • May not provide advanced algorithmic solutions
  • Updates less frequently compared to codeforces-go

Code Comparison

Introduction-to-Golang (basic Go syntax):

package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}

codeforces-go (competitive programming solution):

package main

import (
    "bufio"
    "os"
)

func solve(in *bufio.Reader, out *bufio.Writer) {
    // Problem-specific solution
}

The Introduction-to-Golang repository is better suited for beginners learning Go from scratch, offering a structured approach to understanding the language. On the other hand, codeforces-go is tailored for competitive programmers, providing solutions to Codeforces problems and focusing on algorithmic challenges. While Introduction-to-Golang covers a broader range of topics, codeforces-go offers more specialized content for algorithm enthusiasts and those preparing for coding competitions.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

算法竞赛模板库 by 灵茶山艾府 💭💡🎈

算法 Algorithm

由于算法知识点繁杂,将自己学习到的算法、做过的题目分类整理好是有必要的。

一个算法模板应当涵盖以下几点:

  • 对该算法的基本介绍(核心思想、复杂度等)
  • 参考链接或书籍章节(讲得比较好的资料)
  • 模板代码(代码注释、使用说明)
  • 模板补充(常见题型中的额外代码、建模技巧等)
  • 相关题目(模板题、经典题、思维转换题等)

算法目录

不了解 Go?快速入门教程

算法题单

如何科学刷题?

  1. 滑动窗口与双指针(定长/不定长/单序列/双序列/三指针/分组循环)
  2. 二分算法(二分答案/最小化最大值/最大化最小值/第K小)
  3. 单调栈(基础/矩形面积/贡献法/最小字典序)
  4. 网格图(DFS/BFS/综合应用)
  5. 位运算(基础/性质/拆位/试填/恒等式/思维)
  6. 图论算法(DFS/BFS/拓扑排序/最短路/最小生成树/二分图/基环树/欧拉路径)
  7. 🔥动态规划(入门/背包/状态机/划分/区间/状压/数位/数据结构优化/树形/博弈/概率期望)
  8. 常用数据结构(前缀和/差分/栈/队列/堆/字典树/并查集/树状数组/线段树)
  9. 数学算法(数论/组合/概率期望/博弈/计算几何/随机算法)
  10. 贪心与思维(基本贪心策略/反悔/区间/字典序/数学/思维/脑筋急转弯/构造)
  11. 链表、二叉树与回溯(前后指针/快慢指针/DFS/BFS/直径/LCA/一般树)
  12. 字符串(KMP/Z函数/Manacher/字符串哈希/AC自动机/后缀数组/子序列自动机)

欢迎关注 B站@灵茶山艾府

如何选择题目 How to Choose Problems

Rating < 2100

这一阶段主要目标是提高对问题的观察能力。做构造题可以针对性地训练这一点。

选择难度在自己 rating 到 rating+200 范围内的构造题 (tag: constructive algorithms),按照过题人数降序做题,比如 [1700,1900] 区间的就是下面这个链接:

https://codeforces.com/problemset?order=BY_SOLVED_DESC&tags=constructive+algorithms%2C1700-1900

通过大量的构造题训练,提高观察能力,快速找到切题入口。具体见我在知乎上的这篇 回答。

Rating >= 2100(个人训练用,仅供参考)

见识更高的山、更广的海。

按人数从高到低,做 2200+ 的题目。建议不设置难度上限!由于按人数排序,难度分不会太高,不设上限可以避免错过高分好题。

我的 Codeforces 账号

0x3F

测试及对拍 Testing

编写一个 run(io.Reader, io.Writer) 函数来处理输入输出。这样写的理由是:

  • 在 main 中调用 run(os.Stdin, os.Stdout) 来执行代码;
  • 测试时,将测试数据转换成 strings.Reader 当作输入,并用一个 strings.Builder 来接收输出,将这二者传入 run 中,然后就能比较输出与答案了;
  • 对拍时需要实现一个暴力算法 runAC,参数和 run 一样。通过 随机数据生成器 来生成数据,分别传入 runAC 和 run,通过比对各自的输出,来检查 run 中的问题。

具体可以见 Codeforces 代码仓库 main,所有非交互题的代码及其对应测试全部按照上述框架实现。

例如:1439C_test.go

交互题的写法要复杂一些,需要把涉及输入输出的地方抽象成接口,详见 interactive_problem。

学习资料及题目 Resources

注:由于入门经典上选了很多区域赛的题,一部分题目可以在 GYM 上找到,这样可以就可以用 Go 编程提交了。

算法竞赛入门经典(第二版)

算法竞赛入门经典训练指南

算法竞赛入门经典训练指南(升级版)

算法竞赛进阶指南

算法竞赛入门到进阶

《算法竞赛》配套题单

国家集训队论文列表

算法竞赛 (ICPC, OI, etc) 论文,课件,文档,笔记等

算法竞赛课件分享 by hzwer

算法第四版 Java 源码

数据结构和算法动态可视化

OI Wiki

CP-Algorithms

The Ultimate Topic List (with Resources, Problems and Templates)

A Huge Update on The Ultimate Topic List

洛谷日报

All the good tutorials found for Competitive Programming

Codeforces Problem Topics

The Ultimate Topic List(with Tutorials, Problems, and Templates)

GeeksforGeeks 上的算法合集

Pepcy 模板

F0RE1GNERS 模板

https://github.com/hh2048/XCPC 含 jiangly 模板

https://www.cnblogs.com/alex-wei/p/contents.html

【模板整合计划】目录

算法学习笔记(目录)

洛谷模板题(建议按难度筛选)

能力全面提升综合题单

Luogu Problem List

洛谷原试炼场

Links of ICPC/CCPC Contests from China

AtCoder 题目分类

AtCoder 版《挑战程序设计竞赛》

AtCoder 版!蟻本 (初級編)

AtCoder 版!蟻本 (中級編)

AtCoder 版!蟻本 (上級編)

AtCoder 版!蟻本 (発展的トピック編)

待整理

【杂文】记一些有用的神奇网站

偶然在 GitHub 上发现的超长列表

算法竞赛训练中较难的部分

算法竞赛中可能不太会遇到的论文题

[杂谈]OI/ACM中冷门算法

Things I don't know

[meme] If you know at least 3 of these things and you are not red — you are doing it wrong. Stop learning useless algorithms, go and solve some problems, learn how to use binary search.

https://blog.csdn.net/calabash_boy/article/details/79973483

https://github.com/zimpha/algorithmic-library

https://www.luogu.com.cn/blog/command-block/blog-suo-yin-zhi-ding-post

https://wcysai.github.io/

https://www.luogu.com.cn/blog/Troverld/index

C++ @cache

其他 Others

My GoLand Live Templates and Postfix Completion settings

Useful Tools

查看汇编

GeoGebra

Draw Geometry

Draw Graph

OEIS

Wolfram|Alpha

ACD Ladders

Contests Filter

Codeforced

Codeforces Visualizer

Codeforces Solve Tracker

Another Codeforces Solve Tracker

AtCoder Problems

AtCoder Companions

AtCoder-Codeforces Rating converter

在线 Markdown + LaTeX

Rating and Difficulties

Open Codeforces Rating System

How to Interpret Contest Ratings

Codeforces: Problem Difficulties

Elo rating system

Stay Healthy

Exercises!