diff --git a/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0004]. median_of_merge_list.cpp b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0004]. median_of_merge_list.cpp new file mode 100644 index 000000000..d70cd346a --- /dev/null +++ b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0004]. median_of_merge_list.cpp @@ -0,0 +1,32 @@ +/* +''' +4. Median of Two Sorted Arrays + +Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays. + +The overall run time complexity should be O(log (m+n)). +''' +*/ + +class Solution +{ +public: + double findMedianSortedArrays(vector &nums1, vector &nums2) + { + for (auto i : nums2) + nums1.push_back(i); + + sort(begin(nums1), end(nums1)); + + for (auto i : nums1) + cout << i << " "; + + int size = nums1.size() - 1; + + if (size % 2 == 0) + return nums1[size / 2]; + else + return (nums1[size / 2] + nums1[size / 2 + 1]) / 2.0; + return 0; + } +}; \ No newline at end of file diff --git a/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0029]. min_size_subarray_sum.cpp b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0029]. min_size_subarray_sum.cpp new file mode 100644 index 000000000..c3dd244b9 --- /dev/null +++ b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0029]. min_size_subarray_sum.cpp @@ -0,0 +1,54 @@ +/* +''' +209. Minimum Size Subarray Sum + +Given an array of positive integers nums and a positive integer target, return the minimal length of a subarray whose sum is greater than or equal to target. +If there is no such subarray, return 0 instead. +''' +*/ + +class Solution +{ +public: + int minSubArrayLen(int target, vector &nums) + { + int n = (int)nums.size(); + int sum = 0, j = 0; + int res = INT_MAX; + + for (int i = 0; i < n; i++) + { + sum += nums[i]; + while (sum >= target) + { + res = min(res, i - j + 1); + sum -= nums[j++]; + } + } + return res == INT_MAX ? 0 : res; + } +}; + +// +class Solution +{ +public: + int minSubArrayLen(int s, vector &nums) + { + int n = nums.size(), + len = INT_MAX; + vector sums(n + 1, 0); + + for (int i = 1; i <= n; i++) + { + sums[i] = sums[i - 1] + nums[i - 1]; + } + + for (int i = n; i >= 0 && sums[i] >= s; i--) + { + int j = upper_bound(sums.begin(), sums.end(), sums[i] - s) - sums.begin(); + len = min(len, i - j + 1); + } + return len == INT_MAX ? 0 : len; + } +}; \ No newline at end of file diff --git a/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0050]. pow(x,n).cpp b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0050]. pow(x,n).cpp new file mode 100644 index 000000000..ac1abc305 --- /dev/null +++ b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0050]. pow(x,n).cpp @@ -0,0 +1,58 @@ +/* +50. Pow(x, n) + +Implement pow(x, n), which calculates x raised to the power n (i.e., xn). +*/ + +class Solution +{ +public: + double myPow(double x, int n) + { + return pow(x, n); + } +}; + + + + +// + +class Solution +{ +public: + double myPower(double x, long long N) + { + if (N == 0) + return 1.0; + double y = myPower(x, N / 2); + return N % 2 == 0 ? y * y : y * y * x; + } + + double myPow(double x, int n) + { + long long N = n; + return N >= 0 ? myPower(x, N) : 1.0 / myPower(x, -N); + } +}; + + +//fast +class Solution +{ +public: + double myPow(double x, int n) + { + if (n == 0) + return 1; + if (n < 0) + { + n = abs(n); + x = 1 / x; + } + if (n % 2 == 0) + return myPow(x * x, n / 2); + else + return x * myPow(x, n - 1); + } +}; \ No newline at end of file diff --git a/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0055]. jump_to.cpp b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0055]. jump_to.cpp new file mode 100644 index 000000000..dbefde100 --- /dev/null +++ b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0055]. jump_to.cpp @@ -0,0 +1,44 @@ +/* +''' +55. Jump Game + +You are given an integer array nums. You are initially positioned at the array's first index, +and each element in the array represents your maximum jump length at that position. +Return true if you can reach the last index, or false otherwise. + +Example 1: + +Input: nums = [2,3,1,1,4] +Output: true +Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. +Example 2: + +Input: nums = [3,2,1,0,4] +Output: false +Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index. +''' +*/ + +class Solution +{ +public + boolean canJump(int[] nums) + { + // Take curr variable to keep the current maximum jump... + int curr = 0; + // Traverse all the elements through loop... + for (int i = 0; i < nums.length; i++) + { + // If the current index 'i' is less than current maximum jump 'curr'... + // It means there is no way to jump to current index... + // so we should return false... + if (i > curr) + { + return false; + } + // Update the current maximum jump... + curr = Math.max(curr, i + nums[i]); // It’s possible to reach the end of the array... + } + return true; + } +} \ No newline at end of file diff --git a/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0066]. plus_one.cpp b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0066]. plus_one.cpp new file mode 100644 index 000000000..7c1e178e7 --- /dev/null +++ b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0066]. plus_one.cpp @@ -0,0 +1,134 @@ +/* +''' +66. Plus One + +You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. +The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0's. + +Increment the large integer by one and return the resulting array of digits. +''' +*/ + +class Solution +{ +public: + vector plusOne(vector &digits) + { + long long num = 0; + for (auto i : digits) + num = num * 10 + i; + num++; + digits.clear(); + while (num) + { + int n = num % 10; + digits.emplace(begin(digits), n); + num /= 10; + } + cout << num; + return digits; + } +}; + + +// +class Solution +{ +public: + vector plusOne(vector &digits) + { + int sz = digits.size() - 1; + cout << sz; + /*if (sz==0 && digits[sz]==9) + { + digits[sz] = 0; + digits.emplace(digits.begin(), 1); + return digits; + } + else if (sz==0) + { + int n=digits[sz]; + digits[sz] = n+1; + return digits; + }*/ + while (sz >= 0) + { + + if (digits[sz] <= 8) + { + int n = digits[sz]; + digits[sz] = n + 1; + break; + } + else + { + digits[sz] = 0; + sz--; + } + if (sz == -1) + { + digits.emplace(digits.begin(), 1); + } + } + return digits; + } +}; + +// +class Solution +{ +public: + vector plusOne(vector &v) + { + int n = v.size(); + for (int i = n - 1; i >= 0; i--) + { + if (i == n - 1) + v[i]++; + if (v[i] == 10) + { + v[i] = 0; + if (i != 0) + { + v[i - 1]++; + } + else + { + v.push_back(0); + v[i] = 1; + } + } + } + return v; + } +}; + +// +class Solution +{ +public: + vector plusOne(vector &digits) + { + int n = digits.size(); + for (int i = n - 1; i >= 0; i--) + { + if (i == n - 1) + { + digits[i]++; + } + if (digits[i] == 10) + { + digits[i] = 0; + if (i != 0) + { + digits[i - 1] += 1; + } + else + { + digits.insert(digits.begin(), 1); + } + } + } + return digits; + } +}; \ No newline at end of file diff --git a/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0069]. sqrt.cpp b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0069]. sqrt.cpp new file mode 100644 index 000000000..16ed63832 --- /dev/null +++ b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0069]. sqrt.cpp @@ -0,0 +1,51 @@ +/* +''' +69. Sqrt(x) + +Given a non-negative integer x, return the square root of x rounded down to the nearest integer. The returned integer should be non-negative as well. + +You must not use any built-in exponent function or operator. + +For example, do not use pow(x, 0.5) in c++ or x ** 0.5 in python. +''' +*/ + +class Solution +{ +public: + int mySqrt(int x) + { + double i = 0; + while (i * i <= x) + { + if ((i * i) <= x && (i + 1) * (i + 1) > x) + return int(i); + i++; + } + return 0; + } +}; + +// +class Solution +{ +public: + int mySqrt(int num) + { + if (num <= 1) // square root of 0 is 0, square root of 1 is 1, square root of a negative is an imaginary. + return num; + + double tolerance = 1e-7; + + double x0 = num; + double x = x0 - (x0 * x0 - num) / (2 * x0); + + while (abs(x - x0) > tolerance) + { + x0 = x; + x = x0 - (x0 * x0 - num) / (2 * x0); + } + + return (int)x; + } +}; \ No newline at end of file diff --git a/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0070]. climbing_stairs.cpp b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0070]. climbing_stairs.cpp new file mode 100644 index 000000000..b535e248d --- /dev/null +++ b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0070]. climbing_stairs.cpp @@ -0,0 +1,31 @@ +/* +''' +70. Climbing Stairs + +You are climbing a staircase. It takes n steps to reach the top. + +Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? +''' +*/ + +class Solution +{ +public: + int climbStairs(int n) + { + if (n == 1) + return 1; + else if (n == 2) + return 2; + + int c1 = 0, c2 = 1; + while (n > 0) + { + n--; + int temp = c1; + c1 = c2; + c2 = c1 + temp; + } + return c2; + } +}; \ No newline at end of file diff --git a/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0088]. sort_merge_array.cpp b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0088]. sort_merge_array.cpp new file mode 100644 index 000000000..eb00e2b8c --- /dev/null +++ b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0088]. sort_merge_array.cpp @@ -0,0 +1,48 @@ +/* +''' +88. Merge Sorted Array + +You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, +representing the number of elements in nums1 and nums2 respectively. + +Merge nums1 and nums2 into a single array sorted in non-decreasing order. + +The final sorted array should not be returned by the function, but instead be stored inside the array nums1. +To accommodate this, nums1 has a length of m + n, where the first m elements denote the elements that should be merged, + and the last n elements are set to 0 and should be ignored. nums2 has a length of n. + +''' +*/ + + +// space +class Solution +{ +public: + void merge(vector &nums1, int m, vector &nums2, int n) + { + for (int i = 0; i < n; i++) + { + nums1[m + i] = nums2[i]; + } + sort(nums1.begin(), nums1.end()); + } +}; + + +//time +int i = m - 1; +int j = n - 1; +int k = m + n - 1; + +while (j >= 0) +{ + if (i >= 0 && nums1[i] > nums2[j]) + { + nums1[k--] = nums1[i--]; + } + else + { + nums1[k--] = nums2[j--]; + } +} \ No newline at end of file diff --git a/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0111]. maximum_depth_of_Binary_tree.cpp b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0111]. maximum_depth_of_Binary_tree.cpp new file mode 100644 index 000000000..0fb22075c --- /dev/null +++ b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0111]. maximum_depth_of_Binary_tree.cpp @@ -0,0 +1,96 @@ +/* +111. Minimum Depth of Binary Tree + +Given a binary tree, find its minimum depth. + +The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. + +Note: A leaf is a node with no children. +*/ + +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution +{ +public: + int minDepth(TreeNode *root) + { + if (root == NULL) + return 0; + + if (!root->left && !root->right) + return 1; + + if (root->left == NULL) + return 1 + minDepth(root->right); + + if (root->right == NULL) + return 1 + minDepth(root->left); + + return 1 + min(minDepth(root->left), minDepth(root->right)); + } +}; + +// +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ + +class Solution +{ +public: + int maxDepth(TreeNode *root) + { + if (root == NULL) + return 0; + + int l = maxDepth(root->left); + int r = maxDepth(root->right); + + return max(l, r) + 1; + } +}; + + +// +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ + +class Solution +{ +public: + int maxDepth(TreeNode *root) + { + if (!root) + return 0; + return std::max( + maxDepth(root->left) + 1, + maxDepth(root->right) + 1); + } +}; \ No newline at end of file diff --git a/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0111]. minimum_depth_of_Binary_tree.cpp b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0111]. minimum_depth_of_Binary_tree.cpp new file mode 100644 index 000000000..2b5476367 --- /dev/null +++ b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0111]. minimum_depth_of_Binary_tree.cpp @@ -0,0 +1,89 @@ +/* +''' +111. Minimum Depth of Binary Tree + +Given a binary tree, find its minimum depth. + +The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. + +Note: A leaf is a node with no children. +''' +*/ + +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ + +//BFS +class Solution +{ +public: + int minDepth(TreeNode *root) + { + if (root == NULL) + return 0; + + int mdepth = 0; + queue q; + q.push(root); + + while (!q.empty()) + { + int size = q.size(); + mdepth++; + while (size--) + { + TreeNode *curr = q.front(); + q.pop(); + if (curr->left) + q.push(curr->left); + if (curr->right) + q.push(curr->right); + if (curr->left == NULL && curr->right == NULL) + return mdepth; + } + } + return mdepth; + } +}; + +//DFS +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution +{ +public: + int minDepth(TreeNode *root) + { + if (root == NULL) + return 0; + + if (!root->left && !root->right) + return 1; + + if (root->left == NULL) + return 1 + minDepth(root->right); + + if (root->right == NULL) + return 1 + minDepth(root->left); + + return 1 + min(minDepth(root->left), minDepth(root->right)); + } +}; \ No newline at end of file diff --git a/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0125]. ispalindrome.cpp b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0125]. ispalindrome.cpp new file mode 100644 index 000000000..184d7ef79 --- /dev/null +++ b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0125]. ispalindrome.cpp @@ -0,0 +1,74 @@ +/* +''' +125. Valid Palindrome + +A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, +it reads the same forward and backward. Alphanumeric characters include letters and numbers. + +Given a string s, return true if it is a palindrome, or false otherwise. +''' +*/ + +class Solution +{ +public: + bool isPalindrome(string s) + { + int start = 0; + int end = s.size() - 1; + + while (start <= end) + { + if (!isalnum(s[start])) + { + start++; + continue; + } + if (!isalnum(s[end])) + { + end--; + continue; + } + if (tolower(s[start]) != tolower(s[end])) + return false; + else + { + start++; + end--; + } + } + return true; + } +}; + + +// +class Solution +{ +public: + bool isPalindrome(string s) + { + if (s.empty()) + return true; + vector v; + for (auto ch : s) + { + if (isalnum(ch)) + { + v.push_back(tolower(ch)); + } + } + int begin = 0; + int end = v.size() - 1; + while (begin < end) + { + if (v[begin] != v[end]) + { + return false; + } + begin++; + end--; + } + return true; + } +}; \ No newline at end of file diff --git a/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0169]. majority_element.cpp b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0169]. majority_element.cpp new file mode 100644 index 000000000..916226c0b --- /dev/null +++ b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0169]. majority_element.cpp @@ -0,0 +1,55 @@ +/* +''' +169. Majority Element + +Given an array nums of size n, return the majority element. + +The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array. +''' + +*/ + +class Solution +{ +public: + int majorityElement(vector &nums) + { + int n = nums.size(); + sort(begin(nums), end(nums)); + + if (nums[0] == nums[n / 2]) + return nums[0]; + else if (nums[n - 1] == nums[n / 2]) + return nums[n - 1]; + else + return nums[n / 2]; + } +}; + +//time +class Solution +{ +public: + int majorityElement(vector &nums) + { + int candidate = nums[0]; + int score = 0; + for (auto n : nums) + { + if (n == candidate) + { + score++; + } + else + { + score--; + if (score < 0) + { + candidate = n; + score = 1; + } + } + } + return candidate; + } +}; \ No newline at end of file diff --git a/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0205]. isomorphic_strings.cpp b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0205]. isomorphic_strings.cpp new file mode 100644 index 000000000..7cbc5976c --- /dev/null +++ b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0205]. isomorphic_strings.cpp @@ -0,0 +1,36 @@ +/* +''' +205. Isomorphic Strings + +Given two strings s and t, determine if they are isomorphic. + +Two strings s and t are isomorphic if the characters in s can be replaced to get t. + +All occurrences of a character must be replaced with another character while preserving the order of characters. +No two characters may map to the same character, but a character may map to itself. +''' +*/ + +class Solution +{ +public: + bool isIsomorphic(string s, string t) + { + map map; + set set; + int n = s.size() - 1; + + while (n >= 0) + { + if (map.count(s[n]) && map[s[n]] != t[n]) + return false; + if (!map.count(s[n]) && set.count(t[n])) + return false; + + set.insert(t[n]); + map[s[n]] = t[n]; + n--; + } + return true; + } +}; \ No newline at end of file diff --git a/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0231]. powOfTwo.cpp b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0231]. powOfTwo.cpp new file mode 100644 index 000000000..0b09e964d --- /dev/null +++ b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0231]. powOfTwo.cpp @@ -0,0 +1,44 @@ +/* +231. Power of Two + +Given an integer n, return true if it is a power of two. Otherwise, return false. + +An integer n is a power of two, if there exists an integer x such that n == 2x. +*/ + +class Solution +{ +public: + bool isPowerOfTwo(int n) + { + if (n == 1 or n == 2) + return true; + long long int i = 2; + while (1) + { + if (i * 2 == n) + return true; + else if (i * 2 > n) + return false; + else + i = i * 2; + cout << i << endl; + } + } +}; + +//best +class Solution +{ +public: + bool isPowerOfTwo(int n) + { + if (n == 1 || n == 2) + return true; + if (n == 0 || n == INT_MIN) + return false; + if (n & (n - 1)) + return false; + return true; + } +}; \ No newline at end of file diff --git a/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0383]. ransom_note.cpp b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0383]. ransom_note.cpp new file mode 100644 index 000000000..fd6f871fa --- /dev/null +++ b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0383]. ransom_note.cpp @@ -0,0 +1,98 @@ +/* +''' +383. Ransom Note + +Given two strings ransomNote and magazine, return true if ransomNote can be constructed by using the letters from magazine and false otherwise. + +Each letter in magazine can only be used once in ransomNote. + + + +Example 1: + +Input: ransomNote = "a", magazine = "b" +Output: false +Example 2: + +Input: ransomNote = "aa", magazine = "ab" +Output: false +Example 3: + +Input: ransomNote = "aa", magazine = "aab" +Output: true +''' +*/ + +class Solution +{ +public: + bool canConstruct(string ransomNote, string magazine) + { + unordered_map dic; + + for (auto c : magazine) + { + if (dic.find(c) == dic.end()) + dic[c] = 1; + else + dic[c]++; + } + + for (char c : ransomNote) + { + if (dic.find(c) != dic.end() && dic[c] > 0) + dic[c]--; + else + return false; + } + + return true; + } +}; + +// +class Solution +{ +public: + bool canConstruct(string ransomNote, string magazine) + { + sort(begin(ransomNote), end(ransomNote)); + sort(begin(magazine), end(magazine)); + + int i = 0, j = 0; + + while (i < ransomNote.length() && j < magazine.size()) + { + if (ransomNote[i] == magazine[j]) + { + i++; + j++; + } + else if (ransomNote[i] > magazine[j]) + j++; + else + return false; + } + + return i == ransomNote.size(); + } +}; + +//best +class Solution +{ +public: + bool canConstruct(string ransomNote, string magazine) + { + valarray count(0, 26); + for (char c : ransomNote) + { + count[c - 'a']++; + } + for (char c : magazine) + { + count[c - 'a']--; + } + return count.max() <= 0; + } +}; \ No newline at end of file diff --git a/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0704]. binary_search.cpp b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0704]. binary_search.cpp new file mode 100644 index 000000000..7634b37f7 --- /dev/null +++ b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0704]. binary_search.cpp @@ -0,0 +1,59 @@ +/* +704. Binary Search + +Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. +If target exists, then return its index. Otherwise, return -1. + +You must write an algorithm with O(log n) runtime complexity. +*/ + +class Solution +{ +public: + int search(vector &nums, int target) + { + int len = nums.size(); + int index = -1, i; + for (i = 0; i < len; i++) + { + if (nums[i] == target) + { + index = i; + break; + } + } + // cout< &nums, int target) + { + ios_base::sync_with_stdio(false); + int n = (int)nums.size(); + int low = 0; + int high = n - 1; + while (low <= high) + { + int mid = (low + high) / 2; + + if (nums[mid] == target) + { + return mid; + } + else if (nums[mid] < target) + { + low = mid + 1; + } + else + { + high = mid - 1; + } + } + return -1; + } +}; \ No newline at end of file diff --git a/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0860]. lemonade.cpp b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0860]. lemonade.cpp new file mode 100644 index 000000000..ae631341c --- /dev/null +++ b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/0860]. lemonade.cpp @@ -0,0 +1,62 @@ +/* +860. Lemonade Change + +At a lemonade stand, each lemonade costs $5. Customers are standing in a queue to buy from you and order one at a time (in the order specified by bills). +Each customer will only buy one lemonade and pay with either a $5, $10, or $20 bill. +You must provide the correct change to each customer so that the net transaction is that the customer pays $5. + +Note that you do not have any change in hand at first. + +Given an integer array bills where bills[i] is the bill the ith customer pays, return true if you can provide every customer with the correct change, +or false otherwise. +*/ + +class Solution +{ +public: + bool lemonadeChange(vector &bills) + { + if (bills[0] == 10 || bills[0] == 20 || bills[1] == 20) + return false; + + unordered_map m; + for (int i = 0; i < bills.size(); i++) + { + if (bills[i] == 5) + { + m[5]++; + } + else if (bills[i] == 10) + { + m[10]++; + if (bills[i] >= 1) + { + m[5]--; + } + else + return false; + } + else if (bills[i] == 20) + { + m[20]++; + if (m[10] >= 1 && m[5] >= 1) + { + m[10]--; + m[5]--; + continue; + } + else if (m[5] >= 3) + { + m[5]--; + m[5]--; + m[5]--; + } + else + return false; + } + else + return false; + } + return true; + } +}; \ No newline at end of file diff --git a/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/1331]. rank_array.cpp b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/1331]. rank_array.cpp new file mode 100644 index 000000000..83d3db1db --- /dev/null +++ b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/1331]. rank_array.cpp @@ -0,0 +1,75 @@ +/*- +1331. Rank Transform of an Array + +Given an array of integers arr, replace each element with its rank. + +The rank represents how large the element is. The rank has the following rules: + +Rank is an integer starting from 1. +The larger the element, the larger the rank. If two elements are equal, their rank must be the same. +Rank should be as small as possible. +*/ + +//map only +class Solution +{ +public: + vector arrayRankTransform(vector &arr) + { + map mp; + for (auto &it : arr) + mp[it] = 0; + int rank = 0; + for (auto &it : mp) + mp[it.first] = ++rank; + for (int i = 0; i < arr.size(); i++) + arr[i] = mp[arr[i]]; + + return arr; + } +}; + +//sort and map +class Solution +{ +public: + vector arrayRankTransform(vector &arr) + { + map mp; + vector temp = arr; + int rank = 1; + + sort(temp.begin(), temp.end()); + auto it = unique(temp.begin(), temp.end()); + temp.resize(distance(temp.begin(), it)); + + for (int i = 0; i < temp.size(); i++) + mp[temp[i]] = rank, ++rank; + + for (int i = 0; i < arr.size(); i++) + arr[i] = mp[arr[i]]; + + return arr; + } +}; + +//map and set + +class Solution +{ +public: + vector arrayRankTransform(vector &arr) + { + map mp; + set s; + for (int i = 0; i < arr.size(); i++) + s.insert(arr[i]); + int rank = 1; + for (auto &it : s) + mp[it] = rank, ++rank; + for (int i = 0; i < arr.size(); i++) + arr[i] = mp[arr[i]]; + + return arr; + } +}; \ No newline at end of file diff --git a/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/1870]. min_time_to_reach.cpp b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/1870]. min_time_to_reach.cpp new file mode 100644 index 000000000..915681990 --- /dev/null +++ b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/1870]. min_time_to_reach.cpp @@ -0,0 +1,80 @@ +/* +''' +1870. Minimum Speed to Arrive on Time + +You are given a floating-point number hour, representing the amount of time you have to reach the office. +To commute to the office, you must take n trains in sequential order. +You are also given an integer array dist of length n, where dist[i] describes the distance (in kilometers) of the ith train ride. + +Each train can only depart at an integer hour, so you may need to wait in between each train ride. + +For example, if the 1st train ride takes 1.5 hours, you must wait for an additional 0.5 hours before you can depart on the 2nd train ride at the 2 hour mark. + +Return the minimum positive integer speed (in kilometers per hour) that all the trains must travel at for you to reach the office on time, +or -1 if it is impossible to be on time. +Tests are generated such that the answer will not exceed 10^7 and hour will have at most two digits after the decimal point. + +Example 1: + +Input: dist = [1,3,2], hour = 6 +Output: 1 +Explanation: At speed 1: +- The first train ride takes 1/1 = 1 hour. +- Since we are already at an integer hour, we depart immediately at the 1 hour mark. The second train takes 3/1 = 3 hours. +- Since we are already at an integer hour, we depart immediately at the 4 hour mark. The third train takes 2/1 = 2 hours. +- You will arrive at exactly the 6 hour mark. + +Example 2: + +Input: dist = [1,3,2], hour = 2.7 +Output: 3 +Explanation: At speed 3: +- The first train ride takes 1/3 = 0.33333 hours. +- Since we are not at an integer hour, we wait until the 1 hour mark to depart. The second train ride takes 3/3 = 1 hour. +- Since we are already at an integer hour, we depart immediately at the 2 hour mark. The third train takes 2/3 = 0.66667 hours. +- You will arrive at the 2.66667 hour mark. + +Example 3: + +Input: dist = [1,3,2], hour = 1.9 +Output: -1 +Explanation: It is impossible because the earliest the third train can depart is at the 2 hour mark. + +''' +*/ + +class Solution +{ +public: + double timer(vector &dist, int speed) + { + double time = 0; + for (int i = 0; i < dist.size(); i++) + { + double t = (double)dist[i] / (double)speed; + + time += (i == dist.size() - 1 ? t : ceil(t)); + } + return time; + } + int minSpeedOnTime(vector &dist, double hour) + { + int left = 1, right = 1e7, minspeed = -1; + + while (left <= right) + { + int mid = (left + right) / 2; + + if (timer(dist, mid) <= hour) + { + minspeed = mid; + right = mid - 1; + } + else + { + left = mid + 1; + } + } + return minspeed; + } +}; \ No newline at end of file diff --git a/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/2000]. reverse_of_substring.cpp b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/2000]. reverse_of_substring.cpp new file mode 100644 index 000000000..3a8dc705b --- /dev/null +++ b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/2000]. reverse_of_substring.cpp @@ -0,0 +1,94 @@ +/* +2000. Reverse Prefix of Word + +Given a 0-indexed string word and a character ch, reverse the segment of word that starts at index 0 and ends at the index of the first occurrence of ch +(inclusive). If the character ch does not exist in word, do nothing. + +For example, if word = "abcdefd" and ch = "d", then you should reverse the segment that starts at 0 and ends at 3 (inclusive). +The resulting string will be "dcbaefd". +Return the resulting string. + +Example 1: + +Input: word = "abcdefd", ch = "d" +Output: "dcbaefd" +Explanation: The first occurrence of "d" is at index 3. +Reverse the part of word from 0 to 3 (inclusive), the resulting string is "dcbaefd". +Example 2: + +Input: word = "xyxzxe", ch = "z" +Output: "zxyxxe" +Explanation: The first and only occurrence of "z" is at index 3. +Reverse the part of word from 0 to 3 (inclusive), the resulting string is "zxyxxe". +Example 3: + +Input: word = "abcd", ch = "z" +Output: "abcd" +Explanation: "z" does not exist in word. +You should not do any reverse operation, the resulting string is "abcd". + +*/ + +class Solution +{ +public: + string reversePrefix(string word, char ch) + { + int ind = 0; + for (auto c : word) + { + ind = word.find(ch); + if (ind == -1) + return word; + } + string str1 = ""; + string str2 = ""; + for (int i = 0; i < word.length(); i++) + { + if (i <= ind) + str1 += word[i]; + else + str2 += word[i]; + } + int i = 0; + int n = str1.length() - 1; + while (i <= n) + { + swap(str1[i], str1[n]); + i++; + n--; + } + str1 += str2; + cout << str1 << endl; + cout << str2; + + return str1; + } +}; + +// +class Solution +{ +public: + string reversePrefix(string word, char ch) + { + string w = ""; + int index = word.find(ch); + if (index == 0) + { + return word; + } + else + { + for (int i = index; i >= 0; i--) + { + w += word[i]; + } + for (int i = index + 1; i < word.size(); i++) + { + w += word[i]; + } + return w; + } + } +}; \ No newline at end of file diff --git a/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/2784]. good_array.cpp b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/2784]. good_array.cpp new file mode 100644 index 000000000..81ec1fcd5 --- /dev/null +++ b/1]. DSA + CP/2]. Competitive Programming/03]. LeetCode/1]. Problems/C++/2784]. good_array.cpp @@ -0,0 +1,102 @@ +/* +''' +2784. Check if Array is Good + +You are given an integer array nums. We consider an array good if it is a permutation of an array base[n]. + +base[n] = [1, 2, ..., n - 1, n, n] (in other words, it is an array of length n + 1 which contains 1 to n - 1 exactly once, plus two occurrences of n). +For example, base[1] = [1, 1] and base[3] = [1, 2, 3, 3]. + +Return true if the given array is good, otherwise return false. + +Note: A permutation of integers represents an arrangement of these numbers. + +Example 1: + +Input: nums = [2, 1, 3] +Output: false +Explanation: Since the maximum element of the array is 3, the only candidate n for which this array could be a permutation of base[n], is n = 3. +However, base[3] has four elements but array nums has three. Therefore, it can not be a permutation of base[3] = [1, 2, 3, 3]. So the answer is false. +Example 2: + +Input: nums = [1, 3, 3, 2] +Output: true +Explanation: Since the maximum element of the array is 3, the only candidate n for which this array could be a permutation of base[n], is n = 3. +It can be seen that nums is a permutation of base[3] = [1, 2, 3, 3] (by swapping the second and fourth elements in nums, we reach base[3]). +Therefore, the answer is true. +Example 3: + +Input: nums = [1, 1] +Output: true +Explanation: Since the maximum element of the array is 1, the only candidate n for which this array could be a permutation of base[n], is n = 1. +It can be seen that nums is a permutation of base[1] = [1, 1]. Therefore, the answer is true. +Example 4: + +Input: nums = [3, 4, 4, 1, 2, 1] +Output: false +Explanation: Since the maximum element of the array is 4, the only candidate n for which this array could be a permutation of base[n], is n = 4. + However, base[4] has five elements but array nums has six. Therefore, it can not be a permutation of base[4] = [1, 2, 3, 4, 4]. So the answer is false. +''' +*/ + +class Solution +{ +public: + bool isGood(vector &nums) + { + + sort(nums.begin(), nums.end()); + int size = nums.size(); + int last = nums[size - 1]; + + if (last + 1 != size) + return false; + + unordered_map map; + + for (int i = 1; i < last + 1; i++) + map[i] = 0; + + for (auto x : nums) + map[x]++; + + for (auto x : map) + cout << x.first << " " << x.second << endl; + + auto it = min_element(begin(map), end(map), [](const auto &l, const auto &r) + { return l.second < r.second; }); + + if (map[last] == 2 && it->second > 0) + return true; + return false; + } +}; + + +// +class Solution +{ +public: + bool isGood(vector &nums) + { + sort(nums.begin(), nums.end()); + int maxi = *max_element(nums.begin(), nums.end()); + int n = nums.size(); + if (maxi + 1 != n) + return 0; + int cnt = 0, count = 0; + for (auto it : nums) + if (it == maxi) + cnt++; + if (cnt != 2) + return 0; + for (int i = 1; i < n; i++) + { + if (nums[i] == nums[i - 1]) + count++; + if (count > 1) + return 0; + } + return 1; + } +}; \ No newline at end of file