Leetcode Algorithn
Today(3/30) Algorithm
1603. Design Parking System
Example 1:
Input
["ParkingSystem", "addCar", "addCar", "addCar", "addCar"][1, 1, 0], [1], [2], [3], [1]]
Output
[null, true, true, false, false]
Explanation
ParkingSystem parkingSystem = new ParkingSystem(1, 1, 0);
parkingSystem.addCar(1); // return true because there is 1 available slot for a big car
parkingSystem.addCar(2); // return true because there is 1 available slot for a medium car
parkingSystem.addCar(3); // return false because there is no available slot for a small car
parkingSystem.addCar(1); // return false because there is no available slot for a big car. It is already occupied.
code
var ParkingSystem = function(big, medium, small) {
bigCar = big;
mediumCar = medium;
smallCar = small;
};
/**
* @param {number} carType
* @return {boolean}
*/
ParkingSystem.prototype.addCar = function(carType) {
if(carType === 1 && bigCar){
bigCar--
return true;
}
if(carType === 2 && mediumCar){
mediumCar--
return true;
}
if(carType === 3 && smallCar){
smallCar--
return true;
}
return false
};
1512. Number of Good Pairs
Example 1:
Input: nums = [1,2,3,1,1,3]
Output: 4
Explanation: There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed.
Example 2:
Input: nums = [1,1,1,1]
Output: 6
Explanation: Each pair in the array are good.
Example 3:
Input: nums = [1,2,3]
Output: 0
code
var numIdenticalPairs = function(nums) {
let numArr =[];
for(let i =0; i< nums.length; i++){
for (let j =1; j<nums.length; j++){
if(i !== j && i < j){
if(nums[i] === nums[j]) {
numArr.push([i, j])
}
}
}
}
return numArr.length;
};
1365. How Many Numbers Are Smaller Than the Current Number
Example 1:
Input: nums = [8,1,2,2,3]
Output: [4,0,1,1,3]
Explanation:
For nums[0]=8 there exist four smaller numbers than it (1, 2, 2 and 3).
For nums[1]=1 does not exist any smaller number than it.
For nums[2]=2 there exist one smaller number than it (1).
For nums[3]=2 there exist one smaller number than it (1).
For nums[4]=3 there exist three smaller numbers than it (1, 2 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]
code
var smallerNumbersThanCurrent = function(nums) {
let array = [];
for(let i= 0; i < nums.length; i++){
let count= 0;
for(let j = 0; j<nums.length; j++) {
if(nums[i] > nums[j]){
count++
}
}
array.push(count);
}
return array;
};
1281. Subtract the Product and Sum of Digits of an Integer
Example 1:
Input: n = 234
Output: 15
Explanation:
Product of digits = 2 3 4 = 24
Sum of digits = 2 + 3 + 4 = 9
Result = 24 - 9 = 15
Example 2:
Input: n = 4421
Output: 21
Explanation:
Product of digits = 4 4 2 * 1 = 32
Sum of digits = 4 + 4 + 2 + 1 = 11
Result = 32 - 11 = 21
code
var subtractProductAndSum = function(n) {
let arr = n.toString().split("")
let multiply = arr.reduce((prev, curr) => {
return Number(prev * curr)
})
let sum = arr.reduce((prev, curr) => {
return Number(prev) + Number(curr)
})
return multiply - sum
};
Author And Source
이 문제에 관하여(Leetcode Algorithn), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@qkrdudgh052/Leetcode-Algorithn저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)