Skip to content

Commit 62e8521

Browse files
author
shrinathjoshi
committed
Updated solution to day 6 of June Leetcode Challenge
1 parent 0f030e7 commit 62e8521

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.leetcode.JuneChallenge.week1;
2+
3+
import java.util.Arrays;
4+
5+
public class QueueReconstructionByHeight {
6+
7+
public int[][] reconstructQueue(int[][] people) {
8+
int n = people.length;
9+
int result[][] = new int[n][2];
10+
11+
for (int i = 0; i < n; i++) {
12+
result[i][0] = -1;
13+
result[i][1] = -1;
14+
}
15+
16+
Arrays.sort(people, (a, b) -> (a[0] != b[0]) ? a[0] - b[0] : a[1] - b[1]);
17+
18+
for (int i = 0; i < n; i++) {
19+
int count = people[i][1];
20+
for (int j = 0; j < n; j++) {
21+
if (count == 0 && result[j][0] == -1) {
22+
result[j][0] = people[i][0];
23+
result[j][1] = people[i][1];
24+
break;
25+
} else if (result[j][0] >= people[i][0] || result[j][0] == -1) {
26+
count--;
27+
}
28+
}
29+
}
30+
31+
return result;
32+
}
33+
34+
public static void main(String[] args) {
35+
36+
int people[][] = { { 7, 0 }, { 4, 4 }, { 7, 1 }, { 5, 0 }, { 6, 1 }, { 5, 2 } };
37+
38+
int result[][] = new QueueReconstructionByHeight().reconstructQueue(people);
39+
40+
for (int i = 0; i < result.length; i++) {
41+
System.out.print("[" + result[i][0] + "," + result[i][1] + "]");
42+
System.out.println(" ");
43+
}
44+
45+
// {{5,0},{6,1},{5,2},{7,0},{4,4},{7,1}}
46+
}
47+
48+
}

0 commit comments

Comments
 (0)