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 f541a22

Browse files
committed
day-15
1 parent 80a0a6c commit f541a22

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

‎README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@
1414
- Day 11: [Flood Fill](https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-eleven/index.ts) :raising_hand:
1515
- Day 12: [Single Element in a Sorted Array](https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-twelve/index.ts) :hear_no_evil:
1616
- Day 13: [Remove K Digits](https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-thirteen/index.ts) :tongue:
17-
- Day 14: [Implement Trie (Prefix Tree)](https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-thirteen/index.ts) :tongue:
17+
- Day 14: [Implement Trie (Prefix Tree)](https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-thirteen/index.ts) :pig_nose:
18+
- Day 15: [Maximum Sum Circular Subarray](https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-thirteen/index.ts) :whale:

‎src/may/day-fifteen/index.ts‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @param {number[]} A
3+
* @return {number}
4+
*/
5+
6+
let filterPostitives = (arr: number[]) => arr.every((i) => i > 0);
7+
let filterNegatives = (arr: number[]) => arr.every((i) => i < 0);
8+
9+
var maxSubarraySumCircular = function (A: number[]): number {
10+
if (A.length === 0 || filterNegatives(A)) return A.sort((a, b) => b - a)[0];
11+
if (filterNegatives(A)) return A.reduce((a, b) => a + b, 0);
12+
13+
let i: number = 0;
14+
let max_kadane: number = kadane(A);
15+
let max_wrap: number = 0;
16+
17+
while (i < A.length) {
18+
max_wrap += A[i];
19+
A[i] = -A[i];
20+
i++;
21+
}
22+
23+
max_wrap = max_wrap + kadane(A);
24+
return max_wrap > max_kadane ? max_wrap : max_kadane;
25+
};
26+
27+
const kadane = (A: number[]): number => {
28+
let i: number = 0;
29+
let cur_max: number = 0;
30+
let max_so_far: number = 0;
31+
32+
while (i < A.length) {
33+
cur_max = Math.max(0, cur_max + A[i]);
34+
max_so_far = Math.max(cur_max, max_so_far);
35+
i++;
36+
}
37+
38+
return max_so_far;
39+
};
40+
41+
const sd = maxSubarraySumCircular([-3, -2, -1]);
42+
const sd1 = maxSubarraySumCircular([3, -1, 2, -1]);
43+
const sd2 = maxSubarraySumCircular([5, -3, 5]);
44+
45+
console.log('a', sd);
46+
console.log('a', sd1);
47+
console.log('a', sd2);

0 commit comments

Comments
(0)

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