Skip to content

Commit d3d503a

Browse files
Accepted problems
1 parent 365b47a commit d3d503a

4 files changed

Lines changed: 197 additions & 0 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ My accepted leetcode solutions to some of the common interview problems.
3838
- [Minimum Moves to Equal Array Elements II](problems/src/array/MinimumMovesToEqualArray.java) (Median)
3939
- [Image Smoother](problems/src/array/ImageSmoother.java) (Easy)
4040
- [Minimum Index Sum of Two Lists](problems/src/array/MinimumIndexSumOfTwoLists.java) (Easy)
41+
- [Card Flipping Game](problems/src/array/CardFilipGame.java) (Medium)
4142

4243
#### [Backtracking](problems/src/backtracking)
4344

@@ -168,6 +169,7 @@ My accepted leetcode solutions to some of the common interview problems.
168169
- [Palindrome Pairs](problems/src/dynamic_programming/PalindromePairs.java) (Hard)
169170
- [Cherry Pickup](problems/src/dynamic_programming/CherryPickup.java) (Hard)
170171
- [Knight Probability in Chessboard](problems/src/dynamic_programming/KnightProbabilityInChessboard.java) (Medium)
172+
- [Largest Sum of Averages](problems/src/dynamic_programming/LargestSumOfAverages.java) (Medium)
171173

172174

173175
#### [Greedy](problems/src/greedy)
@@ -195,6 +197,7 @@ My accepted leetcode solutions to some of the common interview problems.
195197
- [Brick Wall](problems/src/hashing/BrickWall.java) (Medium)
196198
- [Partition Labels](problems/src/hashing/PartitionLabels.java) (Medium)
197199
- [Custom Sort String](problems/src/hashing/CustomSortString.java) (Medium)
200+
- [Short Encoding of Words](problems/src/hashing/ShortEncodingOfWords.java) (Medium)
198201

199202
#### [Heap](problems/src/heap)
200203

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package array;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
/**
8+
* Created by gouthamvidyapradhan on 04/05/2018.
9+
*
10+
* On a table are N cards, with a positive integer printed on the front and back of each card (possibly different).
11+
12+
We flip any number of cards, and after we choose one card.
13+
14+
If the number X on the back of the chosen card is not on the front of any card, then this number X is good.
15+
16+
What is the smallest number that is good? If no number is good, output 0.
17+
18+
Here, fronts[i] and backs[i] represent the number on the front and back of card i.
19+
20+
A flip swaps the front and back numbers, so the value on the front is now on the back and vice versa.
21+
22+
Example:
23+
24+
Input: fronts = [1,2,4,4,7], backs = [1,3,4,1,3]
25+
Output: 2
26+
Explanation: If we flip the second card, the fronts are [1,3,4,4,7] and the backs are [1,2,4,1,3].
27+
We choose the second card, which has number 2 on the back, and it isn't on the front of any card, so 2 is good.
28+
29+
30+
Note:
31+
32+
1 <= fronts.length == backs.length <= 1000.
33+
1 <= fronts[i] <= 2000.
34+
1 <= backs[i] <= 2000.
35+
36+
*/
37+
public class CardFilipGame {
38+
39+
public static void main(String[] args) {
40+
41+
}
42+
43+
public int flipgame(int[] fronts, int[] backs) {
44+
List<Integer> numbers = new ArrayList<>();
45+
for(int i = 0; i < fronts.length; i ++){
46+
numbers.add(fronts[i]);
47+
numbers.add(backs[i]);
48+
}
49+
Collections.sort(numbers);
50+
for(int n : numbers){
51+
boolean success = true;
52+
for(int i = 0; i < fronts.length; i ++){
53+
if(n == fronts[i] && n == backs[i]){
54+
success = false;
55+
break;
56+
}
57+
}
58+
if(success){
59+
return n;
60+
}
61+
}
62+
return 0;
63+
}
64+
65+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package dynamic_programming;
2+
/**
3+
* Created by gouthamvidyapradhan on 04/05/2018.
4+
* We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?
5+
6+
Note that our partition must use every number in A, and that scores are not necessarily integers.
7+
8+
Example:
9+
Input:
10+
A = [9,1,2,3,9]
11+
K = 3
12+
Output: 20
13+
Explanation:
14+
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
15+
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
16+
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
17+
18+
19+
Note:
20+
21+
1 <= A.length <= 100.
22+
1 <= A[i] <= 10000.
23+
1 <= K <= A.length.
24+
Answers within 10^-6 of the correct answer will be accepted as correct.
25+
26+
Solution: O(N x N x K): Calculate average for each sub-array starting from the right.
27+
*/
28+
29+
public class LargestSumOfAverages {
30+
31+
public static void main(String[] args) {
32+
int[] A = {9,1,2,3,9};
33+
System.out.println(new LargestSumOfAverages().largestSumOfAverages(A, 3));
34+
}
35+
36+
public double largestSumOfAverages(int[] A, int K) {
37+
double[][] dp = new double[K][A.length];
38+
for(int i = dp[0].length - 1; i >= 0; i --){
39+
dp[0][i] = A[i];
40+
if(i + 1 < dp[0].length){
41+
dp[0][i] += dp[0][i + 1];
42+
}
43+
}
44+
45+
for(int i = dp[0].length - 1, j = 1; i >= 0; i --, j++){
46+
dp[0][i] = dp[0][i] / j;
47+
}
48+
49+
for(int i = dp[0].length - 1; i >= 0; i --){
50+
for(int j = 1; j < dp.length; j ++){
51+
double sum = 0.0D;
52+
for(int k = i, c = 1; k < dp[0].length; k ++, c++){
53+
sum += A[k];
54+
if(k + 1 < dp[0].length){
55+
double avg = sum / c;
56+
avg += dp[j - 1][k + 1];
57+
dp[j][i] = Math.max(dp[j][i], avg);
58+
}
59+
}
60+
}
61+
}
62+
return dp[K-1][0];
63+
}
64+
65+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package hashing;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Created by gouthamvidyapradhan on 04/05/2018.
7+
* Given a list of words, we may encode it by writing a reference string S and a list of indexes A.
8+
9+
For example, if the list of words is ["time", "me", "bell"], we can write it as S = "time#bell#" and indexes = [0,
10+
2, 5].
11+
12+
Then for each index, we will recover the word by reading from the reference string from that index until we reach a
13+
"#" character.
14+
15+
What is the length of the shortest reference string S possible that encodes the given words?
16+
17+
Example:
18+
19+
Input: words = ["time", "me", "bell"]
20+
Output: 10
21+
Explanation: S = "time#bell#" and indexes = [0, 2, 5].
22+
Note:
23+
24+
1 <= words.length <= 2000.
25+
1 <= words[i].length <= 7.
26+
Each word has only lowercase letters.
27+
28+
Solution: Sort the words by length and then use a hashmap to map each substring of a string with its position.
29+
*/
30+
public class ShortEncodingOfWords {
31+
class Node {
32+
String s;
33+
int l;
34+
Node(String s, int l){
35+
this.s = s;
36+
this.l = l;
37+
}
38+
}
39+
40+
public static void main(String[] args) {
41+
String[] A = {"memo", "me", "mo"};
42+
System.out.println(new ShortEncodingOfWords().minimumLengthEncoding(A));
43+
}
44+
45+
public int minimumLengthEncoding(String[] words) {
46+
List<Node> list = new ArrayList<>();
47+
for(String w : words){
48+
list.add(new Node(w, w.length()));
49+
}
50+
Collections.sort(list, (o1, o2) -> Integer.compare(o2.l, o1.l));
51+
Map<String, Integer> map = new HashMap<>();
52+
int count = 0;
53+
for(Node node : list){
54+
String str = node.s;
55+
if(!map.containsKey(str)){
56+
for(int i = 0, l = str.length(); i < l; i++){
57+
map.put(str.substring(i, l), count + i);
58+
}
59+
count += (str.length() + 1);
60+
}
61+
}
62+
return count;
63+
}
64+
}

0 commit comments

Comments
 (0)