Цена за 48 часов в ленте | 3000,00 |
Цена за 1 час закрепления | N/A |
Взаимопиар | Нет |
Дополнительные условия рекламы | Отсутствуют |
Input: expression = "-1/2+1/2+1/3"
Output: "1/3"
var fractionAddition = function(expression) {
let sign = [];
if (expression[0] !== '-') sign.push('+');
for (let char of expression) {
if (char === '+' || char === '-') sign.push(char);
}
let fractions = expression.replace(/-/g, '+-').split('+').filter(f => f.length > 0);
let prevNum = 0, prevDen = 1, i = 0;
for (let sub of fractions) {
let [num, den] = sub.split('/').map(Number);
let g = gcd(prevDen, den);
if (sign[i++] === '+') prevNum = prevNum * den / g + num * prevDen / g;
else prevNum = prevNum * den / g - num * prevDen / g;
prevDen = prevDen * den / g;
g = Math.abs(gcd(prevDen, prevNum));
prevNum /= g;
prevDen /= g;
}
return `${prevNum}/${prevDen}`;
};
function gcd(a, b) {
while (b !== 0) {
[a, b] = [b, a % b];
}
return a;
}
Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
Output: [2,6,14,11,7,3,12,8,4,13,9,10,5,1]
function Node(val, children) {
this.val = val;
this.children = children ? children : [];
}
var postorder = function(root) {
if (!root) return [];
const stack = [root];
const output = [];
while (stack.length) {
const node = stack.pop();
output.unshift(node.val);
for (const child of node.children) {
stack.push(child);
}
}
return output;
};
Input: s = "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL tsetnoc"
class Solution {
reverseWords(s) {
let chars = s.split('');
let lastSpaceIndex = -1;
let len = chars.length;
for (let strIndex = 0; strIndex <= len; strIndex++) {
if (strIndex == len || chars[strIndex] == ' ') {
let startIndex = lastSpaceIndex + 1;
let endIndex = strIndex - 1;
while (startIndex < endIndex) {
let temp = chars[startIndex];
chars[startIndex] = chars[endIndex];
chars[endIndex] = temp;
startIndex++;
endIndex--;
}
lastSpaceIndex = strIndex;
}
}
return chars.join('');
}
}
Input: n = 12
Output: 21
class Solution {
swap(s, i0, i1) {
if (i0 === i1) return s;
let chars = s.split('');
[chars[i0], chars[i1]] = [chars[i1], chars[i0]];
return chars.join('');
}
constructor() {
this.list = [];
}
permute(a, l, r) {
if (l === r) {
this.list.push(a);
} else {
for (let i = l; i <= r; i++) {
a = this.swap(a, l, i);
this.permute(a, l + 1, r);
a = this.swap(a, l, i);
}
}
}
nextGreaterElement(n) {
let s = '' + n;
this.permute(s, 0, s.length - 1);
this.list.sort();
let index = this.list.indexOf(s);
if (index !== -1 && index < this.list.length - 1) {
let result = parseInt(this.list[index + 1]);
if (result <= 2147483647) {
return result;
}
}
return -1;
}
}
Input: wall = [[1,2,2,1],[3,1,2],[1,3,2],[2,4],[3,1,2],[1,3,1,1]]
Output: 2
var leastBricks = function(wall) {
let pos = new Array(wall.length).fill(0);
let sum = wall[0].reduce((a, b) => a + b, 0);
let res = Infinity;
while (sum !== 0) {
let count = 0;
for (let i = 0; i < wall.length; i++) {
if (wall[i][pos[i]] !== 0) {
count++;
} else {
pos[i]++;
}
wall[i][pos[i]]--;
}
sum--;
res = Math.min(res, count);
}
return res;
};
Input: password = "a"
Output: 5
function strongPasswordChecker(s) {
const n = s.length;
const hasLower = /[a-z]/.test(s);
const hasUpper = /[A-Z]/.test(s);
const hasDigit = /\d/.test(s);
let repeatCount = 0;
for (let i = 0; i < n;) {
const start = i;
while (i < n && s[i] === s[start]) {
i++;
}
repeatCount += Math.floor((i - start) / 3);
}
const missingTypes = 3 - [hasLower, hasUpper, hasDigit].filter(Boolean).length;
if (n < 6) {
return Math.max(missingTypes, 6 - n);
} else if (n <= 20) {
return Math.max(missingTypes, repeatCount);
} else {
let excessChars = n - 20;
let overLenReduction = 0;
for (let i = 2; i < n && excessChars > 0; i++) {
if (i % 3 === 2 && s[i] === s[i - 1] && s[i] === s[i - 2]) {
overLenReduction++;
excessChars--;
}
}
repeatCount -= overLenReduction;
return (n - 20) + Math.max(missingTypes, repeatCount);
}
}
Input: nums = [1000,100,10,2]
Output: "1000/(100/10/2)"
Explanation: 1000/(100/10/2) = 1000/((100/10)/2) = 200
However, the bold parenthesis in "1000/((100/10)/2)" are redundant since they do not influence the operation priority.
So you should return "1000/(100/10/2)".
Other cases:
1000/(100/10)/2 = 50
1000/(100/(10/2)) = 50
1000/100/10/2 = 0.5
1000/100/(10/2) = 2
class Solution {
addStrings(num1, num2) {
let ans = [];
let carry = 0;
const n1 = num1.length;
const n2 = num2.length;
for (let i = 0; i < Math.max(n1, n2) || carry; i++) {
const digit1 = i < n1 ? num1[i] : 0;
const digit2 = i < n2 ? num2[i] : 0;
const sum = digit1 + digit2 + carry;
carry = Math.floor(sum / 10);
ans.push(sum % 10);
}
return ans;
}
multiplyOneDigit(firstNumber, secondNumberDigit, numZeros) {
const currentResult = new Array(numZeros).fill(0);
let carry = 0;
for (const digit of firstNumber) {
const multiplication = (secondNumberDigit * digit) + carry;
carry = Math.floor(multiplication / 10);
currentResult.push(multiplication % 10);
}
if (carry !== 0) {
currentResult.push(carry);
}
return currentResult;
}
multiply(firstNumber, secondNumber) {
if (firstNumber === "0" || secondNumber === "0") {
return "0";
}
firstNumber = firstNumber.split('').reverse().join('');
secondNumber = secondNumber.split('').reverse().join('');
let ans = new Array(firstNumber.length + secondNumber.length).fill(0);
for (let i = 0; i < secondNumber.length; i++) {
ans = this.addStrings(this.multiplyOneDigit(firstNumber, +secondNumber[i], i), ans);
}
while (ans[ans.length - 1] === 0) {
ans.pop();
}
return ans.reverse().join('');
}
}
Input: board = [["X",".",".","X"],[".",".",".","X"],[".",".",".","X"]]
Output: 2
function countBattleships(board) {
let m = board.length, n = board[0].length;
let count = 0;
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (board[i][j] === 'X') {
if ((i === 0 || board[i - 1][j] === '.') && (j === 0 || board[i][j - 1] === '.')) {
count++;
}
}
}
}
return count;
}
Input: sentence = ["hello","world"], rows = 2, cols = 8
Output: 1
function wordsTyping(sentence, rows, cols) {
const sentenceStr = sentence.join(" ") + " ";
const length = sentenceStr.length;
let count = 0;
for (let i = 0; i < rows; i++) {
count += cols;
if (sentenceStr[count % length] === " ") {
count++;
} else {
while (count > 0 && sentenceStr[(count - 1) % length] !== " ") {
count--;
}
}
}
return Math.floor(count / length);
}
Input: heights = [[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]]
Output: [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]
function pacificAtlantic(heights) {
const m = heights.length, n = heights[0].length;
const pacific = Array.from({ length: m }, () => Array(n).fill(false));
const atlantic = Array.from({ length: m }, () => Array(n).fill(false));
const directions = [[-1, 0], [1, 0], [0, -1], [0, 1]];
function dfs(r, c, ocean) {
ocean[r][c] = true;
for (const [dr, dc] of directions) {
const nr = r + dr, nc = c + dc;
if (nr >= 0 && nc >= 0 && nr < m && nc < n && !ocean[nr][nc] && heights[nr][nc] >= heights[r][c]) {
dfs(nr, nc, ocean);
}
}
}
for (let i = 0; i < m; i++) {
dfs(i, 0, pacific);
dfs(i, n - 1, atlantic);
}
for (let j = 0; j < n; j++) {
dfs(0, j, pacific);
dfs(m - 1, j, atlantic);
}
const result = [];
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (pacific[i][j] && atlantic[i][j]) {
result.push([i, j]);
}
}
}
return result;
}
Input: nums = [1,5,11,5]
Output: true
function canPartition(nums) {
const sum = nums.reduce((a, b) => a + b, 0)
if (sum % 2 !== 0) return false
const target = sum / 2
const dp = Array(target + 1).fill(false)
dp[0] = true
for (const num of nums) {
for (let j = target; j >= num; j--) {
dp[j] = dp[j] || dp[j - num]
}
}
return dp[target]
}
Input: num1 = "11", num2 = "123"
Output: "134"
function thirdMax(nums) {
let first = null;
let second = null;
let third = null;
for (const num of nums) {
if (num === first || num === second || num === third) {
continue;
}
if (first === null || num > first) {
third = second;
second = first;
first = num;
} else if (second === null || num > second) {
third = second;
second = num;
} else if (third === null || num > third) {
third = num;
}
}
return third !== null ? third : first;
}