Hugo's Blog

(Updating) LeetCode November 2024 Daily Challenge by Hugo

November 30, 2024 (6d ago)

Hey there 👋! This is Hugo. This is my first time participating in the LeetCode Daily Challenge. Below is a list of the problems I've solved in November 2024. I'll continue updating the notes and solutions until the end of the month.

1957. Delete Characters to Make Fancy String [Easy] - 2024-11-01

1/** 2 * Author: 1chooo<hugo970217@gmail.com> 3 * Problem: https://leetcode.com/problems/delete-characters-to-make-fancy-string 4 * Runtime: 15ms (93.51%) 5 */ 6 7const static auto _ = []() { 8 cin.tie(nullptr)->sync_with_stdio(false); 9 return nullptr; 10}(); 11 12class Solution { 13 public: 14 string makeFancyString(string s) { 15 int count = 1; 16 int len = s.size(); 17 string ans = ""; 18 ans.push_back(s[0]); 19 20 for (int i = 1; i < len; i++) { 21 if (s[i] != ans.back()) { 22 count = 1; 23 ans.push_back(s[i]); 24 } else if (++count < 3) { 25 ans.push_back(s[i]); 26 } 27 } 28 29 return ans; 30 } 31};

2490. Circular Sentence [Easy] - 2024-11-02

1/** 2 * Author: 1chooo<hugo970217@gmail.com> 3 * Problem: https://leetcode.com/problems/circular-sentence 4 * Runtime: 0ms (100.00%) 5 */ 6 7const static auto _ = []() { 8 cin.tie(nullptr)->sync_with_stdio(false); 9 return nullptr; 10}(); 11 12class Solution { 13 public: 14 bool isCircularSentence(string sentence) { 15 int len = sentence.size(); 16 17 if (sentence[0] != sentence[len - 1]) 18 return false; 19 20 for (int i = 1; i < len - 1; i++) { 21 if (sentence[i] == ' ' && 22 sentence[i - 1] != sentence[i + 1]) 23 return false; 24 } 25 26 return true; 27 } 28};

796. Rotate String [Easy] - 2024-11-03

1/** 2 * Author: 1chooo<hugo970217@gmail.com> 3 * Problem: https://leetcode.com/problems/rotate-string 4 * Runtime: 0ms (100.00%) 5 */ 6 7const static auto _ = []() { 8 cin.tie(nullptr)->sync_with_stdio(false); 9 return nullptr; 10}(); 11 12class Solution { 13public: 14 bool rotateString(string s, string goal) { 15 if (s.length() != goal.length()) { 16 return false; 17 } 18 19 string rotated = s; 20 21 for (int i = 0; i < s.length(); i++) { 22 if (rotated == goal) 23 return true; 24 25 char ch = rotated[0]; 26 rotated.erase(0, 1); 27 rotated.push_back(ch); 28 } 29 return false; 30 } 31}; 32 33/** 34 * Author: 1chooo<hugo970217@gmail.com> 35 * Problem: https://leetcode.com/problems/rotate-string 36 * Runtime: 0ms (100.00%) 37 */ 38 39const static auto _ = []() { 40 cin.tie(nullptr)->sync_with_stdio(false); 41 return nullptr; 42}(); 43 44class Solution { 45public: 46 bool rotateString(string s, string goal) { 47 if (s.length() != goal.length()) { 48 return false; 49 } 50 51 string doubled = s + s; 52 53 return doubled.find(goal) != string::npos; 54 } 55};

3163. String Compression III [Medium] - 2024-11-04

1/** 2 * Author: 1chooo<hugo970217@gmail.com> 3 * Problem: https://leetcode.com/problems/string-compression-iii 4 * Runtime: 11ms (94.02%) 5 */ 6 7const static auto _ = []() { 8 cin.tie(nullptr)->sync_with_stdio(false); 9 return nullptr; 10}(); 11 12class Solution { 13 public: 14 string compressedString(string word) { 15 string result = ""; 16 char comp = word[0]; 17 int count = 1; 18 int len = word.size(); 19 20 for (int i = 1; i < len; i++) { 21 if (word[i] == comp && count < 9) { 22 count++; 23 } else { 24 result.push_back(count + '0'); 25 result.push_back(comp); 26 27 comp = word[i]; 28 count = 1; 29 } 30 } 31 32 result.push_back(count + '0'); 33 result.push_back(comp); 34 35 return result; 36 } 37};

2914. Minimum Number of Changes to Make Binary String Beautiful [Medium] - 2024-11-05

1/** 2 * Author: 1chooo<hugo970217@gmail.com> 3 * Problem: https://leetcode.com/problems/minimum-number-of-changes-to-make-binary-string-beautiful 4 * Runtime: 0ms (100.00%) 5 */ 6 7const static auto _ = []() { 8 cin.tie(nullptr)->sync_with_stdio(false); 9 return nullptr; 10}(); 11 12class Solution { 13 public: 14 int minChanges(string s) { 15 int len = s.size(); 16 int count = 0; 17 18 for (int i = 0; i < len - 1; i += 2) 19 if (s[i] != s[i + 1]) 20 count++; 21 22 return count; 23 } 24};

2257. Count Unguarded Cells in the Grid [Medium] - 2024-11-21

1/** 2 * Author: 1chooo<hugo970217@gmail.com> 3 * Problem: https://leetcode.com/problems/count-unguarded-cells-in-the-grid/ 4 * Runtime: 52ms (77.32%) 5 */ 6 7const static auto _ = []() { 8 cin.tie(nullptr)->sync_with_stdio(false); 9 return nullptr; 10}(); 11 12class Solution { 13 public: 14 int countUnguarded(int m, int n, vector<vector<int>> &guards, vector<vector<int>> &walls) { 15 vector<vector<int>> grid(m, vector<int>(n, 0)); 16 // 0 = free, 1 = guard, 2 = wall, 3 = guardable 17 for (const auto &guard : guards) { 18 grid[guard[0]][guard[1]] = 1; 19 } 20 for (const auto &wall : walls) { 21 grid[wall[0]][wall[1]] = 2; 22 } 23 auto mark_guarded = [&](int r, int c) { 24 for (int row = r + 1; row < m; row++) { 25 if (grid[row][c] == 1 || grid[row][c] == 2) break; 26 grid[row][c] = 3; 27 } 28 for (int row = r - 1; row >= 0; row--) { 29 if (grid[row][c] == 1 || grid[row][c] == 2) break; 30 grid[row][c] = 3; 31 } 32 for (int col = c + 1; col < n; col++) { 33 if (grid[r][col] == 1 || grid[r][col] == 2) break; 34 grid[r][col] = 3; 35 } 36 for (int col = c - 1; col >= 0; col--) { 37 if (grid[r][col] == 1 || grid[r][col] == 2) break; 38 grid[r][col] = 3; 39 } 40 }; 41 for (const auto &guard : guards) { 42 mark_guarded(guard[0], guard[1]); 43 } 44 int res = 0; 45 for (const auto &row : grid) { 46 for (int cell : row) { 47 if (cell == 0) res++; 48 } 49 } 50 return res; 51 } 52};

1072. Flip Columns For Maximum Number of Equal Rows [Medium] - 2024-11-22

1/** 2 * Author: 1chooo<hugo970217@gmail.com> 3 * Problem: https://leetcode.com/problems/flip-columns-for-maximum-number-of-equal-rows 4 * Runtime: 12ms (89.13%) 5 */ 6 7const static auto _ = []() { 8 cin.tie(nullptr)->sync_with_stdio(false); 9 return nullptr; 10}(); 11 12class Solution { 13 public: 14 int maxEqualRowsAfterFlips(vector<vector<int>> &matrix) { 15 unordered_map<string, int> patternCount; 16 17 for (const auto &row : matrix) { 18 string pattern = ""; 19 20 // Generate a pattern string for this row based on the first row 21 for (int j = 0; j < row.size(); ++j) { 22 // Compare with the first element in the row to determine if flip is needed 23 pattern += (row[j] ^ row[0]) ? '1' : '0'; 24 } 25 26 // Increment the count for this pattern 27 patternCount[pattern]++; 28 } 29 30 // Find the maximum count of rows with the same pattern 31 int maxRows = 0; 32 for (const auto &entry : patternCount) { 33 maxRows = max(maxRows, entry.second); 34 } 35 36 return maxRows; 37 } 38};

Comments