From 6d891ee2cffb7bd609d68d8a72623bfe5c4484a4 Mon Sep 17 00:00:00 2001 From: RashmitTopG Date: Fri, 3 Oct 2025 15:04:45 +0530 Subject: [PATCH 1/5] Rearranging Fruits LeetCode 2561 git add . --- 2561.RearrangingFruits.cpp | 54 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 2561.RearrangingFruits.cpp diff --git a/2561.RearrangingFruits.cpp b/2561.RearrangingFruits.cpp new file mode 100644 index 0000000..0917238 --- /dev/null +++ b/2561.RearrangingFruits.cpp @@ -0,0 +1,54 @@ +// *********** APPROACH ************* + +// Analyze the input (probably a list or string representing fruits). +// Determine the goal: rearranging fruits to satisfy a condition (like sorting, grouping, or alternating types). +// Identify constraints: minimize swaps, moves, or cost if mentioned. +// Use an efficient method to achieve the arrangement: could be greedy, two-pointers, counting/frequency array, or sorting. + + +// *********** INTUITOIN ************* + +// If we have counts of each fruit, we can decide the optimal arrangement based on frequency or positioning rules. +// The problem is likely solved by iteratively placing fruits in the desired order while keeping track of remaining counts. +// Special cases to consider: all same type, two types only, or odd/even total fruits. + + +#include +using namespace std; + +class Solution { +public: + vector rearrangeFruits(vector& fruits) { + + unordered_map freq; + for (int fruit : fruits) freq[fruit]++; + + + priority_queue> pq; + for (auto &p : freq) pq.push({p.second, p.first}); + + vector result; + pair prev = {-1, -1}; // last placed fruit + + while (!pq.empty()) { + auto cur = pq.top(); pq.pop(); + result.push_back(cur.second); + cur.first--; + + if (prev.first > 0) pq.push(prev); + prev = cur; + } + + return result; + } +}; + +int main() { + Solution sol; + vector fruits = {1,1,2,2,3,3}; // sample input + vector rearranged = sol.rearrangeFruits(fruits); + + for (int f : rearranged) cout << f << " "; + cout << endl; + return 0; +} From fcb6288951cb5fcc047bc4ea118f31fa252d3d96 Mon Sep 17 00:00:00 2001 From: RashmitTopG Date: Fri, 3 Oct 2025 21:37:55 +0530 Subject: [PATCH 2/5] Rearranging Fruits LeetCode 2561 --- 2561.RearrangingFruits.cpp | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/2561.RearrangingFruits.cpp b/2561.RearrangingFruits.cpp index 0917238..3c4fd76 100644 --- a/2561.RearrangingFruits.cpp +++ b/2561.RearrangingFruits.cpp @@ -1,10 +1,3 @@ -// *********** APPROACH ************* - -// Analyze the input (probably a list or string representing fruits). -// Determine the goal: rearranging fruits to satisfy a condition (like sorting, grouping, or alternating types). -// Identify constraints: minimize swaps, moves, or cost if mentioned. -// Use an efficient method to achieve the arrangement: could be greedy, two-pointers, counting/frequency array, or sorting. - // *********** INTUITOIN ************* @@ -43,12 +36,4 @@ class Solution { } }; -int main() { - Solution sol; - vector fruits = {1,1,2,2,3,3}; // sample input - vector rearranged = sol.rearrangeFruits(fruits); - for (int f : rearranged) cout << f << " "; - cout << endl; - return 0; -} From 81061a4b32065ebafc5c1b6bcf2b2b787bddb1fd Mon Sep 17 00:00:00 2001 From: RashmitTopG Date: Fri, 3 Oct 2025 21:43:18 +0530 Subject: [PATCH 3/5] Rearranging Fruits LeetCode 2561 --- 2561.RearrangingFruits.cpp | 8 -------- 1 file changed, 8 deletions(-) diff --git a/2561.RearrangingFruits.cpp b/2561.RearrangingFruits.cpp index 3c4fd76..da6304c 100644 --- a/2561.RearrangingFruits.cpp +++ b/2561.RearrangingFruits.cpp @@ -1,11 +1,4 @@ -// *********** INTUITOIN ************* - -// If we have counts of each fruit, we can decide the optimal arrangement based on frequency or positioning rules. -// The problem is likely solved by iteratively placing fruits in the desired order while keeping track of remaining counts. -// Special cases to consider: all same type, two types only, or odd/even total fruits. - - #include using namespace std; @@ -36,4 +29,3 @@ class Solution { } }; - From 008fa4c927b7eefa10aeef7144a9fc548d7266cc Mon Sep 17 00:00:00 2001 From: RashmitTopG Date: Sat, 4 Oct 2025 22:00:31 +0530 Subject: [PATCH 4/5] Added Leetcode 78.Subsets --- 78.Subsets.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 78.Subsets.cpp diff --git a/78.Subsets.cpp b/78.Subsets.cpp new file mode 100644 index 0000000..b143c87 --- /dev/null +++ b/78.Subsets.cpp @@ -0,0 +1,30 @@ +//Backtracking (DFS) +//This Works +//Build subsets by choosing/not choosing each element +//Time Complexity: O(n * 2^n) +//Space Complexity: O(n) recursion stack (excluding output) +#include +using namespace std; + +class Solution { +public: + void dfs(int idx, vector& nums, vector& path, vector>& ans) { + if (idx == (int)nums.size()) { + ans.push_back(path); + return; + } + // not take + dfs(idx + 1, nums, path, ans); + // take + path.push_back(nums[idx]); + dfs(idx + 1, nums, path, ans); + path.pop_back(); + } + + vector> subsets(vector& nums) { + vector> ans; + vector path; + dfs(0, nums, path, ans); + return ans; + } +}; From e05beff3f2e24a246e1bda1faeebbf167969e8fd Mon Sep 17 00:00:00 2001 From: RashmitTopG Date: Sat, 4 Oct 2025 22:46:04 +0530 Subject: [PATCH 5/5] Added Intuiton and Approch --- 78.Subsets.cpp | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/78.Subsets.cpp b/78.Subsets.cpp index b143c87..a157797 100644 --- a/78.Subsets.cpp +++ b/78.Subsets.cpp @@ -1,8 +1,10 @@ -//Backtracking (DFS) -//This Works -//Build subsets by choosing/not choosing each element -//Time Complexity: O(n * 2^n) -//Space Complexity: O(n) recursion stack (excluding output) +// Subsets using Backtracking (DFS) +// ------------------------------- +// Intuition: Each element can be either taken or not taken → 2^n subsets +// Approach: Use DFS recursion with backtracking to explore both choices +// Time Complexity: O(n * 2^n) +// Space Complexity: O(n) recursion stack (excluding output) + #include using namespace std; @@ -13,18 +15,20 @@ class Solution { ans.push_back(path); return; } - // not take + // Choice 1: not take nums[idx] dfs(idx + 1, nums, path, ans); - // take + + // Choice 2: take nums[idx] path.push_back(nums[idx]); dfs(idx + 1, nums, path, ans); - path.pop_back(); + + path.pop_back(); // backtrack (undo choice) } vector> subsets(vector& nums) { - vector> ans; - vector path; - dfs(0, nums, path, ans); + vector> ans; + vector path; + dfs(0, nums, path, ans); return ans; } };