Skip to content

Commit 3562831

Browse files
committedJul 2, 2024
Added more algorithsm
1 parent db610d1 commit 3562831

File tree

7 files changed

+289
-58
lines changed

7 files changed

+289
-58
lines changed
 

‎algorithm.xcodeproj/xcuserdata/aseshshrestha.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
filePath = "algorithm/main.cpp"
1515
startingColumnNumber = "9223372036854775807"
1616
endingColumnNumber = "9223372036854775807"
17-
startingLineNumber = "388"
18-
endingLineNumber = "388"
17+
startingLineNumber = "400"
18+
endingLineNumber = "400"
1919
landmarkName = "main(argc, argv)"
2020
landmarkType = "9">
2121
</BreakpointContent>
@@ -30,8 +30,8 @@
3030
filePath = "algorithm/main.cpp"
3131
startingColumnNumber = "9223372036854775807"
3232
endingColumnNumber = "9223372036854775807"
33-
startingLineNumber = "354"
34-
endingLineNumber = "354"
33+
startingLineNumber = "366"
34+
endingLineNumber = "366"
3535
landmarkName = "main(argc, argv)"
3636
landmarkType = "9">
3737
</BreakpointContent>
@@ -46,8 +46,8 @@
4646
filePath = "algorithm/main.cpp"
4747
startingColumnNumber = "9223372036854775807"
4848
endingColumnNumber = "9223372036854775807"
49-
startingLineNumber = "279"
50-
endingLineNumber = "279"
49+
startingLineNumber = "291"
50+
endingLineNumber = "291"
5151
landmarkName = "main(argc, argv)"
5252
landmarkType = "9">
5353
</BreakpointContent>
@@ -62,8 +62,8 @@
6262
filePath = "algorithm/main.cpp"
6363
startingColumnNumber = "9223372036854775807"
6464
endingColumnNumber = "9223372036854775807"
65-
startingLineNumber = "247"
66-
endingLineNumber = "247"
65+
startingLineNumber = "259"
66+
endingLineNumber = "259"
6767
landmarkName = "main(argc, argv)"
6868
landmarkType = "9">
6969
</BreakpointContent>
@@ -126,8 +126,8 @@
126126
filePath = "algorithm/main.cpp"
127127
startingColumnNumber = "9223372036854775807"
128128
endingColumnNumber = "9223372036854775807"
129-
startingLineNumber = "205"
130-
endingLineNumber = "205"
129+
startingLineNumber = "217"
130+
endingLineNumber = "217"
131131
landmarkName = "main(argc, argv)"
132132
landmarkType = "9">
133133
</BreakpointContent>
@@ -142,8 +142,8 @@
142142
filePath = "algorithm/main.cpp"
143143
startingColumnNumber = "9223372036854775807"
144144
endingColumnNumber = "9223372036854775807"
145-
startingLineNumber = "168"
146-
endingLineNumber = "168"
145+
startingLineNumber = "180"
146+
endingLineNumber = "180"
147147
landmarkName = "main(argc, argv)"
148148
landmarkType = "9">
149149
</BreakpointContent>
@@ -158,8 +158,8 @@
158158
filePath = "algorithm/main.cpp"
159159
startingColumnNumber = "9223372036854775807"
160160
endingColumnNumber = "9223372036854775807"
161-
startingLineNumber = "127"
162-
endingLineNumber = "127"
161+
startingLineNumber = "139"
162+
endingLineNumber = "139"
163163
landmarkName = "main(argc, argv)"
164164
landmarkType = "9">
165165
</BreakpointContent>
@@ -190,8 +190,8 @@
190190
filePath = "algorithm/main.cpp"
191191
startingColumnNumber = "9223372036854775807"
192192
endingColumnNumber = "9223372036854775807"
193-
startingLineNumber = "116"
194-
endingLineNumber = "116"
193+
startingLineNumber = "128"
194+
endingLineNumber = "128"
195195
landmarkName = "main(argc, argv)"
196196
landmarkType = "9">
197197
</BreakpointContent>
@@ -206,8 +206,8 @@
206206
filePath = "algorithm/main.cpp"
207207
startingColumnNumber = "9223372036854775807"
208208
endingColumnNumber = "9223372036854775807"
209-
startingLineNumber = "102"
210-
endingLineNumber = "102"
209+
startingLineNumber = "114"
210+
endingLineNumber = "114"
211211
landmarkName = "main(argc, argv)"
212212
landmarkType = "9">
213213
</BreakpointContent>
@@ -270,8 +270,8 @@
270270
filePath = "algorithm/number.cpp"
271271
startingColumnNumber = "9223372036854775807"
272272
endingColumnNumber = "9223372036854775807"
273-
startingLineNumber = "936"
274-
endingLineNumber = "936"
273+
startingLineNumber = "908"
274+
endingLineNumber = "908"
275275
landmarkName = "reverse_integer(input)"
276276
landmarkType = "9">
277277
</BreakpointContent>

‎algorithm/main.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,21 @@
1818

1919
int main(int argc, const char* argv[]) {
2020

21+
// invoke_median_of_two_sorted_arrays();
22+
2123
// invoke_four_number_sum();
2224

23-
invoke_reverse_integer();
25+
invoke_jump_game_ii();
26+
27+
// invoke_is_subsequence();
28+
29+
// invoke_integer_to_roman();
30+
31+
// invoke_jump_game();
32+
33+
// invoke_best_time_to_buy_and_sell_stock_ii();
34+
35+
// invoke_reverse_integer();
2436

2537
// invoke_rotate_array();
2638

‎algorithm/number.cpp

Lines changed: 180 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -236,34 +236,6 @@ int missing_number() {
236236
return missing_number - std::accumulate(input_array.begin(), input_array.end(), 0);
237237
}
238238

239-
/*
240-
https://leetcode.com/problems/median-of-two-sorted-arrays/
241-
Input: nums1 = [1,3], nums2 = [2]
242-
Output: 2.00000
243-
*/
244-
void median_of_two_sorted_arrays(const std::vector<int>& array_one, const std::vector<int>& array_two) {
245-
auto array_one_count = array_one.size();
246-
auto array_two_count = array_two.size();
247-
248-
// Assign an array with the max number of items
249-
auto max_array = array_one_count >= array_two_count ? array_one : array_two;
250-
251-
std::vector<int> total(max_array.size());
252-
253-
// Iterate through all the elements of the max array
254-
/*
255-
1, 2 -> 3, 4
256-
*/
257-
for(int index = 0; index < max_array.size(); index++) {
258-
259-
}
260-
}
261-
262-
void invoke_median_of_two_sorted_arrays() {
263-
std::vector<int> input_one{1, 3}, input_two{2};
264-
median_of_two_sorted_arrays(input_one, input_two);
265-
}
266-
267239
/*
268240
11. Container With Most Water
269241
https://leetcode.com/problems/container-with-most-water/
@@ -942,3 +914,183 @@ int reverse_integer(int input) {
942914
void invoke_reverse_integer() {
943915
std::cout<<reverse_integer(-123);
944916
}
917+
918+
/*
919+
nums1 = [1,2], nums2 = [3,4]; Output: 2.50000
920+
nums1 = [[1,3,4,5], nums = [2,3,6,7]; Output: 3.50000
921+
*/
922+
double median_of_two_sorted_arrays(std::vector<int>& nums1, std::vector<int>& nums2) {
923+
double output = 0.0;
924+
return output;
925+
}
926+
void invoke_median_of_two_sorted_arrays() {
927+
std::vector<int> nums1 = {1,2}, nums2 = {3,4};
928+
std::cout<<median_of_two_sorted_arrays(nums1, nums2);
929+
}
930+
931+
/*
932+
prices = [7,1,5,3,6,4]; Output = 5-1 + 6-3 => 7
933+
prices = [1,2,3,4,5]; Output = 5-1 => 4
934+
prices = [7,6,4,3,1]; Output = 0
935+
*/
936+
int best_time_to_buy_and_sell_stock_ii(std::vector<int>& prices) {
937+
int output = 0;
938+
for(int index = 1; index < prices.size(); ++index) {
939+
auto diff = prices[index] - prices[index - 1];
940+
if(diff > 0) {
941+
output += diff;
942+
continue;
943+
}
944+
}
945+
return output;
946+
}
947+
void invoke_best_time_to_buy_and_sell_stock_ii() {
948+
std::vector<int> prices = {7,1,5,3,6,4};
949+
std::cout<<"Max profit: "<<best_time_to_buy_and_sell_stock_ii(prices);
950+
}
951+
952+
/*
953+
nums: [3,2,1,0,4] => false
954+
955+
nums: [2,3,1,1,4] => true
956+
0 1 2 3 4
957+
2 3 1 1 4 => 1 1$; 2 1 1$
958+
959+
nums: [6,5,5,7,4] => true
960+
961+
nums: [1 1 1 1 1]
962+
*/
963+
bool jump_game(std::vector<int>& nums) {
964+
int last_index = nums.size() - 1;
965+
for(int index = nums.size() - 2; index >= 0; --index) {
966+
if(index + nums[index] >= last_index) {
967+
last_index = index;
968+
}
969+
}
970+
971+
return last_index <= 0;
972+
}
973+
void invoke_jump_game() {
974+
std::vector<int> nums = {2,3,1,1,4};
975+
std::cout<<std::boolalpha<<jump_game(nums);
976+
}
977+
978+
/*
979+
1 3 *
980+
[2,3,1,1,4] => 2
981+
/\
982+
1 2
983+
| |
984+
3 1
985+
0 1 2 3 4 5 6
986+
[2,3,1,2,1,1,4] => 3
987+
/\
988+
1 2
989+
| |
990+
3 1
991+
|
992+
1
993+
|\
994+
1 2
995+
996+
[1,2] => 1
997+
998+
[1,2,3] => 2
999+
|
1000+
1
1001+
|\
1002+
1 2
1003+
\
1004+
1*
1005+
1006+
[1,1,1,1,2,4] = 5
1007+
|\
1008+
1 2
1009+
*/
1010+
int jump_game_ii(std::vector<int>& nums) {
1011+
int min_jumps = 0;
1012+
1013+
return min_jumps;
1014+
}
1015+
void invoke_jump_game_ii() {
1016+
std::vector<int> nums = {2,3,1,2,1,1,4};
1017+
std::cout<<jump_game_ii(nums)<<std::endl;
1018+
}
1019+
1020+
/*
1021+
Hashmap:
1022+
1: I
1023+
5: V
1024+
10: X
1025+
50: L
1026+
100: C
1027+
500: D
1028+
1000: M
1029+
1030+
3749 => MMMDCCXLIX
1031+
3000: MMM: 1000 + 1000 + 1000
1032+
700: DCC: 500 + 100 + 100
1033+
40: XL: 50 - 10
1034+
9: IX: 10 - 1
1035+
1036+
3749 % 10 = 9 = IX
1037+
3749 % 10 = 374 => 374 % 10 = 4 * 10 = 40 = XL
1038+
37 % 10 = 7 => 7 * 100 = 700 = DCC
1039+
3 % 10 = 3 => 3 * 1000 = 3000 = MMM
1040+
*/
1041+
std::string integer_to_roman(int number) {
1042+
std::string output;
1043+
1044+
std::vector<std::string> symbol = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
1045+
std::vector<int> symbol_value = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
1046+
1047+
for(int index = 0; number != 0; ++index) {
1048+
while(number >= symbol_value[index]) {
1049+
number -= symbol_value[index];
1050+
output += symbol[index];
1051+
}
1052+
}
1053+
1054+
return output;
1055+
1056+
// static std::unordered_map<int, std::string_view> roman_map = {
1057+
// {1, "I"},
1058+
// {5, "V"},
1059+
// {9, "IX"},
1060+
// {10, "X"},
1061+
// {40, "XL"},
1062+
// {50, "L"},
1063+
// {100, "C"},
1064+
// {500, "D"},
1065+
// {700, "DCC"},
1066+
// {1000, "M"},
1067+
// {3000, "MMM"},
1068+
// };
1069+
//
1070+
// std::vector<std::string_view> temp_output;
1071+
//
1072+
// int multiplicant = 0;
1073+
//
1074+
// do {
1075+
// auto last_number = number % 10;
1076+
// number /= 10;
1077+
//
1078+
// multiplicant *= 10;
1079+
// if(multiplicant == 0) {
1080+
// multiplicant = 1;
1081+
// }
1082+
//
1083+
// std::cout<<last_number * multiplicant<<" => "<<roman_map[last_number * multiplicant]<<std::endl;
1084+
// temp_output.push_back(roman_map[last_number * multiplicant]);
1085+
//
1086+
// } while(number != 0);
1087+
//
1088+
// for(int index = temp_output.size() - 1; index >= 0; --index) {
1089+
// output.append(temp_output[index]);
1090+
// }
1091+
// return output;
1092+
}
1093+
void invoke_integer_to_roman() {
1094+
std::cout<<integer_to_roman(3749)<<std::endl;
1095+
}
1096+

‎algorithm/number.hpp

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ CSingleLinkedList<int> invoke_reverse_linked_list(CSingleLinkedList<int> input_l
3333

3434
int missing_number();
3535

36-
/*
37-
*/
38-
void median_of_two_sorted_arrays(const std::vector<int>& array_one, const std::vector<int>& array_two);
39-
void invoke_median_of_two_sorted_arrays();
40-
4136
int container_with_most_water(const std::vector<int>& container_height);
4237
void invoke_container_with_most_water();
4338

@@ -226,4 +221,27 @@ void invoke_rotate_array();
226221
*/
227222
void invoke_reverse_integer();
228223

224+
/*
225+
4. Median of Two Sorted Arrays: https://leetcode.com/problems/median-of-two-sorted-arrays/description/
226+
https://www.algoexpert.io/questions/median-of-two-sorted-arrays
227+
*/
228+
void invoke_median_of_two_sorted_arrays();
229+
230+
/*
231+
122. Best Time to Buy and Sell Stock II https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii
232+
*/
233+
void invoke_best_time_to_buy_and_sell_stock_ii();
234+
235+
/*
236+
55. Jump Game https://leetcode.com/problems/jump-game
237+
*/
238+
void invoke_jump_game();
239+
240+
void invoke_jump_game_ii();
241+
242+
/*
243+
12. Integer to Roman https://leetcode.com/problems/integer-to-roman
244+
*/
245+
void invoke_integer_to_roman();
246+
229247
#endif /* NUMBER_HPP */

0 commit comments

Comments
 (0)