[자바스크립트/알고리즘] Leetcode - 부분 배열의 최대합
[문제] 53. Maximum Subarray Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. [예시] Input Output [-2,1,-3,4,-1,2,1,-5,4] 6 [1] 1 [5,4,-1,7,8] 23 [풀이] var maxSubArray = function(nums) { for(let i = 1; i < nums.length; i++){ nums[i] = Math.max(nums[i], nums[i]+nums[i-1]); } return Math.max(...nums) }; 반복문을 돌면서 nums..
2021. 3. 13.
[자바스크립트/알고리즘] LeetCode - 정렬된 두 리스트 합치기
[문제] 21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a sorted list. The list should be made by splicing together the nodes of the first two lists. [예시] Input Output l1 = [1,2,4], l2 = [1,3,4] [1,1,2,3,4,4] l1 = [], l2 = [] [] l1 = [], l2 = [0] [0] [풀이] /** * Definition for singly-linked list. * function ListNode(val, next) { * this.val = (val===undefined ? 0 : val) * th..
2021. 3. 7.