From e6206a0e8936861313da7503606e5d0fb7c8c0cc Mon Sep 17 00:00:00 2001 From: LEEWONCHEOL Date: Wed, 24 Apr 2024 16:24:03 +0900 Subject: [PATCH 1/6] solved Two Sum --- two-sum/wclee2265.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 two-sum/wclee2265.cpp diff --git a/two-sum/wclee2265.cpp b/two-sum/wclee2265.cpp new file mode 100644 index 000000000..49dadd4c1 --- /dev/null +++ b/two-sum/wclee2265.cpp @@ -0,0 +1,25 @@ +// https://leetcode.com/problems/two-sum/ + +class Solution { +public: + vector twoSum(vector& nums, int target) { + int n = nums.size(); + vector > numsIndices; + + for(int i=0; i target) j--; + if(numsIndices[i].first + numsIndices[j].first == target) break; + } + + vector ans; + ans.push_back(numsIndices[i].second); + ans.push_back(numsIndices[j].second); + return ans; + } +}; From f644e1f77c1f85c8684bd365d428ab443162dd7c Mon Sep 17 00:00:00 2001 From: LEEWONCHEOL Date: Wed, 24 Apr 2024 16:36:24 +0900 Subject: [PATCH 2/6] solved Valid Anagram --- valid-anagram/wclee2265.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 valid-anagram/wclee2265.cpp diff --git a/valid-anagram/wclee2265.cpp b/valid-anagram/wclee2265.cpp new file mode 100644 index 000000000..4342667a2 --- /dev/null +++ b/valid-anagram/wclee2265.cpp @@ -0,0 +1,16 @@ +// https://leetcode.com/problems/valid-anagram/ + +class Solution { +public: + bool isAnagram(string s, string t) { + int cnt1[26]={0,}, cnt2[26] = {0,}; + for(char c : s) cnt1[c-'a']++; + for(char c : t) cnt2[c-'a']++; + + for(int i=0; i<26; i++) { + if(cnt1[i] != cnt2[i]) return false; + } + + return true; + } +}; From 29dc019e298d630c53235c39a77484595f949cec Mon Sep 17 00:00:00 2001 From: LEEWONCHEOL Date: Wed, 24 Apr 2024 16:45:30 +0900 Subject: [PATCH 3/6] solved Valid Palindrome --- valid-palindrome/wclee2265.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 valid-palindrome/wclee2265.cpp diff --git a/valid-palindrome/wclee2265.cpp b/valid-palindrome/wclee2265.cpp new file mode 100644 index 000000000..6aacd5f48 --- /dev/null +++ b/valid-palindrome/wclee2265.cpp @@ -0,0 +1,19 @@ +// https://leetcode.com/problems/valid-palindrome/ + +class Solution { +public: + bool isPalindrome(string s) { + string ns; + for(char c : s){ + if(c >= '0' && c <= '9') ns += c; + else if(c >= 'A' && c <= 'Z') ns += (c + 32); + else if(c >= 'a' && c <= 'z') ns += c; + } + + int n = ns.size(); + for(int i=0; i Date: Wed, 24 Apr 2024 16:53:07 +0900 Subject: [PATCH 4/6] solved Contains Duplicate --- contains-duplicate/wclee2265.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 contains-duplicate/wclee2265.cpp diff --git a/contains-duplicate/wclee2265.cpp b/contains-duplicate/wclee2265.cpp new file mode 100644 index 000000000..b8fd08feb --- /dev/null +++ b/contains-duplicate/wclee2265.cpp @@ -0,0 +1,13 @@ +// https://leetcode.com/problems/contains-duplicate/ + +class Solution { +public: + bool containsDuplicate(vector& nums) { + unordered_map hm; + for(int x : nums) { + if(hm[x]) return true; + hm[x] = true; + } + return false; + } +}; From c86130a35e64a58664a407044d737bb67ba688cf Mon Sep 17 00:00:00 2001 From: LEEWONCHEOL Date: Wed, 24 Apr 2024 17:05:18 +0900 Subject: [PATCH 5/6] solved Best Time to Buy and Sell Stock --- best-time-to-buy-and-sell-stock/wclee2265.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/wclee2265.cpp diff --git a/best-time-to-buy-and-sell-stock/wclee2265.cpp b/best-time-to-buy-and-sell-stock/wclee2265.cpp new file mode 100644 index 000000000..e987723f7 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/wclee2265.cpp @@ -0,0 +1,15 @@ +// https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ + +class Solution { +public: + int maxProfit(vector& prices) { + int n = prices.size(); + int minimum = prices[0]; + int ans = 0; + for(int i=1; i Date: Thu, 25 Apr 2024 15:26:16 +0900 Subject: [PATCH 6/6] wrote Time/Space Complexities for each algorithm --- best-time-to-buy-and-sell-stock/wclee2265.cpp | 2 ++ contains-duplicate/wclee2265.cpp | 2 ++ two-sum/wclee2265.cpp | 2 ++ valid-anagram/wclee2265.cpp | 2 ++ valid-palindrome/wclee2265.cpp | 2 ++ 5 files changed, 10 insertions(+) diff --git a/best-time-to-buy-and-sell-stock/wclee2265.cpp b/best-time-to-buy-and-sell-stock/wclee2265.cpp index e987723f7..7a0b65755 100644 --- a/best-time-to-buy-and-sell-stock/wclee2265.cpp +++ b/best-time-to-buy-and-sell-stock/wclee2265.cpp @@ -1,4 +1,6 @@ // https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ +// Time Complexity : O(n) +// Space Complexity : O(1) class Solution { public: diff --git a/contains-duplicate/wclee2265.cpp b/contains-duplicate/wclee2265.cpp index b8fd08feb..a16c744a2 100644 --- a/contains-duplicate/wclee2265.cpp +++ b/contains-duplicate/wclee2265.cpp @@ -1,4 +1,6 @@ // https://leetcode.com/problems/contains-duplicate/ +// Time Complexity : O(n) +// Space Complexity : O(n) class Solution { public: diff --git a/two-sum/wclee2265.cpp b/two-sum/wclee2265.cpp index 49dadd4c1..b23c30432 100644 --- a/two-sum/wclee2265.cpp +++ b/two-sum/wclee2265.cpp @@ -1,4 +1,6 @@ // https://leetcode.com/problems/two-sum/ +// Time Complexity : O(nlogn) +// Space Complexity : O(n) class Solution { public: diff --git a/valid-anagram/wclee2265.cpp b/valid-anagram/wclee2265.cpp index 4342667a2..2f2d65e76 100644 --- a/valid-anagram/wclee2265.cpp +++ b/valid-anagram/wclee2265.cpp @@ -1,4 +1,6 @@ // https://leetcode.com/problems/valid-anagram/ +// Time Complexity : O(n) +// Space Complexity : O(1) class Solution { public: diff --git a/valid-palindrome/wclee2265.cpp b/valid-palindrome/wclee2265.cpp index 6aacd5f48..a5c98bd63 100644 --- a/valid-palindrome/wclee2265.cpp +++ b/valid-palindrome/wclee2265.cpp @@ -1,4 +1,6 @@ // https://leetcode.com/problems/valid-palindrome/ +// Time Complexity : O(n) +// Space Complexity : O(n) class Solution { public: