From 7f7691e3bfd75e1b39a59badbc4d95b42cc8f4a8 Mon Sep 17 00:00:00 2001 From: "huangge1199@hotmail.com" Date: Mon, 5 Apr 2021 21:12:21 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AB=9E=E8=B5=9B=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../code/leet/Official/t20210219/Flatten.java | 50 +++--- .../main/java/com/code/leet/entiy/Node.java | 34 ++++ .../leet/study/t2021/t20210219/Flatten.java | 42 ++--- .../java/com/code/leet/week/LcpSolution.java | 100 +++++++++++ .../java/com/code/leet/week/Solution235.java | 101 ++++++++++++ .../java/com/code/leet/week/Solution49.java | 96 +++++++++++ .../leet/weekold/AuthenticationManager.java | 40 +++++ .../java/com/code/leet/weekold/Solution.java | 155 ++++++++++++++++++ 8 files changed, 572 insertions(+), 46 deletions(-) create mode 100644 LeetCode/src/main/java/com/code/leet/week/LcpSolution.java create mode 100644 LeetCode/src/main/java/com/code/leet/week/Solution235.java create mode 100644 LeetCode/src/main/java/com/code/leet/week/Solution49.java create mode 100644 LeetCode/src/main/java/com/code/leet/weekold/AuthenticationManager.java create mode 100644 LeetCode/src/main/java/com/code/leet/weekold/Solution.java diff --git a/LeetCode/src/main/java/com/code/leet/Official/t20210219/Flatten.java b/LeetCode/src/main/java/com/code/leet/Official/t20210219/Flatten.java index 6822076..cd13868 100644 --- a/LeetCode/src/main/java/com/code/leet/Official/t20210219/Flatten.java +++ b/LeetCode/src/main/java/com/code/leet/Official/t20210219/Flatten.java @@ -22,29 +22,29 @@ public class Flatten { /** * 430. 扁平化多级双向链表 */ - public Node flatten(Node head) { - if (head == null) return head; - // pseudo head to ensure the `prev` pointer is never none - Node pseudoHead = new Node(0, null, head, null); - - flattenDFS(pseudoHead, head); - - // detach the pseudo head from the real head - pseudoHead.next.prev = null; - return pseudoHead.next; - } - /* return the tail of the flatten list */ - public Node flattenDFS(Node prev, Node curr) { - if (curr == null) return prev; - curr.prev = prev; - prev.next = curr; - - // the curr.next would be tempered in the recursive function - Node tempNext = curr.next; - - Node tail = flattenDFS(curr, curr.child); - curr.child = null; - - return flattenDFS(tail, tempNext); - } +// public Node flatten(Node head) { +// if (head == null) return head; +// // pseudo head to ensure the `prev` pointer is never none +// Node pseudoHead = new Node(0, null, head, null); +// +// flattenDFS(pseudoHead, head); +// +// // detach the pseudo head from the real head +// pseudoHead.next.prev = null; +// return pseudoHead.next; +// } +// /* return the tail of the flatten list */ +// public Node flattenDFS(Node prev, Node curr) { +// if (curr == null) return prev; +// curr.prev = prev; +// prev.next = curr; +// +// // the curr.next would be tempered in the recursive function +// Node tempNext = curr.next; +// +// Node tail = flattenDFS(curr, curr.child); +// curr.child = null; +// +// return flattenDFS(tail, tempNext); +// } } diff --git a/LeetCode/src/main/java/com/code/leet/entiy/Node.java b/LeetCode/src/main/java/com/code/leet/entiy/Node.java index c811715..d6ca39e 100644 --- a/LeetCode/src/main/java/com/code/leet/entiy/Node.java +++ b/LeetCode/src/main/java/com/code/leet/entiy/Node.java @@ -1,5 +1,8 @@ package com.code.leet.entiy; +import java.util.ArrayList; +import java.util.List; + public class Node { public int val; public Node next; @@ -10,4 +13,35 @@ public class Node { this.next = null; this.random = null; } + + public Node(int val, Node random) { + this.val = val; + this.next = null; + this.random = random; + } + + public Node setHead(List list, List index) { + Node head = new Node(list.get(0)); + int i = 1; + int size = list.size(); + Node temp = head; + List nodeList = new ArrayList<>(); + nodeList.add(head); + while (i < size) { + temp.next = new Node(list.get(i)); + nodeList.add(temp.next); + i++; + temp = temp.next; + } + i = 0; + temp = head; + while (i < size) { + if (index.get(i) != null) { + temp.random = nodeList.get(index.get(i)); + } + i++; + temp = temp.next; + } + return head; + } } diff --git a/LeetCode/src/main/java/com/code/leet/study/t2021/t20210219/Flatten.java b/LeetCode/src/main/java/com/code/leet/study/t2021/t20210219/Flatten.java index 3bed374..0b09054 100644 --- a/LeetCode/src/main/java/com/code/leet/study/t2021/t20210219/Flatten.java +++ b/LeetCode/src/main/java/com/code/leet/study/t2021/t20210219/Flatten.java @@ -22,25 +22,25 @@ public class Flatten { /** * 430. 扁平化多级双向链表 */ - public Node flatten(Node head) { - Node temp = head; - while (temp != null) { - if (temp.child != null) { - Node next = temp.next; - Node mid = flatten(temp.child); - temp.child = null; - temp.next = mid; - mid.prev = temp; - while (temp.next != null) { - temp = temp.next; - } - temp.next = next; - if (next != null) { - next.prev = temp; - } - } - temp = temp.next; - } - return head; - } +// public Node flatten(Node head) { +// Node temp = head; +// while (temp != null) { +// if (temp.child != null) { +// Node next = temp.next; +// Node mid = flatten(temp.child); +// temp.child = null; +// temp.next = mid; +// mid.prev = temp; +// while (temp.next != null) { +// temp = temp.next; +// } +// temp.next = next; +// if (next != null) { +// next.prev = temp; +// } +// } +// temp = temp.next; +// } +// return head; +// } } diff --git a/LeetCode/src/main/java/com/code/leet/week/LcpSolution.java b/LeetCode/src/main/java/com/code/leet/week/LcpSolution.java new file mode 100644 index 0000000..92c3fff --- /dev/null +++ b/LeetCode/src/main/java/com/code/leet/week/LcpSolution.java @@ -0,0 +1,100 @@ +package com.code.leet.week; + +import java.util.*; +import java.util.stream.Collectors; + +/** + * @Author: 轩辕龙儿 + * @Date: 2021/4/5 15:02 + * @Description: + */ +public class LcpSolution { + public static void main(String[] args) { + LcpSolution solution = new LcpSolution(); + System.out.println(solution.orchestraLayout(4, 1, 2)); + +// int[] nums = {2, 2, 1, 9}; +// int target = 10; +// System.out.println(solution.purchasePlans(nums, target)); + } + + public int magicTower(int[] nums) { + long sum = 1; + Stack stack1 = new Stack<>(); + Stack stack2 = new Stack<>(); + List list = new ArrayList<>(); + int size = nums.length; + int count = 0; + for (int i = 0; i < size; i++) { + sum += nums[i]; + if (nums[i] < 0) { + while (!stack1.isEmpty() && stack1.peek() < nums[i]) { + stack2.push(stack1.pop()); + } + stack1.push(nums[i]); + while (!stack2.isEmpty()) { + stack1.push(stack2.pop()); + } + } + if (sum <= 0) { + sum -= stack1.peek(); + list.add(stack1.pop()); + count++; + } + } + size = list.size(); + for (int i = 0; i < size; i++) { + sum += list.get(i); + if (sum <= 0) { + return -1; + } + } + return count; + } + + public int orchestraLayout(int num, int xPos, int yPos) { + int round = Math.min(Math.min(xPos, num - xPos - 1), Math.min(yPos, num - yPos - 1)); + int result = 4 * num * round - 4 * round * round; + if (xPos == round) { + result += yPos - round + 1; + } else if (yPos == num - 1 - round) { + result += (num - 1 - round * 2) + xPos - round + 1; + } else if (xPos == num - 1 - round) { + result += (num - 1 - round * 2) * 2 + num - round - yPos; + } else { + result += (num - 1 - round * 2) * 3 + num - round - xPos; + } + return result % 9; + } + + public int purchasePlans(int[] nums, int target) { + long count = 0; + List numList = Arrays.stream(nums).boxed().collect(Collectors.toList()); + Collections.sort(numList); + Map map = new HashMap<>(); + int temp = -1; + for (int num : numList) { + if (num >= target) { + break; + } + if (temp == -1) { + for (int i = 1; i < num; i++) { + map.put(i, Long.parseLong("0")); + } + map.put(num, Long.parseLong("1")); + } else { + if (map.containsKey(target - num)) { + count += map.get(target - num); + } else { + count += map.get(temp); + } + for (int i = temp + 1; i < num; i++) { + map.put(i, map.get(temp)); + } + map.put(num, map.get(temp) + 1); + } + temp = num; + } + return (int) (count % (Math.pow(10, 9) + 7)); + } +} diff --git a/LeetCode/src/main/java/com/code/leet/week/Solution235.java b/LeetCode/src/main/java/com/code/leet/week/Solution235.java new file mode 100644 index 0000000..b070ea8 --- /dev/null +++ b/LeetCode/src/main/java/com/code/leet/week/Solution235.java @@ -0,0 +1,101 @@ +package com.code.leet.week; + +import java.util.*; +import java.util.stream.Collectors; + +/** + * @Author: 轩辕龙儿 + * @Date: 2021/4/4 10:33 + * @Description: + */ +public class Solution235 { + public String truncateSentence(String s, int k) { + String[] strings = s.split(" "); + if (strings.length == 0) { + return s; + } + StringBuilder sBuilder = new StringBuilder(); + for (int i = 0; i < k; i++) { + sBuilder.append(strings[i]).append(" "); + } + s = sBuilder.toString(); + s = s.substring(0, s.length() - 1); + return s; + } + + public static void main(String[] args) { + Solution235 solution235 = new Solution235(); + + int[][] logs = {{0, 5}, {1, 2}, {0, 2}, {0, 5}, {1, 3}}; + int k = 5; + solution235.findingUsersActiveMinutes(logs, k); + } + + public int[] findingUsersActiveMinutes(int[][] logs, int k) { + Map> map = new HashMap<>(); + int min = Integer.MAX_VALUE; + for (int i = 0; i < logs.length; i++) { + List temp; + min = Math.min(min, logs[i][0]); + if (map.containsKey(logs[i][0])) { + temp = map.get(logs[i][0]); + if (!temp.contains(logs[i][1])) { + temp.add(logs[i][1]); + } + map.put(logs[i][0], temp); + } else { + temp = new ArrayList<>(); + temp.add(logs[i][1]); + map.put(logs[i][0], temp); + } + } + int[] result = new int[k]; + for (int i = 0; i < k; i++) { + result[i] = 0; + } + for (int key : map.keySet()) { + int index = map.get(key).size(); + result[index - 1] = result[index - 1] + 1; + } + return result; + } + + public int minAbsoluteSumDiff(int[] nums1, int[] nums2) { + long result = 0; + Stack stack = new Stack<>(); + Stack tempStack = new Stack<>(); + int size = nums1.length; + for (int i = 0; i < size; i++) { + int temp = Math.abs(nums1[i] - nums2[i]); + result += temp; + if (!tempStack.isEmpty()) { + while (temp > tempStack.peek()[1]) { + stack.push(tempStack.pop()); + } + } + while (temp < stack.peek()[1]) { + tempStack.push(stack.pop()); + } + stack.push(new int[]{i, temp}); + } + while (!tempStack.isEmpty()) { + stack.push(tempStack.pop()); + } + Set set = Arrays.stream(nums1).boxed().collect(Collectors.toSet()); + int max = -1; + while (!stack.isEmpty()) { + int[] arr = stack.pop(); + int i; + for (i = 0; i < arr[1]; i++) { + if (set.contains(nums2[arr[0]] - i) || set.contains(nums2[arr[0]] + i)) { + break; + } + } + if(nums2[arr[0]] - i>stack.peek()[1]){ + break; + } + } + + return (int) ((result - max) % (Math.pow(10, 9) + 7)); + } +} diff --git a/LeetCode/src/main/java/com/code/leet/week/Solution49.java b/LeetCode/src/main/java/com/code/leet/week/Solution49.java new file mode 100644 index 0000000..90090b7 --- /dev/null +++ b/LeetCode/src/main/java/com/code/leet/week/Solution49.java @@ -0,0 +1,96 @@ +package com.code.leet.week; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @Author: 轩辕龙儿 + * @Date: 2021/4/3 22:33 + * @Description: + */ +public class Solution49 { + public static void main(String[] args) { + Solution49 solution49 = new Solution49(); +// System.out.println(solution.squareIsWhite("a1")); + } + + public boolean squareIsWhite(String coordinates) { + char[] chars = coordinates.toCharArray(); + if ((chars[0] - chars[1]) % 2 == 0) { + return false; + } else { + return true; + } + } + + public boolean areSentencesSimilar(String sentence1, String sentence2) { + sentence1 += " "; + sentence2 += " "; + String[] strs1 = sentence1.split(" "); + String[] strs2 = sentence2.split(" "); + int size1 = strs1.length; + int size2 = strs2.length; + if (size1 == size2) { + return sentence1.equals(sentence2); + } else if (size1 > size2) { + return isTrue(strs1, strs2); + } else { + return isTrue(strs2, strs1); + } + } + private boolean isTrue(String[] strs1, String[] strs2) { + int size1 = strs1.length; + int size2 = strs2.length; + int index = 0; + while (index < size2 && strs2[index].equals(strs1[index])) { + index++; + } + if (index == size2) { + return true; + } + for (int i = 1; i <= size2 - index; i++) { + if (!strs2[size2 - i].equals(strs1[size1 - i])) { + return false; + } + } + return true; + } + + public int countNicePairs(int[] nums) { + Map map = new HashMap<>(); + for (int num : nums) { + int key = num - revert(num); + if (map.containsKey(key)) { + map.put(key, map.get(key) + 1); + } else { + map.put(key, Long.parseLong("1")); + } + } + long count = 0; + for (int key : map.keySet()) { + long value = map.get(key); + if (value > 1) { + count += value * (value - 1) / 2; + } + } + return (int)(count % (Math.pow(10, 9) + 7)); + } + private int revert(int num) { + String str = "" + num; + str = new StringBuilder(str).reverse().toString(); + return Integer.parseInt(str); + } + + public int maxHappyGroups(int batchSize, int[] groups) { + Map> map = new HashMap<>(); + int size = groups.length; + int count = 0; + for (int i = 0; i < size; i++) { + if (groups[i] % batchSize == 0) { + count++; + } + } + return 0; + } +} diff --git a/LeetCode/src/main/java/com/code/leet/weekold/AuthenticationManager.java b/LeetCode/src/main/java/com/code/leet/weekold/AuthenticationManager.java new file mode 100644 index 0000000..0e13f83 --- /dev/null +++ b/LeetCode/src/main/java/com/code/leet/weekold/AuthenticationManager.java @@ -0,0 +1,40 @@ +package com.code.leet.weekold; + +import java.util.HashMap; +import java.util.Map; + +/** + * Created with IntelliJ IDEA. + * User: 轩辕龙儿 + * Date: 2021/3/20 + * Time: 22:42 + * Description: No Description + */ +public class AuthenticationManager { + Map map = new HashMap<>(); + int timeToLive; + + public AuthenticationManager(int timeToLive) { + this.timeToLive = timeToLive; + } + + public void generate(String tokenId, int currentTime) { + map.put(tokenId, currentTime + timeToLive); + } + + public void renew(String tokenId, int currentTime) { + if (map.containsKey(tokenId) && currentTime < map.get(tokenId)) { + map.put(tokenId, currentTime + timeToLive); + } + } + + public int countUnexpiredTokens(int currentTime) { + int num = 0; + for (String key:map.keySet()) { + if(map.get(key)>currentTime){ + num++; + } + } + return num; + } +} diff --git a/LeetCode/src/main/java/com/code/leet/weekold/Solution.java b/LeetCode/src/main/java/com/code/leet/weekold/Solution.java new file mode 100644 index 0000000..7479d2e --- /dev/null +++ b/LeetCode/src/main/java/com/code/leet/weekold/Solution.java @@ -0,0 +1,155 @@ +package com.code.leet.weekold; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created with IntelliJ IDEA. + * User: 轩辕龙儿 + * Date: 2021/3/20 + * Time: 22:31 + * Description: No Description + */ +public class Solution { + public int secondHighest(String s) { + int max = -1; + int second = -2; + int size = s.length(); + for (int i = 0; i < size; i++) { + String temp; + temp = i + 1 < size ? s.substring(i, i + 1) : s.substring(i); + if (temp.matches("[0-9]+")) { + int cur = Integer.parseInt(temp); + if (cur > max) { + second = max; + max = cur; + } else if (cur > second && cur != max) { + second = cur; + } + } + } + return second > 0 ? second : -1; + } + + public static void main(String[] args) { +// getMaximumConsecutive(new int[]{1,1,1,4}); + int[][] orders = new int[][]{{23,8,0},{28,29,1},{11,30,1},{30,25,0},{26,9,0},{3,21,0},{28,19,1},{19,30,0},{20,9,1},{17,6,0}}; + Solution solution = new Solution(); + solution.getNumberOfBacklogOrders(orders); + } + +// public static int getMaximumConsecutive(int[] coins) { +// int size = coins.length; +// for (int i = 0; i < size; i++) { +// for (int j = i + 1; j < size; j++) { +// if (coins[j] < coins[i]) { +// int temp = coins[j]; +// coins[j] = coins[i]; +// coins[i] = temp; +// } +// } +// } +// int num = 1; +// while (isTrue(coins, num)) { +// num++; +// } +// return num - 1; +// } +// +// private static boolean isTrue(int[] arr, int num) { +// if (num == 0) { +// return true; +// } +// int size = arr.length; +// int index = -1; +// for (int i = 0; i < size; i++) { +// if (arr[i] > num) { +// index = i; +// break; +// } +// } +// if (index == -1) { +// return false; +// } +// for (int i = 0; i < index; i++) { +// int[] newArr = new int[index - i - 1]; +// for (int j = i + 1; j < index; j++) { +// newArr[j] = arr[j]; +// } +// if (isTrue(newArr, num - arr[i])) { +// return true; +// } +// } +// return false; +// } + + public int maxAscendingSum(int[] nums) { + int max = 0; + int size = nums.length; + int sum = nums[0]; + for (int i = 1; i < size; i++) { + if (nums[i] > nums[i - 1]) { + sum += nums[i]; + } else { + max = Math.max(sum, max); + sum = nums[i]; + } + } + max = Math.max(sum, max); + return max; + } + + public int getNumberOfBacklogOrders(int[][] orders) { + List orderList = new ArrayList<>(); + int length = orders.length; + long num = 0; + for (int i = 0; i < length; i++) { + int size = orderList.size(); + for (int j = 0; j < size; j++) { + if (orders[i][2] - orderList.get(j)[2] != 0) { + boolean buy = orders[i][2] == 0 && orderList.get(j)[0] <= orders[i][0]; + boolean sell = orders[i][2] == 1 && orderList.get(j)[0] >= orders[i][0]; + if (buy || sell) { + if (orderList.get(j)[1] < orders[i][1]) { + orders[i][1] -= orderList.get(j)[1]; + num -= orderList.get(j)[1]; + orderList.remove(orderList.get(j)); + size = orderList.size(); + j--; + } else if (orderList.get(j)[1] > orders[i][1]) { + num -= orders[i][1]; + orderList.get(j)[1] -= orders[i][1]; + orders[i][1] = 0; + break; + } + } + } + } + size = orderList.size(); + if (orders[i][1] > 0) { + boolean isTrue = true; + for (int j = 0; j < size; j++) { + if (orders[i][2] == orderList.get(j)[2]) { + if (orders[i][2] == 0 && orders[i][0] > orderList.get(j)[0]) { + num += orders[i][1]; + orderList.add(j, orders[i]); + isTrue = false; + break; + } + if (orders[i][2] == 1 && orders[i][0] < orderList.get(j)[0]) { + num += orders[i][1]; + orderList.add(j, orders[i]); + isTrue = false; + break; + } + } + } + if (isTrue) { + num += orders[i][1]; + orderList.add(orders[i]); + } + } + } + return (int)(num % (Math.pow(10, 9) + 7)); + } +}