62. Analyzing not make any comparison operator to find a larger value of two integers
Topic Link
Title Description
Enter a description:
Output two integers a and b, a and b are 32-bit integers.
Output Description:
Output an integer, the larger of two numbers a.
Entry
1 0
Export
1
Remarks:
The time complexity of O (. 1) O ( . 1 ), the additional space complexity O (. 1) O ( . 1 ).
Heavy and difficult
sort sort
Topic analysis
- Acquiring an integer a, b;
- Sorted using the sort function, comparing the numerical size;
- A large number of output: 1 subscript number.
var input = readline().split(' '); var num = []; num[0] = parseInt(input[0]); num[1] = parseInt(input[1]); num.sort(function(a,b){ return a-b; }); print(num[1]);
63. 1365. How much less than the current number of digital
Source title
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/how-many-numbers-are-smaller-than-the-current-number
Title Description
To give you an array nums, all figures for the number of each element nums [i], you count the array smaller than it therein.
In other words, for each nums [i] You must calculate the effective number of j, where j satisfies j! = I and nums [j] <nums [i].
The answer is returned in an array.
Example 1
Input: nums = [8,1,2,2,3]
Output: [4,0,1,1,3]
Explanation:
For nums [0] = 8, there are four smaller than its numbers: (1 , 2 and 3).
For nums [1] = 1 is smaller than its number does not exist.
There is a smaller number than it is for nums [2] = 2: ( 1).
For nums [3] = 2 there is a smaller number than it: (1).
For nums [4] = 3 it is smaller than the presence of three figures: (1 and 2).
Example 2
Input: nums = [6,5,4,8]
Output: [2,1,0,3]
Example 3
Input: nums = [7,7,7,7]
Output: [0,0,0,0]
prompt:
2 <= nums.length <= 500
0 <= nums[i] <= 100
Key Technology
sort sort
Topic analysis
- Nums assigned to the new array: res = [] .concat (nums);
- Sort by Sort values given, after sorting, each number corresponding to the number of the subscript is smaller than this number.
- Found source array nums numbers sequentially in the sorted array index;
- Sequentially output index.
/** * @param {number[]} nums * @return {number[]} */ var smallerNumbersThanCurrent = function(nums) { var res = [].concat(nums); //把nums赋值给新的数组 res.sort(function(a,b){ return a-b; }); var index = []; for(let i=0;i<nums.length;i++){ for(let j=0;j<nums.length;j++){ if(nums[i] == res[j]){ index.push(j); break; } } } return index; };
64. 1103. points Confectionery II
Source title
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/distribute-candies-to-people
Title Description
Cruncher, sub-candy.
We bought some candy candies, they are going to give queued n = num_people two children.
A child to the first confectionery, the second two children, and so on, until the last child to n pieces of candy.
Then, we go back to the starting point of the team, to the first pieces of candy children n + 1, n + 2 second child stars, and so on, until the last child to 2 * n pieces of candy.
Repeat the process (and more each time than the last given a candy, when the team arrived at the end from the beginning starting point for the team again) until we finish all the sub-candy. Note that even if the rest is not in our hands the number of candy (candy before issuing more than once), these candies will be distributed to all current children.
Returns an array of NUM_PEOPLE length, and for the elements of the candies to represent the case of candies final distribution (i.e. ANS [i] represents the number of i-th candy assigned to children).
Example 1
Input: candies = 7, num_people = 4
Output: [1,2,3,1]
Explanation:
first, ans [0] + = 1 , the array becomes [1,0,0,0].
Second, ans [1] + = 2 , the array becomes [1,2,0,0].
Third, ans [2] + = 3 , the array becomes [1,2,3,0].
Fourth, ans [3] + = 1 ( because the only one candy), the final array becomes [1,2,3,1].
Example 2
Input: candies = 10, num_people = 3
Output: [5,2,3]
Explanation:
first, ans [0] + = 1 , the array becomes [1,0,0].
Second, ans [1] + = 2 , the array becomes [1,2,0].
Third, ans [2] + = 3 , the array becomes [2,3].
Fourth, ans [0] + = 4 , the final array becomes [5,2,3].
prompt:
1 <= candies <= 10^9
1 <= num_people <= 1000
Heavy and difficult
Initialize the array with the same value:Array.from({ length }, () => 值)
Topic analysis
- Initialize the array using 0: Array.from (Array (num_people), () => 0);
- With each record num how much sugar to;
- Analyzing num points according candy, candy whether the remaining> 0, if> 0, then points to each child candy num;
- If <0, not in accordance with the prior num minutes gave the children remaining candy, candy and the total number of 0 is assigned.
/** * @param {number} candies * @param {number} num_people * @return {number[]} */ var distributeCandies = function(candies, num_people) { const res = Array.from(Array(num_people), () => 0); //使用0来初始化数组 let num = 0; while(candies){ for(let i=0;i<num_people;i++){ num++; if(candies - num > 0){ res[i] += num; candies -= num; } else{ res[i] += candies; candies = 0; break; } } } return res; };