Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.14.8
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
### 📊 Repository Stats

[![Last Commit](https://img.shields.io/github/last-commit/mathusanm6/LeetCode?style=for-the-badge&logo=git&logoColor=white&color=blue)](https://github.com/mathusanm6/LeetCode/commits/main)
[![C++ Solutions](https://img.shields.io/badge/C%2B%2B%20Solutions-8-blue?style=for-the-badge&logo=cplusplus&logoColor=white)](https://github.com/mathusanm6/LeetCode/tree/main/problems)
[![Python Solutions](https://img.shields.io/badge/Python%20Solutions-8-blue?style=for-the-badge&logo=python&logoColor=white)](https://github.com/mathusanm6/LeetCode/tree/main/problems)
[![C++ Solutions](https://img.shields.io/badge/C%2B%2B%20Solutions-9-blue?style=for-the-badge&logo=cplusplus&logoColor=white)](https://github.com/mathusanm6/LeetCode/tree/main/problems)
[![Python Solutions](https://img.shields.io/badge/Python%20Solutions-9-blue?style=for-the-badge&logo=python&logoColor=white)](https://github.com/mathusanm6/LeetCode/tree/main/problems)

</div>

Expand Down Expand Up @@ -192,6 +192,7 @@ This repository covers a comprehensive range of algorithmic patterns and data st

| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
|---|-------|----------|------|-------|------------|-----|------|
| 88 | [Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/) | [Python](./problems/merge_sorted_array/merge_sorted_array.py), [C++](./problems/merge_sorted_array/merge_sorted_array.cc) | _O(m + n)_ | _O(1)_ | Easy | | |
| 125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [Python](./problems/valid_palindrome/valid_palindrome.py), [C++](./problems/valid_palindrome/valid_palindrome.cc) | _O(n)_ | _O(1)_ | Easy | | |

## Trees
Expand Down
2 changes: 1 addition & 1 deletion common/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .trees.treenode import TreeNode
from common.trees.treenode import TreeNode

__all__ = ["TreeNode"]
10 changes: 4 additions & 6 deletions problems/brace_expansion/brace_expansion.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import re

from typing import List

def braceExpansion(s: str) -> list[str]:
results: list[str] = []

def braceExpansion(s: str) -> List[str]:
results: List[str] = []

def backtrack(prefixes: List[str], remaining: str) -> None:
def backtrack(prefixes: list[str], remaining: str) -> None:
"""
Recursive helper that expands the string.

Expand Down Expand Up @@ -37,7 +35,7 @@ def backtrack(prefixes: List[str], remaining: str) -> None:

# If no prefixes yet, start from empty string
base_prefixes = prefixes if prefixes else [""]
new_prefixes: List[str] = []
new_prefixes: list[str] = []

# Build new partial expansions by combining each prefix with each choice
for prefix in base_prefixes:
Expand Down
1 change: 0 additions & 1 deletion problems/brace_expansion/brace_expansion_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Test cases for the brace_expansion function."""

import pytest

from brace_expansion import braceExpansion


Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
from typing import List


def calculateAmountPaidInTaxes(brackets: List[List[int]], income: int) -> float:
def calculateAmountPaidInTaxes(brackets: list[list[int]], income: int) -> float:
total_tax = 0.0

income_remaining = income
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Test cases for the calculate_amount_paid_in_taxes function."""

import pytest

from calculate_amount_paid_in_taxes import calculateAmountPaidInTaxes


Expand Down
5 changes: 1 addition & 4 deletions problems/contains_duplicate/contains_duplicate.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
from typing import List


def containsDuplicate(nums: List[int]) -> bool:
def containsDuplicate(nums: list[int]) -> bool:
seen = set()
for num in nums:
if num in seen:
Expand Down
1 change: 0 additions & 1 deletion problems/contains_duplicate/contains_duplicate_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Test cases for the contains_duplicate function."""

import pytest

from contains_duplicate import containsDuplicate


Expand Down
5 changes: 2 additions & 3 deletions problems/evaluate_division/evaluate_division.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from typing import List
from collections import defaultdict


def evaluateDivision(
equations: List[List[str]], values: List[float], queries: List[List[str]]
) -> List[float]:
equations: list[list[str]], values: list[float], queries: list[list[str]]
) -> list[float]:
def find(node: str) -> str:
if parent[node] != node:
original_parent = parent[node]
Expand Down
1 change: 0 additions & 1 deletion problems/evaluate_division/evaluate_division_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Test cases for the evaluate_division function."""

import pytest

from evaluate_division import evaluateDivision


Expand Down
4 changes: 1 addition & 3 deletions problems/group_anagrams/group_anagrams.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import collections

from typing import List


def groupAnagrams(strs: List[str]) -> List[List[str]]:
def groupAnagrams(strs: list[str]) -> list[list[str]]:
groups = collections.defaultdict(list)
for s in strs:
count = [0] * 26
Expand Down
1 change: 0 additions & 1 deletion problems/group_anagrams/group_anagrams_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Test cases for the group_anagrams function."""

import pytest

from group_anagrams import groupAnagrams


Expand Down
17 changes: 17 additions & 0 deletions problems/merge_sorted_array/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
problem:
number: 88
title: "Merge Sorted Array"
leetcode_url: "https://leetcode.com/problems/merge-sorted-array/"
difficulty: "easy"
tags: ["Two Pointers"]

solutions:
python: "problems/merge_sorted_array/merge_sorted_array.py"
cpp: "problems/merge_sorted_array/merge_sorted_array.cc"

complexity:
time: "O(m + n)"
space: "O(1)"

notes: ""
readme_link: ""
19 changes: 19 additions & 0 deletions problems/merge_sorted_array/merge_sorted_array.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "merge_sorted_array.h"
#include <vector>

void merge(std::vector<int>& nums1, int m, std::vector<int>& nums2, int n) {
int i = m - 1;
int j = n - 1;
int k = m + n - 1;

while (j >= 0) {
if (i >= 0 && nums1[i] >= nums2[j]) {
nums1[k] = nums1[i];
i -= 1;
} else {
nums1[k] = nums2[j];
j -= 1;
}
k -= 1;
}
}
5 changes: 5 additions & 0 deletions problems/merge_sorted_array/merge_sorted_array.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include <vector>

void merge(std::vector<int>& nums1, int m, std::vector<int>& nums2, int n);
16 changes: 16 additions & 0 deletions problems/merge_sorted_array/merge_sorted_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def merge(nums1: list[int], m: int, nums2: list[int], n: int) -> None:
"""
Do not return anything, modify nums1 in-place instead.
"""
i = m - 1
j = n - 1
k = m + n - 1

while j >= 0:
if i >= 0 and nums1[i] >= nums2[j]:
nums1[k] = nums1[i]
i -= 1
else:
nums1[k] = nums2[j]
j -= 1
k -= 1
47 changes: 47 additions & 0 deletions problems/merge_sorted_array/merge_sorted_array_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "merge_sorted_array.h"

#include <gtest/gtest.h>
#include <string>
#include <vector>

struct MergeSortedArrayCase {
std::string test_name;
std::vector<int> nums1;
int m;
std::vector<int> nums2;
int n;
std::vector<int> expected;
};

using MergeSortedArrayTest = ::testing::TestWithParam<MergeSortedArrayCase>;

TEST_P(MergeSortedArrayTest, TestCases) {
MergeSortedArrayCase testCase = GetParam();
merge(testCase.nums1, testCase.m, testCase.nums2, testCase.n);
EXPECT_EQ(testCase.nums1, testCase.expected);
}

INSTANTIATE_TEST_SUITE_P(
MergeSortedArrayTestCases, MergeSortedArrayTest,
::testing::Values(
MergeSortedArrayCase{.test_name = "basic_example",
.nums1 = {1, 2, 3, 0, 0, 0},
.m = 3,
.nums2 = {2, 5, 6},
.n = 3,
.expected = {1, 2, 2, 3, 5, 6}},
MergeSortedArrayCase{
.test_name = "both_empty", .nums1 = {}, .m = 0, .nums2 = {}, .n = 0, .expected = {}},
MergeSortedArrayCase{.test_name = "nums1_empty",
.nums1 = {0},
.m = 0,
.nums2 = {1},
.n = 1,
.expected = {1}},
MergeSortedArrayCase{.test_name = "nums2_empty",
.nums1 = {1},
.m = 1,
.nums2 = {},
.n = 0,
.expected = {1}}),
[](const testing::TestParamInfo<MergeSortedArrayCase> &info) { return info.param.test_name; });
24 changes: 24 additions & 0 deletions problems/merge_sorted_array/merge_sorted_array_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Test cases for the merge_sorted_array function."""

import pytest
from merge_sorted_array import merge


@pytest.mark.parametrize(
"nums1, m, nums2, n, expected",
[
([1, 2, 3, 0, 0, 0], 3, [2, 5, 6], 3, [1, 2, 2, 3, 5, 6]), # Basic case
([], 0, [], 0, []), # Empty arrays
([0], 0, [1], 1, [1]), # num1 is empty
([1], 1, [], 0, [1]), # num2 is empty
],
ids=[
"basic_case",
"both_empty",
"num1_empty",
"num2_empty",
],
)
def test_merge(nums1, m, nums2, n, expected):
merge(nums1, m, nums2, n)
assert nums1 == expected
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from common import TreeNode
from math import inf
from typing import Optional, Tuple

from common import TreeNode


def minimumFlips(root: Optional[TreeNode], result: bool) -> int:
def dfs(node: Optional[TreeNode]) -> Tuple[int, int]:
def minimumFlips(root: TreeNode | None, result: bool) -> int:
def dfs(node: TreeNode | None) -> tuple[int, int]:
"""Returns a tuple (min_flips_to_false, min_flips_to_true)"""
if node is None:
return inf, inf
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
root_dir = Path(__file__).parent.parent.parent
sys.path.insert(0, str(root_dir))

from common import TreeNode # noqa: E402
from minimum_flips_in_binary_tree_to_get_result import minimumFlips # noqa: E402
from minimum_flips_in_binary_tree_to_get_result import minimumFlips

from common import TreeNode


@pytest.mark.parametrize(
Expand Down
5 changes: 1 addition & 4 deletions problems/two_sum/two_sum.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
from typing import List


def twoSum(nums: List[int], target: int) -> List[int]:
def twoSum(nums: list[int], target: int) -> list[int]:
index_of = {}
for idx, num in enumerate(nums):
need = target - num
Expand Down
1 change: 0 additions & 1 deletion problems/two_sum/two_sum_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Test cases for the two_sum function."""

import pytest

from two_sum import twoSum


Expand Down
1 change: 0 additions & 1 deletion problems/valid_palindrome/valid_palindrome_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Test cases for the isPalindrome function."""

import pytest

from valid_palindrome import isPalindrome


Expand Down
Loading
Loading