Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 964f49e

Browse files
Some solved problems
1 parent ebe0744 commit 964f49e

File tree

12 files changed

+1058
-0
lines changed

12 files changed

+1058
-0
lines changed

‎README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ My accepted leetcode solutions to some of the common interview problems.
4242
- [Card Flipping Game](problems/src/array/CardFilipGame.java) (Medium)
4343
- [Employee Free Time](problems/src/array/EmployeeFreeTime.java) (Hard)
4444
- [Best Meeting Point](problems/src/array/BestMeetingPoint.java) (Hard)
45+
- [My Calendar III](problems/src/array/MyCalendarThree.java) (Hard)
4546

4647
#### [Backtracking](problems/src/backtracking)
4748

@@ -93,6 +94,8 @@ My accepted leetcode solutions to some of the common interview problems.
9394
- [Cut Off Trees for Golf Event](problems/src/breadth_first_search/CutOffTreesForGolfEvent.java) (Hard)
9495
- [Race Car](problems/src/breadth_first_search/RaceCar.java) (Hard)
9596
- [Bus Routes](problems/src/breadth_first_search/BusRoutes.java) (Hard)
97+
- [Sliding Puzzle](problems/src/breadth_first_search/SlidingPuzzle.java) (Hard)
98+
- [Matrix](problems/src/breadth_first_search/Matrix.java) (Medium)
9699

97100
#### [Depth First Search](problems/src/depth_first_search)
98101

@@ -183,6 +186,8 @@ My accepted leetcode solutions to some of the common interview problems.
183186
- [Largest Sum of Averages](problems/src/dynamic_programming/LargestSumOfAverages.java) (Medium)
184187
- [Minimum Number of Refueling Stops](problems/src/dynamic_programming/MinimumNumberOfRefuelingStops.java) (Hard)
185188
- [Cat and Mouse](problems/src/dynamic_programming/CatAndMouse.java) (Hard)
189+
- [Stone Game](problems/src/dynamic_programming/StoneGame.java) (Medium)
190+
- [Odd Even Jump](problems/src/dynamic_programming/OddEvenJump.java) (Hard)
186191

187192

188193
#### [Greedy](problems/src/greedy)
@@ -232,6 +237,7 @@ My accepted leetcode solutions to some of the common interview problems.
232237
- [Delete Node in a Linked List](problems/src/linked_list/DeleteNode.java) (Easy)
233238
- [Reverse Nodes in k-Group](problems/src/linked_list/ReverseNodesKGroup.java) (Hard)
234239
- [Swap Nodes in Pairs](problems/src/linked_list/SwapNodesInPairs.java) (Medium)
240+
- [Middle of Linked List](problems/src/linked_list/MiddleOfLinkedList.java) (Easy)
235241

236242
#### [Math](problems/src/math)
237243

@@ -247,6 +253,7 @@ My accepted leetcode solutions to some of the common interview problems.
247253
- [Solve the Equation](problems/src/math/SolveTheEquation.java) (Medium)
248254
- [Couples Holding Hands](problems/src/math/CouplesHoldingHands.java) (Hard)
249255
- [Reaching Points](problems/src/math/ReachingPoints.java) (Hard)
256+
- [Nth Magical Number](problems/src/math/NthMagicalNumber.java) (Hard)
250257

251258
#### [Reservoir Sampling](problems/src/reservoir_sampling)
252259

@@ -292,6 +299,8 @@ My accepted leetcode solutions to some of the common interview problems.
292299
- [Find the Closest Palindrome](problems/src/string/FindTheClosestPalindrome.java) (Hard)
293300
- [Monotone Increasing Digits](problems/src/string/MonotoneIncreasingDigits.java) (Medium)
294301
- [Shortest Palindrome](problems/src/string/ShortestPalindrome.java) (Hard)
302+
- [Valid Word Abbreviation](problems/src/string/ValidWordAbbreviation.java) (Easy)
303+
- [Longest Palindrome](problems/src/string/LongestPalindrome.java) (Easy)
295304

296305
#### [Tree](problems/src/tree)
297306

@@ -335,6 +344,8 @@ My accepted leetcode solutions to some of the common interview problems.
335344
- [Maximum Width of Binary Tree](problems/src/tree/MaximumWidthOfBinaryTree.java) (Medium)
336345
- [Recover Binary Search Tree](problems/src/tree/RecoverBinarySearchTree.java) (Hard)
337346
- [Binary Tree Postorder Traversal](problems/src/tree/BinaryTreePostorderTraversal.java) (Hard)
347+
- [Serialize and Deserialize N-ary Tree](problems/src/tree/SerializeAndDeserializeNAryTree.java) (Hard)
348+
- [Convert BST to Greater Tree](problems/src/tree/ConvertBSTToGreaterTree.java) (Easy)
338349

339350
#### [Two Pointers](problems/src/two_pointers)
340351

‎problems/src/array/MyCalendarThree.java

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package array;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Created by gouthamvidyapradhan on 12/03/2019
7+
* Implement a MyCalendarThree class to store your events. A new event can always be added.
8+
*
9+
* Your class will have one method, book(int start, int end). Formally, this represents a booking on the half open
10+
* interval [start, end), the range of real numbers x such that start <= x < end.
11+
*
12+
* A K-booking happens when K events have some non-empty intersection (ie., there is some time that is common to all
13+
* K events.)
14+
*
15+
* For each call to the method MyCalendar.book, return an integer K representing the largest integer such that there
16+
* exists a K-booking in the calendar.
17+
*
18+
* Your class will be called like this: MyCalendarThree cal = new MyCalendarThree(); MyCalendarThree.book(start, end)
19+
* Example 1:
20+
*
21+
* MyCalendarThree();
22+
* MyCalendarThree.book(10, 20); // returns 1
23+
* MyCalendarThree.book(50, 60); // returns 1
24+
* MyCalendarThree.book(10, 40); // returns 2
25+
* MyCalendarThree.book(5, 15); // returns 3
26+
* MyCalendarThree.book(5, 10); // returns 3
27+
* MyCalendarThree.book(25, 55); // returns 3
28+
* Explanation:
29+
* The first two events can be booked and are disjoint, so the maximum K-booking is a 1-booking.
30+
* The third event [10, 40) intersects the first event, and the maximum K-booking is a 2-booking.
31+
* The remaining events cause the maximum K-booking to be only a 3-booking.
32+
* Note that the last event locally causes a 2-booking, but the answer is still 3 because
33+
* eg. [10, 20), [10, 40), and [5, 15) are still triple booked.
34+
*/
35+
public class MyCalendarThree{
36+
37+
private List<Node> events;
38+
private int max;
39+
40+
private class Node{
41+
int n, index;
42+
Node(int n, int index){
43+
this.n = n;
44+
this.index = index;
45+
}
46+
47+
public int getN() {
48+
return n;
49+
}
50+
51+
public int getIndex() {
52+
return index;
53+
}
54+
}
55+
56+
public MyCalendarThree() {
57+
events = new ArrayList<>();
58+
max = 0;
59+
}
60+
61+
/**
62+
* Main method
63+
* @param args
64+
*/
65+
public static void main(String[] args) {
66+
MyCalendarThree calendar = new MyCalendarThree();
67+
System.out.println(calendar.book(10, 20));
68+
System.out.println(calendar.book(50, 60));
69+
System.out.println(calendar.book(10, 40));
70+
System.out.println(calendar.book(5, 15));
71+
System.out.println(calendar.book(5, 10));
72+
System.out.println(calendar.book(25, 55));
73+
}
74+
75+
76+
public int book(int start, int end) {
77+
events.add(new Node(start, 1));
78+
events.add(new Node(end, 0));
79+
events.sort((Comparator.comparing(Node::getN).thenComparing(Node::getIndex)));
80+
int count = 0;
81+
for(Node node : events){
82+
if(node.index == 1 && node.n >= end){
83+
break;
84+
}
85+
count += node.index == 1 ? 1 : -1;
86+
if(node.getN() >= start){
87+
max = Math.max(max, count);
88+
}
89+
}
90+
return max;
91+
}
92+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package breadth_first_search;
2+
3+
import java.util.*;
4+
/**
5+
* Created by gouthamvidyapradhan on 14/03/2019
6+
* Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell.
7+
*
8+
* The distance between two adjacent cells is 1.
9+
* Example 1:
10+
* Input:
11+
*
12+
* 0 0 0
13+
* 0 1 0
14+
* 0 0 0
15+
* Output:
16+
* 0 0 0
17+
* 0 1 0
18+
* 0 0 0
19+
* Example 2:
20+
* Input:
21+
*
22+
* 0 0 0
23+
* 0 1 0
24+
* 1 1 1
25+
* Output:
26+
* 0 0 0
27+
* 0 1 0
28+
* 1 2 1
29+
* Note:
30+
* The number of elements of the given matrix will not exceed 10,000.
31+
* There are at least one 0 in the given matrix.
32+
* The cells are adjacent in only four directions: up, down, left and right.
33+
*
34+
* Solution: Add all the 0th cell to the queue and do a multi-source bfs to count the minimum distance
35+
*/
36+
public class Matrix {
37+
private static class Node{
38+
int r, c;
39+
int d;
40+
Node(int r, int c){
41+
this.r = r;
42+
this.c = c;
43+
}
44+
45+
@Override
46+
public boolean equals(Object o) {
47+
if (this == o) return true;
48+
if (!(o instanceof Node)) return false;
49+
Node node = (Node) o;
50+
return r == node.r &&
51+
c == node.c;
52+
}
53+
54+
@Override
55+
public int hashCode() {
56+
return Objects.hash(r, c);
57+
}
58+
}
59+
private final int[] R = {0, 0, 1, -1};
60+
private final int[] C = {1, -1, 0, 0};
61+
private Set<Node> done;
62+
/**
63+
* Main method
64+
* @param args
65+
*/
66+
public static void main(String[] args) {
67+
int[][] temp = {{0, 0, 0}, {0, 1, 0}, {1, 1, 1}};
68+
int[][] result = new Matrix().updateMatrix(temp);
69+
System.out.println();
70+
}
71+
72+
public int[][] updateMatrix(int[][] matrix) {
73+
int[][] temp = new int[matrix.length][matrix[0].length];
74+
done = new HashSet<>();
75+
Queue<Node> queue = new ArrayDeque<>();
76+
for(int i = 0; i < matrix.length; i ++){
77+
for(int j = 0; j < matrix[0].length; j ++){
78+
temp[i][j] = matrix[i][j];
79+
if(matrix[i][j] == 0){
80+
Node node = new Node(i, j);
81+
queue.offer(node);
82+
done.add(node);
83+
}
84+
}
85+
}
86+
while(!queue.isEmpty()){
87+
Node curr = queue.poll();
88+
for(int i = 0; i < 4; i ++){
89+
int newR = curr.r + R[i];
90+
int newC = curr.c + C[i];
91+
if(newR >= 0 && newR < matrix.length && newC >= 0 && newC < matrix[0].length){
92+
Node child = new Node(newR, newC);
93+
if(!done.contains(child)){
94+
done.add(child);
95+
child.d = curr.d + 1;
96+
temp[newR][newC] = child.d;
97+
queue.offer(child);
98+
}
99+
}
100+
}
101+
}
102+
return temp;
103+
}
104+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /