Compare commits

..

10 Commits

Author SHA1 Message Date
1f7318460c 博客标题图案生成示例 2023-09-11 10:15:25 +08:00
de3468cfba 双周赛112 2023-09-03 10:26:12 +08:00
5ebae8a62d 双周赛111 2023-08-20 00:10:13 +08:00
c9b3e328eb 周赛358 2023-08-13 11:27:12 +08:00
78c8e940dc 周赛356 2023-08-13 10:14:30 +08:00
09e594c270 周赛355 2023-07-23 13:38:51 +08:00
7338475b3b 双周赛109 2023-07-23 00:17:00 +08:00
41e484a0a8 周赛354 2023-07-16 12:14:19 +08:00
f71248bde4 1073:负二进制数相加 2023-05-18 11:22:23 +08:00
4adf7dee61 851:喧闹和富有 2023-04-10 21:32:20 +08:00
14 changed files with 1046 additions and 0 deletions

View File

@ -0,0 +1,59 @@
package blog;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
/**
* @author hyy
* @Classname ImageGenerator
* @Description TODO
* @Date 2023/9/11 9:59:25
*/
public class ImageGenerator {
public static void main(String[] args) {
String text = "你好,世界!"; // 要显示的文字
String backgroundImageFilePath = "src/main/java/blog/bg.png"; // 默认背景图片的文件路径
String outputFilePath = "src/main/java/blog/test.png"; // 生成的图片的输出路径
ImageGenerator.generateImage(text, backgroundImageFilePath, outputFilePath);
}
public static void generateImage(String text, String backgroundImageFilePath, String outputFilePath) {
try {
// 读取背景图片
BufferedImage backgroundImage = ImageIO.read(new File(backgroundImageFilePath));
// 创建一个Graphics2D对象用于在背景图片上绘制文字
Graphics2D g2d = backgroundImage.createGraphics();
// 设置字体和颜色
Font font = new Font("宋体", Font.PLAIN, 36).deriveFont(Font.PLAIN);
g2d.setFont(font);
g2d.setColor(Color.BLACK); // 设置文字颜色
// 获取文字的尺寸
FontMetrics fm = g2d.getFontMetrics();
int textWidth = fm.stringWidth(text);
int textHeight = fm.getHeight();
// 计算文字的位置使其居中显示
int x = (backgroundImage.getWidth() - textWidth) / 2;
int y = (backgroundImage.getHeight() + textHeight) / 2;
// 在指定位置绘制文字
g2d.drawString(text, x, y);
// 释放资源
g2d.dispose();
// 保存生成的图片
ImageIO.write(backgroundImage, "png", new File(outputFilePath));
System.out.println("图片生成成功!");
} catch (Exception e) {
e.printStackTrace();
}
}
}

BIN
src/main/java/blog/bg.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 471 KiB

BIN
src/main/java/blog/test.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 503 KiB

View File

@ -0,0 +1,97 @@
package contest.y2023;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class BW109 {
public static void main(String[] args) {
BW109 solution = new BW109();
// int[] nums = new int[]{2,3,6,1,9,2};
// int x = 5;
// System.out.println(solution.maxScore(nums,x));
System.out.println(solution.numberOfWays(4, 1));
}
public int numberOfWays(int n, int x) {
List<Integer> list = new ArrayList<>();
for (int i = 1; i <= n; i++) {
int sum = (int) Math.pow(i, x);
if (sum <= n) {
list.add(sum);
} else {
break;
}
}
int[] dp = new int[n + 1];
dp[0] = 1;
for (int num : list) {
for (int i = n; i >= num; i--) {
dp[i] = (dp[i] + dp[i - num]) % 1000000007;
}
}
return dp[n];
}
public long maxScore(int[] nums, int x) {
long max = nums[0];
long[] sum = new long[nums.length];
Arrays.fill(sum, Integer.MIN_VALUE);
sum[0] = nums[0];
int n1 = -1;
int n2 = -1;
if (nums[0] % 2 == 0) {
n2 = 0;
} else {
n1 = 0;
}
for (int i = 1; i < nums.length; i++) {
if (n1 >= 0) {
sum[i] = Math.max(sum[i], (nums[i] % 2 == 0 ? sum[n1] + nums[i] - x : sum[n1] + nums[i]));
}
if (n2 >= 0) {
sum[i] = Math.max(sum[i], (nums[i] % 2 == 0 ? sum[n2] + nums[i] : sum[n2] + nums[i] - x));
}
if (nums[i] % 2 == 0) {
n2 = i;
} else {
n1 = i;
}
max = Math.max(max, sum[i]);
}
return max;
}
public String sortVowels(String s) {
String str = "aeiouAEIOU";
List<String> list = new ArrayList<>();
for (int i = 0; i < s.length(); i++) {
if (str.contains(String.valueOf(s.charAt(i)))) {
list.add(String.valueOf(s.charAt(i)));
}
}
Collections.sort(list);
StringBuilder nstr = new StringBuilder();
int index = 0;
for (int i = 0; i < s.length(); i++) {
if (str.contains(String.valueOf(s.charAt(i)))) {
nstr.append(list.get(index));
index++;
} else {
nstr.append(String.valueOf(s.charAt(i)));
}
}
return nstr.toString();
}
public boolean isGood(int[] nums) {
Arrays.sort(nums);
for (int i = 0; i < nums.length - 1; i++) {
if (i + 1 != nums[i]) {
return false;
}
}
return nums[nums.length - 1] == nums.length - 1;
}
}

View File

@ -0,0 +1,114 @@
package contest.y2023;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class BW111 {
public static void main(String[] args) {
BW111 so = new BW111();
List<Integer> nums = Arrays.asList(2, 1, 3, 2, 1);
so.minimumOperations(nums);
}
public int numberOfBeautifulIntegers(int low, int high, int k) {
int cnt = 0;
List<Integer> list = Arrays.asList(10, 1001, 100011, 10000111);
if (low <= 10 && high >= 10) {
int num = 10;
int add = k % 2 == 0 ? k : 2 * k;
while (num % k != 0 && num <= high) {
num += 2;
}
if (num > high) {
return cnt;
}
cnt += (Math.min(99, high) - num) / add + 1;
}
if (low <= 1001 && high >= 1001) {
int num = 1001;
int add = k % 2 == 0 ? k : 2 * k;
while (num % k != 0 && num <= high) {
num += 2;
}
if (num > high) {
return cnt;
}
cnt += (Math.min(9999, high) - num) / add + 1;
}
if (low <= 100011 && high >= 100011) {
int num = 100011;
int add = k % 2 == 0 ? k : 2 * k;
while (num % k != 0 && num <= high) {
num += 2;
}
if (num > high) {
return cnt;
}
cnt += (Math.min(999999, high) - num) / add + 1;
}
if (low <= 10000111 && high >= 10000111) {
int num = 10000111;
int add = k % 2 == 0 ? k : 2 * k;
while (num % k != 0 && num <= high) {
num += 2;
}
if (num > high) {
return cnt;
}
cnt += (Math.min(99999999, high) - num) / add + 1;
}
return cnt;
}
public int minimumOperations(List<Integer> nums) {
int x = 0, y = 0, z = 0;
for (int num : nums) {
if (num == 1) {
y++;
z++;
} else if (num == 2) {
z++;
y = Math.min(x, y);
x++;
} else {
z = Math.min(x, Math.min(y, z));
x++;
y++;
}
}
return Math.min(x, Math.min(y, z));
}
public boolean canMakeSubsequence(String str1, String str2) {
if (str2.length() > str1.length()) {
return false;
}
int index = 0;
for (char ch : str1.toCharArray()) {
if (index == str2.length()) {
break;
}
if (ch == str2.charAt(index) || (ch == 'z' ? 'a' : ch + 1) == str2.charAt(index)) {
index++;
}
}
return index == str2.length();
}
public int countPairs(List<Integer> nums, int target) {
int cnt = 0;
Collections.sort(nums);
for (int i = 0; i < nums.size(); i++) {
for (int j = i + 1; j < nums.size(); j++) {
if (nums.get(i) + nums.get(j) < target) {
cnt++;
} else {
break;
}
}
}
return cnt;
}
}

View File

@ -0,0 +1,96 @@
package contest.y2023;
import java.util.*;
public class BW112 {
public static void main(String[] args) {
BW112 so = new BW112();
// String s1 = "abe";
// String s2 = "bea";
// so.checkStrings(s1, s2);
List<Integer> nums = Arrays.asList(2, 6, 7, 3, 1, 7);
int m = 3;
int k = 4;
so.maxSum(nums, m, k);
}
public long maxSum(List<Integer> nums, int m, int k) {
long max = 0;
Set<Integer> set = new HashSet<>();
List<Integer> list = new ArrayList<>();
long sum = 0;
for (int i = 0; i < nums.size(); i++) {
sum += nums.get(i);
set.add(nums.get(i));
list.add(nums.get(i));
if (i < k - 1) {
continue;
} else if (i == k - 1) {
if (set.size() >= m) {
max = Math.max(max, sum);
}
} else {
list.remove(nums.get(i - k));
if (!list.contains(nums.get(i - k))) {
set.remove(nums.get(i - k));
}
sum -= nums.get(i - k);
if (set.size() >= m) {
max = Math.max(max, sum);
}
}
}
return max;
}
public boolean checkStrings(String s1, String s2) {
char[] ch1 = s1.toCharArray();
char[] ch2 = s2.toCharArray();
int size = s1.length();
char[] ch10 = new char[size / 2 + (size % 2)];
char[] ch20 = new char[size / 2 + (size % 2)];
char[] ch11 = new char[size / 2];
char[] ch21 = new char[size / 2];
for (int i = 0; i < size / 2 + (size % 2); i++) {
ch10[i] = ch1[i * 2];
ch20[i] = ch2[i * 2];
if (i < size / 2) {
ch11[i] = ch1[i * 2 + 1];
ch21[i] = ch2[i * 2 + 1];
}
}
Arrays.sort(ch10);
Arrays.sort(ch11);
Arrays.sort(ch20);
Arrays.sort(ch21);
for (int i = 0; i < size / 2 + (size % 2); i++) {
if (ch10[i] != ch20[i]) {
return false;
}
if (i < size / 2) {
if (ch11[i] != ch21[i]) {
return false;
}
}
}
return true;
}
public boolean canBeEqual(String s1, String s2) {
char[] ch1 = s1.toCharArray();
char[] ch2 = s2.toCharArray();
for (int i = 0; i < 4; i++) {
if (ch1[i] != ch2[i]) {
if (i > 1) {
return false;
} else {
if (ch1[i + 2] != ch2[i]) {
return false;
}
ch1[i + 2] = ch1[i];
}
}
}
return true;
}
}

View File

@ -0,0 +1,123 @@
package contest.y2023;
import java.lang.reflect.Array;
import java.util.*;
public class Week354 {
public static void main(String[] args) {
String word = "cbaaaabc";
List<String> forbidden = Arrays.asList("aaa", "cb");
System.out.println(longestValidSubstring(word, forbidden));
}
static List<String> strs = new ArrayList<>();
public static int longestValidSubstring(String word, List<String> forbidden) {
Comparator<String> lengthComparator = Comparator.comparingInt(String::length);
forbidden.sort(lengthComparator);
for (int i = 0; i < forbidden.size(); i++) {
for (int j = i + 1; j < forbidden.size(); j++) {
if(forbidden.get(j).contains(forbidden.get(i))){
forbidden.remove(forbidden.get(j));
}
}
}
int max = getMax(word, forbidden);
strs.sort(lengthComparator);
while (strs.size() > 0 && strs.get(strs.size() - 1).length() > max) {
String str = strs.get(strs.size() - 1);
strs.remove(str);
max = Math.max(max, getMax(str, forbidden));
strs.sort(lengthComparator);
}
return max;
}
private static int getMax(String word, List<String> forbidden) {
for (String str : forbidden) {
if (word.contains(str)) {
int index = word.indexOf(str);
if (!strs.contains(word.substring(0, index))) {
strs.add(word.substring(0, index));
}
if (!strs.contains(word.substring(index + 1))) {
strs.add(word.substring(index + 1));
}
String substring = word.substring(0, index + str.length() - 1);
if (!strs.contains(substring)) {
strs.add(substring);
}
String substring1 = word.substring(index + str.length());
if (!strs.contains(substring1)) {
strs.add(substring1);
}
return 0;
}
}
return word.length();
}
public int minimumIndex(List<Integer> nums) {
int[] arr = new int[nums.size()];
for (int i = 0; i < nums.size(); i++) {
arr[i] = nums.get(i);
}
Arrays.sort(arr);
int cnt = 1;
int num = arr[0];
int max = 0;
for (int i = 1; i < nums.size(); i++) {
if (arr[i] == arr[i - 1]) {
cnt++;
} else {
if (max < cnt) {
max = cnt;
num = arr[i - 1];
}
cnt = 1;
}
}
if (max < cnt) {
max = cnt;
num = arr[nums.size() - 1];
}
if (cnt * 2 == nums.size() + 1) {
return -1;
}
cnt = 0;
for (int i = 0; i < nums.size(); i++) {
if (nums.get(i) == num) {
cnt++;
if (cnt * 2 > i + 1 && (max - cnt) * 2 > nums.size() - i - 1) {
return i;
}
}
}
return -1;
}
public int maximumBeauty(int[] nums, int k) {
Arrays.sort(nums);
int max = 0;
int index = 0;
for (int i = 1; i < nums.length; i++) {
while (nums[i] - nums[index] > k * 2) {
max = Math.max(max, i - index);
index++;
}
}
return Math.max(max, nums.length - index);
}
public int sumOfSquares(int[] nums) {
int sum = 0;
int size = nums.length;
for (int i = 0; i < size; i++) {
if (size % (i + 1) == 0) {
sum += nums[i] * nums[i];
}
}
return sum;
}
}

View File

@ -0,0 +1,66 @@
package contest.y2023;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Week355 {
public static void main(String[] args) {
Week355 so = new Week355();
// List<String> words = Arrays.asList("one.two.three","four.five","six");
// char separator = '.';
// System.out.println(so.splitWordsBySeparator(words,separator));
List<Integer> usageLimits = Arrays.asList(2, 2, 2);
System.out.println(so.maxIncreasingGroups(usageLimits));
}
public int maxIncreasingGroups(List<Integer> usageLimits) {
Collections.sort(usageLimits);
int size = usageLimits.size();
int left = 1;
int right = size;
while (left < right) {
int mid = (left + right + 1) / 2;
int cnt = 0;
for (int i = 1; i <= size; i++) {
cnt += Math.max(0, mid - i);
cnt -= Math.min(cnt, usageLimits.get(size - i - 1));
}
if (cnt == 0) {
left = mid;
} else {
right = mid - 1;
}
}
return left;
}
public long maxArrayValue(int[] nums) {
long max = nums[nums.length - 1];
long cur = nums[nums.length - 1];
for (int i = nums.length - 2; i >= 0; i--) {
if (nums[i] <= cur) {
cur += nums[i];
} else {
max = Math.max(max, cur);
cur = nums[i];
}
}
return Math.max(max, cur);
}
public List<String> splitWordsBySeparator(List<String> words, char separator) {
List<String> list = new ArrayList<>();
for (String word : words) {
String[] strs = word.split("\\" + separator);
for (String str : strs) {
if (str.length() > 0) {
list.add(str);
}
}
}
return list;
}
}

View File

@ -0,0 +1,109 @@
package contest.y2023;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Week356 {
public static void main(String[] args) {
Week356 so = new Week356();
String a = "ab", b = "a", c = "c";
so.minimumString(a, b, c);
}
public String minimumString(String a, String b, String c) {
String str = getStr(getStr(a, b), c);
List<String> list = new ArrayList<>();
list.add(getStr(a, getStr(b, c)));
list.add(getStr(getStr(a, c), b));
list.add(getStr(a, getStr(c, b)));
list.add(getStr(getStr(b, a), c));
list.add(getStr(b, getStr(a, c)));
list.add(getStr(getStr(b, c), a));
list.add(getStr(b, getStr(c, a)));
list.add(getStr(getStr(c, a), b));
list.add(getStr(c, getStr(a, b)));
list.add(getStr(getStr(c, b), a));
list.add(getStr(c, getStr(b, a)));
for (String tmp : list) {
tmp = getStr(a, getStr(b, c));
if (str.length() < tmp.length() || (str.length() == tmp.length() && isBef(tmp, str))) {
str = tmp;
}
}
return str;
}
private boolean isBef(String str1, String str2) {
char[] ch1 = str1.toCharArray();
char[] ch2 = str2.toCharArray();
for (int i = 0; i < ch1.length; i++) {
if (ch1[i] < ch2[i]) {
return true;
} else if (ch1[i] > ch2[i]) {
return false;
}
}
return true;
}
private String getStr(String str1, String str2) {
if (str1.contains(str2)) {
return str1;
}
for (int i = str2.length(); i >= 1; i--) {
if (str1.endsWith(str2.substring(0, i))) {
return str1 + str2.substring(i);
}
}
return str1 + str2;
}
public int countCompleteSubarrays(int[] nums) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : nums) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
int size = map.size();
int res = 0;
for (int i = 0; i < nums.length; i++) {
int cnt = 1;
map = new HashMap<>();
map.put(nums[i], 1);
if (cnt == size) {
res++;
}
for (int j = i + 1; j < nums.length; j++) {
if (map.containsKey(nums[j])) {
map.put(nums[j], map.get(nums[j]) + 1);
} else {
map.put(nums[j], 1);
cnt++;
}
if (cnt == size) {
res += nums.length - j;
break;
}
if (cnt + nums.length - 1 - j < size) {
break;
}
}
if (nums.length - 1 - i < size) {
break;
}
}
return res;
}
public int numberOfEmployeesWhoMetTarget(int[] hours, int target) {
int cnt = 0;
for (int hour : hours) {
if (hour >= target) {
cnt++;
}
}
return cnt;
}
}

View File

@ -0,0 +1,88 @@
package contest.y2023;
import com.code.leet.entiy.ListNode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Week358 {
public static void main(String[] args) {
Week358 so = new Week358();
// ListNode head = new ListNode(Arrays.asList(9,9,9));
// so.doubleIt(head);
}
public int minAbsoluteDifference(List<Integer> nums, int x) {
int min = Integer.MAX_VALUE;
for (int i = 0; i < nums.size(); i++) {
for (int j = i + x; j < nums.size(); j++) {
min = Math.min(min, Math.abs(nums.get(i) - nums.get(j)));
}
}
return min;
}
public ListNode doubleIt(ListNode head) {
List<Integer> list = new ArrayList<>();
ListNode curr = head;
while (curr != null) {
list.add(curr.val);
curr = curr.next;
}
boolean bl = false;
for (int i = list.size() - 1; i >= 0; i--) {
int num = list.get(i);
num *= 2;
if (bl) {
num += 1;
}
if (num >= 10) {
list.set(i, num -= 10);
bl = true;
} else {
list.set(i, num);
bl = false;
}
}
if (bl) {
list.add(0, 1);
}
head = new ListNode(list.get(0));
curr = head;
for (int i = 1; i < list.size(); i++) {
curr.next = new ListNode(list.get(i));
curr = curr.next;
}
return head;
}
public int maxSum(int[] nums) {
int maxSum = -1;
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
// 获取两个数数位上的最大数字
int maxDigitI = getMaxDigit(nums[i]);
int maxDigitJ = getMaxDigit(nums[j]);
// 如果最大数字相等则更新最大和
if (maxDigitI == maxDigitJ) {
maxSum = Math.max(maxSum, nums[i] + nums[j]);
}
}
}
return maxSum;
}
public int getMaxDigit(int num) {
int maxDigit = 0;
while (num > 0) {
int digit = num % 10;
maxDigit = Math.max(maxDigit, digit);
num /= 10;
}
return maxDigit;
}
}

View File

@ -0,0 +1,98 @@
//<p>给出基数为 <strong>-2</strong>&nbsp;的两个数&nbsp;<code>arr1</code> &nbsp;<code>arr2</code>返回两数相加的结果</p>
//
//<p>数字以&nbsp;<em>数组形式</em><strong>&nbsp;</strong>给出数组由若干 0 1 组成按最高有效位到最低有效位的顺序排列例如<code>arr&nbsp;= [1,1,0,1]</code>&nbsp;表示数字&nbsp;<code>(-2)^3&nbsp;+ (-2)^2 + (-2)^0 = -3</code><em>数组形式</em>&nbsp;中的数字 <code>arr</code> 也同样不含前导零&nbsp;<code>arr == [0]</code>&nbsp;&nbsp;<code>arr[0] == 1</code></p>
//
//<p>返回相同表示形式的 <code>arr1</code> <code>arr2</code> 相加的结果两数的表示形式为不含前导零由若干 0 1 组成的数组</p>
//
//<p>&nbsp;</p>
//
//<p><strong>示例 1</strong></p>
//
//<pre>
//<strong>输入</strong>arr1 = [1,1,1,1,1], arr2 = [1,0,1]
//<strong>输出</strong>[1,0,0,0,0]
//<strong>解释</strong>arr1 表示 11arr2 表示 5输出表示 16
//</pre>
//
//<p>
// <meta charset="UTF-8" /></p>
//
//<p><strong>示例 2</strong></p>
//
//<pre>
//<strong>输入</strong>arr1 = [0], arr2 = [0]
//<strong>输出</strong>[0]
//</pre>
//
//<p><strong>示例 3</strong></p>
//
//<pre>
//<strong>输入</strong>arr1 = [0], arr2 = [1]
//<strong>输出</strong>[1]
//</pre>
//
//<p>&nbsp;</p>
//
//<p><strong>提示</strong></p>
//<meta charset="UTF-8" />
//
//<ul>
// <li><code>1 &lt;= arr1.length,&nbsp;arr2.length &lt;= 1000</code></li>
// <li><code>arr1[i]</code>&nbsp;&nbsp;<code>arr2[i]</code>&nbsp;都是&nbsp;<code>0</code>&nbsp;&nbsp;<code>1</code></li>
// <li><code>arr1</code>&nbsp;&nbsp;<code>arr2</code>&nbsp;都没有前导0</li>
//</ul>
//
//<div><div>Related Topics</div><div><li>数组</li><li>数学</li></div></div><br><div><li>👍 80</li><li>👎 0</li></div>
package leetcode.editor.cn;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
// 1073:负二进制数相加
public class AddingTwoNegabinaryNumbers {
public static void main(String[] args) {
Solution solution = new AddingTwoNegabinaryNumbers().new Solution();
// TO TEST
}
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int[] addNegabinary(int[] arr1, int[] arr2) {
int i = arr1.length - 1, j = arr2.length - 1;
int carry = 0;
List<Integer> ans = new ArrayList<>();
while (i >= 0 || j >= 0 || carry != 0) {
int x = carry;
if (i >= 0) {
x += arr1[i];
}
if (j >= 0) {
x += arr2[j];
}
if (x >= 2) {
ans.add(x - 2);
carry = -1;
} else if (x >= 0) {
ans.add(x);
carry = 0;
} else {
ans.add(1);
carry = 1;
}
--i;
--j;
}
while (ans.size() > 1 && ans.get(ans.size() - 1) == 0) {
ans.remove(ans.size() - 1);
}
int[] arr = ans.stream().mapToInt(Integer::intValue).toArray();
for (i = 0, j = ans.size() - 1; j >= 0; i++, j--) {
arr[i] = ans.get(j);
}
return arr;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,103 @@
////有一组 n 个人作为实验对象 0 n - 1 编号其中每个人都有不同数目的钱以及不同程度的安静值quietness为了方便起见我们将编号
// x 的人简称为 "person x "
//
// 给你一个数组 richer 其中 richer[i] = [ai, bi] 表示 person ai person bi 更有钱另给你一个整数数组
// quiet 其中 quiet[i] person i 的安静值richer 中所给出的数据 逻辑自洽也就是说 person x
//person y 更有钱的同时不会出现 person y person x 更有钱的情况
//
// 现在返回一个整数数组 answer 作为答案其中 answer[x] = y 的前提是在所有拥有的钱肯定不少于 person x 的人中
//person y 是最安静的人也就是安静值 quiet[y] 最小的人
//
//
//
// 示例 1
//
//
//输入richer = [[1,0],[2,1],[3,1],[3,7],[4,3],[5,3],[6,3]], quiet = [3,2,5,4,6,1,
//7,0]
//输出[5,5,2,5,4,5,6,7]
//解释
//answer[0] = 5
//person 5 person 3 有更多的钱person 3 person 1 有更多的钱person 1 person 0 有更多的钱
//
//唯一较为安静有较低的安静值 quiet[x]的人是 person 7
//但是目前还不清楚他是否比 person 0 更有钱
//answer[7] = 7
//在所有拥有的钱肯定不少于 person 7 的人中这可能包括 person 3456 以及 7
//最安静有较低安静值 quiet[x]的人是 person 7
//其他的答案也可以用类似的推理来解释
//
//
// 示例 2
//
//
//输入richer = [], quiet = [0]
//输出[0]
//
//
//
//
// 提示
//
//
// n == quiet.length
// 1 <= n <= 500
// 0 <= quiet[i] < n
// quiet 的所有值 互不相同
// 0 <= richer.length <= n * (n - 1) / 2
// 0 <= ai, bi < n
// ai != bi
// richer 中的所有数对 互不相同
// richer 的观察在逻辑上是一致的
//
//
// Related Topics 深度优先搜索 拓扑排序 数组 👍 222 👎 0
package leetcode.editor.cn;
import java.util.*;
// 851:喧闹和富有
public class LoudAndRich {
public static void main(String[] args) {
Solution solution = new LoudAndRich().new Solution();
}
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int[] loudAndRich(int[][] richer, int[] quiet) {
Map<Integer, List<Integer>> map = new HashMap<>();
for (int i = 0; i < quiet.length; i++) {
map.put(i,new ArrayList<>());
}
int[] sizes = new int[quiet.length];
for (int[] rich : richer) {
map.computeIfAbsent(rich[0], key -> new ArrayList<>()).add(rich[1]);
sizes[rich[1]]++;
}
int[] res = new int[quiet.length];
Queue<Integer> queue = new ArrayDeque<>();
for (int i = 0; i < quiet.length; i++) {
res[i]=i;
if(sizes[i]==0){
queue.offer(i);
}
}
while (!queue.isEmpty()) {
int key = queue.poll();
for (int index : map.get(key)) {
if (quiet[res[key]] < quiet[res[index]]) {
res[index] = res[key]; // 更新 x 的邻居的答案
}
if (--sizes[index] == 0) {
queue.offer(index);
}
}
}
return res;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,45 @@
<p>给出基数为 <strong>-2</strong>&nbsp;的两个数&nbsp;<code>arr1</code>&nbsp;<code>arr2</code>,返回两数相加的结果。</p>
<p>数字以&nbsp;<em>数组形式</em><strong>&nbsp;</strong>给出:数组由若干 0 和 1 组成,按最高有效位到最低有效位的顺序排列。例如,<code>arr&nbsp;= [1,1,0,1]</code>&nbsp;表示数字&nbsp;<code>(-2)^3&nbsp;+ (-2)^2 + (-2)^0 = -3</code><em>数组形式</em>&nbsp;中的数字 <code>arr</code> 也同样不含前导零:即&nbsp;<code>arr == [0]</code>&nbsp;&nbsp;<code>arr[0] == 1</code></p>
<p>返回相同表示形式的 <code>arr1</code><code>arr2</code> 相加的结果。两数的表示形式为:不含前导零、由若干 0 和 1 组成的数组。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>arr1 = [1,1,1,1,1], arr2 = [1,0,1]
<strong>输出:</strong>[1,0,0,0,0]
<strong>解释:</strong>arr1 表示 11arr2 表示 5输出表示 16 。
</pre>
<p>
<meta charset="UTF-8" /></p>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>arr1 = [0], arr2 = [0]
<strong>输出:</strong>[0]
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>arr1 = [0], arr2 = [1]
<strong>输出:</strong>[1]
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<meta charset="UTF-8" />
<ul>
<li><code>1 &lt;= arr1.length,&nbsp;arr2.length &lt;= 1000</code></li>
<li><code>arr1[i]</code>&nbsp;&nbsp;<code>arr2[i]</code>&nbsp;都是&nbsp;<code>0</code>&nbsp;&nbsp;<code>1</code></li>
<li><code>arr1</code>&nbsp;&nbsp;<code>arr2</code>&nbsp;都没有前导0</li>
</ul>
<div><div>Related Topics</div><div><li>数组</li><li>数学</li></div></div><br><div><li>👍 80</li><li>👎 0</li></div>

View File

@ -0,0 +1,48 @@
<p>有一组 <code>n</code> 个人作为实验对象,从 <code>0</code><code>n - 1</code> 编号其中每个人都有不同数目的钱以及不同程度的安静值quietness。为了方便起见我们将编号为&nbsp;<code>x</code>&nbsp;的人简称为 "person&nbsp;<code>x</code>&nbsp;"。</p>
<p>给你一个数组 <code>richer</code> ,其中 <code>richer[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> 表示 person&nbsp;<code>a<sub>i</sub></code>&nbsp;比 person&nbsp;<code>b<sub>i</sub></code>&nbsp;更有钱。另给你一个整数数组 <code>quiet</code> ,其中&nbsp;<code>quiet[i]</code> 是 person <code>i</code> 的安静值。<code>richer</code> 中所给出的数据 <strong>逻辑自洽</strong>(也就是说,在 person <code>x</code> 比 person <code>y</code> 更有钱的同时,不会出现 person <code>y</code> 比 person <code>x</code> 更有钱的情况 )。</p>
<p>现在,返回一个整数数组 <code>answer</code> 作为答案,其中&nbsp;<code>answer[x] = y</code>&nbsp;的前提是,在所有拥有的钱肯定不少于&nbsp;person&nbsp;<code>x</code>&nbsp;的人中person&nbsp;<code>y</code>&nbsp;是最安静的人(也就是安静值&nbsp;<code>quiet[y]</code>&nbsp;最小的人)。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>richer = [[1,0],[2,1],[3,1],[3,7],[4,3],[5,3],[6,3]], quiet = [3,2,5,4,6,1,7,0]
<strong>输出:</strong>[5,5,2,5,4,5,6,7]
<strong>解释: </strong>
answer[0] = 5
person 5 比 person 3 有更多的钱person 3 比 person 1 有更多的钱person 1 比 person 0 有更多的钱。
唯一较为安静(有较低的安静值 quiet[x])的人是 person 7
但是目前还不清楚他是否比 person 0 更有钱。
answer[7] = 7
在所有拥有的钱肯定不少于 person 7 的人中(这可能包括 person 3456 以及 7
最安静(有较低安静值 quiet[x])的人是 person 7。
其他的答案也可以用类似的推理来解释。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>richer = [], quiet = [0]
<strong>输出:</strong>[0]
</pre>
&nbsp;
<p><strong>提示:</strong></p>
<ul>
<li><code>n == quiet.length</code></li>
<li><code>1 &lt;= n &lt;= 500</code></li>
<li><code>0 &lt;= quiet[i] &lt; n</code></li>
<li><code>quiet</code> 的所有值 <strong>互不相同</strong></li>
<li><code>0 &lt;= richer.length &lt;= n * (n - 1) / 2</code></li>
<li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li>
<li><code>a<sub>i </sub>!= b<sub>i</sub></code></li>
<li><code>richer</code> 中的所有数对 <strong>互不相同</strong></li>
<li><strong> </strong><code>richer</code> 的观察在逻辑上是一致的</li>
</ul>
<div><div>Related Topics</div><div><li>深度优先搜索</li><li></li><li>拓扑排序</li><li>数组</li></div></div><br><div><li>👍 222</li><li>👎 0</li></div>