Dev.Beth
πŸπŸ’»πŸ
Dev.Beth
전체 방문자
였늘
μ–΄μ œ
  • λΆ„λ₯˜ 전체보기 (175)
    • πŸ€” PS(Problem Solving) (119)
      • λ°±μ€€(BOJ) (59)
      • ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ (47)
      • Leet, ꡬ름 (6)
      • μ½”ν…Œ (7)
    • πŸ› οΈ 툴, κ·Έμ™Έ (10)
    • πŸ•·οΈ μ—λŸ¬, 버그 (15)
    • ✍️ 이둠 (30)
      • 이둠, 섀계 (3)
      • λ””μžμΈνŒ¨ν„΄ (1)
      • 자료ꡬ쑰, μ•Œκ³ λ¦¬μ¦˜ (13)
      • λ„€νŠΈμ›Œν¬, λ°μ΄ν„°λ² μ΄μŠ€ (11)
      • κ°œλ°œμ„œ (2)

λΈ”λ‘œκ·Έ 메뉴

  • WRITE
  • ADMIN

곡지사항

  • 🍡 PS challenge

인기 κΈ€

νƒœκ·Έ

  • κ°€λŸ­μ‹œ κ°•μ œ μž¬λΆ€νŒ…
  • boj 2293
  • 2294 λ°±μ€€ c++
  • μ‚Όμ„± ν™”λ©΄ 멈좀
  • κ°€λŸ­μ‹œ λ¦¬λΆ€νŒ…
  • 1520 c++
  • λ°±μ€€ c++ 2293
  • λ°±μ€€ 2240
  • λ°±μ€€ 1509 c++
  • κ°€λŸ­μ‹œ 검은화면 μž¬λΆ€νŒ…
  • Retrofit ν•œκΈ€κΉ¨μ§
  • κ°€λŸ­μ‹œ 멈좀
  • 2294
  • c++ 2294
  • 2293
  • λ°±μ€€ 1520 c++
  • λ°±μ€€ c++
  • boj 1509 c++
  • ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ λ„€νŠΈμ›Œν¬ java
  • λ°±μ€€ 2294 c++
  • Retrofit Post ν•œκΈ€
  • 2293 c++
  • Retrofit ν•œκΈ€ 깨짐
  • 1509 c++
  • 2294 c++
  • λ°±μ€€ 2293
  • κ°€λŸ­μ‹œ 검은화면
  • λ°±μ€€
  • λ°±μ€€ 2294
  • κ°€λŸ­μ‹œ μž¬λΆ€νŒ…

졜근 λŒ“κΈ€

졜근 κΈ€

ν‹°μŠ€ν† λ¦¬

hELLO Β· Designed By μ •μƒμš°.
Dev.Beth

πŸπŸ’»πŸ

πŸ€” PS(Problem Solving)/Leet, ꡬ름

[λ§€μΌν”„λ‘œκ·Έλž˜λ°] μ½”λ”©ν…ŒμŠ€νŠΈ 07/08/2019

2019. 7. 9. 22:50
λ°˜μ‘ν˜•

문제 


μ •μˆ˜ λ°°μ—΄(int array)κ°€ μ£Όμ–΄μ§€λ©΄ κ°€μž₯ 큰 μ΄μ–΄μ§€λŠ” μ›μ†Œλ“€μ˜ 합을 κ΅¬ν•˜μ‹œμ˜€. 단, μ‹œκ°„λ³΅μž‘λ„λŠ” O(n).

Given an integer array, find the largest consecutive sum of elements.


예제}

Input: [-1, 3, -1, 5]

Output: 7 // 3 + (-1) + 5


Input: [-5, -3, -1]

Output: -1 // -1


Input: [2, 4, -2, -3, 8]

Output: 9 // 2 + 4 + (-2) + (-3) + 8



λ‚΄κ°€ 생각해본 풀이과정 


1.κ·œμΉ™

  • μ‹œκ°„ λ³΅μž‘λ„λŠ” O(n)μž…λ‹ˆλ‹€.
  • λ”ν•΄μ§€λŠ” μ›μ†ŒλŠ” λ°˜λ“œμ‹œ μΈλ±μŠ€κ°€ 이어져 μžˆμ–΄μ•Ό ν•©λ‹ˆλ‹€. (arr[i] + arr[i+3] 같은 κ²½μš°λŠ” X)


2.μˆœμ„œ


μ‹œκ°„λ³΅μž‘λ„κ°€ O(n)μ΄λ‹ˆ 2쀑 for문은 κ³ λ €ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

λ°°μ—΄μ˜ 총 ν•©κ³„κ°’μœΌλ‘œ μ΄ˆκΈ°ν™”ν•œ answerλ³€μˆ˜μ™€, μΈλ±μŠ€ iλ²ˆμ§ΈλΆ€ν„° n-1λ²ˆμ§ΈκΉŒμ§€μ˜ 합이 μžˆμ„λ•Œ

두 값을 λΉ„κ΅ν•˜μ—¬ 큰 값을 answer에 λ„£μŠ΅λ‹ˆλ‹€.

λ°°μ—΄μ˜ 길이만큼(n) μ΄ μž‘μ—…μ„ λ°˜λ³΅ν•œ ν›„ λ¦¬ν„΄ν•©λ‹ˆλ‹€.


3.μ½”λ“œ(μ‹œκ°„λ³΅μž‘λ„ O(n)?)


int solution(list<int> arr){
    int *memo = new int[arr.size() + 1];
    int answer = 0, sum = 0;
    list<int>::iterator it;
    for (it = arr.begin(); it != arr.end(); it++) sum += *it;
    while (!arr.empty()) {
        answer = max((sum -= arr.front()), answer);
        arr.pop_front();
    }
    delete[] memo;
    return answer;
}
Colored by Color Scripter
cs


ν•΄μ„€κ³Ό μ •λ‹΅μ½”λ“œ 


1.ν•΄μ„€


이 λ¬Έμ œλŠ” λ‘κ°œμ˜ μ •μˆ˜ λ³€μˆ˜λ‘œ ν˜„μž¬μ˜ ν•©(currentSum) κ³Ό μ „μ²΄μ˜ 제일 큰 ν•©(max Sum)을 μ €μž₯ν•΄μ•Ό ν•©λ‹ˆλ‹€.

각 μ›μ†Œλ§ˆλ‹€ (currentSum + μ›μ†Œ) 값을 μ›μ†Œ κ°’μ΄λž‘ λΉ„κ΅ν•˜κ³ , 더 큰 값이 currentSum이 λ©λ‹ˆλ‹€.

maxSum은 currentSum 이 λ°”λ€”λ•Œλ§ˆλ‹€ μ²΄ν¬ν•˜μ—¬ 제일 큰 값을 μ €μž₯ν•˜λ©΄ λ©λ‹ˆλ‹€.


2.μ½”λ“œ(μ‹œκ°„ λ³΅μž‘λ„: O(n) , κ³΅κ°„ λ³΅μž‘λ„: O(1))


int solution(int[] arr) {
  int maxSum = arr[0];
  int currentSum = arr[0];
  for(int i = 1; i < arr.length; i++) {
    currentSum = Math.max(currentSum + arr[i], arr[i]);
    maxSum = Math.max(currentSum, maxSum);
  }
  return maxSum;
}
Colored by Color Scripter
cs


λ°˜μ‘ν˜•
μ €μž‘μžν‘œμ‹œ λΉ„μ˜λ¦¬ 동일쑰건 (μƒˆμ°½μ—΄λ¦Ό)

'πŸ€” PS(Problem Solving) > Leet, ꡬ름' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

[LeetCode] 1108번 - Defanging an IP Address(IPμ£Όμ†Œ λͺ»μ“°κ²Œ λ§Œλ“€κΈ°)  (0) 2019.07.09
[LeetCode] 4번 - Median of Two Sorted Arrays(두 μ •λ ¬λœ λ°°μ—΄μ˜ 쀑앙값)  (0) 2019.07.09
[LeetCode] 832번 - Flipping an Image(상 λ’€μ§‘κΈ°)  (0) 2019.07.07
[LeetCode] 905번 - Sort Array By Parity(홀짝성 μ—¬λΆ€λ‘œ λ°°μ—΄ μ •λ ¬)  (0) 2019.07.05
[LeetCode] 217번 - Contains Duplicate(쀑볡이 ν¬ν•¨λ˜μ–΄ μžˆλŠ”κ°€?)  (0) 2019.07.03
    'πŸ€” PS(Problem Solving)/Leet, ꡬ름' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€
    • [LeetCode] 1108번 - Defanging an IP Address(IPμ£Όμ†Œ λͺ»μ“°κ²Œ λ§Œλ“€κΈ°)
    • [LeetCode] 4번 - Median of Two Sorted Arrays(두 μ •λ ¬λœ λ°°μ—΄μ˜ 쀑앙값)
    • [LeetCode] 832번 - Flipping an Image(상 λ’€μ§‘κΈ°)
    • [LeetCode] 905번 - Sort Array By Parity(홀짝성 μ—¬λΆ€λ‘œ λ°°μ—΄ μ •λ ¬)
    Dev.Beth
    Dev.Beth
    Beth의 곡뢀 λΈ”λ‘œκ·Έ

    ν‹°μŠ€ν† λ¦¬νˆ΄λ°”