Skip to content

Commit de17ef0

Browse files
authored
Create 04 - Space Optimized | DP | Approach.cpp
1 parent ca15d21 commit de17ef0

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
class Solution {
2+
public:
3+
// Function to solve the problem iteratively with optimized space
4+
// `nums1` and `nums2` are the input arrays
5+
// `index` is the starting index for processing
6+
// `n` is the size of the arrays
7+
// `swapped` is the initial state indicating whether a swap was performed
8+
int solve(vector<int>& nums1, vector<int>& nums2, int index, int n, bool swapped) {
9+
// DP arrays to store the results of the current and previous states
10+
vector<vector<int>> dp(n + 1, vector<int>(2, 0));
11+
12+
// Variables to store the state of swapping for the previous index
13+
int swap = 0; // Minimum swaps needed if the previous state involved a swap
14+
int noswap = 0; // Minimum swaps needed if the previous state did not involve a swap
15+
int currswap = 0; // Current state when a swap occurs
16+
int currnoswap = 0; // Current state when no swap occurs
17+
18+
// Iterate backwards from the second last index to the first index
19+
for (int index = n - 1; index >= 1; index--) {
20+
for (int swapped = 1; swapped >= 0; swapped--) {
21+
int ans = INT_MAX; // Initialize the minimum swaps for this state
22+
23+
// Determine the previous elements
24+
int prev1 = nums1[index - 1];
25+
int prev2 = nums2[index - 1];
26+
27+
// Adjust previous elements if the previous state was swapped
28+
if (swapped) {
29+
int temp = prev2;
30+
prev2 = prev1;
31+
prev1 = temp;
32+
}
33+
34+
// Case 1: No swap at the current index
35+
// Check if the current elements maintain the increasing order without a swap
36+
if (nums1[index] > prev1 && nums2[index] > prev2)
37+
ans = noswap;
38+
39+
// Case 2: Swap at the current index
40+
// Check if swapping the current elements maintains the increasing order
41+
if (nums1[index] > prev2 && nums2[index] > prev1)
42+
ans = min(ans, 1 + swap);
43+
44+
// Update the current state variables
45+
if (swapped)
46+
currswap = ans; // If current state involves a swap
47+
else
48+
currnoswap = ans; // If current state does not involve a swap
49+
}
50+
51+
// Update the previous state variables for the next iteration
52+
swap = currswap;
53+
noswap = currnoswap;
54+
}
55+
56+
// Return the minimum swaps needed starting from index 1 without a swap
57+
return min(swap, noswap);
58+
}
59+
60+
// Main function to find the minimum number of swaps required
61+
int minSwap(vector<int>& nums1, vector<int>& nums2) {
62+
// Insert dummy values at the start of the arrays to simplify comparisons
63+
nums1.insert(nums1.begin(), -1);
64+
nums2.insert(nums2.begin(), -1);
65+
66+
// Get the size of the arrays
67+
int n = nums1.size();
68+
69+
// Call the solve function to compute the result
70+
bool swapped = 0;
71+
return solve(nums1, nums2, 1, n, swapped);
72+
}
73+
};

0 commit comments

Comments
 (0)