깊은바다거북
개발 공부 기록
깊은바다거북
전체 방문자
오늘
어제
  • 분류 전체보기 (219)
    • JAVA (9)
    • JavaScript (15)
    • 스파르타코딩클럽 (11)
      • [내일배움단] 웹개발 종합반 개발일지 (5)
      • [내일배움캠프] 프로젝트와 트러블 슈팅 (6)
    • SQL | NoSQL (4)
    • CS 등등 (0)
    • TIL | WIL (173)
    • 기타 에러 해결 (3)
    • 내 살 길 궁리 (4)

인기 글

최근 글

최근 댓글

태그

  • Backtracking(백트래킹)
  • 팀 프로젝트
  • 01. 미니 프로젝트
  • 재귀 함수
  • 프로그래머스
  • DFS(깊이우선탐색)
  • Leetcode
  • Linked List
  • TypeScript
  • Preorder Traversal(전위 순회)
  • 점화식(Recurrence Relation)
  • 트러블 슈팅 Troubleshooting
  • POST / GET 요청
  • 혼자 공부하는 자바스크립트
  • BFS(너비우선탐색)
  • 자료 구조
  • 코딩테스트 연습문제
  • Binary Tree(이진 트리)
  • BST(이진 탐색 트리)
  • 최소 힙(Min Heap)
  • TIT (Today I Troubleshot)
  • Trie
  • Inorder Traversal(중위 순회)
  • tree
  • Til
  • 최대 힙(Max Heap)
  • 시간 복잡도
  • leetcode-cli
  • 자바스크립트 기초 문법
  • 자잘한 에러 해결
hELLO · Designed By 정상우.
깊은바다거북

개발 공부 기록

TIL | WIL

11/3 (금) 합이 가장 큰 부분 배열 TIL

2023. 11. 3. 20:31

공부한 것

  • LeetCode #53. Maximum Subarray
    Maximum Subarray - LeetCode
    Can you solve this real interview question? Maximum Subarray - Given an integer array nums, find the subarray with the largest sum, and return its sum.   Example 1: Input: nums = [-2,1,-3,4,-1,2,1,-5,4] Output: 6 Explanation: The subarray [4,-1,2,1] has the largest sum 6. Example 2: Input: nums = [1] Output: 1 Explanation: The subarray [1] has the largest sum 1. Example 3: Input: nums = [5,4,-1,7,8] Output: 23 Explanation: The subarray [5,4,-1,7,8] has the largest sum 23.   Constraints: * 1 <= nums.length <= 105 * -104 <= nums[i] <= 104   Follow up: If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
    https://leetcode.com/problems/maximum-subarray/description/
    • 가장 신기하고 간결하다고 생각된 풀이를 첨부한다.
    • 해답 코드:
      // Time complexity: O(N)
      // Space complexity: O(1) 
      function maxSubArray3(nums: number[]): number {
      	let localSum = 0;
      	let maxSum = -Infinity;
      
      	for (const num of nums) {
      		// 지금 수와 지금 수를 부분 합계에 더한 값 중 더 큰 쪽을 새로운 부분 합계로 삼는다. 
      		localSum = Math.max(num, localSum + num);
      		// 만약 새로운 부분 합계가 최고 합계보다 더 크면 최고 합계를 업데이트한다. 
      		if (localSum > maxSum) {
      			maxSum = localSum;
      		}
      	}
      
      	return maxSum;
      }


Uploaded by N2T

    'TIL | WIL' 카테고리의 다른 글
    • 12/5 (화) D3.js로 그래프 자료구조 시각화하기 TIL
    • 11/14 (화) 대소문자를 모두 가지는 최장 부분 문자열 구하기 TIL
    • 11/2 (목) 쿼리를 포함하기 위한 최소 인터벌 TIL
    • 10/28 (토) (완료)인터벌이 안 겹치도록 삭제해야 하는 최소 인터벌 개수 TIL
    깊은바다거북
    깊은바다거북

    티스토리툴바