You are given a 0-indexed integer array nums
of even length consisting of an equal number of positive and negative integers.
You should rearrange the elements of nums
such that the modified array follows the given conditions:
Every consecutive pair of integers have opposite signs.
For all integers with the same sign, the order in which they were present in nums is preserved.
The rearranged array begins with a positive integer.
Return the modified array after rearranging the elements to satisfy the aforementioned conditions.
Example 1:
Example 2:
2 <= nums.length <= 2 * 10^5
nums.length
is even1 <= |nums[i]| <= 10^5
nums
consists of equal number of positive and negative integers./**
* @param {number[]} nums
* @return {number[]}
*/
var rearrangeArray = function(nums) {
const positiveNums = nums.filter(x => x > 0);
const negativeNums = nums.filter(x => x < 0);
const answer = [];
for (let i = 0; i < nums.length / 2; i++) {
answer.push(positiveNums[i], negativeNums[i]);
}
return answer;
};
It is a sweltering summer day, and a boy wants to buy some ice cream bars.
At the store, there are n
ice cream bars. You are given an array costs
of length n
, where costs[i]
is the price of the ith
ice cream bar in coins. The boy initially has coins
coins to spend, and he wants to buy as many ice cream bars as possible.
*Note: The boy can buy the ice cream bars in any order.
Return the maximum number of ice cream bars the boy can buy with coins
coins.
You must solve the problem by counting sort.
Example 1:
Input: costs = [1,3,2,4,1], coins = 7
Output: 4
Explanation: The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7.
Example 2:
Input: costs = [10,6,8,7,7,8], coins = 5
Output: 0
Explanation: The boy cannot afford any of the ice cream bars.
Example 3:
Input: costs = [1,6,3,1,2,5], coins = 20
Output: 6
Explanation: The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18.
costs.length == n
1 <= n <= 10^5
1 <= costs[i] <= 10^5
1 <= coins <= 10^8
/**
* @param {number[]} costs
* @param {number} coins
* @return {number}
*/
var maxIceCream = function(costs, coins) {
let iceCreamCount = 0;
costs.sort((a, b) => a - b);
for (const cost of costs) {
if (cost > coins) {
break;
} else {
coins -= cost;
iceCreamCount++;
}
}
return iceCreamCount;
};
x
를 y
로 변환하려고 합니다. 사용할 수 있는 연산은 다음과 같습니다.x
에 n
을 더합니다x
에 2를 곱합니다.x
에 3을 곱합니다.x
, y
, n
이 매개변수로 주어질 때, x
를 y
로 변환하기 위해 필요한 최소 연산 횟수를 return하도록 solution 함수를 완성해주세요. 이때 x
를 y
로 만들 수 없다면 -1
을 return 해주세요.x
≤ y
≤ 1,000,000n
< y
function solution(x, y, n) {
let answer = -1;
const valueSet = new Set();
const queue = [[x, 0]];
while (queue.length) {
const [value, count] = queue.shift();
valueSet.add(value);
if (value === y) {
return count;
}
if (value > y) {
continue;
}
if (!valueSet.has(value + n)) {
queue.push([value + n, count + 1]);
}
if (!valueSet.has(value * 2)) {
queue.push([value * 2, count + 1]);
}
if (!valueSet.has(value * 3)) {
queue.push([value * 3, count + 1]);
}
}
return answer;
}
function solution(x, y, n) {
var answer = 0;
const dp = new Array(y + 1).fill(Infinity);
dp[x] = 0;
for (let i = x; i <= y; i++) {
dp[i + n] = Math.min(dp[i + n], dp[i] + 1);
dp[i * 2] = Math.min(dp[i * 2], dp[i] + 1);
dp[i * 3] = Math.min(dp[i * 3], dp[i] + 1);
}
return dp[y] === Infinity ? - 1 : dp[y];
}
Given a binary tree root
and an integer target
, delete all the leaf nodes with value target
.
Note that once you delete a leaf node with value target
, if its parent node becomes a leaf node and has the value target
, it should also be deleted (you need to continue doing that until you cannot).
[1, 3000]
.1 <= Node.val, target <= 1000
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {number} target
* @return {TreeNode}
*/
var removeLeafNodes = function(root, target) {
const dfs = (node) => {
if (!node) {
return null;
}
const { val, left, right } = node;
node.left = dfs(left);
node.right = dfs(right);
return val === target && !node.right && !node.left ? null : node;
};
return dfs(root);
};