Skip to content

Commit

Permalink
Update Table
Browse files Browse the repository at this point in the history
  • Loading branch information
hikjik committed Oct 27, 2023
1 parent 1f8365d commit 447fa7e
Show file tree
Hide file tree
Showing 12 changed files with 1,100 additions and 1,115 deletions.
2,024 changes: 1,012 additions & 1,012 deletions README.md

Large diffs are not rendered by default.

18 changes: 8 additions & 10 deletions leetcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,14 +744,9 @@ def update_readme_table() -> None:
break

file.write("\n")
file.write(
"| № | Title | Solution | Difficulty | Time " "| Space | Tags | Notes |\n"
)
file.write(
"| - | ----- | -------- | ---------- | ---- |" " ---- | ----- | ----- |\n"
)
file.write("| Title | Code | Difficulty | Time | Space | Tags | Notes |\n")
file.write("| ----- | :--: | :--------: | :--: | :---: | ---- | ----- |\n")

problems.sort(key=lambda x: int(x["ID"]), reverse=True)
colors = get_colors(saturation=0.6, value=0.5)
for problem in problems:
task_id = problem["ID"]
Expand All @@ -767,15 +762,18 @@ def update_readme_table() -> None:
badge(tag=tag, color=colors[TAGS.index(tag)]) for tag in problem["Tags"]
]
space = problem["Space"]
if space:
space = f"<nobr>{space}</nobr>"
time = problem["Time"]
if time:
time = f"<nobr>{time}</nobr>"
notes = problem["Notes"]
file.write(
f"| {task_id}. "
f"| [{title}]({url}) "
f"| [{task_id}. {title}]({url}) "
f"| [C++](./solutions/{slug}/solution.hpp) "
f"| {difficulty_badge} "
f"| {time} "
f"| {space}"
f"| {space} "
f"| {' '.join(tags)} "
f"| {notes} |\n"
)
Expand Down
36 changes: 18 additions & 18 deletions res/problems.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"Array",
"Hash Table"
],
"Space": "",
"Time": "",
"Space": "O(n)",
"Time": "O(n)",
"Notes": ""
},
{
Expand All @@ -22,8 +22,8 @@
"Math",
"Recursion"
],
"Space": "",
"Time": "",
"Space": "O(1)",
"Time": "O(n+m)",
"Notes": ""
},
{
Expand All @@ -36,8 +36,8 @@
"String",
"Sliding Window"
],
"Space": "",
"Time": "",
"Space": "O(1)",
"Time": "O(n)",
"Notes": ""
},
{
Expand All @@ -50,8 +50,8 @@
"Binary Search",
"Divide and Conquer"
],
"Space": "",
"Time": "",
"Space": "O(1)",
"Time": "O(log min(n,m))",
"Notes": ""
},
{
Expand All @@ -75,8 +75,8 @@
"Tags": [
"String"
],
"Space": "",
"Time": "",
"Space": "O(1)",
"Time": "O(n)",
"Notes": ""
},
{
Expand All @@ -87,8 +87,8 @@
"Tags": [
"Math"
],
"Space": "",
"Time": "",
"Space": "O(1)",
"Time": "O(log n)",
"Notes": ""
},
{
Expand All @@ -99,8 +99,8 @@
"Tags": [
"String"
],
"Space": "",
"Time": "",
"Space": "O(1)",
"Time": "O(n)",
"Notes": ""
},
{
Expand All @@ -111,8 +111,8 @@
"Tags": [
"Math"
],
"Space": "",
"Time": "",
"Space": "O(log n)",
"Time": "O(log n)",
"Notes": ""
},
{
Expand All @@ -125,8 +125,8 @@
"Dynamic Programming",
"Recursion"
],
"Space": "",
"Time": "",
"Space": "O(nm)",
"Time": "O(nm)",
"Notes": ""
},
{
Expand Down
16 changes: 6 additions & 10 deletions solutions/add-two-numbers/solution.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,22 @@
class Solution {
public:
static ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
auto dummy = ListNode();

ListNode dummy;
auto *node = &dummy;
int carry = 0;
auto node = &dummy;
while (l1 || l2 || carry) {
int sum = carry;
if (l1) {
sum += l1->val;
carry += l1->val;
l1 = l1->next;
}
if (l2) {
sum += l2->val;
carry += l2->val;
l2 = l2->next;
}

carry = sum / 10;
node->next = new ListNode(sum % 10);
node->next = new ListNode(carry % 10);
carry /= 10;
node = node->next;
}

return dummy.next;
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
class Solution {
public:
static int lengthOfLongestSubstring(std::string s) {
std::unordered_map<char, size_t> letters;
size_t start = 0;
size_t length = 0;
for (size_t i = 0; i < s.size(); ++i) {
if (letters.count(s[i])) {
start = std::max(start, letters[s[i]] + 1);
std::unordered_map<char, int> indexes;
int length = 0;
for (int i = 0, j = 0; j < std::ssize(s); ++j) {
if (indexes.contains(s[j])) {
i = std::max(i, indexes[s[j]] + 1);
}
letters[s[i]] = i;
length = std::max(length, i - start + 1);
indexes[s[j]] = j;
length = std::max(length, j - i + 1);
}
return length;
}
Expand Down
14 changes: 7 additions & 7 deletions solutions/median-of-two-sorted-arrays/solution.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@ class Solution {
public:
static double findMedianSortedArrays(const std::vector<int> &nums1,
const std::vector<int> &nums2) {
const int n1 = nums1.size(), n2 = nums2.size();
if (n1 > n2) {
const int n = nums1.size(), m = nums2.size();
if (n > m) {
return findMedianSortedArrays(nums2, nums1);
}

int left = 0, right = n1;
int left = 0, right = n;
while (left <= right) {
const auto l1 = left + (right - left) / 2;
const auto l2 = (n1 + n2 + 1) / 2 - l1;
const auto l2 = (n + m + 1) / 2 - l1;

const auto f1 = l1 ? nums1[l1 - 1] : INT_MIN;
const auto f2 = l1 < n1 ? nums1[l1] : INT_MAX;
const auto f2 = l1 < n ? nums1[l1] : INT_MAX;

const auto s1 = l2 ? nums2[l2 - 1] : INT_MIN;
const auto s2 = l2 < n2 ? nums2[l2] : INT_MAX;
const auto s2 = l2 < m ? nums2[l2] : INT_MAX;

if (f1 > s2) {
right = l1 - 1;
} else if (f2 < s1) {
left = l1 + 1;
} else {
if ((n1 + n2) % 2 == 0) {
if ((n + m) % 2 == 0) {
return (std::max(f1, s1) + std::min(f2, s2)) * 0.5;
} else {
return std::max(f1, s1);
Expand Down
13 changes: 5 additions & 8 deletions solutions/palindrome-number/solution.hpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
#pragma once

#include <algorithm>
#include <string>

class Solution {
public:
static bool isPalindrome(int x) {
const auto str = std::to_string(x);
for (size_t i = 0; i < str.size() / 2; ++i) {
if (str[i] != str[str.size() - i - 1]) {
return false;
}
}
return true;
static bool isPalindrome(int n) {
const auto str = std::to_string(n);
return std::equal(str.cbegin(), str.cbegin() + str.size() / 2,
str.crbegin());
}
};
12 changes: 6 additions & 6 deletions solutions/palindrome-number/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@

TEST_CASE("Simple") {
struct TestCase {
int x;
int n;
bool expected;
};

std::vector<TestCase> test_cases{
{
.x = 121,
.n = 121,
.expected = true,
},
{
.x = -121,
.n = -121,
.expected = false,
},
{
.x = 10,
.n = 10,
.expected = false,
},
};

for (const auto &[x, expected] : test_cases) {
const auto actual = Solution::isPalindrome(x);
for (const auto &[n, expected] : test_cases) {
const auto actual = Solution::isPalindrome(n);
REQUIRE(expected == actual);
}
}
19 changes: 8 additions & 11 deletions solutions/reverse-integer/solution.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,16 @@

class Solution {
public:
static int reverse(int x) {
int res = 0;
while (x) {
int d = x % 10;
if (res > INT_MAX / 10 || res == INT_MAX / 10 && d > 7) {
static int reverse(int n) {
int ans = 0;
while (n) {
const auto r = n % 10;
if (ans > INT_MAX / 10 || ans < INT_MIN / 10) {
return 0;
}
if (res < INT_MIN / 10 || res == INT_MIN / 10 && d < -8) {
return 0;
}
res = res * 10 + d;
x /= 10;
ans = ans * 10 + r;
n /= 10;
}
return res;
return ans;
}
};
12 changes: 6 additions & 6 deletions solutions/reverse-integer/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@

TEST_CASE("Simple") {
struct TestCase {
int x;
int n;
int expected;
};

std::vector<TestCase> test_cases{
{
.x = 123,
.n = 123,
.expected = 321,
},
{
.x = -123,
.n = -123,
.expected = -321,
},
{
.x = 120,
.n = 120,
.expected = 21,
},
};

for (const auto &[x, expected] : test_cases) {
const auto actual = Solution::reverse(x);
for (const auto &[n, expected] : test_cases) {
const auto actual = Solution::reverse(n);
REQUIRE(expected == actual);
}
}
12 changes: 5 additions & 7 deletions solutions/two-sum/solution.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@
class Solution {
public:
static std::vector<int> twoSum(const std::vector<int> &nums, int target) {
std::unordered_map<int, int> table;
int size = nums.size();
for (int i = 0; i < size; ++i) {
if (auto it = table.find(nums[i]); it != table.end()) {
return {it->second, i};
std::unordered_map<int, int> map;
for (int i = 0; i < std::ssize(nums); ++i) {
if (map.contains(nums[i])) {
return {map[nums[i]], i};
}
table[target - nums[i]] = i;
map[target - nums[i]] = i;
}

return {};
}
};
Loading

0 comments on commit 447fa7e

Please sign in to comment.