Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions best-time-to-buy-and-sell-stock/wclee2265.cpp
Original file line number Diff line number Diff line change
@@ -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<int>& prices) {
int n = prices.size();
int minimum = prices[0];
int ans = 0;
for(int i=1; i<n; i++) {
ans = max(ans, prices[i] - minimum);
minimum = min(minimum, prices[i]);
}
return ans;
}
};
15 changes: 15 additions & 0 deletions contains-duplicate/wclee2265.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// https://leetcode.com/problems/contains-duplicate/
// Time Complexity : O(n)
// Space Complexity : O(n)

class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
unordered_map<int, bool> hm;
for(int x : nums) {
if(hm[x]) return true;
hm[x] = true;
}
return false;
}
};
27 changes: 27 additions & 0 deletions two-sum/wclee2265.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// https://leetcode.com/problems/two-sum/
// Time Complexity : O(nlogn)
// Space Complexity : O(n)

class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int n = nums.size();
vector<pair<int, int> > numsIndices;

for(int i=0; i<n; i++){
numsIndices.push_back({nums[i], i});
}

sort(numsIndices.begin(), numsIndices.end());
int i, j = n-1;
for(i=0; i<n; i++) {
while(numsIndices[i].first + numsIndices[j].first > target) j--;
if(numsIndices[i].first + numsIndices[j].first == target) break;
}

vector<int> ans;
ans.push_back(numsIndices[i].second);
ans.push_back(numsIndices[j].second);
return ans;
}
};
18 changes: 18 additions & 0 deletions valid-anagram/wclee2265.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
};
21 changes: 21 additions & 0 deletions valid-palindrome/wclee2265.cpp
Original file line number Diff line number Diff line change
@@ -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<n/2; i++) {
if(ns[i] != ns[n-i-1]) return false;
}
return true;
}
};