From a4b2d5557f360d1369dd71081375b0c3c224cd65 Mon Sep 17 00:00:00 2001 From: Lam Haoyin Date: Fri, 7 Jan 2022 21:42:21 +0800 Subject: [PATCH] add: 220107 --- 2201/220107-CN.cpp | 15 +++++++++++++ 2201/220107.cpp | 54 +++++++++++++++++++++++++++++++++++++++++++++ 2201/CMakeLists.txt | 2 +- 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 2201/220107.cpp diff --git a/2201/220107-CN.cpp b/2201/220107-CN.cpp index 776f9a3..3a5369b 100644 --- a/2201/220107-CN.cpp +++ b/2201/220107-CN.cpp @@ -1,6 +1,21 @@ #include #include +/** + * 1614. Maximum Nesting Depth of the Parentheses + * A string is a valid parentheses string (denoted VPS) if it meets one of the following: + * It is an empty string "", or a single character not equal to "(" or ")", + * It can be written as AB (A concatenated with B), where A and B are VPS's, or + * It can be written as (A), where A is a VPS. + * We can similarly define the nesting depth depth(S) of any VPS S as follows: + * depth("") = 0 + * depth(C) = 0, where C is a string with a single character not equal to "(" or ")". + * depth(A + B) = max(depth(A), depth(B)), where A and B are VPS's. + * depth("(" + A + ")") = 1 + depth(A), where A is a VPS. + * For example, "", "()()", and "()(()())" are VPS's (with nesting depths 0, 1, and 2), and ")(" and "(()" are not VPS's. + * Given a VPS represented as string s, return the nesting depth of s. + */ + class Solution { public: static int maxDepth(const std::string& s) { diff --git a/2201/220107.cpp b/2201/220107.cpp new file mode 100644 index 0000000..e722914 --- /dev/null +++ b/2201/220107.cpp @@ -0,0 +1,54 @@ +#include +#include +#include + +/** + * 382. Linked List Random Node + * Given a singly linked list, return a random node's value from the linked list. Each node must have the same probability of being chosen. + * Implement the Solution class: + * Solution(ListNode head) Initializes the object with the integer array nums. + * int getRandom() Chooses a node randomly from the list and returns its value. All the nodes of the list should be equally likely to be choosen. + */ + +/** + * Definition for singly-linked list. + */ +struct ListNode { + int val; + ListNode *next; + ListNode() : val(0), next(nullptr) {} + explicit ListNode(int x) : val(x), next(nullptr) {} + ListNode(int x, ListNode *next) : val(x), next(next) {} + ~ListNode() { delete this->next; } +}; + +class Solution { +private: + inline static std::mt19937 r = std::mt19937(std::time(nullptr)); + int nums[10005]{}; + int n = 0; +public: + explicit Solution(ListNode* head) { + do { + nums[n++] = head->val; + } while (head = head->next); + } + + int getRandom() { + return nums[r() % n]; + } +}; + +/** + * Your Solution object will be instantiated and called as such: + * Solution* obj = new Solution(head); + * int param_1 = obj->getRandom(); + */ +int main() { + auto* head = new ListNode(1, new ListNode(2, new ListNode(3))); + auto* instance = new Solution(head); + std::cout << instance->getRandom() << std::endl; + std::cout << instance->getRandom() << std::endl; + std::cout << instance->getRandom() << std::endl; + return 0; +} \ No newline at end of file diff --git a/2201/CMakeLists.txt b/2201/CMakeLists.txt index 5318f89..9bcff76 100644 --- a/2201/CMakeLists.txt +++ b/2201/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2201) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2201 220106.cpp) \ No newline at end of file +ADD_EXECUTABLE(2201 220107.cpp) \ No newline at end of file