前言

第一次满分(我有一个压线动作),激动!!!😁
虽然题目难度上确实有降低😅。

第一题:文件夹操作日志搜集器

题解一:模拟

  • 遇到 ../ 深度-1
  • 遇到 ./ 深度不变
  • 遇到 ${文件名} 深度+1
    1. class Solution {
    2. public int minOperations(String[] logs) {
    3. int depth = 0;
    4. for (String str : logs) {
    5. switch (str) {
    6. case "../":
    7. if (depth > 0) {
    8. --depth;
    9. }
    10. break;
    11. case "./":
    12. break;
    13. default:
    14. depth++;
    15. }
    16. }
    17. return depth;
    18. }
    19. }

第二题:经营摩天轮的最大利润

题解一:模拟

题目描述比较复杂,但只需要模拟即可。我一开始还以为需要用到贪心什么的。

  1. class Solution {
  2. public int minOperationsMaxProfit(int[] customers, int boardingCost, int runningCost) {
  3. final int capacity = 4;
  4. int maxProfit = -1;
  5. int ans = -1;
  6. int waitNum = 0;
  7. int index = 0;
  8. int profit = 0;
  9. int period = 0;
  10. while ((index < customers.length) || (waitNum > 0)) {
  11. ++period;
  12. // 新游客到达
  13. if (index < customers.length) {
  14. waitNum += customers[index++];
  15. }
  16. // 是否能满载
  17. if (waitNum >= capacity) {
  18. profit += capacity * boardingCost - runningCost;
  19. waitNum -= capacity;
  20. } else {
  21. profit += waitNum * boardingCost - runningCost;
  22. waitNum = 0;
  23. }
  24. // 更新最大利润
  25. if (profit > maxProfit) {
  26. maxProfit = profit;
  27. ans = period;
  28. }
  29. }
  30. if (maxProfit == -1) {
  31. return -1;
  32. } else {
  33. return ans;
  34. }
  35. }
  36. }

第三题:皇位继承顺序

题解一:模拟

题目描述比较长,读懂后根据意思实现各个方法即可。会涉及大量查找操作,用Map来加速查找。

  1. import java.util.*;
  2. class ThroneInheritance {
  3. // 国王
  4. private Person king;
  5. // 人名->人物实例映射表
  6. private Map<String, Person> personMap;
  7. // 继承序列
  8. private List<Person> order;
  9. // 加速在继承序列中的查找效率
  10. private Map<String, Person> orderMap;
  11. public ThroneInheritance(String kingName) {
  12. king = new Person(null, kingName);
  13. personMap = new HashMap<>();
  14. personMap.put(kingName, king);
  15. }
  16. public void birth(String parentName, String childName) {
  17. Person parent = personMap.get(parentName);
  18. Person temp = new Person(parent, childName);
  19. parent.children.add(temp);
  20. personMap.put(childName, temp);
  21. }
  22. public void death(String name) {
  23. Person person = personMap.get(name);
  24. person.isLive = false;
  25. }
  26. public List<String> getInheritanceOrder() {
  27. order = new LinkedList<>();
  28. orderMap = new HashMap<>();
  29. for (Person cur = king; cur != null; cur = successor(cur)) {
  30. order.add(cur);
  31. orderMap.put(cur.name, cur);
  32. }
  33. List<String> ans = new ArrayList<>(order.size());
  34. for (Person i : order) {
  35. if (i.isLive) {
  36. ans.add(i.name);
  37. }
  38. }
  39. return ans;
  40. }
  41. /**
  42. * 寻找下一顺位继承人
  43. *
  44. * @param x 当前继承人
  45. * @return x的下一顺位继承人
  46. */
  47. private Person successor(Person x) {
  48. if ((x.children.size() == 0) || allIn(x)) {
  49. if (x.equals(king)) {
  50. return null;
  51. } else {
  52. return successor(x.parent);
  53. }
  54. } else {
  55. for (Person i : x.children) {
  56. if (!orderMap.containsKey(i.name)) {
  57. return i;
  58. }
  59. }
  60. }
  61. return null;
  62. }
  63. /**
  64. * 判断x的所有孩子是否已经在继承序列中
  65. *
  66. * @param x 人
  67. * @return x的所有孩子均已在继承序列中则返回true, 否则返回false
  68. */
  69. private boolean allIn(Person x) {
  70. for (Person i : x.children) {
  71. if (!orderMap.containsKey(i.name)) {
  72. return false;
  73. }
  74. }
  75. return true;
  76. }
  77. public static void main(String[] args) {
  78. ThroneInheritance obj = new ThroneInheritance("king");
  79. obj.birth("king", "andy");
  80. obj.birth("king", "bob");
  81. obj.birth("king", "catherine");
  82. List<String> param_3 = obj.getInheritanceOrder();
  83. System.out.print(param_3);
  84. }
  85. }
  86. class Person {
  87. String name;
  88. Person parent;
  89. List<Person> children;
  90. boolean isLive;
  91. Person(Person parent, String name) {
  92. this.name = name;
  93. this.parent = parent;
  94. this.children = new LinkedList<>();
  95. this.isLive = true;
  96. }
  97. @Override
  98. public boolean equals(Object o) {
  99. if (this == o) return true;
  100. if (o == null || getClass() != o.getClass()) return false;
  101. Person person = (Person) o;
  102. return Objects.equals(name, person.name) && Objects.equals(parent, person.parent);
  103. }
  104. }
  105. /**
  106. * Your ThroneInheritance object will be instantiated and called as such:
  107. * ThroneInheritance obj = new ThroneInheritance(kingName);
  108. * obj.birth(parentName,childName);
  109. * obj.death(name);
  110. * List<String> param_3 = obj.getInheritanceOrder();
  111. */

第四题:最多可达成的换楼请求数目

题解一:暴搜

应该是一个图论的问题,选若干条边,满足所有点的入度等于出度。我图论还没开始练习。。。。
最后留给我的时间不多了,看了下数据范围 1 <= requests.length <= 16 ,直接暴搜的话,每条请求都考虑执行或不执行,2这个范围根本不大。然后就成了。。。

  1. class Solution {
  2. private int max;
  3. public int maximumRequests(int n, int[][] requests) {
  4. max = 0;
  5. dfs(n, requests, 0, new int[n], new int[n], 0);
  6. return max;
  7. }
  8. private void dfs(int n, int[][] requests, int index, int[] in, int[] out, int ans) {
  9. if (index >= requests.length) {
  10. boolean flag = true;
  11. for (int i = 0; i < n; ++i) {
  12. if (in[i] != out[i]) {
  13. flag = false;
  14. return;
  15. }
  16. }
  17. max = Math.max(max, ans);
  18. return;
  19. }
  20. dfs(n, requests, index + 1, in, out, ans);
  21. int inNum = requests[index][0];
  22. int outNum = requests[index][1];
  23. in[inNum]++;
  24. out[outNum]++;
  25. dfs(n, requests, index + 1, in, out, ans + 1);
  26. in[inNum]--;
  27. out[outNum]--;
  28. }
  29. }