Skip to content

Commit c6a59ac

Browse files
author
Mia Xia
committed
671 - 675
1 parent ff5c048 commit c6a59ac

5 files changed

+368
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly two or zero sub-node. If the node has two sub-nodes, then this node's value is the smaller value among its two sub-nodes. More formally, the property root.val = min(root.left.val, root.right.val) always holds.
2+
// Given such a binary tree, you need to output the second minimum value in the set made of all the nodes' value in the whole tree.
3+
// If no such second minimum value exists, output -1 instead.
4+
5+
// Example 1:
6+
// Input:
7+
// 2
8+
// / \
9+
// 2 5
10+
// / \
11+
// 5 7
12+
// Output: 5
13+
// Explanation: The smallest value is 2, the second smallest value is 5.
14+
15+
// Example 2:
16+
// Input:
17+
// 2
18+
// / \
19+
// 2 2
20+
// Output: -1
21+
// Explanation: The smallest value is 2, but there isn't any second smallest value.
22+
23+
/**
24+
* Definition for a binary tree node.
25+
* function TreeNode(val) {
26+
* this.val = val;
27+
* this.left = this.right = null;
28+
* }
29+
*/
30+
/**
31+
* @param {TreeNode} root
32+
* @return {number}
33+
*/
34+
var findSecondMinimumValue = (root) => {
35+
let valSet = new Set();
36+
const helper = node => {
37+
if (!valSet.has(node.val)) valSet.add(node.val);
38+
if (node.left) helper(node.left);
39+
if (node.right) helper(node.right);
40+
};
41+
helper(root);
42+
valSet = Array.from(valSet).sort((a, b) => a - b).sort();
43+
if (valSet.length > 1) return valSet[1];
44+
else return -1;
45+
};

672. Bulb Switcher II.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// There is a room with n lights which are turned on initially and 4 buttons on the wall. After performing exactly m unknown operations towards buttons, you need to return how many different kinds of status of the n lights could be.
2+
// Suppose n lights are labeled as number [1, 2, 3 ..., n], function of these 4 buttons are given below:
3+
// Flip all the lights.
4+
// Flip lights with even numbers.
5+
// Flip lights with odd numbers.
6+
// Flip lights with (3k + 1) numbers, k = 0, 1, 2, ...
7+
8+
// Example 1:
9+
// Input: n = 1, m = 1.
10+
// Output: 2
11+
// Explanation: Status can be: [on], [off]
12+
13+
// Example 2:
14+
// Input: n = 2, m = 1.
15+
// Output: 3
16+
// Explanation: Status can be: [on, off], [off, on], [off, off]
17+
18+
// Example 3:
19+
// Input: n = 3, m = 1.
20+
// Output: 4
21+
// Explanation: Status can be: [off, on, off], [on, off, on], [off, off, off], [off, on, on].
22+
23+
// Note: n and m both fit in range [0, 1000].
24+
25+
/**
26+
* @param {number} n
27+
* @param {number} m
28+
* @return {number}
29+
*/
30+
var flipAll = (lights) => {
31+
var newLights = lights.slice();
32+
for (let i = 0; i < newLights.length; i++) {
33+
if (newLights[i] === 1) newLights[i] = 0;
34+
else newLights[i] = 1;
35+
}
36+
return newLights;
37+
};
38+
39+
var flipEven = (lights) => {
40+
var newLights = lights.slice();
41+
for (let i = 0; i < newLights.length; i++) {
42+
if ((i + 1) % 2 === 0) {
43+
if (newLights[i] === 1) newLights[i] = 0;
44+
else newLights[i] = 1;
45+
}
46+
}
47+
return newLights;
48+
};
49+
50+
var flipOdd = (lights) =>{
51+
var newLights = lights.slice();
52+
for (let i = 0; i < newLights.length; i++) {
53+
if ((i + 1) % 2 !== 0) {
54+
if (newLights[i] === 1) newLights[i] = 0;
55+
else newLights[i] = 1;
56+
}
57+
}
58+
return newLights;
59+
};
60+
61+
var flipNumber = (lights) => {
62+
var newLights = lights.slice();
63+
for (let i = 0; i < newLights.length; i++) {
64+
if ((i + 1) % 3 === 1) {
65+
if (newLights[i] === 1) newLights[i] = 0;
66+
else newLights[i] = 1;
67+
}
68+
}
69+
return newLights;
70+
};
71+
72+
var deleteDuplicatedCase = (cases) => {
73+
var newCases = cases.slice();
74+
for (let i = 0; i < newCases.length; i++) {
75+
for (let j = i + 1; j < newCases.length; j++) {
76+
let isSame = true;
77+
for (let index = 0; index < newCases[i].length; index++) {
78+
if (newCases[i][index] !== newCases[j][index]) {
79+
isSame = false;
80+
}
81+
}
82+
if (isSame) {
83+
newCases.splice(j, 1);
84+
j--;
85+
}
86+
}
87+
}
88+
return newCases;
89+
};
90+
91+
var flipLights = (n, m) => {
92+
var lights = []
93+
var cases = []
94+
if (m === 0) return 1;
95+
for (let i = 0; i < n; i++) lights.push(1);
96+
for (let i = 0; i < m; i++) {
97+
const casesLength = cases.length
98+
if (i === 0) {
99+
cases.push(flipAll(lights));
100+
cases.push(flipEven(lights));
101+
cases.push(flipOdd(lights));
102+
cases.push(flipNumber(lights));
103+
} else {
104+
for (let j = 0; j < casesLength; j++) {
105+
cases.push(flipAll(cases[j]));
106+
cases.push(flipEven(cases[j]));
107+
cases.push(flipOdd(cases[j]));
108+
cases.push(flipNumber(cases[j]));
109+
}
110+
}
111+
cases.splice(0, casesLength)
112+
cases = deleteDuplicatedCase(cases).slice();
113+
}
114+
return cases.length;
115+
};
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Given an unsorted array of integers, find the number of longest increasing subsequence.
2+
3+
// Example 1:
4+
// Input: [1,3,5,4,7]
5+
// Output: 2
6+
// Explanation: The two longest increasing subsequence are [1, 3, 4, 7] and [1, 3, 5, 7].
7+
8+
// Example 2:
9+
// Input: [2,2,2,2,2]
10+
// Output: 5
11+
// Explanation: The length of longest continuous increasing subsequence is 1, and there are 5 subsequences' length is 1, so output 5.
12+
// Note: Length of the given array will be not exceed 2000 and the answer is guaranteed to be fit in 32-bit signed int.
13+
14+
/**
15+
* @param {number[]} nums
16+
* @return {number}
17+
*/
18+
var findNumberOfLIS = (nums) => {
19+
if (!nums || nums.length === 0) return 0;
20+
if (nums.length === 1) return 1;
21+
22+
const N = nums.length;
23+
const len = new Array(N);
24+
const cnt = new Array(N);
25+
let maxLen = 0, ans = 0;
26+
27+
for (let i = 0; i < N; i++){
28+
len[i] = 1;
29+
cnt[i] = 1;
30+
for (let j = 0; j < i; j++){
31+
if (nums[j] < nums[i]){
32+
if (len[j] + 1 > len[i]){
33+
len[i] = len[j] + 1;
34+
cnt[i] = cnt[j];
35+
} else if (len[j] + 1 === len[i]){
36+
cnt[i] += cnt[j];
37+
}
38+
}
39+
}
40+
if (len[i] === maxLen){
41+
ans += cnt[i];
42+
} else if (len[i] > maxLen){
43+
maxLen = len[i];
44+
ans = cnt[i];
45+
}
46+
}
47+
return ans;
48+
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Given an unsorted array of integers, find the length of longest continuous increasing subsequence (subarray).
2+
3+
// Example 1:
4+
// Input: [1,3,5,4,7]
5+
// Output: 3
6+
// Explanation: The longest continuous increasing subsequence is [1,3,5], its length is 3.
7+
// Even though [1,3,5,7] is also an increasing subsequence, it's not a continuous one where 5 and 7 are separated by 4.
8+
9+
// Example 2:
10+
// Input: [2,2,2,2,2]
11+
// Output: 1
12+
// Explanation: The longest continuous increasing subsequence is [2], its length is 1.
13+
// Note: Length of the array will not exceed 10,000.
14+
15+
/**
16+
* @param {number[]} nums
17+
* @return {number}
18+
*/
19+
var findLengthOfLCIS = (nums) => {
20+
let left = 0, right = 1, limit = nums.length - 1, max = 0, result = [];
21+
if (limit < 1) return nums.length;
22+
23+
while (left < right && right <= limit) {
24+
if (nums[left] < nums[right]) {
25+
let active = true;
26+
max++
27+
} else {
28+
result.push(max);
29+
max = 0;
30+
}
31+
left++;
32+
right++;
33+
}
34+
result.push(max);
35+
return Math.max(...result) + 1
36+
}

675. Cut Off Trees for Golf Event.js

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
// You are asked to cut off trees in a forest for a golf event. The forest is represented as a non-negative 2D map, in this map:
2+
// 0 represents the obstacle can't be reached.
3+
// 1 represents the ground can be walked through.
4+
// The place with number bigger than 1 represents a tree can be walked through, and this positive number represents the tree's height.
5+
// You are asked to cut off all the trees in this forest in the order of tree's height - always cut off the tree with lowest height first. And after cutting, the original place has the tree will become a grass (value 1).
6+
// You will start from the point (0, 0) and you should output the minimum steps you need to walk to cut off all the trees. If you can't cut off all the trees, output -1 in that situation.
7+
// You are guaranteed that no two trees have the same height and there is at least one tree needs to be cut off.
8+
9+
// Example 1:
10+
// Input:
11+
// [
12+
// [1,2,3],
13+
// [0,0,4],
14+
// [7,6,5]
15+
// ]
16+
// Output: 6
17+
18+
// Example 2:
19+
// Input:
20+
// [
21+
// [1,2,3],
22+
// [0,0,0],
23+
// [7,6,5]
24+
// ]
25+
// Output: -1
26+
27+
// Example 3:
28+
// Input:
29+
// [
30+
// [2,3,4],
31+
// [0,0,5],
32+
// [8,7,6]
33+
// ]
34+
// Output: 6
35+
// Explanation: You started from the point (0,0) and you can cut off the tree in (0,0) directly without walking.
36+
37+
// Hint: size of the given matrix will not exceed 50x50.
38+
39+
/**
40+
* @param {number[][]} forest
41+
* @return {number}
42+
*/
43+
var Tree = function(height, row, col) {
44+
return {
45+
height,
46+
location: new Location(row, col)
47+
};
48+
}
49+
50+
var Location = function(row, col) {
51+
return {
52+
row,
53+
col
54+
}
55+
}
56+
var ListNode = function(val) {
57+
return {
58+
val,
59+
next: null
60+
}
61+
}
62+
63+
var cutOffTree = (forest) => {
64+
const height = forest.length;
65+
const width = forest[0].length;
66+
const trees = [];
67+
68+
for (let row = 0; row < height; row++) {
69+
for (let col = 0; col < width; col++) {
70+
if (forest[row][col] > 1) {
71+
trees.push(new Tree(forest[row][col], row, col));
72+
}
73+
}
74+
}
75+
trees.sort((a, b) => b.height - a.height);
76+
77+
let location = new Location(0, 0);
78+
let distance = 0;
79+
80+
while (trees.length) {
81+
let tree = trees.pop();
82+
let travelled = findTree(forest, tree, location);
83+
if (travelled >= 0) {
84+
distance += travelled;
85+
location = tree.location;
86+
} else {
87+
return -1;
88+
}
89+
}
90+
return distance;
91+
};
92+
93+
var findTree = (forest, tree, location) => {
94+
const visited = {};
95+
const height = forest.length;
96+
const width = forest[0].length;
97+
98+
let head = new ListNode([location, 0]);
99+
let tail = head;
100+
101+
while (head) {
102+
let [current, distance] = head.val;
103+
if (current.row < 0 || current.col < 0 || current.row >= height || current.col >= width) {
104+
} else if (forest[current.row][current.col] == 0) {
105+
} else if (visited[`${current.row}.${current.col}`]) {
106+
} else {
107+
if (tree.location.row == current.row && tree.location.col == current.col) {
108+
return distance;
109+
} else {
110+
tail.next = new ListNode([new Location(current.row - 1, current.col), distance + 1]);
111+
tail = tail.next;
112+
tail.next = new ListNode([new Location(current.row, current.col + 1), distance + 1]);
113+
tail = tail.next;
114+
tail.next = new ListNode([new Location(current.row + 1, current.col), distance + 1]);
115+
tail = tail.next;
116+
tail.next = new ListNode([new Location(current.row, current.col - 1), distance + 1]);
117+
tail = tail.next;
118+
}
119+
visited[`${current.row}.${current.col}`] = true;
120+
}
121+
head = head.next;
122+
}
123+
return -1;
124+
}

0 commit comments

Comments
 (0)