Skip to content

Commit

Permalink
add 801 java, progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Sep 9, 2024
1 parent 027e185 commit 41255d3
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1084,7 +1084,7 @@
788 | [Rotated Digits](https://leetcode.com/problems/rotated-digits/) | [Python](./leetcode_python//Dynamic_Programming/rotated-digits.py) | _O(logn)_ | _O(logn)_ | Easy |`Brute Force`, Memoization | OK*
790 | [Domino and Tromino Tiling](https://leetcode.com/problems/domino-and-tromino-tiling/) |[Python](./leetcode_python//Dynamic_Programming/domino-and-tromino-tiling.py) | _O(logn)_ | _O(1)_ | Medium | Matrix Exponentiation, `dp`, `google`| AGAIN (not start)
799 | [Champagne Tower](https://leetcode.com/problems/champagne-tower/) | [Python](./leetcode_python//Dynamic_Programming/champagne-tower.py) | _O(n^2)_ | _O(n)_ | Medium |`dp`| AGAIN
801 | [Minimum Swaps To Make Sequences Increasing](https://leetcode.com/problems/minimum-swaps-to-make-sequences-increasing/) | [Python](./leetcode_python//Dynamic_Programming/minimum-swaps-to-make-sequences-increasing.py) | _O(n)_ | _O(1)_ | Medium |`dp`, `amazon`, `fb`| AGAIN***** (not start) (2)
801 | [Minimum Swaps To Make Sequences Increasing](https://leetcode.com/problems/minimum-swaps-to-make-sequences-increasing/) | [Python](./leetcode_python//Dynamic_Programming/minimum-swaps-to-make-sequences-increasing.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/MinimumSwapsToMakeSequencesIncreasing.java) | _O(n)_ | _O(1)_ | Medium |`dp`, `amazon`, `fb`| AGAIN***** (not start) (2)
808 | [Soup Servings](https://leetcode.com/problems/soup-servings/) | [Python](./leetcode_python//Dynamic_Programming/soup-servings.py) | _O(1)_ | _O(1)_ | Medium || AGAIN (not start)
813 | [Largest Sum of Averages](https://leetcode.com/problems/largest-sum-of-averages/) | [Python](./leetcode_python//Dynamic_Programming/largest-sum-of-averages.py) | _O(k * n^2)_ | _O(n)_ | Medium |`dp`, `google`| AGAIN (not start)
823 | [Binary Trees With Factors](https://leetcode.com/problems/binary-trees-with-factors/) | [Python](./leetcode_python/Dynamic_Programming/binary-trees-with-factors.py) | _O(n^2)_ | _O(n)_ | Medium |`dp`| AGAIN (not start)
Expand Down
3 changes: 3 additions & 0 deletions data/progress.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Progress

# 2024-09-09
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf

# 2024-09-08
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf

Expand Down
3 changes: 2 additions & 1 deletion data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
20240908: 1057,1066
20240909: 801
20240908: 1057,1066,1110
20240907: 315
20240831: 1110, 1055
20240824: 359,1057,1055(todo)
Expand Down
26 changes: 16 additions & 10 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
2024-11-02 -> ['1057,1066']
2024-11-03 -> ['801']
2024-11-02 -> ['1057,1066,1110']
2024-10-25 -> ['1110, 1055']
2024-10-18 -> ['359,1057,1055(todo)']
2024-10-12 -> ['1057,1066']
2024-10-13 -> ['801']
2024-10-12 -> ['1057,1066,1110']
2024-10-04 -> ['315', '1110, 1055']
2024-09-29 -> ['1057,1066']
2024-09-30 -> ['801']
2024-09-29 -> ['1057,1066,1110']
2024-09-27 -> ['359,1057,1055(todo)']
2024-09-21 -> ['1057,1066', '1110, 1055']
2024-09-16 -> ['1057,1066']
2024-09-22 -> ['801']
2024-09-21 -> ['1057,1066,1110', '1110, 1055']
2024-09-17 -> ['801']
2024-09-16 -> ['1057,1066,1110']
2024-09-15 -> ['809']
2024-09-14 -> ['359,1057,1055(todo)']
2024-09-13 -> ['1057,1066', '315', '1110, 1055']
2024-09-11 -> ['1057,1066']
2024-09-10 -> ['1057,1066']
2024-09-09 -> ['1057,1066']
2024-09-14 -> ['801', '359,1057,1055(todo)']
2024-09-13 -> ['1057,1066,1110', '315', '1110, 1055']
2024-09-12 -> ['801']
2024-09-11 -> ['801', '1057,1066,1110']
2024-09-10 -> ['801', '1057,1066,1110']
2024-09-09 -> ['1057,1066,1110']
2024-09-08 -> ['1110, 1055']
2024-09-06 -> ['359,1057,1055(todo)']
2024-09-05 -> ['1110, 1055']
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package LeetCodeJava.DynamicProgramming;

// https://leetcode.com/problems/minimum-swaps-to-make-sequences-increasing/description/

import java.util.Arrays;

public class MinimumSwapsToMakeSequencesIncreasing {

// V0
// TODO : implement
// public int minSwap(int[] nums1, int[] nums2) {
//
// }

// V1
// IDEA : DP
// https://leetcode.com/problems/minimum-swaps-to-make-sequences-increasing/solutions/3471328/solution/
public int minSwap_1(int[] A, int[] B) {
int swapRecord = 1, fixRecord = 0;
for (int i = 1; i < A.length; i++) {
if (A[i - 1] >= B[i] || B[i - 1] >= A[i]) {
swapRecord++;
} else if (A[i - 1] >= A[i] || B[i - 1] >= B[i]) {
int temp = swapRecord;
swapRecord = fixRecord + 1;
fixRecord = temp;
} else {
int min = Math.min(swapRecord, fixRecord);
swapRecord = min + 1;
fixRecord = min;
}
}
return Math.min(swapRecord, fixRecord);
}

// V2
// IDEA : DFS + MEMORY
// https://leetcode.com/problems/minimum-swaps-to-make-sequences-increasing/solutions/3976863/java-with-explanation-dfs-memoization-sc-o-n-tc-o-n/
Integer[][] memo;
int SWAPPED = 0;
int NOT_SWAPPED = 1;

int NOT_VALID = (int) Math.pow(10, 6);
public int minSwap_2(int[] nums1, int[] nums2) {
this.memo = new Integer[nums1.length][2];
return dfs(nums1, nums2, 0, NOT_SWAPPED);
}

private int dfs(int[] nums1, int[] nums2, int index, int swapped) {
if (index == nums1.length) return 0;

if (memo[index][swapped] != null) return memo[index][swapped];

// we have 2 option: 1) swap our arr[index] or not
int option1 = NOT_VALID;
// before proceed any option check: is it valid?
// Is arr[i] > arr[i-1]?
if (isValidOption(nums1, nums2, index)) {
// we didn't swapped our arr[i], amount of operations the same
option1 = dfs(nums1, nums2, index + 1, NOT_SWAPPED);
}

// swap, dfs and swap again
swap(nums1, nums2, index);
int option2 = NOT_VALID;
if (isValidOption(nums1, nums2, index) && nums1[index] != nums2[index]) {
// we swapped our arr[i], amount of operations increased by 1
option2 = dfs(nums1, nums2, index + 1, SWAPPED) + 1;
}
swap(nums1, nums2, index);

// take the minimum amount of operations
int result = Math.min(option1, option2);

// we have 2 ways to save data:
// if our PREVIOUS index was swapped hold 0, opposite 1
memo[index][swapped] = result;
return result;
}

private boolean isValidOption(int[] nums1, int[] nums2, int index) {
if (index == 0) return true;
return nums1[index] > nums1[index - 1] && nums2[index] > nums2[index - 1];
}


private void swap(int[] arr1, int[] arr2, int i) {
int temp = arr1[i];
arr1[i] = arr2[i];
arr2[i] = temp;
}

// V3
// https://leetcode.com/problems/minimum-swaps-to-make-sequences-increasing/solutions/1849616/fully-explained-solution-for-beginner-recursion-memoization/
public int minSwap_3(int[] nums1, int[] nums2) {
int dp[] = new int[nums1.length];
Arrays.fill(dp, -1);
int ans = solve(nums1, nums2, 0, dp);
return ans;
}

public int solve(int nums1[], int nums2[], int ind, int[] dp) {

if (ind == nums1.length) return 0;

// Condition 1

if (ind > 0 && (nums1[ind - 1] >= nums1[ind] || nums2[ind - 1] >= nums2[ind])) {

// nums1[] = [3,2]
// nums2[] = [1,4]

int t = nums1[ind];
nums1[ind] = nums2[ind];
nums2[ind] = t;

int val = 1 + solve(nums1, nums2, ind + 1, dp);

// Since after swapping array becomes
// nums1[] = [3,4]
// nums2[] = [1,2]
// therefore we have to swap it back so that we can have our original array.

t = nums1[ind];
nums1[ind] = nums2[ind];
nums2[ind] = t;

return val;

}
// Condition 2

else if (ind > 0 && (nums1[ind - 1] >= nums2[ind] || nums2[ind - 1] >= nums1[ind])) {
return solve(nums1, nums2, ind + 1, dp);
}
// Condition 3

else {
if (dp[ind] != -1) return dp[ind];

int tempAns1 = solve(nums1, nums2, ind + 1, dp);

int t = nums1[ind];
nums1[ind] = nums2[ind];
nums2[ind] = t;

int tempAns2 = 1 + solve(nums1, nums2, ind + 1, dp);

t = nums1[ind];
nums1[ind] = nums2[ind];
nums2[ind] = t;

return dp[ind] = Math.min(tempAns1, tempAns2);
}
}

// V4
// https://leetcode.com/problems/minimum-swaps-to-make-sequences-increasing/solutions/1277478/from-recursion-to-dp-4-solutions-java/

}

0 comments on commit 41255d3

Please sign in to comment.