diff --git a/algorithm.xcodeproj/project.xcworkspace/xcuserdata/aseshshrestha.xcuserdatad/UserInterfaceState.xcuserstate b/algorithm.xcodeproj/project.xcworkspace/xcuserdata/aseshshrestha.xcuserdatad/UserInterfaceState.xcuserstate index 8eab1fb..c92ef8f 100755 Binary files a/algorithm.xcodeproj/project.xcworkspace/xcuserdata/aseshshrestha.xcuserdatad/UserInterfaceState.xcuserstate and b/algorithm.xcodeproj/project.xcworkspace/xcuserdata/aseshshrestha.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/algorithm.xcodeproj/xcuserdata/aseshshrestha.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/algorithm.xcodeproj/xcuserdata/aseshshrestha.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist index d6c93ed..218f386 100755 --- a/algorithm.xcodeproj/xcuserdata/aseshshrestha.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/algorithm.xcodeproj/xcuserdata/aseshshrestha.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -14,8 +14,8 @@ filePath = "algorithm/main.cpp" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "388" - endingLineNumber = "388" + startingLineNumber = "400" + endingLineNumber = "400" landmarkName = "main(argc, argv)" landmarkType = "9"> @@ -30,8 +30,8 @@ filePath = "algorithm/main.cpp" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "354" - endingLineNumber = "354" + startingLineNumber = "366" + endingLineNumber = "366" landmarkName = "main(argc, argv)" landmarkType = "9"> @@ -46,8 +46,8 @@ filePath = "algorithm/main.cpp" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "279" - endingLineNumber = "279" + startingLineNumber = "291" + endingLineNumber = "291" landmarkName = "main(argc, argv)" landmarkType = "9"> @@ -62,8 +62,8 @@ filePath = "algorithm/main.cpp" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "247" - endingLineNumber = "247" + startingLineNumber = "259" + endingLineNumber = "259" landmarkName = "main(argc, argv)" landmarkType = "9"> @@ -126,8 +126,8 @@ filePath = "algorithm/main.cpp" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "205" - endingLineNumber = "205" + startingLineNumber = "217" + endingLineNumber = "217" landmarkName = "main(argc, argv)" landmarkType = "9"> @@ -142,8 +142,8 @@ filePath = "algorithm/main.cpp" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "168" - endingLineNumber = "168" + startingLineNumber = "180" + endingLineNumber = "180" landmarkName = "main(argc, argv)" landmarkType = "9"> @@ -158,8 +158,8 @@ filePath = "algorithm/main.cpp" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "127" - endingLineNumber = "127" + startingLineNumber = "139" + endingLineNumber = "139" landmarkName = "main(argc, argv)" landmarkType = "9"> @@ -190,8 +190,8 @@ filePath = "algorithm/main.cpp" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "116" - endingLineNumber = "116" + startingLineNumber = "128" + endingLineNumber = "128" landmarkName = "main(argc, argv)" landmarkType = "9"> @@ -206,8 +206,8 @@ filePath = "algorithm/main.cpp" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "102" - endingLineNumber = "102" + startingLineNumber = "114" + endingLineNumber = "114" landmarkName = "main(argc, argv)" landmarkType = "9"> @@ -270,8 +270,8 @@ filePath = "algorithm/number.cpp" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "936" - endingLineNumber = "936" + startingLineNumber = "908" + endingLineNumber = "908" landmarkName = "reverse_integer(input)" landmarkType = "9"> diff --git a/algorithm/main.cpp b/algorithm/main.cpp index bb530fb..4377138 100755 --- a/algorithm/main.cpp +++ b/algorithm/main.cpp @@ -18,9 +18,21 @@ int main(int argc, const char* argv[]) { + // invoke_median_of_two_sorted_arrays(); + // invoke_four_number_sum(); - invoke_reverse_integer(); + invoke_jump_game_ii(); + +// invoke_is_subsequence(); + +// invoke_integer_to_roman(); + +// invoke_jump_game(); + +// invoke_best_time_to_buy_and_sell_stock_ii(); + +// invoke_reverse_integer(); // invoke_rotate_array(); diff --git a/algorithm/number.cpp b/algorithm/number.cpp index f944ace..8254f8a 100755 --- a/algorithm/number.cpp +++ b/algorithm/number.cpp @@ -236,34 +236,6 @@ int missing_number() { return missing_number - std::accumulate(input_array.begin(), input_array.end(), 0); } -/* - https://leetcode.com/problems/median-of-two-sorted-arrays/ - Input: nums1 = [1,3], nums2 = [2] - Output: 2.00000 - */ -void median_of_two_sorted_arrays(const std::vector& array_one, const std::vector& array_two) { - auto array_one_count = array_one.size(); - auto array_two_count = array_two.size(); - - // Assign an array with the max number of items - auto max_array = array_one_count >= array_two_count ? array_one : array_two; - - std::vector total(max_array.size()); - - // Iterate through all the elements of the max array - /* - 1, 2 -> 3, 4 - */ - for(int index = 0; index < max_array.size(); index++) { - - } -} - -void invoke_median_of_two_sorted_arrays() { - std::vector input_one{1, 3}, input_two{2}; - median_of_two_sorted_arrays(input_one, input_two); -} - /* 11. Container With Most Water https://leetcode.com/problems/container-with-most-water/ @@ -942,3 +914,183 @@ int reverse_integer(int input) { void invoke_reverse_integer() { std::cout<& nums1, std::vector& nums2) { + double output = 0.0; + return output; +} +void invoke_median_of_two_sorted_arrays() { + std::vector nums1 = {1,2}, nums2 = {3,4}; + std::cout< 7 +prices = [1,2,3,4,5]; Output = 5-1 => 4 +prices = [7,6,4,3,1]; Output = 0 +*/ +int best_time_to_buy_and_sell_stock_ii(std::vector& prices) { + int output = 0; + for(int index = 1; index < prices.size(); ++index) { + auto diff = prices[index] - prices[index - 1]; + if(diff > 0) { + output += diff; + continue; + } + } + return output; +} +void invoke_best_time_to_buy_and_sell_stock_ii() { + std::vector prices = {7,1,5,3,6,4}; + std::cout<<"Max profit: "< false + +nums: [2,3,1,1,4] => true +0 1 2 3 4 +2 3 1 1 4 => 1 1$; 2 1 1$ + +nums: [6,5,5,7,4] => true + +nums: [1 1 1 1 1] +*/ +bool jump_game(std::vector& nums) { + int last_index = nums.size() - 1; + for(int index = nums.size() - 2; index >= 0; --index) { + if(index + nums[index] >= last_index) { + last_index = index; + } + } + + return last_index <= 0; +} +void invoke_jump_game() { + std::vector nums = {2,3,1,1,4}; + std::cout< 2 +/\ +1 2 +| | +3 1 + 0 1 2 3 4 5 6 +[2,3,1,2,1,1,4] => 3 +/\ +1 2 +| | +3 1 +| +1 +|\ +1 2 + +[1,2] => 1 + +[1,2,3] => 2 +| +1 +|\ +1 2 + \ + 1* + +[1,1,1,1,2,4] = 5 + |\ + 1 2 +*/ +int jump_game_ii(std::vector& nums) { + int min_jumps = 0; + + return min_jumps; +} +void invoke_jump_game_ii() { + std::vector nums = {2,3,1,2,1,1,4}; + std::cout< MMMDCCXLIX +3000: MMM: 1000 + 1000 + 1000 +700: DCC: 500 + 100 + 100 +40: XL: 50 - 10 +9: IX: 10 - 1 + +3749 % 10 = 9 = IX +3749 % 10 = 374 => 374 % 10 = 4 * 10 = 40 = XL +37 % 10 = 7 => 7 * 100 = 700 = DCC +3 % 10 = 3 => 3 * 1000 = 3000 = MMM +*/ +std::string integer_to_roman(int number) { + std::string output; + + std::vector symbol = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; + std::vector symbol_value = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; + + for(int index = 0; number != 0; ++index) { + while(number >= symbol_value[index]) { + number -= symbol_value[index]; + output += symbol[index]; + } + } + + return output; + +// static std::unordered_map roman_map = { +// {1, "I"}, +// {5, "V"}, +// {9, "IX"}, +// {10, "X"}, +// {40, "XL"}, +// {50, "L"}, +// {100, "C"}, +// {500, "D"}, +// {700, "DCC"}, +// {1000, "M"}, +// {3000, "MMM"}, +// }; +// +// std::vector temp_output; +// +// int multiplicant = 0; +// +// do { +// auto last_number = number % 10; +// number /= 10; +// +// multiplicant *= 10; +// if(multiplicant == 0) { +// multiplicant = 1; +// } +// +// std::cout< "<= 0; --index) { +// output.append(temp_output[index]); +// } +// return output; +} +void invoke_integer_to_roman() { + std::cout< invoke_reverse_linked_list(CSingleLinkedList input_l int missing_number(); -/* - */ -void median_of_two_sorted_arrays(const std::vector& array_one, const std::vector& array_two); -void invoke_median_of_two_sorted_arrays(); - int container_with_most_water(const std::vector& container_height); void invoke_container_with_most_water(); @@ -226,4 +221,27 @@ void invoke_rotate_array(); */ void invoke_reverse_integer(); +/* +4. Median of Two Sorted Arrays: https://leetcode.com/problems/median-of-two-sorted-arrays/description/ + https://www.algoexpert.io/questions/median-of-two-sorted-arrays +*/ +void invoke_median_of_two_sorted_arrays(); + +/* + 122. Best Time to Buy and Sell Stock II https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii +*/ +void invoke_best_time_to_buy_and_sell_stock_ii(); + +/* + 55. Jump Game https://leetcode.com/problems/jump-game +*/ +void invoke_jump_game(); + +void invoke_jump_game_ii(); + +/* +12. Integer to Roman https://leetcode.com/problems/integer-to-roman +*/ +void invoke_integer_to_roman(); + #endif /* NUMBER_HPP */ diff --git a/algorithm/string.cpp b/algorithm/string.cpp index 2145ab0..a503b20 100755 --- a/algorithm/string.cpp +++ b/algorithm/string.cpp @@ -243,6 +243,7 @@ bool is_anagram(const std::string& first_word, const std::string& second_word) { return false; } +// R: O(nm) S: O(min(n,m)) int levenshtein_distance(const std::string &first_word, const std::string& second_word) { // Space: O(mn) std::vector> edit_distance_table(first_word.size() + 1); @@ -287,17 +288,14 @@ int levenshtein_distance(const std::string &first_word, const std::string& secon return edit_distance_table[first_word.size()][second_word.size()]; } - -// R: O(nm) S: O(min(n,m)) void invoke_levenshtein_distance() { std::cout<<"The number of edit operations required: "<>& input_matrix) { return 0; } - -// T: O(wh), S(wh) void invoke_river_sizes() { // Output: 2, 2, 5, 1, 2 std::vector> matrix = { @@ -319,3 +317,50 @@ void valid_ip_address(const std::string& ip_addresses) { void invoke_valid_ip_address() { valid_ip_address("1921680"); } + +bool valid_palindrome(std::string s) { + int backward_index = s.size() - 1; + for(int forward_index = 0; forward_index < backward_index;) { + if(!isalnum(s[forward_index])) { + ++forward_index; + continue; + } + if(!isalnum(s[backward_index])) { + --backward_index; + continue; + } + + if(std::tolower(s[forward_index]) != std::tolower(s[backward_index])) { + return false; + } + --backward_index; + ++forward_index; + } + return true; +} +void invoke_valid_palindrome() { + std::cout<