0% found this document useful (0 votes)
8 views4 pages

Adv Task 4-0249

The document contains code solutions for various LeetCode problems, including checking if a string contains all binary codes of size k, finding the longest chunked palindrome decomposition, calculating a constrained subsequence sum, and determining the maximum value of an equation based on given points. Each solution is presented in Java and includes the logic and structure necessary to solve the respective problem. Links to the problems on LeetCode are also provided for reference.

Uploaded by

shhreyeah30
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Adv Task 4-0249

The document contains code solutions for various LeetCode problems, including checking if a string contains all binary codes of size k, finding the longest chunked palindrome decomposition, calculating a constrained subsequence sum, and determining the maximum value of an equation based on given points. Each solution is presented in Java and includes the logic and structure necessary to solve the respective problem. Links to the problems on LeetCode are also provided for reference.

Uploaded by

shhreyeah30
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

‭ AIVALYA PEKALA‬

K
‭VU21CSEN0100249‬
‭Task-4‬

‭ .‬‭https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/‬
1
‭Code:‬
‭class Solution {‬
‭public boolean hasAllCodes(String s, int k) {‬
‭Set<String> permutations = new HashSet<>();‬
‭String temp = "";‬
‭for (int i = 0; i < s.length(); i++) {‬
‭temp += s.charAt(i);‬
‭if (i < k - 1) {‬
‭continue;‬
‭}‬
‭permutations.add(temp);‬
‭temp = temp.substring(1);‬

‭}‬

‭double nums = Math.pow(2, k);‬

‭return permutations.size() >= nums;‬


‭}‬
‭}‬
‭Output:‬

‭ .‬‭https://leetcode.com/problems/longest-chunked-palindrome-decomposition/‬
2
‭Code:‬
‭class Solution {‬
‭public int longestDecomposition(String text) {‬
‭int n = text.length();‬
‭int k = 0, totalLength = 0;‬
‭int str1Start = 0, str1End = 0;‬
‭int str2Start = n-1, str2End = n;‬
‭while (str1End < str2Start) {‬
‭if (text.substring(str1Start, str1End + 1).equals(text.substring(str2Start, str2End))) {‬
‭totalLength += (str2End - str2Start) * 2;‬
‭ AIVALYA PEKALA‬
K
‭VU21CSEN0100249‬
k‭ ++;‬
‭str1Start = str1End + 1;‬
‭str2End = str2Start;‬
}‭ ‬
‭str1End++;‬
‭str2Start--;‬
}‭ ‬
‭if (totalLength < n) return (k * 2) + 1;‬
‭return k * 2;‬
‭}‬
‭}‬

‭Output:‬

‭3.‬‭https://leetcode.com/problems/constrained-subsequence-sum‬
‭ ode:‬
C
‭class Solution {‬
‭public int constrainedSubsetSum(int[] nums, int k) {‬
‭int n = nums.length;‬
‭int[] dp = new int[n];‬
‭dp[0] = nums[0];‬
‭int temp = Math.max(dp[0],0);‬
‭Queue<Integer> q = new LinkedList<>();‬
‭TreeMap<Integer,Integer> map = new TreeMap<>();‬
‭q.add(dp[0]);‬
‭map.put(dp[0],1);‬
‭for(int i=1;i<n;i++){‬
‭dp[i] = nums[i];‬
‭if(map.lastKey()>0) dp[i] += map.lastKey();‬
‭map.put(dp[i],map.getOrDefault(dp[i],0)+1);‬
‭q.add(dp[i]);‬
‭if(q.size()>k){‬
‭int val = q.remove();‬
‭if(map.get(val)==1) map.remove(val);‬
‭ AIVALYA PEKALA‬
K
‭VU21CSEN0100249‬
‭else map.put(val,map.get(val)-1);‬
‭}‬
}‭ ‬
‭int ans = dp[0];‬
‭for(int i=0;i<n;i++){‬
‭ans = Math.max(ans,dp[i]);‬
‭}‬
‭return ans;‬
‭}‬
}‭ ‬
‭Output:‬
‭ AIVALYA PEKALA‬
K
‭VU21CSEN0100249‬
‭ .‬‭https://leetcode.com/problems/max-value-of-equation‬
5
‭Code:‬
‭class Solution {‬
‭public int findMaxValueOfEquation(int[][] points, int k) {‬
‭int n = points.length;‬
‭int ans = Integer.MIN_VALUE;‬

‭for (int i = 0; i < n - 1; i++) {‬


‭for (int j = i + 1; j < n && Math.abs(points[i][0] - points[j][0]) <= k; j++) {‬
‭int sum = points[i][1] + points[j][1] + Math.abs(points[i][0] - points[j][0]);‬
‭ans = Math.max(ans, sum);‬
‭}‬
‭}‬
‭return ans;‬
‭}‬
‭}‬

‭Output:‬

You might also like