TIL & Practice/Coding Practice 18

연속된 부분 배열의 최대합 (Kadane's Algorithm)

네!! 저는 요즘 ai랑 코드 테스트하는 재미에 푹 빠져있습니다!!!(누구한테 말하냐..?) 카다인 알고리즘으로 푸는 연속 부분 배열 최대 합 문제 설명정수로 이루어진 배열 nums가 주어졌을 때, 연속된 부분 배열 중 가장 큰 합을 구하세요.예시Input: [-2, 1, -3, 4, -1, 2, 1, -5, 4]Output: 6 "연속된 부분 배열 중 가장 큰 합을 구하시오."— 아니 나만 헷갈려? 연속된 부분 배열이라니… 연속된 숫자를 찾으라는 건가? 도대체 배열을 만들어야 하나? 문제 설명이 좀 모호한거 같다고 그래서 ai한테 따져 물었더니 이렇게 말했다. 사실 문제의 진짜 의도는 이거야:“주어진 배열 안에서, 숫자들이 연속해서 붙어 있는 모든 구간 중에서, 합이 제일 큰 구간을 찾아라.”예를 ..

코딩 테스트 자주 나오는 문제: 두 수의 합 (Two Sum)

코딩 테스트 단골손님: 두 수의 합 (Two Sum)면접관이 좋아하는 문제 Top 3 안에 반드시 들어가는 두 수의 합!(근거 없음.)심지어 파이썬, 자바스크립트, 자바, C++ 가리지 않고 나오는 무적의 인기 문제다. 이걸 몰라? 그럼 면접관이 속으로 "이 친구는 기본기가 부족하구만..." 할지도 모른다 🤔 (이 또한 근거없음.) 문제 설명정수 배열 nums와 정수 target이 주어졌을 때, 배열에서 두 수의 합이 target이 되는 인덱스를 찾아 반환하시오. 단, 딱 한 쌍만 존재한다는 전제가 있다 (즉, 여러 쌍 없음!). 입력 예시nums = [2, 7, 11, 15], target = 9// 출력: [0, 1] (2 + 7 = 9) 1. 순수 근성 풀이: 이중 for문음...... 그냥..

[codewars - 5kyu] Tic-Tac-Toe Checker

Instructions. If we were to set up a Tic-Tac-Toe game, we would want to know whether the board's current state is solved, wouldn't we? Our goal is to create a function that will check that for us! 틱택토 게임을 만들었다면, 보드의 현재 상태가 해결됐는지 여부를 알고 싶지 않은가? 우리의 목표는 그것을 확인할 함수를 만들어내는 것이다. Assume that the board comes in the form of a 3x3 array, where the value is 0 if a spot is empty, 1 if it is an "X", or 2 if..

[codewars] Simple Pig Latin

Instruction. Move the first letter of each word to the end of it, then add 'ay' to the end of the word. Leave punctuation marks untouched. 각 단어의 첫번째 글자를 단어의 마지막으로 옮기고 그 뒤에 'ay'를 추가해라. 부호는 변경하지 마라. Example. pigIt('Pig latin is cool'); // igPay atinlay siay oolcay pigIt('Hello world !'); // elloHay orldway ! Solution. function pigIt(str){ let result = []; let arr = str.split(' '); for(let word of ..

[codewars] Moving Zeros To The End

Instruction. Write an algorithm that takes an array and moves all of the zeros to the end, preserving the order of the other elements. 배열 요소 중 0을 제외한 나머지 요소의 순서를 유지하면서, 배열에 포함된 0을 모두 마지막으로 이동 시키는 알고리즘을 완성하라. Example. moveZeros([false, 1, 0, 1, 2, 0, 1, 3, "a"]) // returns [false, 1, 1, 2, 1, 3, "a", 0, 0] Solution. function moveZeros(arr) { return arr.filter(function(x) {return x !== 0}) .concat..

[codewars] Count characters in your string

Instructions. The main idea is to count all the occurring characters in a string. If you have a string like aba, then the result should be {'a': 2, 'b': 1}. What if the string is empty? Then the result should be empty object literal, {} 중요한 것은 모든 숫자열의 숫자를 세는 것이다. 예를들어 aba 라는 문자열이 있을 경우, {'a': 2, 'b':1} 이라는 산출물을 가지게 될 것이다. 빈 문자열의 경우 빈 객체 리터럴을 반환해야 한다. Solution. function count(string) { const arr ..

[codewars] Human Readable Time

Instruction. Write a function, which takes a non-negative integer(seconds) as input and returns the time in a human readable format(HH:MM:SS) HH = hours, padded to 2 digits, range: 00- 99 MM = Minutes, padded to 2 digits, range: 00-59 SS = seconds, padded to 2 digits, range: 00-59 The maximum time never exceeds 359999(99:59:59) You can find some examples in the test fixtures. 음이 아닌 정수(초)를 입력받아 사..