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 78147c9

Browse files
Solved problems
1 parent d6e745a commit 78147c9

14 files changed

+1170
-59
lines changed

‎README.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ My accepted leetcode solutions to some of the common interview problems.
4646
- [Champagne Tower](problems/src/array/ChampagneTower.java) (Medium)
4747
- [Valid Tic-Tac-Toe State](problems/src/array/ValidTicTacToeState.java) (Medium)
4848
- [Number of Subarrays with Bounded Maximum](problems/src/array/SubArraysWithBoundedMaximum.java) (Medium)
49+
- [Surface Area of 3D Shapes](problems/src/array/SurfaceAreaOfThreeDShapes.java) (Easy)
4950

5051
#### [Backtracking](problems/src/backtracking)
5152

@@ -67,6 +68,7 @@ My accepted leetcode solutions to some of the common interview problems.
6768
- [Wildcard Matching](problems/src/backtracking/WildcardMatching.java) (Hard)
6869
- [Letter Case Permutation](problems/src/backtracking/LetterCasePermutation.java) (Easy)
6970

71+
7072
#### [Binary Search](problems/src/binary_search)
7173

7274
- [Minimum Sorted Rotated Array](problems/src/binary_search/MinSortedRotatedArray.java) (Medium)
@@ -194,6 +196,7 @@ My accepted leetcode solutions to some of the common interview problems.
194196
- [Stone Game](problems/src/dynamic_programming/StoneGame.java) (Medium)
195197
- [Odd Even Jump](problems/src/dynamic_programming/OddEvenJump.java) (Hard)
196198
- [Profitable Schemes](problems/src/dynamic_programming/ProfitableSchemes.java) (Hard)
199+
- [Maximum Vacation Days](problems/src/dynamic_programming/MaximumVacationDays.java) (Hard)
197200

198201

199202
#### [Greedy](problems/src/greedy)
@@ -207,6 +210,9 @@ My accepted leetcode solutions to some of the common interview problems.
207210
- [Queue Reconstruction By Height](problems/src/greedy/QueueReconstructionByHeight.java) (Medium)
208211
- [Task Scheduler](problems/src/greedy/TaskScheduler.java) (Medium)
209212
- [Maximum Length of Pair Chain](problems/src/greedy/MaximumLengthOfPairChain.java) (Medium)
213+
- [Lemonade Change](problems/src/greedy/LemonadeChange.java) (Easy)
214+
- [Score After Flipping Matrix](problems/src/greedy/ScoreAfterFlippingMatrix.java) (Medium)
215+
- [IPO](problems/src/greedy/IPO.java) (Hard)
210216

211217
#### [Hashing](problems/src/hashing)
212218

@@ -223,6 +229,8 @@ My accepted leetcode solutions to some of the common interview problems.
223229
- [Custom Sort String](problems/src/hashing/CustomSortString.java) (Medium)
224230
- [Short Encoding of Words](problems/src/hashing/ShortEncodingOfWords.java) (Medium)
225231
- [Substring with Concatenation of All Words](problems/src/hashing/SubstringConcatenationOfWords.java) (Hard)
232+
- [Distribute Candies](problems/src/hashing/DistributeCandies.java) (Easy)
233+
- [Groups of Special-Equivalent Strings](problems/src/hashing/GroupsOfSpecialEquivalentStrings.java) (Easy)
226234

227235
#### [Heap](problems/src/heap)
228236

@@ -232,6 +240,7 @@ My accepted leetcode solutions to some of the common interview problems.
232240
- [Top K Frequent Words](problems/src/heap/TopKFrequentWords.java) (Medium)
233241
- [Candy](problems/src/heap/Candy.java) (Hard)
234242
- [Smallest Rotation with Highest Score](problems/src/heap/SmallestRotationWithHighestScore.java) (Hard)
243+
- [Maximum Frequency Stack](problems/src/heap/FreqStack.java) (Hard)
235244

236245
#### [Linked List](problems/src/linked_list)
237246

@@ -261,6 +270,7 @@ My accepted leetcode solutions to some of the common interview problems.
261270
- [Couples Holding Hands](problems/src/math/CouplesHoldingHands.java) (Hard)
262271
- [Reaching Points](problems/src/math/ReachingPoints.java) (Hard)
263272
- [Nth Magical Number](problems/src/math/NthMagicalNumber.java) (Hard)
273+
- [Squirrel Simulation](problems/src/math/SquirrelSimulation.java) (Medium)
264274

265275
#### [Reservoir Sampling](problems/src/reservoir_sampling)
266276

@@ -310,6 +320,7 @@ My accepted leetcode solutions to some of the common interview problems.
310320
- [Longest Palindrome](problems/src/string/LongestPalindrome.java) (Easy)
311321
- [Replace Words](problems/src/string/ReplaceWords.java) (Medium)
312322
- [Rotate String](problems/src/string/RotateString.java) (Easy)
323+
- [Keyboard Row](problems/src/string/KeyboardRow.java) (Easy)
313324

314325
#### [Tree](problems/src/tree)
315326

@@ -355,6 +366,8 @@ My accepted leetcode solutions to some of the common interview problems.
355366
- [Binary Tree Postorder Traversal](problems/src/tree/BinaryTreePostorderTraversal.java) (Hard)
356367
- [Serialize and Deserialize N-ary Tree](problems/src/tree/SerializeAndDeserializeNAryTree.java) (Hard)
357368
- [Convert BST to Greater Tree](problems/src/tree/ConvertBSTToGreaterTree.java) (Easy)
369+
- [All Nodes Distance K in Binary Tree](problems/src/tree/AllNodesDistanceKInBinaryTree.java) (Medium)
370+
- [All Possible Full Binary Trees](problems/src/tree/AllPossibleFullBinaryTrees.java) (Medium)
358371

359372
#### [Two Pointers](problems/src/two_pointers)
360373

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package array;
2+
3+
/**
4+
* Created by gouthamvidyapradhan on 30/04/2019
5+
* On a N * N grid, we place some 1 * 1 * 1 cubes.
6+
*
7+
* Each value v = grid[i][j] represents a tower of v cubes placed on top of grid cell (i, j).
8+
*
9+
* Return the total surface area of the resulting shapes.
10+
*
11+
*
12+
*
13+
* Example 1:
14+
*
15+
* Input: [[2]]
16+
* Output: 10
17+
* Example 2:
18+
*
19+
* Input: [[1,2],[3,4]]
20+
* Output: 34
21+
* Example 3:
22+
*
23+
* Input: [[1,0],[0,2]]
24+
* Output: 16
25+
* Example 4:
26+
*
27+
* Input: [[1,1,1],[1,0,1],[1,1,1]]
28+
* Output: 32
29+
* Example 5:
30+
*
31+
* Input: [[2,2,2],[2,1,2],[2,2,2]]
32+
* Output: 46
33+
*
34+
*
35+
* Note:
36+
*
37+
* 1 <= N <= 50
38+
* 0 <= grid[i][j] <= 50
39+
*
40+
* Solution: O(N x M) For each cell, check each adjacent cell and sum the value of (current cell - adjacent cell)
41+
* if the current cell value is greater than adjacent cell.
42+
* For every cell which has value grater then 0, the top surface area is by default 1 therefore add one to
43+
* the sum of each cell.
44+
*/
45+
public class SurfaceAreaOfThreeDShapes {
46+
47+
private final int[] R = {0, 0, -1, 1};
48+
private final int[] C = {1, -1, 0, 0};
49+
/**
50+
* Main method
51+
* @param args
52+
*/
53+
public static void main(String[] args) {
54+
int[][] A = {{2}};
55+
System.out.println(new SurfaceAreaOfThreeDShapes().surfaceArea(A));
56+
}
57+
58+
public int surfaceArea(int[][] grid) {
59+
int sum = 0;
60+
for(int i = 0; i < grid.length; i ++){
61+
for(int j = 0; j < grid[i].length; j ++){
62+
int cell = grid[i][j];
63+
for(int k = 0; k < 4; k ++){
64+
int newR = i + R[k];
65+
int newC = j + C[k];
66+
if(newR >= 0 && newC >= 0 && newR < grid.length && newC < grid[0].length){
67+
int adjacent = grid[newR][newC];
68+
if(cell > adjacent){
69+
sum += (cell - adjacent);
70+
}
71+
} else if(newR < 0 || newR >= grid.length || newC < 0 || newC >= grid[0].length){
72+
sum += cell;
73+
}
74+
}
75+
if(cell > 0){
76+
sum += 2;
77+
}
78+
}
79+
}
80+
return sum;
81+
}
82+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package dynamic_programming;
2+
3+
/**
4+
* Created by gouthamvidyapradhan on 13/04/2019
5+
*
6+
* LeetCode wants to give one of its best employees the option to travel among N cities to collect algorithm
7+
* problems. But all work and no play makes Jack a dull boy, you could take vacations in some particular cities and
8+
* weeks. Your job is to schedule the traveling to maximize the number of vacation days you could take, but there are
9+
* certain rules and restrictions you need to follow.
10+
*
11+
* Rules and restrictions:
12+
* You can only travel among N cities, represented by indexes from 0 to N-1. Initially, you are in the city indexed 0
13+
* on Monday.
14+
* The cities are connected by flights. The flights are represented as a N*N matrix (not necessary symmetrical),
15+
* called flights representing the airline status from the city i to the city j. If there is no flight from the city
16+
* i to the city j, flights[i][j] = 0; Otherwise, flights[i][j] = 1. Also, flights[i][i] = 0 for all i.
17+
* You totally have K weeks (each week has 7 days) to travel. You can only take flights at most once per day and can
18+
* only take flights on each week's Monday morning. Since flight time is so short, we don't consider the impact of
19+
* flight time.
20+
* For each city, you can only have restricted vacation days in different weeks, given an N*K matrix called days
21+
* representing this relationship. For the value of days[i][j], it represents the maximum days you could take
22+
* vacation in the city i in the week j.
23+
* You're given the flights matrix and days matrix, and you need to output the maximum vacation days you could take
24+
* during K weeks.
25+
*
26+
* Example 1:
27+
* Input:flights = [[0,1,1],[1,0,1],[1,1,0]], days = [[1,3,1],[6,0,3],[3,3,3]]
28+
* Output: 12
29+
* Explanation:
30+
* Ans = 6 + 3 + 3 = 12.
31+
*
32+
* One of the best strategies is:
33+
* 1st week : fly from city 0 to city 1 on Monday, and play 6 days and work 1 day.
34+
* (Although you start at city 0, we could also fly to and start at other cities since it is Monday.)
35+
* 2nd week : fly from city 1 to city 2 on Monday, and play 3 days and work 4 days.
36+
* 3rd week : stay at city 2, and play 3 days and work 4 days.
37+
* Example 2:
38+
* Input:flights = [[0,0,0],[0,0,0],[0,0,0]], days = [[1,1,1],[7,7,7],[7,7,7]]
39+
* Output: 3
40+
* Explanation:
41+
* Ans = 1 + 1 + 1 = 3.
42+
*
43+
* Since there is no flights enable you to move to another city, you have to stay at city 0 for the whole 3 weeks.
44+
* For each week, you only have one day to play and six days to work.
45+
* So the maximum number of vacation days is 3.
46+
* Example 3:
47+
* Input:flights = [[0,1,1],[1,0,1],[1,1,0]], days = [[7,0,0],[0,7,0],[0,0,7]]
48+
* Output: 21
49+
* Explanation:
50+
* Ans = 7 + 7 + 7 = 21
51+
*
52+
* One of the best strategies is:
53+
* 1st week : stay at city 0, and play 7 days.
54+
* 2nd week : fly from city 0 to city 1 on Monday, and play 7 days.
55+
* 3rd week : fly from city 1 to city 2 on Monday, and play 7 days.
56+
* Note:
57+
* N and K are positive integers, which are in the range of [1, 100].
58+
* In the matrix flights, all the values are integers in the range of [0, 1].
59+
* In the matrix days, all the values are integers in the range [0, 7].
60+
* You could stay at a city beyond the number of vacation days, but you should work on the extra days, which won't be
61+
* counted as vacation days.
62+
* If you fly from the city A to the city B and take the vacation on that day, the deduction towards vacation days
63+
* will count towards the vacation days of city B in that week.
64+
* We don't consider the impact of flight hours towards the calculation of vacation days.
65+
*
66+
* Solution:
67+
* O(N x N x K)
68+
* Start from the last week K; Calculate for the last week maximum vacation days for all possible cities.
69+
* Now, iteratively calculate backwards
70+
* for each week K - 1
71+
* and for each city
72+
* and for each available connection from the city
73+
* memoize the maximum vacation days possible for this city
74+
* Return the answer for city 0 and week 0
75+
*/
76+
public class MaximumVacationDays {
77+
78+
/**
79+
* Main method
80+
* @param args
81+
*/
82+
public static void main(String[] args) {
83+
int[][] flights = {{0,1,0,1,1},{1,0,0,1,1},{1,0,0,1,0},{0,1,0,0,0},{0,0,0,1,0}};
84+
int[][] days = {{0,4,1,6,6},{4,3,3,0,1},{3,6,6,5,0},{1,3,1,1,4},{5,3,3,3,4}};
85+
System.out.println(new MaximumVacationDays().maxVacationDays(flights, days));
86+
}
87+
88+
public int maxVacationDays(int[][] flights, int[][] days) {
89+
int N = days.length;
90+
int W = days[0].length;
91+
int[][] DP = new int[N][W + 1];
92+
for(int w = W - 1; w >= 0; w --){
93+
for(int n = 0; n < N; n ++){
94+
DP[n][w] = days[n][w] + DP[n][w + 1];
95+
}
96+
97+
for(int n = 0; n < N; n ++){
98+
int max = Integer.MIN_VALUE;
99+
int[] F = flights[n];
100+
for(int i = 0; i < F.length; i ++){
101+
if(F[i] == 1){
102+
max = Math.max(max, days[i][w] + DP[i][w + 1]);
103+
}
104+
}
105+
DP[n][w] = Math.max(DP[n][w], max);
106+
}
107+
}
108+
return DP[0][0];
109+
}
110+
}

0 commit comments

Comments
(0)

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