-
-
Notifications
You must be signed in to change notification settings - Fork 7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: added LeetCode problem 1,2 #2450
Open
nageshnnazare
wants to merge
16
commits into
TheAlgorithms:master
Choose a base branch
from
nageshnnazare:leetcode-cpp
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
c4e557f
feat: add LeetCode problem 1,2
nageshnnazare 505eb91
chore: apply suggestions from code review
Panquesito7 7435ff1
chore: apply suggestions from code review
Panquesito7 ce1698f
feat: added github workflow action file for leetcode Directory.md file
nageshnnazare 3f15ce2
Merge remote-tracking branch 'refs/remotes/origin/leetcode-cpp' into …
nageshnnazare c994028
Merge branch 'master' into leetcode-cpp
nageshnnazare 2a6c418
added leetcode-python script for directory file generation
nageshnnazare 29535e6
Merge remote-tracking branch 'refs/remotes/origin/leetcode-cpp' into …
nageshnnazare 9b65a20
Update scripts/leetcode_directory_md.py
nageshnnazare 82e3e47
feat: added header files to solutions to help CI
nageshnnazare 06f5713
Merge remote-tracking branch 'refs/remotes/origin/leetcode-cpp' into …
nageshnnazare cd6145c
Merge branch 'TheAlgorithms:master' into leetcode-cpp
nageshnnazare 079a161
feat: update the yml file for leetcode dir actions
nageshnnazare 76c791b
Merge remote-tracking branch 'refs/remotes/origin/leetcode-cpp' into …
nageshnnazare 24379ec
feat: fix for CI warnings in 2.cpp
nageshnnazare c54849d
feat: updated the yml and cpp files
nageshnnazare File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# The objective of this GitHub Action is to update the leetcode DIRECTORY.md file (if needed) | ||
# when doing a git push | ||
name: leetcode_directory_writer | ||
on: | ||
push: | ||
paths: | ||
- 'leetcode/src/**.cpp' | ||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
fetch-depth: 0 | ||
- uses: actions/setup-python@v4 | ||
with: | ||
python-version: 3.x | ||
- name: Add python dependencies | ||
run: | | ||
pip install requests | ||
- name: Commit and push changes | ||
uses: stefanzweifel/git-auto-commit-action@v4 | ||
id: commit-push | ||
with: | ||
commit_message: 'docs: updating `leetcode/DIRECTORY.md`' | ||
branch: 'leetcode-directory-${{ github.sha }}' | ||
create_branch: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# 📚 Contributing 📚 | ||
|
||
We're glad you're interested in adding C++ LeetCode solutions to the repository.\ | ||
Here we'll be explaining how to contribute to LeetCode solutions properly. | ||
|
||
## 💻 Cloning/setting up the project 💻 | ||
|
||
First off, you'll need to fork the repository [**here**](https://github.com/TheAlgorithms/C-Plus-Plus/fork).\ | ||
Then, you'll need to clone the repository to your local machine. | ||
|
||
```bash | ||
git clone https://github.com/your-username/C-Plus-Plus.git | ||
``` | ||
|
||
After that, you'll need to create a new branch for your solution. | ||
|
||
```bash | ||
git checkout -b solution/your-solution-name | ||
``` | ||
|
||
## 📝 Adding a new solution 📝 | ||
|
||
All LeetCode problems can be found [**here**](https://leetcode.com/problemset/all/).\ | ||
If you have a solution to any of these problems (which are not being **repeated**, that's great! Here are the steps: | ||
|
||
1. Add a new file in `leetcode/src` with the number of the problem. | ||
- For example: if the problem's number is 98, the filename should be `98.cpp`. | ||
2. Provide a small description of the solution at the top of the file. A function should go below that. For example: | ||
|
||
```c | ||
/** | ||
* Return an vector of vector of size returnSize. | ||
* The sizes of the vectors are returned as returnColumnSizes vector. | ||
* Note: Both returned vector and columnSizes vector must be dynamically allocated, assume caller calls free. | ||
*/ | ||
``` | ||
|
||
3. Do not provide a `main` function. Use the required standalone functions instead. | ||
4. Doxygen documentation isn't used in LeetCode solutions. Simple/small documentation or comments should be fine. | ||
5. Please include dependency libraries/headers such as `<iostream>`, `<vector>` along with the `Solution` class to the problem. This will help the CI to succeed without the dependency errors. | ||
|
||
> **Note** | ||
> There was a requirement to update the `leetcode/DIRECTORY.md` file with details of the solved problem. It's not required anymore. The information about the problem is fetched automatically throughout the LeetCode API. | ||
|
||
## 📦 Committing your changes 📦 | ||
|
||
Once you're done with adding a new LeetCode solution, it's time we make a pull request. | ||
|
||
1. First, stage your changes. | ||
|
||
```bash | ||
git add leetcode/src/98.cpp # Use `git add .` to stage all changes. | ||
``` | ||
|
||
2. Then, commit your changes. | ||
|
||
```bash | ||
git commit -m "feat: add LeetCode problem 98" -m "Commit description" # Optional | ||
``` | ||
|
||
3. Finally, push your changes to your forked repository. | ||
|
||
```bash | ||
git push origin solution/your-solution-name:solution/your-solution-name | ||
``` | ||
|
||
4. You're done now! You just have to make a [**pull request**](https://github.com/TheAlgorithms/C-Plus-Plus/compare). 🎉 | ||
|
||
If you need any help, don't hesitate to ask and join our [**Discord server**](https://the-algorithms.com/discord)! 🙂 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Given an array of integers nums and an integer target, return indices of the | ||
* two numbers such that they add up to target. | ||
* You may assume that each input would have exactly one solution, and you may not use the same element twice. | ||
*/ | ||
|
||
#include <vector> | ||
#include <unordered_map> | ||
class Solution { | ||
public: | ||
std::vector<int> twoSum(std::vector<int>& nums, int target) { | ||
std::unordered_map<int, int> mp; // value:index | ||
std::vector<int> res; | ||
|
||
for (int i = 0; i < nums.size(); i++) { | ||
int diff = target - nums[i]; | ||
|
||
if (mp.find(diff) != mp.end()) { | ||
res.push_back(mp[diff]); | ||
res.push_back(i); | ||
return res; | ||
} | ||
mp[nums[i]] = i; | ||
} | ||
return res; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* You are given two non - empty linked lists representing two non - negative integers. | ||
* The digits are stored in reverse order, and each of their nodes contains a single digit. | ||
* Add the two numbersand return the sum as a linked list. | ||
*/ | ||
|
||
#include <memory> | ||
|
||
/* | ||
* Definition for singly-linked list. | ||
*/ | ||
struct ListNode { | ||
int val; | ||
ListNode *next; | ||
explicit ListNode() : val(0), next(nullptr) {} | ||
ListNode(int x) : val(x), next(nullptr) {} | ||
ListNode(int x, ListNode *next) : val(x), next(next) {} | ||
}; | ||
|
||
class Solution { | ||
public: | ||
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { | ||
int carry = 0; | ||
std::unique_ptr<ListNode> head(new ListNode(0)); | ||
ListNode* sum = head.get(); | ||
|
||
while(l1 || l2) { | ||
int val = carry + (l1 ? l1->val : 0) + (l2 ? l2->val : 0); | ||
carry = val/10; | ||
sum->next = new ListNode(val%10); | ||
sum = sum->next; | ||
l1 = (l1 ? l1->next : nullptr); | ||
l2 = (l2 ? l2->next : nullptr); | ||
} | ||
if(carry){ | ||
sum->next = new ListNode(carry); | ||
} | ||
|
||
return head->next; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
#!/usr/bin/env python3 | ||
|
||
from dataclasses import dataclass | ||
from os import listdir | ||
from pathlib import Path | ||
|
||
import requests | ||
|
||
|
||
@dataclass | ||
class Task: | ||
"""The task dataclass. Container for task info""" | ||
|
||
id: str | ||
title: str | ||
solution: str | ||
difficulty: str | ||
|
||
|
||
def fetch_leetcode_folder_tasks(solutions_folder: Path) -> list[Task]: | ||
"""Fetch leetcode tasks from the Leetcode""" | ||
|
||
# Fetch tasks info from the leetcode API. | ||
resp = requests.get("https://leetcode.com/api/problems/algorithms/", timeout=10) | ||
content_dict = resp.json() | ||
|
||
raw_tasks_id_dict = {} | ||
|
||
for task in content_dict["stat_status_pairs"]: | ||
task_stat = task["stat"] | ||
raw_tasks_id_dict[str(task_stat["frontend_question_id"])] = task | ||
|
||
# Generate result tasks info to be inserted into the document | ||
tasks_list = [] | ||
|
||
difficulty = {1: "Easy", 2: "Medium", 3: "Hard"} | ||
|
||
for fl in listdir(solutions_folder): | ||
task_id = fl.split(".")[0] | ||
|
||
raw_task = raw_tasks_id_dict.get(task_id, None) | ||
if raw_task is None: | ||
continue | ||
|
||
raw_task_stat = raw_task["stat"] | ||
tasks_list.append( | ||
Task( | ||
id=f'{raw_task_stat["frontend_question_id"]}', | ||
title=f'[{raw_task_stat["question__title"]}](https://leetcode.com/problems/{raw_task_stat["question__title_slug"]})', | ||
solution=f"[C++](./src/{fl})", | ||
difficulty=f'{difficulty.get(raw_task["difficulty"]["level"], "")}', | ||
) | ||
) | ||
|
||
return tasks_list | ||
|
||
|
||
HEADER_ID = "#" | ||
HEADER_TITLE = "Title" | ||
HEADER_SOLUTION = "Solution" | ||
HEADER_DIFFICULTY = "Difficulty" | ||
SEPARATOR = "-" | ||
|
||
|
||
def print_directory_md(tasks_list: list[Task]) -> None: | ||
"""Print tasks into the stdout""" | ||
|
||
def get_max_len(get_item): | ||
return max(list(map(lambda x: len(get_item(x)), tasks_list))) | ||
|
||
id_max_length = max(get_max_len(lambda x: x.id), len(HEADER_ID)) | ||
title_max_length = max(get_max_len(lambda x: x.title), len(HEADER_TITLE)) | ||
solution_max_length = max(get_max_len(lambda x: x.solution), len(HEADER_SOLUTION)) | ||
difficulty_max_length = max( | ||
get_max_len(lambda x: x.difficulty), len(HEADER_DIFFICULTY) | ||
) | ||
|
||
def formatted_string(header, title, solution, difficulty): | ||
return ( | ||
f"| {header.rjust(id_max_length)} " | ||
+ f"| {title.ljust(title_max_length)} " | ||
+ f"| {solution.ljust(solution_max_length)} " | ||
+ f"| {difficulty.ljust(difficulty_max_length)} |" | ||
) | ||
|
||
tasks_rows = [] | ||
|
||
tasks_rows.append( | ||
formatted_string(HEADER_ID, HEADER_TITLE, HEADER_SOLUTION, HEADER_DIFFICULTY) | ||
) | ||
tasks_rows.append( | ||
formatted_string( | ||
id_max_length * SEPARATOR, | ||
title_max_length * SEPARATOR, | ||
solution_max_length * SEPARATOR, | ||
difficulty_max_length * SEPARATOR, | ||
) | ||
) | ||
|
||
tasks_list.sort(key=lambda x: int(x.id.strip())) | ||
|
||
for task in tasks_list: | ||
tasks_rows.append( | ||
formatted_string(task.id, task.title, task.solution, task.difficulty) | ||
) | ||
|
||
print( | ||
""" | ||
# LeetCode | ||
|
||
### LeetCode Algorithm | ||
""" | ||
) | ||
|
||
for item in tasks_rows: | ||
print(item) | ||
|
||
|
||
if __name__ == "__main__": | ||
top_dir = Path(".") | ||
solutions_folder = top_dir / "leetcode" / "src" | ||
|
||
tasks_list = fetch_leetcode_folder_tasks(solutions_folder) | ||
print_directory_md(tasks_list) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this for every push? 🤔 I think it suffices to have this for a pull_request instead... What do you think @Panquesito7?