From 791d9d2f93cbad7c7e58e7d6f1d0c627ad3e72d2 Mon Sep 17 00:00:00 2001 From: Avani Mathur <151984867+avanimathur@users.noreply.github.com> Date: Wed, 17 Jan 2024 18:14:43 +0530 Subject: [PATCH 1/5] Add files via upload Day18 q2 solution --- .../solution_C++.cpp | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 Day-18/Day-18/q2: Search in Rotated Sorted Array/solution_C++.cpp diff --git a/Day-18/Day-18/q2: Search in Rotated Sorted Array/solution_C++.cpp b/Day-18/Day-18/q2: Search in Rotated Sorted Array/solution_C++.cpp new file mode 100644 index 00000000..de180cc1 --- /dev/null +++ b/Day-18/Day-18/q2: Search in Rotated Sorted Array/solution_C++.cpp @@ -0,0 +1,49 @@ +#include +using namespace std; + +class Solution { +public: + // Function to perform the search in a rotated array + int search(const vector& nums, int target) { + // Check if the array is empty + if (nums.empty()) { + return -1; + } + + // Binary search to find the pivot point + int left = 0, right = nums.size() - 1; + while (left < right) { + int mid = (left + right) / 2; + + if (nums[mid] > nums[right]) { + left = mid + 1; + } else { + right = mid; + } + } + + // 'left' now contains the pivot index + int pivot = left; + + // Reset pointers for the second binary search + left = 0; + right = nums.size() - 1; + + // Binary search to find the target in the rotated array + while (left <= right) { + int mid = (left + right) / 2; + int midVal = nums[(mid + pivot) % nums.size()]; + + if (midVal == target) { + return (mid + pivot) % nums.size(); + } else if (midVal < target) { + left = mid + 1; + } else { + right = mid - 1; + } + } + + // If Target not found + return -1; + } +}; From d85065abe3e1e1000e7df0027ae35c72d22b1807 Mon Sep 17 00:00:00 2001 From: Avani Mathur <151984867+avanimathur@users.noreply.github.com> Date: Wed, 17 Jan 2024 18:19:17 +0530 Subject: [PATCH 2/5] Solution Day19_q1 --- .../Day19_q1Solution.cpp | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Day-19/q1 Find Players With Zero or One Losses/Day19_q1Solution.cpp diff --git a/Day-19/q1 Find Players With Zero or One Losses/Day19_q1Solution.cpp b/Day-19/q1 Find Players With Zero or One Losses/Day19_q1Solution.cpp new file mode 100644 index 00000000..fe9d6d4d --- /dev/null +++ b/Day-19/q1 Find Players With Zero or One Losses/Day19_q1Solution.cpp @@ -0,0 +1,35 @@ +#include +using namespace std; + +class Solution { +public: + vector> findPlayers(vector>& matches) { + // Dictionary to store the number of losses for each player + unordered_map lossesCount; + + // Count the number of losses for each player + for (const auto& match : matches) { + lossesCount[match[1]]++; + } + + // Vectors to store players with zero and one losses + vector zeroLosses; + vector oneLoss; + + // Separate players into zero and one losses categories + for (const auto& entry : lossesCount) { + if (entry.second == 0) { + zeroLosses.push_back(entry.first); + } else if (entry.second == 1) { + oneLoss.push_back(entry.first); + } + } + + // Sort the lists in increasing order + sort(zeroLosses.begin(), zeroLosses.end()); + sort(oneLoss.begin(), oneLoss.end()); + + // Return the result in the required format + return {zeroLosses, oneLoss}; + } +}; From f347de11ee185d31c6fccf579bd45b3dd3a06732 Mon Sep 17 00:00:00 2001 From: Avani Mathur <151984867+avanimathur@users.noreply.github.com> Date: Sun, 21 Jan 2024 08:16:58 +0530 Subject: [PATCH 3/5] Update and rename solution_C++.cpp to avanimathur--C.cpp --- .../{solution_C++.cpp => avanimathur--C.cpp} | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) rename Day-18/Day-18/q2: Search in Rotated Sorted Array/{solution_C++.cpp => avanimathur--C.cpp} (70%) diff --git a/Day-18/Day-18/q2: Search in Rotated Sorted Array/solution_C++.cpp b/Day-18/Day-18/q2: Search in Rotated Sorted Array/avanimathur--C.cpp similarity index 70% rename from Day-18/Day-18/q2: Search in Rotated Sorted Array/solution_C++.cpp rename to Day-18/Day-18/q2: Search in Rotated Sorted Array/avanimathur--C.cpp index de180cc1..44ddf0aa 100644 --- a/Day-18/Day-18/q2: Search in Rotated Sorted Array/solution_C++.cpp +++ b/Day-18/Day-18/q2: Search in Rotated Sorted Array/avanimathur--C.cpp @@ -3,14 +3,13 @@ using namespace std; class Solution { public: - // Function to perform the search in a rotated array + int search(const vector& nums, int target) { - // Check if the array is empty + if (nums.empty()) { return -1; } - - // Binary search to find the pivot point + int left = 0, right = nums.size() - 1; while (left < right) { int mid = (left + right) / 2; @@ -22,14 +21,11 @@ class Solution { } } - // 'left' now contains the pivot index int pivot = left; - // Reset pointers for the second binary search left = 0; right = nums.size() - 1; - // Binary search to find the target in the rotated array while (left <= right) { int mid = (left + right) / 2; int midVal = nums[(mid + pivot) % nums.size()]; @@ -43,7 +39,6 @@ class Solution { } } - // If Target not found return -1; } }; From 0f75a819626e418ca5b203a579aafccb48e8768e Mon Sep 17 00:00:00 2001 From: Avani Mathur <151984867+avanimathur@users.noreply.github.com> Date: Sun, 21 Jan 2024 08:18:11 +0530 Subject: [PATCH 4/5] Update and rename Day19_q1Solution.cpp to avanimathur--C.cpp --- .../{Day19_q1Solution.cpp => avanimathur--C.cpp} | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) rename Day-19/q1 Find Players With Zero or One Losses/{Day19_q1Solution.cpp => avanimathur--C.cpp} (65%) diff --git a/Day-19/q1 Find Players With Zero or One Losses/Day19_q1Solution.cpp b/Day-19/q1 Find Players With Zero or One Losses/avanimathur--C.cpp similarity index 65% rename from Day-19/q1 Find Players With Zero or One Losses/Day19_q1Solution.cpp rename to Day-19/q1 Find Players With Zero or One Losses/avanimathur--C.cpp index fe9d6d4d..1be23c09 100644 --- a/Day-19/q1 Find Players With Zero or One Losses/Day19_q1Solution.cpp +++ b/Day-19/q1 Find Players With Zero or One Losses/avanimathur--C.cpp @@ -4,19 +4,16 @@ using namespace std; class Solution { public: vector> findPlayers(vector>& matches) { - // Dictionary to store the number of losses for each player + unordered_map lossesCount; - - // Count the number of losses for each player + for (const auto& match : matches) { lossesCount[match[1]]++; } - // Vectors to store players with zero and one losses vector zeroLosses; vector oneLoss; - // Separate players into zero and one losses categories for (const auto& entry : lossesCount) { if (entry.second == 0) { zeroLosses.push_back(entry.first); @@ -25,11 +22,9 @@ class Solution { } } - // Sort the lists in increasing order sort(zeroLosses.begin(), zeroLosses.end()); sort(oneLoss.begin(), oneLoss.end()); - // Return the result in the required format return {zeroLosses, oneLoss}; } }; From f5d15cce90614c36d3f43600f17046e8888f5d5e Mon Sep 17 00:00:00 2001 From: Bhumika Gupta <95757762+bh-g@users.noreply.github.com> Date: Tue, 23 Jan 2024 02:52:52 +0530 Subject: [PATCH 5/5] Delete Day-18/Day-18/q2: Search in Rotated Sorted Array/avanimathur--C.cpp --- .../avanimathur--C.cpp | 44 ------------------- 1 file changed, 44 deletions(-) delete mode 100644 Day-18/Day-18/q2: Search in Rotated Sorted Array/avanimathur--C.cpp diff --git a/Day-18/Day-18/q2: Search in Rotated Sorted Array/avanimathur--C.cpp b/Day-18/Day-18/q2: Search in Rotated Sorted Array/avanimathur--C.cpp deleted file mode 100644 index 44ddf0aa..00000000 --- a/Day-18/Day-18/q2: Search in Rotated Sorted Array/avanimathur--C.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include -using namespace std; - -class Solution { -public: - - int search(const vector& nums, int target) { - - if (nums.empty()) { - return -1; - } - - int left = 0, right = nums.size() - 1; - while (left < right) { - int mid = (left + right) / 2; - - if (nums[mid] > nums[right]) { - left = mid + 1; - } else { - right = mid; - } - } - - int pivot = left; - - left = 0; - right = nums.size() - 1; - - while (left <= right) { - int mid = (left + right) / 2; - int midVal = nums[(mid + pivot) % nums.size()]; - - if (midVal == target) { - return (mid + pivot) % nums.size(); - } else if (midVal < target) { - left = mid + 1; - } else { - right = mid - 1; - } - } - - return -1; - } -};