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 155b765

Browse files
p190
1 parent f8e9343 commit 155b765

File tree

5 files changed

+57
-2
lines changed

5 files changed

+57
-2
lines changed

‎AllQuestions.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3018,7 +3018,7 @@ its elements are distinct.
30183018
For example, given the array [5, 1, 3, 5, 2, 3, 4, 1], return 5 as the longest
30193019
subarray of distinct elements is [5, 2, 3, 4, 1].
30203020

3021-
## Problem-190:waxing_crescent_moon:
3021+
## [Problem-190](src/main/java/in/ashwanik/dcp/problems/p181_210/p190):sunny:
30223022

30233023

30243024
> This problem was asked by Facebook.

‎README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ Solutions to the coding problems from [Daily coding problem](https://dailycoding
1313
|[P165](src/main/java/in/ashwanik/dcp/problems/p151_180/p165)|[P169](src/main/java/in/ashwanik/dcp/problems/p151_180/p169)|[P175](src/main/java/in/ashwanik/dcp/problems/p151_180/p175)|[P179](src/main/java/in/ashwanik/dcp/problems/p151_180/p179)|[P180](src/main/java/in/ashwanik/dcp/problems/p151_180/p180)|[P181](src/main/java/in/ashwanik/dcp/problems/p181_210/p181)|[P189](src/main/java/in/ashwanik/dcp/problems/p181_210/p189)|
1414

1515

16-
## **Facebook (24)**
16+
## **Facebook (25)**
1717
| | | | | | | | | | | | |
1818
|--|--|--|--|--|--|--|--|--|--|--|--|
1919
|[P7](src/main/java/in/ashwanik/dcp/problems/p1_30/p7)|[P15](src/main/java/in/ashwanik/dcp/problems/p1_30/p15)|[P19](src/main/java/in/ashwanik/dcp/problems/p1_30/p19)|[P25](src/main/java/in/ashwanik/dcp/problems/p1_30/p25)|[P27](src/main/java/in/ashwanik/dcp/problems/p1_30/p27)|[P30](src/main/java/in/ashwanik/dcp/problems/p1_30/p30)|[P41](src/main/java/in/ashwanik/dcp/problems/p31_60/p41)|[P47](src/main/java/in/ashwanik/dcp/problems/p31_60/p47)|[P51](src/main/java/in/ashwanik/dcp/problems/p31_60/p51)|[P60](src/main/java/in/ashwanik/dcp/problems/p31_60/p60)|[P62](src/main/java/in/ashwanik/dcp/problems/p61_90/p62)|[P69](src/main/java/in/ashwanik/dcp/problems/p61_90/p69)|
2020
|[P79](src/main/java/in/ashwanik/dcp/problems/p61_90/p79)|[P85](src/main/java/in/ashwanik/dcp/problems/p61_90/p85)|[P110](src/main/java/in/ashwanik/dcp/problems/p91_120/p110)|[P114](src/main/java/in/ashwanik/dcp/problems/p91_120/p114)|[P117](src/main/java/in/ashwanik/dcp/problems/p91_120/p117)|[P126](src/main/java/in/ashwanik/dcp/problems/p121_150/p126)|[P130](src/main/java/in/ashwanik/dcp/problems/p121_150/p130)|[P134](src/main/java/in/ashwanik/dcp/problems/p121_150/p134)|[P156](src/main/java/in/ashwanik/dcp/problems/p151_180/p156)|[P168](src/main/java/in/ashwanik/dcp/problems/p151_180/p168)|[P170](src/main/java/in/ashwanik/dcp/problems/p151_180/p170)|[P182](src/main/java/in/ashwanik/dcp/problems/p181_210/p182)|
21+
|[P190](src/main/java/in/ashwanik/dcp/problems/p181_210/p190)|
2122

2223

2324
## **Amazon (17)**
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Given a circular array, compute its maximum subarray sum in O(n) time.
2+
3+
For example, given [8, -1, 3, 4], return 15 as we choose the numbers 3, 4, and 8
4+
where the 8 is obtained from wrapping around.
5+
6+
Given [-4, 5, 1, 0], return 6 as we choose the numbers 5 and 1.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package in.ashwanik.dcp.problems.p181_210.p190;
2+
3+
class Solution {
4+
///https://linlaw0229.github.io/2018/10/22/918-Maximum-Sum-Circular-Subarray/
5+
int maxSubArraySum(int[] array) {
6+
if (array == null || array.length == 0) {
7+
return -1;
8+
}
9+
10+
int maxKadane = maxSubArraySumHelper(array);
11+
if (maxKadane < 0) {
12+
return maxKadane;
13+
}
14+
int sum = 0;
15+
16+
for (int index = 0; index < array.length; index++) {
17+
sum += array[index];
18+
array[index] = -array[index];
19+
}
20+
int maxWrap = sum + maxSubArraySumHelper(array);
21+
return Math.max(maxKadane, maxWrap);
22+
}
23+
24+
private int maxSubArraySumHelper(int[] array) {
25+
int max = array[0];
26+
int currentSum = array[0];
27+
28+
for (int index = 1; index < array.length; index++) {
29+
int currentIndex = index % array.length;
30+
currentSum = Math.max(array[currentIndex], currentSum + array[currentIndex]);
31+
max = Math.max(max, currentSum);
32+
}
33+
return Math.max(currentSum, max);
34+
}
35+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package in.ashwanik.dcp.problems.p181_210.p190;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
7+
public class SolutionTest {
8+
@Test
9+
void testMaxSubArraySumForCircularArray() {
10+
assertEquals(15, new Solution().maxSubArraySum(new int[]{8, -1, 3, 4}));
11+
assertEquals(6, new Solution().maxSubArraySum(new int[]{-4, 5, 1, 0}));
12+
}
13+
}

0 commit comments

Comments
(0)

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