Skip to content

Commit 8b3de3c

Browse files
DAY 14
1 parent 0df0edc commit 8b3de3c

File tree

3 files changed

+258
-0
lines changed

3 files changed

+258
-0
lines changed

Day 15/It.java

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Given an array, rotate the array to the right by k steps, where k is non-negative.
3+
4+
Input Format
5+
6+
len = 7
7+
nums = [1,2,3,4,5,6,7],
8+
k = 3
9+
Constraints
10+
11+
1 <= nums.length <= 105
12+
-231 <= nums[i] <= 231 - 1
13+
0 <= k <= 105
14+
Output Format
15+
16+
[5,6,7,1,2,3,4]
17+
Sample Input 0
18+
19+
7
20+
3
21+
1 2 3 4 5 6 7
22+
Sample Output 0
23+
24+
5 6 7 1 2 3 4
25+
Explanation 0
26+
27+
rotate 1 steps to the right: [7,1,2,3,4,5,6]
28+
rotate 2 steps to the right: [6,7,1,2,3,4,5]
29+
rotate 3 steps to the right: [5,6,7,1,2,3,4]
30+
Sample Input 1
31+
32+
4
33+
2
34+
-1 -100 3 99
35+
Sample Output 1
36+
37+
3 99 -1 -100
38+
Explanation 1
39+
40+
rotate 1 steps to the right: [99,-1,-100,3]
41+
rotate 2 steps to the right: [3,99,-1,-100]
42+
*/
43+
import java.io.*;
44+
import java.util.*;
45+
46+
public class Solution {
47+
public void rotate(int[] nums, int k) {
48+
k = k % nums.length; // for length of array is less to k
49+
if(k < 0){ // for -ve value
50+
k += nums.length;
51+
}
52+
53+
//complete rotate array
54+
rev(nums, 0, nums.length - 1);
55+
56+
//K part reversal
57+
rev(nums, 0, k-1);
58+
59+
//Non K part reversal
60+
rev(nums, k, nums.length -1);
61+
}
62+
63+
private void rev(int[] arr, int i, int j){
64+
while (i < j){
65+
int temp = arr[i];
66+
arr[i] = arr[j];
67+
arr[j] = temp;
68+
i++;
69+
j--;
70+
}
71+
72+
}
73+
public static void main(String[] args) {
74+
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
75+
Scanner sc = new Scanner(System.in);
76+
int n = sc.nextInt();
77+
int k = sc.nextInt();
78+
int[] a = new int[n];
79+
for(int i =0;i<n;i++){
80+
a[i] = sc.nextInt();
81+
}
82+
Solution s = new Solution();
83+
s.rotate(a,k);
84+
for(int i =0;i<n;i++){
85+
System.out.print(a[i] + " ");
86+
}
87+
}
88+
}

Day 15/Made.java

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* You are given an array of words where each word consists of lowercase English letters.
3+
4+
wordA is a predecessor of wordB if and only if we can insert exactly one letter anywhere in wordA without changing the order of the other characters to make it equal to wordB.
5+
6+
For example, "abc" is a predecessor of "abac", while "cba" is not a predecessor of "bcad". A word chain is a sequence of words [word1, word2, ..., wordk] with k >= 1, where word1 is a predecessor of word2, word2 is a predecessor of word3, and so on. A single word is trivially a word chain with k == 1.
7+
8+
Return the length of the longest possible word chain with words chosen from the given list of words.
9+
10+
Input Format
11+
12+
len = 6 arr = ["a","b","ba","bca","bda","bdca"]
13+
14+
Constraints
15+
16+
1 <= words.length <= 1000
17+
1 <= words[i].length <= 16
18+
words[i] only consists of lowercase English letters.
19+
Output Format
20+
21+
4
22+
23+
Sample Input 0
24+
25+
6
26+
a b ba bca bda bdca
27+
Sample Output 0
28+
29+
4
30+
Explanation 0
31+
32+
One of the longest word chains is ["a","ba","bda","bdca"].
33+
Sample Input 1
34+
35+
5
36+
xbc pcxbcf xb cxbc pcxbc
37+
Sample Output 1
38+
39+
5
40+
Explanation 1
41+
42+
All the words can be put in a word chain ["xb", "xbc", "cxbc", "pcxbc", "pcxbcf"].
43+
44+
Sample Input 2
45+
46+
2
47+
abcd dbqca
48+
Sample Output 2
49+
50+
1
51+
Explanation 2
52+
53+
The trivial word chain ["abcd"] is one of the longest word chains.
54+
["abcd","dbqca"] is not a valid word chain because the ordering of the letters is changed.
55+
*/
56+
import java.io.*;
57+
import java.util.*;
58+
59+
public class Solution {
60+
public static int longestStrChain(String[] words) {
61+
Map<String, Integer> dp = new HashMap<>();
62+
Arrays.sort(words, (a, b)->a.length() - b.length());
63+
int res = 0;
64+
for (String word : words) {
65+
int best = 0;
66+
for (int i = 0; i < word.length(); ++i) {
67+
String prev = word.substring(0, i) + word.substring(i + 1);
68+
best = Math.max(best, dp.getOrDefault(prev, 0) + 1);
69+
}
70+
dp.put(word, best);
71+
res = Math.max(res, best);
72+
}
73+
return res;
74+
}
75+
public static void main(String[] args) {
76+
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
77+
Scanner sc = new Scanner(System.in);
78+
int n = sc.nextInt();
79+
String[] s = new String[n];
80+
for(int i =0;i<n;i++){
81+
s[i] = sc.next();
82+
}
83+
System.out.println(longestStrChain(s));
84+
}
85+
}

Day 15/You.java

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* You are given an integer array matchsticks where matchsticks[i] is the length of the ith matchstick. You want to use all the matchsticks to make one square. You should not break any stick, but you can link them up, and each matchstick must be used exactly one time. Return true if you can make this square and false otherwise.
3+
4+
Example 1:
5+
6+
Input: matchsticks = [1,1,2,2,2] Output: true Explanation: You can form a square with length 2, one side of the square came two sticks with length 1.
7+
8+
Example 2: Input: matchsticks = [3,3,3,3,4] Output: false Explanation: You cannot find a way to form a square with all the matchsticks.
9+
10+
Input Format
11+
12+
5 1 1 2 2 2
13+
14+
Constraints
15+
16+
1 <= matchsticks.length <= 15 1 <= matchsticks[i] <= 108
17+
18+
Output Format
19+
20+
true
21+
22+
Sample Input 0
23+
24+
5
25+
1 1 2 2 2
26+
Sample Output 0
27+
28+
true
29+
Sample Input 1
30+
31+
5
32+
3 3 3 3 4
33+
Sample Output 1
34+
35+
false
36+
*/
37+
import java.io.*;
38+
import java.util.*;
39+
40+
public class Solution {
41+
public boolean makesquare(int[] matchsticks) {
42+
43+
int total = 0;
44+
45+
for (int i : matchsticks) {
46+
total += i;
47+
}
48+
49+
if (total % 4 != 0) return false; // if we cant make 4 equals sides then theres no way to make a square
50+
// sort the array and place the largest sides first. required optimization to not TLE
51+
Arrays.sort(matchsticks);
52+
return match(matchsticks, matchsticks.length - 1, 0, 0, 0, 0, total / 4);
53+
}
54+
55+
public boolean match(int[] matchsticks, int index, int top, int bottom, int left, int right, int target) {
56+
57+
if (top == target && bottom == target && left == target && right == target) return true;
58+
59+
if (top > target || bottom > target || left > target || right > target) return false;
60+
61+
int val = matchsticks[index];
62+
63+
boolean t = match(matchsticks, index - 1, top + val, bottom, left, right, target);
64+
if (t) return true;
65+
boolean b = match(matchsticks, index - 1, top, bottom + val, left, right, target);
66+
if (b) return true;
67+
boolean l = match(matchsticks, index - 1, top, bottom, left + val, right, target);
68+
if (l) return true;
69+
boolean r = match(matchsticks, index - 1, top, bottom, left, right + val, target);
70+
if (r) return true;
71+
72+
return false;
73+
}
74+
public static void main(String[] args) {
75+
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
76+
Scanner sc = new Scanner(System.in);
77+
int n = sc.nextInt();
78+
int[] a = new int[n];
79+
for(int i = 0;i<n;i++){
80+
a[i] = sc.nextInt();
81+
}
82+
Solution s = new Solution();
83+
System.out.println(s.makesquare(a));
84+
}
85+
}

0 commit comments

Comments
 (0)