Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Given two 0-indexed integer arrays nums1
and nums2
, return a list answer
of size 2
where:
answer[0]
is a list of all distinct integers in nums1
which are not present in nums2
.answer[1]
is a list of all distinct integers in nums2
which are not present in nums1
.Note that the integers in the lists may be returned in any order.
Example 1:
Input: nums1 = [1,2,3], nums2 = [2,4,6]
Output: [[1,3],[4,6]]
Explanation:
For nums1, nums1[1] = 2 is present at index 0 of nums2, whereas nums1[0] = 1 and nums1[2] = 3 are not present in nums2. Therefore, answer[0] = [1,3].
For nums2, nums2[0] = 2 is present at index 1 of nums1, whereas nums2[1] = 4 and nums2[2] = 6 are not present in nums2. Therefore, answer[1] = [4,6].
Example 2:
Input: nums1 = [1,2,3,3], nums2 = [1,1,2,2]
Output: [[3],[]]
Explanation:
For nums1, nums1[2] and nums1[3] are not present in nums2. Since nums1[2] == nums1[3], their value is only included once and answer[0] = [3].
Every integer in nums2 is present in nums1. Therefore, answer[1] = [].
Constraints:
1 <= nums1.length, nums2.length <= 1000
-1000 <= nums1[i], nums2[i] <= 1000
def findDifference(self, nums1, nums2):
s1, s2 = set(nums1), set(nums2)
return [list(s1 - s2), list(s2 - s1)]
vector<vector<int>> findDifference(vector<int>& nums1, vector<int>& nums2) {
vector<int> v1, v2;
set<int> s1(begin(nums1), end(nums1)), s2(begin(nums2), end(nums2));
set_difference(begin(s1), end(s1), begin(s2), end(s2), back_inserter(v1));
set_difference(begin(s2), end(s2), begin(s1), end(s1), back_inserter(v2));
return {v1, v2};
}
class Solution {
public List<List<Integer>> findDifference(int[] nums1, int[] nums2) {
Set<Integer> s1 = new HashSet<Integer>();
Set<Integer> s2 = new HashSet<Integer>();
for(int i : nums1){
s1.add(i);
}
for(int i : nums2){
s2.add(i);
}
List<List<Integer>> ret = new ArrayList();
ret.add(new ArrayList());
ret.add(new ArrayList());
for(int i : s1){
if(!s2.contains(i)){
ret.get(0).add(i);
}
}
for(int i : s2){
if(!s1.contains(i)){
ret.get(1).add(i);
}
}
return ret;
}
}
class Solution:
def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]:
set1=set(nums1)
set2=set(nums2)
return [ list(set1-set2), list(set2-set1) ]
You are given a 0-indexed integer array nums
. The array nums
is beautiful if:
nums.length
is even.nums[i] != nums[i + 1]
for all i % 2 == 0
.Note that an empty array is considered beautiful.
You can delete any number of elements from nums
. When you delete an element, all the elements to the right of the deleted element will be shifted one unit to the left to fill the gap created and all the elements to the left of the deleted element will remain unchanged.
Return the minimum number of elements to delete from nums
to make it beautiful.
Example 1:
Input: nums = [1,1,2,3,5]
Output: 1
Explanation: You can delete either nums[0] or nums[1] to make nums = [1,2,3,5] which is beautiful. It can be proven you need at least 1 deletion to make nums beautiful.
Example 2:
Input: nums = [1,1,2,2,3,3]
Output: 2
Explanation: You can delete nums[0] and nums[5] to make nums = [1,2,2,3] which is beautiful. It can be proven you need at least 2 deletions to make nums beautiful.
Constraints:
1 <= nums.length <= 105
0 <= nums[i] <= 105
def minDeletion(self, A):
res, pre = 0, -1
for a in A:
if a == pre:
res += 1
else:
pre = a if pre < 0 else -1
return res + (pre >= 0)
public int minDeletion(int[] A) {
int res = 0, pre = -1;
for (int a : A) {
if (a == pre)
res++;
else
pre = pre < 0 ? a : -1;
}
return pre < 0 ? res : res + 1;
}
int minDeletion(vector<int>& A) {
int res = 0, pre = -1;
for (int& a: A) {
if (a == pre)
res++;
else
pre = pre < 0 ? a : -1;
}
return res + (pre >= 0);
}
Given an integer array queries
and a positive integer intLength
, return an array answer
where answer[i]
is either the queries[i]th
smallest positive palindrome of length intLength
or -1
if no such palindrome exists.
A palindrome is a number that reads the same backwards and forwards. Palindromes cannot have leading zeros.
Example 1:
Input: queries = [1,2,3,4,5,90], intLength = 3
Output: [101,111,121,131,141,999]
Explanation:
The first few palindromes of length 3 are:
101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 202, ...
The 90th palindrome of length 3 is 999.
Example 2:
Input: queries = [2,4,6], intLength = 4
Output: [1111,1331,1551]
Explanation:
The first six palindromes of length 4 are:
1001, 1111, 1221, 1331, 1441, and 1551.
Constraints:
1 <= queries.length <= 5 * 104
1 <= queries[i] <= 109
1 <= intLength <= 15
int reverse(long long n, bool skip) {
long long res = 0;
for (n = skip ? n / 10 : n; n > 0; n /= 10)
res = res * 10 + n % 10;
return res;
}
vector<long long> kthPalindrome(vector<int>& queries, int sz) {
vector<long long> res;
long long start = pow(10, (sz + 1) / 2 - 1), end = pow(10, (sz + 1 ) / 2), mul = pow(10, sz / 2);
for (int q : queries)
res.push_back(start + q > end ? -1 :
(start + q - 1) * mul + reverse(start + q - 1, sz % 2));
return res;
}
class Solution {
/*
First Palindrome of length 4 = "10"+"01"
First Palindrome of length 3 = "10"+"_1" (without first character 0)
First half can range from 10 (which is 10^1) to 99 (which is 10^2-1)
*/
public long[] kthPalindrome(int[] queries, int intLength) {
long[] result = new long[queries.length];
int i = 0;
for(int num: queries){
long half = (intLength+1)/2;//half for 3 =2, half for 4 = 2
long start = (long)Math.pow(10,half-1);
long end = (long)Math.pow(10,half)-1;
if(num<=(end-start+1)){ //check if query is within range
String firstHalfOfPalindrome = ((start)+ (num-1))+"";
String secondHalf = (new StringBuffer(firstHalfOfPalindrome)).reverse().toString();
result[i++] = Long.parseLong(firstHalfOfPalindrome+secondHalf.substring(intLength%2)); // since half is calculated as 1 extra for odd numbers, remove the first char for odd length in substring - 1001 vs 10+(0)1
}else{
result[i++]=-1;
}
}
return result;
}
}
def kthPalindrome(self, queries, l):
base = 10 ** ((l - 1) / 2)
res = [q - 1 + base for q in queries]
for i,a in enumerate(res):
a = str(a) + str(a)[-1 - l % 2::-1]
res[i] = int(a) if len(a) == l else -1
return res
There are n
piles of coins on a table. Each pile consists of a positive number of coins of assorted denominations.
In one move, you can choose any coin on top of any pile, remove it, and add it to your wallet.
Given a list piles
, where piles[i]
is a list of integers denoting the composition of the ith
pile from top to bottom, and a positive integer k
, return the maximum total value of coins you can have in your wallet if you choose exactly k
coins optimally.
Example 1:
Input: piles = [[1,100,3],[7,8,9]], k = 2
Output: 101
Explanation:
The above diagram shows the different ways we can choose k coins.
The maximum total we can obtain is 101.
Example 2:
Input: piles = [[100],[100],[100],[100],[100],[100],[1,1,1,1,1,1,700]], k = 7
Output: 706
Explanation:
The maximum total can be obtained if we choose all coins from the last pile.
Constraints:
n == piles.length
1 <= n <= 1000
1 <= piles[i][j] <= 105
1 <= k <= sum(piles[i].length) <= 2000
public int maxValueOfCoins(List<List<Integer>> piles, int k) {
Integer[][] memo = new Integer[piles.size() + 1][k + 1];
return dp(piles, memo, 0, k);
}
public int dp(List<List<Integer>> piles, Integer[][] memo, int i, int k) {
if (k == 0 || i == piles.size()) return 0;
if (memo[i][k] != null) return memo[i][k];
int res = dp(piles, memo, i + 1, k);
int cur = 0;
for (int j = 0; j < Math.min(piles.get(i).size(), k); ++j) {
cur += piles.get(i).get(j);
res = Math.max(res, cur + dp(piles, memo, i + 1, k - j - 1));
}
return memo[i][k] = res;
}
int maxValueOfCoins(vector<vector<int>>& A, int K) {
int n = A.size();
vector<vector<int>> memo(n + 1, vector<int>(K + 1, 0));
function<int(int, int)> dp = [&](int i, int k) {
if (memo[i][k] > 0) return memo[i][k];
if (i == n || k == 0) return 0;
int res = dp(i + 1, k), cur = 0;
for (int j = 0; j < A[i].size() && j < k; ++j) {
cur += A[i][j];
res = max(res, dp(i + 1, k - j - 1) + cur);
}
memo[i][k] = res;
return res;
};
return dp(0, K);
}
def maxValueOfCoins(self, A, K):
@functools.lru_cache(None)
def dp(i, k):
if k == 0 or i == len(A): return 0
res, cur = dp(i + 1, k), 0
for j in range(min(len(A[i]), k)):
cur += A[i][j]
res = max(res, cur + dp(i+1, k-j-1))
return res
return dp(0, K)
In our experience, we suggest you solve this Weekly Contest 286 LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.
If you are stuck anywhere between any coding problem, just visit Queslers to get the Weekly Contest 286 LeetCode Solution
I hope this Weekly Contest 286 LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.
This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.
Keep Learning!
More Coding Solutions >>