Skip to content

Commit 697612e

Browse files
add 1051
1 parent 55488a3 commit 697612e

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Your ideas/fixes/algorithms are more than welcome!
2727

2828
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2929
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
30+
|1051|[Height Checker](https://leetcode.com/problems/height-checker/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1051.java) | O(nlogn) | O(1) | |Easy||
3031
|1047|[Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1047.java) | O(n) | O(1) | |Easy||
3132
|1037|[Valid Boomerang](https://leetcode.com/problems/valid-boomerang/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1037.java) | O(1) | O(1) | |Easy|Math|
3233
|1033|[Moving Stones Until Consecutive](https://leetcode.com/problems/moving-stones-until-consecutive/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1033.java) | O(1) | O(1) | |Easy|Math|
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* 1051. Height Checker
7+
*
8+
* Students are asked to stand in non-decreasing order of heights for an annual photo.
9+
* Return the minimum number of students not standing in the right positions.
10+
* (This is the number of students that must move in order for all students to be standing in non-decreasing order of height.)
11+
*
12+
* Example 1:
13+
* Input: [1,1,4,2,1,3]
14+
* Output: 3
15+
* Explanation:
16+
* Students with heights 4, 3 and the last 1 are not standing in the right positions.
17+
*
18+
* Note:
19+
*
20+
* 1 <= heights.length <= 100
21+
* 1 <= heights[i] <= 100
22+
* */
23+
public class _1051 {
24+
public static class Solution1 {
25+
public int heightChecker(int[] heights) {
26+
int[] originals = Arrays.copyOf(heights, heights.length);
27+
Arrays.sort(heights);
28+
int count = 0;
29+
for (int i = 0; i < originals.length; i++) {
30+
if (heights[i] != originals[i]) {
31+
count++;
32+
}
33+
}
34+
return count;
35+
}
36+
}
37+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1051;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1051Test {
10+
private static _1051.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1051.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(3, solution1.heightChecker(new int[]{1, 1, 4, 2, 1, 3}));
20+
}
21+
22+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy