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..7a0b65755 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/wclee2265.cpp @@ -0,0 +1,17 @@ +// https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ +// Time Complexity : O(n) +// Space Complexity : O(1) + +class Solution { +public: + int maxProfit(vector& prices) { + int n = prices.size(); + int minimum = prices[0]; + int ans = 0; + for(int i=1; i& nums) { + unordered_map hm; + for(int x : nums) { + if(hm[x]) return true; + hm[x] = true; + } + return false; + } +}; diff --git a/two-sum/wclee2265.cpp b/two-sum/wclee2265.cpp new file mode 100644 index 000000000..b23c30432 --- /dev/null +++ b/two-sum/wclee2265.cpp @@ -0,0 +1,27 @@ +// https://leetcode.com/problems/two-sum/ +// Time Complexity : O(nlogn) +// Space Complexity : O(n) + +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; + } +}; diff --git a/valid-anagram/wclee2265.cpp b/valid-anagram/wclee2265.cpp new file mode 100644 index 000000000..2f2d65e76 --- /dev/null +++ b/valid-anagram/wclee2265.cpp @@ -0,0 +1,18 @@ +// https://leetcode.com/problems/valid-anagram/ +// Time Complexity : O(n) +// Space Complexity : O(1) + +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; + } +}; diff --git a/valid-palindrome/wclee2265.cpp b/valid-palindrome/wclee2265.cpp new file mode 100644 index 000000000..a5c98bd63 --- /dev/null +++ b/valid-palindrome/wclee2265.cpp @@ -0,0 +1,21 @@ +// https://leetcode.com/problems/valid-palindrome/ +// Time Complexity : O(n) +// Space Complexity : O(n) + +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