diff --git a/cpp/2203/220309-CN.cpp b/cpp/2203/220309-CN.cpp index 94c5839..5fa8387 100644 --- a/cpp/2203/220309-CN.cpp +++ b/cpp/2203/220309-CN.cpp @@ -2,6 +2,13 @@ #include #include +/** + * 798. Smallest Rotation with Highest Score + * You are given an array nums. You can rotate it by a non-negative integer k so that the array becomes [nums[k], nums[k + 1], ... nums[nums.length - 1], nums[0], nums[1], ..., nums[k-1]]. Afterward, any entries that are less than or equal to their index are worth one point. + * - For example, if we have nums = [2,4,1,3,0], and we rotate by k = 2, it becomes [1,3,0,2,4]. This is worth 3 points because 1 > 0 [no points], 3 > 1 [no points], 0 <= 2 [one point], 2 <= 3 [one point], 4 <= 4 [one point]. + * Return the rotation index k that corresponds to the highest score we can achieve if we rotated nums by it. If there are multiple answers, return the smallest such index k. + */ + class Solution { public: static int bestRotation(const std::vector& nums) { diff --git a/cpp/2203/220310.cpp b/cpp/2203/220310.cpp index 7355c5e..9f0c0b7 100644 --- a/cpp/2203/220310.cpp +++ b/cpp/2203/220310.cpp @@ -10,6 +10,12 @@ struct ListNode { explicit ListNode(int x = 0, ListNode* next = nullptr) : val(x), next(next) {} }; +/** + * 2. Add Two Numbers + * 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 numbers and return the sum as a linked list. + * You may assume the two numbers do not contain any leading zero, except the number 0 itself. + */ + class Solution { public: static ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { diff --git a/cpp/2203/220311-CN.cpp b/cpp/2203/220311-CN.cpp index 161bb80..d4bf128 100644 --- a/cpp/2203/220311-CN.cpp +++ b/cpp/2203/220311-CN.cpp @@ -2,6 +2,13 @@ #include #include +/** + * 2049. Count Nodes With the Highest Score + * There is a binary tree rooted at 0 consisting of n nodes. The nodes are labeled from 0 to n - 1. You are given a 0-indexed integer array parents representing the tree, where parents[i] is the parent of node i. Since node 0 is the root, parents[0] == -1. + * Each node has a score. To find the score of a node, consider if the node and the edges connected to it were removed. The tree would become one or more non-empty subtrees. The size of a subtree is the number of the nodes in it. The score of the node is the product of the sizes of all those subtrees. + * Return the number of nodes that have the highest score. + */ + class Solution { public: static int countHighestScoreNodes(const std::vector& parents) { diff --git a/cpp/2203/220313-CN.cpp b/cpp/2203/220313-CN.cpp index 4435a81..e82fa81 100644 --- a/cpp/2203/220313-CN.cpp +++ b/cpp/2203/220313-CN.cpp @@ -1,6 +1,15 @@ #include #include +/** + * 393. UTF-8 Validation + * Given an integer array data representing the data, return whether it is a valid UTF-8 encoding. + * A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules: + * - For a 1-byte character, the first bit is a 0, followed by its Unicode code. + * - For an n-bytes character, the first n bits are all one's, the n + 1 bit is 0, followed by n - 1 bytes with the most significant 2 bits being 10. + * Note: The input is an array of integers. Only the least significant 8 bits of each integer is used to store the data. This means each integer represents only 1 byte of data. + */ + class Solution { public: static bool validUtf8(const std::vector& data) { diff --git a/cpp/2203/220313.cpp b/cpp/2203/220313.cpp index f19f44b..a475c88 100644 --- a/cpp/2203/220313.cpp +++ b/cpp/2203/220313.cpp @@ -1,6 +1,14 @@ #include #include +/** + * 20. Valid Parentheses + * Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. + * An input string is valid if: + * 1. Open brackets must be closed by the same type of brackets. + * 2. Open brackets must be closed in the correct order. + */ + class Solution { public: static bool isValid(const std::string& s) { diff --git a/cpp/2203/220314-CN.cpp b/cpp/2203/220314-CN.cpp index 715e7a5..d5a3c82 100644 --- a/cpp/2203/220314-CN.cpp +++ b/cpp/2203/220314-CN.cpp @@ -2,6 +2,12 @@ #include #include +/** + * 599. Minimum Index Sum of Two Lists + * Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite restaurants represented by strings. + * You need to help them find out their common interest with the least list index sum. If there is a choice tie between answers, output all of them with no order requirement. You could assume there always exists an answer. + */ + class Solution { public: static std::vector findRestaurant(const std::vector& list1, const std::vector& list2) { diff --git a/cpp/2203/220315-CN.cpp b/cpp/2203/220315-CN.cpp index ae12a3e..559a7f1 100644 --- a/cpp/2203/220315-CN.cpp +++ b/cpp/2203/220315-CN.cpp @@ -1,5 +1,12 @@ #include +/** + * 2044. Count Number of Maximum Bitwise-OR Subsets + * Given an integer array nums, find the maximum possible bitwise OR of a subset of nums and return the number of different non-empty subsets with the maximum bitwise OR. + * An array a is a subset of an array b if a can be obtained from b by deleting some (possibly zero) elements of b. Two subsets are considered different if the indices of the elements chosen are different. + * The bitwise OR of an array a is equal to a[0] OR a[1] OR ... OR a[a.length - 1] (0-indexed). + */ + class Solution { public: static int countMaxOrSubsets(const std::vector& nums) { diff --git a/cpp/2203/220315.cpp b/cpp/2203/220315.cpp index 2cc6ce1..d39cf8d 100644 --- a/cpp/2203/220315.cpp +++ b/cpp/2203/220315.cpp @@ -2,6 +2,16 @@ #include #include +/** + * 1249. Minimum Remove to Make Valid Parentheses + * Given a string s of '(' , ')' and lowercase English characters. + * Your task is to remove the minimum number of parentheses ( '(' or ')', in any positions ) so that the resulting parentheses string is valid and return any valid string. + * Formally, a parentheses string is valid if and only if: + * - It is the empty string, contains only lowercase characters, or + * - It can be written as AB (A concatenated with B), where A and B are valid strings, or + * - It can be written as (A), where A is a valid string. + */ + class Solution { public: static std::string minRemoveToMakeValid(const std::string& s) { diff --git a/cpp/2203/220316-CN.cpp b/cpp/2203/220316-CN.cpp index 42b2ff6..1be307a 100644 --- a/cpp/2203/220316-CN.cpp +++ b/cpp/2203/220316-CN.cpp @@ -5,6 +5,17 @@ #include #include +/** + * 432. All O`one Data Structure + * Design a data structure to store the strings' count with the ability to return the strings with minimum and maximum counts. + * Implement the AllOne class: + * - AllOne() Initializes the object of the data structure. + * - inc(String key) Increments the count of the string key by 1. If key does not exist in the data structure, insert it with count 1. + * - dec(String key) Decrements the count of the string key by 1. If the count of key is 0 after the decrement, remove it from the data structure. It is guaranteed that key exists in the data structure before the decrement. + * - getMaxKey() Returns one of the keys with the maximal count. If no element exists, return an empty string "". + * - getMinKey() Returns one of the keys with the minimum count. If no element exists, return an empty string "". + */ + class AllOne { private: int n = 0; diff --git a/cpp/2203/220316.cpp b/cpp/2203/220316.cpp index cca0386..0e958e1 100644 --- a/cpp/2203/220316.cpp +++ b/cpp/2203/220316.cpp @@ -3,6 +3,11 @@ #include #include +/** + * 946. Validate Stack Sequences + * Given two integer arrays pushed and popped each with distinct values, return true if this could have been the result of a sequence of push and pop operations on an initially empty stack, or false otherwise. + */ + class Solution { public: static bool validateStackSequences(const std::vector& pushed, const std::vector& popped) { diff --git a/cpp/2203/220317-CN.cpp b/cpp/2203/220317-CN.cpp index 2286085..0e17e83 100644 --- a/cpp/2203/220317-CN.cpp +++ b/cpp/2203/220317-CN.cpp @@ -4,6 +4,12 @@ #include #include +/** + * 720. Longest Word in Dictionary + * Given an array of strings words representing an English Dictionary, return the longest word in words that can be built one character at a time by other words in words. + * If there is more than one possible answer, return the longest word with the smallest lexicographical order. If there is no answer, return the empty string. + */ + class Solution { public: static std::string longestWord(const std::vector& words) { diff --git a/cpp/2203/220317.cpp b/cpp/2203/220317.cpp index 0bc25eb..74b8b56 100644 --- a/cpp/2203/220317.cpp +++ b/cpp/2203/220317.cpp @@ -1,6 +1,15 @@ #include #include +/** + * 856. Score of Parentheses + * Given a balanced parentheses string s, return the score of the string. + * The score of a balanced parentheses string is based on the following rule: + * - "()" has score 1. + * - AB has score A + B, where A and B are balanced parentheses strings. + * - (A) has score 2 * A, where A is a balanced parentheses string. + */ + class Solution { public: static int scoreOfParentheses(const std::string& s) { diff --git a/cpp/2203/220319.cpp b/cpp/2203/220319.cpp index 6580690..167dc2c 100644 --- a/cpp/2203/220319.cpp +++ b/cpp/2203/220319.cpp @@ -7,6 +7,18 @@ #include #include +/** + * 895. Maximum Frequency Stack + * Design a stack-like data structure to push elements to the stack and pop the most frequent element from the stack. + * + * Implement the FreqStack class: + * + * - FreqStack() constructs an empty frequency stack. + * - void push(int val) pushes an integer val onto the top of the stack. + * - int pop() removes and returns the most frequent element in the stack. + * - If there is a tie for the most frequent element, the element closest to the stack's top is removed and returned. + */ + class FreqStack { private: std::unordered_map> s;