Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 27 additions & 19 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions src/main/java/datastructures/Pair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package datastructures;

public class Pair {
private Integer index;
private Integer person;

public Pair(Integer index, Integer person) {
this.index = index;
this.person = person;
}

public Integer getIndex() {
return index;
}

public Integer getPerson() {
return person;
}
}
38 changes: 38 additions & 0 deletions src/main/java/problemset/a2251/FullBloom.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package problemset.a2251;

import java.util.*;

public class FullBloom {

public int[] fullBloomFlowers(int[][] flowers, int[] people) {
int[] res = new int[people.length];
int count = 0;
PriorityQueue<Integer> start = new PriorityQueue<>();
PriorityQueue<Integer> end = new PriorityQueue<>();
for (int[] flower : flowers) {
start.add(flower[0]);
end.add(flower[1]);
}

TreeMap<Integer, Integer> map = new TreeMap<>();
for (int i = 0; i < people.length; i++) {
map.put(i, people[i]);
}

for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
int p = entry.getValue();
int i = entry.getKey();
while (!start.isEmpty() && start.peek() <= p) {
start.poll();
count++;
}
while (!end.isEmpty() && end.peek() < p) {
end.poll();
count--;
}

res[i] = count;
}
return res;
}
}
41 changes: 41 additions & 0 deletions src/test/java/problemset/a2251/FullBloomTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package problemset.a2251;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class FullBloomTest {

private FullBloom fullBloom;

private int[][] testcaseOne;
private int[] peopleOne;
private int[][] testcaseTwo;
private int[] peopleTwo;

@BeforeEach
void setUp() {
fullBloom = new FullBloom();

testcaseOne = new int[][]{{1, 6}, {3, 7}, {9, 12}, {4, 13}};
peopleOne = new int[]{2, 3, 7, 11};

testcaseTwo = new int[][]{{1, 10}, {3, 3}};
peopleTwo = new int[]{3, 3, 2};
}

@Test
void test_fullBloomFlowers() {
int[] expectedResult = new int[]{1, 2, 2, 2};
int[] actualResult = fullBloom.fullBloomFlowers(testcaseOne, peopleOne);
assertArrayEquals(expectedResult, actualResult);
}

@Test
void test_fullBloomFlowers_v2() {
int[] expectedResult = new int[]{2, 2, 1};
int[] actualResult = fullBloom.fullBloomFlowers(testcaseTwo, peopleTwo);
assertArrayEquals(expectedResult, actualResult);
}
}