当前位置:   article > 正文

第 215 场力扣周赛题解_力扣 网格幸福感

力扣 网格幸福感

先附上本次周赛排名:

5601. 设计有序流

思路:利用map模拟题目过程即可。

  1. class OrderedStream {
  2. int n, ptr;
  3. int[] arr;
  4. Map<Integer, String> map;
  5. public OrderedStream(int n) {
  6. ptr = 1;
  7. this.n = n;
  8. arr = new int[n + 1];
  9. map = new HashMap<>();
  10. }
  11. public List<String> insert(int id, String value) {
  12. List<String> res = new ArrayList<>();
  13. if (id != ptr)
  14. map.put(id, value);
  15. else {
  16. int i = id;
  17. map.put(id, value);
  18. for (; i <= n; i++) {
  19. if (!map.containsKey(i))
  20. break;
  21. res.add(map.get(i));
  22. }
  23. ptr = i;
  24. }
  25. return res;
  26. }
  27. }

5603. 确定两个字符串是否接近

思路:记录下两个串中字符出现的数量,之后将这两个数组排序判断即可。

  1. class Solution {
  2. public boolean closeStrings(String word1, String word2) {
  3. int n = word1.length();
  4. int m = word2.length();
  5. if (n != m) return false;
  6. int[] a = new int[26];
  7. int[] b = new int[26];
  8. for (int i = 0; i < n; i++) {
  9. a[word1.charAt(i) - 'a']++;
  10. b[word2.charAt(i) - 'a']++;
  11. }
  12. for (int i = 0; i < 26; i++) {
  13. if (a[i] != 0 && b[i] == 0 || a[i] == 0 && b[i] != 0)
  14. return false;
  15. }
  16. Arrays.sort(a);
  17. Arrays.sort(b);
  18. for (int i = 0; i < 26; i++)
  19. if (a[i] != b[i])
  20. return false;
  21. return true;
  22. }
  23. }

5602. 将 x 减到 0 的最小操作数

思路:这类题套路都一样,枚举左边拿了多少,利用后缀和优化右边能拿多少。

  1. class Solution {
  2. public int minOperations(int[] nums, int x) {
  3. int n = nums.length;
  4. int[] sum = new int[n + 1];
  5. Map<Integer, Integer> map = new HashMap<>();
  6. sum[n - 1] = nums[n - 1];
  7. int ans = n + 1;
  8. map.put(0, 0);
  9. for (int i = n - 1; i >= 0; i--) {
  10. sum[i] = sum[i + 1] + nums[i];
  11. if (sum[i] == x)
  12. ans = Math.min(ans, n - i);
  13. map.put(sum[i], n - i);
  14. }
  15. for (int i = 0; i < n; i++) {
  16. x -= nums[i];
  17. if (map.containsKey(x))
  18. ans = Math.min(ans, i + 1 + map.get(x));
  19. }
  20. return ans == n + 1 ? -1 : ans;
  21. }
  22. }

5604. 最大化网格幸福感

思路:力扣好久没出这种质量较高的动态规划啦。

我们考虑状压dp。定义dp[i][j][k][x][y]:表示第i行外向的人放置的状态为j,内向的人放置的状态为k并且前i行已经使用了x个外向的人和y个内向的人的最大价值和。

  1. class Solution {
  2. public int getMaxGridHappiness(int m, int n, int introvertsCount, int extrovertsCount) {
  3. int[][][][][] dp = new int[m][1 << n][1 << n][introvertsCount + 1][extrovertsCount + 1];
  4. for (int i = 0; i < m; i++)
  5. for (int j = 0; j < (1 << n); j++)
  6. for (int k = 0; k < (1 << n); k++)
  7. for (int x = 0; x <= introvertsCount; x++)
  8. for (int y = 0; y <= extrovertsCount; y++)
  9. dp[i][j][k][x][y] = -1;
  10. for (int i = 0; i < (1 << n); i++)
  11. for (int j = 0; j < (1 << n); j++) {
  12. if (!check(i, j, introvertsCount, extrovertsCount))
  13. continue;
  14. int num1 = findNum(i);
  15. int num2 = findNum(j);
  16. dp[0][i][j][num1][num2] = findSum(i, j);
  17. }
  18. for (int i = 1; i < m; i++)
  19. for (int x = 0; x < (1 << n); x++)
  20. for (int y = 0; y < (1 << n); y++) {
  21. if (!check(x, y, introvertsCount, extrovertsCount))
  22. continue;
  23. int last1 = findNum(x);
  24. int last2 = findNum(y);
  25. for (int num1 = 0; num1 <= introvertsCount; num1++)
  26. for (int num2 = 0; num2 <= extrovertsCount; num2++) {
  27. if (last1 > num1 || last2 > num2 || dp[i - 1][x][y][num1][num2] == -1)
  28. continue;
  29. for (int xx = 0; xx < (1 << n); xx++)
  30. for (int yy = 0; yy < (1 << n); yy++) {
  31. if (!check(xx, yy, introvertsCount - num1, extrovertsCount - num2))
  32. continue;
  33. int now1 = findNum(xx);
  34. int now2 = findNum(yy);
  35. int sum = dp[i - 1][x][y][num1][num2];
  36. int state = x & xx;
  37. sum -= findNum(state) * 30 * 2;
  38. state = x & yy;
  39. sum = sum - findNum(state) * 30 + findNum(state) * 20;
  40. state = y & xx;
  41. sum = sum + findNum(state) * 20 - findNum(state) * 30;
  42. state = y & yy;
  43. sum += findNum(state) * 20 * 2;
  44. dp[i][xx][yy][num1 + now1][num2 + now2] =
  45. Math.max(dp[i][xx][yy][num1 + now1][num2 + now2], sum + findSum(xx, yy));
  46. }
  47. }
  48. }
  49. int ans = 0;
  50. for (int i = 0; i < (1 << n); i++)
  51. for (int j = 0; j < (1 << n); j++)
  52. for (int x = 0; x <= introvertsCount; x++)
  53. for (int y = 0; y <= extrovertsCount; y++)
  54. ans = Math.max(ans, dp[m - 1][i][j][x][y]);
  55. return ans;
  56. }
  57. private int findSum(int x, int y) {
  58. int z = x | y;
  59. int ans = findNum(x) * 120 + findNum(y) * 40;
  60. ans -= findNum(x & (z << 1)) * 30;
  61. ans -= findNum(x & (z >> 1)) * 30;
  62. ans += findNum(y & (z << 1)) * 20;
  63. ans += findNum(y & (z >> 1)) * 20;
  64. return ans;
  65. }
  66. private int findNum(int x) {
  67. int num = 0;
  68. while (x > 0) {
  69. if ((x & 1) != 0)
  70. num++;
  71. x >>= 1;
  72. }
  73. return num;
  74. }
  75. private boolean check(int x, int y, int xx, int yy) {
  76. if ((x & y) != 0) return false;
  77. return findNum(x) <= xx && findNum(y) <= yy;
  78. }
  79. }

 

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号