-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
JavaCode/random_records/N2601_N3000/N2766_relocate_marbles.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package JavaCode.random_records.N2601_N3000; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
/** | ||
* @author mikusugar | ||
* @version 1.0, 2024/7/24 下午5:42 | ||
* @description N2766_relocate_marbles | ||
*/ | ||
public class N2766_relocate_marbles { | ||
public List<Integer> relocateMarbles(int[] nums, int[] moveFrom, int[] moveTo) { | ||
final Set<Integer> result = new HashSet<>(); | ||
for (int num : nums) { | ||
result.add(num); | ||
} | ||
for (int i = 0; i < moveFrom.length; i++) { | ||
result.remove(moveFrom[i]); | ||
result.add(moveTo[i]); | ||
} | ||
final ArrayList<Integer> ans = new ArrayList<>(result); | ||
ans.sort(Integer::compareTo); | ||
return ans; | ||
} | ||
} | ||
/* | ||
给你一个下标从 0 开始的整数数组 nums ,表示一些石块的初始位置。再给你两个长度 相等 下标从 0 开始的整数数组 moveFrom 和 moveTo 。 | ||
在 moveFrom.length 次操作内,你可以改变石块的位置。在第 i 次操作中,你将位置在 moveFrom[i] 的所有石块移到位置 moveTo[i] 。 | ||
完成这些操作后,请你按升序返回所有 有 石块的位置。 | ||
注意: | ||
如果一个位置至少有一个石块,我们称这个位置 有 石块。 | ||
一个位置可能会有多个石块。 | ||
示例 1: | ||
输入:nums = [1,6,7,8], moveFrom = [1,7,2], moveTo = [2,9,5] | ||
输出:[5,6,8,9] | ||
解释:一开始,石块在位置 1,6,7,8 。 | ||
第 i = 0 步操作中,我们将位置 1 处的石块移到位置 2 处,位置 2,6,7,8 有石块。 | ||
第 i = 1 步操作中,我们将位置 7 处的石块移到位置 9 处,位置 2,6,8,9 有石块。 | ||
第 i = 2 步操作中,我们将位置 2 处的石块移到位置 5 处,位置 5,6,8,9 有石块。 | ||
最后,至少有一个石块的位置为 [5,6,8,9] 。 | ||
示例 2: | ||
输入:nums = [1,1,3,3], moveFrom = [1,3], moveTo = [2,2] | ||
输出:[2] | ||
解释:一开始,石块在位置 [1,1,3,3] 。 | ||
第 i = 0 步操作中,我们将位置 1 处的石块移到位置 2 处,有石块的位置为 [2,2,3,3] 。 | ||
第 i = 1 步操作中,我们将位置 3 处的石块移到位置 2 处,有石块的位置为 [2,2,2,2] 。 | ||
由于 2 是唯一有石块的位置,我们返回 [2] 。 | ||
提示: | ||
1 <= nums.length <= 105 | ||
1 <= moveFrom.length <= 105 | ||
moveFrom.length == moveTo.length | ||
1 <= nums[i], moveFrom[i], moveTo[i] <= 109 | ||
测试数据保证在进行第 i 步操作时,moveFrom[i] 处至少有一个石块。 | ||
*/ |
126 changes: 126 additions & 0 deletions
126
JavaCode/random_records/N3001_N3200/N3098_find_the_sum_of_subsequence_powers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package JavaCode.random_records.N3001_N3200; | ||
|
||
import utils.CheckUtil; | ||
import utils.Parse; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
|
||
/** | ||
* @author mikusugar | ||
* @version 1.0, 2024/7/23 下午5:17 | ||
* @description N3098_find_the_sum_of_subsequence_powers | ||
*/ | ||
public class N3098_find_the_sum_of_subsequence_powers { | ||
|
||
public static void main(String[] args) { | ||
CheckUtil.check(200000000, new N3098_find_the_sum_of_subsequence_powers(). | ||
sumOfPowers(Parse.toIntArr("[-100000000,100000000]"), 2)); | ||
CheckUtil.check(990202285, new N3098_find_the_sum_of_subsequence_powers(). | ||
sumOfPowers(Parse.toIntArr("[-24, -921, 119, -291, -65, " + | ||
"-628, 372, 274, 962, -592, -10, 67, -977, 85, -294, 349, " + | ||
"-119, -846, -959, -79, -877, 833, 857, 44, 826, -295, -855, 55" + | ||
"4, -999, 759, -653, -423, -599, -928]"), 19)); | ||
CheckUtil.check(817691423, new N3098_find_the_sum_of_subsequence_powers().sumOfPowers( | ||
Parse.toIntArr("[67108864" + | ||
",33554432,16777216,8388608,4194304,2097152,1048576,524288,262144,131072,65536,32768,16384," + | ||
"8192,4096,2048,1024,512,256,128,64,32,16,8,4,2,1,4782969,1594323,531441,177147" + | ||
",59049,19683,6561,2187,729,243,81,27,9,3,40353607,5764801,823543,117649,16807,2401,343,49,7]") | ||
, 25)); | ||
} | ||
|
||
private final static int MOD = 1000000007; | ||
private Map<Long, Integer> dp; | ||
private int[] nums; | ||
|
||
public int sumOfPowers(int[] nums, int k) { | ||
//todo timeout!!! | ||
Arrays.sort(nums); | ||
this.nums = nums; | ||
this.dp = new HashMap<>(); | ||
return dfs(0, k, -1, Integer.MAX_VALUE); | ||
} | ||
|
||
private int dfs(int index, int k, int last, int power) { | ||
if (k == 0) { | ||
return power % MOD; | ||
} | ||
if (index >= nums.length || nums.length < k) { | ||
return 0; | ||
} | ||
final long key = getKey(index, k, last, power); | ||
|
||
Integer sum = dp.get(key); | ||
if (sum != null) { | ||
return sum; | ||
} | ||
sum = dfs(index + 1, k, last, power) % MOD; | ||
if (last != -1) { | ||
sum += (dfs(index + 1, k - 1, index, Math.min(power, nums[index] - nums[last])) % MOD); | ||
} else { | ||
sum += (dfs(index + 1, k - 1, index, power) % MOD); | ||
} | ||
sum %= MOD; | ||
dp.put(key, sum); | ||
return sum; | ||
} | ||
|
||
private static long getKey(int index, int k, int last, int power) { | ||
final int left = index + (k * 100) + last * 10000; | ||
return ((long) left << 32) | (power & 0xFFFFFFFFL); | ||
} | ||
} | ||
/* | ||
给你一个长度为 n 的整数数组 nums 和一个 正 整数 k 。 | ||
一个 | ||
子序列 | ||
的 能量 定义为子序列中 任意 两个元素的差值绝对值的 最小值 。 | ||
请你返回 nums 中长度 等于 k 的 所有 子序列的 能量和 。 | ||
由于答案可能会很大,将答案对 10^9 + 7 取余 后返回。 | ||
示例 1: | ||
输入:nums = [1,2,3,4], k = 3 | ||
输出:4 | ||
解释: | ||
nums 中总共有 4 个长度为 3 的子序列:[1,2,3] ,[1,3,4] ,[1,2,4] 和 [2,3,4] 。能量和为 |2 - 3| + |3 - 4| + |2 - 1| + |3 - 4| = 4 。 | ||
示例 2: | ||
输入:nums = [2,2], k = 2 | ||
输出:0 | ||
解释: | ||
nums 中唯一一个长度为 2 的子序列是 [2,2] 。能量和为 |2 - 2| = 0 。 | ||
示例 3: | ||
输入:nums = [4,3,-1], k = 2 | ||
输出:10 | ||
解释: | ||
nums 总共有 3 个长度为 2 的子序列:[4,3] ,[4,-1] 和 [3,-1] 。能量和为 |4 - 3| + |4 - (-1)| + |3 - (-1)| = 10 。 | ||
提示: | ||
2 <= n == nums.length <= 50 | ||
-10^8 <= nums[i] <= 10^8 | ||
2 <= k <= n | ||
*/ |