Skip to content

Commit 578287c

Browse files
Ayush GangulyAyush Ganguly
Ayush Ganguly
authored and
Ayush Ganguly
committed
solved older leetcode 150
1 parent 68b9f00 commit 578287c

7 files changed

+193
-0
lines changed

Best_Time_To_Buy_Stocks.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
int maxProfit(vector<int>& prices) {
4+
int cost = 0;
5+
int acc_cost = 0;
6+
int min = prices[0];
7+
8+
for (int i = 1; i < prices.size(); i++) {
9+
if (min > prices[i]) {
10+
min = prices[i];
11+
}
12+
cost = prices[i] - min;
13+
14+
if (cost > acc_cost){
15+
acc_cost = cost;
16+
}
17+
18+
}
19+
return acc_cost;
20+
}
21+
};

First_Occurrence_String.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using namespace std;
2+
3+
class Solution {
4+
public:
5+
int strStr(string haystack, string needle) {
6+
if ((needle == " ") || (haystack == " ") || (needle.length() > haystack.length())){
7+
return -1;
8+
}
9+
char first_letter = needle[0];
10+
//first occurrance
11+
int fo = 0;
12+
int traverse = 0;
13+
while ((abs(fo) < haystack.length()) && (traverse < haystack.length())) {
14+
// cout << "1 " << fo << endl;
15+
// cout << "traverse " << traverse << endl;
16+
// cout << haystack[traverse] << " == " << first_letter << endl;
17+
if (haystack[traverse] == first_letter){
18+
int temp_traverse = traverse;
19+
fo = traverse;
20+
for (int i = 0; i < needle.length(); ++i){
21+
if (haystack[temp_traverse] != needle[i]){
22+
fo = -1;
23+
break;
24+
}
25+
temp_traverse++;
26+
}
27+
if (fo != -1){
28+
return fo;
29+
} else {
30+
traverse = traverse + 1;
31+
// cout << "2 " << fo << endl;
32+
}
33+
} else {
34+
traverse++;
35+
}
36+
37+
}
38+
// cout << (fo < haystack.length()) << endl;
39+
// cout << haystack.length() << " fo " << fo << " traverse " << traverse << endl;
40+
return -1;
41+
}
42+
};

Length_of_last_word.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using namespace std;
2+
class Solution {
3+
public:
4+
int lengthOfLastWord(string s) {
5+
int count_lword = 0;
6+
int str_len = s.length();
7+
int lw_index = 0;
8+
for (int i = 1; i < str_len; i++) {
9+
if ((s[i - 1] == ' ') && (s[i] != ' ')) {
10+
lw_index = i;
11+
}
12+
}
13+
while ((s[lw_index] != ' ') && (lw_index < str_len)) {
14+
count_lword++;
15+
lw_index++;
16+
}
17+
18+
return count_lword;
19+
}
20+
};

Valid_Palindrome.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using namespace std;
2+
class Solution {
3+
public:
4+
bool isPalindrome(string s) {
5+
string check_valid = "";
6+
for (char letter : s) {
7+
if ((int(letter) >= 65) && (int(letter) <= 90)) {
8+
check_valid += tolower(letter);
9+
} else if ((int(letter) >= 97) && (int(letter) <= 122)) {
10+
check_valid += letter;
11+
} else if ((int(letter) >= 48) && (int(letter) <= 57)) {
12+
check_valid += letter;
13+
}
14+
}
15+
16+
if (check_valid == "") {
17+
return true;
18+
}
19+
int front = 0;
20+
int back = check_valid.length() - 1;
21+
22+
while (front <= back) {
23+
if (check_valid[front] != check_valid[back]) {
24+
return false;
25+
}
26+
front++;
27+
back--;
28+
}
29+
return true;
30+
31+
}
32+
};

is_Subsequence.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
bool isSubsequence(string s, string t) {
4+
if (s.length() > t.length()) {
5+
return false;
6+
}
7+
int s_curr = 0;
8+
int t_traverse = 0;
9+
while ((t_traverse < t.length()) && (s_curr < s.length())) {
10+
if (s[s_curr] == t[t_traverse]) {
11+
s_curr++;
12+
t_traverse++;
13+
} else if (t[t_traverse] != s[s_curr]) {
14+
if (t_traverse == t.length() - 1) {
15+
return false;
16+
}
17+
t_traverse++;
18+
}
19+
}
20+
21+
//Have not finished iterating through all of s
22+
if (s_curr != s.length()) {
23+
return false;
24+
}
25+
return true;
26+
}
27+
};

majority_element.cpp

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using namespace std;
2+
3+
class Solution {
4+
public:
5+
int majorityElement(vector<int>& nums) {
6+
7+
// My Solution - does not work for insanity tests
8+
// int index = 0;
9+
// int curr = nums[index];
10+
// int count = 0;
11+
// if (nums.size() == 1) {
12+
// return nums[0];
13+
// }
14+
// while (count < nums.size() / 2){
15+
// for (int traverse = index; traverse < nums.size(); traverse++) {
16+
// if (nums[traverse] == curr){
17+
// count++;
18+
// }
19+
// }
20+
// if (count > nums.size() / 2){
21+
// return curr;
22+
// } else {
23+
// count = 0;
24+
// index++;
25+
// curr = nums[index];
26+
// }
27+
// }
28+
// return 0;
29+
30+
// Sort the array and return the element at index n/2 since element appears more than n/2 times.
31+
// int length = nums.size();
32+
// sort(nums.begin(), nums.end());
33+
// return nums[length / 2];
34+
35+
// Moore Voting Algorithm
36+
int count = 0;
37+
int candidate = 0;
38+
39+
for (int i = 0; i < nums.size(); i++){
40+
if (count == 0){
41+
candidate = nums[i];
42+
count++;
43+
} else if (nums[i] == candidate){
44+
count++;
45+
} else {
46+
count--;
47+
}
48+
}
49+
return candidate;
50+
}
51+
};

reverse_words_string.cpp

Whitespace-only changes.

0 commit comments

Comments
 (0)