image_2021-11-30_16-14-54.png
36.1 KB
#N617. Merge Two Binary Trees
problem link
#solution
problem link
#solution
class Solution {
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
if(root1 == null && root2 == null) return null;
else if(root1 == null) return root2;
else if(root2 == null) return root1;
TreeNode res = new TreeNode(root1.val + root2.val);
res.left = mergeTrees(root1.left, root2.left);
res.right = mergeTrees(root1.right, root2.right);
return res;
}
}image_2021-12-03_18-30-06.png
41.6 KB
#N2053. Kth Distinct String in an Array
problem link
#solution
problem link
#solution
class Solution {
public String kthDistinct(String[] arr, int k) {
Map<String, Integer> map = new HashMap<>();
for(String s:arr){
if(map.containsKey(s)) map.put(s, map.get(s)+1);
else map.put(s, 1);
}
for(String s:arr){
if(map.get(s)==1)
if(k-- == 1)
return s;
}
return "";
}
}#recommendation
Advent of Code is a annually virtual event taking place in every December 1th-25th. Every day features new programming exercises, which can be solved in your programming language of choice.
It is full of interesting algorithmic problems, i recommend it)👍🏻
https://adventofcode.com/2021
Advent of Code is a annually virtual event taking place in every December 1th-25th. Every day features new programming exercises, which can be solved in your programming language of choice.
It is full of interesting algorithmic problems, i recommend it)👍🏻
https://adventofcode.com/2021
image_2021-12-04_14-43-10.png
73.3 KB
#N1160. Find Words That Can Be Formed by Characters
problem link
#solution
problem link
#solution
class Solution {
public int countCharacters(String[] words, String chars) {
int sum=0;
Map<Character, Integer> map = new HashMap<>();
for(char ch:chars.toCharArray()){
if(map.containsKey(ch))
map.put(ch, map.get(ch)+1);
else
map.put(ch, 1);
}
for(String word:words){
Map<Character, Integer> temp = new HashMap<>(map);
boolean isGood = true;
for(char ch:word.toCharArray()){
temp.put(ch, temp.getOrDefault(ch, 0)-1);
if(temp.get(ch)<0) {
isGood=false;
break;
}
}
if(isGood) {
sum+=word.length();
}
}
return sum;
}
}image_2021-12-04_16-17-14.png
56.5 KB
#N1002. Find Common Characters
problem link
#solution
problem link
#solution
class Solution {
public List<String> commonChars(String[] words) {
int[][] arr = new int[words.length][26];
int n=0;
for(String word:words){
for(char ch:word.toCharArray()){
arr[n][ch-'a']++;
}
n++;
}
List<String> list = new ArrayList<>();
for(int i=0; i<26; i++){
int k=100;
for(int j=0; j<arr.length; j++){
k=Math.min(k, arr[j][i]);
}
while(k-- >0){
list.add(Character.toString(i+'a'));
}
}
return list;
}
}image_2021-12-04_17-19-07.png
49.5 KB
#N349. Intersection of Two Arrays
problem link
#solution
problem link
#solution
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
int[][] arr = new int[2][1001];
List<Integer> list = new ArrayList<>();
for(int i:nums1)
arr[0][i]++;
for(int i:nums2)
arr[1][i]++;
for(int i=0; i<1001; i++){
if(arr[0][i]*arr[1][i]>0) list.add(i);
}
int[] res = new int[list.size()];
for(int i=0; i<list.size(); i++){
res[i] = list.get(i);
}
return res;
}
}image_2021-12-05_22-41-41.png
53.5 KB
#N1200. Minimum Absolute Difference
problem link
#solution
problem link
#solution
class Solution {
public List<List<Integer>> minimumAbsDifference(int[] arr) {
Arrays.sort(arr);
int diff = Integer.MAX_VALUE;
for(int i=0; i<arr.length-1; i++)
diff = Math.min(diff, arr[i+1] - arr[i]);
List<List<Integer>> list = new ArrayList<>();
for(int i=0; i<arr.length-1; i++){
if(diff == arr[i+1] - arr[i]){
List<Integer> sublist = new ArrayList<>();
sublist.add(arr[i]);
sublist.add(arr[i+1]);
list.add(sublist);
}
}
return list;
}
}👍1
image_2021-12-06_01-28-46.png
38.4 KB
#N1779. Find Nearest Point That Has the Same X or Y Coordinate
problem link
#solution
problem link
#solution
class Solution {
public int nearestValidPoint(int x, int y, int[][] points) {
int min=Integer.MAX_VALUE, index=-1;;
for(int i=points.length-1; i>=0; i--){
if(points[i][0]==x||points[i][1]==y){
if(min>=Math.abs(points[i][0]-x)+Math.abs(points[i][1]-y)){
min=Math.abs(points[i][0]-x)+Math.abs(points[i][1]-y);
index=i;
}
}
}
return index;
}
}image_2021-12-06_01-55-11.png
54.7 KB
#N999. Available Captures for Rook
problem link
#solution
problem link
#solution
class Solution {
public int numRookCaptures(char[][] board) {
int Rx=0, Ry=0, count=0;;
for(int i=0; i<8; i++){
for(int j=0; j<8; j++){
if(board[i][j]=='R'){
Rx=i;
Ry=j;
}
}
}
int[][] helper = new int[][]{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
for(int[] h:helper){
for(int i=Rx+h[0], j=Ry+h[1]; i>=0 && i<8 && j>=0 && j<8; i+=h[0], j+=h[1]){
if(board[i][j]=='p') count++;
if(board[i][j]!='.') break;
}
}
return count;
}
}image_2021-12-06_02-02-44.png
30.7 KB
#N1491. Average Salary Excluding the Minimum and Maximum Salary
problem link
#solution
problem link
#solution
class Solution {
public double average(int[] salary) {
Arrays.sort(salary);
int sum=0;
for(int i=1; i<salary.length-1; i++)
sum+=salary[i];
return (double)sum/(salary.length-2);
}
}image_2021-12-06_02-13-52.png
52.5 KB
#N929. Unique Email Addresses
problem link
#solution
problem link
#solution
class Solution {
public int numUniqueEmails(String[] emails) {
String lname, dname;
Map<String, Integer> map = new HashMap<>();
for(String email:emails){
StringBuilder s = new StringBuilder();
lname=email.split("@")[0];
dname=email.split("@")[1];
lname=lname.replace(".", "");
if(lname.contains("+")) lname=lname.substring(0, lname.indexOf("+"));
s.append(lname).append('@').append(dname);
map.put(s.toString(), map.getOrDefault(s.toString(), 0) +1);
}
return map.size();
}
}image_2021-12-06_11-27-57.png
77.3 KB
#N500. Keyboard Row
problem link
#solution
problem link
#solution
class Solution {
public String[] findWords(String[] words) {
String row1="QWERTYUIOPqwertyuiop";
String row2="ASDFGHJKLasdfghjkl";
String row3="ZXCVBNMzxcvbnm";
List<String> list = new ArrayList<>();
for(String word:words){
int[][] counter = new int[1][3];
for(int i=0; i<word.length(); i++){
if(row1.contains(word.valueOf(word.charAt(i))))
counter[0][0]++;
if(row2.contains(word.valueOf(word.charAt(i))))
counter[0][1]++;
if(row3.contains(word.valueOf(word.charAt(i))))
counter[0][2]++;
}
if(counter[0][0]==word.length()||counter[0][1]==word.length()||counter[0][2]==word.length()) list.add(word);
}
return list.toArray(new String[0]);
}
}image_2021-12-06_13-34-12.png
26.8 KB
#N766. Toeplitz Matrix
problem link
#solution
class Solution {
public boolean isToeplitzMatrix(int[][] matrix) {
for(int i=0; i<matrix.length-1; i++){
for(int j=0; j<matrix[i].length-1; j++){
if(matrix[i][j]!=matrix[i+1][j+1]) return false;
}
}
return true;
}
}image_2021-12-06_13-47-38.png
25.4 KB
#N908. Smallest Range I
problem link
#solution
problem link
#solution
class Solution {
public int smallestRangeI(int[] nums, int k) {
int max=nums[0], min=nums[0];
for(int n:nums){
max = Math.max(max, n);
min = Math.min(min, n);
}
return Math.max(max-min-2*k, 0);
}
}image_2021-12-06_14-34-59.png
51.5 KB
#N1385. Find the Distance Value Between Two Arrays
problem link
#solution
problem link
#solution
class Solution {
public int findTheDistanceValue(int[] arr1, int[] arr2, int d) {
int res=0, l=arr2.length;
Arrays.sort(arr1); Arrays.sort(arr2);
for(int n:arr1){
int count=0, start=0, end=l-1;
while(start<=end){
int mid=(start+end)/2;
if(Math.abs(arr2[mid]-n)<=d) {
count++;
break;
}
if(arr2[mid]>n+d) end=mid-1;
if(arr2[mid]<n-d) start=mid+1;
}
if(count==0) res++;
}
return res;
}
}image_2021-12-06_23-38-19.png
26.5 KB
#N575. Distribute Candies
problem link
#solution
problem link
#solution
class Solution {
public int distributeCandies(int[] candyType) {
Set<Integer> set = new HashSet<>();
for(int candy:candyType)
set.add(candy);
return Math.min(set.size(), candyType.length/2);
}
}image_2021-12-06_23-50-19.png
51.2 KB
#N1800. Maximum Ascending Subarray Sum
problem link
#solution
problem link
#solution
class Solution {
public int maxAscendingSum(int[] nums) {
List<Integer> sumlist = new ArrayList<>();
int sum=0, i=0;
for(; i<nums.length-1; i++){
if(nums[i]<nums[i+1]){
sum+=nums[i];
}else{
sum+=nums[i];
sumlist.add(sum);
sum=0;
}
}
sum+=nums[i];
sumlist.add(sum);
int max=sumlist.get(0);
for(int n:sumlist)
max=Math.max(max, n);
return max;
}
}image_2021-12-07_00-24-22.png
46.9 KB
#N1582. Special Positions in a Binary Matrix
problem link
#solution
problem link
#solution
class Solution {
public int numSpecial(int[][] mat) {
int count=0, row[]=new int[mat.length], col[]=new int[mat[0].length];
for(int i=0; i<mat.length; i++)
for(int j=0; j<mat[i].length; j++)
if(mat[i][j]==1){
row[i]++;
col[j]++;
}
for(int i=0; i<mat.length; i++)
for(int j=0; j<mat[i].length; j++)
if(mat[i][j]==1 && row[i] == 1 && col [j] == 1)
count++;
return count;
}
}image_2021-12-07_00-39-22.png
33.5 KB
#N1619. Mean of Array After Removing Some Elements
problem link
#solution
problem link
#solution
class Solution {
public double trimMean(int[] arr) {
Arrays.sort(arr);
for(int i=0; i<arr.length/20; i++)
arr[i]=arr[arr.length-i-1]=0;
int sum=0;
for(int n:arr)
sum+=n;
return (double)sum/(arr.length*9/10);
}
}image_2021-12-07_00-42-27.png
23.6 KB
#N1550. Three Consecutive Odds
problem link
#solution
problem link
#solution
class Solution {
public boolean threeConsecutiveOdds(int[] arr) {
for(int i=0; i<arr.length-2; i++)
if(arr[i]*arr[i+1]*arr[i+2] % 2 == 1) return true;
return false;
}
}