0% found this document useful (0 votes)
15 views10 pages

Advance Coding4

Uploaded by

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

Advance Coding4

Uploaded by

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

N.

KARTHIKEYA
VU21CSEN0101813

ADVANCE CODING-4
N.KARTHIKEYA

VU21CSEN0101813
QUESTION1

Given a circular integer array nums of length n, return the maximum possible
sum of a non-empty subarray of nums.
A circular array means the end of the array connects to the beginning of the
array. Formally, the next element of nums[i] is nums[(i + 1) % n] and the
previous element of nums[i] is nums[(i - 1 + n) % n].
A subarray may only include each element of the fixed buffer nums at most
once. Formally, for a subarray nums[i], nums[i + 1], ..., nums[j], there does not
exist i <= k1, k2 <= j with k1 % n == k2 % n.

Example 1:
Input: nums = [1,-2,3,-2]
Output: 3
Explanation: Subarray [3] has maximum sum 3.
Example 2:
Input: nums = [5,-3,5]
Output: 10
Explanation: Subarray [5,5] has maximum sum 5 + 5 = 10.
Example 3:
Input: nums = [-3,-2,-3]
Output: -2
Explanation: Subarray [-2] has maximum sum -2.

Constraints:
 n == nums.length
 1 <= n <= 3 * 104
 -3 * 104 <= nums[i] <= 3 * 104
CODE:
class Solution {
N.KARTHIKEYA
VU21CSEN0101813

public int maxSubarraySumCircular(int[] A) {


int nonCircularSum = kadaneMaxSum(A);
int totalSum = 0;
for(int i=0;i<A.length;i++){
totalSum += A[i];
A[i] = -A[i];
}

int circularSum = totalSum + kadaneMaxSum(A);


if(circularSum == 0)
return nonCircularSum;
return Math.max(circularSum,nonCircularSum);
}

int kadaneMaxSum(int[] A){


int currentSum = A[0];
int maxSum = A[0];
for(int i=1;i<A.length;i++){
if(currentSum < 0)
currentSum = 0;
currentSum = A[i] + currentSum;
maxSum = Math.max(maxSum,currentSum);
}
return maxSum;
}
SUBMISSION RESULT:
N.KARTHIKEYA
VU21CSEN0101813

TEST CASE 1
N.KARTHIKEYA
VU21CSEN0101813

TEST CASE2

TEST CASE 3
N.KARTHIKEYA
VU21CSEN0101813

QUESTION2
You are given two strings stamp and target. Initially, there is a string s of
length target.length with all s[i] == '?'.
In one turn, you can place stamp over s and replace every letter in the s with the
corresponding letter from stamp.
 For example, if stamp = "abc" and target = "abcba",
then s is "?????" initially. In one turn you can:
 place stamp at index 0 of s to obtain "abc??",
 place stamp at index 1 of s to obtain "?abc?", or
 place stamp at index 2 of s to obtain "??abc".
Note that stamp must be fully contained in the boundaries of s in order to stamp
(i.e., you cannot place stamp at index 3 of s).
We want to convert s to target using at most 10 * target.length turns.
Return an array of the index of the left-most letter being stamped at each turn. If
we cannot obtain target from s within 10 * target.length turns, return an empty
array.

Example 1:
Input: stamp = "abc", target = "ababc"
Output: [0,2]
Explanation: Initially s = "?????".
- Place stamp at index 0 to get "abc??".
N.KARTHIKEYA
VU21CSEN0101813

- Place stamp at index 2 to get "ababc".


[1,0,2] would also be accepted as an answer, as well as some other answers.
Example 2:
Input: stamp = "abca", target = "aabcaca"
Output: [3,0,1]
Explanation: Initially s = "???????".
- Place stamp at index 3 to get "???abca".
- Place stamp at index 0 to get "abcabca".
- Place stamp at index 1 to get "aabcaca".

Constraints:
 1 <= stamp.length <= target.length <= 1000
 stamp and target consist of lowercase English letters.
CODE
class Solution {
public int[] movesToStamp(String stamp, String target) {
char[] S = stamp.toCharArray();
char[] T = target.toCharArray();
List<Integer> res = new ArrayList<>();
boolean[] visited = new boolean[T.length];
int stars = 0;

while (stars < T.length) {


boolean doneReplace = false;
for (int i = 0; i <= T.length - S.length; i++) {
if (!visited[i] && canReplace(T, i, S)) {
stars = doReplace(T, i, S.length, stars);
doneReplace = true;
visited[i] = true;
res.add(i);
if (stars == T.length) {
break;
N.KARTHIKEYA
VU21CSEN0101813

}
}
}

if (!doneReplace) {
return new int[0];
}
}

int[] resArray = new int[res.size()];


for (int i = 0; i < res.size(); i++) {
resArray[i] = res.get(res.size() - i - 1);
}
return resArray;
}

private boolean canReplace(char[] T, int p, char[] S) {


for (int i = 0; i < S.length; i++) {
if (T[i + p] != '*' && T[i + p] != S[i]) {
return false;
}
}
return true;
}

private int doReplace(char[] T, int p, int len, int count) {


for (int i = 0; i < len; i++) {
if (T[i + p] != '*') {
T[i + p] = '*';
count++;
}
}
N.KARTHIKEYA
VU21CSEN0101813

return count;
}
}
SUBMISSION RESULT

TEST CASE1
N.KARTHIKEYA
VU21CSEN0101813

TESTCASE2
N.KARTHIKEYA
VU21CSEN0101813

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy