kk
This commit is contained in:
22
src/main/java/com/dota/bit/_2857/Solution.java
Normal file
22
src/main/java/com/dota/bit/_2857/Solution.java
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package com.dota.bit._2857;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public int countPairs(List<List<Integer>> coordinates, int k) {
|
||||||
|
int res = 0;
|
||||||
|
var map = new HashMap<Long, Integer>();
|
||||||
|
for (List<Integer> coordinate : coordinates) {
|
||||||
|
var x = coordinate.get(0);
|
||||||
|
var y = coordinate.get(1);
|
||||||
|
|
||||||
|
for (int i = 0; i <= k; i++) {
|
||||||
|
res += map.getOrDefault((x ^ i) * 2000000L + (y ^ (k - i)), 0);
|
||||||
|
}
|
||||||
|
map.merge(x * 2000000L + y, 1, Integer::sum);
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
36
src/main/java/com/dota/bit/_3153/Solution.java
Normal file
36
src/main/java/com/dota/bit/_3153/Solution.java
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
package com.dota.bit._3153;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
new Solution().sumDigitDifferences(new int[]{13,23,12});
|
||||||
|
}
|
||||||
|
public long sumDigitDifferences(int[] nums) {
|
||||||
|
long res = 0;
|
||||||
|
int t = nums[0];
|
||||||
|
int len = 0;
|
||||||
|
while (t > 0) {
|
||||||
|
len++;
|
||||||
|
t /= 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<Integer, Integer>[] dp = new Map[len];
|
||||||
|
for (int i = 0; i < dp.length; i++) {
|
||||||
|
dp[i] = new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
int sum=0;
|
||||||
|
for (int num : nums) {
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
t = num % 10;
|
||||||
|
res = res + (sum - dp[i].getOrDefault(t, 0));
|
||||||
|
dp[i].put(t, 1 + dp[i].getOrDefault(t, 0));
|
||||||
|
num /= 10;
|
||||||
|
}
|
||||||
|
sum++;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user