You are given an integer array nums. We consider an array good if it is a permutation of an array base[n].
base[n] = [1, 2, ..., n - 1, n, n] (in other words, it is an array of length n + 1 which contains 1 to n - 1 exactly once, plus two occurrences of n). For example, base[1] = [1, 1] and base[3] = [1, 2, 3, 3].
Return true if the given array is good, otherwise return false.
Note: A permutation of integers represents an arrangement of these numbers.
Example 1:
Example 2:
Example 3:
1 <= nums.length <= 1001 <= num[i] <= 200/**
* @param {number[]} nums
* @return {boolean}
*/
var isGood = function(nums) {
const length = nums.length;
nums.sort((a, b) => a - b);
if (nums[length - 1] !== nums[length - 2]) {
return false;
}
let number = 1;
for (let i = 0; i < length - 1; i++) {
if (nums[i] !== number) {
return false;
}
number++;
}
return true;
};
nums of distinct integers, return all the possible permutations. You can return the answer in any order.Example 1:
Example 2:
1 <= nums.length <= 6-10 <= nums[i] <= 10nums are unique./**
* @param {number[]} nums
* @return {number[][]}
*/
var permute = function(nums) {
const answer = [];
const dfs = (permutation, rest) => {
if (!rest.length) {
answer.push(permutation);
return;
}
for (let i = 0; i < rest.length; i++) {
dfs([...permutation, rest[i]], [...rest.slice(0, i), ...rest.slice(i+1)]);
}
};
dfs([], nums);
return answer;
};
You are given a 0-indexed integer array nums and a target element target.
A target index is an index i such that nums[i] == target.
Return a list of the target indices of nums after sorting nums in non-decreasing order. If there are no target indices, return an empty list.
Example 1:
Example 2
1 <= nums.length <= 1001 <= nums[i], target <= 100/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var targetIndices = function(nums, target) {
const answer = [];
if (!nums.includes(target)) {
return answer;
}
nums.sort((a, b) => a - b);
const binarySearch = (start, end) => {
if (start > end) {
return;
}
const middle = Math.floor((start + end)/2);
if (nums[middle] === target) {
answer.push(middle);
}
binarySearch(start, middle - 1);
binarySearch(middle + 1, end);
}
binarySearch(0, nums.length - 1);
return answer.sort((a, b) => a - b);
};