본문 바로가기

내 맘대로 알고리즘

(59)
Leetcode[day7] - Counting Elements Given an integer array arr, count element x such that x + 1 is also in arr. If there're duplicates in arr, count them seperately. Input: arr = [1,2,3] Output: 2 Explanation: 1 and 2 are counted cause 2 and 3 are in arr. Input: arr = [1,1,3,3,5,5,7,7] Output: 0 Explanation: No numbers are counted, cause there's no 2, 4, 6, or 8 in arr. Input: arr = [1,3,2,3,5,0] Output: 3 Explanation: 0, 1 and 2 ..
Leetcode[day6] - Group Anagrams Given an array of strings, group anagrams together. Note: All inputs will be in lowercase.The order of your output does not matter. Input: ["eat", "tea", "tan", "ate", "nat", "bat"] Output: [ ["ate","eat","tea"], ["nat","tan"], ["bat"] ] 1. 알고리즘을 해결하기 위해서 생각한 방법 - HashMap 자료구조를 사용한다. - Key는 "eat","tea","ate"를 하나로 묶을 수 있는 sort된 단어가 필요하다. - value는 list가 들어간다. - key에 대한 value가 있다면, 해당 value인 리스트를 가..
LeetCode[day5] - Best Time to Buy and Sell Stock II Say you have an array prices for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times). Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again). Input: [7,1,5,3,6,4]..
LeetCode[day4] - Move zeros Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. Note: 1. You must do this in-place without making a copy of the array.Minimize the total number of operations. 2. Minimize the total number of operations. Example: Input: [0,1,0,3,12] Output: [1,3,12,0,0] 1. 알고리즘을 해결하기 위해서 생각한 방법 - 문제의 조건에서는 별도의 다른 array 공간을 만들면 안..
LeetCode[day3] - 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. 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. Example: Input: [-2,1,-3,4,-1,2,1,-5,4], Output: 6 Explanation: [4,-1,2,1] has the largest sum = 6. 1. 알고리즘을 해결하기 위해서 ..
LeetCode[day2] - Happy Number Write an algorithm to determine if a number n is "happy". A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are ha..
LeetCode[day1] - Single Number Given a non-empty array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? Example 1: Input: [2,2,1] Output: 1 Example 2: Input: [4,1,2,1,2] Output: 4 0. 내가 하고 싶은 것 Input은 주어진 숫자들에 대해서 쌍을 이뤄서 어떤 값들이 들어있다고 한다. 이 때, Stream 코드와 단순 코드로 해당 알고리즘을 구해보려고 한다. 1. 단순 ..
HashSet, TreeSet, LinkedHashSet 예제 with Java -1. 이전글 2018/12/31 - [내 맘대로 알고리즘] - Java ArrayList, Vector 예제2019/01/01 - [내 맘대로 알고리즘] - Java Queue, Priority Queue 예제 0.Set Set은 데이터의 중복된 값이 없는 자료구조입니다.따라서,AABBBCCDD의 값이 순사적으로 입력되더라도ABCD의 값만 남아있게 되는 자료구조입니다. https://developer.android.com/reference/java/util/Set Android Developer에는 중복 요소가없는 컬렉션입니다. 이 인터페이스는 이름에서 알 수 있듯이 수학적 집합 추상을 모델링합니다. 라고 적혀있습니다. 1. HashSet HashSet은 기본적인 Set의 구조를 갖고 있는 자료구조입니..