├── .gitignore ├── AllQuestions.md ├── LICENSE ├── PendingQuestions.md ├── README.md ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src ├── main └── java │ └── in │ └── ashwanik │ └── dcp │ ├── common │ ├── ListNode.java │ └── TreeNode.java │ └── problems │ ├── p121_150 │ ├── p121 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p125 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p126 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p127 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p128 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p129 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p131 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p133 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p134 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p135 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p136 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p137 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p141 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p142 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p143 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p145 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p146 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p147 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p148 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p149 │ │ ├── Readme.md │ │ └── Solution.java │ └── p150 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p151_180 │ ├── p151 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p153 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p154 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p155 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p156 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p157 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p159 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p163 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p164 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p166 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p168 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p169 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p170 │ │ ├── Readme.md │ │ ├── Solution.java │ │ └── variation │ │ │ ├── Readme.md │ │ │ └── Solution.java │ ├── p171 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p172 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p173 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p175 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p176 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p177 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p178 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p179 │ │ ├── Readme.md │ │ └── Solution.java │ └── p180 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p181_210 │ ├── p182 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p184 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p189 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p190 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p191 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p195 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p196 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p197 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p198 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p199 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p201 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p202 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p203 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p204 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p205 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p206 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p208 │ │ ├── Readme.md │ │ └── Solution.java │ └── p210 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p1_30 │ ├── p1 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p11 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p12 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p13 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p15 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p16 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p17 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p18 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p19 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p2 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p20 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p21 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p22 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p23 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p24 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p25 │ │ └── Readme.md │ ├── p26 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p27 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p28 │ │ ├── Leetcode.java │ │ ├── Readme.md │ │ └── Solution.java │ ├── p29 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p3 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p30 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p4 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p7 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p8 │ │ ├── Readme.md │ │ └── Solution.java │ └── p9 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p211_p240 │ ├── p211 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p212 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p213 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p214 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p215 │ │ ├── Readme.md │ │ └── Solution.java │ └── p216 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p31_60 │ ├── p31 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p33 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p34 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p35 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p36 │ │ ├── MorrisTraversal.java │ │ ├── Readme.md │ │ └── Solution.java │ ├── p37 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p38 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p39 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p41 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p42 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p43 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p44 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p45 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p46 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p47 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p48 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p49 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p50 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p51 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p52 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p53 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p55 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p57 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p58 │ │ ├── Readme.md │ │ └── Solution.java │ └── p60 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p61_90 │ ├── p61 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p62 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p65 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p66 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p67 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p69 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p70 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p73 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p74 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p77 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p78 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p79 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p80 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p81 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p83 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p84 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p85 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p86 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p88 │ │ ├── Readme.md │ │ └── Solution.java │ ├── p89 │ │ ├── Readme.md │ │ └── Solution.java │ └── p90 │ │ ├── Readme.md │ │ └── Solution.java │ └── p91_120 │ ├── p100 │ ├── Readme.md │ └── Solution.java │ ├── p101 │ ├── Readme.md │ └── Solution.java │ ├── p102 │ ├── Readme.md │ └── Solution.java │ ├── p103 │ ├── Readme.md │ └── Solution.java │ ├── p104 │ ├── Readme.md │ └── Solution.java │ ├── p107 │ ├── Readme.md │ └── Solution.java │ ├── p108 │ ├── Readme.md │ └── Solution.java │ ├── p110 │ ├── Readme.md │ └── Solution.java │ ├── p111 │ ├── Readme.md │ └── Solution.java │ ├── p112 │ ├── Readme.md │ └── Solution.java │ ├── p113 │ ├── Readme.md │ └── Solution.java │ ├── p114 │ ├── Readme.md │ └── Solution.java │ ├── p115 │ ├── Readme.md │ └── Solution.java │ ├── p116 │ ├── Readme.md │ └── Solution.java │ ├── p117 │ ├── Readme.md │ └── Solution.java │ ├── p118 │ ├── Readme.md │ └── Solution.java │ ├── p119 │ ├── Readme.md │ └── Solution.java │ ├── p120 │ ├── Readme.md │ └── Solution.java │ ├── p92 │ ├── Readme.md │ └── Solution.java │ ├── p94 │ ├── Readme.md │ └── Solution.java │ ├── p95 │ ├── Readme.md │ └── Solution.java │ ├── p97 │ ├── Readme.md │ └── Solution.java │ └── p99 │ ├── Readme.md │ └── Solution.java └── test └── java └── in └── ashwanik └── dcp └── problems ├── p121_150 ├── p121 │ └── SolutionTest.java ├── p125 │ └── SolutionTest.java ├── p126 │ └── SolutionTest.java ├── p127 │ └── SolutionTest.java ├── p128 │ └── SolutionTest.java ├── p129 │ └── SolutionTest.java ├── p131 │ └── SolutionTest.java ├── p133 │ └── SolutionTest.java ├── p134 │ └── SolutionTest.java ├── p135 │ └── SolutionTest.java ├── p136 │ └── SolutionTest.java ├── p137 │ └── SolutionTest.java ├── p141 │ └── SolutionTest.java ├── p142 │ └── SolutionTest.java ├── p143 │ └── SolutionTest.java ├── p145 │ └── SolutionTest.java ├── p146 │ └── SolutionTest.java ├── p147 │ └── SolutionTest.java ├── p148 │ └── SolutionTest.java └── p150 │ └── SolutionTest.java ├── p151_180 ├── p151 │ └── SolutionTest.java ├── p153 │ └── SolutionTest.java ├── p154 │ └── SolutionTest.java ├── p155 │ └── SolutionTest.java ├── p156 │ └── SolutionTest.java ├── p157 │ └── SolutionTest.java ├── p159 │ └── SolutionTest.java ├── p163 │ └── SolutionTest.java ├── p164 │ └── SolutionTest.java ├── p166 │ └── SolutionTest.java ├── p168 │ └── SolutionTest.java ├── p169 │ └── SolutionTest.java ├── p170 │ └── variation │ │ └── SolutionTest.java ├── p171 │ └── SolutionTest.java ├── p172 │ └── SolutionTest.java ├── p173 │ └── SolutionTest.java ├── p175 │ └── SolutionTest.java ├── p176 │ └── SolutionTest.java ├── p177 │ └── SolutionTest.java ├── p178 │ └── SolutionTest.java ├── p179 │ └── SolutionTest.java └── p180 │ └── SolutionTest.java ├── p181_210 ├── p182 │ └── SolutionTest.java ├── p184 │ └── SolutionTest.java ├── p189 │ └── SolutionTest.java ├── p190 │ └── SolutionTest.java ├── p191 │ └── SolutionTest.java ├── p195 │ └── SolutionTest.java ├── p196 │ └── SolutionTest.java ├── p197 │ └── SolutionTest.java ├── p198 │ └── SolutionTest.java ├── p199 │ └── SolutionTest.java ├── p201 │ └── SolutionTest.java ├── p202 │ └── SolutionTest.java ├── p203 │ └── SolutionTest.java ├── p204 │ └── SolutionTest.java ├── p205 │ └── SolutionTest.java ├── p206 │ └── SolutionTest.java ├── p208 │ └── SolutionTest.java └── p210 │ └── SolutionTest.java ├── p1_30 ├── p1 │ └── SolutionTest.java ├── p11 │ └── SolutionTest.java ├── p12 │ └── SolutionTest.java ├── p13 │ └── SolutionTest.java ├── p15 │ └── SolutionTest.java ├── p16 │ └── SolutionTest.java ├── p17 │ └── SolutionTest.java ├── p18 │ └── SolutionTest.java ├── p2 │ └── SolutionTest.java ├── p20 │ └── SolutionTest.java ├── p21 │ └── SolutionTest.java ├── p22 │ └── SolutionTest.java ├── p23 │ └── SolutionTest.java ├── p24 │ └── SolutionTest.java ├── p26 │ └── SolutionTest.java ├── p27 │ └── SolutionTest.java ├── p28 │ ├── LeetcodeTest.java │ └── SolutionTest.java ├── p29 │ └── SolutionTest.java ├── p3 │ └── SolutionTest.java ├── p30 │ └── SolutionTest.java ├── p4 │ └── SolutionTest.java ├── p7 │ └── SolutionTest.java ├── p8 │ └── SolutionTest.java └── p9 │ └── SolutionTest.java ├── p211_p240 ├── p211 │ └── SolutionTest.java ├── p212 │ └── SolutionTest.java ├── p213 │ └── SolutionTest.java ├── p214 │ └── SolutionTest.java ├── p215 │ └── SolutionTest.java └── p216 │ └── SolutionTest.java ├── p31_60 ├── p31 │ └── SolutionTest.java ├── p33 │ └── SolutionTest.java ├── p35 │ └── SolutionTest.java ├── p36 │ ├── MorrisTraversalTest.java │ └── SolutionTest.java ├── p37 │ └── SolutionTest.java ├── p38 │ └── SolutionTest.java ├── p39 │ └── SolutionTest.java ├── p41 │ └── SolutionTest.java ├── p42 │ └── SolutionTest.java ├── p43 │ └── SolutionTest.java ├── p44 │ └── SolutionTest.java ├── p45 │ └── SolutionTest.java ├── p46 │ └── SolutionTest.java ├── p47 │ └── SolutionTest.java ├── p48 │ └── SolutionTest.java ├── p49 │ └── SolutionTest.java ├── p50 │ └── SolutionTest.java ├── p52 │ └── SolutionTest.java ├── p53 │ └── SolutionTest.java ├── p55 │ └── SolutionTest.java ├── p57 │ └── SolutionTest.java ├── p58 │ └── SolutionTest.java └── p60 │ └── SolutionTest.java ├── p61_90 ├── p61 │ └── SolutionTest.java ├── p62 │ └── SolutionTest.java ├── p65 │ └── SolutionTest.java ├── p66 │ └── SolutionTest.java ├── p67 │ └── SolutionTest.java ├── p69 │ └── SolutionTest.java ├── p70 │ └── SolutionTest.java ├── p73 │ └── SolutionTest.java ├── p74 │ └── SolutionTest.java ├── p77 │ └── SolutionTest.java ├── p78 │ └── SolutionTest.java ├── p79 │ └── SolutionTest.java ├── p80 │ └── SolutionTest.java ├── p81 │ └── SolutionTest.java ├── p83 │ └── SolutionTest.java ├── p84 │ └── SolutionTest.java ├── p85 │ └── SolutionTest.java ├── p86 │ └── SolutionTest.java ├── p88 │ └── SolutionTest.java ├── p89 │ └── SolutionTest.java └── p90 │ └── SolutionTest.java └── p91_120 ├── p100 └── SolutionTest.java ├── p101 └── SolutionTest.java ├── p102 └── SolutionTest.java ├── p103 └── SolutionTest.java ├── p104 └── SolutionTest.java ├── p107 └── SolutionTest.java ├── p108 └── SolutionTest.java ├── p110 └── SolutionTest.java ├── p111 └── SolutionTest.java ├── p112 └── SolutionTest.java ├── p113 └── SolutionTest.java ├── p114 └── SolutionTest.java ├── p115 └── SolutionTest.java ├── p116 └── SolutionTest.java ├── p117 └── SolutionTest.java ├── p118 └── SolutionTest.java ├── p119 └── SolutionTest.java ├── p120 └── SolutionTest.java ├── p92 └── SolutionTest.java ├── p94 └── SolutionTest.java ├── p95 └── SolutionTest.java ├── p97 └── SolutionTest.java └── p99 └── SolutionTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /build/ 3 | .terraform 4 | 5 | ### STS ### 6 | .apt_generated 7 | .classpath 8 | .factorypath 9 | .project 10 | .settings 11 | .springBeans 12 | classes 13 | 14 | ### IntelliJ IDEA ### 15 | .idea 16 | *.iws 17 | *.iml 18 | *.ipr 19 | out 20 | 21 | ### NetBeans ### 22 | nbproject/private/ 23 | build/ 24 | nbbuild/ 25 | dist/ 26 | nbdist/ 27 | .nb-gradle/ 28 | .DS_Store 29 | logs/app.log 30 | logs/ 31 | bin 32 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | } 4 | 5 | group 'in.ashwanik' 6 | version '1.0-SNAPSHOT' 7 | 8 | sourceCompatibility = 1.8 9 | 10 | repositories { 11 | mavenCentral() 12 | } 13 | 14 | dependencies { 15 | testImplementation('org.junit.jupiter:junit-jupiter:5.4.2') 16 | 17 | 18 | } 19 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashwanikumar04/daily-coding-problems/765c469557b0655604f83d785640865284e351f8/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Apr 23 17:10:12 BST 2019 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.10-all.zip 7 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'dcp' 2 | 3 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p121_150/p121/Readme.md: -------------------------------------------------------------------------------- 1 | Given a string which we can delete at most k, return whether you can make a 2 | palindrome. 3 | 4 | For example, given 'waterrfetawx' and a k of 2, you could delete f and x to get 5 | 'waterretaw'. 6 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p121_150/p125/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | Given the root of a binary search tree, and a target K, return two nodes in the 3 | tree whose sum equals K. 4 | 5 | For example, given the following tree and K of 20 6 | ``` 7 | 10 8 | / \ 9 | 5 15 10 | / \ 11 | 11 15 12 | 13 | ``` 14 | Return the nodes 5 and 15. -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p121_150/p126/Readme.md: -------------------------------------------------------------------------------- 1 | Write a function that rotates a list by k elements. For example, [1, 2, 3, 4, 5, 2 | 6] rotated by two becomes [3, 4, 5, 6, 1, 2]. Try solving this without creating 3 | a copy of the list. How many swap or move operations do you need? -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p121_150/p126/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p121_150.p126; 2 | 3 | class Solution { 4 | void rotateArray(int[] array, int k) { 5 | if (array == null || array.length < k) { 6 | return; 7 | } 8 | 9 | for (int index = 0; index < k; index++) { 10 | rotateArrayByOne(array); 11 | } 12 | } 13 | 14 | private void rotateArrayByOne(int[] array) { 15 | int index; 16 | int first = array[0]; 17 | for (index = 0; index < array.length - 1; index++) { 18 | array[index] = array[index + 1]; 19 | } 20 | array[index] = first; 21 | } 22 | } -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p121_150/p127/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | This problem was asked by Microsoft. 3 | 4 | Let's represent an integer in a linked list format by having each node represent 5 | a digit in the number. The nodes make up the number in reversed order. 6 | 7 | For example, the following linked list: 8 | 9 | 1 -> 2 -> 3 -> 4 -> 5 10 | 11 | 12 | is the number 54321. 13 | 14 | Given two linked lists in this format, return their sum in the same linked list 15 | format. 16 | 17 | For example, given 18 | 19 | 9 -> 9 20 | 21 | 22 | 5 -> 2 23 | 24 | 25 | return 124 (99 + 25) as: 26 | 27 | 4 -> 2 -> 1 28 | 29 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p121_150/p129/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | Given a real number n, find the square root of n. For example, given n = 9, 4 | return 3. 5 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p121_150/p129/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p121_150.p129; 2 | 3 | class Solution { 4 | 5 | int sqrt(int number) { 6 | 7 | if (number <= 0) { 8 | return 0; 9 | } 10 | int answer = 0; 11 | int start = 0; 12 | int end = number; 13 | while (start <= end) { 14 | int mid = start + (end - start) / 2; 15 | int sqr = mid * mid; 16 | if (sqr == number) { 17 | return mid; 18 | } 19 | if (sqr < number) { 20 | start = mid + 1; 21 | answer = mid; 22 | } else { 23 | end = mid - 1; 24 | } 25 | } 26 | return answer; 27 | } 28 | } -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p121_150/p131/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | Given the head to a singly linked list, where each node also has a “random” 3 | pointer that points to anywhere in the linked list, deep clone the list. 4 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p121_150/p133/Readme.md: -------------------------------------------------------------------------------- 1 | Given a node in a binary tree, return the next bigger element, also known as the 2 | inorder successor. 3 | 4 | For example, the inorder successor of 22 is 30. 5 | ``` 6 | 10 7 | / \ 8 | 5 30 9 | / \ 10 | 22 35 11 | ``` 12 | 13 | You can assume each node has a parent pointer. -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p121_150/p134/Readme.md: -------------------------------------------------------------------------------- 1 | You have a large array with most of the elements as zero. 2 | 3 | Use a more space-efficient data structure, SparseArray, that implements the same 4 | interface: 5 | 6 | * init(arr, size): initialize with the original large array and size. 7 | * set(i, val): updates index at i with val. 8 | * get(i): gets the value at index i. -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p121_150/p134/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p121_150.p134; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | class Solution { 7 | 8 | private Map map; 9 | 10 | Solution() { 11 | map = new HashMap<>(); 12 | } 13 | 14 | void init(Integer[] array, int size) { 15 | for (int index = 0; index < size; index++) { 16 | if (array[index] != null) { 17 | map.put(index, array[index]); 18 | } 19 | } 20 | } 21 | 22 | void set(int index, Integer value) { 23 | if (value == null) { 24 | map.remove(index); 25 | } else { 26 | map.put(index, value); 27 | } 28 | } 29 | 30 | Integer get(int index) { 31 | return map.get(index); 32 | } 33 | 34 | } -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p121_150/p135/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | Given a binary tree, find a minimum path sum from root to a leaf. 3 | 4 | For example, the minimum path in this tree is [10, 5, 1, -1], which has sum 15. 5 | 6 | ``` 7 | 10 8 | / \ 9 | 5 5 10 | \ \ 11 | 2 1 12 | / 13 | -1 14 | 15 | ``` -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p121_150/p136/Readme.md: -------------------------------------------------------------------------------- 1 | Given an N by M matrix consisting only of 1's and 0's, find the largest 2 | rectangle containing only 1's and return its area. 3 | 4 | For example, given the following matrix: 5 | ``` 6 | {{1, 0, 0, 0}, 7 | {1, 0, 1, 1}, 8 | {1, 0, 1, 1}, 9 | {0, 1, 0, 0}} 10 | 11 | ``` 12 | 13 | Return 4. 14 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p121_150/p137/Readme.md: -------------------------------------------------------------------------------- 1 | Implement a bit array. 2 | 3 | A bit array is a space efficient array that holds a value of 1 or 0 at each 4 | index. 5 | 6 | * init(size): initialize the array with size 7 | * set(i, val): updates index at i with val where val is either 1 or 0. 8 | * get(i): gets the value at index i. 9 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p121_150/p141/Readme.md: -------------------------------------------------------------------------------- 1 | Implement 3 stacks using a single list: 2 | ``` 3 | class Stack: 4 | def __init__(self): 5 | self.list = [] 6 | 7 | def pop(self, stack_number): 8 | pass 9 | 10 | def push(self, item, stack_number): 11 | pass 12 | 13 | ``` -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p121_150/p142/Readme.md: -------------------------------------------------------------------------------- 1 | You're given a string consisting solely of (, ), and *. * can represent either a 2 | (, ), or an empty string. Determine whether the parentheses are balanced. 3 | 4 | For example, (()* and (*) are balanced. )*( is not balanced. 5 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p121_150/p143/Readme.md: -------------------------------------------------------------------------------- 1 | Given a pivot x, and a list lst, partition the list into three parts. 2 | 3 | * The first part contains all elements in lst that are less than x 4 | * The second part contains all elements in lst that are equal to x 5 | * The third part contains all elements in lst that are larger than x 6 | 7 | Ordering within a part can be arbitrary. 8 | 9 | For example, given x = 10 and lst = [9, 12, 3, 5, 14, 10, 10], one partition may 10 | be `[9, 3, 5, 10, 10, 12, 14]. -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p121_150/p145/Readme.md: -------------------------------------------------------------------------------- 1 | Given the head of a singly linked list, swap every two nodes and return its 2 | head. 3 | 4 | For example, given 1 -> 2 -> 3 -> 4, return 2 -> 1 -> 4 -> 3. 5 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p121_150/p145/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p121_150.p145; 2 | 3 | import in.ashwanik.dcp.common.ListNode; 4 | 5 | class Solution { 6 | 7 | ListNode reverse(ListNode head, int nodeCount) { 8 | if (head == null) { 9 | return null; 10 | } 11 | 12 | ListNode current = head; 13 | ListNode prev = null; 14 | ListNode next = null; 15 | int k = 0; 16 | while (current != null && k < nodeCount) { 17 | next = current.getNext(); 18 | current.setNext(prev); 19 | prev = current; 20 | current = next; 21 | k++; 22 | } 23 | if (next != null) { 24 | head.setNext(reverse(next, nodeCount)); 25 | } 26 | return prev; 27 | } 28 | 29 | } -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p121_150/p146/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | Given a binary tree where all nodes are either 0 or 1, prune the tree so that 3 | subtrees containing all 0s are removed. 4 | 5 | For example, given the following tree: 6 | 7 | ``` 8 | 0 9 | / \ 10 | 1 0 11 | / \ 12 | 1 0 13 | / \ 14 | 0 0 15 | ``` 16 | 17 | should be pruned to: 18 | ``` 19 | 0 20 | / \ 21 | 1 0 22 | / 23 | 1 24 | ``` 25 | 26 | We do not remove the tree at the root or its left child because it still has a 1 27 | as a descendant. 28 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p121_150/p146/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p121_150.p146; 2 | 3 | import in.ashwanik.dcp.common.TreeNode; 4 | 5 | class Solution { 6 | 7 | TreeNode pruneTree(TreeNode root) { 8 | if (root == null) { 9 | return null; 10 | } 11 | root.setLeft(pruneTree(root.getLeft())); 12 | root.setRight(pruneTree(root.getRight())); 13 | 14 | if (root.getData().equals(0) && root.getLeft() == null && root.getRight() == null) { 15 | return null; 16 | } 17 | return root; 18 | } 19 | } -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p121_150/p147/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | Given a list, sort it using this method: reverse(lst, i, j), which sorts lst 3 | from i to j`. 4 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p121_150/p148/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | Gray code [https://en.wikipedia.org/wiki/Gray_code] is a binary code where each 3 | successive value differ in only one bit, as well as when wrapping around. Gray 4 | code is common in hardware so that we don't see temporary spurious values during 5 | transitions. 6 | 7 | Given a number of bits n, generate a possible gray code for it. 8 | 9 | For example, for n = 2, one gray code would be [00, 01, 11, 10]. -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p121_150/p148/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p121_150.p148; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | class Solution { 7 | 8 | List grayCode(int numberOfBits) { 9 | if (numberOfBits == 0) { 10 | List result = new ArrayList<>(); 11 | result.add(0); 12 | return result; 13 | } 14 | 15 | List result = grayCode(numberOfBits - 1); 16 | int numberToAdd = 1 << (numberOfBits - 1); 17 | for (int index = result.size() - 1; index >= 0; index--) { 18 | result.add(result.get(index) + numberToAdd); 19 | } 20 | return result; 21 | } 22 | } -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p121_150/p149/Readme.md: -------------------------------------------------------------------------------- 1 | Given a list of numbers L, implement a method sum(i, j) which returns the sum 2 | from the sublist L[i:j] (including i, excluding j). 3 | 4 | For example, given L = [1, 2, 3, 4, 5], sum(1, 3) should return sum([2, 3]), 5 | which is 5. 6 | 7 | You can assume that you can do some pre-processing. sum() should be optimized 8 | over the pre-processing step. 9 | 10 | ### More https://stackoverflow.com/questions/17466218/what-are-the-differences-between-segment-trees-interval-trees-binary-indexed-t -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p121_150/p149/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p121_150.p149; 2 | 3 | class Solution { 4 | 5 | int sum(int[] array, int start, int end) { 6 | return 0; 7 | } 8 | } -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p121_150/p150/Readme.md: -------------------------------------------------------------------------------- 1 | Given a list of points, a central point, and an integer k, find the nearest k 2 | points from the central point. 3 | 4 | For example, given the list of points [(0, 0), (5, 4), (3, 1)], the central 5 | point (1, 2), and k = 2, return[(0, 0), (3, 1)]. 6 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p151_180/p151/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | Given a 2-D matrix representing an image, a location of a pixel in the screen 3 | and a color C, replace the color of the given pixel and all adjacent same 4 | colored pixels with C. 5 | 6 | For example, given the following matrix, and location pixel of (2, 2), and 'G' 7 | for green: 8 | 9 | ``` 10 | B B W 11 | W W W 12 | W W W 13 | B B B 14 | ``` 15 | 16 | Becomes 17 | 18 | ``` 19 | B B G 20 | G G G 21 | G G G 22 | B B B 23 | 24 | ``` -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p151_180/p153/Readme.md: -------------------------------------------------------------------------------- 1 | Find an efficient algorithm to find the smallest distance (measured in number of 2 | words) between any two given words in a string. 3 | 4 | For example, given words "hello", and "world" and a text content of "dog cat 5 | hello cat dog dog hello cat world", return 1 because there's only one word "cat" 6 | in between the two words. 7 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p151_180/p154/Readme.md: -------------------------------------------------------------------------------- 1 | Implement a stack API using only a heap. A stack implements the following 2 | methods: 3 | 4 | * push(item), which adds an element to the stack 5 | * pop(), which removes and returns the most recently added element (or throws 6 | an error if there is nothing on the stack) 7 | 8 | Recall that a heap has the following operations: 9 | 10 | * push(item), which adds a new key to the heap 11 | * pop(), which removes and returns the max value of the heap -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p151_180/p155/Readme.md: -------------------------------------------------------------------------------- 1 | Given a list of elements, find the majority element, which appears more than 2 | half the time (> floor(len(lst) / 2.0)). 3 | 4 | You can assume that such element exists. 5 | 6 | For example, given [1, 2, 1, 1, 3, 4, 0], return 1. 7 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p151_180/p155/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p151_180.p155; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | class Solution { 7 | 8 | 9 | int getMajorityElement(int[] array) { 10 | 11 | Map map = new HashMap<>(); 12 | 13 | for (int i : array) { 14 | map.putIfAbsent(i, 1); 15 | map.put(i, map.get(i) + 1); 16 | } 17 | for (Map.Entry entry : map.entrySet()) { 18 | if (entry.getValue() >= array.length / 2) { 19 | return entry.getKey(); 20 | } 21 | } 22 | return -1; 23 | } 24 | } -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p151_180/p156/Readme.md: -------------------------------------------------------------------------------- 1 | Given a positive integer n, find the smallest number of squared integers which 2 | sum to n. 3 | 4 | For example, given n = 13, return 2 since 13 = 32 + 22 = 9 + 4. 5 | 6 | Given n = 27, return 3 since 27 = 32 + 32 + 32 = 9 + 9 + 9. 7 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p151_180/p157/Readme.md: -------------------------------------------------------------------------------- 1 | Given a string, determine whether any permutation of it is a palindrome. 2 | 3 | For example, carrace should return true, since it can be rearranged to form 4 | racecar, which is a palindrome. daily should return false, since there's no 5 | rearrangement that can form a palindrome. 6 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p151_180/p157/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p151_180.p157; 2 | 3 | class Solution { 4 | 5 | boolean isPalindrome(String str) { 6 | if (str == null || str.length() == 0) { 7 | return true; 8 | } 9 | int[] array = new int[256]; 10 | for (char c : str.toCharArray()) { 11 | array[c] = array[c] + 1; 12 | } 13 | boolean isOddCountFound = false; 14 | for (int i : array) { 15 | if ((i & 1) == 1) { 16 | if (isOddCountFound) { 17 | return false; 18 | } else { 19 | isOddCountFound = true; 20 | } 21 | } 22 | } 23 | return true; 24 | } 25 | } -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p151_180/p159/Readme.md: -------------------------------------------------------------------------------- 1 | Given a string, return the first recurring character in it, or null if there is 2 | no recurring character. 3 | 4 | For example, given the string "acbbac", return "b". Given the string "abcdef", 5 | return null. 6 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p151_180/p159/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p151_180.p159; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | class Solution { 7 | 8 | Character findRecurring(String str) { 9 | if (str == null || str.length() == 0) { 10 | return null; 11 | } 12 | Set set = new HashSet<>(); 13 | for (char c : str.toCharArray()) { 14 | if (set.contains(c)) { 15 | return c; 16 | } 17 | set.add(c); 18 | } 19 | return null; 20 | } 21 | } -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p151_180/p163/Readme.md: -------------------------------------------------------------------------------- 1 | Given an arithmetic expression in Reverse Polish Notation 2 | [https://en.wikipedia.org/wiki/Reverse_Polish_notation], write a program to 3 | evaluate it. 4 | 5 | The expression is given as a list of numbers and operands. For example: [5, 3, 6 | '+'] should return 5 + 3 = 8. 7 | 8 | For example, [15, 7, 1, 1, '+', '-', '/', 3, '*', 2, 1, 1, '+', '+', '-'] should 9 | return 5, since it is equivalent to ((15 / (7 - (1 + 1))) * 3) - (2 + (1 + 1)) = 10 | 5. 11 | 12 | You can assume the given expression is always valid. -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p151_180/p164/Readme.md: -------------------------------------------------------------------------------- 1 | You are given an array of length n + 1 whose elements belong to the set {1, 2, 2 | ..., n}. By the pigeonhole principle, there must be a duplicate. Find it in 3 | linear time and space. 4 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p151_180/p164/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p151_180.p164; 2 | 3 | class Solution { 4 | 5 | int getDuplicateNumber(int[] array, int n) { 6 | int xor = 0; 7 | 8 | for (int number : array) { 9 | xor = xor ^ number; 10 | } 11 | 12 | for (int index = 1; index <= n; index++) { 13 | xor = xor ^ index; 14 | } 15 | return xor; 16 | } 17 | } -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p151_180/p166/Readme.md: -------------------------------------------------------------------------------- 1 | Implement a 2D iterator class. It will be initialized with an array of arrays, 2 | and should implement the following methods: 3 | 4 | * next(): returns the next element in the array of arrays. If there are no more 5 | elements, raise an exception. 6 | * has_next(): returns whether or not the iterator still has elements left. 7 | 8 | For example, given the input [[1, 2], [3], [], [4, 5, 6]], calling next() 9 | repeatedly should output 1, 2, 3, 4, 5, 6. 10 | 11 | Do not use flatten or otherwise clone the arrays. Some of the arrays can be 12 | empty. -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p151_180/p168/Readme.md: -------------------------------------------------------------------------------- 1 | Given an N by N matrix, rotate it by 90 degrees clockwise. 2 | 3 | For example, given the following matrix: 4 | ``` 5 | [[1, 2, 3], 6 | [4, 5, 6], 7 | [7, 8, 9]] 8 | ``` 9 | 10 | you should return: 11 | ``` 12 | [[7, 4, 1], 13 | [8, 5, 2], 14 | [9, 6, 3]] 15 | ``` 16 | 17 | Follow-up: What if you couldn't use any extra space? 18 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p151_180/p168/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p151_180.p168; 2 | 3 | class Solution { 4 | 5 | ///https://code.likeagirl.io/rotate-an-2d-matrix-90-degree-clockwise-without-create-another-array-49209ea8b6e6 6 | void rotate(int[][] array) { 7 | if (array == null || array.length == 0) { 8 | return; 9 | } 10 | 11 | int x = (int) Math.floor(array.length / 2); 12 | int y = array[0].length - 1; 13 | 14 | 15 | for (int i = 0; i < x; i++) { 16 | for (int j = i; j < y - i; j++) { 17 | int temp = array[i][j]; 18 | array[i][j] = array[y - j][i]; 19 | array[y - j][i] = array[y - i][y - j]; 20 | array[y - i][y - j] = array[j][y - i]; 21 | array[j][y - i] = temp; 22 | } 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p151_180/p169/Readme.md: -------------------------------------------------------------------------------- 1 | Given a linked list, sort it in O(n log n) time and constant space. 2 | 3 | For example, the linked list 4 -> 1 -> -3 -> 99 should become -3 -> 1 -> 4 -> 99 4 | . 5 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p151_180/p170/Readme.md: -------------------------------------------------------------------------------- 1 | Given a start word, an end word, and a dictionary of valid words, find the 2 | shortest transformation sequence from start to end such that only one letter is 3 | changed at each step of the sequence, and each transformed word exists in the 4 | dictionary. If there is no possible transformation, return null. Each word in 5 | the dictionary have the same length as start and end and is lowercase. 6 | 7 | For example, given start = "dog", end = "cat", and dictionary = {"dot", "dop", 8 | "dat", "cat"}, return ["dog", "dot", "dat", "cat"]. 9 | 10 | Given start = "dog", end = "cat", and dictionary = {"dot", "tod", "dat", "dar"}, 11 | return null as there is no possible transformation from dog to cat. 12 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p151_180/p170/variation/Readme.md: -------------------------------------------------------------------------------- 1 | Given a source word, target word and an English dictionary, transform the source word to target by changing/adding/removing 1 character at a time, while all intermediate words being valid English words. Return the transformation chain which has the smallest number of intermediate words. -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p151_180/p171/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | You are given a list of data entries that represent entries and exits of groups 3 | of people into a building. An entry looks like this: 4 | 5 | {"timestamp": 1526579928, count: 3, "type": "enter"} 6 | 7 | This means 3 people entered the building. An exit looks like this: 8 | 9 | {"timestamp": 1526580382, count: 2, "type": "exit"} 10 | 11 | This means that 2 people exited the building. timestamp is in Unix time 12 | [https://en.wikipedia.org/wiki/Unix_time]. 13 | 14 | Find the busiest period in the building, that is, the time with the most people 15 | in the building. Return it as a pair of (start, end) timestamps. You can assume 16 | the building always starts off and ends up empty, i.e. with 0 people inside. -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p151_180/p172/Readme.md: -------------------------------------------------------------------------------- 1 | Given a string s and a list of words words, where each word is the same length, 2 | find all starting indices of substrings in s that is a concatenation of every 3 | word in words exactly once. 4 | 5 | For example, given s = "dogcatcatcodecatdog" and words = ["cat", "dog"], return 6 | [0, 13], since "dogcat" starts at index 0 and "catdog" starts at index 13. 7 | 8 | Given s = "barfoobazbitbyte" and words = ["dog", "cat"], return [] since there 9 | are no substrings composed of "dog" and "cat" in s. 10 | 11 | The order of the indices does not matter. 12 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p151_180/p173/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | Write a function to flatten a nested dictionary. Namespace the keys with a 3 | period. 4 | 5 | For example, given the following dictionary: 6 | ``` 7 | { 8 | "key": 3, 9 | "foo": { 10 | "a": 5, 11 | "bar": { 12 | "baz": 8 13 | } 14 | } 15 | } 16 | ``` 17 | 18 | it should become: 19 | ``` 20 | { 21 | "key": 3, 22 | "foo.a": 5, 23 | "foo.bar.baz": 8 24 | } 25 | ``` 26 | 27 | You can assume keys do not contain dots in them, i.e. no clobbering will occur. 28 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p151_180/p175/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | You are given a starting state start, a list of transition probabilities for a 3 | Markov chain, and a number of steps num_steps. Run the Markov chain starting 4 | from start for num_steps and compute the number of times we visited each state. 5 | 6 | For example, given the starting state a, number of steps 5000, and the following 7 | transition probabilities: 8 | ``` 9 | [ 10 | ('a', 'a', 0.9), 11 | ('a', 'b', 0.075), 12 | ('a', 'c', 0.025), 13 | ('b', 'a', 0.15), 14 | ('b', 'b', 0.8), 15 | ('b', 'c', 0.05), 16 | ('c', 'a', 0.25), 17 | ('c', 'b', 0.25), 18 | ('c', 'c', 0.5) 19 | ] 20 | ``` 21 | 22 | One instance of running this Markov chain might produce ```{ 'a': 3012, 'b': 1656, 'c': 332 }```. 23 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p151_180/p176/Readme.md: -------------------------------------------------------------------------------- 1 | Determine whether there exists a one-to-one character mapping from one string s1 2 | to another s2. 3 | 4 | For example, given s1 = abc and s2 = bcd, return true since we can map a to b, b 5 | to c, and c to d. 6 | 7 | Given s1 = foo and s2 = bar, return false since the o cannot map to two 8 | characters. 9 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p151_180/p177/Readme.md: -------------------------------------------------------------------------------- 1 | Given a linked list and a positive integer k, rotate the list to the right by k 2 | places. 3 | 4 | For example, given the linked list 7 -> 7 -> 3 -> 5 and k = 2, it should become 5 | 3 -> 5 -> 7 -> 7. 6 | 7 | Given the linked list 1 -> 2 -> 3 -> 4 -> 5 and k = 3, it should become 3 -> 4 8 | -> 5 -> 1 -> 2. 9 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p151_180/p178/Readme.md: -------------------------------------------------------------------------------- 1 | Alice wants to join her school's Probability Student Club. Membership dues are 2 | computed via one of two simple probabilistic games. 3 | 4 | The first game: roll a die repeatedly. Stop rolling once you get a five followed 5 | by a six. Your number of rolls is the amount you pay, in dollars. 6 | 7 | The second game: same, except that the stopping condition is a five followed by 8 | a five. 9 | 10 | Which of the two games should Alice elect to play? Does it even matter? Write a 11 | program to simulate the two games and calculate their expected value. 12 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p151_180/p178/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p151_180.p178; 2 | 3 | import java.util.Random; 4 | 5 | class Solution { 6 | private Random random; 7 | 8 | Solution() { 9 | random = new Random(); 10 | } 11 | 12 | private int dice() { 13 | return random.nextInt(6) + 1; 14 | } 15 | 16 | int numberOfDiceToWin(int a, int b) { 17 | int diceThrown = 1; 18 | int current = dice(); 19 | while (true) { 20 | int next = dice(); 21 | diceThrown++; 22 | if (current == a && next == b) { 23 | return diceThrown; 24 | } 25 | current = next; 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p151_180/p179/Readme.md: -------------------------------------------------------------------------------- 1 | Given the sequence of keys visited by a postorder traversal of a binary search 2 | tree, reconstruct the tree. 3 | 4 | For example, given the sequence 2, 4, 3, 8, 7, 5, you should construct the 5 | following tree: 6 | ``` 7 | 5 8 | / \ 9 | 3 7 10 | / \ \ 11 | 2 4 8 12 | ``` -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p151_180/p180/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | Given a stack of N elements, interleave the first half of the stack with the 3 | second half reversed using only one other queue. This should be done in-place. 4 | 5 | Recall that you can only push or pop from a stack, and enqueue or dequeue from a 6 | queue. 7 | 8 | For example, if the stack is [1, 2, 3, 4, 5], it should become [1, 5, 2, 4, 3]. 9 | If the stack is [1, 2, 3, 4], it should become [1, 4, 2, 3]. 10 | 11 | Hint: Try working backwards from the end state. 12 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p181_210/p182/Readme.md: -------------------------------------------------------------------------------- 1 | A graph is minimally-connected if it is connected and there is no edge that can 2 | be removed while still leaving the graph connected. For example, any binary tree 3 | is minimally-connected. 4 | 5 | Given an undirected graph, check if the graph is minimally-connected. You can 6 | choose to represent the graph as either an adjacency matrix or adjacency list. 7 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p181_210/p184/Readme.md: -------------------------------------------------------------------------------- 1 | Given n numbers, find the greatest common denominator between them. 2 | 3 | For example, given the numbers [42, 56, 14], return 14. 4 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p181_210/p184/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p181_210.p184; 2 | 3 | class Solution { 4 | 5 | 6 | int gcd(int[] array) { 7 | if (array == null || array.length == 0) { 8 | return 1; 9 | } 10 | 11 | int result = array[0]; 12 | 13 | for (int index = 1; index < array.length; index++) { 14 | result = gcd(array[index], result); 15 | } 16 | return result; 17 | } 18 | 19 | private int gcd(int a, int b) { 20 | if (a == 0) { 21 | return b; 22 | } 23 | return gcd(b % a, a); 24 | } 25 | } -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p181_210/p189/Readme.md: -------------------------------------------------------------------------------- 1 | Given an array of elements, return the length of the longest subarray where all 2 | its elements are distinct. 3 | 4 | For example, given the array [5, 1, 3, 5, 2, 3, 4, 1], return 5 as the longest 5 | subarray of distinct elements is [5, 2, 3, 4, 1]. 6 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p181_210/p190/Readme.md: -------------------------------------------------------------------------------- 1 | Given a circular array, compute its maximum subarray sum in O(n) time. 2 | 3 | For example, given [8, -1, 3, 4], return 15 as we choose the numbers 3, 4, and 8 4 | where the 8 is obtained from wrapping around. 5 | 6 | Given [-4, 5, 1, 0], return 6 as we choose the numbers 5 and 1. 7 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p181_210/p191/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | Given a collection of intervals, find the minimum number of intervals you need 3 | to remove to make the rest of the intervals non-overlapping. 4 | 5 | Intervals can "touch", such as [0, 1] and [1, 2], but they won't be considered 6 | overlapping. 7 | 8 | For example, given the intervals (7, 9), (2, 4), (5, 8), return 1 as the last 9 | interval can be removed and the first two won't overlap. 10 | 11 | The intervals are not necessarily sorted in any order. 12 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p181_210/p191/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p181_210.p191; 2 | 3 | import java.util.Arrays; 4 | import java.util.Comparator; 5 | 6 | class Solution { 7 | int minimumNumberOfIntervalsToRemove(int[][] intervals) { 8 | if (intervals == null || intervals.length <= 1) { 9 | return -1; 10 | } 11 | 12 | Arrays.sort(intervals, Comparator.comparingInt(o -> o[1])); 13 | 14 | int[] current = intervals[0]; 15 | int nonOverlapping = 1; 16 | for (int index = 1; index < intervals.length; index++) { 17 | if (intervals[index][0] >= current[1]) { 18 | nonOverlapping++; 19 | current = intervals[index]; 20 | } 21 | } 22 | return intervals.length - nonOverlapping; 23 | } 24 | } -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p181_210/p195/Readme.md: -------------------------------------------------------------------------------- 1 | Let A be an N by M matrix in which every row and every column is sorted. 2 | 3 | Given i1, j1, i2, and j2, compute the number of elements of M smaller than M{i1, 4 | j1} and larger than M{i2, j2}. 5 | 6 | For example, given the following matrix: 7 | 8 | {{1, 3, 7, 10, 15, 20}, 9 | {2, 6, 9, 14, 22, 25}, 10 | {3, 8, 10, 15, 25, 30}, 11 | {10, 11, 12, 23, 30, 35}, 12 | {20, 25, 30, 35, 40, 45}} 13 | 14 | 15 | And i1 = 1, j1 = 1, i2 = 3, j2 = 3, return 15 as there are 15 numbers in the 16 | matrix smaller than 6 or greater than 23. -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p181_210/p196/Readme.md: -------------------------------------------------------------------------------- 1 | Given the root of a binary tree, find the most frequent subtree sum. The subtree 2 | sum of a node is the sum of all values under a node, including the node itself. 3 | 4 | For example, given the following tree: 5 | 6 | ``` 7 | 5 8 | / \ 9 | 2 -5 10 | ``` 11 | 12 | Return 2 as it occurs twice: once as the left leaf, and once as the sum of 2 + 5- 5. 13 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p181_210/p197/Readme.md: -------------------------------------------------------------------------------- 1 | Given an array and a number k that's smaller than the length of the array, 2 | rotate the array to the right k elements in-place. 3 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p181_210/p197/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p181_210.p197; 2 | 3 | class Solution { 4 | 5 | void rotateArrayToRight(int[] array, int k) { 6 | if (array == null || array.length == 0) { 7 | return; 8 | } 9 | reverse(array, 0, array.length - 1); 10 | reverse(array, 0, k - 1); 11 | reverse(array, k, array.length - 1); 12 | } 13 | 14 | private void reverse(int[] array, int start, int end) { 15 | int low = start; 16 | int high = end; 17 | while (low < high) { 18 | int temp = array[low]; 19 | array[low] = array[high]; 20 | array[high] = temp; 21 | low++; 22 | high--; 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p181_210/p198/Readme.md: -------------------------------------------------------------------------------- 1 | Given a set of distinct positive integers, find the largest subset such that 2 | every pair of elements in the subset (i, j) satisfies either i % j = 0 or j % i 3 | = 0. 4 | 5 | For example, given the set [3, 5, 10, 20, 21], you should return [5, 10, 20]. 6 | Given [1, 3, 6, 24], return [1, 3, 6, 24]. 7 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p181_210/p199/Readme.md: -------------------------------------------------------------------------------- 1 | Given a string of parentheses, find the balanced string that can be produced 2 | from it using the minimum number of insertions and deletions. If there are 3 | multiple solutions, return any of them. 4 | 5 | For example, given "(()", you could return "(())". Given "))()(", you could 6 | return "()()()()". 7 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p181_210/p201/Readme.md: -------------------------------------------------------------------------------- 1 | You are given an array of arrays of integers, where each array corresponds to a 2 | row in a triangle of numbers. For example, [[1], [2, 3], [1, 5, 1]] represents 3 | the triangle: 4 | ``` 5 | 1 6 | 2 3 7 | 1 5 1 8 | ``` 9 | 10 | We define a path in the triangle to start at the top and go down one row at a 11 | time to an adjacent value, eventually ending with an entry on the bottom row. 12 | For example, 1 -> 3 -> 5. The weight of the path is the sum of the entries. 13 | 14 | Write a program that returns the weight of the maximum weight path. -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p181_210/p201/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p181_210.p201; 2 | 3 | class Solution { 4 | 5 | 6 | /* 7 | 1 5 1 8 | 2 3 9 | 1 10 | */ 11 | ///https://www.mathblog.dk/project-euler-18/ 12 | int maxSum(int[][] array) { 13 | if (array == null) { 14 | return -1; 15 | } 16 | 17 | int maxArraySize = array[array.length - 1].length; 18 | 19 | int[] values = new int[maxArraySize]; 20 | 21 | System.arraycopy(array[maxArraySize - 1], 0, values, 0, maxArraySize); 22 | 23 | for (int i = maxArraySize - 2; i >= 0; i--) { 24 | for (int j = 0; j <= i; j++) { 25 | values[j] = array[i][j] + Math.max(values[j], values[j + 1]); 26 | } 27 | } 28 | return values[0]; 29 | } 30 | } -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p181_210/p202/Readme.md: -------------------------------------------------------------------------------- 1 | Write a program that checks whether an integer is a palindrome. For example, 121 2 | is a palindrome, as well as 888. 678 is not a palindrome. Do not convert the 3 | integer into a string. 4 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p181_210/p202/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p181_210.p202; 2 | 3 | class Solution { 4 | 5 | 6 | ///1234 7 | boolean isPalindrom(int number) { 8 | if (number == 0) { 9 | return true; 10 | } 11 | 12 | int reverse = 0; 13 | 14 | int n = number; 15 | int mod = 0; 16 | while (n > 0) { 17 | mod = n % 10; 18 | reverse = reverse * 10 + mod; 19 | n = n / 10; 20 | } 21 | return number == reverse; 22 | } 23 | } -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p181_210/p203/Readme.md: -------------------------------------------------------------------------------- 1 | Suppose an array sorted in ascending order is rotated at some pivot unknown to 2 | you beforehand. Find the minimum element in O(log N) time. You may assume the 3 | array does not contain duplicates. 4 | 5 | For example, given [5, 7, 10, 3, 4], return 3. 6 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p181_210/p203/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p181_210.p203; 2 | 3 | class Solution { 4 | 5 | //// 5 7 10 15 17 3 4 6 | 7 | /// 10 3 4 5 6 7 8 8 | 9 | int getMinimum(int[] array) { 10 | if (array == null || array.length == 0) { 11 | return -1; 12 | } 13 | 14 | int low = 0; 15 | int high = array.length - 1; 16 | int mid = 0; 17 | while (low <= high) { 18 | mid = low + (high - low) / 2; 19 | 20 | if (mid + 1 < array.length && array[mid] > array[mid + 1]) { 21 | return array[mid + 1]; 22 | } 23 | 24 | if (array[mid] > array[high]) { 25 | low = mid + 1; 26 | } else { 27 | high = mid - 1; 28 | } 29 | } 30 | return array[mid]; 31 | } 32 | } -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p181_210/p204/Readme.md: -------------------------------------------------------------------------------- 1 | Given a complete binary tree, count the number of nodes in faster than O(n) 2 | time. Recall that a complete binary tree has every level filled except the last, 3 | and the nodes in the last level are filled starting from the left. -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p181_210/p205/Readme.md: -------------------------------------------------------------------------------- 1 | Given an integer, find the next permutation of it in absolute order. For 2 | example, given 48975, the next permutation would be 49578. -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p181_210/p206/Readme.md: -------------------------------------------------------------------------------- 1 | A permutation can be specified by an array P, where P[i] represents the location 2 | of the element at i in the permutation. For example, [2, 1, 0] represents the 3 | permutation where elements at the index 0 and 2 are swapped. 4 | 5 | Given an array and a permutation, apply the permutation to the array. For 6 | example, given the array ["a", "b", "c"] and the permutation [2, 1, 0], return 7 | ["c", "b", "a"]. 8 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p181_210/p206/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p181_210.p206; 2 | 3 | class Solution { 4 | 5 | //a b c 6 | //2 1 0 7 | 8 | void permuteArray(char[] array, int[] indices) { 9 | if (array == null || indices == null) { 10 | return; 11 | } 12 | for (int index = 0; index < array.length; index++) { 13 | int temp = indices[index]; 14 | indices[index] = indices[temp]; 15 | indices[temp] = temp; 16 | 17 | int k = array[index] - 'a'; 18 | array[index] = array[temp]; 19 | array[temp] = array[k]; 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p181_210/p208/Readme.md: -------------------------------------------------------------------------------- 1 | Given a linked list of numbers and a pivot k, partition the linked list so that 2 | all nodes less than k come before nodes greater than or equal to k. 3 | 4 | For example, given the linked list 5 -> 1 -> 8 -> 0 -> 3 and k = 3, the solution 5 | could be 1 -> 0 -> 5 -> 8 -> 3. 6 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p181_210/p210/Readme.md: -------------------------------------------------------------------------------- 1 | A Collatz sequence in mathematics can be defined as follows. Starting with any 2 | positive integer: 3 | 4 | * if n is even, the next number in the sequence is n / 2 5 | * if n is odd, the next number in the sequence is 3n + 1 6 | 7 | It is conjectured that every such sequence eventually reaches the number 1. Test 8 | this conjecture. 9 | 10 | Bonus: What input n <= 1000000 gives the longest sequence? 11 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p181_210/p210/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p181_210.p210; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | class Solution { 7 | 8 | List getCollatzSequence(int number) { 9 | List sequence = new ArrayList<>(); 10 | if (number < 1) { 11 | return sequence; 12 | } 13 | sequence.add(number); 14 | if (number == 1) { 15 | return sequence; 16 | } 17 | 18 | int current = number; 19 | while (current != 1) { 20 | if (current % 2 == 0) { 21 | current = current / 2; 22 | } else { 23 | current = current * 3 + 1; 24 | } 25 | sequence.add(current); 26 | } 27 | return sequence; 28 | } 29 | } -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p1_30/p1/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | Given a list of numbers and a number k, return whether any two numbers from the 3 | list add up to k. 4 | 5 | For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17. 6 | 7 | Bonus: Can you do this in one pass? 8 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p1_30/p11/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | Implement an autocomplete system. That is, given a query string s and a set of 3 | all possible query strings, return all strings in the set that have s as a 4 | prefix. 5 | 6 | For example, given the query string de and the set of strings [dog, deer, deal], 7 | return [deer, deal]. 8 | 9 | Hint: Try preprocessing the dictionary into a more efficient data structure to 10 | speed up queries. 11 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p1_30/p12/Readme.md: -------------------------------------------------------------------------------- 1 | There exists a staircase with N steps, and you can climb up either 1 or 2 steps 2 | at a time. Given N, write a function that returns the number of unique ways you 3 | can climb the staircase. The order of the steps matters. 4 | 5 | For example, if N is 4, then there are 5 unique ways: 6 | 7 | * 1, 1, 1, 1 8 | * 2, 1, 1 9 | * 1, 2, 1 10 | * 1, 1, 2 11 | * 2, 2 12 | 13 | What if, instead of being able to climb 1 or 2 steps at a time, you could climb 14 | any number from a set of positive integers X? For example, if X = {1, 3, 5}, you 15 | could climb 1, 3, or 5 steps at a time. -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p1_30/p12/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p1_30.p12; 2 | 3 | class Solution { 4 | 5 | int numberOfWaysToClimb(int numberOfStairs) { 6 | if (numberOfStairs == 0) { 7 | return 0; 8 | } 9 | if (numberOfStairs == 1) { 10 | return 1; 11 | } 12 | if (numberOfStairs == 2) { 13 | return 2; 14 | } 15 | int a = 1; 16 | int b = 2; 17 | int c = a + b; 18 | for (int index = 3; index <= numberOfStairs; index++) { 19 | c = a + b; 20 | a = b; 21 | b = c; 22 | } 23 | return c; 24 | } 25 | } 26 | 27 | 28 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p1_30/p13/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | Given an integer k and a string s, find the length of the longest substring that 4 | contains at most k distinct characters. 5 | 6 | For example, given s = "abcba" and k = 2, the longest substring with k distinct 7 | characters is "bcb". 8 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p1_30/p15/Readme.md: -------------------------------------------------------------------------------- 1 | Given a stream of elements too large to store in memory, pick a random element 2 | from the stream with uniform probability. -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p1_30/p15/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p1_30.p15; 2 | 3 | import java.util.Random; 4 | 5 | class Solution { 6 | 7 | private Random random; 8 | private int res = 0; 9 | private int count = 0; 10 | 11 | Solution() { 12 | random = new Random(); 13 | } 14 | 15 | int getRandom(int x) { 16 | count++; 17 | if (count == 1) { 18 | res = x; 19 | } else { 20 | int i = random.nextInt(count); 21 | if (i == count - 1) { 22 | res = x; 23 | } 24 | } 25 | return res; 26 | } 27 | } 28 | 29 | 30 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p1_30/p16/Readme.md: -------------------------------------------------------------------------------- 1 | You run an e-commerce website and want to record the last N order ids in a log. 2 | Implement a data structure to accomplish this, with the following API: 3 | 4 | * record(order_id): adds the order_id to the log 5 | * get_last(i): gets the ith last element from the log. i is guaranteed to be 6 | smaller than or equal to N. 7 | 8 | You should be as efficient with time and space as possible. 9 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p1_30/p16/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p1_30.p16; 2 | 3 | class Solution { 4 | 5 | private int max; 6 | private int current; 7 | private int[] array; 8 | 9 | Solution(int max) { 10 | this.max = max; 11 | this.array = new int[max]; 12 | } 13 | 14 | void record(int orderId) { 15 | array[current] = orderId; 16 | current = (current + 1) % max; 17 | } 18 | 19 | int getLast(int i) { 20 | return array[(current - i + max) % max]; 21 | } 22 | } 23 | 24 | 25 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p1_30/p18/Readme.md: -------------------------------------------------------------------------------- 1 | Given an array of integers and a number k, where 1 <= k <= length of the array, 2 | compute the maximum values of each subarray of length k. 3 | 4 | For example, given array = [10, 5, 2, 7, 8, 7] and k = 3, we should get: [10, 7, 5 | 8, 8], since: 6 | 7 | * 10 = max(10, 5, 2) 8 | * 7 = max(5, 2, 7) 9 | * 8 = max(2, 7, 8) 10 | * 8 = max(7, 8, 7) 11 | 12 | Do this in O(n) time and O(k) space. You can modify the input array in-place and 13 | you do not need to store the results. You can simply print them out as you 14 | compute them. 15 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p1_30/p19/Readme.md: -------------------------------------------------------------------------------- 1 | A builder is looking to build a row of N houses that can be of K different 2 | colors. He has a goal of minimizing cost while ensuring that no two neighboring 3 | houses are of the same color. 4 | 5 | Given an N by K matrix where the nth row and kth column represents the cost to 6 | build the nthhouse with kth color, return the minimum cost which achieves this 7 | goal. 8 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p1_30/p19/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p1_30.p19; 2 | 3 | class Solution { 4 | 5 | int getMinimumCost(int[][] array) { 6 | return 0; 7 | } 8 | } 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p1_30/p2/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | Given an array of integers, return a new array such that each element at index i 3 | of the new array is the product of all the numbers in the original array except 4 | the one at i. 5 | 6 | For example, if our input was [1, 2, 3, 4, 5], the expected output would be 7 | [120, 60, 40, 30, 24]. If our input was [3, 2, 1], the expected output would be 8 | [2, 3, 6]. 9 | 10 | Follow-up: what if you can't use division? 11 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p1_30/p2/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p1_30.p2; 2 | 3 | class Solution { 4 | int[] product(int[] array) { 5 | if (array == null || array.length == 0) { 6 | return array; 7 | } 8 | int length = array.length; 9 | 10 | int[] result = new int[length]; 11 | 12 | int temp = 1; 13 | 14 | for (int index = 0; index < length; index++) { 15 | result[index] = temp; 16 | temp *= array[index]; 17 | } 18 | 19 | temp = 1; 20 | for (int index = length - 1; index >= 0; index--) { 21 | result[index] *= temp; 22 | temp *= array[index]; 23 | } 24 | 25 | return result; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p1_30/p20/Readme.md: -------------------------------------------------------------------------------- 1 | Given two singly linked lists that intersect at some point, find the 2 | intersecting node. The lists are non-cyclical. 3 | 4 | For example, given A = 3 -> 7 -> 8 -> 10 and B = 99 -> 1 -> 8 -> 10, return the 5 | node with value 8. 6 | 7 | In this example, assume nodes with the same value are the exact same node 8 | objects. 9 | 10 | Do this in O(M + N) time (where M and N are the lengths of the lists) and 11 | constant space. -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p1_30/p21/Readme.md: -------------------------------------------------------------------------------- 1 | Given an array of time intervals (start, end) for classroom lectures (possibly 2 | overlapping), find the minimum number of rooms required. 3 | 4 | For example, given [(30, 75), (0, 50), (60, 150)], you should return 2. 5 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p1_30/p22/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | Given a dictionary of words and a string made up of those words (no spaces), 3 | return the original sentence in a list. If there is more than one possible 4 | reconstruction, return any of them. If there is no possible reconstruction, then 5 | return null. 6 | 7 | For example, given the set of words 'quick', 'brown', 'the', 'fox', and the 8 | string "thequickbrownfox", you should return ['the', 'quick', 'brown', 'fox']. 9 | 10 | Given the set of words 'bed', 'bath', 'bedbath', 'and', 'beyond', and the string 11 | "bedbathandbeyond", return either ['bed', 'bath', 'and', 'beyond] or ['bedbath', 12 | 'and', 'beyond']. -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p1_30/p25/Readme.md: -------------------------------------------------------------------------------- 1 | Implement regular expression matching with the following special characters: 2 | 3 | * . (period) which matches any single character 4 | * * (asterisk) which matches zero or more of the preceding element 5 | 6 | That is, implement a function that takes in a string and a valid regular 7 | expression and returns whether or not the string matches the regular expression. 8 | 9 | For example, given the regular expression "ra." and the string "ray", your 10 | function should return true. The same regular expression on the string "raymond" 11 | should return false. 12 | 13 | Given the regular expression ".*at" and the string "chat", your function should 14 | return true. The same regular expression on the string "chats" should return 15 | false. 16 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p1_30/p26/Readme.md: -------------------------------------------------------------------------------- 1 | Given a singly linked list and an integer k, remove the kth last element from 2 | the list. k is guaranteed to be smaller than the length of the list. 3 | 4 | The list is very long, so making more than one pass is prohibitively expensive. 5 | 6 | Do this in constant space and in one pass. 7 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p1_30/p26/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p1_30.p26; 2 | 3 | import in.ashwanik.dcp.common.ListNode; 4 | 5 | class Solution { 6 | 7 | ListNode getKthNodeFromLast(ListNode head, int k) { 8 | if (head == null) { 9 | return null; 10 | } 11 | 12 | ListNode first = head; 13 | 14 | int count = 0; 15 | while (count < k) { 16 | first = first.getNext(); 17 | count++; 18 | } 19 | 20 | ListNode second = head; 21 | 22 | while (first != null) { 23 | first = first.getNext(); 24 | second = second.getNext(); 25 | } 26 | 27 | return second; 28 | 29 | } 30 | } 31 | 32 | 33 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p1_30/p27/Readme.md: -------------------------------------------------------------------------------- 1 | Given a string of round, curly, and square open and closing brackets, return 2 | whether the brackets are balanced (well-formed). 3 | 4 | For example, given the string "([])[]({})", you should return true. 5 | 6 | Given the string "([)]" or "((()", you should return false. -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p1_30/p29/Readme.md: -------------------------------------------------------------------------------- 1 | Run-length encoding is a fast and simple method of encoding strings. The basic 2 | idea is to represent repeated successive characters as a single count and 3 | character. For example, the string "AAAABBBCCDAA" would be encoded as 4 | "4A3B2C1D2A". 5 | 6 | Implement run-length encoding and decoding. You can assume the string to be 7 | encoded have no digits and consists solely of alphabetic characters. You can 8 | assume the string to be decoded is valid. -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p1_30/p3/Readme.md: -------------------------------------------------------------------------------- 1 | Given the root to a binary tree, implement serialize(root), which serializes the 2 | tree into a string, and deserialize(s), which deserializes the string back into 3 | the tree. 4 | 5 | For example, given the following Node class 6 | 7 | ```python 8 | class Node: 9 | def __init__(self, val, left=None, right=None): 10 | self.val = val 11 | self.left = left 12 | self.right = right 13 | 14 | 15 | The following test should pass: 16 | 17 | node = Node('root', Node('left', Node('left.left')), Node('right')) 18 | assert deserialize(serialize(node)).left.left.val == 'left.left' 19 | 20 | ``` -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p1_30/p30/Readme.md: -------------------------------------------------------------------------------- 1 | You are given an array of non-negative integers that represents a 2 | two-dimensional elevation map where each element is unit-width wall and the 3 | integer is the height. Suppose it will rain and all spots between two walls get 4 | filled up. 5 | 6 | Compute how many units of water remain trapped on the map in O(N) time and O(1) 7 | space. 8 | 9 | For example, given the input [2, 1, 2], we can hold 1 unit of water in the 10 | middle. 11 | 12 | Given the input [3, 0, 1, 3, 0, 5], we can hold 3 units in the first index, 2 in 13 | the second, and 3 in the fourth index (we cannot hold 5 since it would run off 14 | to the left), so we can trap 8 units of water. 15 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p1_30/p4/Readme.md: -------------------------------------------------------------------------------- 1 | Given an array of integers, find the first missing positive integer in linear 2 | time and constant space. In other words, find the lowest positive integer that 3 | does not exist in the array. The array can contain duplicates and negative 4 | numbers as well. 5 | 6 | For example, the input [3, 4, -1, 1] should give 2. The input [1, 2, 0] should 7 | give 3. 8 | 9 | You can modify the input array in-place. 10 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p1_30/p7/Readme.md: -------------------------------------------------------------------------------- 1 | Given the mapping a = 1, b = 2, ... z = 26, and an encoded message, count the 2 | number of ways it can be decoded. 3 | 4 | For example, the message '111' would give 3, since it could be decoded as 'aaa', 5 | 'ka', and 'ak'. 6 | 7 | You can assume that the messages are decodable. For example, '001' is not 8 | allowed. 9 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p1_30/p8/Readme.md: -------------------------------------------------------------------------------- 1 | A unival tree (which stands for "universal value") is a tree where all nodes 2 | under it have the same value. 3 | 4 | Given the root to a binary tree, count the number of unival subtrees. 5 | 6 | For example, the following tree has 5 unival subtrees: 7 | 8 | 0 9 | / \ 10 | 1 0 11 | / \ 12 | 1 0 13 | / \ 14 | 1 1 15 | 16 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p1_30/p8/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p1_30.p8; 2 | 3 | import in.ashwanik.dcp.common.TreeNode; 4 | 5 | class Solution { 6 | 7 | public int univalSubtreeCount(TreeNode root) { 8 | if (root == null) { 9 | return 0; 10 | } 11 | 12 | int count = univalSubtreeCount(root.getLeft()) + univalSubtreeCount(root.getRight()); 13 | 14 | if (root.getLeft() != null && !root.getLeft().getData().equals(root.getData())) { 15 | return count; 16 | } 17 | 18 | if (root.getRight() != null && !root.getRight().getData().equals(root.getData())) { 19 | return count; 20 | } 21 | 22 | return count + 1; 23 | } 24 | 25 | } 26 | 27 | 28 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p1_30/p9/Readme.md: -------------------------------------------------------------------------------- 1 | Given a list of integers, write a function that returns the largest sum of 2 | non-adjacent numbers. Numbers can be 0 or negative. 3 | 4 | For example, [2, 4, 6, 2, 5] should return 13, since we pick 2, 6, and 5. [5, 1, 5 | 1, 5] should return 10, since we pick 5 and 5. 6 | 7 | Follow-up: Can you do this in O(N) time and constant space? 8 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p1_30/p9/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p1_30.p9; 2 | 3 | import static java.lang.Math.max; 4 | 5 | class Solution { 6 | 7 | int getLargestSum(int[] array) { 8 | if (array == null || array.length == 0) { 9 | return 0; 10 | } 11 | if (array.length == 1) { 12 | return array[0]; 13 | } 14 | int first = max(0, array[0]); 15 | int second = max(first, array[1]); 16 | int sum = second; 17 | for (int index = 2; index < array.length; index++) { 18 | sum = max(second, first + array[index]); 19 | first = second; 20 | second = sum; 21 | } 22 | return sum; 23 | } 24 | 25 | } 26 | 27 | 28 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p211_p240/p211/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | Given a string and a pattern, find the starting indices of all occurrences of 3 | the pattern in the string. For example, given the string "abracadabra" and the 4 | pattern "abr", you should return [0, 7]. 5 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p211_p240/p211/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p211_p240.p211; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | class Solution { 7 | 8 | List getIndices(String text, String pattern) { 9 | List list = new ArrayList<>(); 10 | if (text == null || text.isEmpty() || pattern == null) { 11 | return list; 12 | } 13 | 14 | int index = 0; 15 | while (index != -1) { 16 | index = text.indexOf(pattern, index); 17 | if (index != -1) { 18 | list.add(index); 19 | index += pattern.length(); 20 | } 21 | } 22 | return list; 23 | } 24 | } -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p211_p240/p212/Readme.md: -------------------------------------------------------------------------------- 1 | Spreadsheets often use this alphabetical encoding for its columns: "A", "B", 2 | "C", ..., "AA", "AB", ..., "ZZ", "AAA", "AAB", .... 3 | 4 | Given a column number, return its alphabetical column id. For example, given 1, 5 | return "A". Given 27, return "AA". -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p211_p240/p212/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p211_p240.p212; 2 | 3 | class Solution { 4 | 5 | String getExcelColumn(int number) { 6 | StringBuilder column = new StringBuilder(); 7 | int current = number; 8 | 9 | while (current > 0) { 10 | int rem = current % 26; 11 | if (rem == 0) { 12 | column.append("Z"); 13 | current = (current / 26) - 1; 14 | } else { 15 | column.append((char) ((rem - 1) + 'A')); 16 | current = current / 26; 17 | } 18 | } 19 | return column.reverse().toString(); 20 | } 21 | } -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p211_p240/p213/Readme.md: -------------------------------------------------------------------------------- 1 | Given a string of digits, generate all possible valid IP address combinations. 2 | 3 | IP addresses must follow the format A.B.C.D, where A, B, C, and D are numbers 4 | between 0 and 255. Zero-prefixed numbers, such as 01 and 065, are not allowed, 5 | except for 0 itself. 6 | 7 | For example, given "2542540123", you should return ['254.25.40.123', 8 | '254.254.0.123']. 9 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p211_p240/p214/Readme.md: -------------------------------------------------------------------------------- 1 | Given an integer n, return the length of the longest consecutive run of 1s in 2 | its binary representation. 3 | 4 | For example, given 156, you should return 3. 5 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p211_p240/p214/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p211_p240.p214; 2 | 3 | class Solution { 4 | 5 | int lengthOf1s(int number) { 6 | String binary = Integer.toBinaryString(number); 7 | int max = 0; 8 | 9 | int current = 0; 10 | for (char c : binary.toCharArray()) { 11 | if (c == '1') { 12 | current++; 13 | } else { 14 | max = Math.max(max, current); 15 | current = 0; 16 | } 17 | } 18 | return max; 19 | } 20 | } -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p211_p240/p216/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | Given a number in Roman numeral [https://en.wikipedia.org/wiki/Roman_numerals] 3 | format, convert it to decimal. 4 | 5 | The values of Roman numerals are as follows: 6 | ``` 7 | { 8 | 'M': 1000, 9 | 'D': 500, 10 | 'C': 100, 11 | 'L': 50, 12 | 'X': 10, 13 | 'V': 5, 14 | 'I': 1 15 | } 16 | ``` 17 | 18 | In addition, note that the Roman numeral system uses subtractive notation 19 | [https://en.wikipedia.org/wiki/Subtractive_notation] for numbers such as IV and 20 | XL. 21 | 22 | For the input XIV, for instance, you should return 14. 23 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p31_60/p31/Readme.md: -------------------------------------------------------------------------------- 1 | The edit distance between two strings refers to the minimum number of character 2 | insertions, deletions, and substitutions required to change one string to the 3 | other. For example, the edit distance between “kitten” and “sitting” is three: 4 | substitute the “k” for “s”, substitute the “e” for “i”, and append a “g”. 5 | 6 | Given two strings, compute the edit distance between them. 7 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p31_60/p33/Readme.md: -------------------------------------------------------------------------------- 1 | Compute the running median of a sequence of numbers. That is, given a stream of 2 | numbers, print out the median of the list so far on each new element. 3 | 4 | Recall that the median of an even-numbered list is the average of the two middle 5 | numbers. 6 | 7 | For example, given the sequence [2, 1, 5, 7, 2, 0, 5], your algorithm should 8 | print out: 9 | 10 | 2 11 | 1.5 12 | 2 13 | 3.5 14 | 2 15 | 2 16 | 2 -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p31_60/p34/Readme.md: -------------------------------------------------------------------------------- 1 | Given a string, find the palindrome that can be made by inserting the fewest 2 | number of characters as possible anywhere in the word. If there is more than one 3 | palindrome of minimum length that can be made, return the lexicographically 4 | earliest one (the first one alphabetically). 5 | 6 | For example, given the string "race", you should return "ecarace", since we can 7 | add three letters to it (which is the smallest amount to make a palindrome). 8 | There are seven other palindromes that can be made from "race" by adding three 9 | letters, but "ecarace" comes first alphabetically. 10 | 11 | As another example, given the string "google", you should return "elgoogle". -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p31_60/p34/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p31_60.p34; 2 | 3 | class Solution { 4 | 5 | String getPalindrom(String text) { 6 | return text; 7 | } 8 | 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p31_60/p35/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | Given an array of strictly the characters 'R', 'G', and 'B', segregate the 3 | values of the array so that all the Rs come first, the Gs come second, and the 4 | Bs come last. You can only swap elements of the array. 5 | 6 | Do this in linear time and in-place. 7 | 8 | For example, given the array ['G', 'B', 'R', 'R', 'B', 'R', 'G'], it should 9 | become ['R', 'R', 'R', 'G', 'G', 'B', 'B']. 10 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p31_60/p36/Readme.md: -------------------------------------------------------------------------------- 1 | Given the root to a binary search tree, find the second largest node in the 2 | tree. 3 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p31_60/p36/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p31_60.p36; 2 | 3 | import in.ashwanik.dcp.common.TreeNode; 4 | 5 | class Solution { 6 | private int current; 7 | 8 | TreeNode getKthLargest(TreeNode root, int k) { 9 | if (root == null) { 10 | return null; 11 | } 12 | TreeNode right = getKthLargest(root.getRight(), k); 13 | if (right != null && current == k) { 14 | return right; 15 | } 16 | current++; 17 | if (current == k) { 18 | return root; 19 | } 20 | 21 | return getKthLargest(root.getLeft(), k); 22 | } 23 | } 24 | 25 | 26 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p31_60/p37/Readme.md: -------------------------------------------------------------------------------- 1 | The power set of a set is the set of all its subsets. Write a function that, 2 | given a set, generates its power set. 3 | 4 | For example, given the set {1, 2, 3}, it should return {{}, {1}, {2}, {3}, {1, 5 | 2}, {1, 3}, {2, 3}, {1, 2, 3}}. 6 | 7 | You may also use a list or array to represent a set. -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p31_60/p38/Readme.md: -------------------------------------------------------------------------------- 1 | You have an N by N board. Write a function that, given N, returns the number of 2 | possible arrangements of the board where N queens can be placed on the board 3 | without threatening each other, i.e. no two queens share the same row, column, 4 | or diagonal. 5 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p31_60/p42/Readme.md: -------------------------------------------------------------------------------- 1 | Given a list of integers S and a target number k, write a function that returns 2 | a subset of S that adds up to k. If such a subset cannot be made, then return 3 | null. 4 | 5 | Integers can appear more than once in the list. You may assume all numbers in 6 | the list are positive. 7 | 8 | For example, given S = [12, 1, 61, 5, 9, 2] and k = 24, return [12, 9, 2, 1] 9 | since it sums up to 24. 10 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p31_60/p43/Readme.md: -------------------------------------------------------------------------------- 1 | Implement a stack that has the following methods: 2 | 3 | * push(val), which pushes an element onto the stack 4 | * pop(), which pops off and returns the topmost element of the stack. If there 5 | are no elements in the stack, then it should throw an error or return null. 6 | * max(), which returns the maximum value in the stack currently. If there are 7 | no elements in the stack, then it should throw an error or return null. 8 | 9 | Each method should run in constant time. -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p31_60/p44/Readme.md: -------------------------------------------------------------------------------- 1 | We can determine how "out of order" an array A is by counting the number of 2 | inversions it has. Two elements A[i] and A[j] form an inversion if A[i] > A[j] 3 | but i < j. That is, a smaller element appears after a larger element. 4 | 5 | Given an array, count the number of inversions it has. Do this faster than 6 | O(N^2) time. 7 | 8 | You may assume each element in the array is distinct. 9 | 10 | For example, a sorted list has zero inversions. The array [2, 4, 1, 3, 5] has 11 | three inversions: (2, 1), (4, 1), and (4, 3). The array [5, 4, 3, 2, 1] has ten 12 | inversions: every distinct pair forms an inversion. -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p31_60/p45/Readme.md: -------------------------------------------------------------------------------- 1 | Using a function rand5() that returns an integer from 1 to 5 (inclusive) with 2 | uniform probability, implement a function rand7() that returns an integer from 1 3 | to 7 (inclusive). 4 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p31_60/p45/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p31_60.p45; 2 | 3 | import java.util.Random; 4 | 5 | class Solution { 6 | 7 | 8 | int rand7() { 9 | while (true) { 10 | int number = 5 * rand5() + rand5() - 5; 11 | if (number < 22) { 12 | return (number % 7) + 1; 13 | } 14 | } 15 | } 16 | 17 | private int rand5() { 18 | return new Random().nextInt(5); 19 | } 20 | } 21 | 22 | 23 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p31_60/p46/Readme.md: -------------------------------------------------------------------------------- 1 | Given a string, find the longest palindromic contiguous substring. If there are 2 | more than one with the maximum length, return any one. 3 | 4 | For example, the longest palindromic substring of "aabcdcb" is "bcdcb". The 5 | longest palindromic substring of "bananas" is "anana". 6 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p31_60/p47/Readme.md: -------------------------------------------------------------------------------- 1 | Given a array of numbers representing the stock prices of a company in 2 | chronological order, write a function that calculates the maximum profit you 3 | could have made from buying and selling that stock once. You must buy before you 4 | can sell it. 5 | 6 | For example, given [9, 11, 8, 5, 7, 10], you should return 5, since you could 7 | buy the stock at 5 dollars and sell it at 10 dollars. 8 | 9 | 10 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p31_60/p47/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p31_60.p47; 2 | 3 | class Solution { 4 | int maxProfit(int[] array) { 5 | if (array == null || array.length == 0) { 6 | return -1; 7 | } 8 | 9 | int maxDiff = array[1] - array[0]; 10 | int minSoFar = array[0]; 11 | 12 | for (int index = 1; index < array.length; index++) { 13 | if (array[index] - minSoFar > maxDiff) { 14 | maxDiff = array[index] - minSoFar; 15 | } 16 | if (array[index] < minSoFar) { 17 | minSoFar = array[index]; 18 | } 19 | } 20 | return maxDiff; 21 | } 22 | } 23 | 24 | 25 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p31_60/p48/Readme.md: -------------------------------------------------------------------------------- 1 | Given pre-order and in-order traversals of a binary tree, write a function to 2 | reconstruct the tree. 3 | 4 | For example, given the following preorder traversal: 5 | 6 | [a, b, d, e, c, f, g] 7 | 8 | And the following inorder traversal: 9 | 10 | [d, b, e, a, f, c, g] 11 | 12 | You should return the following tree: 13 | 14 | ``` 15 | a 16 | / \ 17 | b c 18 | / \ / \ 19 | d e f g 20 | 21 | ``` 22 | 23 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p31_60/p49/Readme.md: -------------------------------------------------------------------------------- 1 | Given an array of numbers, find the maximum sum of any contiguous subarray of 2 | the array. 3 | 4 | For example, given the array [34, -50, 42, 14, -5, 86], the maximum sum would be 5 | 137, since we would take elements 42, 14, -5, and 86. 6 | 7 | Given the array [-5, -1, -8, -9], the maximum sum would be 0, since we would not 8 | take any elements. 9 | 10 | Do this in O(N) time. 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p31_60/p49/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p31_60.p49; 2 | 3 | class Solution { 4 | 5 | int maxSum(int[] array) { 6 | if (array == null || array.length == 0) { 7 | return 0; 8 | } 9 | 10 | int maxTillNow = array[0]; 11 | int currentMax = array[0]; 12 | 13 | for (int index = 1; index < array.length; index++) { 14 | currentMax = Math.max(array[index], currentMax + array[index]); 15 | maxTillNow = Math.max(currentMax, maxTillNow); 16 | } 17 | return maxTillNow > 0 ? maxTillNow : 0; 18 | } 19 | } 20 | 21 | 22 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p31_60/p50/Readme.md: -------------------------------------------------------------------------------- 1 | Suppose an arithmetic expression is given as a binary tree. Each leaf is an 2 | integer and each internal node is one of '+', '−', '∗', or '/'. 3 | 4 | Given the root to such a tree, write a function to evaluate it. 5 | 6 | For example, given the following tree: 7 | ``` 8 | * 9 | / \ 10 | + + 11 | / \ / \ 12 | 3 2 4 5 13 | ``` 14 | 15 | 32+45+* 16 | 17 | 18 | You should return 45, as it is (3 + 2) * (4 + 5). -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p31_60/p51/Readme.md: -------------------------------------------------------------------------------- 1 | Given a function that generates perfectly random numbers between 1 and k 2 | (inclusive), where k is an input, write a function that shuffles a deck of cards 3 | represented as an array using only swaps. 4 | 5 | It should run in O(N) time. -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p31_60/p51/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p31_60.p51; 2 | 3 | import java.util.Random; 4 | 5 | class Solution { 6 | 7 | private Random random; 8 | 9 | public Solution() { 10 | random = new Random(); 11 | } 12 | 13 | void shuffle(int[] array) { 14 | if (array == null || array.length == 0) { 15 | return; 16 | } 17 | 18 | for (int index = array.length - 1; index > 0; index--) { 19 | int current = random.nextInt(index + 1); 20 | int temp = array[index]; 21 | array[index] = array[current]; 22 | array[current] = temp; 23 | } 24 | } 25 | } 26 | 27 | 28 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p31_60/p52/Readme.md: -------------------------------------------------------------------------------- 1 | Implement an LRU (Least Recently Used) cache. It should be able to be 2 | initialized with a cache size n, and contain the following methods: 3 | 4 | * set(key, value): sets key to value. If there are already n items in the cache 5 | and we are adding a new item, then it should also remove the least recently 6 | used item. 7 | * get(key): gets the value at key. If no such key exists, return null. 8 | 9 | Each operation should run in O(1) time. 10 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p31_60/p53/Readme.md: -------------------------------------------------------------------------------- 1 | Implement a queue using two stacks. Recall that a queue is a FIFO (first-in, 2 | first-out) data structure with the following methods: enqueue, which inserts an 3 | element into the queue, and dequeue, which removes it. 4 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p31_60/p53/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p31_60.p53; 2 | 3 | import java.util.Stack; 4 | 5 | class Solution { 6 | private Stack first; 7 | private Stack second; 8 | 9 | public Solution() { 10 | first = new Stack<>(); 11 | second = new Stack<>(); 12 | } 13 | 14 | void enqueue(Integer value) { 15 | first.push(value); 16 | } 17 | 18 | Integer dequeue() { 19 | if (second.isEmpty()) { 20 | while (!first.isEmpty()) { 21 | second.push(first.pop()); 22 | } 23 | } 24 | if (second.isEmpty()) { 25 | return null; 26 | } 27 | return second.pop(); 28 | } 29 | } 30 | 31 | 32 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p31_60/p55/Readme.md: -------------------------------------------------------------------------------- 1 | Implement a URL shortener with the following methods: 2 | 3 | * shorten(url), which shortens the url into a six-character alphanumeric 4 | string, such as zLg6wl. 5 | * restore(short), which expands the shortened string into the original url. If 6 | no such shortened string exists, return null. 7 | 8 | Hint: What if we enter the same URL twice? -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p31_60/p57/Readme.md: -------------------------------------------------------------------------------- 1 | Given a string s and an integer k, break up the string into multiple texts such 2 | that each text has a length of k or less. You must break it up so that words 3 | don't break across lines. If there's no way to break the text up, then return 4 | null. 5 | 6 | You can assume that there are no spaces at the ends of the string and that there 7 | is exactly one space between each word. 8 | 9 | For example, given the string "the quick brown fox jumps over the lazy dog" and 10 | k = 10, you should return: ["the quick", "brown fox", "jumps over", "the lazy", 11 | "dog"]. No string in the list has a length of more than 10. 12 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p31_60/p58/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | An sorted array of integers was rotated an unknown number of times. 3 | 4 | Given such an array, find the index of the element in the array in faster than 5 | linear time. If the element doesn't exist in the array, return null. 6 | 7 | For example, given the array [13, 18, 25, 2, 8, 10] and the element 8, return 4 8 | (the index of 8 in the array). 9 | 10 | You can assume all the integers in the array are unique. 11 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p31_60/p60/Readme.md: -------------------------------------------------------------------------------- 1 | Given a multiset of integers, return whether it can be partitioned into two 2 | subsets whose sums are the same. 3 | 4 | For example, given the multiset {15, 5, 20, 10, 35, 15, 10}, it would return 5 | true, since we can split it up into {15, 5, 10, 15, 10} and {20, 35}, which both 6 | add up to 55. 7 | 8 | Given the multiset {15, 5, 20, 10, 35}, it would return false, since we can't 9 | split it up into two subsets that add up to the same sum. 10 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p61_90/p61/Readme.md: -------------------------------------------------------------------------------- 1 | Implement integer exponentiation. That is, implement the pow(x, y) function, 2 | where x and y are integers and returns x^y. 3 | 4 | Do this faster than the naive method of repeated multiplication. 5 | 6 | For example, pow(2, 10) should return 1024. 7 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p61_90/p61/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p61_90.p61; 2 | 3 | class Solution { 4 | 5 | long power(int x, int n) { 6 | 7 | if (n == 0) { 8 | return 1; 9 | } 10 | 11 | long temp = power(x, n / 2); 12 | if (n % 2 == 0) { 13 | return temp * temp; 14 | } else { 15 | return x * temp * temp; 16 | } 17 | } 18 | } 19 | 20 | 21 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p61_90/p62/Readme.md: -------------------------------------------------------------------------------- 1 | There is an N by M matrix of zeroes. Given N and M, write a function to count 2 | the number of ways of starting at the top-left corner and getting to the 3 | bottom-right corner. You can only move right or down. 4 | 5 | For example, given a 2 by 2 matrix, you should return 2, since there are two 6 | ways to get to the bottom-right: 7 | 8 | * Right, then down 9 | * Down, then right 10 | 11 | Given a 5 by 5 matrix, there are 70 ways to get to the bottom-right. -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p61_90/p62/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p61_90.p62; 2 | 3 | class Solution { 4 | 5 | long power(int x, int n) { 6 | return 0; 7 | } 8 | } 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p61_90/p65/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | Given a N by M matrix of numbers, print out the matrix in a clockwise spiral. 3 | 4 | For example, given the following matrix: 5 | 6 | [[1, 2, 3, 4, 5], 7 | [6, 7, 8, 9, 10], 8 | [11, 12, 13, 14, 15], 9 | [16, 17, 18, 19, 20]] 10 | 11 | 12 | You should print out the following: 13 | 14 | 1 15 | 2 16 | 3 17 | 4 18 | 5 19 | 10 20 | 15 21 | 20 22 | 19 23 | 18 24 | 17 25 | 16 26 | 11 27 | 6 28 | 7 29 | 8 30 | 9 31 | 14 32 | 13 33 | 12 34 | 35 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p61_90/p66/Readme.md: -------------------------------------------------------------------------------- 1 | Assume you have access to a function toss_biased() which returns 0 or 1 with a 2 | probability that's not 50-50 (but also not 0-100 or 100-0). You do not know the 3 | bias of the coin. 4 | 5 | Write a function to simulate an unbiased coin toss. 6 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p61_90/p66/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p61_90.p66; 2 | 3 | import java.util.Random; 4 | 5 | class Solution { 6 | 7 | private Random random = new Random(); 8 | 9 | private boolean tossBiased() { 10 | float leftLimit = 0F; 11 | float rightLimit = 1F; 12 | float value = leftLimit + random.nextFloat() * (rightLimit - leftLimit); 13 | return value < .6F; 14 | } 15 | 16 | boolean fairCoin() { 17 | boolean a; 18 | boolean b; 19 | do { 20 | a = tossBiased(); 21 | b = tossBiased(); 22 | } while (a == b); 23 | return a; 24 | } 25 | } 26 | 27 | 28 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p61_90/p67/Readme.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Google. 2 | 3 | Implement an LFU (Least Frequently Used) cache. It should be able to be 4 | initialized with a cache size n, and contain the following methods: 5 | 6 | * set(key, value): sets key to value. If there are already n items in the cache 7 | and we are adding a new item, then it should also remove the least frequently 8 | used item. If there is a tie, then the least recently used key should be 9 | removed. 10 | * get(key): gets the value at key. If no such key exists, return null. 11 | 12 | Each operation should run in O(1) time. 13 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p61_90/p69/Readme.md: -------------------------------------------------------------------------------- 1 | Given a list of integers, return the largest product that can be made by 2 | multiplying any three integers. 3 | 4 | For example, if the list is [-10, -10, 5, 2], we should return 500, since that's 5 | -10 * -10 * 5. 6 | 7 | You can assume the list has at least three integers. 8 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p61_90/p70/Readme.md: -------------------------------------------------------------------------------- 1 | A number is considered perfect if its digits sum up to exactly 10. 2 | 3 | Given a positive integer n, return the n-th perfect number. 4 | 5 | For example, given 1, you should return 19. Given 2, you should return 28. 6 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p61_90/p70/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p61_90.p70; 2 | 3 | class Solution { 4 | 5 | int perfectNumber(int n) { 6 | int count = 0; 7 | for (int index = 19; ; index += 9) { 8 | 9 | int current = index; 10 | int sum = 0; 11 | while (current > 0) { 12 | sum += current % 10; 13 | current = current / 10; 14 | } 15 | 16 | if (sum == 10) { 17 | count++; 18 | } 19 | if (count == n) { 20 | return index; 21 | 22 | } 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p61_90/p73/Readme.md: -------------------------------------------------------------------------------- 1 | Given the head of a singly linked list, reverse it in-place. -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p61_90/p73/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p61_90.p73; 2 | 3 | import in.ashwanik.dcp.common.ListNode; 4 | 5 | class Solution { 6 | ListNode reverse(ListNode head) { 7 | if (head == null) { 8 | return null; 9 | } 10 | ListNode current = head; 11 | ListNode previous = null; 12 | ListNode next; 13 | 14 | while (current != null) { 15 | next = current.getNext(); 16 | current.setNext(previous); 17 | previous = current; 18 | current = next; 19 | } 20 | return previous; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p61_90/p74/Readme.md: -------------------------------------------------------------------------------- 1 | Suppose you have a multiplication table that is N by N. That is, a 2D array 2 | where the value at the i-th row and j-th column is (i + 1) * (j + 1) (if 3 | 0-indexed) or i * j (if 1-indexed). 4 | 5 | Given integers N and X, write a function that returns the number of times X 6 | appears as a value in an N by N multiplication table. 7 | 8 | For example, given N = 6 and X = 12, you should return 4, since the 9 | multiplication table looks like this: 10 | 11 | | 1 | 2 | 3 | 4 | 5 | 6 | 12 | 13 | | 2 | 4 | 6 | 8 | 10 | 12 | 14 | 15 | | 3 | 6 | 9 | 12 | 15 | 18 | 16 | 17 | | 4 | 8 | 12 | 16 | 20 | 24 | 18 | 19 | | 5 | 10 | 15 | 20 | 25 | 30 | 20 | 21 | | 6 | 12 | 18 | 24 | 30 | 36 | 22 | 23 | And there are 4 12's in the table. 24 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p61_90/p74/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p61_90.p74; 2 | 3 | class Solution { 4 | int findCount(int n, int x) { 5 | int count = 0; 6 | for (int index = 1; index <= n; index++) { 7 | if (index > x) { 8 | break; 9 | } 10 | if (x % index == 0 && x / index <= n) { 11 | count++; 12 | } 13 | } 14 | return count; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p61_90/p77/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | Given a list of possibly overlapping intervals, return a new list of intervals 3 | where all overlapping intervals have been merged. 4 | 5 | The input list is not necessarily ordered in any way. 6 | 7 | For example, given [(1, 3), (5, 8), (4, 10), (20, 25)], you should return [(1, 8 | 3), (4, 10), (20, 25)]. 9 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p61_90/p78/Readme.md: -------------------------------------------------------------------------------- 1 | Given k sorted singly linked lists, write a function to merge all the lists into 2 | one sorted singly linked list. 3 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p61_90/p79/Readme.md: -------------------------------------------------------------------------------- 1 | Given an array of integers, write a function to determine whether the array 2 | could become non-decreasing by modifying at most 1 element. 3 | 4 | For example, given the array [10, 5, 7], you should return true, since we can 5 | modify the 10 into a 1 to make the array non-decreasing. 6 | 7 | Given the array [10, 5, 1], you should return false, since we can't modify any 8 | one element to get a non-decreasing array. 9 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p61_90/p79/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p61_90.p79; 2 | 3 | class Solution { 4 | 5 | boolean canBeSorted(int[] array) { 6 | if (array == null || array.length == 0) { 7 | return false; 8 | } 9 | 10 | int p = -1; 11 | 12 | for (int index = 0; index < array.length - 1; index++) { 13 | if (array[index + 1] < array[index]) { 14 | if (p != -1) { 15 | return false; 16 | } 17 | p = index; 18 | } 19 | } 20 | return (p == -1) || (p == 0) || (p == array.length - 2) || (array[p - 1] <= array[p + 1]) || (array[p] <= array[p + 2]); 21 | } 22 | } -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p61_90/p80/Readme.md: -------------------------------------------------------------------------------- 1 | Given the root of a binary tree, return a deepest node. For example, in the 2 | following tree, return d. 3 | 4 | ``` 5 | a 6 | / \ 7 | b c 8 | / 9 | d 10 | 11 | ``` -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p61_90/p80/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p61_90.p80; 2 | 3 | import in.ashwanik.dcp.common.TreeNode; 4 | 5 | class Solution { 6 | 7 | private int deepestLevel = 0; 8 | private TreeNode deepestNode = null; 9 | 10 | TreeNode getDeepestNode(TreeNode root) { 11 | if (root == null) { 12 | return null; 13 | } 14 | 15 | getNode(root, 0); 16 | return deepestNode; 17 | } 18 | 19 | private void getNode(TreeNode root, int level) { 20 | if (root != null) { 21 | getNode(root.getLeft(), ++level); 22 | if (level > deepestLevel) { 23 | deepestNode = root; 24 | deepestLevel = level; 25 | } 26 | getNode(root.getRight(), level); 27 | } 28 | } 29 | } -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p61_90/p81/Readme.md: -------------------------------------------------------------------------------- 1 | Given a mapping of digits to letters (as in a phone number), and a digit string, 2 | return all possible letters the number could represent. You can assume each 3 | valid number in the mapping is a single digit. 4 | 5 | For example if {“2”: [“a”, “b”, “c”], 3: [“d”, “e”, “f”], …} then “23” should 6 | return [“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf"]. 7 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p61_90/p83/Readme.md: -------------------------------------------------------------------------------- 1 | Invert a binary tree. 2 | 3 | For example, given the following tree: 4 | 5 | ``` 6 | a 7 | / \ 8 | b c 9 | / \ / 10 | d e f 11 | 12 | ``` 13 | ``` 14 | should become: 15 | 16 | a 17 | / \ 18 | c b 19 | \ / \ 20 | f e d 21 | 22 | ``` -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p61_90/p83/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p61_90.p83; 2 | 3 | import in.ashwanik.dcp.common.TreeNode; 4 | 5 | class Solution { 6 | 7 | TreeNode invert(TreeNode root) { 8 | if (root == null) { 9 | return root; 10 | } 11 | 12 | TreeNode left = invert(root.getLeft()); 13 | TreeNode right = invert(root.getRight()); 14 | 15 | root.setRight(left); 16 | root.setLeft(right); 17 | 18 | 19 | return root; 20 | } 21 | } -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p61_90/p84/Readme.md: -------------------------------------------------------------------------------- 1 | Given a matrix of 1s and 0s, return the number of "islands" in the matrix. A 1 2 | represents land and 0 represents water, so an island is a group of 1s that are 3 | neighboring and their perimeter is surrounded by water. 4 | 5 | For example, this matrix has 4 islands. 6 | 7 | ``` 8 | 1 0 0 0 0 9 | 0 0 1 1 0 10 | 0 1 1 0 0 11 | 0 0 0 0 0 12 | 1 1 0 0 1 13 | 1 1 0 0 1 14 | ``` 15 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p61_90/p85/Readme.md: -------------------------------------------------------------------------------- 1 | Given three 32-bit integers x, y, and b, return x if b is 1 and y if b is 0, 2 | using only mathematical or bit operations. You can assume b can only be 1 or 0. 3 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p61_90/p85/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p61_90.p85; 2 | 3 | class Solution { 4 | 5 | int get(int x, int y, int b) { 6 | return x * b + y * Math.abs(1 - b); 7 | } 8 | } -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p61_90/p86/Readme.md: -------------------------------------------------------------------------------- 1 | Given a string of parentheses, write a function to compute the minimum number of 2 | parentheses to be removed to make the string valid (i.e. each open parenthesis 3 | is eventually closed). 4 | 5 | For example, given the string "()())()", you should return 1. Given the string 6 | ")(", you should return 2, since we must remove all of them. 7 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p61_90/p88/Readme.md: -------------------------------------------------------------------------------- 1 | Implement division of two positive integers without using the division, 2 | multiplication, or modulus operators. Return the quotient as an integer, 3 | ignoring the remainder. 4 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p61_90/p88/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p61_90.p88; 2 | 3 | class Solution { 4 | 5 | int divideWithoutDivision(int dividend, int divisor) { 6 | int sign; 7 | if (dividend < 0 && divisor < 0) { 8 | sign = 1; 9 | } else if (dividend < 0 || divisor < 0) { 10 | sign = -1; 11 | } else { 12 | sign = 1; 13 | } 14 | int absDividend = Math.abs(dividend); 15 | int absDivisor = Math.abs(divisor); 16 | int count = 0; 17 | while (absDividend >= absDivisor) { 18 | absDividend -= absDivisor; 19 | count++; 20 | } 21 | return count * sign; 22 | } 23 | } -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p61_90/p89/Readme.md: -------------------------------------------------------------------------------- 1 | Determine whether a tree is a valid binary search tree. 2 | 3 | A binary search tree is a tree with two children, left and right, and satisfies 4 | the constraint that the key in the left child must be less than or equal to the 5 | root and the key in the right child must be greater than or equal to the root. 6 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p61_90/p89/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p61_90.p89; 2 | 3 | import in.ashwanik.dcp.common.TreeNode; 4 | 5 | class Solution { 6 | 7 | private TreeNode prev = null; 8 | 9 | boolean isBST(TreeNode root) { 10 | if (root == null) { 11 | return true; 12 | } 13 | if (!isBST(root.getLeft())) { 14 | return false; 15 | } 16 | if (prev != null && prev.getData() >= root.getData()) { 17 | return false; 18 | } 19 | prev = root; 20 | return isBST(root.getRight()); 21 | } 22 | } -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p61_90/p90/Readme.md: -------------------------------------------------------------------------------- 1 | Given an integer n and a list of integers l, write a function that randomly 2 | generates a number from 0 to n-1 that isn't in l (uniform) -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p91_120/p100/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | You are in an infinite 2D grid where you can move in any of the 8 directions: 3 | 4 | (x,y) to 5 | (x+1, y), 6 | (x - 1, y), 7 | (x, y+1), 8 | (x, y-1), 9 | (x-1, y-1), 10 | (x+1,y+1), 11 | (x-1,y+1), 12 | (x+1,y-1) 13 | 14 | 15 | You are given a sequence of points and the order in which you need to cover the 16 | points. Give the minimum number of steps in which you can achieve it. You start 17 | from the first point. 18 | 19 | Example: 20 | 21 | Input: [(0, 0), (1, 1), (1, 2)] 22 | Output: 2 23 | 24 | 25 | It takes 1 step to move from (0, 0) to (1, 1). It takes one more step to move 26 | from (1, 1) to (1, 2). -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p91_120/p100/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p91_120.p100; 2 | 3 | class Solution { 4 | int getMinimumDistance(int[][] points) { 5 | if (points == null || points.length < 2) { 6 | return 0; 7 | } 8 | 9 | int distance = 0; 10 | 11 | for (int index = 0; index < points.length - 1; index++) { 12 | distance += distance(points[index], points[index + 1]); 13 | } 14 | 15 | return distance; 16 | } 17 | 18 | private int distance(int[] point1, int[] point2) { 19 | int x = Math.abs(point1[0] - point2[0]); 20 | int y = Math.abs(point1[1] - point2[1]); 21 | return Math.max(x, y); 22 | } 23 | } -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p91_120/p101/Readme.md: -------------------------------------------------------------------------------- 1 | Given an even number (greater than 2), return two prime numbers whose sum will 2 | be equal to the given number. 3 | 4 | A solution will always exist. See Goldbach’s conjecture 5 | [https://en.wikipedia.org/wiki/Goldbach%27s_conjecture]. 6 | 7 | Example: 8 | 9 | Input: 4 10 | Output: 2 + 2 = 4 11 | 12 | 13 | If there are more than one solution possible, return the lexicographically 14 | smaller solution. 15 | 16 | If [a, b] is one solution with a <= b, and [c, d] is another solution with c <= 17 | d, then 18 | 19 | [a, b] < [c, d] 20 | 21 | 22 | If a < c OR a==c AND b < d. 23 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p91_120/p102/Readme.md: -------------------------------------------------------------------------------- 1 | Given a list of integers and a number K, return which contiguous elements of the 2 | list sum to K. 3 | 4 | For example, if the list is [1, 2, 3, 4, 5] and K is 9, then it should return 5 | [2, 3, 4]. 6 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p91_120/p103/Readme.md: -------------------------------------------------------------------------------- 1 | Given a string and a set of characters, return the shortest substring containing 2 | all the characters in the set. 3 | 4 | For example, given the string "figehaeci" and the set of characters {a, e, i}, 5 | you should return "aeci". 6 | 7 | If there is no substring containing all the characters in the set, return null. 8 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p91_120/p104/Readme.md: -------------------------------------------------------------------------------- 1 | Determine whether a doubly linked list is a palindrome. What if it’s singly 2 | linked? 3 | 4 | For example, 1 -> 4 -> 3 -> 4 -> 1 returns true while 1 -> 4 returns false. 5 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p91_120/p107/Readme.md: -------------------------------------------------------------------------------- 1 | Print the nodes in a binary tree level-wise. For example, the following should 2 | print 1, 2, 3, 4, 5. 3 | 4 | ``` 5 | 6 | 1 7 | / \ 8 | 2 3 9 | / \ 10 | 4 5 11 | 12 | ``` 13 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p91_120/p108/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | Given two strings A and B, return whether or not A can be shifted some number of 3 | times to get B. 4 | 5 | For example, if A is abcde and B is cdeab, return true. If A is abc and B is acb 6 | , return false. 7 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p91_120/p108/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p91_120.p108; 2 | 3 | class Solution { 4 | 5 | boolean isRotated(String str1, String str2) { 6 | if (str1 == null && str2 == null) { 7 | return true; 8 | } 9 | 10 | if (str1 == null) { 11 | return false; 12 | } 13 | 14 | if (str2 == null) { 15 | return false; 16 | } 17 | if (str1.length() != str2.length()) { 18 | return false; 19 | } 20 | return (str1 + str1).contains(str2); 21 | } 22 | } -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p91_120/p110/Readme.md: -------------------------------------------------------------------------------- 1 | Given a binary tree, return all paths from the root to leaves. 2 | 3 | For example, given the tree 4 | 5 | ``` 6 | 1 7 | / \ 8 | 2 3 9 | / \ 10 | 4 5 11 | 12 | ``` 13 | it should return [[1, 2], [1, 3, 4], [1, 3, 5]]. -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p91_120/p111/Readme.md: -------------------------------------------------------------------------------- 1 | Given a word W and a string S, find all starting indices in S which are anagrams 2 | of W. 3 | 4 | For example, given that W is "ab", and S is "abxaba", return 0, 3, and 4. 5 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p91_120/p112/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in 3 | the tree. Assume that each node in the tree also has a pointer to its parent. 4 | 5 | According to the definition of LCA on Wikipedia 6 | [https://en.wikipedia.org/wiki/Lowest_common_ancestor]: “The lowest common 7 | ancestor is defined between two nodes v and w as the lowest node in T that has 8 | both v and w as descendants (where we allow a node to be a descendant of 9 | itself).” 10 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p91_120/p112/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p91_120.p112; 2 | 3 | import in.ashwanik.dcp.common.TreeNode; 4 | 5 | class Solution { 6 | 7 | TreeNode getLCA(TreeNode root, Integer left, Integer right) { 8 | if (root == null) { 9 | return null; 10 | } 11 | 12 | if (root.getData() == left || root.getData() == right) { 13 | return root; 14 | } 15 | 16 | TreeNode leftLCA = getLCA(root.getLeft(), left, right); 17 | TreeNode rightLCA = getLCA(root.getRight(), left, right); 18 | if (leftLCA != null && rightLCA != null) { 19 | return root; 20 | } 21 | return leftLCA != null ? leftLCA : rightLCA; 22 | } 23 | } -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p91_120/p113/Readme.md: -------------------------------------------------------------------------------- 1 | Given a string of words delimited by spaces, reverse the words in string. For 2 | example, given "hello world here", return "here world hello" 3 | 4 | Follow-up: given a mutable string representation, can you perform this operation 5 | in-place? 6 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p91_120/p114/Readme.md: -------------------------------------------------------------------------------- 1 | Given a string and a set of delimiters, reverse the words in the string while 2 | maintaining the relative order of the delimiters. For example, given 3 | "hello/world:here", return "here/world:hello" 4 | 5 | Follow-up: Does your solution work for the following cases: "hello/world:here/", 6 | "hello//world:here" -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p91_120/p115/Readme.md: -------------------------------------------------------------------------------- 1 | Given two non-empty binary trees s and t, check whether tree t has exactly the 2 | same structure and node values with a subtree of s. A subtree of s is a tree 3 | consists of a node in s and all of this node's descendants. The tree s could 4 | also be considered as a subtree of itself. 5 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p91_120/p116/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | Generate a finite, but an arbitrarily large binary tree quickly in O(1). 3 | 4 | That is, generate() should return a tree whose size is unbounded but finite. 5 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p91_120/p116/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p91_120.p116; 2 | 3 | import in.ashwanik.dcp.common.TreeNode; 4 | 5 | import java.util.Random; 6 | 7 | class Solution { 8 | 9 | private Random random; 10 | 11 | Solution() { 12 | random = new Random(); 13 | } 14 | 15 | TreeNode generate() { 16 | TreeNode root = new TreeNode<>(0); 17 | if (random.nextBoolean()) { 18 | root.setLeft(generate()); 19 | } 20 | if (random.nextBoolean()) { 21 | root.setRight(generate()); 22 | } 23 | return root; 24 | } 25 | 26 | 27 | } -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p91_120/p117/Readme.md: -------------------------------------------------------------------------------- 1 | Given a binary tree, return the level of the tree with minimum sum. 2 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p91_120/p118/Readme.md: -------------------------------------------------------------------------------- 1 | Given a sorted list of integers, square the elements and give the output in 2 | sorted order. 3 | 4 | For example, given [-9, -2, 0, 2, 3], return [0, 4, 4, 9, 81]. 5 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p91_120/p119/Readme.md: -------------------------------------------------------------------------------- 1 | Given a set of closed intervals, find the smallest set of numbers that covers 2 | all the intervals. If there are multiple smallest sets, return any of them. 3 | 4 | For example, given the intervals [0, 3], [2, 6], [3, 4], [6, 9], one set of 5 | numbers that covers all these intervals is {3, 6}. 6 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p91_120/p119/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p91_120.p119; 2 | 3 | class Solution { 4 | 5 | int[] minimumIntervalSet(int[][] array) { 6 | if (array == null || array.length == 0) { 7 | return null; 8 | } 9 | int index = 0; 10 | int max = Integer.MIN_VALUE; 11 | int min = Integer.MAX_VALUE; 12 | 13 | while (index < array.length) { 14 | int lowIndex = array[index][0] < array[index][1] ? 0 : 1; 15 | max = Math.max(array[index][lowIndex], max); 16 | min = Math.min(array[index][lowIndex == 0 ? 1 : 0], min); 17 | index++; 18 | } 19 | return new int[]{Math.min(max, min), Math.max(min, max)}; 20 | } 21 | } -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p91_120/p120/Readme.md: -------------------------------------------------------------------------------- 1 | Implement the singleton pattern with a twist. First, instead of storing one 2 | instance, store two instances. And in every even call of getInstance(), return 3 | the first instance and in every odd call of getInstance(), return the second 4 | instance. 5 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p91_120/p92/Readme.md: -------------------------------------------------------------------------------- 1 | We're given a hashmap with a key courseId and value a list of courseIds, which 2 | represents that the prerequsite of courseId is courseIds. Return a sorted 3 | ordering of courses such that we can finish all courses. 4 | 5 | Return null if there is no such ordering. 6 | 7 | For example, given {'CSC300': ['CSC100', 'CSC200'], 'CSC200': ['CSC100'], 8 | 'CSC100': []}, should return ['CSC100', 'CSC200', 'CSCS300']. 9 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p91_120/p94/Readme.md: -------------------------------------------------------------------------------- 1 | Given a binary tree of integers, find the maximum path sum between two nodes. 2 | The path must go through at least one node, and does not need to go through the 3 | root. 4 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p91_120/p95/Readme.md: -------------------------------------------------------------------------------- 1 | Given a number represented by a list of digits, find the next greater 2 | permutation of a number, in terms of lexicographic ordering. If there is not 3 | greater permutation possible, return the permutation with the lowest 4 | value/ordering. 5 | 6 | For example, the list [1,2,3] should return [1,3,2]. The list [1,3,2] should 7 | return [2,1,3]. The list [3,2,1] should return [1,2,3]. 8 | 9 | Can you perform the operation without allocating extra memory (disregarding the 10 | input memory)? 11 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p91_120/p99/Readme.md: -------------------------------------------------------------------------------- 1 | Given an unsorted array of integers, find the length of the longest consecutive 2 | elements sequence. 3 | 4 | For example, given [100, 4, 200, 1, 3, 2], the longest consecutive element 5 | sequence is [1, 2, 3, 4]. Return its length: 4. 6 | 7 | Your algorithm should run in O(n) complexity. 8 | 9 | -------------------------------------------------------------------------------- /src/main/java/in/ashwanik/dcp/problems/p91_120/p99/Solution.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p91_120.p99; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | class Solution { 7 | int getLongestConsecutiveSequenceLength(int[] numbers) { 8 | if (numbers == null || numbers.length == 0) { 9 | return 0; 10 | } 11 | 12 | Set set = new HashSet<>(); 13 | for (int n : numbers) { 14 | set.add(n); 15 | } 16 | 17 | int max = 0; 18 | for (int n : numbers) { 19 | int k = n; 20 | if (!set.contains(k - 1)) { 21 | int count = 1; 22 | while (set.contains(++k)) { 23 | count++; 24 | } 25 | max = Math.max(max, count); 26 | } 27 | } 28 | return max; 29 | } 30 | } -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p121_150/p121/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p121_150.p121; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertTrue; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testIsAtMostKPalindrome(){ 10 | assertTrue(new Solution().isAtMostKPalindrome("waterrfetawx",2)); 11 | assertTrue(new Solution().isAtMostKPalindrome("abcdecba",1)); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p121_150/p125/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p121_150.p125; 2 | 3 | import in.ashwanik.dcp.common.TreeNode; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import java.util.Arrays; 7 | 8 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 9 | 10 | public class SolutionTest { 11 | @Test 12 | void testSumPair() { 13 | 14 | TreeNode root = new TreeNode<>(10, 15 | new TreeNode<>(5), 16 | new TreeNode<>(15, 17 | new TreeNode<>(11), 18 | new TreeNode<>(15))); 19 | int[] pair = new Solution().getSumPair(root, 20); 20 | Arrays.sort(pair); 21 | assertArrayEquals(new int[]{5, 15}, pair); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p121_150/p126/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p121_150.p126; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testRotateArray() { 10 | int[] array = new int[]{1, 2, 3, 4, 5, 6}; 11 | new Solution().rotateArray(array, 2); 12 | assertArrayEquals(new int[]{3, 4, 5, 6, 1, 2}, array); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p121_150/p127/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p121_150.p127; 2 | 3 | import in.ashwanik.dcp.common.ListNode; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import java.util.Arrays; 7 | import java.util.Collections; 8 | 9 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 10 | 11 | public class SolutionTest { 12 | @Test 13 | void testListAdd() { 14 | assertArrayEquals(new Integer[]{4, 2, 1}, new Solution().add(new ListNode<>(Arrays.asList(9, 9)), new ListNode<>(Arrays.asList(5, 2))).data(Integer.class)); 15 | assertArrayEquals(new Integer[]{8, 0, 0, 1}, new Solution().add(new ListNode<>(Collections.singletonList(9)), new ListNode<>(Arrays.asList(9, 9, 9))).data(Integer.class)); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p121_150/p128/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p121_150.p128; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testTowerOfHanoi() { 10 | assertArrayEquals(new String[]{"Move 1 to 3", 11 | "Move 1 to 2", 12 | "Move 3 to 2", 13 | "Move 1 to 3", 14 | "Move 2 to 1", 15 | "Move 2 to 3", 16 | "Move 1 to 3"}, new Solution().towerOfHanoi(3).toArray()); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p121_150/p129/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p121_150.p129; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testSqrt() { 10 | assertEquals(3, new Solution().sqrt(9)); 11 | assertEquals(4, new Solution().sqrt(16)); 12 | assertEquals(4, new Solution().sqrt(18)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p121_150/p134/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p121_150.p134; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | import static org.junit.jupiter.api.Assertions.assertNull; 7 | 8 | public class SolutionTest { 9 | 10 | @Test 11 | void testSparseArray() { 12 | Solution solution = new Solution(); 13 | solution.init(new Integer[]{1, null, 3, null, null}, 5); 14 | assertNull(solution.get(4)); 15 | solution.set(3, 90); 16 | assertEquals(90, solution.get(3)); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p121_150/p136/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p121_150.p136; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testMaximumAreas1(){ 10 | assertEquals(4, new Solution().areaWithMaximum1(new int[][]{{1, 0, 0, 0}, 11 | {1, 0, 1, 1}, 12 | {1, 0, 1, 1}, 13 | {0, 1, 0, 0}})); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p121_150/p137/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p121_150.p137; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testBitArray() { 10 | Solution solution = new Solution(); 11 | solution.init(5); 12 | assertEquals(0, solution.get(3)); 13 | solution.set(0, 1); 14 | assertEquals(1, solution.get(0)); 15 | solution.set(1, 1); 16 | assertEquals(1, solution.get(1)); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p121_150/p141/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p121_150.p141; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SolutionTest { 8 | 9 | @Test 10 | void testStack() { 11 | 12 | Solution solution = new Solution(3, 6); 13 | 14 | solution.push(0, 5); 15 | solution.push(0, 6); 16 | solution.push(2, 7); 17 | 18 | 19 | assertEquals(6, solution.pop(0)); 20 | assertEquals(5, solution.pop(0)); 21 | assertEquals(7, solution.pop(2)); 22 | 23 | 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p121_150/p142/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p121_150.p142; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertFalse; 6 | import static org.junit.jupiter.api.Assertions.assertTrue; 7 | 8 | public class SolutionTest { 9 | @Test 10 | void testIsBalanced() { 11 | assertTrue(new Solution().isBalanced("(()*")); 12 | assertTrue(new Solution().isBalanced("(*)")); 13 | assertFalse(new Solution().isBalanced(")*(")); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p121_150/p143/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p121_150.p143; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testPartition() { 10 | int[] array = new int[]{9, 12, 3, 5, 14, 10, 10}; 11 | new Solution().partition(array, 10); 12 | assertArrayEquals(new int[]{9, 3, 5, 10, 10, 14, 12}, array); 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p121_150/p145/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p121_150.p145; 2 | 3 | import in.ashwanik.dcp.common.ListNode; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import java.util.Arrays; 7 | 8 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 9 | 10 | public class SolutionTest { 11 | 12 | @Test 13 | void testReverse() { 14 | ListNode head = new ListNode<>(Arrays.asList(1, 2, 3, 4, 5)); 15 | Integer[] result = new Solution().reverse(head, 2).data(Integer.class); 16 | assertArrayEquals(new Integer[]{2, 1, 4, 3, 5}, result); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p121_150/p146/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p121_150.p146; 2 | 3 | import in.ashwanik.dcp.common.TreeNode; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 7 | 8 | public class SolutionTest { 9 | 10 | @Test 11 | void testPruneTree() { 12 | 13 | TreeNode root = new TreeNode<>(0, 14 | new TreeNode<>(1), 15 | new TreeNode<>(0, 16 | new TreeNode<>(1, 17 | new TreeNode<>(0), 18 | new TreeNode<>(0)), 19 | new TreeNode<>(0))); 20 | assertArrayEquals(new Integer[]{1, 0, 1, 0}, new Solution().pruneTree(root).inorder().toArray()); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p121_150/p147/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p121_150.p147; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 6 | 7 | public class SolutionTest { 8 | 9 | @Test 10 | void testPancakeSort() { 11 | Solution solution = new Solution(); 12 | int[] array = new int[]{9, 10, 1, 8, 2, 3}; 13 | solution.pancakeSort(array); 14 | 15 | assertArrayEquals(new int[]{1, 2, 3, 8, 9, 10}, array); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p121_150/p148/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p121_150.p148; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testGrayCode() { 10 | assertArrayEquals(new Integer[]{0, 1, 3, 2}, new Solution().grayCode(2).toArray()); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p121_150/p150/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p121_150.p150; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import java.util.List; 6 | 7 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 8 | import static org.junit.jupiter.api.Assertions.assertEquals; 9 | 10 | public class SolutionTest { 11 | @Test 12 | void testKNearestPoints() { 13 | int[][] output = new int[][]{{3, 1}, {0, 0}}; 14 | List result = new Solution().kNearestPoints(new int[][]{{0, 0}, {5, 4}, {3, 1}}, new int[]{1, 2}, 2); 15 | assertEquals(output.length, result.size()); 16 | assertArrayEquals(output[0], result.get(0)); 17 | assertArrayEquals(output[1], result.get(1)); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p151_180/p153/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p151_180.p153; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testMinimumDistance() { 10 | assertEquals(2, new Solution().minimumDistanceBetweenWords("dog cat hello cat dog dog hello cat world", 11 | "hello", "world")); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p151_180/p154/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p151_180.p154; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testStack() { 10 | Solution solution = new Solution(); 11 | 12 | solution.push(1); 13 | solution.push(2); 14 | solution.push(3); 15 | 16 | assertEquals(3, solution.pop()); 17 | assertEquals(2, solution.pop()); 18 | assertEquals(1, solution.pop()); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p151_180/p155/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p151_180.p155; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testGetMajorityElement() { 10 | assertEquals(1, new Solution().getMajorityElement(new int[]{1, 2, 1, 1, 3, 4, 0})); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p151_180/p156/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p151_180.p156; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertAll; 6 | import static org.junit.jupiter.api.Assertions.assertEquals; 7 | 8 | public class SolutionTest { 9 | @Test 10 | void testMinimumNumberOfSquare() { 11 | assertAll(() -> assertEquals(2, new Solution().minimumNumberOfSquaresBF(13)), 12 | () -> assertEquals(3, new Solution().minimumNumberOfSquaresBF(27)), 13 | () -> assertEquals(2, new Solution().minimumNumberOfSquaresDP(13)), 14 | () -> assertEquals(3, new Solution().minimumNumberOfSquaresDP(27))); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p151_180/p157/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p151_180.p157; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertAll; 6 | import static org.junit.jupiter.api.Assertions.assertFalse; 7 | import static org.junit.jupiter.api.Assertions.assertTrue; 8 | 9 | public class SolutionTest { 10 | @Test 11 | void testIsPalindrome() { 12 | assertAll("Is String palindrome", () -> assertTrue(new Solution().isPalindrome("carrace")), 13 | () -> assertFalse(new Solution().isPalindrome("daily"))); 14 | 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p151_180/p159/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p151_180.p159; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertAll; 6 | import static org.junit.jupiter.api.Assertions.assertEquals; 7 | import static org.junit.jupiter.api.Assertions.assertNull; 8 | 9 | public class SolutionTest { 10 | 11 | @Test 12 | void testGetRecurring() { 13 | 14 | assertAll(() -> assertEquals(new Character('b'), new Solution().findRecurring("acbbac")), 15 | () -> assertNull(new Solution().findRecurring("abcd"))); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p151_180/p163/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p151_180.p163; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SolutionTest { 8 | 9 | @Test 10 | void testEvaluate() { 11 | assertEquals(5, new Solution().evaluate(new String[]{"15", "7", "1", "1", "+", "-", "/", "3", "*", "2", "1", "1", "+", "+", "-"})); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p151_180/p164/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p151_180.p164; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SolutionTest { 8 | 9 | @Test 10 | void testGetDuplicateNumber() { 11 | assertEquals(5, new Solution().getDuplicateNumber(new int[]{1, 2, 3, 4, 5, 5}, 5)); 12 | assertEquals(2, new Solution().getDuplicateNumber(new int[]{1, 2, 3, 4, 2, 5}, 5)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p151_180/p168/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p151_180.p168; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testRotateMatrix() { 10 | int[][] array = new int[][]{ 11 | {1, 2, 3}, 12 | {4, 5, 6}, 13 | {7, 8, 9}}; 14 | 15 | int[][] expected = new int[][]{{7, 4, 1}, 16 | {8, 5, 2}, 17 | {9, 6, 3}}; 18 | 19 | new Solution().rotate(array); 20 | 21 | for (int index = 0; index < array.length; index++) { 22 | assertArrayEquals(expected[index], array[index]); 23 | } 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p151_180/p169/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p151_180.p169; 2 | 3 | import in.ashwanik.dcp.common.ListNode; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import java.util.Arrays; 7 | 8 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 9 | 10 | public class SolutionTest { 11 | 12 | @Test 13 | void testListSort() { 14 | assertArrayEquals(new Integer[]{1, 2, 3, 9}, 15 | new Solution().sort(new ListNode<>(Arrays.asList(1, 9, 2, 3))).data(Integer.class)); 16 | assertArrayEquals(new Integer[]{1, 2, 3, 9, 10}, 17 | new Solution().sort(new ListNode<>(Arrays.asList(10, 1, 9, 2, 3))).data(Integer.class)); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p151_180/p170/variation/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p151_180.p170.variation; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import java.util.Arrays; 6 | import java.util.HashSet; 7 | 8 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 9 | import static org.junit.jupiter.api.Assertions.assertNull; 10 | 11 | public class SolutionTest { 12 | 13 | @Test 14 | void testTransformation() { 15 | Solution solution = new Solution(new HashSet<>(Arrays.asList("dog", "dot", "dop", 16 | "dat", "cat"))); 17 | 18 | assertArrayEquals(new String[]{"dog", "dot", "dat", "cat"}, solution.getTransformation("dog", "cat").toArray()); 19 | assertNull(solution.getTransformation("dog", "mat")); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p151_180/p172/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p151_180.p172; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import java.util.Arrays; 6 | 7 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 8 | 9 | public class SolutionTest { 10 | @Test 11 | void testGetIndices() { 12 | assertArrayEquals(new Integer[]{0, 13}, new Solution().getIndices("dogcatcatcodecatdog", Arrays.asList("cat", "dog")).toArray()); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p151_180/p176/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p151_180.p176; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertFalse; 6 | import static org.junit.jupiter.api.Assertions.assertTrue; 7 | 8 | public class SolutionTest { 9 | @Test 10 | void testCheckOneOnOneMapping() { 11 | assertTrue(new Solution().checkOneToOneMapping("abc", "bcd")); 12 | assertFalse(new Solution().checkOneToOneMapping("foo", "bar")); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p151_180/p177/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p151_180.p177; 2 | 3 | import in.ashwanik.dcp.common.ListNode; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import java.util.Arrays; 7 | 8 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 9 | 10 | public class SolutionTest { 11 | @Test 12 | void testRotate() { 13 | ListNode head = new ListNode<>(Arrays.asList(7, 7, 3, 5)); 14 | assertArrayEquals(new Integer[]{3, 5, 7, 7}, new Solution().rotate(head, 2).data(Integer.class)); 15 | head = new ListNode<>(Arrays.asList(1, 2, 3, 4, 5)); 16 | //head = new ListNode<>(Arrays.asList(10, 20, 30, 40, 50, 60)); 17 | assertArrayEquals(new Integer[]{3, 4, 5, 1, 2}, new Solution().rotate(head, 3).data(Integer.class)); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p151_180/p178/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p151_180.p178; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertTrue; 6 | 7 | public class SolutionTest { 8 | private Solution solution = new Solution(); 9 | 10 | @Test 11 | void testDice() { 12 | assertTrue(count(5, 6) < count(5, 5)); 13 | } 14 | 15 | private int count(int a, int b) { 16 | int count = 0; 17 | for (int index = 0; index < 10000; index++) { 18 | count = solution.numberOfDiceToWin(a, b); 19 | } 20 | return count; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p151_180/p179/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p151_180.p179; 2 | 3 | import in.ashwanik.dcp.common.TreeNode; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 7 | 8 | public class SolutionTest { 9 | 10 | @Test 11 | void testPostOrderToBST() { 12 | TreeNode root = new Solution().getBST(new int[]{2, 4, 3, 8, 7, 5}); 13 | assertArrayEquals(new Integer[]{2, 3, 4, 5, 7, 8}, root.inorder().toArray()); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p181_210/p182/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p181_210.p182; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertTrue; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void isMinimalConnected() { 10 | int[][] graph = new int[][]{ 11 | {1, 2}, 12 | {3}, 13 | {}, 14 | {} 15 | }; 16 | 17 | assertTrue(new Solution().isMinimallyConnected(graph)); 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p181_210/p184/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p181_210.p184; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testGCD() { 10 | assertEquals(14, new Solution().gcd(new int[]{42, 56, 14})); 11 | } 12 | } -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p181_210/p189/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p181_210.p189; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testMaxDistinctSubArray() { 10 | assertEquals(5, new Solution().maxDistinctLength(new int[]{5, 1, 3, 5, 2, 3, 4, 1})); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p181_210/p190/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p181_210.p190; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testMaxSubArraySumForCircularArray() { 10 | assertEquals(15, new Solution().maxSubArraySum(new int[]{8, -1, 3, 4})); 11 | assertEquals(6, new Solution().maxSubArraySum(new int[]{-4, 5, 1, 0})); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p181_210/p191/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p181_210.p191; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testIntervalsToRemove() { 10 | assertEquals(1, new Solution().minimumNumberOfIntervalsToRemove(new int[][]{{7, 9}, {2, 4}, {5, 8}})); 11 | assertEquals(0, new Solution().minimumNumberOfIntervalsToRemove(new int[][]{{0, 1}, {1, 2}})); 12 | 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p181_210/p195/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p181_210.p195; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testCount() { 10 | assertEquals(15, new Solution().countNumbers(new int[][]{{1, 3, 7, 10, 15, 20}, 11 | {2, 6, 9, 14, 22, 25}, 12 | {3, 8, 10, 15, 25, 30}, 13 | {10, 11, 12, 23, 30, 35}, 14 | {20, 25, 30, 35, 40, 45}}, new int[]{1, 1}, new int[]{3, 3})); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p181_210/p196/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p181_210.p196; 2 | 3 | import in.ashwanik.dcp.common.TreeNode; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import static org.junit.jupiter.api.Assertions.assertEquals; 7 | 8 | public class SolutionTest { 9 | @Test 10 | void testCount() { 11 | assertEquals(2, new Solution().countSum(new TreeNode<>(5, 12 | new TreeNode<>(2), 13 | new TreeNode<>(-5)))); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p181_210/p197/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p181_210.p197; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testRotateArray() { 10 | int[] array = {1, 2, 3, 4, 5, 6, 7, 8}; 11 | new Solution().rotateArrayToRight(array, 3); 12 | assertArrayEquals(new int[]{6, 7, 8, 1, 2, 3, 4, 5}, array); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p181_210/p198/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p181_210.p198; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 6 | 7 | public class SolutionTest { 8 | 9 | @Test 10 | void testSubset() { 11 | assertArrayEquals(new Integer[]{20, 10, 5}, new Solution().getSubset(new int[]{3, 5, 10, 20, 21}).toArray()); 12 | assertArrayEquals(new Integer[]{24, 6, 3, 1}, new Solution().getSubset(new int[]{1, 3, 6, 24}).toArray()); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p181_210/p199/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p181_210.p199; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testBalancedParentheses(){ 10 | assertEquals("(())",new Solution().getBalancedParentheses("(()")); 11 | assertEquals("()()()()",new Solution().getBalancedParentheses("))()(")); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p181_210/p201/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p181_210.p201; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testMaxSum() { 10 | assertEquals(9, new Solution().maxSum(new int[][]{{1}, {2, 3}, {1, 5, 1}})); 11 | assertEquals(23, new Solution().maxSum(new int[][]{{3}, {7, 4}, {2, 4, 6}, {8, 5, 9, 3}})); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p181_210/p202/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p181_210.p202; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertFalse; 6 | import static org.junit.jupiter.api.Assertions.assertTrue; 7 | 8 | public class SolutionTest { 9 | 10 | @Test 11 | void testIsPalindrome() { 12 | assertTrue(new Solution().isPalindrom(12321)); 13 | assertFalse(new Solution().isPalindrom(1234)); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p181_210/p203/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p181_210.p203; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testGetMinimum() { 10 | assertEquals(3, new Solution().getMinimum(new int[]{5, 7, 10, 3, 4})); 11 | assertEquals(3, new Solution().getMinimum(new int[]{10, 3, 4, 5, 6, 7, 8})); 12 | assertEquals(3, new Solution().getMinimum(new int[]{5, 7, 10, 17, 17, 18, 3, 4})); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p181_210/p204/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p181_210.p204; 2 | 3 | import in.ashwanik.dcp.common.TreeNode; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import static org.junit.jupiter.api.Assertions.assertEquals; 7 | 8 | public class SolutionTest { 9 | @Test 10 | void testCountNodes() { 11 | TreeNode root = new TreeNode<>(1 12 | , new TreeNode<>(2 13 | , new TreeNode<>(4) 14 | , new TreeNode<>(5)) 15 | , new TreeNode<>(3)); 16 | 17 | assertEquals(5, new Solution().numberOfNodes(root)); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p181_210/p205/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p181_210.p205; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testNextNumber() { 10 | int[] array = {4, 8, 9, 7, 5}; 11 | new Solution().nextNumber(array); 12 | assertArrayEquals(new int[]{4, 9, 5, 7, 8}, array); 13 | 14 | array = new int[]{2, 1, 8, 7, 6, 5}; 15 | new Solution().nextNumber(array); 16 | assertArrayEquals(new int[]{2, 5, 1, 6, 7, 8}, array); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p181_210/p206/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p181_210.p206; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 6 | 7 | 8 | public class SolutionTest { 9 | @Test 10 | void testPermutation() { 11 | char[] array = {'a', 'b', 'c'}; 12 | int[] indices = {2, 1, 0}; 13 | new Solution().permuteArray(array, indices); 14 | assertArrayEquals(new char[]{'c', 'b', 'c'}, array); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p181_210/p208/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p181_210.p208; 2 | 3 | import in.ashwanik.dcp.common.ListNode; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import java.util.Arrays; 7 | 8 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 9 | 10 | public class SolutionTest { 11 | @Test 12 | void testPartition() { 13 | ListNode head = new ListNode<>(Arrays.asList(5, 1, 8, 0, 3)); 14 | assertArrayEquals(new Integer[]{1, 0, 5, 8, 3}, new Solution().partition(head, 3).data(Integer.class)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p181_210/p210/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p181_210.p210; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 6 | 7 | public class SolutionTest { 8 | 9 | @Test 10 | void testCollatzSequence() { 11 | assertArrayEquals(new Integer[]{}, new Solution().getCollatzSequence(0).toArray()); 12 | assertArrayEquals(new Integer[]{1}, new Solution().getCollatzSequence(1).toArray()); 13 | assertArrayEquals(new Integer[]{12, 6, 3, 10, 5, 16, 8, 4, 2, 1}, new Solution().getCollatzSequence(12).toArray()); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p1_30/p1/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p1_30.p1; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertFalse; 6 | 7 | class SolutionTest { 8 | @Test 9 | void testSumExistsForNull() { 10 | int[] array = null; 11 | assertFalse(new Solution().sumExists(array, 5)); 12 | } 13 | 14 | @Test 15 | void testSumExistsTrue() { 16 | int[] array = {10, 5}; 17 | assertFalse(new Solution().sumExists(array, 10)); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p1_30/p11/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p1_30.p11; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import java.util.Arrays; 6 | import java.util.LinkedHashSet; 7 | 8 | import static org.junit.jupiter.api.Assertions.assertEquals; 9 | 10 | class SolutionTest { 11 | 12 | @Test 13 | void testLargestSum() { 14 | Solution solution = new Solution(); 15 | solution.preprocess(new LinkedHashSet<>(Arrays.asList("cat", "do", "dog", "deer", "deal"))); 16 | assertEquals("", String.join(",", solution.search("a"))); 17 | assertEquals("deal,deer", String.join(",", solution.search("de"))); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p1_30/p12/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p1_30.p12; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | class SolutionTest { 8 | 9 | @Test 10 | void testNumberOfWaysToClimb() { 11 | assertEquals(5, new Solution().numberOfWaysToClimb(4)); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p1_30/p13/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p1_30.p13; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | class SolutionTest { 8 | 9 | @Test 10 | void testLongestStringWithDistinctCharacter() { 11 | assertEquals(3, new Solution().longestSubstringWithDistinctCharacters("abcba",2)); 12 | assertEquals(4, new Solution().longestSubstringWithDistinctCharacters("araaci",2)); 13 | assertEquals(2, new Solution().longestSubstringWithDistinctCharacters("araaci",1)); 14 | assertEquals(5, new Solution().longestSubstringWithDistinctCharacters("cbbebi",3)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p1_30/p15/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p1_30.p15; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | class SolutionTest { 8 | 9 | @Test 10 | void testGetRandom() { 11 | assertEquals(2, new Solution().getRandom(2)); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p1_30/p16/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p1_30.p16; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | class SolutionTest { 8 | 9 | @Test 10 | void testLog() { 11 | Solution solution = new Solution(5); 12 | solution.record(1); 13 | solution.record(2); 14 | solution.record(3); 15 | solution.record(4); 16 | assertEquals(2, solution.getLast(3)); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p1_30/p17/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p1_30.p17; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | class SolutionTest { 8 | 9 | @Test 10 | void testMaxAbsoluteFilePathLength() { 11 | assertEquals(5, new Solution().maxAbsoluteFilePathLength("a.txt")); 12 | assertEquals(32, new Solution().maxAbsoluteFilePathLength("dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext")); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p1_30/p18/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p1_30.p18; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 6 | import static org.junit.jupiter.api.Assertions.assertEquals; 7 | 8 | class SolutionTest { 9 | 10 | @Test 11 | void testMaxAbsoluteFilePathLength() { 12 | assertArrayEquals(new int[]{3, 3, 5, 5, 6, 7}, new Solution().getMaximumInSubArray(new int[]{1, 3, -1, -3, 5, 3, 6, 7}, 3)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p1_30/p2/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p1_30.p2; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | class SolutionTest { 8 | @Test 9 | void testProduct() { 10 | int[] array = {3, 2, 1}; 11 | int[] result = new Solution().product(array); 12 | assertEquals(2, result[0]); 13 | assertEquals(3, result[1]); 14 | assertEquals(6, result[2]); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p1_30/p20/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p1_30.p20; 2 | 3 | import in.ashwanik.dcp.common.ListNode; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import static org.junit.jupiter.api.Assertions.assertEquals; 7 | 8 | class SolutionTest { 9 | 10 | @Test 11 | void testMaxAbsoluteFilePathLength() { 12 | //3 -> 7 -> 8 -> 10 and B = 99 -> 1 -> 8 -> 10 13 | ListNode first = new ListNode<>(3, new ListNode<>(7, new ListNode<>(8, new ListNode<>(10)))); 14 | ListNode second = new ListNode<>(99, new ListNode<>(1, new ListNode<>(8, new ListNode<>(10)))); 15 | 16 | assertEquals(8, new Solution().getIntersectingNode(first, second).getData()); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p1_30/p21/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p1_30.p21; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | import static org.junit.jupiter.api.Assertions.assertEquals; 9 | 10 | class SolutionTest { 11 | 12 | @Test 13 | void testMinimumNumberOfRooms() { 14 | List entries = new ArrayList<>(); 15 | entries.add(new int[]{30, 75}); 16 | entries.add(new int[]{0, 50}); 17 | entries.add(new int[]{60, 150}); 18 | 19 | 20 | assertEquals(2, new Solution().getMinimumNumberOfRooms(entries)); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p1_30/p23/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p1_30.p23; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | class SolutionTest { 8 | 9 | @Test 10 | void testGetMinimumSteps() { 11 | 12 | boolean[][] matrix = {{true, true, true, true}, 13 | {false, false, true, false}, 14 | {true, true, true, true}, 15 | {true, true, true, true}}; 16 | 17 | Solution.Point start = new Solution.Point(3, 0); 18 | Solution.Point end = new Solution.Point(0, 0); 19 | 20 | 21 | assertEquals(7, new Solution().getMinimumNumberOfSteps(matrix, start, end)); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p1_30/p24/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p1_30.p24; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertTrue; 6 | 7 | class SolutionTest { 8 | 9 | @Test 10 | void testLockUnLockTree() { 11 | Solution.TreeNode treeNode = new Solution.TreeNode(); 12 | treeNode.data = 1; 13 | treeNode.left = new Solution.TreeNode(); 14 | treeNode.right = new Solution.TreeNode(); 15 | assertTrue(new Solution().lock(treeNode)); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p1_30/p26/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p1_30.p26; 2 | 3 | import in.ashwanik.dcp.common.ListNode; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import static org.junit.jupiter.api.Assertions.assertEquals; 7 | 8 | class SolutionTest { 9 | 10 | @Test 11 | void testKthLastNode() { 12 | ListNode head = new ListNode<>(1, 13 | new ListNode<>(2, 14 | new ListNode<>(3, 15 | new ListNode<>(4, 16 | new ListNode<>(5))))); 17 | 18 | assertEquals(3, new Solution().getKthNodeFromLast(head, 3).getData()); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p1_30/p28/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p1_30.p28; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import java.util.Arrays; 6 | import java.util.List; 7 | 8 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 9 | 10 | public class SolutionTest { 11 | @Test 12 | void testJustifyText() { 13 | List text = Arrays.asList("the", "quick", "brown", "fox", "jumps", 14 | "over", "the", "lazy", "dog"); 15 | assertArrayEquals(new String[]{"the quick brown", "fox jumps over", "the lazy dog"}, new Solution().justifyText(text, 16).toArray()); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p1_30/p29/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p1_30.p29; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | class SolutionTest { 8 | 9 | @Test 10 | void testEncoded() { 11 | assertEquals("4A3B2C1D2A", new Solution().encode("AAAABBBCCDAA")); 12 | assertEquals("ABCD", new Solution().encode("ABCD")); 13 | assertEquals("4w3a1d1e6x", new Solution().encode("wwwwaaadexxxxxx")); 14 | 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p1_30/p30/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p1_30.p30; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | class SolutionTest { 8 | 9 | @Test 10 | void testEncoded() { 11 | assertEquals(0, new Solution().calculateTrappedWater(new int[]{})); 12 | assertEquals(1, new Solution().calculateTrappedWater(new int[]{2, 1, 2})); 13 | assertEquals(8, new Solution().calculateTrappedWater(new int[]{3, 0, 1, 3, 0, 5})); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p1_30/p4/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p1_30.p4; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | class SolutionTest { 8 | 9 | @Test 10 | void testMissingNumber() { 11 | assertEquals(1, new Solution().missingInteger(new int[]{})); 12 | assertEquals(1, new Solution().missingInteger(new int[]{2, 3, 7, 6, 8, -1, -10, 15})); 13 | assertEquals(4, new Solution().missingInteger(new int[]{2, 3, -7, 6, 8, 1, -10, 15})); 14 | assertEquals(2, new Solution().missingInteger(new int[]{1, 1, 0, -1, -2})); 15 | assertEquals(2, new Solution().missingInteger(new int[]{3, 4, -1, 1})); 16 | assertEquals(3, new Solution().missingInteger(new int[]{1, 2, 0})); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p1_30/p7/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p1_30.p7; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | class SolutionTest { 8 | 9 | @Test 10 | void testCount() { 11 | assertEquals(3, new Solution().countOfDecoding("111")); 12 | assertEquals(3, new Solution().countOfDecoding("1234")); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p1_30/p8/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p1_30.p8; 2 | 3 | import in.ashwanik.dcp.common.TreeNode; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import static org.junit.jupiter.api.Assertions.assertEquals; 7 | 8 | class SolutionTest { 9 | 10 | @Test 11 | void testUnivalTreeCount() { 12 | assertEquals(5, new Solution() 13 | .univalSubtreeCount(new TreeNode<>(0, 14 | new TreeNode<>(1), 15 | new TreeNode<>(0, 16 | new TreeNode<>(1, 17 | new TreeNode<>(1), 18 | new TreeNode<>(1)), 19 | new TreeNode<>(0))))); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p1_30/p9/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p1_30.p9; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | class SolutionTest { 8 | 9 | @Test 10 | void testLargestSum() { 11 | assertEquals(0, new Solution().getLargestSum(new int[]{})); 12 | assertEquals(4, new Solution().getLargestSum(new int[]{2, 4})); 13 | assertEquals(13, new Solution().getLargestSum(new int[]{2, 4, 6, 2, 5})); 14 | assertEquals(10, new Solution().getLargestSum(new int[]{5, 1, 1, 5})); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p211_p240/p211/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p211_p240.p211; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testIndices() { 10 | assertArrayEquals(new Integer[]{0, 7}, new Solution().getIndices("abracadabra", "abr").toArray()); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p211_p240/p212/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p211_p240.p212; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testExcelColumn(){ 10 | assertEquals("AA",new Solution().getExcelColumn(27)); 11 | assertEquals("CB",new Solution().getExcelColumn(80)); 12 | assertEquals("AAC",new Solution().getExcelColumn(705)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p211_p240/p213/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p211_p240.p213; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testValidIps() { 10 | assertArrayEquals(new String[]{"254.25.40.123", "254.254.0.123"}, new Solution().generatePossibleIps("2542540123").toArray()); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p211_p240/p214/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p211_p240.p214; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testLength() { 10 | assertEquals(3, new Solution().lengthOf1s(156)); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p211_p240/p216/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p211_p240.p216; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testRomanToDecimal() { 10 | Solution solution = new Solution(); 11 | assertEquals(5, solution.convertRomanToDecimal("V")); 12 | assertEquals(4, solution.convertRomanToDecimal("IV")); 13 | assertEquals(40, solution.convertRomanToDecimal("XL")); 14 | assertEquals(14, solution.convertRomanToDecimal("XIV")); 15 | assertEquals( 1904, solution.convertRomanToDecimal("MCMIV")); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p31_60/p31/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p31_60.p31; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | class SolutionTest { 8 | 9 | @Test 10 | void testEditDistance() { 11 | assertEquals(3, new Solution().editDistanceBF("kitten", "sitting")); 12 | assertEquals(3, new Solution().editDistance("kitten", "sitting")); 13 | 14 | assertEquals(3, new Solution().editDistanceBF("sunday", "saturday")); 15 | assertEquals(3, new Solution().editDistance("sunday", "saturday")); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p31_60/p33/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p31_60.p33; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 6 | 7 | class SolutionTest { 8 | 9 | @Test 10 | void testGetMedian() { 11 | assertArrayEquals(new double[]{}, new Solution().getMedian(new int[]{})); 12 | assertArrayEquals(new double[]{2, 1.5, 2, 3.5, 2, 2, 2}, new Solution().getMedian(new int[]{2, 1, 5, 7, 2, 0, 5})); 13 | assertArrayEquals(new double[]{12.0, 8.0, 5.0, 4.5, 5.0, 6.0}, new Solution().getMedian(new int[]{12, 4, 5, 3, 8, 7})); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p31_60/p35/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p31_60.p35; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 6 | 7 | class SolutionTest { 8 | 9 | @Test 10 | void testSort() { 11 | assertArrayEquals(new char[]{'R', 'R', 'R', 'G', 'G', 'B', 'B'}, new Solution().sort(new char[]{'G', 'B', 'R', 'R', 'B', 'R', 'G'})); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p31_60/p36/MorrisTraversalTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p31_60.p36; 2 | 3 | import in.ashwanik.dcp.common.TreeNode; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 7 | 8 | public class MorrisTraversalTest { 9 | @Test 10 | void testInorder() { 11 | 12 | TreeNode root = new TreeNode<>(15, 13 | new TreeNode<>(10, 14 | new TreeNode<>(8), 15 | new TreeNode<>(12)), 16 | new TreeNode<>(20, 17 | new TreeNode<>(16), 18 | new TreeNode<>(25))); 19 | 20 | 21 | assertArrayEquals(new Integer[]{8, 10, 12, 15, 16, 20, 25}, new MorrisTraversal().getInorderTraversal(root).toArray()); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p31_60/p36/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p31_60.p36; 2 | 3 | import in.ashwanik.dcp.common.TreeNode; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import static org.junit.jupiter.api.Assertions.assertEquals; 7 | 8 | public class SolutionTest { 9 | @Test 10 | void testKthLargest() { 11 | 12 | TreeNode root = new TreeNode<>(15, 13 | new TreeNode<>(10, 14 | new TreeNode<>(8), 15 | new TreeNode<>(12)), 16 | new TreeNode<>(20, 17 | new TreeNode<>(16), 18 | new TreeNode<>(25))); 19 | 20 | 21 | assertEquals(25, new Solution().getKthLargest(root, 1).getData()); 22 | assertEquals(20, new Solution().getKthLargest(root, 2).getData()); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p31_60/p37/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p31_60.p37; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import java.util.Arrays; 6 | import java.util.List; 7 | 8 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 9 | 10 | public class SolutionTest { 11 | @Test 12 | void testPowerSet() { 13 | List data = Arrays.asList("", "1", "2", "3", "1,2", "1,3", "2,3", "1,2,3"); 14 | data.sort(String::compareTo); 15 | assertArrayEquals(data.toArray(), new Solution().powerSet(Arrays.asList(1, 2, 3)).toArray()); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p31_60/p38/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p31_60.p38; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testNumberOfSolutions() { 10 | assertEquals(2, new Solution().numberOfSolutions(4)); 11 | assertEquals(10, new Solution().numberOfSolutions(5)); 12 | assertEquals(4, new Solution().numberOfSolutions(6)); 13 | assertEquals(92, new Solution().numberOfSolutions(8)); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p31_60/p42/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p31_60.p42; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import java.util.Arrays; 6 | import java.util.List; 7 | 8 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 9 | 10 | public class SolutionTest { 11 | @Test 12 | void testSubset() { 13 | List data = Arrays.asList(12, 9, 2, 1); 14 | data.sort(Integer::compareTo); 15 | List result = new Solution().getSubset(new int[]{12, 1, 61, 5, 9, 2}, 24); 16 | result.sort(Integer::compareTo); 17 | assertArrayEquals(data.toArray(), result.toArray()); 18 | } 19 | } -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p31_60/p44/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p31_60.p44; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testCountInversion() { 10 | assertEquals(3, new Solution().numberOfInversions(new int[]{2, 4, 1, 3, 5})); 11 | assertEquals(10, new Solution().numberOfInversions(new int[]{5, 4, 3, 2, 1})); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p31_60/p45/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p31_60.p45; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertTrue; 6 | 7 | public class SolutionTest { 8 | @Test 9 | public void testRand7() { 10 | assertTrue(new Solution().rand7() < 8); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p31_60/p46/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p31_60.p46; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | public void testPalindromicString() { 10 | assertEquals("bcdcb", new Solution().longestPalindromincSubstring("aabcdcb")); 11 | assertEquals("anana", new Solution().longestPalindromincSubstring("bananas")); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p31_60/p47/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p31_60.p47; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | public void testMaxProfit() { 10 | assertEquals(5, new Solution().maxProfit(new int[]{9, 11, 8, 5, 7, 10})); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p31_60/p48/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p31_60.p48; 2 | 3 | import in.ashwanik.dcp.common.TreeNode; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 7 | 8 | public class SolutionTest { 9 | @Test 10 | public void testTreeFromPreorderAndInorder() { 11 | 12 | String[] pre = new String[]{"a", "b", "d", "e", "c", "f", "g"}; 13 | String[] in = new String[]{"d", "b", "e", "a", "f", "c", "g"}; 14 | 15 | TreeNode root = new Solution().createTree(pre, in); 16 | assertArrayEquals(pre, root.preorder().toArray()); 17 | assertArrayEquals(in, root.inorder().toArray()); 18 | } 19 | } -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p31_60/p49/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p31_60.p49; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | public void testMaxSum() { 10 | assertEquals(137, new Solution().maxSum(new int[]{34, -50, 42, 14, -5, 86})); 11 | assertEquals(0, new Solution().maxSum(new int[]{-5, -1, -8, -9})); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p31_60/p50/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p31_60.p50; 2 | 3 | import in.ashwanik.dcp.common.TreeNode; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import static org.junit.jupiter.api.Assertions.assertEquals; 7 | 8 | public class SolutionTest { 9 | 10 | @Test 11 | public void testEvaluate() { 12 | TreeNode root = new TreeNode<>("*", 13 | new TreeNode<>("+", 14 | new TreeNode<>("3"), 15 | new TreeNode<>("2")), 16 | new TreeNode<>("+", 17 | new TreeNode<>("4"), 18 | new TreeNode<>("5"))); 19 | 20 | assertEquals(45, new Solution().evaluate(root)); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p31_60/p52/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p31_60.p52; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | import static org.junit.jupiter.api.Assertions.assertNull; 7 | 8 | public class SolutionTest { 9 | 10 | @Test 11 | public void testLRU() { 12 | Solution solution = new Solution(3); 13 | solution.set("k1", "value1"); 14 | assertEquals("value1", solution.get("k1")); 15 | solution.set("k2", "value2"); 16 | solution.set("k3", "value3"); 17 | solution.set("k4", "value4"); 18 | assertNull(solution.get("k1")); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p31_60/p53/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p31_60.p53; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SolutionTest { 8 | 9 | @Test 10 | public void testQueue() { 11 | Solution solution = new Solution(); 12 | solution.enqueue(1); 13 | solution.enqueue(2); 14 | solution.enqueue(3); 15 | assertEquals(1, solution.dequeue()); 16 | assertEquals(2, solution.dequeue()); 17 | solution.enqueue(4); 18 | solution.enqueue(5); 19 | solution.enqueue(6); 20 | assertEquals(3, solution.dequeue()); 21 | assertEquals(4, solution.dequeue()); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p31_60/p55/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p31_60.p55; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SolutionTest { 8 | 9 | @Test 10 | public void testShortUrl() { 11 | Solution solution = new Solution(1000); 12 | assertEquals("www.google.com", solution.decode(solution.encode("www.google.com"))); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p31_60/p57/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p31_60.p57; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 6 | import static org.junit.jupiter.api.Assertions.assertNull; 7 | 8 | public class SolutionTest { 9 | 10 | @Test 11 | public void testBreak() { 12 | assertArrayEquals(new String[]{"the quick", "brown fox", "jumps over", "the lazy", 13 | "dog"}, new Solution().breakSentence("the quick brown fox jumps over the lazy dog", 10).toArray()); 14 | 15 | assertNull(new Solution().breakSentence("the quick brown fox jumps over the lazy dog", 3)); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p31_60/p58/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p31_60.p58; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | import static org.junit.jupiter.api.Assertions.assertNull; 7 | 8 | public class SolutionTest { 9 | 10 | @Test 11 | public void testFindNumber() { 12 | assertEquals(4, new Solution().findNumber(new int[]{13, 18, 25, 2, 8, 10}, 8)); 13 | assertNull(new Solution().findNumber(new int[]{13, 18, 25, 2, 8, 10}, 11)); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p31_60/p60/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p31_60.p60; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertFalse; 6 | import static org.junit.jupiter.api.Assertions.assertTrue; 7 | 8 | public class SolutionTest { 9 | 10 | @Test 11 | public void testEqualSet() { 12 | assertTrue(new Solution().equalSumSubset(new int[]{15, 5, 20, 10, 35, 15, 10})); 13 | assertFalse(new Solution().equalSumSubset(new int[]{15, 5, 20, 10, 35})); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p61_90/p61/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p61_90.p61; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | class SolutionTest { 8 | 9 | @Test 10 | void testPower() { 11 | assertEquals(1024, new Solution().power(2, 10)); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p61_90/p62/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p61_90.p62; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | class SolutionTest { 8 | 9 | @Test 10 | void testEditDistance() { 11 | 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p61_90/p65/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p61_90.p65; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 6 | 7 | class SolutionTest { 8 | 9 | @Test 10 | void testSpiralPrint() { 11 | assertArrayEquals(new int[]{}, new Solution().printSpiral(new int[][]{})); 12 | assertArrayEquals(new int[]{1, 2, 3, 4, 5, 10, 15, 20, 19, 18, 17, 16, 11, 6, 7, 8, 9, 14, 13, 12}, new Solution().printSpiral(new int[][]{ 13 | {1, 2, 3, 4, 5}, 14 | {6, 7, 8, 9, 10}, 15 | {11, 12, 13, 14, 15}, 16 | {16, 17, 18, 19, 20}})); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p61_90/p66/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p61_90.p66; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | class SolutionTest { 8 | 9 | @Test 10 | void testFairCoin() { 11 | Solution solution = new Solution(); 12 | float heads = 0, tails = 0; 13 | for (int i = 0; i < 2; i++) { 14 | if (solution.fairCoin()) { 15 | heads++; 16 | } else { 17 | tails++; 18 | } 19 | } 20 | assertEquals(.5f, heads / (heads + tails)); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p61_90/p69/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p61_90.p69; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testMaxMultiplication() { 10 | assertEquals(-10 * -10 * 5, new Solution().maxMultiplication(new int[]{-10, -10, 5, 2})); 11 | assertEquals(-4 * -8 * 9, new Solution().maxMultiplication(new int[]{-4, 1, -8, 9, 6})); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p61_90/p70/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p61_90.p70; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testPerfectNumber() { 10 | assertEquals(19, new Solution().perfectNumber(1)); 11 | assertEquals(28, new Solution().perfectNumber(2)); 12 | assertEquals(55, new Solution().perfectNumber(5)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p61_90/p73/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p61_90.p73; 2 | 3 | import in.ashwanik.dcp.common.ListNode; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 7 | 8 | public class SolutionTest { 9 | @Test 10 | void testReverseList() { 11 | ListNode head = new ListNode<>(1, 12 | new ListNode<>(2, 13 | new ListNode<>(3, 14 | new ListNode<>(4, 15 | new ListNode<>(5))))); 16 | 17 | 18 | assertArrayEquals(new Integer[]{5, 4, 3, 2, 1}, new Solution().reverse(head).data(Integer.class)); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p61_90/p74/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p61_90.p74; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testCount() { 10 | assertEquals(4, new Solution().findCount(6, 12)); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p61_90/p77/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p61_90.p77; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import java.util.Arrays; 6 | import java.util.List; 7 | 8 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 9 | 10 | public class SolutionTest { 11 | @Test 12 | void testMergeInterval() { 13 | List intervals = Arrays.asList(new int[]{1, 3}, new int[]{5, 8}, new int[]{4, 10}, new int[]{20, 25}); 14 | 15 | List result = new Solution().mergeIntervals(intervals); 16 | assertArrayEquals(new int[]{1, 3}, result.get(0)); 17 | assertArrayEquals(new int[]{4, 10}, result.get(1)); 18 | assertArrayEquals(new int[]{20, 25}, result.get(2)); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p61_90/p79/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p61_90.p79; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertFalse; 6 | import static org.junit.jupiter.api.Assertions.assertTrue; 7 | 8 | public class SolutionTest { 9 | @Test 10 | void testCanSort() { 11 | 12 | assertTrue(new Solution().canBeSorted(new int[]{4, 2, 3})); 13 | assertFalse(new Solution().canBeSorted(new int[]{4, 2, 1})); 14 | 15 | assertTrue(new Solution().canBeSorted(new int[]{10, 5, 7})); 16 | assertFalse(new Solution().canBeSorted(new int[]{10, 5, 1})); 17 | 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p61_90/p80/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p61_90.p80; 2 | 3 | import in.ashwanik.dcp.common.TreeNode; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import static org.junit.jupiter.api.Assertions.assertEquals; 7 | 8 | public class SolutionTest { 9 | 10 | /* 11 | 12 | 15 13 | 10 20 14 | 8 15 | */ 16 | 17 | @Test 18 | void testDeepestNode() { 19 | TreeNode root = new TreeNode<>(15, 20 | new TreeNode<>(10, 21 | new TreeNode<>(8), 22 | null), 23 | new TreeNode<>(20)); 24 | 25 | assertEquals(8, new Solution().getDeepestNode(root).getData()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p61_90/p81/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p61_90.p81; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testCombination() { 10 | assertArrayEquals(new String[]{"ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"}, new Solution().getCombinations("23").toArray()); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p61_90/p83/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p61_90.p83; 2 | 3 | import in.ashwanik.dcp.common.TreeNode; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 7 | 8 | public class SolutionTest { 9 | 10 | @Test 11 | void testInvert() { 12 | TreeNode root = new TreeNode<>('a', 13 | new TreeNode<>('b', 14 | new TreeNode<>('d'), 15 | new TreeNode<>('e')), 16 | new TreeNode<>('c', 17 | new TreeNode<>('f'), 18 | null)); 19 | 20 | assertArrayEquals(new Character[]{'c', 'f', 'a', 'e', 'b', 'd'}, new Solution().invert(root).inorder().toArray()); 21 | 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p61_90/p84/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p61_90.p84; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testNumberOfIsland() { 10 | assertEquals(0, new Solution().numberOfIslands(new boolean[][]{})); 11 | assertEquals(4, new Solution().numberOfIslands(new boolean[][]{{true, false, false, false, false}, 12 | {false, false, true, true, false}, 13 | {false, true, true, false, false}, 14 | {false, false, false, false, false}, 15 | {true, true, false, false, true}, 16 | {true, true, false, false, true}})); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p61_90/p85/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p61_90.p85; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testGet() { 10 | assertEquals(5, new Solution().get(5, 4, 1)); 11 | assertEquals(4, new Solution().get(5, 4, 0)); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p61_90/p86/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p61_90.p86; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testNumberOfParentheses() { 10 | assertEquals(1, new Solution().minimumNumberOfParanthesisToRemove("()())()")); 11 | assertEquals(2, new Solution().minimumNumberOfParanthesisToRemove(")(")); 12 | assertEquals(4, new Solution().minimumNumberOfParanthesisToRemove("(((((()))(")); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p61_90/p88/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p61_90.p88; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testDivision() { 10 | assertEquals(2, new Solution().divideWithoutDivision(10, 5)); 11 | assertEquals(3, new Solution().divideWithoutDivision(16, 5)); 12 | assertEquals(-5, new Solution().divideWithoutDivision(27, -5)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p61_90/p90/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p61_90.p90; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertTrue; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testRandom() { 10 | Solution solution = new Solution(1000000000, new int[]{640145908}); 11 | 12 | assertTrue(solution.pick() != 640145908); 13 | 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p91_120/p100/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p91_120.p100; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testMinimumDistance() { 10 | assertEquals(2, new Solution().getMinimumDistance(new int[][]{{0, 0}, {1, 1}, {1, 2}})); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p91_120/p101/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p91_120.p101; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testPrimePair() { 10 | assertArrayEquals(new int[]{3, 5}, new Solution().findPrimeSumPair(8)); 11 | assertArrayEquals(new int[]{3, 71}, new Solution().findPrimeSumPair(74)); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p91_120/p102/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p91_120.p102; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testSumSubArray() { 10 | assertArrayEquals(new int[]{2, 3, 4}, new Solution().getContinuousArray(new int[]{1, 2, 3, 4, 5}, 9)); 11 | assertArrayEquals(new int[]{1, 2, 3, 4, 5}, new Solution().getContinuousArray(new int[]{1, 2, 3, 4, 5}, 15)); 12 | assertArrayEquals(new int[]{}, new Solution().getContinuousArray(new int[]{1, 2, 3, 4, 5}, 20)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p91_120/p103/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p91_120.p103; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testMinimumString() { 10 | assertEquals("aeci", new Solution().getSmallestSubstring("figehaeci", "aei")); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p91_120/p107/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p91_120.p107; 2 | 3 | import in.ashwanik.dcp.common.TreeNode; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 7 | 8 | public class SolutionTest { 9 | @Test 10 | void testLevelOrder() { 11 | TreeNode root = new TreeNode<>(1, 12 | new TreeNode<>(2), 13 | new TreeNode<>(3, 14 | new TreeNode<>(4), 15 | new TreeNode<>(5))); 16 | 17 | assertArrayEquals(new int[]{1, 2, 3, 4, 5}, new Solution().levelOrder(root).stream().mapToInt(x -> x).toArray()); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p91_120/p108/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p91_120.p108; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertFalse; 6 | import static org.junit.jupiter.api.Assertions.assertTrue; 7 | 8 | public class SolutionTest { 9 | @Test 10 | void testIsRotated() { 11 | assertTrue(new Solution().isRotated("abcde", "cdeab")); 12 | assertFalse(new Solution().isRotated("abc", "acb")); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p91_120/p111/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p91_120.p111; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testAnagramIndices() { 10 | assertArrayEquals(new int[]{0, 3, 4}, new Solution().getIndices("abxaba", "ab").stream().mapToInt(x -> x).toArray()); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p91_120/p112/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p91_120.p112; 2 | 3 | import in.ashwanik.dcp.common.TreeNode; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import static org.junit.jupiter.api.Assertions.assertEquals; 7 | 8 | public class SolutionTest { 9 | 10 | @Test 11 | void testLCA() { 12 | 13 | TreeNode root = new TreeNode<>(1 14 | , new TreeNode<>(2, new TreeNode<>(4), new TreeNode<>(5)) 15 | , new TreeNode<>(3, new TreeNode<>(6), new TreeNode<>(7))); 16 | 17 | assertEquals(2, new Solution().getLCA(root, 4, 5).getData()); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p91_120/p113/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p91_120.p113; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | import static org.junit.jupiter.api.Assertions.assertNull; 7 | 8 | public class SolutionTest { 9 | @Test 10 | void testReverseSentence() { 11 | assertEquals("", new Solution().reverseWords("")); 12 | assertNull(new Solution().reverseWords(null)); 13 | assertEquals("here world hello", new Solution().reverseWords("hello world here")); 14 | assertEquals("here world hello ", new Solution().reverseWords(" hello world here")); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p91_120/p114/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p91_120.p114; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import java.util.HashSet; 6 | import java.util.Set; 7 | 8 | import static org.junit.jupiter.api.Assertions.assertEquals; 9 | 10 | public class SolutionTest { 11 | @Test 12 | void testReverseStringWithDelimiter() { 13 | Set set = new HashSet<>(); 14 | set.add('/'); 15 | set.add(':'); 16 | assertEquals("here/world:hello", new Solution().reverseWordsWithDelimiters("hello/world:here", set)); 17 | assertEquals("here/world:hello/", new Solution().reverseWordsWithDelimiters("hello/world:here/", set)); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p91_120/p115/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p91_120.p115; 2 | 3 | import in.ashwanik.dcp.common.TreeNode; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import static org.junit.jupiter.api.Assertions.assertTrue; 7 | 8 | public class SolutionTest { 9 | @Test 10 | void testIsSubTree() { 11 | TreeNode root = new TreeNode<>(1, 12 | new TreeNode<>(2), 13 | new TreeNode<>(3, 14 | new TreeNode<>(4), 15 | new TreeNode<>(5))); 16 | 17 | assertTrue(new Solution().isSubTree(root, root.getRight())); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p91_120/p116/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p91_120.p116; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertNotNull; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testGenerate() { 10 | assertNotNull(new Solution().generate()); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p91_120/p117/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p91_120.p117; 2 | 3 | import in.ashwanik.dcp.common.TreeNode; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import static org.junit.jupiter.api.Assertions.assertEquals; 7 | 8 | public class SolutionTest { 9 | @Test 10 | void testMinimumSumLevel() { 11 | TreeNode root = new TreeNode<>(2, 12 | new TreeNode<>(3, 13 | new TreeNode<>(-5), 14 | new TreeNode<>(4)), 15 | new TreeNode<>(-2)); 16 | 17 | assertEquals(2, new Solution().getLevelWithMinimumSum(root)); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p91_120/p118/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p91_120.p118; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testSortArray() { 10 | assertArrayEquals(new int[]{}, new Solution().sortArray(new int[]{})); 11 | assertArrayEquals(new int[]{0, 4, 4, 9, 81}, new Solution().sortArray(new int[]{-9, -2, 0, 2, 3})); 12 | assertArrayEquals(new int[]{0, 4, 4, 9, 81}, new Solution().sortArray(new int[]{0, 2, 2, 3, 9})); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p91_120/p119/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p91_120.p119; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testMinimumSet() { 10 | assertArrayEquals(new int[]{3, 6}, new Solution().minimumIntervalSet(new int[][]{{0, 3}, {2, 6}, {3, 4}, {6, 9}})); 11 | assertArrayEquals(new int[]{10, 10}, new Solution().minimumIntervalSet(new int[][]{{10, 20}, {10, -20}})); 12 | 13 | } 14 | } -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p91_120/p120/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p91_120.p120; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertFalse; 6 | import static org.junit.jupiter.api.Assertions.assertTrue; 7 | 8 | public class SolutionTest { 9 | 10 | @Test 11 | void testSingleton() { 12 | assertFalse(Solution.getInstance().isOdd()); 13 | assertTrue(Solution.getInstance().isOdd()); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p91_120/p94/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p91_120.p94; 2 | 3 | import in.ashwanik.dcp.common.TreeNode; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import static org.junit.jupiter.api.Assertions.assertEquals; 7 | 8 | public class SolutionTest { 9 | @Test 10 | void testMaximumPathSum() { 11 | TreeNode root = new TreeNode<>(10, 12 | new TreeNode<>(2, 13 | new TreeNode<>(20), 14 | new TreeNode<>(1)), 15 | new TreeNode<>(10, 16 | null, 17 | new TreeNode<>(-25, 18 | new TreeNode<>(3), 19 | new TreeNode<>(4)))); 20 | 21 | assertEquals(42, new Solution().maximumPathSum(root)); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/test/java/in/ashwanik/dcp/problems/p91_120/p99/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package in.ashwanik.dcp.problems.p91_120.p99; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class SolutionTest { 8 | @Test 9 | void testLength() { 10 | assertEquals(4, new Solution().getLongestConsecutiveSequenceLength(new int[]{100, 4, 200, 1, 3, 2})); 11 | } 12 | } 13 | --------------------------------------------------------------------------------