├── .gitignore ├── README.md ├── contents ├── 180520.md ├── 180527.md ├── 180603.md ├── 180610.md ├── 180617.md ├── 180624.md ├── 180701.md ├── 180708.md ├── 180715.md ├── 180826.md ├── 180902.md ├── images │ ├── Java-Collections-Framework.png │ ├── binary-tree.png │ ├── deque-method.png │ ├── graph-vs-tree.png │ ├── kruskal-example.png │ ├── prim-example.png │ └── tree.png └── samsung │ ├── 180916.md │ ├── 180923.md │ ├── 180930.md │ ├── 181028.md │ └── README.md ├── src ├── Main.java ├── dami │ ├── Solution.java │ ├── codility │ │ ├── Codility.java │ │ ├── lesson1 │ │ │ └── BinaryGap.java │ │ ├── lesson2 │ │ │ ├── CyclicRotation.java │ │ │ └── OddOccurrencesInArray.java │ │ ├── lesson3 │ │ │ ├── FrogJmp.java │ │ │ ├── PermMissingElem.java │ │ │ └── TapeEquilibrium.java │ │ ├── lesson4 │ │ │ ├── FrogRiverOne.java │ │ │ ├── MissingInteger.java │ │ │ ├── PermCheck.java │ │ │ └── review │ │ │ │ └── MaxCounters.java │ │ └── lesson5 │ │ │ ├── CountDiv.java │ │ │ └── review │ │ │ └── GenomicRangeQuery.java │ ├── codinginterview │ │ ├── Interview.java │ │ ├── array │ │ │ ├── Array3.java │ │ │ └── Array4.java │ │ ├── common │ │ │ ├── ListNode.java │ │ │ └── TreeNode.java │ │ ├── list │ │ │ ├── List1.java │ │ │ ├── List2.java │ │ │ ├── List3.java │ │ │ └── List4.java │ │ ├── queue │ │ │ ├── Queue1.java │ │ │ ├── Queue2.java │ │ │ ├── Queue3.java │ │ │ └── Queue4.java │ │ ├── stack │ │ │ ├── Stack1.java │ │ │ ├── Stack2.java │ │ │ ├── Stack3.java │ │ │ └── Stack4.java │ │ └── tree │ │ │ ├── Tree.java │ │ │ ├── Tree1.java │ │ │ ├── Tree1_1.java │ │ │ └── Tree2.java │ ├── hackerrank │ │ ├── HackerRank.java │ │ └── medium │ │ │ ├── Common_Child.java │ │ │ ├── Minimum_Swaps_2.java │ │ │ ├── Sherlock_and_the_Valid_String.java │ │ │ ├── Sorting_Comparator.java │ │ │ ├── Special_StringAgain.java │ │ │ ├── The_Time_in_Words.java │ │ │ └── review │ │ │ ├── Fraudulent_Activity_Notifications.java │ │ │ ├── Highest_Value_Palindrome.java │ │ │ └── New_Year_Chaos.java │ ├── leetcode │ │ ├── Leetcode.java │ │ ├── common │ │ │ ├── ListNode.java │ │ │ └── TreeNode.java │ │ ├── easy │ │ │ ├── Longest_Common_Prefix_14.java │ │ │ ├── Palindrome_Number_9.java │ │ │ ├── Reverse_Integer_7.java │ │ │ ├── Roman_to_Integer_13.java │ │ │ └── Two_Sum_1.java │ │ └── medium │ │ │ ├── Add_Two_Numbers_2.java │ │ │ ├── Combination_Sum_39.java │ │ │ ├── Container_With_Most_Water_11.java │ │ │ ├── Daily_Temperatures_739.java │ │ │ ├── Generate_Parentheses_22.java │ │ │ ├── Group_Anagrams_49.java │ │ │ ├── Kth_Smallest_Element_230.java │ │ │ ├── Letter_Combinations_17.java │ │ │ ├── Longest_Substring_3.java │ │ │ ├── Partition_Labels_763.java │ │ │ ├── Permutations_46.java │ │ │ ├── Remove_Nth_Node_19.java │ │ │ ├── SubrectangleQueries.java │ │ │ ├── Subsets_78.java │ │ │ ├── Three_Sum_15.java │ │ │ ├── Top_K_Frequent_Elements_347.java │ │ │ └── review │ │ │ ├── Binary_Tree_Inorder_94.java │ │ │ ├── Counting_Bits_338.java │ │ │ ├── Longest_Palindromic_Substring_5.java │ │ │ └── Queue_Reconstruction_406.java │ ├── programmers │ │ ├── Programmers.java │ │ ├── level1 │ │ │ └── 추억_점수.java │ │ ├── level2 │ │ │ ├── JadenCase_문자열_만들기.java │ │ │ ├── review │ │ │ │ ├── 일이사_나라의_숫자.java │ │ │ │ ├── 조이스틱.java │ │ │ │ └── 프렌즈4블록.java │ │ │ ├── 가장_큰_정사각형_찾기.java │ │ │ ├── 기능개발.java │ │ │ ├── 뉴스_클러스터링.java │ │ │ ├── 다음_큰_숫자.java │ │ │ ├── 더_맵게.java │ │ │ ├── 두_원_사이의_정수쌍.java │ │ │ ├── 땅따먹기.java │ │ │ ├── 멀쩡한_사각형.java │ │ │ ├── 문자열_압축.java │ │ │ ├── 방금그곡.java │ │ │ ├── 삼각_달팽이.java │ │ │ ├── 소수찾기.java │ │ │ ├── 수식_최대화.java │ │ │ ├── 숫자의_표현.java │ │ │ ├── 압축.java │ │ │ ├── 영어_끝말잇기.java │ │ │ ├── 예상_대진표.java │ │ │ ├── 오픈채팅방.java │ │ │ ├── 올바른_괄호.java │ │ │ ├── 요격_시스템.java │ │ │ ├── 이진_변환_반복하기.java │ │ │ ├── 점프와_순간_이동.java │ │ │ ├── 짝지어_제거하기.java │ │ │ ├── 최댓값과_최솟값.java │ │ │ ├── 최솟값_만들기.java │ │ │ ├── 카카오프렌즈_컬러링북.java │ │ │ ├── 카펫.java │ │ │ ├── 캐시.java │ │ │ ├── 타겟_넘버.java │ │ │ ├── 튜플.java │ │ │ ├── 파일명_정렬.java │ │ │ ├── 폰켓몬.java │ │ │ └── 피보나치_수.java │ │ ├── level3 │ │ │ ├── review │ │ │ │ ├── 등굣길.java │ │ │ │ ├── 셔틀버스.java │ │ │ │ └── 자물쇠와_열쇠.java │ │ │ ├── 단속카메라.java │ │ │ └── 정수_삼각형.java │ │ └── sql │ │ │ └── questions.sql │ └── questions │ │ ├── Line3.java │ │ ├── Line6.java │ │ ├── Questions.java │ │ ├── Task1.java │ │ ├── Task2.java │ │ ├── Task3.java │ │ ├── Task4.java │ │ └── Task5.java ├── delf │ ├── backjoon │ │ └── Q4963.java │ ├── dailyprogramming │ │ └── CT116.java │ ├── etc │ │ ├── NV01.java │ │ ├── appdevmatching2020 │ │ │ └── Solution.java │ │ ├── devmatching2021 │ │ │ ├── Solution1.java │ │ │ └── Solution2.java │ │ ├── karecoint2020 │ │ │ └── Solution01.java │ │ └── nhn │ │ │ ├── NHN2020_01.java │ │ │ ├── NHN2020_02.java │ │ │ └── NHN2020_03.java │ ├── hackerrank │ │ ├── ATaleofTwoStacks.java │ │ ├── Abbreviation.java │ │ ├── BalancedBrackets.java │ │ ├── Candies.java │ │ ├── CommonChild.java │ │ ├── DavisStaircase.java │ │ ├── FraudulentActivityNotifications.java │ │ ├── FrequencyQueries.java │ │ ├── HuffmanDecoding.java │ │ ├── IsThisABinarySearchTree.java │ │ ├── LargestRectangle.java │ │ ├── MaxArraySum.java │ │ ├── MinMaxRiddle.java │ │ ├── MinimumSwap2.java │ │ ├── MinimumTimeRequired.java │ │ ├── NewYearChaos.java │ │ ├── Pairs.java │ │ ├── PoisonousPlants.java │ │ ├── ReverseShuffleMerge.java │ │ ├── SherlockAndAnagram.java │ │ ├── SherlockAndTheValidString.java │ │ ├── SortComparator.java │ │ ├── SpecialStringAgain.java │ │ └── TripleSum.java │ ├── interviewguidemission │ │ ├── StackMission1.java │ │ └── StackMission2.java │ ├── oncoder │ │ ├── Challenge10.java │ │ ├── Challenge11.java │ │ ├── Challenge12.java │ │ ├── Challenge13.java │ │ ├── Challenge14.java │ │ ├── Challenge16.java │ │ ├── Challenge5.java │ │ ├── Challenge6.java │ │ └── Challenge7.java │ └── programmers │ │ ├── Solution12905.java │ │ ├── Solution12907.java │ │ ├── Solution12913.java │ │ ├── Solution12927.java │ │ ├── Solution12938.java │ │ ├── Solution12941.java │ │ ├── Solution12952.java │ │ ├── Solution12973.java │ │ ├── Solution12980.java │ │ ├── Solution12985.java │ │ ├── Solution12987.java │ │ ├── Solution17676.java │ │ ├── Solution17677.java │ │ ├── Solution17678.java │ │ ├── Solution17679.java │ │ ├── Solution17680.java │ │ ├── Solution17683.java │ │ ├── Solution17684.java │ │ ├── Solution17686.java │ │ ├── Solution1829.java │ │ ├── Solution1845.java │ │ ├── Solution42628.java │ │ ├── Solution42839.java │ │ ├── Solution42860.java │ │ ├── Solution42860_2.java │ │ ├── Solution42861.java │ │ ├── Solution42885.java │ │ ├── Solution42888.java │ │ ├── Solution42890.java │ │ ├── Solution42892.java │ │ ├── Solution42895.java │ │ ├── Solution42898.java │ │ ├── Solution43162.java │ │ ├── Solution43163.java │ │ ├── Solution43164.java │ │ ├── Solution43165.java │ │ ├── Solution43238.java │ │ ├── Solution49189.java │ │ ├── Solution49191.java │ │ ├── Solution60057.java │ │ ├── Solution60059.java │ │ ├── Solution62048.java │ │ ├── Solution64062.java │ │ ├── Solution64064.java │ │ ├── Solution64065.java │ │ ├── Solution67257.java │ │ ├── Solution67258.java │ │ ├── Solution68645.java │ │ ├── Solution68646.java │ │ ├── Solution68936.java │ │ ├── Solution70129.java │ │ └── skillcheck │ │ ├── SkillCheck0101.java │ │ ├── SkillCheck0102.java │ │ ├── SkillCheck0201.java │ │ ├── SkillCheck0202.java │ │ └── SkillCheck0302.java ├── doy │ ├── Solution.java │ ├── ds1 │ │ ├── Doy_Q10799.java │ │ ├── Doy_Q10828.java │ │ └── Doy_Q9012.java │ ├── etc │ │ └── Doy_Q1000.java │ ├── leetcode │ │ └── Two_Sum_1.java │ ├── math1_1 │ │ ├── Doy_Q10430.java │ │ ├── Doy_Q11005.java │ │ ├── Doy_Q1850.java │ │ ├── Doy_Q1934.java │ │ ├── Doy_Q2609.java │ │ ├── Doy_Q2745.java │ │ └── Doy_Q9613.java │ ├── math1_2 │ │ ├── Doy_Q11653.java │ │ ├── Doy_Q1676.java │ │ ├── Doy_Q1929.java │ │ ├── Doy_Q2089.java │ │ └── Doy_Q6588.java │ ├── math2_1 │ │ ├── Doy_Q10830.java │ │ ├── Doy_Q1629.java │ │ ├── Doy_Q1788.java │ │ ├── Doy_Q2749.java │ │ └── Doy_Q9471.java │ ├── math2_2 │ │ ├── Doy_Q10422.java │ │ ├── Doy_Q10972.java │ │ └── Doy_Q11689.java │ ├── samsung │ │ ├── Doy_Q14501.java │ │ ├── Doy_Q14502.java │ │ ├── Doy_Q14503.java │ │ ├── Doy_Q14888.java │ │ ├── Doy_Q14889.java │ │ ├── Doy_Q14890.java │ │ ├── Doy_Q14891.java │ │ ├── Doy_Q15683.java │ │ ├── Doy_Q15684.java │ │ ├── Doy_Q15685.java │ │ └── Doy_Q15686.java │ ├── sort │ │ ├── Doy_Q10814.java │ │ ├── Doy_Q10825.java │ │ ├── Doy_Q10989.java │ │ ├── Doy_Q11004.java │ │ ├── Doy_Q11650.java │ │ ├── Doy_Q11652.java │ │ └── Doy_Q2751.java │ └── tree │ │ └── Doy_Q1991.java └── hee │ ├── Solution.java │ ├── boj │ ├── Hee_Q1012.java │ ├── Hee_Q1094.java │ ├── Hee_Q12851.java │ ├── Hee_Q1600.java │ ├── Hee_Q1697.java │ ├── Hee_Q2309.java │ ├── Hee_Q2455.java │ ├── Hee_Q6603.java │ ├── datastructure1 │ │ ├── Hee_Q10799.java │ │ ├── Hee_Q10808.java │ │ ├── Hee_Q10809.java │ │ ├── Hee_Q10820.java │ │ ├── Hee_Q10824.java │ │ ├── Hee_Q10828.java │ │ ├── Hee_Q10845.java │ │ ├── Hee_Q10866.java │ │ ├── Hee_Q11655.java │ │ ├── Hee_Q11656.java │ │ ├── Hee_Q2743.java │ │ └── Hee_Q9012.java │ ├── datastructure2 │ │ ├── Hee_Q11279.java │ │ ├── Hee_Q11723.java │ │ ├── Hee_Q1717.java │ │ ├── Hee_Q1764.java │ │ ├── Hee_Q1927.java │ │ ├── Hee_Q3015.java │ │ ├── Hee_Q4195.java │ │ └── Hee_Q7785.java │ ├── etc │ │ └── Hee_Q1000.java │ ├── graph1 │ │ ├── Hee_Q10451.java │ │ ├── Hee_Q11724.java │ │ ├── Hee_Q1260.java │ │ ├── Hee_Q1707.java │ │ ├── Hee_Q2146.java │ │ ├── Hee_Q2178.java │ │ ├── Hee_Q2331.java │ │ ├── Hee_Q2667.java │ │ ├── Hee_Q4963.java │ │ ├── Hee_Q7576.java │ │ └── Hee_Q9466.java │ ├── graph2 │ │ ├── Hee_Q1516.java │ │ ├── Hee_Q1766.java │ │ ├── Hee_Q2056.java │ │ └── Hee_Q2252.java │ ├── math1_1 │ │ ├── Hee_Q10430.java │ │ ├── Hee_Q11005.java │ │ ├── Hee_Q1850.java │ │ ├── Hee_Q1934.java │ │ ├── Hee_Q2609.java │ │ ├── Hee_Q2745.java │ │ └── Hee_Q9613.java │ ├── math1_2 │ │ ├── Hee_Q11653.java │ │ ├── Hee_Q1676.java │ │ ├── Hee_Q1929.java │ │ ├── Hee_Q2004.java │ │ ├── Hee_Q2089.java │ │ └── Hee_Q6588.java │ ├── math2_1 │ │ ├── Hee_Q10830.java │ │ ├── Hee_Q1629.java │ │ ├── Hee_Q1788.java │ │ ├── Hee_Q2086.java │ │ ├── Hee_Q2749.java │ │ └── Hee_Q9471.java │ ├── math2_2 │ │ ├── Hee_Q10422.java │ │ ├── Hee_Q10972.java │ │ ├── Hee_Q10974.java │ │ ├── Hee_Q11401.java │ │ ├── Hee_Q11689.java │ │ └── Hee_Q1722.java │ ├── sort │ │ ├── Hee_Q10814.java │ │ ├── Hee_Q10825.java │ │ ├── Hee_Q10989.java │ │ ├── Hee_Q11004.java │ │ ├── Hee_Q11650.java │ │ ├── Hee_Q11652.java │ │ └── Hee_Q2751.java │ └── tree │ │ ├── Hee_Q1167.java │ │ ├── Hee_Q11725.java │ │ ├── Hee_Q1654.java │ │ ├── Hee_Q1967.java │ │ ├── Hee_Q1991.java │ │ ├── Hee_Q2110.java │ │ └── Hee_Q2805.java │ ├── codility │ ├── lesson │ │ ├── Arrays_CyclicRotation.java │ │ ├── Arrays_OddOccurrencesInArray.java │ │ ├── CountingElements_FrogRiverOne.java │ │ ├── CountingElements_MaxCounters.java │ │ ├── CountingElements_MissingInteger.java │ │ ├── CountingElements_PermCheck.java │ │ ├── Iterations_BinaryGap.java │ │ ├── PrefixSums_CountDiv.java │ │ ├── PrefixSums_GenomicRangeQuery.java │ │ ├── PrefixSums_MinAvgTwoSlice.java │ │ ├── PrefixSums_PassingCars.java │ │ ├── Sorting_Distinct.java │ │ ├── Sorting_MaxProductOfThree.java │ │ ├── Sorting_Triangle.java │ │ ├── StacksandQueues_Brackets.java │ │ ├── StacksandQueues_Fish.java │ │ ├── StacksandQueues_Nesting.java │ │ ├── TimeComplexity_FrogJmp.java │ │ ├── TimeComplexity_PermMissingElem.java │ │ └── TimeComplexity_TapeEquilibrium.java │ ├── naverfin │ │ ├── Solution1.java │ │ ├── Solution2.java │ │ └── Solution3.java │ ├── practice │ │ └── Practice1.java │ ├── w │ │ ├── Q1_1.java │ │ ├── Q1_2.java │ │ ├── Q1_3.sql │ │ ├── Q1_4.java │ │ └── Q1_5.java │ └── z │ │ ├── Test1.java │ │ ├── Test2.java │ │ ├── Test3.java │ │ └── Test4.java │ ├── leetcode │ ├── Counting_Bits_338.java │ ├── DefangIPaddr_1108.java │ ├── InorderTraversal_94.java │ ├── MaxIncreaseKeepingSkyline_807.java │ ├── NumJewelsInStones_771.java │ ├── PostorderTraversal_145.java │ ├── PreorderTraversal_144.java │ └── Two_Sum_1.java │ ├── nhn │ ├── Hee_1809_Q1.java │ ├── Hee_1809_Q3.java │ └── Hee_1809_Q4.java │ ├── samsung │ ├── Hee_Q13458.java │ ├── Hee_Q14499.java │ ├── Hee_Q14500.java │ ├── Hee_Q14501.java │ ├── Hee_Q14502.java │ ├── Hee_Q14503.java │ ├── Hee_Q14888.java │ ├── Hee_Q14889.java │ ├── Hee_Q14890.java │ ├── Hee_Q14891.java │ ├── Hee_Q15683.java │ ├── Hee_Q15684.java │ ├── Hee_Q15685.java │ ├── Hee_Q15686.java │ └── Hee_Q3190.java │ └── swexpert │ └── Hee_Q1247.java └── test └── hee └── codility ├── SolutionTest.java ├── lesson ├── Arrays_CyclicRotation_Test.java └── Arrays_OddOccurrencesInArray_Test.java ├── naverfin ├── Solution1Test.java ├── Solution2Test.java └── Solution3Test.java └── z ├── Test1Test.java ├── Test2Test.java ├── Test3Test.java └── Test4Test.java /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | hs_err_pid* 24 | 25 | # ETC 26 | .gitattributes 27 | .gitattributes/ 28 | .gitattributes/* 29 | */.gitattributes 30 | 31 | # IntelliJ 32 | out/ 33 | out/* 34 | .idea/ 35 | *.iml 36 | .idea/* 37 | *.idea 38 | .idea/workspace.xml 39 | .idea/tasks.xml 40 | .idea/gradle.xml 41 | .idea/assetWizardSettings.xml 42 | .idea/dictionaries 43 | .idea/libraries 44 | .idea/caches 45 | 46 | src/dami/codingtest 47 | -------------------------------------------------------------------------------- /contents/180902.md: -------------------------------------------------------------------------------- 1 | ## 그래프2(다익스트라(Dijkstra), 플로이드(Floyd-Warshall), SPFA(Shortest Path Faster)) 2 | 3 | ### 관련 문제들 4 | * [https://www.acmicpc.net/problem/1504](https://www.acmicpc.net/problem/1504) 5 | * [https://www.acmicpc.net/problem/1753](https://www.acmicpc.net/problem/1753) 6 | * [https://www.acmicpc.net/problem/11403](https://www.acmicpc.net/problem/11403) 7 | * [https://www.acmicpc.net/problem/11404](https://www.acmicpc.net/problem/11404) 8 | * [https://www.acmicpc.net/problem/11780](https://www.acmicpc.net/problem/11780) 9 | * [https://www.acmicpc.net/problem/1389](https://www.acmicpc.net/problem/1389) 10 | * [https://www.acmicpc.net/problem/1507](https://www.acmicpc.net/problem/1507) 11 | * [https://www.acmicpc.net/problem/1956](https://www.acmicpc.net/problem/1956) 12 | * [https://www.acmicpc.net/problem/11657](https://www.acmicpc.net/problem/11657) 13 | 14 | 15 | ### [issue]에 대한 정리 16 | #### [#issue1] 17 | 18 | 19 | ### Reference 20 | > - []() 21 | 22 | ### :house: [Go Home](https://github.com/Do-Hee/algorithm-study) 23 | 24 | -------------------------------------------------------------------------------- /contents/images/Java-Collections-Framework.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WeareSoft/algorithm-study/866ca55797bfa56716d1e6eda7559f630406e4e7/contents/images/Java-Collections-Framework.png -------------------------------------------------------------------------------- /contents/images/binary-tree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WeareSoft/algorithm-study/866ca55797bfa56716d1e6eda7559f630406e4e7/contents/images/binary-tree.png -------------------------------------------------------------------------------- /contents/images/deque-method.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WeareSoft/algorithm-study/866ca55797bfa56716d1e6eda7559f630406e4e7/contents/images/deque-method.png -------------------------------------------------------------------------------- /contents/images/graph-vs-tree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WeareSoft/algorithm-study/866ca55797bfa56716d1e6eda7559f630406e4e7/contents/images/graph-vs-tree.png -------------------------------------------------------------------------------- /contents/images/kruskal-example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WeareSoft/algorithm-study/866ca55797bfa56716d1e6eda7559f630406e4e7/contents/images/kruskal-example.png -------------------------------------------------------------------------------- /contents/images/prim-example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WeareSoft/algorithm-study/866ca55797bfa56716d1e6eda7559f630406e4e7/contents/images/prim-example.png -------------------------------------------------------------------------------- /contents/images/tree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WeareSoft/algorithm-study/866ca55797bfa56716d1e6eda7559f630406e4e7/contents/images/tree.png -------------------------------------------------------------------------------- /contents/samsung/181028.md: -------------------------------------------------------------------------------- 1 | ## 삼성 SW 역량 테스트 기출 문제 (4) 2 | 3 | ### 관련 문제들 4 | * [삼성 SW 역량 테스트 기출 문제](https://www.acmicpc.net/workbook/view/1152) 5 | * [큐빙 - 5373번](https://www.acmicpc.net/problem/5373) 6 | * [인구 이동 - 16234번](https://www.acmicpc.net/problem/16234) 7 | * [나무 재테크 - 16235번](https://www.acmicpc.net/problem/16235) 8 | * [아기 상어 - 16236번](https://www.acmicpc.net/problem/16236) 9 | 10 | ### [issue]에 대한 정리 11 | #### [#issue1] 큐빙 12 | ~~~java 13 | ~~~ 14 | * 15 | 16 | #### [#issue2] 인구 이동 17 | ~~~java 18 | ~~~ 19 | * 20 | 21 | #### [#issue3] 나무 재테크 22 | ~~~java 23 | ~~~ 24 | * 25 | 26 | #### [#issue4] 아기 상어 27 | ~~~java 28 | ~~~ 29 | * 30 | 31 | ### Reference 32 | > - []() 33 | 34 | ### :house: [Go Home](https://github.com/Do-Hee/algorithm-study) -------------------------------------------------------------------------------- /contents/samsung/README.md: -------------------------------------------------------------------------------- 1 | ## 삼성 SW 역량 테스트 2 | 3 | ### 기출 문제 (1) 4 | :heavy_check_mark: [issue 정리 내용](/contents/samsung/180916.md) 5 | * [#issue1] 치킨 배달 6 | * [#issue2] 드래곤 커브 7 | * [#issue3] 사다리 조작 8 | * [#issue4] 감시 9 | 10 | ### 기출 문제 (2) 11 | :heavy_check_mark: [issue 정리 내용](/contents/samsung/180923.md) 12 | * [#issue1] 톱니바퀴 13 | * [#issue2] 경사로 14 | * [#issue3] 스타트와 링크 15 | * [#issue4] 연산자 끼워넣기 16 | * [#issue5] 로봇 청소기 17 | * [#issue6] 연구소 18 | 19 | ### 기출 문제 (3) 20 | :heavy_check_mark: [issue 정리 내용](/contents/samsung/180930.md) 21 | * [#issue1] 퇴사 22 | * [#issue2] 테트로미노 23 | * [#issue3] 주사위 굴리기 24 | * [#issue4] 시험 감독 25 | * [#issue5] 뱀 26 | * [#issue6] 구슬 탈출 2 27 | 28 | ### 기출 문제 (4) 29 | :heavy_check_mark: [issue 정리 내용](/contents/samsung/181028.md) 30 | * [#issue1] 큐빙 31 | * [#issue2] 인구 이동 32 | * [#issue3] 나무 재테크 33 | * [#issue4] 아기 상어 34 | -------------------------------------------------------------------------------- /src/dami/Solution.java: -------------------------------------------------------------------------------- 1 | package dami; 2 | 3 | import dami.codility.Codility; 4 | import dami.codinginterview.Interview; 5 | import dami.codingtest.CodingTest; 6 | import dami.hackerrank.HackerRank; 7 | import dami.leetcode.Leetcode; 8 | import dami.programmers.Programmers; 9 | import dami.questions.Questions; 10 | 11 | public class Solution { 12 | public static void main(String[] args) { 13 | //new Codility().solutions(); 14 | //new Leetcode().solutions(); 15 | //new Programmers().solutions(); 16 | //new HackerRank().solutions(); 17 | //new Questions().solutions(); 18 | 19 | //new Interview().solutions(); 20 | //new CodingTest().solutions(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/dami/codility/Codility.java: -------------------------------------------------------------------------------- 1 | package dami.codility; 2 | 3 | import dami.codility.lesson5.review.GenomicRangeQuery; 4 | 5 | public class Codility { 6 | public void solutions() { 7 | //new BinaryGap().solution(328); 8 | //new CyclicRotation().solution(new int[] {3, 8, 9, 7, 6}, 3); 9 | //new OddOccurrencesInArray().solution(new int[] {9,3,9,3,9,7,9}); 10 | //new MaxCounters().solution(5, new int[] {3,4,4,6,1,4,4}); 11 | //new MissingInteger().solution(new int[] {1,3,6,4,1,2}); 12 | //new CountDiv().solution(10, 10, 5); 13 | //new CountDiv().solution(0, Integer.MAX_VALUE, 1); 14 | new GenomicRangeQuery().solution("CAGCCTA", new int[] {2, 5, 0}, new int[] {4, 5, 6}); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/dami/codility/lesson1/BinaryGap.java: -------------------------------------------------------------------------------- 1 | package dami.codility.lesson1; 2 | 3 | // 문제 : https://app.codility.com/programmers/lessons/1-iterations/binary_gap/ 4 | // 결과 : https://app.codility.com/demo/results/trainingVA6BQ5-M7Z/ 5 | public class BinaryGap { 6 | public int solution(int N) { 7 | String binaryString = Integer.toBinaryString(N); 8 | int max = 0, count = 0; 9 | 10 | for (int i = 0; i < binaryString.length(); i++) { 11 | if (binaryString.charAt(i) == '1') { 12 | max = Math.max(max, count); 13 | count = 0; 14 | } else { 15 | count++; 16 | } 17 | } 18 | 19 | return max; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/dami/codility/lesson2/CyclicRotation.java: -------------------------------------------------------------------------------- 1 | package dami.codility.lesson2; 2 | 3 | import java.util.ArrayDeque; 4 | import java.util.Deque; 5 | 6 | // 문제 : https://app.codility.com/programmers/lessons/2-arrays/cyclic_rotation/ 7 | // 결과 : https://app.codility.com/demo/results/training6JDBK4-8SQ/ 8 | public class CyclicRotation { 9 | public int[] solution(int[] A, int K) { 10 | if (null == A || A.length == 0) { 11 | return A; 12 | } 13 | 14 | Deque deque = new ArrayDeque<>(); 15 | for (int num : A) { 16 | deque.push(num); 17 | } 18 | 19 | for (int i = 0; i < K % A.length; i++) { 20 | deque.offer(deque.pop()); 21 | } 22 | 23 | int i = 0; 24 | while (!deque.isEmpty()) { 25 | A[i] = deque.removeLast(); 26 | i++; 27 | } 28 | 29 | return A; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/dami/codility/lesson2/OddOccurrencesInArray.java: -------------------------------------------------------------------------------- 1 | package dami.codility.lesson2; 2 | 3 | // 문제 : https://app.codility.com/programmers/lessons/2-arrays/odd_occurrences_in_array/ 4 | // 결과 : https://app.codility.com/demo/results/trainingPGQRXT-CDK/ 5 | public class OddOccurrencesInArray { 6 | public int solution(int[] A) { 7 | int result = 0; 8 | for (int num : A) { 9 | result ^= num; 10 | } 11 | 12 | return result; 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/dami/codility/lesson3/FrogJmp.java: -------------------------------------------------------------------------------- 1 | package dami.codility.lesson3; 2 | 3 | // 문제 : https://app.codility.com/programmers/lessons/3-time_complexity/frog_jmp/ 4 | // 결과 : https://app.codility.com/demo/results/trainingZW2P29-72A/ 5 | public class FrogJmp { 6 | public int solution(int X, int Y, int D) { 7 | int total = Y - X; 8 | return (total / D) + (total % D != 0 ? 1 : 0); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/dami/codility/lesson3/PermMissingElem.java: -------------------------------------------------------------------------------- 1 | package dami.codility.lesson3; 2 | 3 | import java.util.Arrays; 4 | 5 | // 문제 : https://app.codility.com/programmers/lessons/3-time_complexity/perm_missing_elem/ 6 | // 결과 : https://app.codility.com/demo/results/trainingMTKP8M-WJN/ 7 | public class PermMissingElem { 8 | public int solution(int[] A) { 9 | Arrays.sort(A); 10 | 11 | for (int i = 0; i < A.length; i++) { 12 | if (i + 1 != A[i]) { 13 | return i + 1; 14 | } 15 | } 16 | 17 | return A.length + 1; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/dami/codility/lesson3/TapeEquilibrium.java: -------------------------------------------------------------------------------- 1 | package dami.codility.lesson3; 2 | 3 | // 문제 : https://app.codility.com/programmers/lessons/3-time_complexity/tape_equilibrium/ 4 | // 결과 : https://app.codility.com/demo/results/trainingJPBA75-MDR/ 5 | public class TapeEquilibrium { 6 | public int solution(int[] A) { 7 | int leftSum = A[0]; 8 | int rightSum = 0; 9 | for (int i = 1; i < A.length; i++) { 10 | rightSum += A[i]; 11 | } 12 | 13 | int min = Integer.MAX_VALUE; 14 | for (int i = 1; i < A.length; i++) { 15 | min = Math.min(min, Math.abs(leftSum - rightSum)); 16 | 17 | leftSum += A[i]; 18 | rightSum -= A[i]; 19 | } 20 | 21 | return min; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/dami/codility/lesson4/FrogRiverOne.java: -------------------------------------------------------------------------------- 1 | package dami.codility.lesson4; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | // 문제 : https://app.codility.com/programmers/lessons/4-counting_elements/frog_river_one/ 7 | // 결과 : https://app.codility.com/demo/results/trainingV9AWQG-H4S/ 8 | public class FrogRiverOne { 9 | public int solution(int X, int[] A) { 10 | Set leaves = new HashSet<>(); 11 | for (int i = 1; i <= X; i++) { 12 | leaves.add(i); 13 | } 14 | 15 | for (int i = 0; i < A.length; i++) { 16 | leaves.remove(A[i]); 17 | 18 | if (leaves.isEmpty()) { 19 | return i; 20 | } 21 | } 22 | 23 | return -1; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/dami/codility/lesson4/MissingInteger.java: -------------------------------------------------------------------------------- 1 | package dami.codility.lesson4; 2 | 3 | import java.util.Arrays; 4 | 5 | // ⭐️ 6 | // 문제 : https://app.codility.com/programmers/lessons/4-counting_elements/missing_integer/ 7 | // 결과 : https://app.codility.com/demo/results/trainingGWWV7R-UP7/ 8 | public class MissingInteger { 9 | public int solution(int[] A) { 10 | Arrays.sort(A); 11 | 12 | int result = 1; 13 | for (int num : A) { 14 | if (num < result) { 15 | continue; 16 | } 17 | 18 | if (result < num) { 19 | return result; 20 | } 21 | result++; 22 | } 23 | 24 | return result; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/dami/codility/lesson4/PermCheck.java: -------------------------------------------------------------------------------- 1 | package dami.codility.lesson4; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | // 문제 : https://app.codility.com/programmers/lessons/4-counting_elements/perm_check/ 7 | // 결과 : https://app.codility.com/demo/results/trainingH5WZAU-9AZ/ 8 | public class PermCheck { 9 | public int solution(int[] A) { 10 | Set distinct = new HashSet<>(); 11 | for (int num : A) { 12 | if (num > A.length) { 13 | return 0; 14 | } 15 | 16 | if (distinct.contains(num)) { 17 | return 0; 18 | } 19 | 20 | distinct.add(num); 21 | } 22 | return 1; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/dami/codility/lesson4/review/MaxCounters.java: -------------------------------------------------------------------------------- 1 | package dami.codility.lesson4.review; 2 | 3 | // ⭐️ 4 | // 문제 : https://app.codility.com/programmers/lessons/4-counting_elements/max_counters/ 5 | // 결과1 (타임아웃) : https://app.codility.com/demo/results/trainingN3KWEQ-8DS/ 6 | // 결과2 : https://app.codility.com/demo/results/trainingEX47UA-2RY/ 7 | 8 | // position이 N+1일 때 매번 maxCounter 찍어주면 타임아웃 발생 9 | public class MaxCounters { 10 | public int[] solution(int N, int[] A) { 11 | int[] result = new int[N]; 12 | int max = 0; 13 | int finalMax = 0; 14 | for (int position : A) { 15 | if (position == N + 1) { 16 | finalMax = max; 17 | } else { 18 | if (result[position - 1] < finalMax) { 19 | result[position - 1] = finalMax; 20 | } 21 | max = Math.max(max, ++result[position - 1]); 22 | } 23 | } 24 | 25 | maxUp(result, finalMax); 26 | 27 | return result; 28 | } 29 | 30 | private void maxUp(int[] result, int max) { 31 | for (int i = 0; i < result.length; i++) { 32 | if (result[i] < max) { 33 | result[i] = max; 34 | } 35 | } 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/dami/codility/lesson5/CountDiv.java: -------------------------------------------------------------------------------- 1 | package dami.codility.lesson5; 2 | 3 | import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ; 4 | 5 | // 문제 : https://app.codility.com/programmers/lessons/5-prefix_sums/count_div/ 6 | // 결과 : https://app.codility.com/demo/results/trainingN8CPTP-SQY/ 7 | public class CountDiv { 8 | // 시간 : O(1) 9 | public int solution(int A, int B, int K) { 10 | return (B / K) - (A % K == 0 ? A / K - 1 : A / K); 11 | } 12 | 13 | // 시간 : O(B-A) => O(1) 방법 있을듯 14 | public int timeout(int A, int B, int K) { 15 | int result = 0; 16 | for (int i = A; i <= B; i++) { 17 | if (i % K == 0) { 18 | result++; 19 | } 20 | } 21 | 22 | return result; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/dami/codility/lesson5/review/GenomicRangeQuery.java: -------------------------------------------------------------------------------- 1 | package dami.codility.lesson5.review; 2 | 3 | import java.util.*; 4 | 5 | // ⭐️ Prefix Sum 알고리즘 6 | // 문제 : https://app.codility.com/programmers/lessons/5-prefix_sums/genomic_range_query/ 7 | // 결과 : 62% 8 | public class GenomicRangeQuery { 9 | 10 | // 시간 : O(n) 11 | public int[] solution(String S, int[] P, int[] Q) { 12 | Map dnaMap = new HashMap<>(); 13 | dnaMap.put('A', 1); 14 | dnaMap.put('C', 2); 15 | dnaMap.put('G', 3); 16 | dnaMap.put('T', 4); 17 | 18 | int[][] prefix = new int[S.length()][dnaMap.size()]; 19 | for (int i = 0; i < S.length(); i++) { 20 | prefix[i][dnaMap.get(S.charAt(i)) - 1]++; 21 | } 22 | 23 | return null; 24 | } 25 | 26 | // 시간 : O(n^2) => O(n) 방법 찾기 27 | public int[] timeout(String S, int[] P, int[] Q) { 28 | Map dnaMap = new HashMap<>(); 29 | dnaMap.put('A', 1); 30 | dnaMap.put('C', 2); 31 | dnaMap.put('G', 3); 32 | dnaMap.put('T', 4); 33 | 34 | int[] result = new int[P.length]; 35 | for (int i = 0; i < P.length; i++) { 36 | int min = Integer.MAX_VALUE; 37 | for (int j = P[i]; j <= Q[i]; j++) { 38 | min = Math.min(min, dnaMap.get(S.charAt(j))); 39 | if (min == 1) break; 40 | } 41 | result[i] = min; 42 | } 43 | 44 | return result; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/dami/codinginterview/array/Array4.java: -------------------------------------------------------------------------------- 1 | package dami.codinginterview.array; 2 | 3 | public class Array4 { 4 | public int[] sorting(int[] array) { 5 | return mySolution(array); 6 | //return solution(array); 7 | } 8 | 9 | private int[] mySolution(int[] array) { 10 | int[] sorted = new int[array.length * 2]; 11 | 12 | for (int number : array) { 13 | sorted[number - 1] = number; 14 | } 15 | 16 | int index = 0; 17 | for (int number : sorted) { 18 | if (number != 0) { 19 | array[index++] = number; 20 | } 21 | } 22 | 23 | return array; 24 | } 25 | 26 | private int[] solution(int[] array) { 27 | boolean[] booleans = new boolean[array.length * 2]; 28 | 29 | for (int number : array) { 30 | booleans[number] = true; 31 | } 32 | 33 | int[] sorted = new int[array.length]; 34 | int index = 0; 35 | for (int i = 0; i < booleans.length; i++) { 36 | if (booleans[i]) { 37 | sorted[index++] = i; 38 | } 39 | } 40 | 41 | return sorted; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/dami/codinginterview/common/ListNode.java: -------------------------------------------------------------------------------- 1 | package dami.codinginterview.common; 2 | 3 | public class ListNode { 4 | public int val; 5 | public ListNode next; 6 | 7 | public ListNode() { } 8 | 9 | public ListNode(int val) { 10 | this.val = val; 11 | } 12 | 13 | public ListNode(int val, ListNode next) { 14 | this.val = val; 15 | this.next = next; 16 | } 17 | 18 | public void add(ListNode node) { 19 | ListNode current = this; 20 | while (current.next != null) { 21 | current = current.next; 22 | } 23 | current.next = node; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/dami/codinginterview/common/TreeNode.java: -------------------------------------------------------------------------------- 1 | package dami.codinginterview.common; 2 | 3 | public class TreeNode { 4 | public int val; 5 | public TreeNode left; 6 | public TreeNode right; 7 | public TreeNode() {} 8 | public TreeNode(int val) { this.val = val; } 9 | public TreeNode(int val, TreeNode left, TreeNode right) { 10 | this.val = val; 11 | this.left = left; 12 | this.right = right; 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/dami/codinginterview/tree/Tree.java: -------------------------------------------------------------------------------- 1 | package dami.codinginterview.tree; 2 | 3 | 4 | import com.sun.corba.se.impl.orbutil.StackImpl; 5 | import dami.codinginterview.common.TreeNode; 6 | 7 | import java.util.Stack; 8 | 9 | public class Tree { 10 | public void inorderIterating(TreeNode node) { 11 | if (node == null) { 12 | return; 13 | } 14 | 15 | Stack stack = new Stack<>(); 16 | while (true) { 17 | while (node != null) { 18 | stack.push(node); 19 | node = node.left; 20 | } 21 | 22 | if (stack.isEmpty()) { 23 | break; 24 | } 25 | 26 | node = stack.pop(); 27 | System.out.println(node.val); 28 | node = node.right; 29 | } 30 | } 31 | 32 | public void inorderRecursive(TreeNode node) { 33 | if (node == null) { 34 | return; 35 | } 36 | 37 | inorderRecursive(node.left); 38 | System.out.println(node.val); 39 | inorderRecursive(node.right); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/dami/codinginterview/tree/Tree1.java: -------------------------------------------------------------------------------- 1 | package dami.codinginterview.tree; 2 | 3 | import dami.codinginterview.common.TreeNode; 4 | 5 | public class Tree1 { 6 | public void findNthNode(TreeNode root, int n) { 7 | mySolution(root, n - 1); 8 | solution(root, n - 1); 9 | } 10 | 11 | private int mySolution(TreeNode node, int n) { 12 | if (node == null) { 13 | return n + 1; 14 | } 15 | 16 | if (n < 0) { 17 | return n; 18 | } 19 | 20 | if (n == 0) { 21 | System.out.println(node.val); 22 | } 23 | 24 | n = mySolution(node.left, --n); 25 | n = mySolution(node.right, --n); 26 | 27 | return n; 28 | } 29 | 30 | private int count = 0; 31 | private void solution(TreeNode node, int n) { 32 | if (node == null) { 33 | return; 34 | } 35 | 36 | solution(node.left, n); 37 | solution(node.right, n); 38 | 39 | if (count++ == n) { 40 | System.out.println(node.val); 41 | } 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/dami/codinginterview/tree/Tree1_1.java: -------------------------------------------------------------------------------- 1 | package dami.codinginterview.tree; 2 | 3 | import dami.codinginterview.common.TreeNode; 4 | 5 | public class Tree1_1 { 6 | public void findTreeDepth(TreeNode node) { 7 | 8 | } 9 | 10 | private int solution(TreeNode node) { 11 | if (node == null) { 12 | return 0; 13 | } 14 | 15 | int leftDepth = solution(node.left); 16 | int rightDepth = solution(node.right); 17 | 18 | if (leftDepth > rightDepth) { 19 | return leftDepth + 1; 20 | } else { 21 | return rightDepth + 1; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/dami/hackerrank/HackerRank.java: -------------------------------------------------------------------------------- 1 | package dami.hackerrank; 2 | 3 | public class HackerRank { 4 | public void solutions() { 5 | ////////////////////////////////////////////////////////////////////////////// 6 | /* hacker rank */ 7 | //new The_Time_in_Words().timeInWords(12, 50); 8 | //new Highest_Value_Palindrome().highestValuePalindrome("12321", 5, 3); 9 | //new Sherlock_and_the_Valid_String().isValid("aabbcd"); 10 | //new New_Year_Chaos().minimumBribes(new int[] {1, 2, 5, 3, 7, 8, 6, 4}); 11 | //new Minimum_Swaps_2().minimumSwaps(new int[] {4,3,1,2}); 12 | //new Special_StringAgain().substrCount(5, "asasd"); 13 | //new Common_Child().commonChild("WEWOUCUIDGCGTRMEZEPXZFEJWISRSBBSYXAYDFEJJDLEBVHHKS", "FDAGCXGKCTKWNECHMRXZWMLRYUCOCZHJRRJBOAJOQJZZVUYXIC"); 14 | //new Sorting_Comparator().solution(); 15 | //new Fraudulent_Activity_Notifications().activityNotifications(new int[] {10,20,30,40,50}, 3); 16 | ////////////////////////////////////////////////////////////////////////////// 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/dami/hackerrank/medium/Common_Child.java: -------------------------------------------------------------------------------- 1 | package dami.hackerrank.medium; 2 | 3 | // https://www.hackerrank.com/challenges/common-child/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=strings 4 | 5 | // LCS 문제 6 | // 참고 1. https://ko.wikipedia.org/wiki/%EC%B5%9C%EC%9E%A5_%EA%B3%B5%ED%86%B5_%EB%B6%80%EB%B6%84_%EC%88%98%EC%97%B4 7 | // 참고 2. https://twinw.tistory.com/126 8 | 9 | // 값 구하는 규칙 10 | // - String1[n], String2[k]가 같다면 : [n, k] == [n-1, k-1] + 1 11 | // - String1[n], String2[k]가 다르면 : [n, k] == [n-1, k]와 [n, k-1] 중 큰 값 12 | public class Common_Child { 13 | public int commonChild(String s1, String s2) { 14 | int[][] lcs = new int[s1.length() + 1][s2.length() + 1]; 15 | 16 | char[] s1c = s1.toCharArray(); 17 | char[] s2c = s2.toCharArray(); 18 | for (int i = 1; i < lcs.length; i++) { 19 | for (int j = 1; j < lcs[0].length; j++) { 20 | if (s2c[i - 1] == s1c[j - 1]) { 21 | lcs[i][j] = lcs[i - 1][j - 1] + 1; 22 | } else { 23 | lcs[i][j] = Math.max(lcs[i - 1][j], lcs[i][j - 1]); 24 | } 25 | } 26 | } 27 | 28 | return lcs[s1.length()][s2.length()]; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/dami/hackerrank/medium/Sorting_Comparator.java: -------------------------------------------------------------------------------- 1 | package dami.hackerrank.medium; 2 | 3 | import java.util.Arrays; 4 | import java.util.Comparator; 5 | import java.util.Scanner; 6 | 7 | public class Sorting_Comparator { 8 | public void solution() { 9 | Scanner scan = new Scanner(System.in); 10 | int n = scan.nextInt(); 11 | 12 | Player[] player = new Player[n]; 13 | Checker checker = new Checker(); 14 | 15 | for(int i = 0; i < n; i++){ 16 | player[i] = new Player(scan.next(), scan.nextInt()); 17 | } 18 | scan.close(); 19 | 20 | Arrays.sort(player, checker); 21 | for (Player value : player) { 22 | System.out.printf("%s %s\n", value.name, value.score); 23 | } 24 | } 25 | } 26 | 27 | class Player { 28 | String name; 29 | int score; 30 | 31 | Player(String name, int score) { 32 | this.name = name; 33 | this.score = score; 34 | } 35 | } 36 | 37 | class Checker implements Comparator { 38 | // complete this method 39 | public int compare(Player a, Player b) { 40 | if (a.score == b.score) { 41 | return a.name.compareTo(b.name); 42 | } 43 | 44 | return b.score - a.score; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/dami/hackerrank/medium/Special_StringAgain.java: -------------------------------------------------------------------------------- 1 | package dami.hackerrank.medium; 2 | 3 | // https://www.hackerrank.com/challenges/special-palindrome-again/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=strings 4 | public class Special_StringAgain { 5 | // 재귀 풀이 : 스택오버플로우 발생 6 | private long count = 0L; 7 | 8 | public long substrCount(int n, String s) { 9 | for (int i = 0; i < n; i++) { 10 | recursive(s.substring(i, i + 1), s, i, i + 1); 11 | } 12 | return count; 13 | } 14 | 15 | private void recursive(String prefix, String s, int start, int end) { 16 | if (isSpecial(prefix)) { 17 | count++; 18 | } 19 | 20 | if (end == s.length()) { 21 | return; 22 | } 23 | 24 | recursive(s.substring(start, ++end), s, start, end); 25 | } 26 | 27 | private boolean isSpecial(String prefix) { 28 | char[] charArray = prefix.toCharArray(); 29 | 30 | char result = charArray[0]; 31 | int index = 0; 32 | for (int i = 1; i < charArray.length; i++) { 33 | if (result != charArray[i]) { 34 | index = i; 35 | } 36 | } 37 | 38 | return index == 0 || (charArray.length % 2 != 0 && index == charArray.length / 2); 39 | } 40 | 41 | } 42 | 43 | -------------------------------------------------------------------------------- /src/dami/hackerrank/medium/review/New_Year_Chaos.java: -------------------------------------------------------------------------------- 1 | package dami.hackerrank.medium.review; 2 | 3 | // https://www.hackerrank.com/challenges/new-year-chaos/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=arrays 4 | 5 | // 현재 위치 : 1 2 5 3 7 8 6 4 6 | // 처음 위치 : 1 2 3 4 5 6 7 8 7 | // 위치 차이 : 0 0 2 -1 2 2 -1 -4 8 | // 1. 이동 수(위치 차이)가 2보다 큰 경우는 chaotic 9 | // 2. 현재 값이 원래 있어야 할 위치 - 2부터 현재 값 위치까지 반복하면서 현재 값보다 큰 수 있으면 count++ (뒤로 밀어주는 것) 10 | // ⭐️뒤 부터 반복 11 | public class New_Year_Chaos { 12 | 13 | public void minimumBribes(int[] q) { 14 | int count = 0; 15 | for (int i = q.length - 1; i >= 0; i--) { 16 | if (q[i] - (i + 1) > 2) { 17 | System.out.println("Too chaotic"); 18 | return; 19 | } 20 | 21 | for (int j = Math.max(0, q[i] - 2); j < i; j++) { 22 | if (q[j] > q[i]) { 23 | count++; 24 | } 25 | } 26 | } 27 | System.out.println(count); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/dami/leetcode/common/ListNode.java: -------------------------------------------------------------------------------- 1 | package dami.leetcode.common; 2 | 3 | public class ListNode { 4 | public int val; 5 | public ListNode next; 6 | 7 | public ListNode() { } 8 | 9 | public ListNode(int val) { 10 | this.val = val; 11 | } 12 | 13 | public ListNode(int val, ListNode next) { 14 | this.val = val; 15 | this.next = next; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/dami/leetcode/common/TreeNode.java: -------------------------------------------------------------------------------- 1 | package dami.leetcode.common; 2 | 3 | public class TreeNode { 4 | public int val; 5 | public TreeNode left; 6 | public TreeNode right; 7 | public TreeNode() {} 8 | public TreeNode(int val) { this.val = val; } 9 | public TreeNode(int val, TreeNode left, TreeNode right) { 10 | this.val = val; 11 | this.left = left; 12 | this.right = right; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/dami/leetcode/easy/Palindrome_Number_9.java: -------------------------------------------------------------------------------- 1 | package dami.leetcode.easy; 2 | 3 | public class Palindrome_Number_9 { 4 | private static final int DIVIDING_NUMBER = 10; 5 | 6 | // TEST CASE : 121, -121, 1001, 10, 1000000001 7 | public boolean isPalindrome(int x) { 8 | if (x < 0) { 9 | return false; 10 | } 11 | return x == reverse(x); 12 | } 13 | 14 | private int reverse(int x) { 15 | int quotient = x; 16 | int remainder = 0; 17 | long reversedX = 0; 18 | while (quotient >= 1) { 19 | remainder = quotient % DIVIDING_NUMBER; 20 | quotient = quotient / DIVIDING_NUMBER; 21 | 22 | reversedX = (reversedX + remainder) * DIVIDING_NUMBER; 23 | } 24 | 25 | return (int) (reversedX / DIVIDING_NUMBER); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/dami/leetcode/easy/Reverse_Integer_7.java: -------------------------------------------------------------------------------- 1 | package dami.leetcode.easy; 2 | 3 | // Submission Detail : https://leetcode.com/submissions/detail/356536589/ 4 | public class Reverse_Integer_7 { 5 | 6 | public int reverse(int x) { 7 | try { 8 | long result = 0; 9 | int n = 0; 10 | while(x != 0) { 11 | n = x%10; 12 | result += n; 13 | x = (x-n)/10; 14 | 15 | if(x != 0) { 16 | if(result*10 <= Integer.MAX_VALUE && result*10 >= Integer.MIN_VALUE) 17 | result *= 10; 18 | else 19 | return 0; 20 | } 21 | } 22 | return (int) result; 23 | 24 | } catch(NumberFormatException e) { 25 | return 0; 26 | } catch(ArithmeticException e) { 27 | return 0; 28 | } 29 | } 30 | } 31 | 32 | /* 33 | class Solution { 34 | public int reverse(int x) { 35 | 36 | int result = 0; 37 | 38 | while (x != 0) { 39 | 40 | int pop = x % 10; 41 | 42 | x = x / 10; 43 | 44 | if (result > Integer.MAX_VALUE / 10) { 45 | return 0; 46 | } 47 | 48 | if (result < Integer.MIN_VALUE / 10) { 49 | return 0; 50 | } 51 | 52 | result = result * 10 + pop; 53 | } 54 | 55 | return result; 56 | } 57 | } 58 | */ 59 | -------------------------------------------------------------------------------- /src/dami/leetcode/easy/Roman_to_Integer_13.java: -------------------------------------------------------------------------------- 1 | package dami.leetcode.easy; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | // 테스트 케이스 7 | // "I", "III", "IV", "IX", "LVIII", "MCMXCIV", "MMMCMXCIX" 8 | public class Roman_to_Integer_13 { 9 | private final static Map romanMap = new HashMap<>(); 10 | 11 | static { 12 | romanMap.put("I", 1); 13 | romanMap.put("V", 5); 14 | romanMap.put("X", 10); 15 | romanMap.put("L", 50); 16 | romanMap.put("C", 100); 17 | romanMap.put("D", 500); 18 | romanMap.put("M", 1000); 19 | } 20 | 21 | public int romanToInt(String s) { 22 | int sum = 0; 23 | for (int i = 0; i < s.length(); i++) { 24 | int current = romanMap.get(String.valueOf(s.charAt(i))); 25 | int next = i+1 == s.length() ? 0 : romanMap.get(String.valueOf(s.charAt(i+1))); 26 | 27 | if (current < next) { 28 | sum += next - current; 29 | i++; 30 | continue; 31 | } 32 | sum += current; 33 | } 34 | 35 | return sum; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/dami/leetcode/easy/Two_Sum_1.java: -------------------------------------------------------------------------------- 1 | package dami.leetcode.easy; 2 | 3 | // Submission Detail : https://leetcode.com/submissions/detail/356520239/ 4 | public class Two_Sum_1 { 5 | 6 | public int[] twoSum(int[] nums, int target) { 7 | for(int i=0; i r = new Stack<>(); 13 | r.push(0); 14 | for (int i = 1; i < T.length; i++) { 15 | while (!r.isEmpty() && T[r.peek()] < T[i]) { 16 | int index = r.pop(); 17 | result[index] = i - index; 18 | } 19 | r.push(i); 20 | } 21 | 22 | while (!r.isEmpty()) { 23 | result[r.pop()] = 0; 24 | } 25 | 26 | return result; 27 | } 28 | } 29 | 30 | 31 | 32 | /* 33 | // 시간복잡도 O(n^2), 실행시간 : 1095 ms / 메모리 : 47MB 34 | public int[] dailyTemperatures(int[] T) { 35 | int[] result = new int[T.length]; 36 | for (int i = 0; i < T.length; i++) { 37 | int count = 0; 38 | for (int j = i + 1; j < T.length; j++) { 39 | if (T[i] < T[j]) { 40 | result[i] = count + 1; 41 | break; 42 | } 43 | count++; 44 | } 45 | 46 | if (count + i + 1 == T.length) { 47 | result[i] = 0; 48 | } 49 | } 50 | 51 | return result; 52 | } 53 | 54 | */ 55 | -------------------------------------------------------------------------------- /src/dami/leetcode/medium/Permutations_46.java: -------------------------------------------------------------------------------- 1 | package dami.leetcode.medium; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | // https://leetcode.com/problems/permutations/ 7 | 8 | // 순열 알고리즘 연습 9 | public class Permutations_46 { 10 | public List> permute(int[] nums) { 11 | List> result = new ArrayList<>(); 12 | recursive(result, nums, new ArrayList<>(), 0); 13 | 14 | return result; 15 | } 16 | 17 | private void recursive(List> result, int[] nums, List perms, int depth) { 18 | if (depth == nums.length) { 19 | result.add(new ArrayList<>(perms)); 20 | return; 21 | } 22 | 23 | for (int i = depth; i < nums.length; i++) { 24 | perms.add(nums[i]); 25 | swap(nums, i, depth); 26 | recursive(result, nums, perms, depth + 1); 27 | perms.remove(perms.size() - 1); 28 | swap(nums, i, depth); 29 | } 30 | } 31 | 32 | private void swap(int[] nums, int index, int depth) { 33 | int temp = nums[index]; 34 | nums[index] = nums[depth]; 35 | nums[depth] = temp; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/dami/leetcode/medium/Remove_Nth_Node_19.java: -------------------------------------------------------------------------------- 1 | package dami.leetcode.medium; 2 | 3 | import dami.leetcode.common.ListNode; 4 | 5 | // https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 6 | // 다른 코드 : https://leetcode.com/submissions/detail/431959305/ 7 | 8 | public class Remove_Nth_Node_19 { 9 | public ListNode removeNthFromEnd(ListNode head, int n) { 10 | // head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5))))); 11 | // head = new ListNode(1); 12 | 13 | int listSize = sizeOf(head); 14 | ListNode current = head; 15 | if (n == listSize) { // 첫번째 노드일 때 16 | head = current.next; 17 | 18 | } else if (n == 1) { // 마지막 노드일 때 19 | for (int i = 0; i < listSize - 2; i++) { 20 | current = current.next; 21 | } 22 | current.next = null; 23 | 24 | } else { // 중간 노드 25 | for (int i = 0; i < listSize - n - 1; i++) { 26 | current = current.next; 27 | } 28 | current.next = current.next.next; 29 | } 30 | 31 | return head; 32 | } 33 | 34 | private int sizeOf(ListNode head) { 35 | int size = 0; 36 | ListNode list = head; 37 | while (list != null) { 38 | list = list.next; 39 | size++; 40 | } 41 | return size; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/dami/leetcode/medium/SubrectangleQueries.java: -------------------------------------------------------------------------------- 1 | package dami.leetcode.medium; 2 | 3 | public class SubrectangleQueries { 4 | private final int[][] rectangle; 5 | 6 | public SubrectangleQueries(int[][] rectangle) { 7 | this.rectangle = rectangle; 8 | } 9 | 10 | public void updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) { 11 | for (int i = row1; i <= row2 ; i++) { 12 | for (int j = col1; j <= col2; j++) { 13 | rectangle[i][j] = newValue; 14 | } 15 | } 16 | } 17 | 18 | public int getValue(int row, int col) { 19 | return rectangle[row][col]; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/dami/leetcode/medium/Subsets_78.java: -------------------------------------------------------------------------------- 1 | package dami.leetcode.medium; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | // https://leetcode.com/problems/subsets/ 7 | public class Subsets_78 { 8 | public List> subsets(int[] nums) { 9 | List> result = new ArrayList<>(); 10 | recursive(result, new ArrayList<>(), nums, 0); 11 | return result; 12 | } 13 | 14 | private void recursive(List> result, List set, int[] nums, int start) { 15 | result.add(new ArrayList<>(set)); 16 | 17 | for (int i = start; i < nums.length; i++) { 18 | set.add(nums[i]); 19 | recursive(result, set, nums, i + 1); 20 | set.remove(set.size() - 1); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/dami/leetcode/medium/Three_Sum_15.java: -------------------------------------------------------------------------------- 1 | package dami.leetcode.medium; 2 | 3 | import java.util.*; 4 | import java.util.stream.Collectors; 5 | 6 | // https://leetcode.com/problems/3sum/ 7 | public class Three_Sum_15 { 8 | private static final int TARGET = 0; 9 | 10 | // {-1, 0, 1, 2, -1, -4} 11 | public List> threeSum(int[] nums) { 12 | Map numsMap = new HashMap<>(); 13 | for (int i = 0; i < nums.length; i++) { 14 | numsMap.putIfAbsent(nums[i], i); 15 | } 16 | 17 | List> result = new ArrayList<>(); 18 | for (Integer key : numsMap.keySet()) { 19 | int newTarget = TARGET - key; 20 | int i = numsMap.get(key) + 1; 21 | while (i < nums.length) { 22 | int lastValue = newTarget - nums[i]; 23 | if (numsMap.containsKey(lastValue) && numsMap.get(lastValue) != i && lastValue != key) { 24 | result.add(new ArrayList<>(Arrays.asList(key, nums[i], lastValue))); 25 | break; 26 | } 27 | i++; 28 | } 29 | } 30 | 31 | 32 | return result; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/dami/leetcode/medium/Top_K_Frequent_Elements_347.java: -------------------------------------------------------------------------------- 1 | package dami.leetcode.medium; 2 | 3 | import java.util.Comparator; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | import java.util.PriorityQueue; 7 | 8 | // https://leetcode.com/problems/top-k-frequent-elements/ 9 | public class Top_K_Frequent_Elements_347 { 10 | public int[] topKFrequent(int[] nums, int k) { 11 | Map numMap = new HashMap<>(); 12 | for (int num : nums) { 13 | numMap.put(num, numMap.getOrDefault(num, 0) + 1); 14 | } 15 | 16 | return numMap.keySet().stream() 17 | .sorted((key1, key2) -> numMap.get(key2).compareTo(numMap.get(key1))) 18 | .limit(k) 19 | .mapToInt(num -> num) 20 | .toArray(); 21 | } 22 | } 23 | 24 | /* 25 | // PriorityQueue 사용한 방법 26 | public int[] topKFrequent(int[] nums, int k) { 27 | 28 | Map map = new HashMap(); 29 | for(int i: nums) 30 | map.put(i, map.getOrDefault(i, 0) + 1); 31 | 32 | PriorityQueue queue = new PriorityQueue(k, (n1, n2)->map.get(n1)-map.get(n2)); 33 | for(Map.Entry entry : map.entrySet()){ 34 | queue.add(entry.getKey()); 35 | if(queue.size() > k) 36 | queue.poll(); 37 | } 38 | int[] res = new int[k]; 39 | int i = 0; 40 | while(queue.size() > 0) 41 | res[i++] = queue.poll(); 42 | return res; 43 | 44 | } 45 | 46 | */ 47 | -------------------------------------------------------------------------------- /src/dami/leetcode/medium/review/Binary_Tree_Inorder_94.java: -------------------------------------------------------------------------------- 1 | package dami.leetcode.medium.review; 2 | 3 | import dami.leetcode.common.TreeNode; 4 | 5 | import java.util.*; 6 | 7 | // https://leetcode.com/problems/binary-tree-inorder-traversal/ 8 | public class Binary_Tree_Inorder_94 { 9 | public List inorderTraversal(TreeNode root) { 10 | root = new TreeNode(4, new TreeNode(2, new TreeNode(1), new TreeNode(3)), new TreeNode(5, new TreeNode(7), new TreeNode(8))); 11 | List result = new ArrayList<>(); 12 | 13 | // 1. 순환 방법 14 | recursive(root, result); 15 | // 2. 반복 방법 16 | iteration(root, result); 17 | 18 | return result; 19 | } 20 | 21 | private void recursive(TreeNode node, List result) { 22 | if (null == node) { 23 | return; 24 | } 25 | recursive(node.left, result); 26 | result.add(node.val); 27 | recursive(node.right, result); 28 | } 29 | 30 | private void iteration(TreeNode node, List result) { 31 | if (null == node) { 32 | return; 33 | } 34 | Stack stack = new Stack<>(); 35 | TreeNode current = node; 36 | while (null != current || !stack.isEmpty()) { 37 | while (null != current) { 38 | stack.push(current); 39 | current = current.left; 40 | } 41 | current = stack.pop(); 42 | result.add(current.val); 43 | current = current.right; 44 | } 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/dami/leetcode/medium/review/Counting_Bits_338.java: -------------------------------------------------------------------------------- 1 | package dami.leetcode.medium.review; 2 | 3 | // https://leetcode.com/problems/counting-bits/ 4 | public class Counting_Bits_338 { 5 | public int[] countBits(int num) { 6 | return solution2(num); 7 | } 8 | 9 | // 방법 1. O(n^2) 10 | private int[] solution1(int num) { 11 | int[] result = new int[num + 1]; 12 | for (int i = num; i >= 0; i--) { 13 | int sum = 0; 14 | int n = i; 15 | while (n >= 1) { 16 | sum += n % 2; 17 | n = n / 2; 18 | } 19 | result[i] = sum; 20 | } 21 | return result; 22 | } 23 | 24 | // 방법 2. 비트연산자 사용 O(n) 25 | private int[] solution2(int num) { 26 | int[] result = new int[num + 1]; 27 | for (int i = 1; i <= num; i++) { 28 | // " f[i >> 1] " is equivalent to " f[i / 2] " 29 | // => 짝수는 비트연산으로 한 칸 밀어도(2로 나누면) 무조건 1이 한 개 30 | // " i & 1 " is equivalent to " i % 2 " 31 | // => 홀수는 비트연산으로 한 칸 밀면 1 한 개가 없어짐. 32 | // 예를 들어 5의 2진수는 101이되고 " 101 & 1 " 하면 비트연산으로 없어진 1 추가 가능 33 | result[i] = result[i >> 1] + (i & 1); 34 | } 35 | return result; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/dami/leetcode/medium/review/Queue_Reconstruction_406.java: -------------------------------------------------------------------------------- 1 | package dami.leetcode.medium.review; 2 | 3 | import java.util.*; 4 | 5 | // https://leetcode.com/problems/queue-reconstruction-by-height/ 6 | public class Queue_Reconstruction_406 { 7 | public int[][] reconstructQueue(int[][] people) { 8 | LinkedList reconstructed = new LinkedList<>(); 9 | 10 | // 두 height가 같으면 [7, 0] [7, 1] 11 | // [][1] 오름차순 정렬 12 | // 두 height가 다르면 [4, 4] [5, 0] 13 | // height([][0]) 내림차순 정렬 14 | Arrays.sort(people, (p1, p2) -> p1[0] == p2[0] ? p1[1] - p2[1] : p2[0] - p1[0]); 15 | 16 | // 정렬한 상태에서 [][1] 위치에 넣어주기만 하면 reconstructed 17 | for (int[] person : people) { 18 | reconstructed.add(person[1], person); 19 | } 20 | 21 | return reconstructed.toArray(new int[people.length][]); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/dami/programmers/level1/추억_점수.java: -------------------------------------------------------------------------------- 1 | package dami.programmers.level1; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | public class 추억_점수 { 7 | 8 | public int[] solution(String[] name, int[] yearning, String[][] photo) { 9 | int[] answer = new int[photo.length]; 10 | 11 | Map yearningRank = new HashMap<>(); 12 | for (int i = 0; i < name.length; i++) { 13 | yearningRank.put(name[i], yearning[i]); 14 | } 15 | 16 | int i = 0; 17 | for (String[] currentPhoto : photo) { 18 | int sum = 0; 19 | for (String personName : currentPhoto) { 20 | sum += yearningRank.getOrDefault(personName, 0); 21 | } 22 | answer[i++] = sum; 23 | } 24 | 25 | return answer; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/dami/programmers/level2/review/일이사_나라의_숫자.java: -------------------------------------------------------------------------------- 1 | package dami.programmers.level2.review; 2 | 3 | // ** 이해 제대로 못함 4 | // 나머지 0일 때 왜 몫에서 1 빼야 하는건지? 나머지, 124나라 값이랑 몫 규칙순서 맞출라고?? 5 | /** 6 | * 7 | * 10진법 목(/3) 나머지(%3) 124나라 8 | * ------------------------------------------------ 9 | * 1 0 1 1 10 | * 2 0 2 2 11 | * 3 1 0 4 12 | * 4 1 1 11 13 | * 5 1 2 12 14 | * 6 2 0 14 15 | * 7 2 1 21 16 | * 8 2 2 22 17 | * 9 3 0 24 18 | * 10 3 1 41 19 | * 20 | */ 21 | public class 일이사_나라의_숫자 { 22 | public String solution(int n) { 23 | StringBuilder answer = new StringBuilder(); 24 | 25 | int remain = 0; 26 | while (n != 0) { 27 | remain = n % 3; 28 | n = (n - 1) / 3; 29 | 30 | answer.append("412".charAt(remain)); 31 | } 32 | 33 | return answer.reverse().toString(); 34 | } 35 | 36 | /* // 다른 풀이 37 | public String solution(int n) { 38 | String[] num = {"4","1","2"}; 39 | String answer = ""; 40 | 41 | while(n > 0){ 42 | answer = num[n % 3] + answer; 43 | n = (n - 1) / 3; 44 | } 45 | return answer; 46 | } 47 | */ 48 | } 49 | -------------------------------------------------------------------------------- /src/dami/programmers/level2/가장_큰_정사각형_찾기.java: -------------------------------------------------------------------------------- 1 | package dami.programmers.level2; 2 | 3 | import java.util.Arrays; 4 | 5 | // https://programmers.co.kr/learn/courses/30/lessons/12905 6 | public class 가장_큰_정사각형_찾기 { 7 | public int solution(int [][]board) { 8 | int max = Arrays.stream(board[0]).anyMatch(value -> value == 1) ? 1 : 0; 9 | for (int i = 1; i < board.length; i++) { 10 | for (int j = 1; j < board[i].length; j++) { 11 | if (board[i][j] == 0) { 12 | continue; 13 | } 14 | board[i][j] += minimumOfThree(board[i-1][j-1], board[i][j-1], board[i-1][j]); 15 | max = Math.max(max, board[i][j]); 16 | } 17 | } 18 | return max * max; 19 | } 20 | 21 | private int minimumOfThree(int one, int two, int three) { 22 | return Math.min(one, Math.min(two, three)); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/dami/programmers/level2/기능개발.java: -------------------------------------------------------------------------------- 1 | package dami.programmers.level2; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | // 1. 배포 일 수 배열 구하기 7 | // 2. 배포 일 수 배열 크기만큼 반복 8 | // - 다음 값이 현재 값보다 작으면 count++ 9 | // - 다음 값이 현재 값보다 크면 턴 넘기기 10 | public class 기능개발 { 11 | public int[] solution(int[] progresses, int[] speeds) { 12 | 13 | int[] deploys = new int[progresses.length]; 14 | for (int i = 0; i < progresses.length; i++) { 15 | int maybeDeploy = 100 - progresses[i]; 16 | if (speeds[i] == 1) { 17 | deploys[i] = maybeDeploy; 18 | continue; 19 | } 20 | deploys[i] = maybeDeploy / speeds[i] + (maybeDeploy % speeds[i] == 0 ? 0 : 1); 21 | } 22 | 23 | int i = 1; 24 | int current = 0; 25 | List answer = new ArrayList<>(); 26 | for (; i < deploys.length; i++) { 27 | if (deploys[current] < deploys[i]) { 28 | answer.add(i - current); 29 | current = i; 30 | } 31 | } 32 | answer.add(i - current); 33 | return answer.stream().mapToInt(a -> a).toArray(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/dami/programmers/level2/더_맵게.java: -------------------------------------------------------------------------------- 1 | package dami.programmers.level2; 2 | 3 | import java.util.PriorityQueue; 4 | 5 | // https://programmers.co.kr/learn/courses/30/lessons/42626 6 | // 처음엔 앞에서부터 합치도록 구현함 7 | // 테스트 케이스 실패 8 | // 반례 케이스 : [1, 1, 1, 1, 3] 9 | // 1회 합치면 [3, 1, 1, 3]이 되고, 가장 작은 + 그 다음 작은 수를 찾아야 하는데 정렬이 되어있지 않으니 앞에서부터 3, 1로 합치게 됨 10 | 11 | // 우선순위 큐 사용하도록 수정 12 | // 큐의 각 값에 우선순위가 정해져있으며 힙을 이용해 구현 13 | // 힙 : 완전 이진 트리의 일종으로 **여러 값 중 최대/최솟값을 빨리 찾기** 위한 자료구조 (중복 허용) 14 | // 최소 힙은 우선순위가 오름차순 정렬, 최대 힙은 우선순위가 내림차순 정렬 15 | // 힙 자료구조 : https://gmlwjd9405.github.io/2018/05/10/data-structure-heap.html 16 | // 자바의 PriorityQueue 사용 : https://coding-factory.tistory.com/603 17 | 18 | public class 더_맵게 { 19 | public int solution(int[] scoville, int K) { 20 | PriorityQueue scovilleQ = new PriorityQueue<>(); 21 | for (int scov : scoville) { 22 | scovilleQ.add(scov); 23 | } 24 | 25 | int count = 0; 26 | int min; 27 | while (scovilleQ.size() > 1) { 28 | min = scovilleQ.poll(); 29 | if (min >= K) { 30 | break; 31 | } 32 | 33 | min += scovilleQ.poll() * 2; 34 | scovilleQ.add(min); 35 | count++; 36 | } 37 | 38 | return scovilleQ.poll() >= K ? count : -1; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/dami/programmers/level2/두_원_사이의_정수쌍.java: -------------------------------------------------------------------------------- 1 | package dami.programmers.level2; 2 | 3 | public class 두_원_사이의_정수쌍 { 4 | 5 | public long solution(int r1, int r2) { 6 | int bigDiameter = Math.max(r1, r2) * 2; 7 | int smallDiameter = Math.min(r1, r2) * 2; 8 | return (long) (Math.pow(bigDiameter - 1, 2) - Math.pow(smallDiameter - 1, 2) + 4); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/dami/programmers/level2/땅따먹기.java: -------------------------------------------------------------------------------- 1 | package dami.programmers.level2; 2 | 3 | import java.util.Arrays; 4 | 5 | // https://programmers.co.kr/learn/courses/30/lessons/12913 6 | 7 | // 처음에는 단순히 아래로 내려가면서 최댓값을 sum 하는 반복문으로 풀었지만 8 | // 1 2 3 5 9 | // 5 6 7 100 10 | // 4 3 2 1 11 | // 위와 같은 예시일 경우 오답 12 | 13 | // 1. 두 번째 행부터 시작 14 | // 2. 각 인덱스마다 본인 인덱스 제외한 윗 행 값과 더한 값 중 최댓값으로 갱신 15 | // 3. 마지막 행까지 반복 16 | // 4. 마지막 행의 최댓값 반환 17 | public class 땅따먹기 { 18 | public int solution(int[][] land) { 19 | for (int i = 1; i < land.length; i++) { 20 | for (int j = 0; j < land[i].length; j++) { 21 | land[i][j] = findMaxSum(land[i-1], land[i][j], j); 22 | } 23 | } 24 | 25 | return Arrays.stream(land[land.length - 1]) 26 | .max() 27 | .orElse(0); 28 | } 29 | 30 | private int findMaxSum(int[] prevLine, int currentValue, int exceptIndex) { 31 | int max = 0; 32 | for (int i = 0; i < prevLine.length; i++) { 33 | if (i == exceptIndex) { 34 | continue; 35 | } 36 | max = Math.max(max, currentValue + prevLine[i]); 37 | } 38 | return max; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/dami/programmers/level2/멀쩡한_사각형.java: -------------------------------------------------------------------------------- 1 | package dami.programmers.level2; 2 | 3 | // 1. 두 수의 최대공약수 구하기 4 | // 2. 두 수에 최대공약수로 나눠서 나온 수로 만들어진 작은 직사각형의 대각선 커팅 시 잘린 사각형 개수 구하기 5 | // - 최대 공약수가 1인 두 수로 만들어진 직사각형의 잘린 사각형 개수 = 가로 + 세로 - 1 6 | // 3. 잘린 사각형 개수 * 최대공약수 = 처음에 주어진 두 수로 만들어진 직사각형의 전체 잘린 사각형 개수 7 | // ** 계산 시 형변환 주의 ** 8 | 9 | // - 수식 정리 10 | // 가로 W, 세로 H, W와 H의 최대공약수 gcd 11 | // gcd로 나눈 가로 w, 세로 h (w, h의 최대공약수는 1) 12 | // 멀쩡한 사각형 계산식 : W * H - (w + h - 1) * gcd 13 | // == W * H - (W + H - gcd) 14 | 15 | // #### 최대공약수 구하는 방법 16 | // - 유클리드 호제법 활용 17 | // - 유클리드 호제법은 mod 연산으로 사용 가능 18 | // ** mod 연산? : 두 값을 나눈 나머지를 구하는 연산 19 | 20 | /** 21 | * ex. 1112와 695의 최대공약수 구하기 22 | * 1112 mod 695 = 417 23 | * 695 mod 417 = 278 24 | * 417 mod 278 = 139 25 | * 278 mod 139 = 0 26 | * ==> 139가 1112와 695의 최대공약수 27 | */ 28 | public class 멀쩡한_사각형 { 29 | public long solution(int w, int h) { 30 | return ((long)w * h) - (w + h - calculateGcd(w, h)); 31 | } 32 | 33 | private int calculateGcd(int w, int h) { 34 | if (h > w) { 35 | int temp = w; 36 | w = h; 37 | h = temp; 38 | } 39 | 40 | int remainder; 41 | while (h != 0) { 42 | remainder = w % h; 43 | w = h; 44 | h = remainder; 45 | } 46 | return w; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/dami/programmers/level2/숫자의_표현.java: -------------------------------------------------------------------------------- 1 | package dami.programmers.level2; 2 | 3 | public class 숫자의_표현 { 4 | public int solution(int n) { 5 | int answer = 1; 6 | 7 | for (int i = 1; i <= n / 2; i++) { 8 | int sum = 0; 9 | for (int j = i; sum < n; j++) { 10 | sum += j; 11 | 12 | if (sum == n) { 13 | answer++; 14 | break; 15 | } 16 | } 17 | } 18 | return answer; 19 | } 20 | 21 | // 주어진 자연수를 연속된 자연수의 합으로 표현하는 방법의 수는 주어진 수의 홀수 약수의 개수와 같다라는 정수론 정리가 있습니다. 22 | public int solution2(int n) { 23 | int answer = 0; 24 | for (int i = 1; i <= n; i += 2) 25 | if (n % i == 0) 26 | answer++; 27 | return answer; 28 | } 29 | 30 | public static void main(String[] args) { 31 | System.out.println(new 숫자의_표현().solution2(15)); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/dami/programmers/level2/영어_끝말잇기.java: -------------------------------------------------------------------------------- 1 | package dami.programmers.level2; 2 | 3 | import java.util.*; 4 | 5 | public class 영어_끝말잇기 { 6 | public int[] solution(int n, String[] words) { 7 | int[] answer = new int[2]; 8 | int number; 9 | Set wordSet = new HashSet<>(); 10 | for (int i = 0; i < words.length; i++) { 11 | number = i + 1; 12 | String word = words[i]; 13 | if (wordSet.contains(word) || (i > 0 && !words[i - 1].endsWith(word.substring(0, 1)))) { 14 | if (number % n == 0) { 15 | answer[0] = n; 16 | answer[1] = number / n; 17 | } else { 18 | answer[0] = number % n; 19 | answer[1] = number / n + 1; 20 | } 21 | return answer; 22 | } 23 | wordSet.add(word); 24 | } 25 | 26 | return answer; 27 | } 28 | 29 | public static void main(String[] args) { 30 | int[] answer = new 영어_끝말잇기().solution(2, new String[]{"land", "dream", "mom", "mom", "ror"}); 31 | System.out.println(Arrays.toString(answer)); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/dami/programmers/level2/예상_대진표.java: -------------------------------------------------------------------------------- 1 | package dami.programmers.level2; 2 | 3 | // https://programmers.co.kr/learn/courses/30/lessons/12985 4 | 5 | public class 예상_대진표 { 6 | public int solution(int n, int a, int b) { 7 | int round = 1; 8 | while (divide(a) != divide(b)) { 9 | a = divide(a); 10 | b = divide(b); 11 | round++; 12 | } 13 | 14 | return round; 15 | } 16 | 17 | private int divide(int a) { 18 | return (a + 1) / 2; 19 | } 20 | } 21 | 22 | /* // 다른 풀이 23 | 24 | public int solution(int n, int a, int b) 25 | { 26 | return Integer.toBinaryString((a-1)^(b-1)).length(); 27 | } 28 | 29 | */ 30 | -------------------------------------------------------------------------------- /src/dami/programmers/level2/오픈채팅방.java: -------------------------------------------------------------------------------- 1 | package dami.programmers.level2; 2 | 3 | import java.util.ArrayList; 4 | import java.util.HashMap; 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | // https://programmers.co.kr/learn/courses/30/lessons/42888 9 | public class 오픈채팅방 { 10 | private static final String[] COMMAND = new String[]{"Enter", "Leave", "Change"}; 11 | private static final Map MESSAGES = new HashMap<>(); 12 | static { 13 | MESSAGES.put("Enter", "님이 들어왔습니다"); 14 | MESSAGES.put("Leave", "님이 나갔습니다."); 15 | } 16 | 17 | public String[] solution(String[] record) { 18 | Map userMap = new HashMap<>(); 19 | List results = new ArrayList<>(); 20 | 21 | for (String r : record) { 22 | String[] line = r.split(" "); 23 | if (!line[0].equals(COMMAND[1])) { 24 | userMap.put(line[1], line[2]); 25 | } 26 | 27 | if (!line[0].equals(COMMAND[2])) { 28 | results.add(new StringBuilder(line[1] + MESSAGES.get(line[0]))); 29 | } 30 | } 31 | 32 | for (StringBuilder result : results) { 33 | int uidLength = result.indexOf("님"); 34 | result.replace(0, uidLength, userMap.get(result.substring(0, uidLength))); 35 | } 36 | 37 | return results.stream() 38 | .map(StringBuilder::toString) 39 | .toArray(String[]::new); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/dami/programmers/level2/올바른_괄호.java: -------------------------------------------------------------------------------- 1 | package dami.programmers.level2; 2 | 3 | import java.util.Stack; 4 | 5 | public class 올바른_괄호 { 6 | boolean solution(String s) { 7 | Stack brackets = new Stack<>(); 8 | for (int i = 0; i < s.length(); i++) { 9 | char current = s.charAt(i); 10 | 11 | if (current == '(') { 12 | brackets.push(current); 13 | continue; 14 | } 15 | if (current == ')') { 16 | if (brackets.isEmpty()) { 17 | return false; 18 | } 19 | brackets.pop(); 20 | } 21 | } 22 | return brackets.isEmpty(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/dami/programmers/level2/요격_시스템.java: -------------------------------------------------------------------------------- 1 | package dami.programmers.level2; 2 | 3 | import java.util.Arrays; 4 | import java.util.Comparator; 5 | 6 | public class 요격_시스템 { 7 | 8 | public int solution(int[][] targets) { 9 | Arrays.sort(targets, Comparator.comparing(target -> target[1])); 10 | 11 | int result = 0; 12 | double last = -1.0; 13 | for (int[] target : targets) { 14 | if (target[0] < last) { 15 | continue; 16 | } 17 | 18 | result++; 19 | last = target[1] - 0.1; 20 | } 21 | return result; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/dami/programmers/level2/점프와_순간_이동.java: -------------------------------------------------------------------------------- 1 | package dami.programmers.level2; 2 | 3 | //https://programmers.co.kr/learn/courses/30/lessons/12980 4 | 5 | public class 점프와_순간_이동 { 6 | 7 | public int solution(int n) { 8 | int result = 0; 9 | 10 | while (n > 1) { 11 | if (n % 2 != 0) { 12 | result++; 13 | } 14 | n /= 2; 15 | } 16 | 17 | return ++result; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/dami/programmers/level2/짝지어_제거하기.java: -------------------------------------------------------------------------------- 1 | package dami.programmers.level2; 2 | 3 | import java.util.Stack; 4 | 5 | // https://programmers.co.kr/learn/courses/30/lessons/12973 6 | public class 짝지어_제거하기 { 7 | public int solution_2020(String s) { 8 | Stack stack = new Stack<>(); 9 | stack.push(s.charAt(0)); 10 | for (int i = 1; i < s.length(); i++) { 11 | if (!stack.isEmpty() && stack.peek() == s.charAt(i)) { 12 | stack.pop(); 13 | } else { 14 | stack.push(s.charAt(i)); 15 | } 16 | } 17 | return stack.size() > 0 ? 0 : 1; 18 | } 19 | 20 | public int solution_2023(String s) { 21 | // baabaa cdcd stack? 22 | Stack stack = new Stack<>(); 23 | char[] chars = s.toCharArray(); 24 | for (char c : chars) { 25 | if (!stack.isEmpty() && stack.peek() == c) { 26 | stack.pop(); 27 | } else { 28 | stack.push(c); 29 | } 30 | } 31 | 32 | return stack.isEmpty() ? 1 : 0; 33 | } 34 | 35 | public static void main(String[] args) { 36 | System.out.println(new 짝지어_제거하기().solution_2023("a")); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/dami/programmers/level2/최댓값과_최솟값.java: -------------------------------------------------------------------------------- 1 | package dami.programmers.level2; 2 | 3 | public class 최댓값과_최솟값 { 4 | public String solution(String s) { 5 | String[] integers = s.split(" "); 6 | 7 | int min = Integer.MAX_VALUE; 8 | int max = Integer.MIN_VALUE; 9 | for (String integer : integers) { 10 | min = Math.min(min, Integer.parseInt(integer)); 11 | max = Math.max(max, Integer.parseInt(integer)); 12 | } 13 | 14 | return min + " " + max; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/dami/programmers/level2/카펫.java: -------------------------------------------------------------------------------- 1 | package dami.programmers.level2; 2 | 3 | import java.util.Arrays; 4 | 5 | public class 카펫 { 6 | public int[] solution(int brown, int yellow) { 7 | int[] answer = {}; 8 | for (int i = 1; i <= yellow / i; i++) { 9 | if (yellow % i == 0) { 10 | int height = i + 2; 11 | int width = (yellow / i) + 2; 12 | 13 | int round = (width + height) * 2 - 4; 14 | if (brown == round) { 15 | return new int[]{width, height}; 16 | } 17 | } 18 | } 19 | return answer; 20 | } 21 | 22 | public int[] solution_wow(int brown, int red) { 23 | int a = (brown+4)/2; 24 | int b = red+2*a-4; 25 | int[] answer = {(int)(a+Math.sqrt(a*a-4*b))/2,(int)(a-Math.sqrt(a*a-4*b))/2}; 26 | return answer; 27 | } 28 | 29 | public static void main(String[] args) { 30 | System.out.println(Arrays.toString(new 카펫().solution(24, 24))); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/dami/programmers/level2/튜플.java: -------------------------------------------------------------------------------- 1 | package dami.programmers.level2; 2 | 3 | import java.util.Arrays; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | 7 | // https://programmers.co.kr/learn/courses/30/lessons/64065 8 | 9 | // 1. 중괄호 제거 10 | // 2. 쉼표로 split 11 | // {{4,2,3},{3},{2,3,4,1},{2,3}} 12 | // 4,2,3,3,2,3,4,1,2,3 13 | // 3. HashMap에 put 14 | // key : 각 숫자, value : 숫자 빈도수 15 | // 4. 빈도수 오름차순 정렬해서 return 16 | 17 | public class 튜플 { 18 | public int[] solution(String s) { 19 | s = s.replaceAll("\\{", "") 20 | .replaceAll("}", ""); 21 | 22 | String[] numbers = s.split(","); 23 | Map numberCountMap = new HashMap<>(); 24 | for(String number : numbers) { 25 | numberCountMap.put(number, numberCountMap.getOrDefault(number, 0) + 1); 26 | } 27 | 28 | return numberCountMap.keySet() 29 | .stream() 30 | .sorted((key1, key2) -> numberCountMap.get(key2).compareTo(numberCountMap.get(key1))) 31 | .mapToInt(Integer::parseInt) 32 | .toArray(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/dami/programmers/level2/폰켓몬.java: -------------------------------------------------------------------------------- 1 | package dami.programmers.level2; 2 | 3 | import java.util.Arrays; 4 | import java.util.Set; 5 | import java.util.stream.Collectors; 6 | 7 | // https://programmers.co.kr/learn/courses/30/lessons/1845 8 | 9 | // 1. 최대 종류 수는 nums.length / 2 10 | // 2. 주어진 배열을 set에 저장 11 | // 3. set의 크기와 최대 종류 수 중 작은 값 return 12 | 13 | public class 폰켓몬 { 14 | public int solution(int[] nums) { 15 | int maxType = nums.length / 2; 16 | Set numSet = Arrays.stream(nums) 17 | .boxed() 18 | .collect(Collectors.toSet()); 19 | 20 | return Math.min(numSet.size(), maxType); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/dami/programmers/level2/피보나치_수.java: -------------------------------------------------------------------------------- 1 | package dami.programmers.level2; 2 | 3 | import java.math.BigDecimal; 4 | import java.util.Arrays; 5 | import java.util.List; 6 | 7 | public class 피보나치_수 { 8 | 9 | private static final int[] FIVO = new int[100001]; 10 | 11 | public int solution(int n) { 12 | Arrays.fill(FIVO, -1); 13 | for (int i = 0; i <= n; i++) { 14 | fivo(i); 15 | } 16 | return FIVO[n]; 17 | } 18 | 19 | private int fivo(int n) { 20 | if (FIVO[n] != -1) return FIVO[n]; 21 | if (n == 0) return 0; 22 | if (n == 1) return 1; 23 | 24 | return FIVO[n] = (fivo(n - 1) + fivo(n - 2)) % 1234567; 25 | } 26 | 27 | public static void main(String[] args) { 28 | System.out.println(new 피보나치_수().solution(100000)); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/dami/programmers/level3/단속카메라.java: -------------------------------------------------------------------------------- 1 | package dami.programmers.level3; 2 | 3 | import java.util.Arrays; 4 | import java.util.Comparator; 5 | 6 | public class 단속카메라 { 7 | public int solution(int[][] routes) { 8 | Arrays.sort(routes, Comparator.comparingInt(route -> route[1])); 9 | 10 | int camera = 0; 11 | int point = Integer.MIN_VALUE; 12 | for (int[] route : routes) { 13 | // [i][1] 오름차순으로 정렬을 해주었기 때문에 [i][0] 만 비교해주어도 됨 14 | if (route[0] > point) { 15 | camera++; 16 | point = route[1]; 17 | } 18 | } 19 | 20 | return camera; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/dami/questions/Task2.java: -------------------------------------------------------------------------------- 1 | package dami.questions; 2 | 3 | import java.util.Arrays; 4 | 5 | // 완 6 | // 백준 '빗물' 문제와 유사 7 | public class Task2 { 8 | public void solution(int day, int width, int[][] blocks) { // 작업일, 너비, 공사 기간 동안 쌓을 벽돌 개수 9 | int[] workResult = new int[width]; 10 | int cement = 0; 11 | for (int i = 0; i < day; i++) { 12 | // 다음날 벽돌 추가 13 | for (int j = 0; j < width; j++) { 14 | workResult[j] += blocks[i][j]; 15 | } 16 | 17 | int left = 0, right = width - 1; 18 | while (canWork(workResult)) { 19 | // 시멘트 작업 가능 범위 확인 20 | while (workResult[left] <= 0) { 21 | left++; 22 | } 23 | while (workResult[right] <= 0) { 24 | right--; 25 | } 26 | int min = Math.min(workResult[left], workResult[right]); 27 | 28 | for (int j = 0; j < width; j++) { 29 | // 시멘트 작업 가능 범위 내에만 시멘트 추가 30 | if (j >= left && j <= right && workResult[j] < min) { 31 | cement += min - workResult[j]; 32 | workResult[j] += min - workResult[j]; 33 | } 34 | workResult[j] -= min; 35 | } 36 | } 37 | } 38 | 39 | System.out.println(cement); 40 | } 41 | 42 | // 시멘트가 넘치지 않도록 양쪽 사이드 벽이 될 수 있는지 판단 43 | private boolean canWork(int[] blocks) { 44 | return Arrays.stream(blocks) 45 | .filter(block -> block > 0) 46 | .count() >= 2; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/dami/questions/Task4.java: -------------------------------------------------------------------------------- 1 | package dami.questions; 2 | 3 | public class Task4 { 4 | public int solution(int N) { 5 | int digit = (int) Math.log10(N); 6 | if (digit == 0) { 7 | return 0; 8 | } 9 | return (int) Math.pow(10, digit); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/dami/questions/Task5.java: -------------------------------------------------------------------------------- 1 | package dami.questions; 2 | 3 | // 참고 : https://github.com/geektimus/java-challenges/blob/master/src/main/java/com/codingmaniacs/codility/NumericChallenges.java 4 | public class Task5 { 5 | public int solution(int N, int K) { 6 | if (K == 0) { 7 | return N - 1; 8 | } 9 | 10 | int minPlays = 0; 11 | 12 | while (N > 1 && K > 0) { 13 | if (N % 2 == 0) { 14 | K--; 15 | N /= 2; 16 | } else { 17 | N--; 18 | } 19 | minPlays++; 20 | } 21 | 22 | if (N > 1) { 23 | minPlays += N - 1; 24 | } 25 | return minPlays; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/delf/dailyprogramming/CT116.java: -------------------------------------------------------------------------------- 1 | package dailyprogramming; 2 | 3 | import java.util.Arrays; 4 | 5 | /* 6 | * 빗물 담기 문제(Trapping Rain Water Problem)입니다. 7 | * 8 | * 너비가 1인 막대기들이 서 있고, 그 길이가 배열로 주어집니다. 9 | * 여기에 비가 내려 물이 고인다고 할 때, 담기는 물의 최대 양을 구하시오. 10 | * 11 | * Input: [7, 0, 4, 2, 5, 0, 6, 4, 0, 5] 12 | * Output: 25 13 | */ 14 | @SuppressWarnings("Duplicates") 15 | public class CT116 { 16 | public static void main(String[] args) { 17 | System.out.println(new CT116().solution(new int[]{7, 0, 4, 2, 5, 0, 6, 4, 0, 5})); 18 | } 19 | 20 | public int solution(int[] sticks) { 21 | int waterVolume = 0; 22 | int maxHeight = 0; 23 | int maxIndex = 0; 24 | for (int i = 0; i < sticks.length; i++) { 25 | if (sticks[i] > maxHeight) { // 벽 찾아 26 | for (int j = maxIndex + 1; j < i; j++) { // 물 채워 27 | waterVolume += (maxHeight - sticks[j]); 28 | sticks[j] = maxHeight; 29 | } 30 | maxHeight = sticks[i]; 31 | maxIndex = i; 32 | } 33 | } 34 | 35 | maxHeight = 0; 36 | maxIndex = 0; 37 | for (int i = sticks.length - 1; i >= 0; i--) { 38 | if (sticks[i] > maxHeight) { 39 | for (int j = maxIndex - 1; j > i; j--) { 40 | waterVolume += (maxHeight - sticks[j]); 41 | sticks[j] = maxHeight; 42 | } 43 | maxHeight = sticks[i]; 44 | maxIndex = i; 45 | } 46 | } 47 | return waterVolume; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/delf/etc/appdevmatching2020/Solution.java: -------------------------------------------------------------------------------- 1 | package etc.appdevmatching2020; 2 | 3 | 4 | import java.util.*; 5 | import java.util.stream.Collectors; 6 | 7 | public class Solution { 8 | public int solution(long n) { 9 | long tmp = n; 10 | int cnt = 0; 11 | Set set = new HashSet<>(); 12 | do { 13 | long div = n % 10; 14 | if (div != 0 && !set.contains(div) && tmp % (div) == 0) { 15 | cnt++; 16 | } 17 | set.add(div); 18 | } while ((n /= 10) > 0); 19 | 20 | return cnt; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/delf/etc/devmatching2021/Solution1.java: -------------------------------------------------------------------------------- 1 | package etc.devmatching2021; 2 | 3 | import java.util.Arrays; 4 | import java.util.HashSet; 5 | import java.util.Set; 6 | 7 | public class Solution1 { 8 | public static void main(String[] args) { 9 | System.out.println(Arrays.toString(new Solution1().solution(new int[]{44, 1, 0, 0, 31, 25}, new int[]{31, 10, 45, 1, 6, 19}))); 10 | System.out.println(Arrays.toString(new Solution1().solution(new int[]{0, 0, 0, 0, 0, 0}, new int[]{38, 19, 20, 40, 15, 25}))); 11 | System.out.println(Arrays.toString(new Solution1().solution(new int[]{45, 4, 35, 20, 3, 9}, new int[]{20, 9, 3, 45, 4, 35}))); 12 | } 13 | 14 | private static final int[] WIN = new int[]{6, 6, 5, 4, 3, 2, 1}; 15 | 16 | public int[] solution(int[] lottos, int[] winNums) { 17 | Set set = new HashSet<>(); 18 | for (int winNum : winNums) { 19 | set.add(winNum); 20 | } 21 | int empty = 0; 22 | for (Integer lotto : lottos) { 23 | if (lotto == 0) { 24 | empty++; 25 | } 26 | set.remove(lotto); 27 | } 28 | return new int[]{WIN[6 - set.size() + empty], WIN[6 - set.size()]}; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/delf/etc/karecoint2020/Solution01.java: -------------------------------------------------------------------------------- 1 | package etc.karecoint2020; 2 | 3 | public class Solution01 { 4 | public static void main(String[] args) { 5 | System.out.println(new Solution01().solution(2, 2)); 6 | 7 | } 8 | 9 | public int solution(int m, int w) { 10 | return simplePermutation(m + w, 3) - ((simplePermutation(m, 3) + simplePermutation(w, 3))); 11 | } 12 | 13 | private int simplePermutation(int n, int r) { 14 | long m = 1; 15 | for (int i = n; i > n - r; i--) { 16 | m *= i; 17 | } 18 | System.out.println("m = " + m); 19 | long s = 1; 20 | for (int i = r; i > 0; i--) { 21 | s *= i; 22 | } 23 | System.out.println("s = " + s); 24 | return (int) (m / s); 25 | } 26 | } -------------------------------------------------------------------------------- /src/delf/hackerrank/ATaleofTwoStacks.java: -------------------------------------------------------------------------------- 1 | package hackerrank; 2 | 3 | import java.util.Arrays; 4 | import java.util.LinkedList; 5 | 6 | public class ATaleofTwoStacks { 7 | public static void main(String[] args) { 8 | 9 | Arrays.sort(new long[0]); 10 | } 11 | class MyQueue { 12 | 13 | private LinkedList list = new LinkedList<>(); 14 | 15 | void enqueue(T e) { 16 | list.add(e); 17 | } 18 | 19 | T dequeue() { 20 | return list.removeFirst(); 21 | } 22 | 23 | T peek() { 24 | return list.getLast(); 25 | } 26 | } 27 | 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/delf/hackerrank/Abbreviation.java: -------------------------------------------------------------------------------- 1 | package hackerrank; 2 | 3 | /** 4 | * Abbreviation 5 | * https://www.hackerrank.com/challenges/abbreviation/problem 6 | */ 7 | public class Abbreviation { 8 | public static void main(String[] args) { 9 | System.out.println(abbreviation("daBcd", "ABC")); 10 | System.out.println(abbreviation("AfPZN", "APZNC")); 11 | } 12 | 13 | static String abbreviation(String a, String b) { 14 | boolean[][] dp = new boolean[a.length() + 1][b.length() + 1]; 15 | dp[0][0] = true; 16 | 17 | for (int i = 1; i < a.length() + 1; i++) { 18 | for (int j = 0; j < b.length() + 1; j++) { 19 | char aa = a.charAt(i - 1); 20 | if (j > 0 && dp[i - 1][j - 1]) { 21 | char bb = b.charAt(j - 1); 22 | if (aa == bb || Character.toUpperCase(aa) == bb) { 23 | dp[i][j] = true; 24 | } 25 | } 26 | if (Character.isLowerCase(aa) && dp[i - 1][j]) { 27 | dp[i][j] = true; 28 | } 29 | } 30 | } 31 | return dp[a.length()][b.length()] ? "YES" : "NO"; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/delf/hackerrank/BalancedBrackets.java: -------------------------------------------------------------------------------- 1 | package hackerrank; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | import java.util.Stack; 6 | 7 | 8 | /** 9 | * Balanced Brackets 10 | * https://www.hackerrank.com/challenges/balanced-brackets/problem 11 | */ 12 | public class BalancedBrackets { 13 | 14 | static Map pair = new HashMap<>(); 15 | 16 | static { 17 | pair.put('(', ')'); 18 | pair.put('{', '}'); 19 | pair.put('[', ']'); 20 | } 21 | 22 | static String isBalanced(String s) { 23 | Stack stack = new Stack<>(); 24 | 25 | for (char ch : s.toCharArray()) { 26 | if (ch == ')' || ch == '}' || ch == ']') { 27 | if (stack.isEmpty()) { 28 | return "NO"; 29 | } 30 | char peek = stack.peek(); 31 | if (pair.get(peek) == ch) { 32 | stack.pop(); 33 | } else { 34 | return "NO"; 35 | } 36 | } else { 37 | stack.add(ch); 38 | } 39 | } 40 | 41 | return stack.isEmpty() ? "YES" : "NO"; 42 | } 43 | 44 | public static void main(String[] args) { 45 | System.out.println(isBalanced("{{[[(())]]}}")); 46 | System.out.println(isBalanced("{[(])}")); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/delf/hackerrank/Candies.java: -------------------------------------------------------------------------------- 1 | package hackerrank; 2 | 3 | /** 4 | * Abbreviation 5 | * https://www.hackerrank.com/challenges/abbreviation/problem 6 | */ 7 | public class Candies { 8 | public static void main(String[] args) { 9 | int[] arr = new int[]{10, 2, 4, 2, 6, 1, 7, 8, 9, 2, 1}; 10 | System.out.println(candies(arr.length, arr)); 11 | } 12 | 13 | static long candies(int n, int[] arr) { 14 | 15 | int[] up = new int[arr.length]; 16 | int[] down = new int[arr.length]; 17 | 18 | up[0] = 1; 19 | down[arr.length - 1] = 1; 20 | 21 | for (int i = 1; i < arr.length; i++) { 22 | if (arr[i - 1] < arr[i]) { 23 | up[i] = up[i - 1] + 1; 24 | } else { 25 | up[i] = 1; 26 | } 27 | } 28 | 29 | for (int i = arr.length - 2; i >= 0; i--) { 30 | int t = (arr[i + 1] < arr[i]) ? (down[i + 1] + 1) : 1; 31 | down[i] = Math.max(down[i], t); 32 | 33 | if (arr[i + 1] < arr[i]) { 34 | down[i] = Math.max(down[i], down[i + 1] + 1); 35 | } else { 36 | down[i] = 1; 37 | } 38 | } 39 | 40 | long answer = 0; 41 | for (int i = 0; i < arr.length; i++) { 42 | answer += Math.max(up[i], down[i]); 43 | } 44 | 45 | return answer; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/delf/hackerrank/DavisStaircase.java: -------------------------------------------------------------------------------- 1 | package hackerrank; 2 | 3 | /** 4 | * Recursion: Davis' Staircase 5 | * https://www.hackerrank.com/challenges/ctci-recursive-staircase/problem 6 | */ 7 | public class DavisStaircase { 8 | static int stepPerms(int n) { 9 | if (n < 3) { 10 | return n; 11 | } 12 | if (n == 3) { 13 | return 4; 14 | } 15 | 16 | int[] dp = new int[n]; 17 | 18 | dp[0] = 1; 19 | dp[1] = 2; 20 | dp[2] = 4; 21 | for (int i = 3; i < n; i++) { 22 | dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3]; 23 | } 24 | return dp[n - 1]; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/delf/hackerrank/HuffmanDecoding.java: -------------------------------------------------------------------------------- 1 | package hackerrank; 2 | 3 | import java.util.Objects; 4 | 5 | /** 6 | * Huffman Decoding 7 | * https://www.hackerrank.com/challenges/tree-huffman-decoding/problem 8 | */ 9 | public class HuffmanDecoding { 10 | static void decode(String s, Node root) { 11 | String answer = ""; 12 | Node base = Objects.requireNonNull(root); 13 | while (!s.isEmpty()) { 14 | base = s.charAt(0) == '1' ? Objects.requireNonNull(base.right) : Objects.requireNonNull(base.left); 15 | s = s.substring(1); 16 | 17 | if (Objects.isNull(base.left) && Objects.isNull(base.right)) { 18 | answer += base.data; 19 | base = root; 20 | } 21 | } 22 | System.out.println(answer); 23 | } 24 | 25 | class Node { 26 | public int frequency; // the frequency of this tree 27 | public char data; 28 | public Node left, right; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/delf/hackerrank/IsThisABinarySearchTree.java: -------------------------------------------------------------------------------- 1 | package hackerrank; 2 | 3 | import com.sun.corba.se.pept.encoding.InputObject; 4 | 5 | public class IsThisABinarySearchTree { 6 | boolean checkBST(Node root) { 7 | return isBinarySearchTree(root); 8 | } 9 | 10 | private boolean isBinarySearchTree(Node node) { 11 | if (node.left == null) { 12 | return node.right == null; 13 | } else { 14 | if (node.right == null) { 15 | return false; 16 | } else { 17 | return (node.left.data < node.data && node.data < node.right.data) && (isBinarySearchTree(node.left) && isBinarySearchTree(node.right)); 18 | } 19 | } 20 | } 21 | 22 | class Node { 23 | int data; 24 | Node left; 25 | Node right; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/delf/hackerrank/LargestRectangle.java: -------------------------------------------------------------------------------- 1 | package hackerrank; 2 | 3 | import java.util.Stack; 4 | 5 | /** 6 | * Largest Rectangle 7 | * https://www.hackerrank.com/challenges/largest-rectangle/problem 8 | */ 9 | public class LargestRectangle { 10 | static long largestRectangle(int[] h) { 11 | Stack stack = new Stack<>(); 12 | int maxArea = 0; 13 | int top; 14 | 15 | int i = 0; 16 | while (i < h.length) { 17 | if (stack.empty() || h[stack.peek()] <= h[i]) { 18 | stack.push(i++); 19 | } else { 20 | top = h[stack.pop()] * (stack.empty() ? i : i - stack.peek() - 1); 21 | maxArea = Math.max(maxArea, top); 22 | } 23 | } 24 | 25 | while (!stack.empty()) { 26 | top = h[stack.pop()] * (stack.empty() ? i : i - stack.peek() - 1); 27 | maxArea = Math.max(maxArea, top); 28 | } 29 | 30 | return maxArea; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/delf/hackerrank/MaxArraySum.java: -------------------------------------------------------------------------------- 1 | package hackerrank; 2 | 3 | /** 4 | * Max Array Sum 5 | * https://www.hackerrank.com/challenges/max-array-sum/problem 6 | */ 7 | public class MaxArraySum { 8 | public static void main(String[] args) { 9 | System.out.println(maxSubsetSum(new int[]{3, 5, -7, 8, 10})); 10 | } 11 | 12 | static int maxSubsetSum(int[] arr) { 13 | int[] dp = new int[arr.length + 1]; 14 | dp[0] = 0; 15 | dp[1] = arr[0]; 16 | dp[2] = arr[1]; 17 | 18 | int max = 0; 19 | for (int i = 3; i <= arr.length; i++) { 20 | int tmpMax = Math.max(dp[i - 2], dp[i - 3]); 21 | if (arr[i - 1] >= 0) { 22 | dp[i] = Math.max(arr[i - 1], tmpMax + arr[i - 1]); 23 | } else { 24 | dp[i] = tmpMax; 25 | } 26 | 27 | max = Math.max(max, dp[i]); 28 | } 29 | return max; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/delf/hackerrank/MinimumSwap2.java: -------------------------------------------------------------------------------- 1 | package hackerrank; 2 | 3 | /* Minimum Swaps 2 4 | * https://www.hackerrank.com/challenges/minimum-swaps-2/problem 5 | * */ 6 | public class MinimumSwap2 { 7 | static int minimumSwaps(int[] arr) { 8 | int cnt = 0; 9 | for (int i = 0; i < arr.length; i++) { 10 | int idx = arr[i]; 11 | if (idx == (i + 1)) { 12 | continue; 13 | } 14 | arr[i] = arr[idx - 1]; 15 | arr[idx - 1] = idx; 16 | cnt++; 17 | i--; 18 | } 19 | return cnt; 20 | } 21 | } -------------------------------------------------------------------------------- /src/delf/hackerrank/MinimumTimeRequired.java: -------------------------------------------------------------------------------- 1 | package hackerrank; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * Minimum Time Required 7 | * https://www.hackerrank.com/challenges/minimum-time-required/problem 8 | */ 9 | public class MinimumTimeRequired { 10 | 11 | static long minTime(long[] machines, long goal) { 12 | Arrays.sort(machines); 13 | return binarySearchRecursive(machines, goal, 1, machines[machines.length - 1] * goal); 14 | } 15 | 16 | static long binarySearchRecursive(long[] machines, long goal, long min, long max) { 17 | if (min == max) { 18 | return min; 19 | } 20 | 21 | long mid = (min + max) / 2; 22 | long produce = 0; 23 | 24 | for (long machine : machines) { 25 | produce += Math.floor((double) (mid / machine)); 26 | } 27 | 28 | if (produce < goal) { 29 | return binarySearchRecursive(machines, goal, mid + 1, max); 30 | } else { 31 | return binarySearchRecursive(machines, goal, min, mid); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/delf/hackerrank/Pairs.java: -------------------------------------------------------------------------------- 1 | package hackerrank; 2 | 3 | import java.util.Arrays; 4 | import java.util.HashSet; 5 | import java.util.Set; 6 | 7 | /** 8 | * Pairs 9 | * https://www.hackerrank.com/challenges/pairs/problem 10 | */ 11 | public class Pairs { 12 | static int pairs(int k, Integer[] arr) { 13 | Set set = new HashSet<>(Arrays.asList(arr)); 14 | return (int) Arrays.stream(arr).filter(v -> set.contains(v - k)).count(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/delf/hackerrank/PoisonousPlants.java: -------------------------------------------------------------------------------- 1 | package hackerrank; 2 | 3 | import java.util.*; 4 | import java.util.stream.Collectors; 5 | 6 | /** 7 | * Poisonous Plants 8 | * https://www.hackerrank.com/challenges/poisonous-plants/problem 9 | */ 10 | public class PoisonousPlants { 11 | 12 | public static void main(String[] args) { 13 | System.out.println(poisonousPlants(new int[]{6, 5, 8, 4, 7, 10, 9})); 14 | // System.out.println(poisonousPlants(new int[]{4, 3, 7, 5, 6, 4, 2})); 15 | } 16 | 17 | static int poisonousPlants(int[] p) { 18 | 19 | LinkedList list = Arrays.stream(p).boxed().collect(Collectors.toCollection(LinkedList::new)); 20 | 21 | int answer = 0; 22 | while (true) { 23 | LinkedList tmpList = new LinkedList<>(); 24 | int tmp = list.getFirst(); 25 | tmpList.add(tmp); 26 | for (int i = 1; i < list.size(); i++) { 27 | int n = list.get(i); 28 | if (tmp > n) { 29 | tmpList.add(n); 30 | } 31 | tmp = n; 32 | } 33 | if (list.size() == tmpList.size()) { 34 | return answer; 35 | } 36 | answer++; 37 | list = tmpList; 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/delf/hackerrank/ReverseShuffleMerge.java: -------------------------------------------------------------------------------- 1 | package hackerrank; 2 | 3 | import java.util.Arrays; 4 | import java.util.Stack; 5 | 6 | /** 7 | * Reverse Shuffle Merge 8 | * https://www.hackerrank.com/challenges/reverse-shuffle-merge/problem 9 | */ 10 | public class ReverseShuffleMerge { 11 | public static void main(String[] args) { 12 | System.out.println(reverseShuffleMerge2("zbayc")); 13 | } 14 | 15 | static String reverseShuffleMerge2(final String input) { 16 | char[] reversed = new StringBuilder(input).reverse().toString().toCharArray(); 17 | char[] sorted = input.toCharArray(); 18 | Arrays.sort(sorted); 19 | 20 | StringBuilder result = new StringBuilder(); 21 | 22 | int revIdx = 0, srtIdx = 0; 23 | while (revIdx < reversed.length && srtIdx < sorted.length) { 24 | char r = reversed[revIdx]; 25 | char s = sorted[srtIdx]; 26 | 27 | if (r > s) { 28 | result.append(s); 29 | srtIdx++; 30 | } else { 31 | result.append(r); 32 | revIdx++; 33 | } 34 | } 35 | 36 | return result.toString(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/delf/hackerrank/SortComparator.java: -------------------------------------------------------------------------------- 1 | package hackerrank; 2 | 3 | import java.util.Comparator; 4 | 5 | /** 6 | * Sorting: Comparator 7 | * https://www.hackerrank.com/challenges/ctci-comparator-sorting/problem 8 | */ 9 | public class SortComparator { 10 | static class Checker implements Comparator { 11 | public int compare(Player a, Player b) { 12 | if (a.score == b.score) { 13 | return a.name.compareTo(b.name); 14 | } 15 | return b.score - a.score; 16 | } 17 | } 18 | 19 | static class Player { 20 | String name; 21 | int score; 22 | 23 | Player(String name, int score) { 24 | this.name = name; 25 | this.score = score; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/delf/hackerrank/TripleSum.java: -------------------------------------------------------------------------------- 1 | package hackerrank; 2 | 3 | import java.util.*; 4 | import java.util.stream.Collectors; 5 | 6 | /** 7 | * Triple sum 8 | * https://www.hackerrank.com/challenges/triple-sum/problem 9 | */ 10 | public class TripleSum { 11 | static long triplets(int[] a, int[] b, int[] c) { 12 | List listA = Arrays.stream(a).sorted().distinct().boxed().collect(Collectors.toList()); 13 | List listB = Arrays.stream(b).sorted().distinct().boxed().collect(Collectors.toList()); 14 | List listC = Arrays.stream(c).sorted().distinct().boxed().collect(Collectors.toList()); 15 | 16 | long answer = 0; 17 | 18 | int countA = 0, countB = 0, countC = 0; 19 | while (countB < b.length) { 20 | while (countA < a.length && listA.get(countA) <= listB.get(countB)) { 21 | countA++; 22 | } 23 | while (countC < c.length && listC.get(countC) <= listB.get(countB)) { 24 | countC++; 25 | } 26 | answer += (long) countA * (long) countC; 27 | countB++; 28 | } 29 | return answer; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/delf/interviewguidemission/StackMission1.java: -------------------------------------------------------------------------------- 1 | package interviewguidemission; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | public class StackMission1 { 7 | public static void main(String[] args) { 8 | System.out.println(new StackMission1().solution("[{1+2*(2+2)}-(1-3)]", 1)); 9 | } 10 | 11 | public int solution(String s, int index) { 12 | List open = Arrays.asList('(', '{', '['); 13 | List close = Arrays.asList(')', '}', ']'); 14 | char openTarget = s.charAt(index); 15 | char closeTarget = close.get(open.indexOf(openTarget)); 16 | boolean listen = false; 17 | for (int i = 0; i < s.length(); i++) { 18 | char ch = s.charAt(i); 19 | if (ch == openTarget) { 20 | listen = true; 21 | continue; 22 | } 23 | if (listen && ch == closeTarget) { 24 | return i; 25 | } 26 | } 27 | throw new IllegalStateException(); 28 | } 29 | } -------------------------------------------------------------------------------- /src/delf/interviewguidemission/StackMission2.java: -------------------------------------------------------------------------------- 1 | package interviewguidemission; 2 | 3 | import java.util.*; 4 | 5 | public class StackMission2 { 6 | public static void main(String[] args) { 7 | System.out.println(new StackMission2().solution("{{{}}")); 8 | System.out.println(new StackMission2().solution("{{{{}}")); 9 | System.out.println(new StackMission2().solution("}}}}{}}}")); 10 | System.out.println(new StackMission2().solution("{{{{")); 11 | } 12 | public int solution(String s) { 13 | Stack stack = new Stack<>(); 14 | int cnt = 0; 15 | for (char ch : s.toCharArray()) { 16 | if (stack.isEmpty()) { 17 | if (ch == '}') { 18 | cnt++; 19 | stack.push('{'); 20 | } else { 21 | stack.push(ch); 22 | } 23 | continue; 24 | } 25 | 26 | if (ch == '{') { 27 | stack.push(ch); 28 | } else if (ch == '}' && stack.peek() == '{') { 29 | stack.pop(); 30 | } 31 | } 32 | 33 | if (stack.isEmpty()) { 34 | return cnt; 35 | } 36 | 37 | if (stack.size() % 2 != 0) { 38 | return -1; 39 | } 40 | 41 | return cnt + stack.size() / 2; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/delf/oncoder/Challenge10.java: -------------------------------------------------------------------------------- 1 | package oncoder; 2 | 3 | /** 4 | * 평형점 구하기 5 | * https://www.oncoder.com/ground/BkIQ8NT0Q 6 | */ 7 | public class Challenge10 { 8 | public static void main(String[] args) { 9 | System.out.println(new Challenge10().solution(-1, 1, 2, 1000)); 10 | } 11 | 12 | public double solution(int x1, int m1, int x2, int m2) { 13 | if (m1 == m2) { 14 | return (double) (x1 + x2) / 2.0; 15 | } 16 | int biggerX = Math.max(x1, x2); 17 | int smallerX = Math.min(x1, x2); 18 | return smallerX + ((m1 - Math.sqrt(m1) * Math.sqrt(m2)) * (biggerX - smallerX)) / (m1 - m2); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/delf/oncoder/Challenge11.java: -------------------------------------------------------------------------------- 1 | package oncoder; 2 | 3 | /** 4 | * 잔액 계산 5 | * https://www.oncoder.com/ground/rJjZchpRX 6 | */ 7 | public class Challenge11 { 8 | public int solution(int balance, String[] transactions) { 9 | for (String tr : transactions) { 10 | String[] args = tr.split(" "); 11 | String cmd = args[0]; 12 | int amount = Integer.parseInt(args[1]); 13 | 14 | if (cmd.equals("C")) { 15 | balance += amount; 16 | continue; 17 | } 18 | 19 | balance -= amount; 20 | } 21 | return balance; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/delf/oncoder/Challenge12.java: -------------------------------------------------------------------------------- 1 | package oncoder; 2 | 3 | /** 4 | * 대선 전략 수립 5 | * https://www.oncoder.com/ground/SkmYcmk1E 6 | */ 7 | public class Challenge12 { 8 | public int solution(String[] stats){ 9 | double minRatio = 1; 10 | int minStat = 0; 11 | for (int i = 0; i < stats.length; i++) { 12 | int one = 0; 13 | int two = 0; 14 | for (char ch : stats[i].toCharArray()) { 15 | if (ch == '1') { 16 | one++; 17 | continue; 18 | } 19 | two++; 20 | } 21 | double ratio = (double) one / (double) (one + two); 22 | if (minRatio > ratio) { 23 | minRatio = ratio; 24 | minStat = i; 25 | } 26 | } 27 | return minStat; 28 | } 29 | 30 | public static void main(String[] args) { 31 | System.out.println(new Challenge12().solution(new String[]{"111", "112", "121", "122", "211", "212", "221", "222"})); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/delf/oncoder/Challenge13.java: -------------------------------------------------------------------------------- 1 | package oncoder; 2 | 3 | import java.util.*; 4 | 5 | /** 6 | * @author delf 7 | */ 8 | public class Challenge13 { 9 | public int[] solution(int[] sequence) { 10 | List list = new ArrayList<>(); 11 | for (int i : sequence) { 12 | if (list.contains(i)) { 13 | list.remove((Integer) i); 14 | } 15 | list.add(i); 16 | } 17 | return list.stream().mapToInt(Integer::intValue).toArray(); 18 | } 19 | 20 | public static void main(String[] args) { 21 | System.out.println(Arrays.toString(new Challenge13().solution(new int[]{1, 2, 5, 4, 4, 4, 2, 3}))); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/delf/oncoder/Challenge14.java: -------------------------------------------------------------------------------- 1 | package oncoder; 2 | 3 | /** 4 | * @author delf 5 | */ 6 | public class Challenge14 { 7 | public static void main(String[] args) { 8 | System.out.println(new Challenge14().solution(7, 3, 1)); 9 | System.out.println(new Challenge14().solution(5, 3, 2)); 10 | System.out.println(new Challenge14().solution(5, 5, 3)); 11 | } 12 | 13 | public String solution(int n1, int n2, int K) { 14 | StringBuilder sb = new StringBuilder(); 15 | for (int i = 0; i < n1; i++) { 16 | sb.append("M"); 17 | } 18 | for (int i = 0; i < n2; i++) { 19 | sb.insert(((K - 1) + (K * i)) % sb.length(), "F"); 20 | } 21 | return sb.toString(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/delf/oncoder/Challenge16.java: -------------------------------------------------------------------------------- 1 | package oncoder; 2 | 3 | import java.util.*; 4 | 5 | 6 | /** 7 | * @author delf 8 | */ 9 | public class Challenge16 { 10 | public static void main(String[] args) { 11 | System.out.println(new Challenge16().solution(new int[]{10, 20, 30, 40, 50})); 12 | System.out.println(new Challenge16().solution(new int[]{123, 123, 456, 456, 456, 123})); 13 | System.out.println(new Challenge16().solution(new int[]{1, 2, 2, 3, 3, 3, 4, 4, 4, 4})); 14 | 15 | 16 | } 17 | 18 | public int solution(int[] events) { 19 | Map solveCounter = new LinkedHashMap<>(); 20 | Map orderCounter = new HashMap<>(); 21 | for (int i = 0; i < events.length; i++) { 22 | solveCounter.merge(events[i], 1, Integer::sum); 23 | orderCounter.putIfAbsent(events[i], i); 24 | } 25 | System.out.println(solveCounter); 26 | System.out.println(orderCounter); 27 | 28 | int maxKey = 0; 29 | int maxValue = 0; 30 | for (int key : solveCounter.keySet()) { 31 | maxValue = Math.max(maxValue, solveCounter.get(key)); 32 | int value = solveCounter.get(key); 33 | if (maxValue > value) { 34 | maxValue = value; 35 | maxKey = key; 36 | } else if (maxValue == value) { 37 | if (orderCounter.get(key) > orderCounter.get(maxKey)) { 38 | maxKey = key; 39 | } 40 | } 41 | } 42 | return maxKey; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/delf/oncoder/Challenge5.java: -------------------------------------------------------------------------------- 1 | package oncoder; 2 | 3 | /** 4 | * 13 구하기 5 | * https://www.oncoder.com/ground/B12g8N6RX 6 | */ 7 | public class Challenge5 { 8 | public int solution(int a) { 9 | int cnt = 0; 10 | for (int i = 1; i <= a; i++) { 11 | int tmp = i; 12 | do { 13 | if ((tmp - 13) % 100 == 0) { 14 | cnt++; 15 | break; 16 | } 17 | } while ((tmp /= 10) > 0); 18 | } 19 | return cnt; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/delf/oncoder/Challenge6.java: -------------------------------------------------------------------------------- 1 | package oncoder; 2 | 3 | /** 4 | * 셀프넘버 판별 5 | * https://www.oncoder.com/ground/B12g8N6RX 6 | */ 7 | public class Challenge6 { 8 | public int solution(int a) { 9 | int cnt = 0; 10 | for (int i = 1; i <= a; i++) { 11 | if (F(i) == a) { 12 | cnt++; 13 | } 14 | } 15 | return cnt; 16 | } 17 | 18 | private static int F(final int n) { 19 | int sum = 0; 20 | int cursor = n; 21 | do { 22 | sum += cursor % 10; 23 | } while ((cursor /= 10) > 0); 24 | return n + sum; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/delf/oncoder/Challenge7.java: -------------------------------------------------------------------------------- 1 | package oncoder; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * 정렬하기 7 | * https://www.oncoder.com/ground/rJwbIEaRm 8 | */ 9 | public class Challenge7 { 10 | public int[] solution(int[] arr) { 11 | return Arrays.stream(arr) 12 | .mapToObj(String::valueOf) 13 | .sorted(String::compareTo) 14 | .mapToInt(Integer::parseInt) 15 | .toArray(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/delf/programmers/Solution12905.java: -------------------------------------------------------------------------------- 1 | package programmers; 2 | 3 | /** 4 | * 가장 큰 정사각형 5 | * https://programmers.co.kr/learn/courses/30/lessons/12905 6 | */ 7 | public class Solution12905 { 8 | public int solution(int[][] board) { 9 | int up, left, upLeft; 10 | for (int i = 1; i < board.length; i++) { 11 | for (int j = 1; j < board[i].length; j++) { 12 | if (board[i][j] == 1) { 13 | up = board[i - 1][j]; 14 | left = board[i][j - 1]; 15 | upLeft = board[i - 1][j - 1]; 16 | 17 | int min = Math.min(Math.min(up, left), upLeft); 18 | board[i][j] = min + 1; 19 | } 20 | } 21 | } 22 | 23 | int ans = 0; 24 | for (int[] n : board) { 25 | for (int j = 0; j < board[0].length; j++) { 26 | if (n[j] > 0) { 27 | ans = Math.max(ans, n[j]); 28 | } 29 | } 30 | } 31 | return ans * ans; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/delf/programmers/Solution12907.java: -------------------------------------------------------------------------------- 1 | package programmers; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * 거스름 7 | * https://programmers.co.kr/learn/courses/30/lessons/12907 8 | */ 9 | public class Solution12907 { 10 | static final int MOD = 1000000007; 11 | 12 | public static void main(String[] args) { 13 | System.out.println(new Solution12907().solution(10, new int[]{2, 3, 5})); 14 | } 15 | 16 | public int solution(int n, int[] money) { 17 | int[][] dp = new int[money.length + 1][n + 1]; 18 | 19 | Arrays.sort(money); 20 | 21 | dp[0][0] = 1; 22 | 23 | for (int r = 1; r < dp.length; ++r) { 24 | for (int c = 0; c < dp[0].length; ++c) { 25 | if (c < money[r - 1]) { 26 | dp[r][c] = dp[r - 1][c] % MOD; 27 | } else if (c == money[r - 1]) { 28 | dp[r][c] = dp[r - 1][c] + 1 % MOD; 29 | } else { 30 | dp[r][c] = dp[r - 1][c] + dp[r][c - money[r - 1]] % MOD; 31 | } 32 | } 33 | } 34 | 35 | return dp[money.length][n]; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/delf/programmers/Solution12913.java: -------------------------------------------------------------------------------- 1 | package programmers; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Solution12913 { 6 | public int solution(int[][] land) { 7 | for (int j = 0; j < land.length; j++) { 8 | for (int i = 0; i < land[j].length; i++) { 9 | if (j == 0) continue; 10 | land[j][i] += getMaxExcept(land[j - 1], i); 11 | } 12 | } 13 | 14 | return Arrays.stream(land[land.length - 1]).max().getAsInt(); 15 | } 16 | 17 | private int getMaxExcept(int[] arr, int index) { 18 | int max = 0; 19 | for (int i = 0; i < arr.length; i++) { 20 | if (i == index) { 21 | continue; 22 | } 23 | max = Math.max(max, arr[i]); 24 | } 25 | return max; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/delf/programmers/Solution12927.java: -------------------------------------------------------------------------------- 1 | package programmers; 2 | 3 | import java.util.stream.IntStream; 4 | 5 | /** 6 | * 야근 지수 7 | * https://programmers.co.kr/learn/courses/30/lessons/12927 8 | */ 9 | public class Solution12927 { 10 | public static long solution(int n, int[] works) { 11 | int[] result = new int[works.length]; 12 | int total = IntStream.of(works).sum() - n; 13 | int time = total; 14 | 15 | if (time <= 0) { 16 | return 0; 17 | } 18 | 19 | for (int i = 0; i < works.length; i++) { 20 | result[i] = Math.min(works[i], total / works.length); 21 | time -= result[i]; 22 | } 23 | 24 | for (int i = 0; i < works.length; i++) { 25 | if (time <= 0) { 26 | break; 27 | } 28 | if (works[i] > result[i]) { 29 | result[i]++; 30 | time--; 31 | } 32 | if (i == works.length - 1) { 33 | i = -1; 34 | } 35 | } 36 | 37 | int sum = 0; 38 | for (int work : result) { 39 | sum += (work * work); 40 | } 41 | return sum; 42 | } 43 | 44 | 45 | public static void main(String[] args) { 46 | System.out.println(solution(4, new int[]{4, 3, 3})); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/delf/programmers/Solution12938.java: -------------------------------------------------------------------------------- 1 | package programmers; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * 최고의 집합 7 | * https://programmers.co.kr/learn/courses/30/lessons/12938 8 | */ 9 | public class Solution12938 { 10 | public int[] solution(int n, int s) { 11 | int div = s / n; 12 | if (div == 0) { 13 | return new int[]{-1}; 14 | } 15 | 16 | int[] answer = new int[n]; 17 | Arrays.fill(answer, div); 18 | 19 | int rest = s % n; 20 | for (int i = 0; i < rest; i++) { 21 | answer[n - 1 - i]++; 22 | } 23 | 24 | return answer; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/delf/programmers/Solution12941.java: -------------------------------------------------------------------------------- 1 | package programmers; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * 최솟값 만들기 7 | * https://programmers.co.kr/learn/courses/30/lessons/12941 8 | */ 9 | public class Solution12941 { 10 | public int solution(int[] A, int[] B) { 11 | int answer = 0; 12 | Arrays.sort(A); 13 | Arrays.sort(B); 14 | for (int i = 0; i < A.length; i++) { 15 | answer += (A[i] * B[B.length - (i + 1)]); 16 | } 17 | return answer; 18 | } 19 | 20 | public static void main(String[] args) { 21 | System.out.println(new Solution12941().solution(new int[]{1, 4, 2}, new int[]{5, 4, 4})); 22 | 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/delf/programmers/Solution12952.java: -------------------------------------------------------------------------------- 1 | package programmers; 2 | 3 | public class Solution12952 { 4 | public static int[] arr; 5 | public static int count = 0; 6 | 7 | public static void main(String[] args) { 8 | System.out.println(new Solution12952().solution(8)); 9 | } 10 | 11 | public int solution(int n) { 12 | return dfs(new int[n * n], n, 0); 13 | } 14 | 15 | public static int dfs(int[] arr, int n, int depth) { 16 | if (depth == n) { 17 | return 1; 18 | } 19 | int cnt = 0; 20 | for (int i = 0; i < n; i++) { 21 | arr[depth] = i; 22 | if (isPlacedQueens(arr, depth)) { 23 | cnt += dfs(arr, n, depth + 1); 24 | } 25 | } 26 | return cnt; 27 | } 28 | 29 | public static boolean isPlacedQueens(int[] arr, int col) { 30 | for (int i = 0; i < col; i++) { 31 | if (arr[col] == arr[i]) { 32 | return false; 33 | } else if (Math.abs(col - i) == Math.abs(arr[col] - arr[i])) { 34 | return false; 35 | } 36 | } 37 | return true; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/delf/programmers/Solution12973.java: -------------------------------------------------------------------------------- 1 | package programmers; 2 | 3 | import java.util.Stack; 4 | 5 | /** 6 | * 짝지어 제거하기 7 | * https://programmers.co.kr/learn/courses/30/lessons/12973 8 | */ 9 | public class Solution12973 { 10 | public static void main(String[] args) { 11 | System.out.println(new Solution12973().solution("cdcd")); 12 | System.out.println(new Solution12973().solution("baabaa")); 13 | } 14 | 15 | public int solution(String s) { 16 | Stack stack = new Stack<>(); 17 | for (char ch : s.toCharArray()) { 18 | if (stack.isEmpty()) { 19 | stack.push(ch); 20 | continue; 21 | } 22 | 23 | if (stack.peek() == ch) { 24 | stack.pop(); 25 | continue; 26 | } 27 | 28 | stack.push(ch); 29 | } 30 | 31 | return stack.isEmpty() ? 1 : 0; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/delf/programmers/Solution12980.java: -------------------------------------------------------------------------------- 1 | package programmers; 2 | 3 | /** 4 | * 점프와 순간 이동 5 | * https://programmers.co.kr/learn/courses/30/lessons/12980 6 | */ 7 | public class Solution12980 { 8 | public static void main(String[] args) { 9 | System.out.println(new Solution12980().solution(5)); 10 | System.out.println(new Solution12980().solution(6)); 11 | System.out.println(new Solution12980().solution(5000)); 12 | } 13 | 14 | public int solution(int n) { 15 | int cnt = 0; 16 | while (n > 0) { 17 | if (n % 2 != 0) { 18 | cnt++; 19 | n--; 20 | } 21 | n /= 2; 22 | } 23 | return cnt; 24 | } 25 | } -------------------------------------------------------------------------------- /src/delf/programmers/Solution12985.java: -------------------------------------------------------------------------------- 1 | package programmers; 2 | 3 | /** 4 | * 예상 대진표 5 | * https://programmers.co.kr/learn/courses/30/lessons/12985 6 | */ 7 | public class Solution12985 { 8 | public static void main(String[] args) { 9 | System.out.println(">>" + new Solution12985().solution(8, 1, 2)); // 1 10 | System.out.println(">>" + new Solution12985().solution(8, 2, 3)); // 2 11 | System.out.println(">>" + new Solution12985().solution(8, 4, 7)); // 3 12 | } 13 | 14 | public int solution(int n, int a, int b) { 15 | int answer = 0; 16 | do { 17 | a = (a + 1) / 2; 18 | b = (b + 1) / 2; 19 | answer++; 20 | } while (Math.abs(a - b) > 0); 21 | return answer; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/delf/programmers/Solution12987.java: -------------------------------------------------------------------------------- 1 | package programmers; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.LinkedList; 6 | import java.util.Queue; 7 | /** 8 | * 숫자 게임 9 | * https://programmers.co.kr/learn/courses/30/lessons/12987 10 | **/ 11 | public class Solution12987 { 12 | public int solution(int[] A, int[] B) { 13 | Arrays.sort(A); 14 | Arrays.sort(B); 15 | Queue qA = new LinkedList<>(new ArrayList<>(Arrays.asList(Arrays.stream(A).boxed().toArray(Integer[]::new)))); 16 | Queue qB = new LinkedList<>(new ArrayList<>(Arrays.asList(Arrays.stream(B).boxed().toArray(Integer[]::new)))); 17 | 18 | int answer = 0; 19 | while (!qA.isEmpty() && !qB.isEmpty()) { 20 | if (qA.peek() < qB.peek()) { 21 | qA.poll(); 22 | answer++; 23 | } 24 | qB.poll(); 25 | } 26 | return answer; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/delf/programmers/Solution1845.java: -------------------------------------------------------------------------------- 1 | package programmers; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | import java.util.stream.Collectors; 6 | import java.util.stream.IntStream; 7 | 8 | /** 9 | * 폰켓몬 10 | * https://programmers.co.kr/learn/courses/30/lessons/1845 11 | */ 12 | public class Solution1845 { 13 | public static void main(String[] args) { 14 | System.out.println(new Solution1845().solution(new int[]{3, 3, 3, 2, 2, 4})); 15 | 16 | } 17 | 18 | public int solution(int[] nums) { 19 | 20 | return Math.min(nums.length / 2, IntStream.of(nums).boxed().collect(Collectors.toCollection(HashSet::new)).size()); 21 | 22 | } 23 | 24 | public int solution2(int[] nums) { 25 | Set set = new HashSet<>(); 26 | 27 | for (int num : nums) { 28 | set.add(num); 29 | } 30 | 31 | return Math.min(nums.length / 2, set.size()); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/delf/programmers/Solution42628.java: -------------------------------------------------------------------------------- 1 | package programmers; 2 | 3 | import java.util.Arrays; 4 | import java.util.Collections; 5 | import java.util.PriorityQueue; 6 | import java.util.StringTokenizer; 7 | 8 | /** 9 | * 이중 우선순위 큐 10 | * https://programmers.co.kr/learn/courses/30/lessons/42628 11 | */ 12 | public class Solution42628 { 13 | public static void main(String[] args) { 14 | System.out.println(Arrays.toString(new Solution42628().solution(new String[]{"I 7", "I 5", "I -5", "D -1"}))); 15 | } 16 | 17 | public int[] solution(String[] operations) { 18 | PriorityQueue pq = new PriorityQueue<>(); 19 | PriorityQueue maxPq = new PriorityQueue<>(Collections.reverseOrder()); 20 | 21 | for (String op : operations) { 22 | StringTokenizer st = new StringTokenizer(op); 23 | String judge = st.nextToken(); 24 | int value = Integer.parseInt(st.nextToken()); 25 | 26 | if (pq.size() < 1 && judge.equals("D")) { 27 | continue; 28 | } 29 | 30 | if (judge.equals("I")) { 31 | pq.offer(value); 32 | maxPq.offer(value); 33 | continue; 34 | } 35 | 36 | if (value < 0) { 37 | maxPq.remove(pq.poll()); 38 | continue; 39 | } 40 | 41 | pq.remove(maxPq.poll()); 42 | } 43 | if (pq.size() > 0) { 44 | return new int[]{maxPq.poll(), pq.poll()}; 45 | } 46 | 47 | return new int[]{0, 0}; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/delf/programmers/Solution42885.java: -------------------------------------------------------------------------------- 1 | package programmers; 2 | 3 | import java.util.Arrays; 4 | import java.util.Collections; 5 | 6 | /** 7 | * 구명보트 8 | * https://programmers.co.kr/learn/courses/30/lessons/42885 9 | */ 10 | public class Solution42885 { 11 | public static void main(String[] args) { 12 | System.out.println(new Solution42885().solution(new int[]{70, 50, 80, 50}, 100)); 13 | } 14 | 15 | public int solution(int[] people, int limit) { 16 | int answer = 0; 17 | 18 | Arrays.sort(people); 19 | System.out.println(Arrays.toString(people)); 20 | int left = 0; 21 | int right = people.length - 1; 22 | 23 | while (left <= right) { 24 | int weight = people[left] + people[right]; 25 | if (weight > limit) { 26 | right--; 27 | } else { 28 | left++; 29 | right--; 30 | } 31 | answer++; 32 | } 33 | return answer; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/delf/programmers/Solution42898.java: -------------------------------------------------------------------------------- 1 | package programmers; 2 | 3 | /** 4 | * 등굣길 5 | * https://programmers.co.kr/learn/courses/30/lessons/42898 6 | **/ 7 | public class Solution42898 { 8 | public static void main(String[] args) { 9 | // System.out.println(new Solution42898().solution(4, 3, new int[][]{{2, 2}})); 10 | System.out.println(new Solution42898().solution(100, 100, new int[][]{{0, 2}, {1, 2}, {1, 3}, {1, 4}})); 11 | } 12 | 13 | public int solution(int m, int n, int[][] puddles) { 14 | int[][] puddleMap = new int[n + 1][m + 1]; 15 | int[][] dp = new int[n + 1][m + 1]; 16 | for (int[] puddle : puddles) { 17 | puddleMap[puddle[1]][puddle[0]] = -1; 18 | } 19 | dp[1][1] = 1; 20 | for (int i = 1; i < n + 1; i++) { 21 | for (int j = 1; j < m + 1; j++) { 22 | if ((i == 1 && j == 1) || puddleMap[i][j] == -1) { 23 | continue; 24 | } 25 | dp[i][j] = (dp[i - 1][j] + dp[i][j - 1]) % 1_000_000_007; 26 | } 27 | } 28 | return dp[n][m]; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/delf/programmers/Solution43162.java: -------------------------------------------------------------------------------- 1 | package programmers; 2 | 3 | /** 4 | * 네트워크 5 | * https://programmers.co.kr/learn/courses/30/lessons/43162 6 | */ 7 | public class Solution43162 { 8 | private final static int CONNECTED = 1; 9 | 10 | public int solution(int n, int[][] computers) { 11 | boolean[] visit = new boolean[n]; 12 | int result = 0; 13 | for (int i = 0; i < n; i++) { 14 | if (!visit[i]) { 15 | dfs(i, visit, computers); 16 | result++; 17 | } 18 | } 19 | return result; 20 | } 21 | 22 | public void dfs(int node, boolean[] visit, int[][] computers) { 23 | if (visit[node]) { 24 | return; 25 | } 26 | 27 | visit[node] = true; 28 | for (int i = 0; i < visit.length; i++) { 29 | if (node != i && isConnected(node, i, computers)) { 30 | dfs(i, visit, computers); 31 | visit[i] = true; 32 | } 33 | } 34 | } 35 | 36 | public static boolean isConnected(int v1, int v2, int[][] computers) { 37 | if (computers[v1][v2] != CONNECTED) { 38 | return false; 39 | } 40 | return computers[v1][v2] == computers[v2][v1]; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/delf/programmers/Solution43163.java: -------------------------------------------------------------------------------- 1 | package programmers; 2 | 3 | /** 4 | * 단어 변환 5 | * https://programmers.co.kr/learn/courses/30/lessons/43163 6 | */ 7 | public class Solution43163 { 8 | private String[] words; 9 | 10 | public int solution(String begin, String target, String[] words) { 11 | this.words = words;//new ArrayList<>(Arrays.asList(words)); 12 | boolean[] isVisit = new boolean[words.length]; 13 | return dfs(begin, target, isVisit, 0); 14 | } 15 | 16 | 17 | private int dfs(String begin, String target, boolean[] isVisit, int depth) { 18 | if (begin.equals(target)) { 19 | return 0; 20 | } 21 | 22 | int min = words.length; 23 | for (int i = 0; i < words.length; i++) { 24 | if(isVisit[i]) { 25 | continue; 26 | } 27 | 28 | if (hasNext(begin, words[i])) { 29 | // System.out.println(begin + " -> " + words[i]); 30 | isVisit[i] = true; 31 | int tmp = dfs(words[i], target, isVisit, depth); 32 | isVisit[i] = false; 33 | min = Math.min(min, tmp + 1); 34 | } 35 | } 36 | 37 | return min; 38 | } 39 | 40 | private boolean hasNext(String begin, String target) { 41 | boolean hasNext = false; 42 | for (int i = 0; i < begin.length(); i++) { 43 | if (begin.charAt(i) != target.charAt(i)) { 44 | if (hasNext) { 45 | return false; 46 | } 47 | hasNext = true; 48 | } 49 | } 50 | return hasNext; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/delf/programmers/Solution43165.java: -------------------------------------------------------------------------------- 1 | package programmers; 2 | 3 | /** 4 | * 타겟 넘버 5 | * https://programmers.co.kr/learn/courses/30/lessons/43165 6 | */ 7 | public class Solution43165 { 8 | public static void main(String[] args) { 9 | System.out.println(new Solution43165().solution(new int[]{1, 1, 1, 1, 1}, 3)); 10 | } 11 | 12 | public int solution(int[] numbers, int target) { 13 | return dfs(numbers, target, 0, 0, 0); 14 | } 15 | 16 | private int dfs(int[] numbers, int target, int depth, int sum, int count) { 17 | if (depth == numbers.length) { 18 | if (sum == target) { 19 | count++; 20 | } 21 | return count; 22 | } 23 | return dfs(numbers, target, depth + 1, sum + numbers[depth], count) 24 | + dfs(numbers, target, depth + 1, sum - numbers[depth], count); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/delf/programmers/Solution43238.java: -------------------------------------------------------------------------------- 1 | package programmers; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * 입국 심사 7 | * https://programmers.co.kr/learn/courses/30/lessons/43238 8 | */ 9 | public class Solution43238 { 10 | public long solution(int n, int[] times) { 11 | Arrays.sort(times); 12 | 13 | long left = 1; 14 | long right = times[times.length - 1] * (long) n; 15 | long answer = right; 16 | 17 | while (left <= right) { 18 | long mid = (left + right) / 2; 19 | long sum = 0; 20 | for (long time : times) { 21 | sum += (mid / time); 22 | } 23 | 24 | if (sum < n) { 25 | left = mid + 1; 26 | } else { 27 | if (mid <= answer) { 28 | answer = mid; 29 | } 30 | right = mid - 1; 31 | } 32 | } 33 | return answer; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/delf/programmers/Solution49189.java: -------------------------------------------------------------------------------- 1 | package programmers; 2 | 3 | import java.util.LinkedList; 4 | import java.util.Queue; 5 | 6 | public class Solution49189 { 7 | public static void main(String[] args) { 8 | System.out.println(new Solution49189().solution(6, new int[][]{{3, 6}, {4, 3}, {3, 2}, {1, 3}, {1, 2}, {2, 4}, {5, 2}})); 9 | } 10 | 11 | public int solution(int n, int[][] edge) { 12 | int[] dist = new int[n + 1]; 13 | boolean[][] map = new boolean[n + 1][n + 1]; 14 | for (int[] i : edge) { 15 | map[i[0]][i[1]] = true; 16 | map[i[1]][i[0]] = true; 17 | } 18 | 19 | Queue nodes = new LinkedList<>(); 20 | nodes.add(1); 21 | 22 | int max = 0; 23 | while (!nodes.isEmpty()) { 24 | int i = nodes.poll(); 25 | 26 | for (int j = 2; j <= n; j++) { 27 | if (dist[j] == 0 && map[i][j]) { 28 | dist[j] = dist[i] + 1; 29 | nodes.add(j); 30 | max = Math.max(max, dist[j]); 31 | } 32 | } 33 | } 34 | 35 | int count = 0; 36 | for (int d : dist) { 37 | if (max == d) 38 | count++; 39 | } 40 | 41 | return count; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/delf/programmers/Solution62048.java: -------------------------------------------------------------------------------- 1 | package programmers; 2 | 3 | public class Solution62048 { 4 | public static void main(String[] args) { 5 | System.out.println(new Solution62048().solution(8, 12)); 6 | } 7 | 8 | public long solution(int w, int h) { 9 | return ((long) w * (long) h) - ((long) w + (long) h - getGcd(w, h)); 10 | } 11 | 12 | private long getGcd(long a, long b) { 13 | while (b != 0) { 14 | long temp = a % b; 15 | a = b; 16 | b = temp; 17 | } 18 | return Math.abs(a); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/delf/programmers/Solution64064.java: -------------------------------------------------------------------------------- 1 | package programmers; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | /** 7 | * 불량 사용자 8 | * https://programmers.co.kr/learn/courses/30/lessons/64064 9 | */ 10 | public class Solution64064 { 11 | private Set set = new HashSet<>(); 12 | 13 | public int solution(String[] userId, String[] bannedId) { 14 | dfs(0, "", userId, bannedId, new boolean[userId.length]); 15 | return set.size(); 16 | } 17 | 18 | public void dfs(int idx, String s, String[] userId, String[] bannedId, boolean[] visited) { 19 | if (idx == bannedId.length) { 20 | StringBuilder sb = new StringBuilder(); 21 | for (int i = 0; i < userId.length; i++) { 22 | if (s.contains(String.valueOf(i))) sb.append(i); 23 | } 24 | set.add(sb.toString()); 25 | return; 26 | } 27 | 28 | for (int i = 0; i < userId.length; i++) { 29 | if (visited[i]) { 30 | continue; 31 | } 32 | 33 | String regex = bannedId[idx].replace("*", "."); 34 | if (userId[i].matches(regex)) { 35 | visited[i] = true; 36 | dfs(idx + 1, s + i, userId, bannedId, visited); 37 | visited[i] = false; 38 | } 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/delf/programmers/Solution64065.java: -------------------------------------------------------------------------------- 1 | package programmers; 2 | 3 | import java.util.*; 4 | 5 | import static java.util.stream.Collectors.toList; 6 | 7 | /** 8 | * 튜플 9 | * https://programmers.co.kr/learn/courses/30/lessons/64065 10 | */ 11 | public class Solution64065 { 12 | public int[] solution(String input) { 13 | List> list = new ArrayList<>(); 14 | 15 | input = input.substring(2, input.length() - 2); 16 | String[] arr = input.split("},\\{"); 17 | for (String s : arr) { 18 | list.add(Arrays.stream(s.split(",")).map(Integer::parseInt).collect(toList())); 19 | } 20 | 21 | list.sort(Comparator.comparingInt(List::size)); 22 | 23 | int[] result = new int[list.size()]; 24 | Set set = new HashSet<>(); 25 | int cursor = 0; 26 | for (List l : list) { 27 | for (int n : l) { 28 | if (set.contains(n)) { 29 | continue; 30 | } 31 | set.add(n); 32 | result[cursor++] = n; 33 | break; 34 | } 35 | } 36 | 37 | return result; 38 | } 39 | } -------------------------------------------------------------------------------- /src/delf/programmers/Solution67258.java: -------------------------------------------------------------------------------- 1 | package programmers; 2 | 3 | import java.util.*; 4 | 5 | public class Solution67258 { 6 | public int[] solution(String[] gems) { 7 | int[] answer = {1, gems.length}; 8 | Set jewels = new HashSet<>(); 9 | Map counter = new HashMap<>(); 10 | 11 | Collections.addAll(jewels, gems); 12 | 13 | int start = 0, end = 0; 14 | int diff = gems.length; 15 | 16 | while (true) { 17 | if (counter.size() == jewels.size()) { 18 | if (diff > end - start) { 19 | diff = end - start; 20 | answer[0] = start + 1; 21 | answer[1] = end; 22 | } 23 | if (counter.get(gems[start]) == 1) { 24 | counter.remove(gems[start]); 25 | start++; 26 | } else if (counter.get(gems[start]) > 1) { 27 | counter.merge(gems[start], -1, Integer::sum); 28 | start++; 29 | } 30 | } else if (end == gems.length) { 31 | break; 32 | } else { 33 | counter.merge(gems[end], 1, Integer::sum); 34 | end++; 35 | } 36 | } 37 | return answer; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/delf/programmers/Solution68645.java: -------------------------------------------------------------------------------- 1 | package programmers; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * 삼각 달팽이 7 | * https://programmers.co.kr/learn/courses/30/lessons/68645 8 | */ 9 | public class Solution68645 { 10 | private final static int[][] incremental = {{1, 0}, {0, 1}, {-1, -1}}; // 아래, 오른쪽, 대각선(좌상) 11 | 12 | public int[] solution(int num) { 13 | final int endNumber = num * (1 + num) / 2; 14 | int[][] array = new int[num][num]; 15 | 16 | int n = -1, m = 0; 17 | int direction = 0; 18 | 19 | for (int now = 1; now <= endNumber; ) { // 1 ~ endNumber 20 | for (int i = 0; i < num; i++) { // n ~ 1 21 | n += incremental[direction][0]; 22 | m += incremental[direction][1]; 23 | array[n][m] = now++; // 방향 번갈아가면서 대입 24 | } 25 | direction = (direction + 1) % incremental.length; 26 | num--; 27 | } 28 | return convert(array, endNumber); 29 | } 30 | 31 | private int[] convert(int[][] array, int size) { 32 | int[] result = new int[size]; 33 | int cursor = 0; 34 | for (int[] m : array) { 35 | for (int j = 0; j < array[0].length; j++) { 36 | if (m[j] == 0) { 37 | break; 38 | } 39 | result[cursor++] = m[j]; 40 | } 41 | } 42 | return result; 43 | } 44 | 45 | public static void main(String[] args) { 46 | System.out.println(Arrays.toString(new Solution68645().solution(6))); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/delf/programmers/Solution68646.java: -------------------------------------------------------------------------------- 1 | package programmers; 2 | 3 | /** 4 | * 야근 지수 5 | * https://programmers.co.kr/learn/courses/30/lessons/68646 6 | */ 7 | public class Solution68646 { 8 | public int solution(int[] a) { 9 | if (a.length == 1) { 10 | return 1; 11 | } 12 | 13 | int[] leftMin = new int[a.length]; 14 | int[] rightMin = new int[a.length]; 15 | 16 | int tmpMin = Integer.MAX_VALUE; 17 | for (int i = 0; i < a.length; i++) { 18 | tmpMin = Math.min(a[i], tmpMin); 19 | leftMin[i] = tmpMin; 20 | } 21 | 22 | tmpMin = Integer.MAX_VALUE; 23 | for (int i = a.length - 1; i >= 0; i--) { 24 | tmpMin = Math.min(a[i], tmpMin); 25 | rightMin[i] = tmpMin; 26 | } 27 | 28 | int answer = 0; 29 | for (int i = 0; i < a.length; i++) { 30 | if (leftMin[i] < a[i] && rightMin[i] < a[i]) { 31 | continue; 32 | } 33 | answer++; 34 | } 35 | return answer; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/delf/programmers/skillcheck/SkillCheck0101.java: -------------------------------------------------------------------------------- 1 | package programmers.skillcheck; 2 | 3 | import java.util.*; 4 | 5 | public class SkillCheck0101 { 6 | public static void main(String[] args) { 7 | System.out.println(new SkillCheck0101().solution(new String[]{"marina", "josipa", "nikola", "vinko", "filipa"}, new String[]{"josipa", "filipa", "marina", "nikola"})); 8 | } 9 | 10 | 11 | public String solution(String[] participant, String[] completion) { 12 | Map map = new HashMap<>(); 13 | for (String name : completion) { 14 | map.merge(name, 1, Integer::sum); 15 | } 16 | for (String name : participant) { 17 | Integer n = map.get(name); 18 | if (Objects.isNull(n) || n == 0) { 19 | return name; 20 | } 21 | map.put(name, n - 1); 22 | } 23 | throw new IllegalArgumentException(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/delf/programmers/skillcheck/SkillCheck0102.java: -------------------------------------------------------------------------------- 1 | package programmers.skillcheck; 2 | 3 | public class SkillCheck0102 { 4 | public boolean solution(String s) { 5 | int p = 0; 6 | int y = 0; 7 | for (char ch : s.toCharArray()) { 8 | if (Character.isUpperCase(ch)) { 9 | ch = Character.toLowerCase(ch); 10 | } 11 | if (ch == 'p') { 12 | p++; 13 | } else if (ch == 'y') { 14 | y++; 15 | } 16 | } 17 | return p == y; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/delf/programmers/skillcheck/SkillCheck0201.java: -------------------------------------------------------------------------------- 1 | package programmers.skillcheck; 2 | 3 | import java.util.Arrays; 4 | 5 | public class SkillCheck0201 { 6 | public static void main(String[] args) { 7 | 8 | } 9 | 10 | public int solution(int[] people, int limit) { 11 | int answer = 0; 12 | 13 | Arrays.sort(people); 14 | int left = 0; 15 | int right = people.length - 1; 16 | 17 | while (left <= right) { 18 | int weight = people[left] + people[right]; 19 | if (weight <= limit) { 20 | left++; 21 | } 22 | right--; 23 | answer++; 24 | } 25 | return answer; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/delf/programmers/skillcheck/SkillCheck0202.java: -------------------------------------------------------------------------------- 1 | package programmers.skillcheck; 2 | 3 | public class SkillCheck0202 { 4 | public static void main(String[] args) { 5 | System.out.println(new SkillCheck0202().solution(new int[]{1, 2, 3, 4})); 6 | System.out.println(new SkillCheck0202().solution(new int[]{1, 2, 7, 6, 4})); 7 | } 8 | 9 | public int solution(int[] nums) { 10 | int cnt = 0; 11 | for (int i = 0; i < nums.length - 2; i++) { 12 | for (int j = i + 1; j < nums.length - 1; j++) { 13 | for (int k = j + 1; k < nums.length; k++) { 14 | cnt += isPrime(nums[i] + nums[j] + nums[k]) ? 1 : 0; 15 | } 16 | } 17 | } 18 | return cnt; 19 | } 20 | 21 | private boolean isPrime(int n) { 22 | for (int i = 2; i * i <= n; i++) { 23 | if (n % i == 0) { 24 | return false; 25 | } 26 | } 27 | return true; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/delf/programmers/skillcheck/SkillCheck0302.java: -------------------------------------------------------------------------------- 1 | package programmers.skillcheck; 2 | 3 | import java.util.Arrays; 4 | 5 | public class SkillCheck0302 { 6 | public int[] solution(int n, int s) { 7 | int div = s / n; 8 | int rest = s % n; 9 | 10 | if (div == 0) { 11 | return new int[]{-1}; 12 | } 13 | 14 | int[] answer = new int[n]; 15 | Arrays.fill(answer, div); 16 | 17 | if (rest == 0) { 18 | return answer; 19 | } 20 | 21 | int cursor = 0; 22 | for (int i = 0; i < rest; i++, cursor %= n) { 23 | answer[cursor++]++; 24 | } 25 | Arrays.sort(answer); 26 | return answer; 27 | } 28 | 29 | public static void main(String[] args) { 30 | System.out.println(Arrays.toString(new SkillCheck0302().solution(2, 9))); 31 | 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/doy/Solution.java: -------------------------------------------------------------------------------- 1 | package doy; 2 | 3 | import doy.etc.Doy_Q1000; 4 | 5 | public class Solution { 6 | 7 | public static void main(String[] args) throws Exception { 8 | solution(); 9 | } 10 | 11 | public static void solution() { 12 | Doy_Q1000.run(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/doy/ds1/Doy_Q10799.java: -------------------------------------------------------------------------------- 1 | package doy.ds1; 2 | 3 | import java.util.Scanner; 4 | import java.util.Stack; 5 | 6 | public class Doy_Q10799 { 7 | static Scanner s = new Scanner(System.in); 8 | 9 | public static void run() { 10 | Stack stack = new Stack<>(); 11 | 12 | String input = s.nextLine(); 13 | int res = 0; 14 | 15 | for (int i = 0; i < input.length(); i++) { 16 | char c = input.charAt(i); 17 | switch (c) { 18 | case '(': 19 | stack.push(i); // index를 넣음 20 | break; 21 | 22 | case ')': 23 | if (i - stack.peek() == 1) { // index가 1 차이나면 24 | // 레이저 25 | stack.pop(); 26 | res += stack.size(); 27 | } else { 28 | stack.pop(); 29 | res += 1; 30 | } 31 | break; 32 | } 33 | } 34 | System.out.println(res); 35 | } 36 | } 37 | 38 | /* 39 | 풀이가 동일하네요. 40 | Stack으로 풀 수 있는 문제 중 단순한 count로도 풀리는 경우도 많은 거 같아요. 41 | push()의 경우 count++, pop()의 경우 count--;를 이용하면 간단히 풀 수 있어요!! 42 | 참고) 스택(Stack)의 사용 예제 43 | * 웹 브라우저 방문기록 (뒤로가기) 44 | * 실행취소 (undo) 45 | * 역순 문자열 만들기 46 | * 수식의 괄호 검사 (연산자 우선순위 표현을 위한 괄호 검사) 47 | * 후위표기법 계산 48 | */ 49 | -------------------------------------------------------------------------------- /src/doy/ds1/Doy_Q9012.java: -------------------------------------------------------------------------------- 1 | package doy.ds1; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Doy_Q9012 { 6 | static Scanner s = new Scanner(System.in); 7 | 8 | private static boolean isValidParenthesisString(String input) { 9 | int cnt = 0; 10 | for (int i = 0; i < input.length(); i++) { 11 | if (input.charAt(i) == '(') 12 | cnt++; 13 | else { 14 | cnt--; 15 | if (cnt < 0) { 16 | return false; 17 | } 18 | } 19 | } 20 | 21 | if (cnt == 0) { 22 | return true; 23 | } 24 | return false; 25 | } 26 | 27 | private static String getOutput(boolean result) { 28 | if (result) 29 | return "YES"; 30 | return "NO"; 31 | } 32 | 33 | public static void run() { 34 | int n = s.nextInt(); 35 | while(n > 0) { 36 | String input = s.next(); 37 | System.out.println(getOutput(isValidParenthesisString(input))); 38 | n--; 39 | } 40 | } 41 | } 42 | 43 | /* 44 | 풀이는 동일하네요. 45 | 함수를 여러개로 나누어 조금 더 구조화한 게 보이네요^^ 46 | 이런 습관을 자연스럽게 만들면 좋을 거 같아요. 47 | */ 48 | -------------------------------------------------------------------------------- /src/doy/etc/Doy_Q1000.java: -------------------------------------------------------------------------------- 1 | package doy.etc; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Doy_Q1000 { 6 | static Scanner s = new Scanner(System.in); 7 | public static void run() { 8 | int a = s.nextInt(); 9 | int b = s.nextInt(); 10 | System.out.println(a+b); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/doy/leetcode/Two_Sum_1.java: -------------------------------------------------------------------------------- 1 | package doy.leetcode; 2 | 3 | public class Two_Sum_1 { 4 | public int[] twoSum(int[] nums, int target) { 5 | for (int i = 0; i < nums.length; i++) { 6 | for (int j = i + 1; j < nums.length; j++) { 7 | if (nums[i] + nums[j] == target) { 8 | return new int[]{i, j}; 9 | } 10 | } 11 | } 12 | return null; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/doy/math1_1/Doy_Q10430.java: -------------------------------------------------------------------------------- 1 | package doy.math1_1; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Doy_Q10430 { 6 | // (A+B)%C는 (A%C + B%C)%C 와 같을까? 7 | // (A×B)%C는 (A%C × B%C)%C 와 같을까? 8 | // 세 수 A, B, C가 주어졌을 때, 위의 네가지 값을 구하는 프로그램을 작성하시오. 9 | static Scanner s = new Scanner(System.in); 10 | public static void run() { 11 | int A = s.nextInt(); 12 | int B = s.nextInt(); 13 | int C = s.nextInt(); 14 | 15 | System.out.println((A+B) % C); 16 | System.out.println((A%C + B%C) % C); 17 | System.out.println((A*B) % C); 18 | System.out.println((A%C * B%C) % C); 19 | } 20 | } 21 | 22 | /* 23 | 너무 쉬운 문제라... 피드백할 게 없네요..ㅠㅠ 24 | 다만 변수명이 대문자인 것은 좋지 않아요~ㅎㅎㅎ 25 | 다음에 클린코드를 위한 변수명은 어떻게 설정하면 좋을지 같이 얘기해보는것도 좋을 거 같아요 :) 26 | */ 27 | -------------------------------------------------------------------------------- /src/doy/math1_1/Doy_Q11005.java: -------------------------------------------------------------------------------- 1 | package doy.math1_1; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Doy_Q11005 { 6 | // 10진법 수 N이 주어진다. 이 수를 B진법으로 바꿔 출력하는 프로그램을 작성하시오. 7 | static Scanner s = new Scanner(System.in); 8 | 9 | public static void run() { 10 | int n = s.nextInt(); 11 | int b = s.nextInt(); 12 | StringBuilder sb = new StringBuilder(); 13 | 14 | while (n > 0) { 15 | int r = n % b; 16 | if (r < 10) { 17 | sb.append(r); 18 | } else { 19 | sb.append((char) (r + 'A' - 10)); 20 | } 21 | n /= b; 22 | } 23 | 24 | System.out.println(sb.reverse()); 25 | } 26 | } 27 | 28 | /* 29 | StringBuilder 함수에 reverse()라는 좋은 것이 있었네요^^ 30 | 풀이가 훨씬 간단해지는거 같아요ㅎㅎ 31 | 'A'의 아스키코드 = 65는 기본적으로 알아두는 것이 좋을 거 같아요. 32 | */ 33 | -------------------------------------------------------------------------------- /src/doy/math1_1/Doy_Q1934.java: -------------------------------------------------------------------------------- 1 | package doy.math1_1; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Doy_Q1934 { 6 | // 두 자연수 A와 B가 주어졌을 때, A와 B의 최소공배수를 구하는 프로그램을 작성하시오. 7 | static Scanner s = new Scanner(System.in); 8 | public static void run() { 9 | int count = s.nextInt(); 10 | for(int i=0; i num2 ? num1 : num2; 14 | 15 | for (int j = max; j > 0; j--) { 16 | if (num1 % j == 0 && num2 % j == 0) { 17 | int gcd = j; 18 | int lcm = num1 * num2 / gcd; 19 | 20 | System.out.println(lcm); 21 | break; 22 | } 23 | } 24 | } 25 | } 26 | } 27 | 28 | /* 29 | test case에 대해서 전체를 한 번에 입력받지 않아도 되는 것을 알았어요. 30 | 1) 전체 입력이 큰 경우에는 그만큼 큰 배열이 필요하기 때문에 31 | 2) 받는 입력의 개수를 모를 때 배열의 크기를 정하기 어렵기 때문에 32 | 33 | 한 번 더 정리하고 갑니다 :) 34 | 35 | 일일이 비교해서 gcd를 찾는 방법보다 속도가 빠른 유클리드 호제법에 관한 내용을 같이 정리해보면 좋을 거 같아요~ 36 | */ 37 | -------------------------------------------------------------------------------- /src/doy/math1_1/Doy_Q2609.java: -------------------------------------------------------------------------------- 1 | package doy.math1_1; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Doy_Q2609 { 6 | // 두 개의 자연수를 입력받아 최대 공약수와 최소 공배수를 출력하는 프로그램을 작성하시오. 7 | static Scanner s = new Scanner(System.in); 8 | public static void run() { 9 | int num1 = s.nextInt(); 10 | int num2 = s.nextInt(); 11 | int max = num1 > num2 ? num1 : num2; 12 | 13 | for (int i=max; i>0; i--) { 14 | if(num1%i == 0 && num2%i == 0){ 15 | int gcd = i; 16 | int lcm = num1*num2 / gcd; 17 | 18 | System.out.println(gcd); 19 | System.out.println(lcm); 20 | 21 | break; 22 | } 23 | } 24 | } 25 | 26 | /* 27 | public static int getGCD(int num1, int num2) { 28 | if(num2 == 0) 29 | return num1; 30 | else 31 | return getGCD(num2, num1 % num2); 32 | } 33 | 34 | public static int getLCM(int num1, int num2, int gcd) { 35 | return gcd * (num1/gcd) * (num2/gcd); 36 | } 37 | */ 38 | } 39 | 40 | /* 41 | gcd와 lcm을 간단하게 풀기 좋은 방법 같아요. 따로 함수로 빼지 않고 짧은 시간내에 작성할 수 있는 방법이라고 생각합니다. 42 | 그리고 max에서 하나씩 줄이면서 최대공약수를 바로 찾는 방법이 있다는 것을 알았네요 :) 43 | 저는 2부터 min까지 전부 조사해서 최대공약수를 찾는 방법을 사용했습니다. 시간복잡도는 크게 차이가 나진 않을 거 같아요. 44 | int lcm = num1 * num2 / gcd; 를 생각하고 있다면 gcd만 빠르게 구현한 후에 쉽게 lcm도 구할 수 있을 거 같아요. 45 | */ 46 | -------------------------------------------------------------------------------- /src/doy/math1_1/Doy_Q2745.java: -------------------------------------------------------------------------------- 1 | package doy.math1_1; 2 | 3 | import java.util.Scanner; 4 | 5 | // B진법 수 N이 주어진다. 이 수를 10진법으로 바꿔 출력하는 프로그램을 작성하시오. 6 | public class Doy_Q2745 { 7 | static Scanner s = new Scanner(System.in); 8 | 9 | public static void run() { 10 | // [방법 1] Java API 이용 11 | String num = s.next(); 12 | int b = s.nextInt(); 13 | int res = Integer.parseInt(num, b); 14 | 15 | System.out.println(res); 16 | 17 | // [방법 2] 자리수에 맞게 곱하고 더하기 18 | // StringBuilder n = new StringBuilder(s.next()); 19 | // int b = s.nextInt(); 20 | // int res = 0; 21 | // 22 | // n.reverse(); 23 | // 24 | // for (int i = 0; i < n.length(); i++) { 25 | // int c = n.charAt(i); 26 | // 27 | // if ('0' <= c && c <= '9') { 28 | // c = c - '0'; 29 | // res += c * Math.pow(b, i); 30 | // } else { 31 | // c = c - 'A' + 10; 32 | // res += c * Math.pow(b, i); 33 | // } 34 | // } 35 | // 36 | // System.out.println(res); 37 | } 38 | } 39 | 40 | /* 41 | Integer API를 이용하면 진법 변환을 훨씬 더 간단하게 할 수 있다는 것을 알았습니다 :) 42 | 또한 int c = n.charAt(i); 다음과 같이 char형을 바로 int로 바꿀 때, 43 | 해당 char가 숫자라면 c = c - '0'; 다음과 같이 계산해야 올바른 int가 나오는 것도 알게 되었네요^^ 44 | 45 | 앞으로 (양수의) 진수로 변환하는 문제를 풀 때는 parseInt/toString을 이용하면 좋을 거 같아요! 46 | */ 47 | -------------------------------------------------------------------------------- /src/doy/math1_1/Doy_Q9613.java: -------------------------------------------------------------------------------- 1 | package doy.math1_1; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Doy_Q9613 { 6 | // 양의 정수 n개가 주어졌을 때, 가능한 모든 쌍의 GCD의 합을 구하는 프로그램을 작성하시오. 7 | // 첫째 줄에 테스트 케이스의 개수 t (1 < t < 100)이 주어진다. 각 테스트 케이스는 한 줄로 이루어져 있고, 첫째 줄에는 수의 개수 n (1 < n < 100)가 주어지고, 다음에는 n개의 수가 주어진다. 입력으로 주어지는 수는 1000000을 넘지 않는다. 8 | static Scanner s = new Scanner(System.in); 9 | public static void run() { 10 | int t = s.nextInt(); 11 | while (t > 0) { 12 | int n = s.nextInt(); 13 | int[] numArray = new int[n]; 14 | for (int i=0; i 1) { 18 | System.out.println(n); 19 | } 20 | } 21 | } 22 | 23 | /* 24 | 풀이가 동일하네요^^ 25 | 소인수분해를 구하는 간단한 방법을 알게되었습니다. 26 | 소수와 소인수분해에서는 2~루트N을 잘 기억해두면 좋을 거 같네요 :) 27 | */ 28 | -------------------------------------------------------------------------------- /src/doy/math1_2/Doy_Q1676.java: -------------------------------------------------------------------------------- 1 | package doy.math1_2; 2 | 3 | import java.util.Scanner; 4 | 5 | // N!에서 뒤에서부터 처음 0이 아닌 숫자가 나올 때까지 0의 개수를 구하는 프로그램을 작성하시오. 6 | public class Doy_Q1676 { 7 | static Scanner s = new Scanner(System.in); 8 | 9 | public static void run() { 10 | int n = s.nextInt(); 11 | int count = 0; 12 | int divideNum = 5; 13 | 14 | // n을 소인수분해 했을 때 2*5의 개수가 0의 개수 15 | // 팩토리얼에서는 5의 개수가 항상 2의 개수보다 적기 때문에 5의 개수로만 판단 16 | while (n >= divideNum) { 17 | count += n / divideNum; 18 | divideNum *= 5; 19 | } 20 | 21 | System.out.println(count); 22 | } 23 | } 24 | 25 | /* 26 | 풀이는 동일하네요^^ 27 | 저는 두 개의 반복문을 사용했는데 하나의 반복문을 통해서 푸는 것이 훨씬 간단하고 이해하기 쉬운거 같아요. 28 | 간단한 방법을 배워갑니다 :) 29 | */ 30 | -------------------------------------------------------------------------------- /src/doy/math1_2/Doy_Q1929.java: -------------------------------------------------------------------------------- 1 | package doy.math1_2; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Scanner; 5 | 6 | // M이상 N이하의 소수를 모두 출력하는 프로그램을 작성하시오. 7 | public class Doy_Q1929 { 8 | static Scanner s = new Scanner(System.in); 9 | public static void run() { 10 | int m = s.nextInt(); 11 | int n = s.nextInt(); 12 | 13 | ArrayList primeList = new ArrayList<>(); // 2~n 까지의 소수 저장 14 | boolean[] checkPrime = new boolean[n + 1]; // 에라토스테네스의 체 true : 지워짐, false : 지워지지 않음 15 | 16 | for (int i = 2; i <= n; i++) { 17 | if (!checkPrime[i]) { 18 | primeList.add(i); 19 | for (int j = i * 2; j <= n; j += i) { 20 | checkPrime[j] = true; 21 | } 22 | } 23 | } 24 | 25 | for (int prime : primeList) { 26 | if (m <= prime) { 27 | System.out.println(prime); 28 | } 29 | } 30 | } 31 | } 32 | 33 | /* 34 | 둘 다 '에라토스테네스의 체'를 이용한 풀이로, 동일하네요. 35 | 그리고 Boolean(Object)의 기본값은 null이고 boolean(primitive)의 기본값은 false라는 것도 새로 알게 되었어요.^^ 36 | issue1) i가 1,000,000 인 경우 i*i는 범위를 넘어가므로 j=i*i -> j=i*2 37 | issue2) 주어진 조건의 범위를 제대로 확인하고 값은 초기화해야 된다는 것도 알았어요. 38 | */ 39 | -------------------------------------------------------------------------------- /src/doy/math1_2/Doy_Q2089.java: -------------------------------------------------------------------------------- 1 | package doy.math1_2; 2 | 3 | import java.util.Scanner; 4 | 5 | // 10진법의 수를 입력 받아서 -2진수를 출력하는 프로그램을 작성하시오. 6 | public class Doy_Q2089 { 7 | static Scanner s = new Scanner(System.in); 8 | 9 | public static void run() { 10 | int num = s.nextInt(); 11 | StringBuilder sb = new StringBuilder(); 12 | 13 | if (num == 0) { 14 | System.out.println("0"); 15 | return; 16 | } 17 | 18 | while (num != 0) { 19 | if (num < 0 && num % 2 != 0) { // 음의 홀수일 때 20 | sb.append(1); 21 | num = (num / -2) + 1; 22 | continue; 23 | } 24 | 25 | sb.append(num % -2); 26 | num /= -2; 27 | } 28 | 29 | sb.reverse(); 30 | System.out.println(sb); 31 | } 32 | } 33 | 34 | /* 35 | num = (num / -2) + 1; 를 저는 num = (num - 1) / -2; 로 처리한 것을 제외하면 풀이가 동일하네요. 36 | 음의 진수로 변환할 때는 해당 진수로 나누어 지지 않는 음수의 값들을 주의깊게 고려해야 될 것 같아요 :) 37 | ex) -3의 진수로 변환하는 경우, -1 -2 -4 -5 -7 -8 ... 등을 주의깊게 고려! 38 | ex) -4의 진수로 변환하는 경우, -1 -2 -3 -5 -6 -7 ... 등을 주의깊게 고려! 39 | */ 40 | -------------------------------------------------------------------------------- /src/doy/math2_1/Doy_Q1629.java: -------------------------------------------------------------------------------- 1 | package doy.math2_1; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Doy_Q1629 { 6 | static Scanner s = new Scanner(System.in); 7 | 8 | public static void run() { 9 | long a = s.nextLong(); 10 | long b = s.nextLong(); 11 | long c = s.nextLong(); 12 | 13 | long res = calc(a, b, c); 14 | System.out.println(res); 15 | } 16 | 17 | // 분할정복 18 | private static long calc(long a, long b, long c) { 19 | if (b == 0) { 20 | return 1; 21 | } else if (b == 1) { 22 | return a % c; 23 | } else if (b % 2 == 0) { 24 | long temp = calc(a, b / 2, c); 25 | return (temp * temp) % c; 26 | } else { // b % 2 == 1 27 | return (a * calc(a, b - 1, c)) % c; 28 | } 29 | } 30 | } 31 | 32 | /* 33 | 풀이는 동일하네요. 34 | 로직 안에서 c로 나눠는 것에 대한 처리에 주의해야 될 것 같아요! :) 35 | */ 36 | -------------------------------------------------------------------------------- /src/doy/math2_1/Doy_Q2749.java: -------------------------------------------------------------------------------- 1 | package doy.math2_1; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Doy_Q2749 { 6 | static Scanner s = new Scanner(System.in); 7 | static final int MOD = (int) Math.pow(10, 6); // 나눌 수 : 10^6 8 | static final int PERIOD = 15 * (MOD / 10); // 나눌 수가 10^k 일 때, K>2 라면, 주기는 항상 15 * 10^(k-1) 9 | 10 | public static void run() { 11 | long n = s.nextLong(); 12 | int index = (int) (n % PERIOD); 13 | int fibo[] = new int[index + 1]; 14 | 15 | fibo[0] = 0; 16 | fibo[1] = 1; 17 | for (int i = 2; i <= index; i++) { 18 | fibo[i] = fibo[i - 1] + fibo[i - 2]; 19 | fibo[i] %= MOD; 20 | } 21 | 22 | System.out.println(fibo[index]); 23 | } 24 | } 25 | 26 | /* 27 | 풀이가 동일하네요. 28 | 다음 스터디에서는 피사노의 주기를 구하는 방법에 대해서 함께 얘기해보면 좋을 거 같아요^^ 29 | */ 30 | -------------------------------------------------------------------------------- /src/doy/math2_1/Doy_Q9471.java: -------------------------------------------------------------------------------- 1 | package doy.math2_1; 2 | 3 | import java.util.Scanner; 4 | 5 | // 피보나치 수열을 m으로 나눈 나머지가 주기를 이룬다는 것을 증명했다. 6 | // k(m)을 반복하는 부분 수열의 길이라고 했을 때, m이 주어졌을 때, k(m)을 구하는 프로그램을 작성하시오. 7 | public class Doy_Q9471 { 8 | static Scanner s = new Scanner(System.in); 9 | 10 | public static void run() { 11 | int count = s.nextInt(); 12 | 13 | for (int i = 0; i < count; i++) { 14 | int testNum = s.nextInt(); 15 | int mod = s.nextInt(); 16 | 17 | int f1 = 1, f2 = 1, f3; 18 | int period = 0; 19 | 20 | while (true) { 21 | f3 = (f1 + f2) % mod; 22 | 23 | f1 = f2; 24 | f2 = f3; 25 | period++; 26 | 27 | if (f1 == 1 && f2 == 1) { 28 | break; 29 | } 30 | } 31 | 32 | System.out.println(testNum + " " + period); 33 | } 34 | } 35 | } 36 | 37 | /* 38 | 피사노 주기는 항상 15 * 10^(k-1)인 것도 기억하고 있으면 좋을 거 같아요^^ 39 | 피보나치 수열은 "1, 1, "로 시작하므로 피보나치 수열을 M으로 나눈 나머지가 또 다시 1이 연속("1, 1, ")으로 나올 때, 또 다른 주기가 시작된다는 것을 알았습니다. 40 | */ 41 | -------------------------------------------------------------------------------- /src/doy/math2_2/Doy_Q11689.java: -------------------------------------------------------------------------------- 1 | package doy.math2_2; 2 | 3 | import java.util.Scanner; 4 | 5 | // 자연수 n이 주어졌을 때, GCD(n, k) = 1을 만족하는 자연수 1 ≤ k ≤ n 의 개수를 구하는 프로그램을 작성하시오. 6 | public class Doy_Q11689 { 7 | static Scanner s = new Scanner(System.in); 8 | 9 | public static void run() { 10 | long n = s.nextLong(); 11 | 12 | System.out.println(phi(n)); 13 | } 14 | 15 | // 오일러 피 함수, GCD(n, k) = 1 16 | private static long phi(long n) { 17 | long ans = n; 18 | for (long i = 2; i * i <= n; i++) { 19 | if (n % i == 0) { 20 | while (n % i == 0) { 21 | n /= i; 22 | } 23 | ans -= ans / i; 24 | } 25 | } 26 | 27 | if (n > 1) { 28 | ans -= ans / n; 29 | } 30 | 31 | return ans; 32 | } 33 | } 34 | 35 | /* 36 | 오일러 파이 함수는 암호학의 RSA 암호에서도 핵심적인 역할을 한다고 합니다. 37 | 이번 스터디에서 오일러 파이 함수의 간단한 개념과 원리(곱셈적 함수)에 대해 얘기해보면 좋을 거 같아요 :) 38 | */ 39 | -------------------------------------------------------------------------------- /src/doy/sort/Doy_Q2751.java: -------------------------------------------------------------------------------- 1 | package doy.sort; 2 | 3 | import java.util.*; 4 | 5 | public class Doy_Q2751 { 6 | // N개의 수가 주어졌을 때, 이를 오름차순으로 정렬하는 프로그램을 작성하시오. 7 | // 첫째 줄에 수의 개수 N(1 ≤ N ≤ 1,000,000)이 주어진다. 둘째 줄부터 N개의 줄에는 숫자가 주어진다. 이 수는 절대값이 1,000,000보다 작거나 같은 정수이다. 수는 중복되지 않는다. 8 | // 첫째 줄부터 N개의 줄에 오름차순으로 정렬한 결과를 한 줄에 하나씩 출력한다. 9 | static Scanner s = new Scanner(System.in); 10 | 11 | public static void run() { 12 | int t = s.nextInt(); 13 | List num = new ArrayList<>(); 14 | 15 | for(int i=0; i 0) { 19 | // while (stick > x) 20 | // stick /= 2; 21 | // x = x - stick; 22 | // res++; 23 | // } 24 | // System.out.println(res); 25 | 26 | /* [방법3] */ 27 | // ArrayList subStick = new ArrayList(); 28 | // int stick = 64; 29 | // subStick.add(64); 30 | // int res = 1; 31 | // while (true) { 32 | // if (stick == 0) 33 | // break; 34 | // 35 | // int sum = 0; 36 | // for (int i = 0; i < subStick.size(); i++) 37 | // sum += subStick.get(i); 38 | // 39 | // if (sum > x) { 40 | // subStick.remove(subStick.size() - 1); 41 | // stick /= 2; 42 | // subStick.add(stick); 43 | // continue; 44 | // } else if (sum < x) { 45 | // stick /= 2; 46 | // subStick.add(stick); 47 | // res++; 48 | // } else { 49 | // break; 50 | // } 51 | // } 52 | // System.out.println(res); 53 | 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/hee/boj/Hee_Q2455.java: -------------------------------------------------------------------------------- 1 | package hee.boj; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collections; 5 | import java.util.Scanner; 6 | 7 | public class Hee_Q2455 { 8 | static int add, sub; 9 | static ArrayList people = new ArrayList(); 10 | 11 | public static void run() { 12 | Scanner sc = new Scanner(System.in); 13 | 14 | int sum = 0; 15 | for (int i = 0; i < 4; i++) { 16 | sub = sc.nextInt(); 17 | add = sc.nextInt(); 18 | 19 | sum += add - sub; 20 | people.add(sum); 21 | } 22 | System.out.println(Collections.max(people)); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/hee/boj/Hee_Q6603.java: -------------------------------------------------------------------------------- 1 | package hee.boj; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Scanner; 5 | 6 | // 로또 7 | public class Hee_Q6603 { 8 | static int k; 9 | static int[] s; 10 | static boolean[] visited; 11 | static ArrayList print = new ArrayList(); 12 | 13 | public static void run() { 14 | Scanner sc = new Scanner(System.in); 15 | while (true) { 16 | k = sc.nextInt(); // 집합 S에 포함되는 수 17 | if (k == 0) // 종료 조건. 18 | return; 19 | 20 | s = new int[k]; 21 | visited = new boolean[k]; 22 | for (int i = 0; i < k; i++) { 23 | s[i] = sc.nextInt(); 24 | } 25 | 26 | dfs(0, 0); 27 | System.out.println(); 28 | } 29 | } 30 | 31 | static void dfs(int start, int depth) { 32 | // 출력 33 | if (depth == 6) { 34 | for (int i = 0; i < k; i++) { 35 | if(visited[i]) 36 | System.out.print(s[i] + " "); 37 | } 38 | System.out.println(); 39 | return; 40 | } 41 | 42 | // 재탐색. 43 | for (int i = start; i < k; i++) { 44 | if(!visited[i]) { 45 | visited[i] = true; 46 | dfs(i + 1, depth + 1); 47 | visited[i] = false; 48 | } 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/hee/boj/datastructure1/Hee_Q10808.java: -------------------------------------------------------------------------------- 1 | package hee.boj.datastructure1; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Hee_Q10808 { 6 | static Scanner scanner = new Scanner(System.in); 7 | 8 | public static void run(){ 9 | String S = scanner.next(); 10 | int[] cnt = new int[26]; 11 | 12 | for(int i=0; i= num){ 22 | upperCnt++; 23 | } else if ('a' <= num && 'z' >= num) { 24 | lowerCnt++; 25 | } else if ('0' <= num && '9' >= num) { 26 | blankCnt++; 27 | } else if (num == ' '){ 28 | numCnt++; 29 | } 30 | } 31 | System.out.println(lowerCnt + " " + upperCnt + " " + blankCnt + " " + numCnt); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/hee/boj/datastructure1/Hee_Q10824.java: -------------------------------------------------------------------------------- 1 | package hee.boj.datastructure1; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Hee_Q10824 { 6 | static Scanner scanner = new Scanner(System.in); 7 | 8 | public static void run(){ 9 | // [방법1] valueOf 메서드 이용 10 | // System.out.println(Long.valueOf(scanner.next() + scanner.next() ) + Long.valueOf(scanner.next() + scanner.next())); 11 | 12 | // [방법2] parseLong 메서드 이용 13 | String[] num = new String[4]; 14 | 15 | for (int i=0; i 0){ 15 | String command = scanner.next(); 16 | 17 | switch (command){ 18 | case "push": 19 | stack.push(scanner.nextInt()); 20 | break; 21 | 22 | case "pop": 23 | System.out.println(stack.empty() ? "-1" : stack.pop()); 24 | break; 25 | 26 | case "size": 27 | System.out.println(stack.size()); 28 | break; 29 | 30 | case "empty": 31 | System.out.println(stack.empty() ? "1" : "0"); 32 | break; 33 | 34 | case "top": 35 | System.out.println(stack.empty() ? "-1" : stack.peek()); 36 | break; 37 | default: 38 | break; 39 | } 40 | N--; 41 | } 42 | } 43 | } 44 | 45 | /* 46 | 풀이가 동일하네요. 47 | 삼항연산자를 쓰니 더 깔끔해 보이네요 :) 48 | */ 49 | -------------------------------------------------------------------------------- /src/hee/boj/datastructure1/Hee_Q11655.java: -------------------------------------------------------------------------------- 1 | package hee.boj.datastructure1; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Hee_Q11655 { 6 | static Scanner scanner = new Scanner(System.in); 7 | 8 | public static void run(){ 9 | String S = scanner.nextLine(); 10 | 11 | for (int i=0; i= 'A' && num <= 'Z'){ 16 | System.out.print(num <= 'Z'-13 ? (char)rot13 : (char)((rot13 % 'Z') + 64)); 17 | } else if(num >= 'a' && num <= 'z'){ 18 | System.out.print(num <= 'z'-13 ? (char)rot13 : (char)((rot13 % 'z') + 96)); 19 | } else { 20 | System.out.print((char)num); 21 | } 22 | 23 | // char num = S.charAt(i); 24 | // if(num >= 'A' && num <= 'Z'){ 25 | // num = (char)((num - 'A' + 13) % 26 + 'A'); 26 | // 27 | // } else if(num >= 'a' && num <= 'z'){ 28 | // num = (char)((num - 'a' + 13) % 26 + 'a'); 29 | // } 30 | // System.out.print(num); 31 | 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/hee/boj/datastructure1/Hee_Q11656.java: -------------------------------------------------------------------------------- 1 | package hee.boj.datastructure1; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class Hee_Q11656 { 7 | static Scanner scanner = new Scanner(System.in); 8 | 9 | public static void run(){ 10 | String S = scanner.next(); 11 | String[] suffix = new String[S.length()]; 12 | 13 | for (int i=0; i hashSet1 = new HashSet(); 13 | HashSet hashSet2 = new HashSet(); 14 | 15 | while (N-- > 0) { 16 | hashSet1.add(scanner.next()); 17 | } 18 | while (M-- > 0) { 19 | hashSet2.add(scanner.next()); 20 | } 21 | 22 | hashSet1.retainAll(hashSet2); // Set의 교집합 23 | 24 | /* TreeSet이면 정렬과정이 없어도 된다. */ 25 | String res[] = hashSet1.toArray(new String[hashSet1.size()]); 26 | Arrays.sort(res); // 오름차순 정렬 27 | 28 | System.out.println(res.length); 29 | for (int i = 0; i <= res.length - 1; i++) { 30 | System.out.println(res[i]); 31 | } 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/hee/boj/datastructure2/Hee_Q1927.java: -------------------------------------------------------------------------------- 1 | package hee.boj.datastructure2; 2 | 3 | import java.util.PriorityQueue; 4 | import java.util.Scanner; 5 | 6 | // 최소힙 구현 7 | public class Hee_Q1927 { 8 | static Scanner scanner = new Scanner(System.in); 9 | 10 | public static void run() { 11 | /* 최소힙 구현 */ 12 | // 우선순위 큐의 기본: 최소힙 13 | // o1>o2: 1, o1==o2: 0, o1 q = new PriorityQueue<>(); 15 | 16 | /* 최대힙 구현 */ 17 | // Comparator 인터페이스 재정의 18 | // o1>o2: -1, o1==o2: 0, o1 q = new PriorityQueue<>(1, new Comparator() { 20 | // @Override 21 | // public int compare(Integer o1, Integer o2) { 22 | // return o2.compareTo(o1); // return o2 - o1와 동일 23 | // } 24 | // }); 25 | 26 | 27 | int N = scanner.nextInt(); 28 | while (N-- > 0) { 29 | int x = scanner.nextInt(); 30 | 31 | if (x == 0) { 32 | if (q.isEmpty()) { 33 | System.out.println(0); 34 | } else { 35 | System.out.println(q.poll()); 36 | } 37 | } else { 38 | q.offer(x); 39 | } 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/hee/boj/etc/Hee_Q1000.java: -------------------------------------------------------------------------------- 1 | package hee.boj.etc; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Hee_Q1000 { 6 | static Scanner scanner = new Scanner(System.in); 7 | 8 | public static void run() { 9 | int num1 = scanner.nextInt(); 10 | int num2 = scanner.nextInt(); 11 | 12 | System.out.println(num1+num2); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/hee/boj/graph1/Hee_Q10451.java: -------------------------------------------------------------------------------- 1 | package hee.boj.graph1; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Hee_Q10451 { 6 | static Scanner scanner = new Scanner(System.in); 7 | static int[] permutation; // 순열 8 | static boolean[] visited; // 순열의 방문 여부(초기: 0) 9 | 10 | public static void run() { 11 | int testCase = scanner.nextInt(); 12 | 13 | while (testCase-- > 0) { 14 | int N = scanner.nextInt(); // 순열의 크기 N (2 ≤ N ≤ 1,000) 15 | 16 | permutation = new int[N + 1]; // 순열 17 | visited = new boolean[N + 1]; // 순열의 방문 여부(초기: false) 18 | 19 | for (int i=1; i arrayList = new ArrayList<>(); 14 | arrayList.add(A); 15 | 16 | while (true) { 17 | int res = 0; 18 | 19 | while (A != 0) { 20 | res += Math.pow(A % 10, P); // 나머지^P 21 | A = A / 10; // 몫 22 | } 23 | // System.out.println("res: " + res); 24 | 25 | if (arrayList.contains(res)) { // 해당하는 숫자가 이미 있으면 26 | System.out.println(arrayList.indexOf(res)); // 해당하는 숫자의 index를 구한다. 27 | return; 28 | } 29 | 30 | arrayList.add(res); 31 | A = res; 32 | } 33 | 34 | } 35 | } -------------------------------------------------------------------------------- /src/hee/boj/math1_1/Hee_Q10430.java: -------------------------------------------------------------------------------- 1 | package hee.boj.math1_1; 2 | 3 | import java.util.*; 4 | 5 | 6 | public class Hee_Q10430 { 7 | // (A+B)%C는 (A%C + B%C)%C 와 같을까? 8 | // (A×B)%C는 (A%C × B%C)%C 와 같을까? 9 | // 세 수 A, B, C가 주어졌을 때, 위의 네가지 값을 구하는 프로그램을 작성하시오. 10 | static Scanner scanner = new Scanner(System.in); 11 | 12 | public static void run() { 13 | int num1 = scanner.nextInt(); 14 | int num2 = scanner.nextInt(); 15 | int num3 = scanner.nextInt(); 16 | 17 | System.out.println(((num1 + num2)) % num3); 18 | System.out.println((num1 % num3 + num2 % num3) % num3); 19 | System.out.println( (num1 * num2) % num3); 20 | System.out.println((num1 % num3 * num2 % num3) % num3); 21 | } 22 | } 23 | 24 | /* 25 | 사실.. 이 문제의 의도가 뭔지 잘 모르겠네요 ㅎㅎㅎ 26 | 그냥 단순 계산일까요...? ㅠㅠ 27 | */ 28 | -------------------------------------------------------------------------------- /src/hee/boj/math1_1/Hee_Q11005.java: -------------------------------------------------------------------------------- 1 | package hee.boj.math1_1; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Scanner; 5 | 6 | // 10진법 수 N이 주어진다. 이 수를 B진법으로 바꿔 출력하는 프로그램을 작성하시오. 7 | // 첫째 줄에 N과 B가 주어진다. (2 ≤ B ≤ 36) N은 10억보다 작거나 같은 자연수이다. 8 | public class Hee_Q11005 { 9 | static Scanner scanner = new Scanner(System.in); 10 | 11 | static String codeToString(long code){ 12 | return Character.toString((char)(code + 55)); 13 | } 14 | 15 | public static void run(){ 16 | long num = scanner.nextLong(); 17 | int proposition = scanner.nextInt(); 18 | ArrayList res = new ArrayList<>(); 19 | 20 | // num = 0이 될 때까지 나머지를 계속 구한다 21 | while (num != 0){ 22 | long rest = (int)num%proposition; // 나머지를 구한다 23 | num = num/proposition; // 몫은 다시 num으로 넣는다 24 | 25 | // 나머지를 ArrayList에 넣는다 26 | if(rest >= 10){ 27 | res.add(codeToString(rest)); 28 | } else { 29 | res.add(Long.toString(rest)); 30 | } 31 | } 32 | 33 | // 값 출력 34 | for(int i=res.size()-1; i>=0; i--) 35 | System.out.print(res.get(i)); 36 | 37 | } 38 | } 39 | 40 | /* 41 | 값을 출력할 때 StringBuffer나 StringBuilder의 reverse를 이용하면 더 간편할거 같아요 :) 42 | 그리고 이렇게 일일이 나누는 방법 말고 Java API를 이용하는 방법도 있더라구요 (http://donggov.tistory.com/48) 43 | 이 방법도 한번 정리해보면 좋겠네요 ㅎㅎㅎ 44 | */ 45 | -------------------------------------------------------------------------------- /src/hee/boj/math1_1/Hee_Q9613.java: -------------------------------------------------------------------------------- 1 | package hee.boj.math1_1; 2 | 3 | import java.util.*; 4 | 5 | // 양의 정수 n개가 주어졌을 때, 가능한 모든 쌍의 GCD의 합을 구하는 프로그램을 작성하시오. 6 | public class Hee_Q9613 { 7 | static Scanner scanner = new Scanner(System.in); 8 | 9 | public static long gcd(long num1, long num2){ 10 | if(num2 == 0){ 11 | return num1; 12 | }else { 13 | return gcd(num2, num1%num2); 14 | } 15 | } 16 | 17 | public static void run(){ 18 | int testcase = scanner.nextInt(); 19 | 20 | for(int i=0; i1){ 22 | System.out.println(num); 23 | } 24 | 25 | } 26 | } 27 | 28 | /* 29 | 풀이가 동일하네요 :) 30 | 소인수분해에 대한 개념과 간단한 풀이와를 정리해보는 시간이였습니다 ㅎㅎ 31 | */ 32 | -------------------------------------------------------------------------------- /src/hee/boj/math1_2/Hee_Q1676.java: -------------------------------------------------------------------------------- 1 | package hee.boj.math1_2; 2 | 3 | import java.util.Scanner; 4 | 5 | // N!에서 뒤에서부터 처음 0이 아닌 숫자가 나올 때까지 0의 개수를 구하는 프로그램을 작성하시오. 6 | public class Hee_Q1676 { 7 | static Scanner scanner = new Scanner(System.in); 8 | 9 | public static void run(){ 10 | int num = scanner.nextInt(); 11 | 12 | // num를 소인수분해 했을 때 2*5의 개수가 0의 개수이다 13 | // 팩토리얼에서는 5의 개수가 항상 2의 개수보다 적기 때문에 5의 개수로만 판단 14 | // 즉, N/5 + N/5^2 + N/5^3 + N/5^4 ... 15 | int multiplier=1; 16 | while (num >= Math.pow(5, multiplier)){ 17 | multiplier++; 18 | } 19 | 20 | int result = 0; 21 | for (int j=1; j<=multiplier; j++){ 22 | result += (num/Math.pow(5, j)); 23 | } 24 | System.out.print(result); 25 | } 26 | } 27 | 28 | /* 29 | 풀이가 동일하네요 :) 30 | 반복문은 두 번 돌지만 제 코드보다 가독성은 더 좋아보이네요 ㅎㅎㅎ 31 | */ 32 | -------------------------------------------------------------------------------- /src/hee/boj/math1_2/Hee_Q2089.java: -------------------------------------------------------------------------------- 1 | package hee.boj.math1_2; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Hee_Q2089 { 6 | static Scanner scanner = new Scanner(System.in); 7 | 8 | static public void run(){ 9 | int num = scanner.nextInt(); 10 | StringBuilder stringBuilder = new StringBuilder(); 11 | 12 | if(num == 0){ 13 | System.out.print("0"); 14 | return; 15 | } 16 | 17 | while (num != 0){ 18 | // num가 음의 홀수인 경우 19 | if (num < 0 && (num % 2 != 0)){ 20 | stringBuilder.append(1); // 나머지 = 1; 21 | // System.out.println("num: " + num + ", stringBuilder: " + "1"); 22 | num = (num-1) / -2; 23 | } 24 | // 그 외의 경우 25 | else{ 26 | stringBuilder.append(num % -2); 27 | // System.out.println("num: " + num + ", stringBuilder: " + num % -2); 28 | num = num / -2; 29 | } 30 | } 31 | 32 | System.out.print(stringBuilder.reverse()); 33 | } 34 | } 35 | 36 | /* 37 | 평소에 생각해보지 못했던 -진수에 대해서 알아갑니다~ 38 | 해당 진수로 나누어 지지 않는 음수의 값 고려!! 39 | */ 40 | -------------------------------------------------------------------------------- /src/hee/boj/math2_1/Hee_Q9471.java: -------------------------------------------------------------------------------- 1 | package hee.boj.math2_1; 2 | 3 | import java.util.Scanner; 4 | 5 | // 피보나치 수열을 m으로 나눈 나머지가 주기를 이룬다 6 | // m이 주어졌을 때, k(m)(반복하는 부분 수열의 길이)을 구하는 프로그램 7 | // 첫째 줄에 테스트 케이스의 개수 P (1 ≤ P ≤ 1000) 8 | // N은 테스트 케이스의 번호이고, M은 문제에서 설명한 m이다. (2 ≤ M ≤ 1,000,000) 9 | public class Hee_Q9471 { 10 | static Scanner scanner = new Scanner(System.in); 11 | 12 | static int pisanoPeriod(int modNum){ 13 | int result = 0; 14 | 15 | int a = 1, b = 1, c; 16 | 17 | while (true){ 18 | c = (a + b) % modNum; 19 | b = a; 20 | a = c; 21 | result++; 22 | 23 | if (a == 1 && b == 1){ 24 | return result; 25 | } 26 | } 27 | } 28 | 29 | public static void run(){ 30 | int p = scanner.nextInt(); 31 | 32 | while (p > 0){ 33 | int i = scanner.nextInt(); 34 | int modNum = scanner.nextInt(); 35 | 36 | System.out.println(i + " " + pisanoPeriod(modNum)); 37 | 38 | p--; 39 | } 40 | 41 | } 42 | } 43 | /* 44 | 풀이가 동일하네요 :) 45 | 피사노 주기는 두번 째 1, 1이 나올 때까지 구하면 된다는 걸 알게 됐습니당 ^^ 46 | */ 47 | -------------------------------------------------------------------------------- /src/hee/boj/math2_2/Hee_Q11401.java: -------------------------------------------------------------------------------- 1 | package hee.boj.math2_2; 2 | 3 | public class Hee_Q11401 { 4 | } 5 | -------------------------------------------------------------------------------- /src/hee/boj/math2_2/Hee_Q11689.java: -------------------------------------------------------------------------------- 1 | package hee.boj.math2_2; 2 | 3 | import java.util.Scanner; 4 | 5 | // 자연수 n이 주어졌을 때, GCD(n, k) = 1을 만족하는 자연수 1 ≤ k ≤ n 의 개수를 구하는 프로그램 6 | // 자연수 n (1 ≤ n ≤ 10^12) 7 | public class Hee_Q11689 { 8 | static Scanner scanner = new Scanner(System.in); 9 | 10 | // 오일러 피 함수: 1~n까지의 자연수 중 n과 서소로인 수의 개수 11 | static long phi(long n) { 12 | long ans = n; 13 | 14 | for (long i = 2; i * i <= n; i++) { 15 | if (n % i == 0) { 16 | while (n % i == 0) { 17 | n /= i; 18 | } 19 | ans -= ans / i; 20 | } 21 | } 22 | if (n > 1) 23 | ans -= ans / n; 24 | 25 | return ans; 26 | } 27 | 28 | public static void run(){ 29 | long n = scanner.nextLong(); 30 | 31 | System.out.println(phi(n)); 32 | } 33 | } 34 | 35 | /* 36 | 풀이가 동일하네요 :) 37 | 오일러 피의 개념을 한 번 더 정리해보고, 응용 분야를 알아보면 좋을거 같아요! 38 | */ 39 | -------------------------------------------------------------------------------- /src/hee/boj/sort/Hee_Q11004.java: -------------------------------------------------------------------------------- 1 | package hee.boj.sort; 2 | 3 | import java.util.*; 4 | 5 | 6 | public class Hee_Q11004 { 7 | // 수 N개 A1, A2, ..., AN이 주어진다. 8 | // A를 오름차순 정렬했을 때, 앞에서부터 K번째 있는 수를 구하는 프로그램을 작성하시오. 9 | static Scanner scanner = new Scanner(System.in); 10 | 11 | public static void run() { 12 | int count = scanner.nextInt(); 13 | int position = scanner.nextInt(); 14 | 15 | int arrays[] = new int[count]; 16 | 17 | for (int i=0; i1000000){ 15 | System.out.println("범위를 벗어난 입력값입니다."); 16 | return; 17 | } 18 | 19 | int num[] = new int[count]; 20 | 21 | for(int i=0; i hashmap = new HashMap<>(); 12 | for (int num : A) { 13 | if (hashmap.containsKey(num)) { 14 | hashmap.replace(num, hashmap.get(num) + 1); 15 | continue; 16 | } 17 | hashmap.put(num, 1); 18 | } 19 | 20 | for (int key : hashmap.keySet()) { 21 | if (hashmap.get(key) % 2 != 0) 22 | return key; 23 | } 24 | return -1; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/hee/codility/lesson/CountingElements_FrogRiverOne.java: -------------------------------------------------------------------------------- 1 | package hee.codility.lesson; 2 | 3 | public class CountingElements_FrogRiverOne { 4 | // [TIP] 문제 제대로 이해하기!! input ([2, 2, 2, 2], 2) 생각!! 5 | public int solution(int X, int[] A) { 6 | boolean[] B = new boolean[X + 1]; 7 | int cnt = 0; 8 | for (int i = 0; i < A.length; i++) { 9 | if (A[i] <= X) { 10 | if (!B[A[i]]) { 11 | B[A[i]] = true; 12 | cnt++; 13 | } 14 | } 15 | if (cnt == X) { 16 | return i; // i 시간에 X 위치 도달 가능 17 | } 18 | } 19 | return -1; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/hee/codility/lesson/CountingElements_MaxCounters.java: -------------------------------------------------------------------------------- 1 | package hee.codility.lesson; 2 | 3 | public class CountingElements_MaxCounters { 4 | // [TIP] 시간 초과 주의!! 이중 반복문 쓰지 않도록 O(N + M) 5 | public int[] solution(int N, int[] A) { 6 | int[] C = new int[N]; 7 | int max = 0; 8 | int tmpMax = 0; 9 | 10 | for (int i = 0; i < A.length; i++) { 11 | /* max counter */ 12 | if (A[i] == N + 1) { 13 | // 임시 최댓값을 실제 최댓값으로 변경. 14 | max = tmpMax; 15 | } 16 | /* increase(X) */ 17 | else { 18 | // 최댓값 적용 19 | if (C[A[i] - 1] < max) { 20 | C[A[i] - 1] = max; 21 | } 22 | // 최댓값 적용 후 +1 23 | C[A[i] - 1]++; 24 | // increase(X)한 후의 값이 더 크면 임시 최댓값 변경. 25 | if (C[A[i] - 1] > tmpMax) { 26 | tmpMax = C[A[i] - 1]; 27 | } 28 | } 29 | } 30 | for (int i = 0; i < N; i++) { 31 | // 최댓값 적용 32 | if (C[i] < max) { 33 | C[i] = max; 34 | } 35 | } 36 | return C; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/hee/codility/lesson/CountingElements_MissingInteger.java: -------------------------------------------------------------------------------- 1 | package hee.codility.lesson; 2 | 3 | import java.util.Arrays; 4 | 5 | public class CountingElements_MissingInteger { 6 | // [TIP] 문제 제대로 이해하기!! input ([97, 98, 99]) 생각!! 7 | public int solution(int[] A) { 8 | Arrays.sort(A); 9 | 10 | if (A[A.length - 1] <= 0) { // 양의 정수가 없는 경우 11 | return 1; 12 | } 13 | 14 | boolean[] B = new boolean[1000001]; 15 | B[0] = true; 16 | for (int i = 0; i < A.length; i++) { 17 | if (A[i] > 0 && !B[A[i]]) { // 1 ~ 18 | B[A[i]] = true; 19 | } 20 | } 21 | for (int i = 0; i < B.length; i++) { 22 | if (!B[i]) { 23 | return i; 24 | } 25 | } 26 | return B.length; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/hee/codility/lesson/CountingElements_PermCheck.java: -------------------------------------------------------------------------------- 1 | package hee.codility.lesson; 2 | 3 | import java.util.Arrays; 4 | 5 | public class CountingElements_PermCheck { 6 | public int solution(int[] A) { 7 | // write your code in Java SE 8 8 | Arrays.sort(A); 9 | for (int i = 0; i < A.length; i++) { 10 | if (A[i] != i + 1) { 11 | // System.out.println(0); 12 | return 0; 13 | } 14 | } 15 | // System.out.println(1); 16 | return 1; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/hee/codility/lesson/Iterations_BinaryGap.java: -------------------------------------------------------------------------------- 1 | package hee.codility.lesson; 2 | 3 | public class Iterations_BinaryGap { 4 | public int solution(int N) { 5 | String n = Integer.toBinaryString(N); 6 | 7 | int maxGap = 0; 8 | int startIdx = 0, endIdx = 1; 9 | 10 | while (n.length() > endIdx) { 11 | if(n.charAt(endIdx) == '1') { 12 | int gap = endIdx - startIdx - 1; 13 | startIdx = endIdx; 14 | 15 | if (maxGap <= gap) { 16 | maxGap = gap; 17 | } 18 | } 19 | endIdx++; 20 | } 21 | return maxGap; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/hee/codility/lesson/PrefixSums_CountDiv.java: -------------------------------------------------------------------------------- 1 | package hee.codility.lesson; 2 | 3 | public class PrefixSums_CountDiv { 4 | // [TIP] input [11, 14, 2] 항상 짝수/홀수 구분해서 모두 테스트해보기!! 5 | public int solution(int A, int B, int K) { 6 | // write your code in Java SE 8 7 | if (A == B) { 8 | if (A % K == 0) 9 | return 1; 10 | else 11 | return 0; 12 | } 13 | 14 | if (A % K > 0) { 15 | return (B / K) - (A / K); 16 | } else { 17 | return (B / K) - (A / K) + 1; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/hee/codility/lesson/PrefixSums_MinAvgTwoSlice.java: -------------------------------------------------------------------------------- 1 | package hee.codility.lesson; 2 | 3 | public class PrefixSums_MinAvgTwoSlice { 4 | // [TIP] 2개 또는 3개(음수 포함)의 인자의 평균 값이 최소. O(N) 5 | public int solution(int[] A) { 6 | int idx = 0; 7 | float result = Float.MAX_VALUE; 8 | 9 | for(int i = 0 ; i < A.length ; i++){ 10 | if(i+1 < A.length){ 11 | if( result > (A[i] + A[i+1])/2f){ 12 | result = (A[i] + A[i+1])/2f ; 13 | idx = i; 14 | } 15 | } 16 | 17 | if(i+2 < A.length){ 18 | if( result > (A[i] + A[i+1] + A[i+2])/3f){ 19 | result = (A[i] + A[i+1] + A[i+2])/3f ; 20 | idx = i; 21 | } 22 | } 23 | } 24 | return idx; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/hee/codility/lesson/PrefixSums_PassingCars.java: -------------------------------------------------------------------------------- 1 | package hee.codility.lesson; 2 | 3 | public class PrefixSums_PassingCars { 4 | public int solution(int[] A) { 5 | int total = 0; 6 | for (int i = 0; i < A.length; i++) { 7 | if (A[i] == 1) 8 | total++; 9 | } 10 | 11 | int cnt = 0; 12 | int sum = 0; 13 | for (int i = 0; i < A.length; i++) { 14 | if (A[i] == 0) { // E 15 | sum += (total - cnt); 16 | } else { // W 17 | cnt++; 18 | } 19 | 20 | if (sum > 1000000000) 21 | // System.out.println(-1); 22 | return -1; 23 | } 24 | // System.out.println(sum); 25 | return sum; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/hee/codility/lesson/Sorting_Distinct.java: -------------------------------------------------------------------------------- 1 | package hee.codility.lesson; 2 | 3 | import java.util.HashSet; 4 | 5 | public class Sorting_Distinct { 6 | public int solution(int[] A) { 7 | HashSet hashSet = new HashSet<>(); 8 | for (int i = 0; i < A.length; i++) 9 | hashSet.add(A[i]); 10 | 11 | // System.out.println(hashSet.size()); 12 | return hashSet.size(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/hee/codility/lesson/Sorting_MaxProductOfThree.java: -------------------------------------------------------------------------------- 1 | package hee.codility.lesson; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Sorting_MaxProductOfThree { 6 | public int solution(int[] A) { 7 | Arrays.sort(A); 8 | int length = A.length; 9 | return Math.max(A[0] * A[1] * A[length - 1], A[length - 3] * A[length - 2] * A[length - 1]); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/hee/codility/lesson/Sorting_Triangle.java: -------------------------------------------------------------------------------- 1 | package hee.codility.lesson; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Sorting_Triangle { 6 | // [TIP] int 범위 주의!! 2,147,483,647 + 2,147,483,647 = -2, Long.valueOf(int); 이용 7 | public int solution(int[] A) { 8 | if (A.length == 0) { // 빈 문자열 9 | return 0; 10 | } 11 | 12 | Arrays.sort(A); 13 | 14 | for (int i = A.length - 1; i >= 2; i--) { 15 | if (A[i] < 0) 16 | break; 17 | 18 | long Ai = Long.valueOf(A[i]); 19 | long sum = Long.valueOf(A[i - 1]) + Long.valueOf(A[i - 2]); 20 | if (Ai < sum) { 21 | return 1; 22 | } 23 | 24 | } 25 | return 0; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/hee/codility/lesson/StacksandQueues_Brackets.java: -------------------------------------------------------------------------------- 1 | package hee.codility.lesson; 2 | 3 | import java.util.Stack; 4 | 5 | public class StacksandQueues_Brackets { 6 | // [TIP] inpit "{{{{", null, "" 7 | public int solution(String S) { 8 | if (S == null) { 9 | return 0; 10 | } else if (S.isEmpty()) { 11 | return 1; 12 | } 13 | 14 | Stack stack = new Stack<>(); 15 | for (Character C : S.toCharArray()) { 16 | if (C == ')') { 17 | if (!stack.isEmpty() && stack.peek() == '(') 18 | stack.pop(); 19 | else 20 | return 0; 21 | } else if (C == ']') { 22 | if (!stack.isEmpty() && stack.peek() == '[') 23 | stack.pop(); 24 | else 25 | return 0; 26 | } else if (C == '}') { 27 | if (!stack.isEmpty() && stack.peek() == '{') 28 | stack.pop(); 29 | else 30 | return 0; 31 | } else { 32 | stack.add(C); 33 | } 34 | } 35 | return stack.isEmpty() ? 1 : 0; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/hee/codility/lesson/StacksandQueues_Fish.java: -------------------------------------------------------------------------------- 1 | package hee.codility.lesson; 2 | 3 | import java.util.Stack; 4 | 5 | // https://app.codility.com/demo/results/trainingHJBT6G-52A/ 6 | // 다른 방법 참고: https://app.codility.com/demo/results/trainingFGC284-U39/ 7 | public class StacksandQueues_Fish { 8 | public int solution(int[] A, int[] B) { 9 | Stack stack = new Stack<>(); 10 | 11 | for (int i = 0; i < A.length; i++) { 12 | // UP 13 | if (B[i] == 0) { 14 | // 스택이 비어 있지 않고, 이전의 물고기의 방향이 0이 나올 때까지 15 | while (!stack.isEmpty() && B[stack.peek()] == 1) { 16 | // 이전의 물고기의 방향이 1이면 누군가는 잡아먹힌다. 17 | if (A[stack.peek()] < A[i]) { // 이김. 18 | stack.pop(); 19 | } else { // 짐. 20 | break; // 변경이 없으니, while 문 끝. 21 | } 22 | } // while 문 끝. 23 | 24 | // 스택이 비어있거나(처음 물고기 또는 모두 잡아먹은 경우), 이전의 물고기의 방향이 0이면 25 | if (stack.isEmpty() || B[stack.peek()] == 0) { 26 | stack.add(i); // 이긴 물고기를 넣는다. 졌으면 그냥 넘어감. 27 | } 28 | } 29 | // DOWN 30 | else if (B[i] == 1) { 31 | stack.add(i); 32 | } 33 | } 34 | // System.out.println(stack.size()); 35 | return stack.size(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/hee/codility/lesson/StacksandQueues_Nesting.java: -------------------------------------------------------------------------------- 1 | package hee.codility.lesson; 2 | 3 | // https://app.codility.com/demo/results/trainingZ8M368-F9E/ 4 | public class StacksandQueues_Nesting { 5 | public int solution(String S) { 6 | if (S.isEmpty()) { 7 | return 1; 8 | } 9 | 10 | int sum = 0; 11 | for (char C : S.toCharArray()) { 12 | if (sum < 0) { // 음수가 될 수 없다. 13 | return 0; 14 | } 15 | if (C == '(') { 16 | sum++; 17 | } else { 18 | sum--; 19 | } 20 | } 21 | if (sum == 0) { 22 | return 1; 23 | } else { 24 | return 0; 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/hee/codility/lesson/TimeComplexity_FrogJmp.java: -------------------------------------------------------------------------------- 1 | package hee.codility.lesson; 2 | 3 | public class TimeComplexity_FrogJmp { 4 | public int solution(int X, int Y, int D) { 5 | int c = Y - X; 6 | if (c == 0) { 7 | return 0; 8 | } 9 | 10 | int i = c / D; 11 | if (c % D != 0) { // 나머지가 있으면 12 | i++; 13 | } 14 | return i; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/hee/codility/lesson/TimeComplexity_PermMissingElem.java: -------------------------------------------------------------------------------- 1 | package hee.codility.lesson; 2 | 3 | import java.util.Arrays; 4 | 5 | public class TimeComplexity_PermMissingElem { 6 | // [TIP] 빈 행렬일 때와 마지막 값 확인!! 7 | public int solution(int[] A) { 8 | if (A.length == 0) { // 빈 행렬이면 첫 번째 값 생각!! 9 | return 1; 10 | } 11 | 12 | Arrays.sort(A); 13 | for (int i = 0; i < A.length; i++) { 14 | if (A[i] != i + 1) { 15 | return A[i] - 1; 16 | } 17 | } 18 | // 마지막까지 빠진 것이 없으면 마지막 값+1(N+1) 생각!! 19 | return A[A.length - 1] + 1; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/hee/codility/lesson/TimeComplexity_TapeEquilibrium.java: -------------------------------------------------------------------------------- 1 | package hee.codility.lesson; 2 | 3 | public class TimeComplexity_TapeEquilibrium { 4 | // [TIP] 시간복잡도 생각!! 이중 반복문: O(N^2) 5 | public int solution(int[] A) { 6 | int totalRight = 0; 7 | int totalLeft = 0; 8 | for (int i = 0; i < A.length; i++) { 9 | totalRight += A[i]; 10 | } 11 | 12 | int minDiff = Integer.MAX_VALUE; 13 | for (int P = 1; P < A.length; P++) { 14 | totalLeft += A[P - 1]; 15 | totalRight -= A[P - 1]; 16 | 17 | int diff = Math.abs(totalLeft - totalRight); 18 | if (minDiff > diff) { 19 | minDiff = diff; 20 | } 21 | } 22 | // System.out.println(minDiff); 23 | return minDiff; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/hee/codility/naverfin/Solution1.java: -------------------------------------------------------------------------------- 1 | package hee.codility.naverfin; 2 | 3 | import java.util.Arrays; 4 | import java.util.Set; 5 | import java.util.stream.Collectors; 6 | 7 | public class Solution1 { 8 | public int solution(int[] A) { 9 | Set integers = Arrays.stream(A) 10 | .boxed() 11 | .collect(Collectors.toSet()); 12 | 13 | int result = 0; 14 | for (int value : A) { 15 | if (integers.contains(value + 1)) { 16 | result++; 17 | } 18 | } 19 | return result; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/hee/codility/naverfin/Solution2.java: -------------------------------------------------------------------------------- 1 | package hee.codility.naverfin; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | public class Solution2 { 7 | Set charSet = new HashSet<>(); 8 | 9 | public int solution(String S) { 10 | if (S == null || S.isEmpty()) { 11 | return 0; 12 | } 13 | 14 | int result = 0; 15 | for (int i = 0; i < S.length(); i++) { 16 | String checkChar = Character.toString(S.charAt(i)); 17 | if (charSet.contains(checkChar)) { 18 | result++; 19 | initSet(); 20 | } 21 | charSet.add(checkChar); 22 | } 23 | if (charSet.size() != 0) { 24 | result++; 25 | } 26 | return result; 27 | } 28 | 29 | private void initSet() { 30 | charSet = new HashSet<>(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/hee/codility/naverfin/Solution3.java: -------------------------------------------------------------------------------- 1 | package hee.codility.naverfin; 2 | 3 | public class Solution3 { 4 | public boolean solution(int[] A, int K) { 5 | int n = A.length; 6 | for (int i = 0; i < n - 1; i++) { 7 | if (A[i] + 1 < A[i + 1]) 8 | return false; 9 | } 10 | if (A[0] != 1 || A[n - 1] != K) 11 | return false; 12 | else 13 | return true; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/hee/codility/practice/Practice1.java: -------------------------------------------------------------------------------- 1 | package hee.codility.practice; 2 | 3 | public class Practice1 { 4 | public int solution(int[] A) { 5 | 6 | int[] num1 = new int[]{0, A[0]}; 7 | int[] num2 = new int[]{1, A[1]}; 8 | int length = 2, max = 2; 9 | for (int i = 2; i < A.length; i++) { 10 | int checkNum = A[i]; 11 | if (checkNum == num1[1]) { 12 | num1[0] = i; 13 | length++; 14 | } else if (checkNum == num2[1]) { 15 | num2[0] = i; 16 | length++; 17 | } else { 18 | if (A[i - 1] == num1[1]) { 19 | length = i - num2[0]; 20 | num2[0] = i; 21 | num2[1] = checkNum; 22 | } else { 23 | length = i - num1[0]; 24 | num1[0] = i; 25 | num1[1] = checkNum; 26 | } 27 | } 28 | max = Math.max(max, length); 29 | } 30 | return max; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/hee/codility/w/Q1_2.java: -------------------------------------------------------------------------------- 1 | package hee.codility.w; 2 | 3 | import java.util.HashMap; 4 | 5 | public class Q1_2 { 6 | public final static int RADIX = 10; 7 | 8 | public static void main(String[] args) { 9 | String A = "A586QK"; 10 | String B = "JJ653K"; 11 | HashMap hashMap = new HashMap<>(); 12 | 13 | for (int i = 2; i < 10; i++) 14 | hashMap.put(Character.forDigit(i, RADIX), i); 15 | hashMap.put('T', 10); 16 | hashMap.put('J', 11); 17 | hashMap.put('Q', 12); 18 | hashMap.put('K', 13); 19 | hashMap.put('A', 14); 20 | 21 | int cnt = 0; 22 | char[] a = A.toCharArray(); 23 | char[] b = B.toCharArray(); 24 | 25 | for (int i = 0; i < a.length; i++) { 26 | if (hashMap.get(a[i]) > hashMap.get(b[i])) { 27 | cnt++; 28 | } 29 | } 30 | System.out.println(cnt); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/hee/codility/w/Q1_3.sql: -------------------------------------------------------------------------------- 1 | with results as( 2 | SELECT host_team as team_id, 3 as points 3 | FROM matches where host_goals > guest_goals 4 | UNION 5 | SELECT guest_team as team_id, 3 as points 6 | FROM matches where host_goals < guest_goals 7 | UNION 8 | SELECT host_team as team_id, 1 as points 9 | FROM matches where host_goals = guest_goals 10 | UNION 11 | SELECT guest_team as team_id, 1 as points 12 | FROM matches where host_goals = guest_goals 13 | ), totals as ( 14 | SELECT team_id, SUM(points) as num_points 15 | from results 16 | GROUP BY team_id 17 | ) 18 | SELECT t.team_id, t.team_name, COALESCE(totals.num_points, 0) as num_points 19 | FROM teams t LEFT JOIN totals 20 | ON t.team_id = totals.team_id -------------------------------------------------------------------------------- /src/hee/codility/w/Q1_4.java: -------------------------------------------------------------------------------- 1 | package hee.codility.w; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Q1_4 { 6 | public static void main(String[] args) { 7 | // int A = 1, B = 1, C = 2, D = 3; 8 | int A = 2, B = 4, C = 2, D = 4; 9 | 10 | int nums[] = new int[3]; 11 | nums[0] = squaredDistance(A, B, C, D); 12 | nums[1] = squaredDistance(A, C, B, D); 13 | nums[2] = squaredDistance(A, D, B, C); 14 | 15 | Arrays.sort(nums); 16 | System.out.println(nums[2]); 17 | // return nums[2]; 18 | } 19 | 20 | private static int squaredDistance(int n1, int n2, int n3, int n4) { 21 | return (int) Math.pow(n1 - n2, 2) + (int) Math.pow(n3 - n4, 2); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/hee/codility/z/Test1.java: -------------------------------------------------------------------------------- 1 | package hee.codility.z; 2 | 3 | public class Test1 { 4 | public int solution(int[] A) { 5 | if (A.length == 1) { 6 | // System.out.println(1); 7 | // return; 8 | return 1; 9 | } 10 | 11 | int cnt = 0; 12 | int i = 0; 13 | while (true) { 14 | if (A[i] == -1) { 15 | ++cnt; 16 | break; 17 | } 18 | i = A[i]; 19 | ++cnt; 20 | } 21 | // System.out.println(cnt); 22 | return cnt; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/hee/codility/z/Test3.java: -------------------------------------------------------------------------------- 1 | package hee.codility.z; 2 | 3 | import java.util.LinkedHashMap; 4 | 5 | public class Test3 { 6 | public int solution(int[] A) { 7 | if (A.length < 2) { 8 | // System.out.println(A.length); 9 | // return; 10 | return A.length; 11 | } 12 | 13 | LinkedHashMap nums = new LinkedHashMap(); 14 | nums.put(A[0], 0); // num, startIdx 15 | 16 | int maxLength = 1, length = 1; 17 | for (int i = 1; i < A.length; i++) { 18 | if (nums.containsKey(A[i])) { /* 이미 존재하는 숫자인 경우 */ 19 | ++length; // 길이 증가 20 | nums.remove(A[i]); // 순서 유지를 위해 해당 숫자(인덱스 변경됨)를 삭제 후 다시 추가 21 | } else { /* 새로운 숫자인 경우 */ 22 | if (nums.size() == 2) { // 이미 서로 다른 숫자가 2개인 경우 23 | length = i - nums.get(nums.keySet().toArray()[0]); // 길이 변경 24 | nums.remove(nums.keySet().toArray()[0]); // 첫 번째 숫자 제거 후 새로운 숫자 추가 25 | } else { // 새로운 나머지 하나의 숫자인 경우 26 | ++length; // 길이 증가, 새로운 숫자 추가 27 | } 28 | } 29 | nums.put(A[i], i); 30 | 31 | // 최대 길이 변경 32 | if (maxLength < length) { 33 | maxLength = length; 34 | } 35 | } 36 | // System.out.println(maxLength); 37 | return maxLength; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/hee/codility/z/Test4.java: -------------------------------------------------------------------------------- 1 | package hee.codility.z; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class Test4 { 7 | public int solution(int[] A) { 8 | if (A.length == 1) { 9 | // System.out.println(A[0] + A[0]); 10 | // return; 11 | return A[0] + A[0]; 12 | } 13 | 14 | int maxNum = A[0]; 15 | for (int i = 1; i < A.length; i++) 16 | maxNum = Math.max(maxNum, A[i]); 17 | 18 | List indexesOfMaxNum = new ArrayList<>(); 19 | for (int i = 0; i < A.length; i++) 20 | if (maxNum <= A[i]) 21 | indexesOfMaxNum.add(i); // 최댓값에 해당하는 인덱스 저장 22 | 23 | int max = Integer.MIN_VALUE; 24 | for (int i = 0; i < indexesOfMaxNum.size(); i++) { 25 | int maxLength = A[0] + Math.abs(indexesOfMaxNum.get(i)); 26 | 27 | for (int j = 1; j < A.length; j++) { 28 | int length = A[j] + Math.abs(indexesOfMaxNum.get(i) - j); // 자신의 값 + 해당 최댓값과의 거리 29 | maxLength = Math.max(maxLength, length); 30 | } 31 | max = Math.max(max, maxLength); 32 | } 33 | 34 | // System.out.println(max + maxNum); 35 | return max + maxNum; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/hee/leetcode/Counting_Bits_338.java: -------------------------------------------------------------------------------- 1 | package hee.leetcode; 2 | 3 | public class Counting_Bits_338 { 4 | 5 | public int[] countBits(int num) { 6 | if (num == 0) { 7 | return new int[]{0}; 8 | } else if (num == 1) { 9 | return new int[]{0, 1}; 10 | } 11 | 12 | // int[] result = new int[num + 1]; 13 | // result[0] = 0; 14 | // result[1] = 1; 15 | // 16 | // for (int i = 2; i < result.length; i++) { 17 | // // &: 두 비트 모두 1일 떄 1로 세팅 후 이진수를 십진수로 변환하여 return 18 | // result[i] = result[i & (i - 1)] + 1; 19 | // System.out.println("test: " + (i & (i - 1))); 20 | // System.out.println(result[i & (i - 1)]); 21 | // } 22 | // return result; 23 | 24 | int[] res = new int[num + 1]; 25 | res[0] = 0; 26 | int next = 2; int prev = 0; 27 | 28 | for (int i = 1; i < res.length; i++) { 29 | if (i == next) { 30 | res[i] = 1; 31 | prev = next; 32 | next = next * 2; 33 | } else { 34 | res[i] = 1 + res[i - prev]; 35 | } 36 | } 37 | return res; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/hee/leetcode/DefangIPaddr_1108.java: -------------------------------------------------------------------------------- 1 | package hee.leetcode; 2 | 3 | public class DefangIPaddr_1108 { 4 | public static final String REPLACE_STR = "[.]"; 5 | 6 | public String defangIPaddr(String address) { 7 | address = address.replace( ".", REPLACE_STR); 8 | return address; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/hee/leetcode/InorderTraversal_94.java: -------------------------------------------------------------------------------- 1 | package hee.leetcode; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | // 중위(in-order) 탐색 : 왼쪽 자식 노드, 내 노드, 오른쪽 자식노드 순서로 방문 7 | public class InorderTraversal_94 { 8 | List result = new ArrayList<>(); 9 | 10 | public List inorderTraversal(TreeNode root) { 11 | /* 중위 순회 */ 12 | if(root != null) { 13 | inorderTraversal(root.left); // 왼쪽 14 | result.add(root.val); // 루트 15 | inorderTraversal(root.right); // 오른쪽 16 | } 17 | return result; 18 | } 19 | 20 | public class TreeNode { 21 | int val; 22 | TreeNode left; 23 | TreeNode right; 24 | TreeNode(int x) { val = x; } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/hee/leetcode/NumJewelsInStones_771.java: -------------------------------------------------------------------------------- 1 | package hee.leetcode; 2 | 3 | public class NumJewelsInStones_771 { 4 | public int numJewelsInStones(String J, String S) { 5 | for (int i=0; i result = new ArrayList<>(); 8 | 9 | public List postorderTraversal(TreeNode root) { 10 | if (root != null) { 11 | postorderTraversal(root.left); // 왼쪽 12 | postorderTraversal(root.right); // 오른쪽 13 | result.add(root.val); // 루트 14 | } 15 | return result; 16 | } 17 | 18 | public class TreeNode { 19 | int val; 20 | TreeNode left; 21 | TreeNode right; 22 | TreeNode(int x) { val = x; } 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/hee/leetcode/PreorderTraversal_144.java: -------------------------------------------------------------------------------- 1 | package hee.leetcode; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class PreorderTraversal_144 { 7 | List result = new ArrayList<>(); 8 | 9 | public List preorderTraversal(TreeNode root) { 10 | if (root != null) { 11 | result.add(root.val); // 루트 12 | preorderTraversal(root.left); // 왼쪽 13 | preorderTraversal(root.right); // 오른쪽 14 | } 15 | return result; 16 | } 17 | 18 | public class TreeNode { 19 | int val; 20 | TreeNode left; 21 | TreeNode right; 22 | TreeNode(int x) { val = x; } 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/hee/leetcode/Two_Sum_1.java: -------------------------------------------------------------------------------- 1 | package hee.leetcode; 2 | 3 | public class Two_Sum_1 { 4 | public int[] twoSum(int[] nums, int target) { 5 | 6 | for (int i = 0; i < nums.length; i++) { 7 | for (int j = i + 1; j < nums.length; j++) { 8 | if (nums[i] + nums[j] == target) { 9 | return new int[]{i, j}; 10 | } 11 | } 12 | } 13 | return null; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /test/hee/codility/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package hee.codility; 2 | 3 | import hee.Solution; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | 7 | import static org.hamcrest.CoreMatchers.is; 8 | import static org.junit.Assert.assertThat; 9 | 10 | public class SolutionTest { 11 | 12 | private Solution solution; 13 | 14 | @Before 15 | public void setUp() { 16 | solution = new Solution(); 17 | } 18 | 19 | @Test 20 | public void test() { 21 | // int[] A = {4, 4, 3, 0, 1, 0}; // 3 22 | // 23 | // boolean result = solution.solution(A); 24 | // 25 | // assertThat(result, is(true)); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /test/hee/codility/lesson/Arrays_CyclicRotation_Test.java: -------------------------------------------------------------------------------- 1 | package hee.codility.lesson; 2 | 3 | import org.junit.Before; 4 | import org.junit.Test; 5 | 6 | import java.util.Arrays; 7 | 8 | import static org.hamcrest.CoreMatchers.is; 9 | import static org.junit.Assert.*; 10 | 11 | public class Arrays_CyclicRotation_Test { 12 | private Arrays_CyclicRotation solution; 13 | 14 | @Before 15 | public void setUp() { 16 | solution = new Arrays_CyclicRotation(); 17 | } 18 | 19 | @Test 20 | public void test1() { 21 | int[] A = {3, 8, 9, 7, 6}; 22 | int[] result = solution.solution(A, 3); 23 | 24 | int[] ans = {9, 7, 6, 3, 8}; 25 | assertTrue(Arrays.equals(result, ans)); 26 | } 27 | 28 | @Test 29 | public void test2() { 30 | int[] A = {}; 31 | int[] result = solution.solution(A, 0); 32 | 33 | assertThat(result, is(new int[]{})); 34 | } 35 | 36 | @Test 37 | public void test3() { 38 | int[] A = {1, 2, 3, 4}; 39 | int[] result = solution.solution(A, 100); 40 | 41 | assertArrayEquals(new int[]{1, 2, 3, 4}, result); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /test/hee/codility/lesson/Arrays_OddOccurrencesInArray_Test.java: -------------------------------------------------------------------------------- 1 | package hee.codility.lesson; 2 | 3 | import org.junit.Before; 4 | import org.junit.Test; 5 | 6 | import static org.hamcrest.CoreMatchers.is; 7 | import static org.junit.Assert.assertEquals; 8 | import static org.junit.Assert.assertThat; 9 | 10 | public class Arrays_OddOccurrencesInArray_Test { 11 | private Arrays_OddOccurrencesInArray solution; 12 | 13 | @Before 14 | public void setUp() { 15 | solution = new Arrays_OddOccurrencesInArray(); 16 | } 17 | 18 | @Test 19 | public void test1() { 20 | int[] A = {9, 3, 9, 3, 9, 7, 9}; 21 | int result = solution.solution(A); 22 | 23 | assertEquals(7, result); 24 | } 25 | 26 | @Test 27 | public void test2() { 28 | int[] A = {1}; 29 | int result = solution.solution(A); 30 | 31 | assertThat(result, is(1)); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /test/hee/codility/naverfin/Solution1Test.java: -------------------------------------------------------------------------------- 1 | package hee.codility.naverfin; 2 | 3 | import org.junit.Before; 4 | import org.junit.Test; 5 | 6 | import static org.hamcrest.CoreMatchers.is; 7 | import static org.junit.Assert.assertThat; 8 | 9 | public class Solution1Test { 10 | 11 | private Solution1 solution; 12 | 13 | @Before 14 | public void setUp() { 15 | solution = new Solution1(); 16 | } 17 | 18 | @Test 19 | public void test1() { 20 | // test1 21 | // int[] A = {3, 4, 3, 0, 2, 2, 3, 0, 0}; // 5 22 | // int[] A = {4, 2, 0}; // 0 23 | int[] A = {4, 4, 3, 0, 1, 0}; // 3 24 | 25 | // int[] A = {3, -1, -5, -9}; // 3 26 | // int[] A = {0, 1}; // 0 27 | // int[] A = {1, 1, 2, 5, 7}; // 0 28 | // int[] A = {-1, 1, 3, 3, 3, 2, 3, 2, 1, 0, -1}; // 8 29 | // int[] A = {-1, -1, -1}; // 1 30 | int result = solution.solution(A); 31 | 32 | assertThat(result, is(3)); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /test/hee/codility/naverfin/Solution2Test.java: -------------------------------------------------------------------------------- 1 | package hee.codility.naverfin; 2 | 3 | import org.junit.Before; 4 | import org.junit.Test; 5 | 6 | import static org.hamcrest.CoreMatchers.is; 7 | import static org.junit.Assert.assertThat; 8 | 9 | public class Solution2Test { 10 | 11 | private Solution2 solution; 12 | 13 | @Before 14 | public void setUp() { 15 | solution = new Solution2(); 16 | } 17 | 18 | @Test 19 | public void test2() { 20 | // test1 21 | // String A = "abacdec"; 22 | String A = "dddd"; 23 | int result = solution.solution(A); 24 | 25 | assertThat(result, is(4)); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /test/hee/codility/naverfin/Solution3Test.java: -------------------------------------------------------------------------------- 1 | package hee.codility.naverfin; 2 | 3 | import org.junit.Before; 4 | import org.junit.Test; 5 | 6 | import static org.hamcrest.CoreMatchers.is; 7 | import static org.junit.Assert.assertThat; 8 | 9 | public class Solution3Test { 10 | 11 | private Solution3 solution; 12 | 13 | @Before 14 | public void setUp() { 15 | solution = new Solution3(); 16 | } 17 | 18 | @Test 19 | public void test3() { 20 | // test1 21 | // int[] A = new int[]{1, 1, 2, 4, 5}; 22 | // int K = 5; 23 | 24 | // int[] A = new int[]{1, 1, 3}; 25 | // int K = 2; 26 | 27 | // int[] A = new int[]{1, 1}; 28 | // int K = 1; 29 | 30 | // int[] A = new int[]{1, 1, 2}; 31 | // int K = 1; 32 | 33 | int[] A = new int[]{1, 2, 2, 2, 2, 3}; 34 | int K = 2; 35 | 36 | // int max = 1000000000; 37 | // int[] A = new int[max]; 38 | // for (int i = 0; i < max; i++) { 39 | // A[i] = i + 1; 40 | // } 41 | // int K = 1000000001; 42 | 43 | boolean result = solution.solution(A, K); 44 | 45 | // assertThat(result, is(true)); 46 | assertThat(result, is(false)); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /test/hee/codility/z/Test1Test.java: -------------------------------------------------------------------------------- 1 | package hee.codility.z; 2 | 3 | import org.junit.Before; 4 | import org.junit.Test; 5 | 6 | import static org.hamcrest.CoreMatchers.is; 7 | import static org.junit.Assert.assertEquals; 8 | import static org.junit.Assert.assertThat; 9 | 10 | public class Test1Test { 11 | private Test1 solution; 12 | 13 | @Before 14 | public void setUp() { 15 | solution = new Test1(); 16 | } 17 | 18 | @Test 19 | public void test1() { 20 | int[] A = {1, -1}; 21 | int result = solution.solution(A); 22 | 23 | assertThat(result, is(2)); 24 | } 25 | 26 | @Test 27 | public void test2() { 28 | int[] A = {1}; 29 | int result = solution.solution(A); 30 | 31 | assertEquals(1, result); 32 | } 33 | 34 | @Test 35 | public void test3() { 36 | int[] A = {1, 4, -1, 3, 2}; 37 | int result = solution.solution(A); 38 | 39 | assertThat(result, is(4)); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /test/hee/codility/z/Test2Test.java: -------------------------------------------------------------------------------- 1 | package hee.codility.z; 2 | 3 | import org.junit.Before; 4 | import org.junit.Test; 5 | 6 | import static org.hamcrest.CoreMatchers.is; 7 | import static org.junit.Assert.assertThat; 8 | 9 | public class Test2Test { 10 | private Test2 solution; 11 | 12 | @Before 13 | public void setUp() { 14 | solution = new Test2(); 15 | } 16 | 17 | @Test 18 | public void test1() { 19 | int[] A = {1, 3, 5, 7, 9}; // 6 20 | // int[] A = {7, 7, 7, 7}; // 3 21 | // int[] A = {3, -1, -5, -9}; // 3 22 | // int[] A = {0, 1}; // 0 23 | // int[] A = {1, 1, 2, 5, 7}; // 0 24 | // int[] A = {-1, 1, 3, 3, 3, 2, 3, 2, 1, 0, -1}; // 8 25 | // int[] A = {-1, -1, -1}; // 1 26 | int result = solution.solution(A); 27 | 28 | assertThat(result, is(6)); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /test/hee/codility/z/Test4Test.java: -------------------------------------------------------------------------------- 1 | package hee.codility.z; 2 | 3 | import org.junit.Before; 4 | import org.junit.Test; 5 | 6 | import static org.hamcrest.CoreMatchers.is; 7 | import static org.junit.Assert.assertThat; 8 | 9 | public class Test4Test { 10 | private Test4 solution; 11 | 12 | @Before 13 | public void setUp() { 14 | solution = new Test4(); 15 | } 16 | 17 | @Test 18 | public void test1() { 19 | int[] A = {1, 3, -1}; // 6 20 | int result = solution.solution(A); 21 | 22 | assertThat(result, is(6)); 23 | } 24 | 25 | @Test 26 | public void test2() { 27 | int[] A = {-1000000000, 1}; // 2 28 | int result = solution.solution(A); 29 | 30 | assertThat(result, is(2)); 31 | } 32 | 33 | @Test 34 | public void test3() { 35 | int[] A = {1}; // 2 36 | int result = solution.solution(A); 37 | 38 | assertThat(result, is(2)); 39 | } 40 | 41 | @Test 42 | public void test4() { 43 | int[] A = {-8, 4, 0, 5, -3, 6}; // 14 44 | // int[] A = {-8, 6, 0, 5, -3, 6}; 45 | // int[] A = {-8, -8, -8, -8, -8}; 46 | // int[] A = {6, 6, 5, 6, 6}; 47 | // int[] A = {-8, 6, 0, 6, -3, 6}; 48 | int result = solution.solution(A); 49 | 50 | assertThat(result, is(14)); 51 | } 52 | 53 | } 54 | --------------------------------------------------------------------------------