The next greater element of some element x
in an array is the first greater element that is to the right of x in the same array.
You are given two distinct 0-indexed integer arrays nums1
and nums2
, where nums1
is a subset of nums2
.
For each 0 <= i < nums1.length
, find the index j
such that nums1[i] == nums2[j]
and determine the next greater element of nums2[j]
in nums2
. If there is no next greater element, then the answer for this query is -1
.
Return an array ans
of length nums1.length
such that ans[i]
is the next greater element as described above.
Example 1
Example 2
1 <= nums1.length <= nums2.length <= 1000
0 <= nums1[i], nums2[i] <= 10^4
nums1
and nums2
are unique.nums1
also appear in nums2
./**
* @param {number[]} nums1
* @param {number[]} nums2
* @return {number[]}
*/
var nextGreaterElement = function(nums1, nums2) {
return nums1.map((number) => {
let nextNumberIndex = nums2.indexOf(number) + 1;
while (nextNumberIndex < nums2.length && nums2[nextNumberIndex] < number) {
nextNumberIndex++;
}
return nextNumberIndex >= nums2.length ? -1 : nums2[nextNumberIndex];
});
};
/**
* @param {number[]} nums1
* @param {number[]} nums2
* @return {number[]}
*/
var nextGreaterElement = function(nums1, nums2) {
const numObj = {};
const stack = [];
for (let i = 0; i < nums2.length; i++) {
let curNumber = nums2[i];
while (curNumber > stack[stack.length - 1]) {
let nextNumber = stack.pop();
numObj[nextNumber] = curNumber;
}
stack.push(curNumber);
}
return nums1.map((number) => numObj[number] || -1);
};
Given an integer array nums
of length n
where all the integers of nums are in the range [1, n]
and each integer appears once or twice, return an array of all the integers that appears twice.
You must write an algorithm that runs in O(n)
time and uses only constant extra space.
Example 1:
Example 2:
n == nums.length
1 <= n <= 10^5
1 <= nums[i] <= n
/**
* @param {number[]} nums
* @return {number[]}
*/
var findDuplicates = function(nums) {
const numMap = new Map();
const answer = [];
for (const number of nums) {
if (!numMap.get(number)) {
numMap.set(number, 1);
} else {
numMap.set(number, 2);
answer.push(number);
}
}
return answer;
};
Given two strings ransomNote
and magazine
, return true
if ransomNote
can be constructed by using the letters from magazine
and false
otherwise.
Each letter in magazine
can only be used once in ransomNote
.
Example 1:
Example 2:
1 <= ransomNote.length, magazine.length <= 10^5
ransomNote
and magazine
consist of lowercase English letters./**
* @param {string} ransomNote
* @param {string} magazine
* @return {boolean}
*/
var canConstruct = function(ransomNote, magazine) {
const magazineMap = new Map();
for (let i = 0; i < magazine.length; i++) {
const str = magazine[i];
magazineMap.set(str, (magazineMap.get(str) || 0) + 1);
}
for (let i = 0; i < ransomNote.length; i++) {
const str = ransomNote[i];
if (!magazineMap.get(str)) {
return false;
} else {
magazineMap.set(str, magazineMap.get(str) - 1);
}
}
return true;
}
nums
, return true
if you can partition the array into two subsets such that the sum of the elements in both subsets is equal or false
otherwise.Example 1:
Example 2:
1 <= nums.length <= 200
1 <= nums[i] <= 100
/**
* @param {number[]} nums
* @return {boolean}
*/
var canPartition = function(nums) {
const totalSum = nums.reduce((sum, number) => sum + number, 0);
if (totalSum % 2 === 1) {
return false;
}
const target = totalSum /2;
const dp = { 0: true };
for (let i = 0; i < nums.length; i++) {
const number = nums[i];
Object.keys(dp).forEach((element) => {
let sum = parseInt(element);
dp[sum + number] = true;
dp[sum] = true;
});
}
return dp[target] ? true : false;
};
nums
and an integer k
, return true
if it is possible to divide this array into k
non-empty subsets whose sums are all equal.Example 1:
Example 2:
1 <= k <= nums.length <= 16
1 <= nums[i] <= 10^4
[1, 4]
./**
* @param {number[]} nums
* @param {number} k
* @return {boolean}
*/
var canPartitionKSubsets = function(nums, k) {
const totalSum = nums.reduce((sum, number) => sum + number, 0);
if (totalSum % k) {
return false;
}
const target = totalSum / k;
const visited = new Array(nums.length).fill(false);
nums.sort((a, b) => b - a);
const backtracking = (index, subsetCount, sum) => {
if (subsetCount === 0) {
return true;
}
if (sum === target) {
return backtracking(0, subsetCount - 1, 0);
}
for (let i = index; i < nums.length; i++) {
const number = nums[i];
if (visited[i] || (sum + number > target)) {
continue;
}
visited[i] = true;
if (backtracking(i + 1, subsetCount, sum + number)) {
return true;
}
visited[i] = false;
}
return false;
};
return backtracking(0, k, 0);
};
Given a circular integer array nums
(i.e., the next element of nums[nums.length - 1]
is nums[0]
), return the next greater number for every element in nums
.
The next greater number of a number x
is the first greater number to its traversing-order next in the array, which means you could search circularly to find its next greater number. If it doesn't exist, return -1
for this number.
1 <= nums.length <= 10^4
-10^9 <= nums[i] <= 10^9
* @param {number[]} nums
* @return {number[]}
*/
var nextGreaterElements = function(nums) {
const length = nums.length;
const stack = [];
const answer = new Array(length).fill(-1);
for (let i = 0; i < 2 * length; i++) {
let index = i % length;
while (stack.length && nums[index] > nums[stack[stack.length - 1]]) {
answer[stack.pop()] = nums[index];
}
stack.push(index);
}
return answer;
};
정말 좋은 정보 감사합니다!