본문 바로가기

CS/Algorithm17

[LeetCode] 26. Remove Duplicates from Sorted Array 06.28.20240. Before We BeginI write my code even though my code is ridiculous as is the question just in case someone wonders what the heck is going on and why the judging seems odd.1. Problem2. My Code/** * @param {number[]} nums * @return {number} */var removeDuplicates = function(nums) { const s = new Set(nums); const result = [...s]; const k = nums.length; nums.length = 0; res.. 2024. 6. 28.
[LeetCode] 169. Majority Element 06.27.2024 1. Problem2. My Code/** * @param {number[]} nums * @return {number} */var majorityElement = function(nums) { var obj = Object(); nums.forEach((e) => { if (obj[e]) obj[e]++; else obj[e] = 1; }); let max = 0; Object.entries(obj).forEach((pair) => { const standard = parseInt(nums.length / 2); if (pair[1] > standard) max = pair[0]; }); .. 2024. 6. 27.
[LeetCode] 78. Subsets 01.29.2024 1. Problem 2. My code var subsets = function(nums) { let result = []; for(let i=0; i 2024. 1. 29.
[LeetCode] 446. Arithmetic Slices II - Subsequence 01.07.2024 1. Problem 2. The Things I tried I tried backtracking or a recursive function. But I could not make it and also the quiality of code was bad too. 3. Solution 3-1. Code var numberOfArithmeticSlices = function(nums) { const n = nums.length; let total_count = 0; const dp = new Array(n).fill().map(() => new Map()); for (let i = 1; i < n; ++i) { for (let j = 0; j < i; ++j) { const diff = n.. 2024. 1. 8.
[LeetCode] 46. Permutations 01.03.2024 1. Problem 2. Before 2-1. My Code /** * @param {number[]} nums * @return {number[][]} */ var permute = function(nums) { const len = nums.length; let result = []; result.push(nums); result.push(nums.reverse()); for(var i = 0; i < len; i++ ) { for(var j = 0; j < len-1; j++){ nums[j+1], nums[j] = nums[j], nums[j+1]; if (!(nums in result)) result.push(nums); } }; console.log(result); retu.. 2024. 1. 3.
1912 연속합 with 파이썬 (feat. DP, 동적 프로그래밍) 08.18.2023 1. 문제 시간 제한 메모리 제한 제출 정답 맞힌 사람 정답 비율 1 초 (추가 시간 없음) 128 MB 129671 48065 34077 35.739% 문제 n개의 정수로 이루어진 임의의 수열이 주어진다. 우리는 이 중 연속된 몇 개의 수를 선택해서 구할 수 있는 합 중 가장 큰 합을 구하려고 한다. 단, 수는 한 개 이상 선택해야 한다. 예를 들어서 10, -4, 3, 1, 5, 6, -35, 12, 21, -1 이라는 수열이 주어졌다고 하자. 여기서 정답은 12+21인 33이 정답이 된다. 입력 첫째 줄에 정수 n(1 ≤ n ≤ 100,000)이 주어지고 둘째 줄에는 n개의 정수로 이루어진 수열이 주어진다. 수는 -1,000보다 크거나 같고, 1,000보다 작거나 같은 정수이다... 2023. 8. 18.