diff --git a/.obsidian/graph.json b/.obsidian/graph.json index ece4695..ea07bbe 100644 --- a/.obsidian/graph.json +++ b/.obsidian/graph.json @@ -39,6 +39,6 @@ "repelStrength": 10, "linkStrength": 1, "linkDistance": 250, - "scale": 0.7463733455586773, + "scale": 1.3711753906745523, "close": true } \ No newline at end of file diff --git a/OJ notes/pages/Greedy Algorithm.md b/OJ notes/pages/Greedy Algorithm.md new file mode 100644 index 0000000..297d4f1 --- /dev/null +++ b/OJ notes/pages/Greedy Algorithm.md @@ -0,0 +1,35 @@ +# Greedy Algorithm + +2022-09-05 17:32 + +> ##### Algorithms: +> +> #algorithm #greedy +> +> ##### Difficulty: +> +> #CS_analysis +> +> ##### Additional tags: + +##### Related problems: + +```expander +tag:#coding_problem tag:# -tag:#template_remove_me +``` + +##### Links: + +- [cppreference]() + +--- + +### What is Greedy Algorithm? + +### How does Greedy Algorithm work? + +#### Example code + +### Why and when to use it? + +#template_remove_me diff --git a/OJ notes/pages/Leetcode Longest-Palindrome.md b/OJ notes/pages/Leetcode Longest-Palindrome.md new file mode 100644 index 0000000..321ecd0 --- /dev/null +++ b/OJ notes/pages/Leetcode Longest-Palindrome.md @@ -0,0 +1,108 @@ +# Leetcode Longest-Palindrome + +2022-09-05 17:27 + +> ##### Algorithms: +> +> #algorithm #greedy +> +> ##### Data structures: +> +> #DS #string +> +> ##### Difficulty: +> +> #coding_problem #difficulty-easy +> +> ##### Additional tags: +> +> #leetcode +> +> ##### Revisions: +> +> N/A + +##### Links: + +- [Link to problem](https://leetcode.com/problems/longest-palindrome/) + +--- + +### Problem + +Given a string `s` which consists of lowercase or uppercase letters, return _the length of the **longest palindrome**_ that can be built with those letters. + +Letters are **case sensitive**, for example, `"Aa"` is not considered a palindrome here. + +#### Examples + +**Example 1:** + +**Input:** s = "abccccdd" +**Output:** 7 +**Explanation:** One longest palindrome that can be built is "dccaccd", whose length is 7. + +**Example 2:** + +**Input:** s = "a" +**Output:** 1 +**Explanation:** The longest palindrome that can be built is "a", whose length is 1. + +#### Constraints + +- `1 <= s.length <= 2000` +- `s` consists of lowercase **and/or** uppercase English letters only. + +### Thoughts + +> [!summary] +> This is a #greedy problem. + +The solution is simple: count the apperances, sort it using +std::greater(), and select all even number, for odd +number `i`: +- if odd numbers has been used: `ans += i - 1` (make it + even) +- if not: `ans += i`, and mark odd number used + +### Solution + +```cpp +class Solution { +public: + int longestPalindrome(string s) { + int ans = 0; + bool used = false; + vector table(52, 0); + + // record in table. + for (char ch : s) { + if (ch <= 'Z') { + table[ch - 'A' + 26]++; + } else { + table[ch - 'a']++; + } + } + + sort(table.begin(), table.end(), greater()); + + for (int i : table) { + if (i % 2 == 1) { + if (used == false) { + used = true; + ans += i; + } else { + ans += i - 1; + } + } else { + if (i == 0) { + break; + } + ans += i; + } + } + + return ans; + } +}; +``` \ No newline at end of file