Skip to content

Commit ec3fd45

Browse files
committed
did my first leetcode hard, some greey problems, and string problems
1 parent 309fdd5 commit ec3fd45

19 files changed

+1031
-173
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include "../../../helperFunctions.h"
2+
3+
using namespace std;
4+
5+
/******************************************
6+
Link: https://leetcode.com/problems/ransom-note/description/
7+
Runtime: 73.37%
8+
Memory: 53.74%
9+
******************************************/
10+
11+
bool ransomNote(string ransomNote, string magazine)
12+
{
13+
unordered_map<char, int> letterCounts;
14+
for (int i = 0; i < ransomNote.size(); ++i)
15+
++letterCounts[ransomNote[i]];
16+
17+
for (int i = 0; i < magazine.size(); ++i)
18+
{
19+
if (letterCounts.count(magazine[i]))
20+
{
21+
--letterCounts[magazine[i]];
22+
if (letterCounts[magazine[i]] == 0)
23+
letterCounts.erase(magazine[i]);
24+
}
25+
}
26+
27+
return letterCounts.empty();
28+
}
29+
30+
int main()
31+
{
32+
cout << ransomNote("aa", "aab") << endl;
33+
return 0;
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "../../../helperFunctions.h"
2+
3+
using namespace std;
4+
5+
/******************************************
6+
Link: https://leetcode.com/problems/reverse-vowels-of-a-string/description/
7+
Runtime: 100.00%
8+
Memory: 82.35%
9+
******************************************/
10+
11+
string reverseVowelsOfAString(string s)
12+
{
13+
int i = 0;
14+
int j = s.size() - 1;
15+
16+
while (i < j)
17+
{
18+
char firstLetter = s[i];
19+
char secondLetter = s[j];
20+
bool shouldSwap = true;
21+
if (!(firstLetter == 'a' || firstLetter == 'A' || firstLetter == 'e' || firstLetter == 'E' || firstLetter == 'i' || firstLetter == 'I' || firstLetter == 'o' || firstLetter == 'O' || firstLetter == 'u' || firstLetter == 'U'))
22+
{
23+
++i;
24+
shouldSwap = false;
25+
}
26+
if (!(secondLetter == 'a' || secondLetter == 'A' || secondLetter == 'e' || secondLetter == 'E' || secondLetter == 'i' || secondLetter == 'I' || secondLetter == 'o' || secondLetter == 'O' || secondLetter == 'u' || secondLetter == 'U'))
27+
{
28+
--j;
29+
shouldSwap = false;
30+
}
31+
if (shouldSwap)
32+
{
33+
swap(s[i], s[j]);
34+
++i;
35+
--j;
36+
}
37+
}
38+
39+
return s;
40+
}
41+
42+
int main()
43+
{
44+
cout << reverseVowelsOfAString("IceCreAm") << endl;
45+
return 0;
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include "../../../helperFunctions.h"
2+
3+
using namespace std;
4+
5+
/******************************************
6+
Link: https://leetcode.com/problems/candy/description/
7+
Runtime: 100.0%
8+
Memory: 25.41%
9+
******************************************/
10+
11+
int candy(vector<int> &ratings)
12+
{
13+
vector<int> candies(ratings.size(), 1);
14+
for (int i = 0; i < ratings.size(); ++i)
15+
{
16+
if (i > 0 && ratings[i] < ratings[i - 1] && candies[i - 1] <= candies[i])
17+
candies[i - 1] = candies[i] + 1;
18+
if (i < ratings.size() - 1 && ratings[i] < ratings[i + 1] && candies[i + 1] <= candies[i])
19+
candies[i + 1] = candies[i] + 1;
20+
}
21+
22+
for (int i = ratings.size() - 1; i >= 0; --i)
23+
{
24+
if (i > 0 && ratings[i] < ratings[i - 1] && candies[i - 1] <= candies[i])
25+
candies[i - 1] = candies[i] + 1;
26+
if (i < ratings.size() - 1 && ratings[i] < ratings[i + 1] && candies[i + 1] <= candies[i])
27+
candies[i + 1] = candies[i] + 1;
28+
}
29+
30+
int sum = 0;
31+
for (int i = 0; i < candies.size(); ++i)
32+
sum += candies[i];
33+
return sum;
34+
}
35+
36+
int main()
37+
{
38+
vector<int> nums1({1, 0, 2});
39+
cout
40+
<< candy(nums1) << endl;
41+
return 0;
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "../../../helperFunctions.h"
2+
3+
using namespace std;
4+
5+
/******************************************
6+
Link: https://leetcode.com/problems/gas-station/description/
7+
Runtime: 100.0%
8+
Memory: 72.09%
9+
******************************************/
10+
11+
int gasStation(vector<int> &gas, vector<int> &cost)
12+
{
13+
int gasSum = 0;
14+
int costSum = 0;
15+
for (int i = 0; i < gas.size(); ++i)
16+
{
17+
gasSum += gas[i];
18+
costSum += cost[i];
19+
}
20+
21+
if (gasSum < costSum)
22+
return -1;
23+
24+
int start = 0;
25+
int sum = 0;
26+
for (int i = 0; i < gas.size(); ++i)
27+
{
28+
sum += gas[i] - cost[i];
29+
if (sum < 0)
30+
{
31+
sum = 0;
32+
start = i + 1;
33+
}
34+
}
35+
return start;
36+
}
37+
38+
int main()
39+
{
40+
vector<int> nums1({5, 1, 2, 3, 4});
41+
vector<int> nums2({4, 4, 1, 5, 1});
42+
cout << gasStation(nums1, nums2) << endl;
43+
return 0;
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include "../../../helperFunctions.h"
2+
3+
using namespace std;
4+
5+
/******************************************
6+
Link: https://leetcode.com/problems/house-robber/description/
7+
Runtime: 100.0%
8+
Memory: 20.20%
9+
******************************************/
10+
11+
int rob(vector<int> &nums)
12+
{
13+
if (nums.size() == 1)
14+
return nums[0];
15+
else if (nums.size() == 2)
16+
return max(nums[0], nums[1]);
17+
18+
vector<int> houseValues(nums.size());
19+
houseValues[0] = nums[0];
20+
houseValues[1] = nums[1];
21+
for (int i = 2; i < houseValues.size(); ++i)
22+
{
23+
int max1 = -1;
24+
for (int j = 2; i - j >= 0; ++j)
25+
max1 = max(max1, houseValues[i - j] + nums[i]);
26+
houseValues[i] = max(houseValues[i - 1], max1);
27+
}
28+
29+
return houseValues[houseValues.size() - 1];
30+
}
31+
32+
int main()
33+
{
34+
vector<int> testing({1, 2, 3, 1});
35+
cout << rob(testing) << endl;
36+
37+
return 0;
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
/******************************************
6+
Link: https://leetcode.com/problems/longest-substring-without-repeating-characters/description/
7+
Runtime: 551 ms
8+
Memory: 144.45 mb
9+
******************************************/
10+
11+
int lengthOfLongestSubstring(string s)
12+
{
13+
int longest = -1;
14+
int currentLength = 0;
15+
unordered_map<char, int> previousCharacters;
16+
for (int i = 0; i < s.size(); ++i)
17+
{
18+
if (previousCharacters.count(s[i]) == 0)
19+
{
20+
previousCharacters[s[i]] = i;
21+
++currentLength;
22+
}
23+
else
24+
{
25+
i = previousCharacters[s[i]];
26+
longest = max(longest, currentLength);
27+
currentLength = 0;
28+
previousCharacters.clear();
29+
}
30+
}
31+
32+
longest = max(longest, currentLength);
33+
return longest;
34+
}
35+
36+
int main()
37+
{
38+
cout << lengthOfLongestSubstring("dvdf") << endl;
39+
return 0;
40+
}
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
/******************************************
6+
Link:
7+
Runtime:
8+
Memory:
9+
******************************************/
10+
11+
int stringToInteger(string s)
12+
{
13+
int result = NULL;
14+
vector<int> digits;
15+
int sign = 1;
16+
for (int i = 0; i < s.size(); ++i)
17+
{
18+
if (s[i] == ' ')
19+
continue;
20+
21+
// It's a negative
22+
if (int(s[i]) == 45)
23+
sign *= -1;
24+
// It's a positive
25+
else if (int(s[i]) == 43)
26+
sign *= 1;
27+
else if (int(s[i]) >= 48 && int(s[i]) <= 57)
28+
digits.push_back(s[i] - '0');
29+
30+
// Not an integer
31+
if (int(s[i]) < 48 || int(s[i]) > 57 || (i < s.size() - 1 && (int(s[i + 1]) < 48 || int(s[i + 1]) > 57)))
32+
break;
33+
}
34+
35+
result = 0;
36+
for (int i = digits.size() - 1; i >= 0; --i)
37+
result += digits[i] * pow(10, digits.size() - 1 - i);
38+
39+
return result * sign;
40+
}
41+
42+
int main()
43+
{
44+
cout << stringToInteger("1337c0d3") << endl;
45+
return 0;
46+
}

anki

41.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)