1360. Dates are a few days apart
Please write a program to calculate how many days are between two dates.
The date is given as a string in the format YYYY-MM-DD, as shown in the example.
Example 1: Input: date1 = "2019-06-29", date2 = "2019-06-30" Output: 1
This problem is lazy and solved directly by using Java library functions.(
import java.time.LocalDate; import java.time.temporal.ChronoUnit; class Solution { public int daysBetweenDates(String date1, String date2) { LocalDate startDate = LocalDate.parse(date1); LocalDate endDate = LocalDate.parse(date2); long daysDiff = ChronoUnit.DAYS.between(startDate, endDate); return Math.abs((int)daysDiff); } }
1361. Verify Binary Tree
There are n nodes i n the binary tree, numbered from 0 to n-1, where the two children of node i are leftChild[i] and rightChild[i].
Returns true only if all nodes can form and only form a valid binary tree; otherwise returns false.
If node i has no left child node, then leftChild[i] equals -1.The right child node also conforms to this rule.
Note: Node has no value, only node number is used in this problem.
Example 1: Input: n = 4, leftChild = [1,-1,3,-1], rightChild = [2,-1,-1,-1] Output:true
Be careful:
Sufficient conditions for binary trees:
1) Root node is not accessible
2) Other nodes are accessed only once
class Solution { public boolean validateBinaryTreeNodes(int n, int[] leftChild, int[] rightChild) { int[] cnt = new int[n]; for(int i=0; i<n; i++) { if(leftChild[i] >= 0) { cnt[leftChild[i]] += 1; } if(rightChild[i] >= 0) { cnt[rightChild[i]] += 1; } } if(cnt[0] != 0) { return false; } for(int i=1; i<n; i++) { if(cnt[i] != 1) { return false; } } return true; } }
1362. Closest factor
Give you an integer, num, and find two integers that meet all of the following requirements:
The product of two numbers equals num + 1 or num + 2
Measured by absolute difference, the two numbers are closest
You can return these two integers in any order.
Example 1: Input: num = 8 Output: [3,3] Interpretation: For num + 1 = 9, the closest two factors are 3 & 3; for num + 2 = 10, the closest two factors are 2 & 5, thus returning 3 & 3.
Skill: Traversing from sqrt to 1, the difference must be increasing gradually, so the first difference you encounter must be the smallest.
class Solution { public int[] closestDivisors(int num) { int[] r = new int[2]; int sqrt = (int) Math.sqrt(num + 2); //square root for(int i = sqrt; i >= 1; i--){ if((num + 1) % i == 0){ r[0] = i; r[1] = (num + 1) / i; return r; } if((num + 2) % i == 0){ r[0] = i; r[1] = (num + 2) / i; return r; } } return r; } }
1363. Form a maximum multiple of three
Give you an array of integers, digits. You can form a multiple of 3 by joining some of the numbers in any order. Please return the maximum multiple of 3 you can get.
Since the answer may not be in the range of integer data types, return the answer as a string.
If you cannot get an answer, return an empty string.
Example 1: Input: digits = [8,1,9] Output:'981'
Analysis:
Divided by 3, the sum of digits in each bit can also be divided by 3.
- Calculates the sum of digit arrays.
- Divide the numbers into 3i, 3i+1, 3i+2.
- If sum% 3 == 1, delete a 3i+1. If there is no such number, delete two 3i+2 (there are at least two 3i+2, as you can see); if sum% 3 == 2, the same is true.
The size of the observed data is only 0 to 9, and the number of occurrences of each number can be counted.
class Solution { private int[] num; private boolean delete(int k) { for (int i = k; i < 10; i = i + 3) { if (num[i] > 0) { num[i]--; return true; } } return false; } private String makeString() { StringBuilder ans = new StringBuilder(""); for (int i = 9; i >= 0; i--) while (num[i]-- > 0) ans.append((char)(i + '0')); int index = 0; while (index < ans.length() - 1) { if (ans.charAt(index) != '0') break; index++; } return ans.delete(0, index).toString(); } public String largestMultipleOfThree(int[] digits) { num = new int[10]; int sum = 0; for (int value : digits) { num[value]++; sum += value; } int k = sum % 3; if (k > 0) { if (!delete(k)) { delete(3 - k); delete(3 - k); } } return makeString(); } }