Skip to content

Commit a07bab4

Browse files
authored
Added tasks 1049, 1051.
1 parent b6e56ed commit a07bab4

File tree

6 files changed

+191
-0
lines changed

6 files changed

+191
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g1001_1100.s1049_last_stone_weight_ii;
2+
3+
// #Medium #Array #Dynamic_Programming #2022_02_28_Time_2_ms_(95.98%)_Space_41.8_MB_(31.51%)
4+
5+
public class Solution {
6+
public int lastStoneWeightII(int[] stones) {
7+
// dp[i][j] i is the index of stones, j is the current weight
8+
// goal is to find max closest to half and use it to get the diff
9+
// 0-1 knapsack problem
10+
int sum = 0;
11+
for (int stone : stones) {
12+
sum += stone;
13+
}
14+
int half = sum / 2;
15+
int[] dp = new int[half + 1];
16+
for (int cur : stones) {
17+
for (int j = half; j >= cur; j--) {
18+
dp[j] = Math.max(dp[j], dp[j - cur] + cur);
19+
}
20+
}
21+
return sum - dp[half] * 2;
22+
}
23+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
1049\. Last Stone Weight II
2+
3+
Medium
4+
5+
You are given an array of integers `stones` where `stones[i]` is the weight of the <code>i<sup>th</sup></code> stone.
6+
7+
We are playing a game with the stones. On each turn, we choose any two stones and smash them together. Suppose the stones have weights `x` and `y` with `x <= y`. The result of this smash is:
8+
9+
* If `x == y`, both stones are destroyed, and
10+
* If `x != y`, the stone of weight `x` is destroyed, and the stone of weight `y` has new weight `y - x`.
11+
12+
At the end of the game, there is **at most one** stone left.
13+
14+
Return _the smallest possible weight of the left stone_. If there are no stones left, return `0`.
15+
16+
**Example 1:**
17+
18+
**Input:** stones = [2,7,4,1,8,1]
19+
20+
**Output:** 1
21+
22+
**Explanation:**
23+
24+
We can combine 2 and 4 to get 2, so the array converts to [2,7,1,8,1] then,
25+
26+
we can combine 7 and 8 to get 1, so the array converts to [2,1,1,1] then,
27+
28+
we can combine 2 and 1 to get 1, so the array converts to [1,1,1] then,
29+
30+
we can combine 1 and 1 to get 0, so the array converts to [1], then that's the optimal value.
31+
32+
**Example 2:**
33+
34+
**Input:** stones = [31,26,33,21,40]
35+
36+
**Output:** 5
37+
38+
**Constraints:**
39+
40+
* `1 <= stones.length <= 30`
41+
* `1 <= stones[i] <= 100`
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g1001_1100.s1051_height_checker;
2+
3+
// #Easy #Array #Sorting #Counting_Sort #2022_02_28_Time_1_ms_(94.01%)_Space_42.6_MB_(5.71%)
4+
5+
public class Solution {
6+
public int heightChecker(int[] heights) {
7+
int heightDiff = 0;
8+
int[] count = new int[101];
9+
int[] actualLine = new int[heights.length];
10+
for (int height : heights) {
11+
count[height]++;
12+
}
13+
int heightLength = 0;
14+
for (int i = 0; i < count.length; i++) {
15+
if (count[i] > 0) {
16+
for (int j = 0; j < count[i]; j++) {
17+
actualLine[heightLength] = i;
18+
heightLength++;
19+
}
20+
count[i] = 0;
21+
}
22+
}
23+
for (int i = 0; i < heights.length; i++) {
24+
if (actualLine[i] != heights[i]) {
25+
heightDiff++;
26+
}
27+
}
28+
return heightDiff;
29+
}
30+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
1051\. Height Checker
2+
3+
Easy
4+
5+
A school is trying to take an annual photo of all the students. The students are asked to stand in a single file line in **non-decreasing order** by height. Let this ordering be represented by the integer array `expected` where `expected[i]` is the expected height of the <code>i<sup>th</sup></code> student in line.
6+
7+
You are given an integer array `heights` representing the **current order** that the students are standing in. Each `heights[i]` is the height of the <code>i<sup>th</sup></code> student in line (**0-indexed**).
8+
9+
Return _the **number of indices** where_ `heights[i] != expected[i]`.
10+
11+
**Example 1:**
12+
13+
**Input:** heights = [1,1,4,2,1,3]
14+
15+
**Output:** 3
16+
17+
**Explanation:**
18+
19+
heights: [1,1,4,2,1,3]
20+
21+
expected: [1,1,1,2,3,4]
22+
23+
Indices 2, 4, and 5 do not match.
24+
25+
**Example 2:**
26+
27+
**Input:** heights = [5,1,2,3,4]
28+
29+
**Output:** 5
30+
31+
**Explanation:**
32+
33+
heights: [5,1,2,3,4]
34+
35+
expected: [1,2,3,4,5]
36+
37+
All indices do not match.
38+
39+
**Example 3:**
40+
41+
**Input:** heights = [1,2,3,4,5]
42+
43+
**Output:** 0
44+
45+
**Explanation:**
46+
47+
heights: [1,2,3,4,5]
48+
49+
expected: [1,2,3,4,5]
50+
51+
All indices match.
52+
53+
**Constraints:**
54+
55+
* `1 <= heights.length <= 100`
56+
* `1 <= heights[i] <= 100`
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g1001_1100.s1049_last_stone_weight_ii;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void lastStoneWeightII() {
11+
assertThat(new Solution().lastStoneWeightII(new int[] {2, 7, 4, 1, 8, 1}), equalTo(1));
12+
}
13+
14+
@Test
15+
void lastStoneWeightII2() {
16+
assertThat(new Solution().lastStoneWeightII(new int[] {31, 26, 33, 21, 40}), equalTo(5));
17+
}
18+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g1001_1100.s1051_height_checker;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void heightChecker() {
11+
assertThat(new Solution().heightChecker(new int[] {1, 1, 4, 2, 1, 3}), equalTo(3));
12+
}
13+
14+
@Test
15+
void heightChecker2() {
16+
assertThat(new Solution().heightChecker(new int[] {5, 1, 2, 3, 4}), equalTo(5));
17+
}
18+
19+
@Test
20+
void heightChecker3() {
21+
assertThat(new Solution().heightChecker(new int[] {1, 2, 3, 4, 5}), equalTo(0));
22+
}
23+
}

0 commit comments

Comments
 (0)