From ae0bd6ee581ea789a303b8ec8ddc6909dae66e6f Mon Sep 17 00:00:00 2001 From: "huangge1199@hotmail.com" Date: Sun, 5 Sep 2021 23:30:28 +0800 Subject: [PATCH] =?UTF-8?q?257=E5=91=A8=E8=B5=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/code/leet/week/Solution257.java | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/main/java/com/code/leet/week/Solution257.java diff --git a/src/main/java/com/code/leet/week/Solution257.java b/src/main/java/com/code/leet/week/Solution257.java new file mode 100644 index 0000000..51a67ba --- /dev/null +++ b/src/main/java/com/code/leet/week/Solution257.java @@ -0,0 +1,70 @@ +package com.code.leet.week; + +import com.code.leet.entiy.TwoArray; + +import java.math.BigInteger; +import java.util.*; + +/** + * @description: + * @author: Administrator + * @date: 2021/8/22-10:29 + */ +public class Solution257 { + public static void main(String[] args) { + Solution257 solution = new Solution257(); +// TwoArray twoArray = new TwoArray("[[1,1],[2,1],[2,2],[1,2]]", true); +// System.out.println(solution.numberOfWeakCharacters(twoArray.getArr())); + System.out.println(solution.firstDayBeenInAllRooms(new int[]{0, 0})); + } + + public int countQuadruplets(int[] nums) { + int count = 0; + for (int i = 0; i < nums.length; i++) { + for (int j = i + 1; j < nums.length; j++) { + for (int k = j + 1; k < nums.length; k++) { + for (int l = k + 1; l < nums.length; l++) { + if (nums[i] + nums[j] + nums[k] == nums[l]) { + count++; + } + } + } + } + } + return count; + } + + public int numberOfWeakCharacters(int[][] properties) { + Arrays.sort(properties, (o1, o2) -> o1[0] == o2[0] ? o2[1] - o1[1] : o2[0] - o1[0]); + TreeSet set = new TreeSet<>(); + int count = 0; + List list = new ArrayList<>(); + for (int i = 0; i < properties.length; i++) { + if (set.higher(properties[i][1]) != null) { + count++; + } + list.add(properties[i][1]); + if (i != properties.length - 1 && properties[i][0] != properties[i + 1][0]) { + set.addAll(list); + list.clear(); + } + } + for (int i = properties.length - 1; i >= 0; i--) { + if (i != properties.length - 1 && properties[i][0] != properties[i + 1][0] && set.higher(properties[i][1]) != null) { + count++; + } + set.add(properties[i][1]); + } + return count; + } + + public int firstDayBeenInAllRooms(int[] nextVisit) { + int[] dp = new int[nextVisit.length]; + int[] sum = new int[nextVisit.length]; + for (int i = 1; i < nextVisit.length; i++) { + sum[i] = dp[i - 1] - dp[nextVisit[i - 1]]; + dp[i] = dp[i - 1] + sum[i]; + } + return dp[nextVisit.length - 1]; + } +}