Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
OneDay-OneAlgorithm
LeeJoongGon
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
LeeJoongGon
/
SlidingWindow
/
maxSubArraySum.ts
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
43 lines (34 loc) · 1.62 KB
main
Breadcrumbs
LeeJoongGon
/
SlidingWindow
/
maxSubArraySum.ts
Copy path
Top
File metadata and controls
Code
Blame
43 lines (34 loc) · 1.62 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Problem
// 배열의 n 개의 연속된 요소들의 합의 최대값을 계산하는 `maxSubArraySum` 함수를 작성하라.
// Input
// 정수 배열 `array`, 음수가 아닌 정수 `n`
// Output
// 배열 `array`의 n개의 연속된 요소들의 합의 최대값 `max`
// `array`의 길이가 n보다 작을 경우 null 반환
// `array`의 길이가 n보다 작을 경우 null 반환
// 윈도우의 양 끝을 가리키는 변수 `left`, `right` 생성
// 윈도우 내 요소들의 합을 나타내는 변수 `sum` 생성
// 배열의 n개의 연속된 요소들으 합의 최대값을 나타내는 변수 `max` 생성
// 윈도우를 배열의 왼쪽 끝부터 시작하도록 초기화하고 `sum`, `max`를 윈도우 내 요소들의 합으로 초기화
// 윈도우가 오른쪽 끝에 도달할 때까지 윈도우를 오른쪽으로 이동시키면서 반복
/// `sum`에서 이동 전 윈도우의 왼쪽 끝 요소를 빼고 이동 후 윈도우의 오른쪽 끝 요소를 더하여 `sum`을 업데이트
/// 업데이트된 `sum`이 기존의 `max` 값보다 크다면 `max` 값을 업데이트
// 반복이 종료되었다면 `max` 반환
const maxSubArraySum = (array: number[], n: number) => {
if (array.length < n) {
return null;
}
let left = 0;
let right = n - 1;
// eslint-disable-next-line no-param-reassign, no-return-assign
let sum = array.slice(0, n).reduce((acc, cur) => (acc += cur), 0);
let max = sum;
while (right < array.length - 1) {
left += 1;
right += 1;
sum = sum - array[left - 1] + array[right];
max = Math.max(max, sum);
}
return max;
};
export default maxSubArraySum;
You can’t perform that action at this time.