title: 2022-08-04-1403-非递增顺序的最小子序列
date: 2022-08-04 15:46:12
tags: [Daily Practice, 子序列, 简单题]
categories: 算法题
1403. 非递增顺序的最小子序列
JS
/**
* @param {number[]} nums
* @return {number[]}
*/
var minSubsequence = function(nums) {
let temp = 0, sum = _.sum(nums);;
const ans = [];
nums.sort((a, b) => a - b);
console.log(sum);
for (let i = nums.length - 1; i >= 0; i--) {
if (2 * temp > sum) {
break;
}
temp += nums[i];
ans.push(nums[i]);
}
return ans;
};
title: 2022-08-05-623-在二叉树中增加一行
date: 2022-08-05 14:55:12
tags: [Daily Practice, 二叉树, dfs, bfs, 中等题]
categories: 算法题
623. 在二叉树中增加一行
Java:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode addOneRow(TreeNode root, int v, int d) {
if (d == 0 || d == 1) {
TreeNode t = new TreeNode(v);
if (d == 1) t.left = root;
else t.right = root;
return t;
}
if (root != null && d > 1) {
root.left = addOneRow(root.left, v, d > 2 ? d - 1 : 1);
root.right = addOneRow(root.right, v, d > 2 ? d - 1 : 0);
}
return root;
}
JavaScript
var addOneRow = function(root, val, depth) {
if (!root) {
return null;
}
if (depth === 1) {
return new TreeNode(val, root, null);
}
if (depth === 2) {
root.left = new TreeNode(val, root.left, null);
root.right = new TreeNode(val, null, root.right);
} else {
root.left = addOneRow(root.left, val, depth - 1);
root.right = addOneRow(root.right, val, depth - 1);
}
return root;
};
Java
class Solution {
public TreeNode addOneRow(TreeNode root, int val, int depth) {
if (depth == 1) {
return new TreeNode(val, root, null);
}
List<TreeNode> curLevel = new ArrayList<TreeNode>();
curLevel.add(root);
for (int i = 1; i < depth - 1; i++) {
List<TreeNode> tmpt = new ArrayList<TreeNode>();
for (TreeNode node : curLevel) {
if (node.left != null) {
tmpt.add(node.left);
}
if (node.right != null) {
tmpt.add(node.right);
}
}
curLevel = tmpt;
}
for (TreeNode node : curLevel) {
node.left = new TreeNode(val, node.left, null);
node.right = new TreeNode(val, null, node.right);
}
return root;
}
}
JavaScript
var addOneRow = function(root, val, depth) {
if (depth === 1) {
return new TreeNode(val, root, null);
}
let curLevel = [];
curLevel.push(root);
for (let i = 1; i < depth - 1; i++) {
const tmp = [];
for (const node of curLevel) {
if (node.left) {
tmp.push(node.left);
}
if (node.right) {
tmp.push(node.right);
}
}
curLevel = tmp;
}
for (const node of curLevel) {
node.left = new TreeNode(val, node.left, null);
node.right = new TreeNode(val, null, node.right);
}
return root;
};
title: 2022-08-06-1408-数组中的字符串匹配
date: 2022-08-06 14:55:12
tags: [Daily Practice, 字符串, 简单题]
categories: 算法题
1408. 数组中的字符串匹配
Java
class Solution {
public List<String> stringMatching(String[] words) {
List<String> res = new ArrayList<>();
for (String word : words) {
for (int i = 0; i < words.length; i++) {
if (word.length() >= words[i].length()) continue;
if (words[i].contains(word)) {
res.add(word);
break;
}
}
}
return res;
}
}
JS
/**
* @param {string[]} words
* @return {string[]}
*/
var stringMatching = function(words) {
const ret = [];
for (let i = 0; i < words.length; i++) {
for (let j = 0; j < words.length; j++) {
if (i !== j && words[j].search(words[i]) !== -1) {
ret.push(words[i]);
break;
}
}
}
return ret;
};
title: 2022-08-07-636-函数的独占时间
date: 2022-08-07 16:51:12
tags: [Daily Practice, 模拟, 中等题]
categories: 算法题
636. 函数的独占时间
JAVA
class Solution {
public int[] exclusiveTime(int n, List<String> logs) {
Stack<int[]> stack = new Stack<>();
int[] res = new int[n];
for (String log : logs) {
String[] split = log.split(":");
int id = Integer.parseInt(split[0]);
int time = Integer.parseInt(split[2]);
if ("start".equals(split[1])) {
stack.push(new int[]{id, time});
} else {
int[] pop = stack.pop();
int interval = time - pop[1] + 1;
res[pop[0]] += interval;
if (!stack.isEmpty()) {
res[stack.peek()[0]] -= interval;
}
}
}
return res;
}
}
JS
var exclusiveTime = function(n, logs) {
const stack = []; // {idx, 开始运行的时间}
const res = new Array(n).fill(0);
for (const log of logs) {
const idx = parseInt(log.substring(0, log.indexOf(':')));
const type = log.substring(log.indexOf(':') + 1, log.lastIndexOf(':'));
const timestamp = parseInt(log.substring(log.lastIndexOf(':') + 1));
if ("start" === type) {
if (stack.length) {
res[stack[stack.length - 1][0]] += timestamp - stack[stack.length - 1][1];
stack[stack.length - 1][1] = timestamp;
}
stack.push([idx, timestamp]);
} else {
const t = stack.pop();
res[t[0]] += timestamp - t[1] + 1;
if (stack.length) {
stack[stack.length - 1][1] = timestamp + 1;
}
}
}
return res;
};
title: 2022-08-08-761-特殊的二进制序列
date: 2022-08-08 15:58:12
tags: [Daily Practice, 字符串, 困难题]
categories: 算法题
761. 特殊的二进制序列
JS
/**
* @param {string} s
* @return {string}
*/
var makeLargestSpecial = function(s) {
if (s.length <= 2) {
return s;
}
let cnt = 0, left = 0;
const subs = [];
for (let i = 0; i < s.length; ++i) {
if (s[i] === '1') {
++cnt;
} else {
--cnt;
if (cnt === 0) {
subs.push("1" + makeLargestSpecial(s.substring(left + 1, i)) + '0');
left = i + 1;
}
}
}
subs.sort().reverse();
return subs.join('');
};
title: 2022-08-09-1413-逐步求和得到正数的最小值
date: 2022-08-09 15:58:12
tags: [Daily Practice, 前缀和, 简单题]
categories: 算法题
1413. 逐步求和得到正数的最小值
JAVA
class Solution {
public int minStartValue(int[] nums) {
int sum = 1, min = 1;
for (int num: nums) {
sum += (num);
if (sum < 1) {
min += 1 - sum;
sum = 1;
}
}
return min;
}
}
JS
/**
* @param {number[]} nums
* @return {number}
*/
var minStartValue = function(nums) {
let sum = 1, min = 1;
for (let num of nums) {
sum += (num);
if (sum < 1) {
min += 1 - sum;
sum = 1;
}
}
return min;
};
title: 2022-08-10-1413-求解方程
date: 2022-08-10 15:58:12
tags: [Daily Practice, 模拟, 中等题]
categories: 算法题
640. 求解方程
JAVA
class Solution {
public String solveEquation(String equation) {
// 把x移到左边,把其他移到右边
String[] arr1 = equation.split("=");
int left = 0;
int right = 0;
String[] arr2 = arr1[0].replace("-", "+-").split("\\+");
String[] arr3 = arr1[1].replace("-", "+-").split("\\+");
// 等式左边的处理
for (String s : arr2) {
if (s.equals("x")) {
left += 1;
} else if (s.equals("-x")) {
left += -1;
} else if (s.contains("x")) {
left += Integer.parseInt(s.substring(0, s.length() - 1));
} else if (!s.equals("")) {
right -= Integer.parseInt(s);
}
}
// 等式右边的处理
for (String s : arr3) {
if (s.equals("x")) {
left -= 1;
} else if (s.equals("-x")) {
left -= -1;
} else if (s.contains("x")) {
left -= Integer.parseInt(s.substring(0, s.length() - 1));
} else if (!s.equals("")) {
right += Integer.parseInt(s);
}
}
if (left == 0) {
if (right == 0) {
return "Infinite solutions";
} else {
return "No solution";
}
} else {
return "x=" + right/left;
}
}
}
JS
var solveEquation = function(equation) {
let factor = 0, val = 0;
let index = 0, n = equation.length, sign1 = 1; // 等式左边默认系数为正
while (index < n) {
if (equation[index] === '=') {
sign1 = -1; // 等式右边默认系数为负
index++;
continue;
}
let sign2 = sign1, number = 0;
let valid = false; // 记录 number 是否有效
if (equation[index] === '-' || equation[index] === '+') { // 去掉前面的符号
sign2 = (equation[index] === '-') ? -sign1 : sign1;
index++;
}
while (index < n && isDigit(equation[index])) {
number = number * 10 + (equation[index].charCodeAt() - '0'.charCodeAt());
index++;
valid = true;
}
if (index < n && equation[index] === 'x') { // 变量
factor += valid ? sign2 * number : sign2;
index++;
} else { // 数值
val += sign2 * number;
}
}
if (factor === 0) {
return val === 0 ? "Infinite solutions" : "No solution";
}
return "x=" + (-val / factor);
};
const isDigit = (ch) => {
return parseFloat(ch).toString() === "NaN" ? false : true;
}