Skip to content

Commit dc0b538

Browse files
committed
Added junit + paint house 2
1 parent 775fed8 commit dc0b538

File tree

5 files changed

+398
-2
lines changed

5 files changed

+398
-2
lines changed

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@
5454
<maven.compiler.target>${java.version}</maven.compiler.target>
5555
</properties>
5656

57+
<dependencies>
58+
<dependency>
59+
<groupId>junit</groupId>
60+
<artifactId>junit</artifactId>
61+
<version>4.12</version>
62+
</dependency>
63+
</dependencies>
64+
5765
<profiles>
5866
<profile>
5967
<id>release</id>
Lines changed: 219 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,226 @@
11
package com.hackerrank.java.advanced;
22

3+
import java.util.*;
4+
35
/**
6+
* Level: Medium
7+
* Problem Link: https://www.hackerrank.com/challenges/java-vistor-pattern/
8+
*
49
* @author rampatra
510
* @since 2019-06-22
611
*/
7-
public class JavaVisitorPattern {
12+
enum Color {
13+
RED, GREEN
14+
}
15+
16+
abstract class Tree {
17+
18+
private int value;
19+
private Color color;
20+
private int depth;
21+
22+
public Tree(int value, Color color, int depth) {
23+
this.value = value;
24+
this.color = color;
25+
this.depth = depth;
26+
}
27+
28+
public int getValue() {
29+
return value;
30+
}
31+
32+
public Color getColor() {
33+
return color;
34+
}
35+
36+
public int getDepth() {
37+
return depth;
38+
}
39+
40+
public abstract void accept(TreeVis visitor);
41+
42+
43+
}
44+
45+
class TreeNode extends Tree {
46+
47+
private ArrayList<Tree> children = new ArrayList<>();
48+
49+
public TreeNode(int value, Color color, int depth) {
50+
super(value, color, depth);
51+
}
52+
53+
public void accept(TreeVis visitor) {
54+
visitor.visitNode(this);
55+
56+
for (Tree child : children) {
57+
child.accept(visitor);
58+
}
59+
}
60+
61+
public void addChild(Tree child) {
62+
children.add(child);
63+
}
64+
}
65+
66+
class TreeLeaf extends Tree {
67+
68+
public TreeLeaf(int value, Color color, int depth) {
69+
super(value, color, depth);
70+
}
71+
72+
public void accept(TreeVis visitor) {
73+
visitor.visitLeaf(this);
74+
}
75+
}
76+
77+
abstract class TreeVis {
78+
public abstract int getResult();
79+
80+
public abstract void visitNode(TreeNode node);
81+
82+
public abstract void visitLeaf(TreeLeaf leaf);
83+
84+
}
85+
86+
class SumInLeavesVisitor extends TreeVis {
87+
int nodeSum = 0;
88+
int leafSum = 0;
89+
90+
public int getResult() {
91+
//implement this
92+
return leafSum;
93+
}
94+
95+
public void visitNode(TreeNode node) {
96+
//implement this
97+
//nodeSum += node.getValue();
98+
}
99+
100+
public void visitLeaf(TreeLeaf leaf) {
101+
//implement this
102+
leafSum += leaf.getValue();
103+
}
104+
}
105+
106+
class ProductOfRedNodesVisitor extends TreeVis {
107+
int prodOfRedNodesAndLeaves = 1;
108+
private final int M = 1000000007;
109+
110+
public int getResult() {
111+
//implement this
112+
return prodOfRedNodesAndLeaves;
113+
}
114+
115+
public void visitNode(TreeNode node) {
116+
//implement this
117+
if (node.getColor() == Color.RED) {
118+
prodOfRedNodesAndLeaves *= (node.getValue() % M);
119+
}
120+
}
121+
122+
public void visitLeaf(TreeLeaf leaf) {
123+
//implement this
124+
if (leaf.getColor() == Color.RED) {
125+
prodOfRedNodesAndLeaves *= (leaf.getValue() % M);
126+
}
127+
}
128+
}
129+
130+
class FancyVisitor extends TreeVis {
131+
int sumOfNodesAtEvenDepth = 0;
132+
int sumOfGreenLeaves = 0;
133+
134+
public int getResult() {
135+
//implement this
136+
return Math.abs(sumOfNodesAtEvenDepth - sumOfGreenLeaves);
137+
}
138+
139+
public void visitNode(TreeNode node) {
140+
//implement this
141+
if (node.getDepth() % 2 == 0) {
142+
sumOfNodesAtEvenDepth += node.getValue();
143+
}
144+
}
145+
146+
public void visitLeaf(TreeLeaf leaf) {
147+
//implement this
148+
if (leaf.getColor() == Color.GREEN) {
149+
sumOfGreenLeaves += leaf.getValue();
150+
}
151+
}
8152
}
153+
154+
public class JavaVisitorPattern {
155+
156+
public static Tree solve() {
157+
//read the tree from STDIN and return its root as a return value of this function
158+
Scanner s = new Scanner(System.in);
159+
160+
int numOfNodes = s.nextInt();
161+
int[] nodeValues = new int[numOfNodes];
162+
int[] nodeColors = new int[numOfNodes];
163+
Map<Integer, Set<Integer>> parentToChildMap = new HashMap<>();
164+
Map<Integer, Integer> childToParentMap = new HashMap<>();
165+
166+
for (int i = 0; i < numOfNodes; i++) {
167+
nodeValues[i] = s.nextInt();
168+
}
169+
for (int i = 0; i < numOfNodes; i++) {
170+
nodeColors[i] = s.nextInt();
171+
}
172+
for (int i = 0; i < numOfNodes - 1; i++) {
173+
int parentIndex = s.nextInt();
174+
int childIndex = s.nextInt();
175+
176+
Set<Integer> children = parentToChildMap.get(parentIndex - 1) != null ? parentToChildMap.get(parentIndex - 1) : new HashSet<>();
177+
children.add(childIndex - 1);
178+
parentToChildMap.put(parentIndex - 1, children);
179+
childToParentMap.put(childIndex - 1, parentIndex - 1);
180+
}
181+
182+
List<Tree> nodes = new ArrayList<>(numOfNodes);
183+
for (int i = 0; i < numOfNodes; i++) {
184+
185+
int depth = childToParentMap.get(i) == null ? -1 : nodes.get(childToParentMap.get(i)).getDepth();
186+
187+
if (parentToChildMap.get(i) != null) {
188+
nodes.add(new TreeNode(nodeValues[i], nodeColors[i] == 0 ? Color.RED : Color.GREEN, depth + 1));
189+
} else {
190+
nodes.add(new TreeLeaf(nodeValues[i], nodeColors[i] == 0 ? Color.RED : Color.GREEN, depth + 1));
191+
}
192+
}
193+
194+
195+
for (Map.Entry<Integer, Set<Integer>> entry : parentToChildMap.entrySet()) {
196+
197+
TreeNode parent = (TreeNode) nodes.get(entry.getKey());
198+
199+
for (Integer childIndex : entry.getValue()) {
200+
parent.addChild(nodes.get(childIndex));
201+
}
202+
}
203+
204+
return nodes.get(0);
205+
}
206+
207+
208+
public static void main(String[] args) {
209+
Tree root = solve();
210+
SumInLeavesVisitor vis1 = new SumInLeavesVisitor();
211+
ProductOfRedNodesVisitor vis2 = new ProductOfRedNodesVisitor();
212+
FancyVisitor vis3 = new FancyVisitor();
213+
214+
root.accept(vis1);
215+
root.accept(vis2);
216+
root.accept(vis3);
217+
218+
int res1 = vis1.getResult();
219+
int res2 = vis2.getResult();
220+
int res3 = vis3.getResult();
221+
222+
System.out.println(res1);
223+
System.out.println(res2);
224+
System.out.println(res3);
225+
}
226+
}

src/main/java/com/leetcode/arrays/InsertInterval.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* Output: [[1,2],[3,10],[12,16]]
2020
* Explanation: Because the new interval [4,8] overlaps with [3,5],[6,7],[8,10].
2121
* <p>
22-
* Companies: LinkedIn
22+
* Companies: LinkedIn.
2323
* Related: {@link MergeIntervals}.
2424
*
2525
* @author rampatra
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.leetcode.dynamicprogramming;
2+
3+
/**
4+
* Level: Easy
5+
* Problem Link: https://leetcode.com/problems/paint-house/ (premium)
6+
* Problem Description:
7+
* There are a row of n houses, each house can be painted with one of the three colors: red, blue or green. The cost
8+
* of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent
9+
* houses have the same color. The cost of painting each house with a certain color is represented by a n x 3 cost matrix.
10+
* <p>
11+
* For example, costs[0][0] is the cost of painting house 0 with color red; costs[1][2] is the cost of painting
12+
* house 1 with color green, and so on... Find the minimum cost to paint all houses.
13+
* <p>
14+
* Companies: LinkedIn.
15+
* Related: {@link PaintHouseII}.
16+
*
17+
* @author rampatra
18+
* @since 2019-07-23
19+
*/
20+
public class PaintHouse {
21+
22+
/**
23+
* @param costs of coloring the houses with red, blue, and green respectively. 1st row represents house 1, 2nd row
24+
* house 2 and so on
25+
* @return the minimum cost to paint all houses such that no two adjacent houses are of same color
26+
*/
27+
public static int minCost(int[][] costs) {
28+
if (costs == null || costs.length == 0) return 0;
29+
30+
for (int i = 1; i < costs.length; i++) {
31+
costs[i][0] += Math.min(costs[i - 1][1], costs[i - 1][2]);
32+
costs[i][1] += Math.min(costs[i - 1][0], costs[i - 1][2]);
33+
costs[i][2] += Math.min(costs[i - 1][0], costs[i - 1][1]);
34+
}
35+
36+
int lastRow = costs.length - 1;
37+
return Math.min(Math.min(costs[lastRow][0], costs[lastRow][1]), costs[lastRow][2]);
38+
}
39+
40+
public static void main(String[] args) {
41+
System.out.println(minCost(new int[][]{
42+
}));
43+
44+
System.out.println(minCost(new int[][]{
45+
{2, 3, 4},
46+
{5, 7, 6},
47+
{8, 7, 2}
48+
}));
49+
}
50+
}

0 commit comments

Comments
 (0)