Skip to content

Commit db64801

Browse files
authored
Merge pull request #1 from wclee2265/week1-wclee2265
Week1 wclee2265
2 parents 7db46d5 + 94eb325 commit db64801

File tree

5 files changed

+98
-0
lines changed

5 files changed

+98
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
2+
// Time Complexity : O(n)
3+
// Space Complexity : O(1)
4+
5+
class Solution {
6+
public:
7+
int maxProfit(vector<int>& prices) {
8+
int n = prices.size();
9+
int minimum = prices[0];
10+
int ans = 0;
11+
for(int i=1; i<n; i++) {
12+
ans = max(ans, prices[i] - minimum);
13+
minimum = min(minimum, prices[i]);
14+
}
15+
return ans;
16+
}
17+
};

contains-duplicate/wclee2265.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// https://leetcode.com/problems/contains-duplicate/
2+
// Time Complexity : O(n)
3+
// Space Complexity : O(n)
4+
5+
class Solution {
6+
public:
7+
bool containsDuplicate(vector<int>& nums) {
8+
unordered_map<int, bool> hm;
9+
for(int x : nums) {
10+
if(hm[x]) return true;
11+
hm[x] = true;
12+
}
13+
return false;
14+
}
15+
};

two-sum/wclee2265.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// https://leetcode.com/problems/two-sum/
2+
// Time Complexity : O(nlogn)
3+
// Space Complexity : O(n)
4+
5+
class Solution {
6+
public:
7+
vector<int> twoSum(vector<int>& nums, int target) {
8+
int n = nums.size();
9+
vector<pair<int, int> > numsIndices;
10+
11+
for(int i=0; i<n; i++){
12+
numsIndices.push_back({nums[i], i});
13+
}
14+
15+
sort(numsIndices.begin(), numsIndices.end());
16+
int i, j = n-1;
17+
for(i=0; i<n; i++) {
18+
while(numsIndices[i].first + numsIndices[j].first > target) j--;
19+
if(numsIndices[i].first + numsIndices[j].first == target) break;
20+
}
21+
22+
vector<int> ans;
23+
ans.push_back(numsIndices[i].second);
24+
ans.push_back(numsIndices[j].second);
25+
return ans;
26+
}
27+
};

valid-anagram/wclee2265.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// https://leetcode.com/problems/valid-anagram/
2+
// Time Complexity : O(n)
3+
// Space Complexity : O(1)
4+
5+
class Solution {
6+
public:
7+
bool isAnagram(string s, string t) {
8+
int cnt1[26]={0,}, cnt2[26] = {0,};
9+
for(char c : s) cnt1[c-'a']++;
10+
for(char c : t) cnt2[c-'a']++;
11+
12+
for(int i=0; i<26; i++) {
13+
if(cnt1[i] != cnt2[i]) return false;
14+
}
15+
16+
return true;
17+
}
18+
};

valid-palindrome/wclee2265.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// https://leetcode.com/problems/valid-palindrome/
2+
// Time Complexity : O(n)
3+
// Space Complexity : O(n)
4+
5+
class Solution {
6+
public:
7+
bool isPalindrome(string s) {
8+
string ns;
9+
for(char c : s){
10+
if(c >= '0' && c <= '9') ns += c;
11+
else if(c >= 'A' && c <= 'Z') ns += (c + 32);
12+
else if(c >= 'a' && c <= 'z') ns += c;
13+
}
14+
15+
int n = ns.size();
16+
for(int i=0; i<n/2; i++) {
17+
if(ns[i] != ns[n-i-1]) return false;
18+
}
19+
return true;
20+
}
21+
};

0 commit comments

Comments
 (0)