├── .gitignore ├── .gitmodules ├── LICENSE ├── README.md ├── REPEATING.md └── java ├── README.md ├── daily-coding-problem ├── pom.xml ├── src │ └── main │ │ └── java │ │ └── com │ │ └── vaani │ │ └── dcp │ │ ├── airbnb │ │ ├── _092 │ │ │ ├── #092.pdf │ │ │ └── Solution.java │ │ ├── _101 │ │ │ └── #101.pdf │ │ ├── _167 │ │ │ ├── #167.pdf │ │ │ └── Solution.java │ │ ├── _177 │ │ │ ├── #177.pdf │ │ │ └── Solution.java │ │ └── hard │ │ │ └── _009 │ │ │ ├── #009.pdf │ │ │ └── Solution.java │ │ ├── alibaba │ │ └── _101 │ │ │ └── Solution.java │ │ ├── amazon │ │ ├── _084 │ │ │ ├── #084.pdf │ │ │ └── Solution.java │ │ ├── _133 │ │ │ ├── #133.pdf │ │ │ └── Solution.java │ │ ├── _137 │ │ │ ├── #137.pdf │ │ │ └── Solution.java │ │ ├── _143 │ │ │ ├── #143.pdf │ │ │ └── Solution.java │ │ ├── _154 │ │ │ ├── #154.pdf │ │ │ └── Solution.java │ │ ├── _157 │ │ │ ├── #157.pdf │ │ │ └── Solution.java │ │ ├── _171 │ │ │ ├── #171.pdf │ │ │ └── Solution.java │ │ ├── _184 │ │ │ ├── #184.pdf │ │ │ └── Solution.java │ │ ├── _197 │ │ │ ├── #197.pdf │ │ │ └── Solution.java │ │ ├── _237 │ │ │ ├── #237.pdf │ │ │ └── Solution.java │ │ ├── easy │ │ │ ├── _029 │ │ │ │ ├── #029.pdf │ │ │ │ └── Solution.java │ │ │ ├── _043 │ │ │ │ ├── #043.pdf │ │ │ │ └── Solution.java │ │ │ └── _065 │ │ │ │ ├── #065.pdf │ │ │ │ └── Solution.java │ │ ├── hard │ │ │ ├── _012 │ │ │ │ ├── #012.pdf │ │ │ │ └── Solution.java │ │ │ ├── _013 │ │ │ │ ├── #013.pdf │ │ │ │ └── Solution.java │ │ │ └── _046 │ │ │ │ ├── #046.pdf │ │ │ │ └── Solution.java │ │ └── medium │ │ │ ├── _049 │ │ │ ├── #049.pdf │ │ │ └── Solution.java │ │ │ ├── _057 │ │ │ ├── #057.pdf │ │ │ └── Solution.java │ │ │ └── _058 │ │ │ ├── #058.pdf │ │ │ └── Solution.java │ │ ├── apple │ │ ├── _093 │ │ │ └── Solution.java │ │ ├── _135 │ │ │ ├── #135.pdf │ │ │ └── Solution.java │ │ ├── _148 │ │ │ ├── #148.pdf │ │ │ └── Solution.java │ │ ├── _196 │ │ │ ├── #196.pdf │ │ │ └── Solution.java │ │ ├── _233 │ │ │ ├── #233.pdf │ │ │ └── Solution.java │ │ └── medium │ │ │ ├── _010 │ │ │ ├── #010.pdf │ │ │ └── Solution.java │ │ │ └── _053 │ │ │ ├── #053.pdf │ │ │ └── Solution.java │ │ ├── bloomberg │ │ └── _176 │ │ │ ├── #176.pdf │ │ │ └── Solution.java │ │ ├── bufferbox │ │ └── _146 │ │ │ ├── #146.pdf │ │ │ └── Solution.java │ │ ├── cisco │ │ └── _109 │ │ │ ├── #109.pdf │ │ │ └── Solution.java │ │ ├── coursera │ │ └── _098 │ │ │ ├── #098.pdf │ │ │ └── Solution.java │ │ ├── dropbox │ │ └── _246 │ │ │ ├── #246.pdf │ │ │ └── Solution.java │ │ ├── facebook │ │ ├── _085 │ │ │ ├── #085.pdf │ │ │ └── Solution.java │ │ ├── _110 │ │ │ ├── #110.pdf │ │ │ └── Solution.java │ │ ├── _117 │ │ │ ├── #117.pdf │ │ │ └── Solution.java │ │ ├── _126 │ │ │ ├── #126.pdf │ │ │ └── Solution.java │ │ ├── _130 │ │ │ └── #130.pdf │ │ ├── _134 │ │ │ ├── #134.pdf │ │ │ └── Solution.java │ │ ├── _156 │ │ │ ├── #156.pdf │ │ │ └── Solution.java │ │ ├── _161 │ │ │ ├── #161.pdf │ │ │ └── Solution.java │ │ ├── _168 │ │ │ ├── #168.pdf │ │ │ └── Solution.java │ │ ├── _170 │ │ │ ├── #170.pdf │ │ │ └── Solution.java │ │ ├── _182 │ │ │ ├── #182.pdf │ │ │ └── Solution.java │ │ ├── _190 │ │ │ ├── #190.pdf │ │ │ └── Solution.java │ │ ├── _199 │ │ │ ├── #199.pdf │ │ │ └── Solution.java │ │ ├── easy │ │ │ ├── _027 │ │ │ │ ├── #027.pdf │ │ │ │ └── Solution.java │ │ │ ├── _047 │ │ │ │ ├── #047.pdf │ │ │ │ └── Solution.java │ │ │ └── _069 │ │ │ │ ├── #069.pdf │ │ │ │ └── Solution.java │ │ └── medium │ │ │ ├── _007 │ │ │ ├── #007.pdf │ │ │ └── Solution.java │ │ │ ├── _015 │ │ │ └── #015.pdf │ │ │ ├── _062 │ │ │ ├── #062.pdf │ │ │ └── Solution.java │ │ │ └── _079 │ │ │ ├── #079.pdf │ │ │ └── Solution.java │ │ ├── goldmansachs │ │ └── _149 │ │ │ ├── #149.pdf │ │ │ └── Solution.java │ │ ├── google │ │ ├── _083 │ │ │ ├── #083.pdf │ │ │ └── Solution.java │ │ ├── _086 │ │ │ ├── #086.pdf │ │ │ └── Solution.java │ │ ├── _100 │ │ │ ├── #100.pdf │ │ │ └── Solution.java │ │ ├── _104 │ │ │ ├── #104.pdf │ │ │ └── Solution.java │ │ ├── _108 │ │ │ ├── #108.pdf │ │ │ └── Solution.java │ │ ├── _111 │ │ │ ├── #111.pdf │ │ │ └── Solution.java │ │ ├── _113 │ │ │ ├── #113.pdf │ │ │ └── Solution.java │ │ ├── _115 │ │ │ ├── #115.pdf │ │ │ └── Solution.java │ │ ├── _118 │ │ │ ├── #118.pdf │ │ │ └── Solution.java │ │ ├── _119 │ │ │ ├── #119.pdf │ │ │ └── Solution.java │ │ ├── _125 │ │ │ ├── #125.pdf │ │ │ └── Solution.java │ │ ├── _136 │ │ │ ├── #136.pdf │ │ │ └── Solution.java │ │ ├── _138 │ │ │ ├── #138.pdf │ │ │ └── Solution.java │ │ ├── _139 │ │ │ ├── #139.pdf │ │ │ └── Solution.java │ │ ├── _142 │ │ │ ├── #142.pdf │ │ │ └── Solution.java │ │ ├── _144 │ │ │ ├── #144.pdf │ │ │ └── Solution.java │ │ ├── _145 │ │ │ ├── #145.pdf │ │ │ └── Solution.java │ │ ├── _159 │ │ │ ├── #159.pdf │ │ │ └── Solution.java │ │ ├── _164 │ │ │ ├── #164.pdf │ │ │ └── Solution.java │ │ ├── _165 │ │ │ ├── #165.pdf │ │ │ └── Solution.java │ │ ├── _180 │ │ │ ├── #180.pdf │ │ │ └── Solution.java │ │ ├── _181 │ │ │ ├── #181.pdf │ │ │ └── Solution.java │ │ ├── _189 │ │ │ ├── #189.pdf │ │ │ └── Solution.java │ │ ├── _192 │ │ │ ├── #192.pdf │ │ │ └── Solution.java │ │ ├── _195 │ │ │ ├── #192.pdf │ │ │ └── Solution.java │ │ ├── _198 │ │ │ ├── #198.pdf │ │ │ └── Solution.java │ │ ├── _201 │ │ │ ├── #201.pdf │ │ │ └── Solution.java │ │ ├── _232 │ │ │ ├── #232.pdf │ │ │ └── Solution.java │ │ ├── easy │ │ │ ├── _001 │ │ │ │ ├── #001.pdf │ │ │ │ └── Solution.java │ │ │ ├── _008 │ │ │ │ ├── #008.pdf │ │ │ │ └── Solution.java │ │ │ ├── _020 │ │ │ │ ├── #020.pdf │ │ │ │ └── Solution.java │ │ │ ├── _023 │ │ │ │ ├── #023.pdf │ │ │ │ └── Solution.java │ │ │ ├── _031 │ │ │ │ ├── #031.pdf │ │ │ │ └── Solution.java │ │ │ ├── _037 │ │ │ │ └── Solution.java │ │ │ ├── _073 │ │ │ │ ├── #073.pdf │ │ │ │ └── Solution.java │ │ │ └── _080 │ │ │ │ ├── #080.pdf │ │ │ │ └── Solution.java │ │ ├── hard │ │ │ ├── _006 │ │ │ │ └── #006.pdf │ │ │ ├── _017 │ │ │ │ ├── #017.pdf │ │ │ │ └── Solution.java │ │ │ ├── _018 │ │ │ │ ├── #018.pdf │ │ │ │ └── Solution.java │ │ │ ├── _035 │ │ │ │ ├── #035.pdf │ │ │ │ └── Solution.java │ │ │ ├── _042 │ │ │ │ ├── #042.pdf │ │ │ │ └── Solution.java │ │ │ ├── _052 │ │ │ │ ├── #052.pdf │ │ │ │ └── Solution.java │ │ │ └── _067 │ │ │ │ ├── #067.pdf │ │ │ │ └── Solution.java │ │ └── medium │ │ │ ├── _003 │ │ │ ├── #003.pdf │ │ │ └── Solution.java │ │ │ ├── _014 │ │ │ └── #014.pdf │ │ │ ├── _024 │ │ │ ├── #024.pdf │ │ │ └── Solution.java │ │ │ ├── _026 │ │ │ ├── #026.pdf │ │ │ └── Solution.java │ │ │ ├── _078 │ │ │ ├── #078.pdf │ │ │ └── Solution.java │ │ │ └── _596 │ │ │ └── Solution.java │ │ ├── ibm │ │ └── _231 │ │ │ ├── #231.pdf │ │ │ └── Solution.java │ │ ├── janestreet │ │ ├── _116 │ │ │ ├── #116.pdf │ │ │ └── Solution.java │ │ ├── _163 │ │ │ ├── #163.pdf │ │ │ └── Solution.java │ │ └── medium │ │ │ └── _005 │ │ │ ├── #005.pdf │ │ │ └── Solution.java │ │ ├── linkedin │ │ ├── _089 │ │ │ ├── #089.pdf │ │ │ └── Solution.java │ │ ├── _123 │ │ │ ├── #123.pdf │ │ │ └── Solution.java │ │ └── _150 │ │ │ ├── #150.pdf │ │ │ └── Solution.java │ │ ├── lyft │ │ └── _102 │ │ │ ├── #102.pdf │ │ │ └── Solution.java │ │ ├── microsoft │ │ ├── _096 │ │ │ ├── #096.pdf │ │ │ └── Solution.java │ │ ├── _099 │ │ │ ├── #099.pdf │ │ │ └── Solution.java │ │ ├── _107 │ │ │ ├── #107.pdf │ │ │ └── Solution.java │ │ ├── _120 │ │ │ ├── #120.pdf │ │ │ └── Solution.java │ │ ├── _127 │ │ │ ├── #127.pdf │ │ │ └── Solution.java │ │ ├── _200 │ │ │ ├── #200.pdf │ │ │ └── Solution.java │ │ ├── easy │ │ │ ├── _050 │ │ │ │ ├── #050.pdf │ │ │ │ └── Solution.java │ │ │ ├── _055 │ │ │ │ ├── #055.pdf │ │ │ │ └── Solution.java │ │ │ └── _063 │ │ │ │ ├── #063.pdf │ │ │ │ └── Solution.java │ │ ├── hard │ │ │ └── _075 │ │ │ │ ├── #075.pdf │ │ │ │ └── Solution.java │ │ └── medium │ │ │ └── _022 │ │ │ ├── #022.pdf │ │ │ └── Solution.java │ │ ├── mongodb │ │ └── _155 │ │ │ ├── #155.pdf │ │ │ └── Solution.java │ │ ├── others │ │ ├── _129 │ │ │ ├── #129.pdf │ │ │ └── Solution.java │ │ ├── _147 │ │ │ ├── #147.pdf │ │ │ └── Solution.java │ │ ├── _151 │ │ │ ├── #151.pdf │ │ │ └── Solution.java │ │ └── _153 │ │ │ ├── #153.pdf │ │ │ └── Solution.java │ │ ├── palantir │ │ ├── _095 │ │ │ ├── #095.pdf │ │ │ └── Solution.java │ │ ├── _202 │ │ │ ├── #202.pdf │ │ │ └── Solution.java │ │ ├── _241 │ │ │ ├── #241.pdf │ │ │ └── Solution.java │ │ └── easy │ │ │ └── _028 │ │ │ ├── #028.pdf │ │ │ └── Solution.java │ │ ├── pinterest │ │ └── _106 │ │ │ ├── #106.pdf │ │ │ └── Solution.java │ │ ├── riotgames │ │ └── _132 │ │ │ ├── #132.pdf │ │ │ └── Solution.java │ │ ├── slack │ │ └── _158 │ │ │ ├── #158.pdf │ │ │ └── Solution.java │ │ ├── snapchat │ │ ├── _131 │ │ │ ├── #131.pdf │ │ │ └── Solution.java │ │ └── easy │ │ │ ├── _021 │ │ │ ├── #021.pdf │ │ │ └── Solution.java │ │ │ └── _077 │ │ │ ├── #077.pdf │ │ │ └── Solution.java │ │ ├── square │ │ ├── _103 │ │ │ ├── #103.pdf │ │ │ └── Solution.java │ │ ├── _162 │ │ │ ├── #162.pdf │ │ │ └── Solution.java │ │ └── _244 │ │ │ ├── #244.pdf │ │ │ └── Solution.java │ │ ├── stripe │ │ ├── _097 │ │ │ ├── #097.pdf │ │ │ └── Solution.java │ │ ├── _173 │ │ │ ├── #173.pdf │ │ │ └── Solution.java │ │ ├── _191 │ │ │ ├── #191.pdf │ │ │ └── Solution.java │ │ └── hard │ │ │ └── _004 │ │ │ ├── #004.pdf │ │ │ └── Solution.java │ │ ├── twitter │ │ ├── _112 │ │ │ ├── #112.pdf │ │ │ └── Solution.java │ │ ├── easy │ │ │ └── _016 │ │ │ │ ├── #016.pdf │ │ │ │ └── Solution.java │ │ └── medium │ │ │ └── _011 │ │ │ ├── #011.pdf │ │ │ └── Solution.java │ │ ├── uber │ │ ├── _160 │ │ │ ├── #160.pdf │ │ │ └── Solution.java │ │ ├── _166 │ │ │ ├── #166.pdf │ │ │ └── Solution.java │ │ ├── _203 │ │ │ ├── #203.pdf │ │ │ └── Solution.java │ │ └── hard │ │ │ └── _002 │ │ │ ├── #002.pdf │ │ │ └── Solution.java │ │ ├── yelp │ │ └── _081 │ │ │ ├── #081.pdf │ │ │ └── Solution.java │ │ └── zillow │ │ └── _122 │ │ ├── #122.pdf │ │ └── Solution.java └── target │ └── classes │ └── META-INF │ └── daily-coding-problem.kotlin_module └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | hs_err_pid* 24 | 25 | # idea files 26 | .idea/ 27 | *.iml 28 | .DS_Store 29 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "java/algorithms-java"] 2 | path = java/algorithms-java 3 | url = git@github.com:kinshuk4/algorithms-java.git 4 | branch = master 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Kinshuk 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /java/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | Create submodule: 4 | ```bash 5 | git submodule add -b master git@github.com:kinshuk4/algorithms-java.git 6 | ``` -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/airbnb/_092/#092.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/airbnb/_092/#092.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/airbnb/_092/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.airbnb._092; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.Collections; 6 | import java.util.HashMap; 7 | import java.util.HashSet; 8 | import java.util.List; 9 | import java.util.Map; 10 | import java.util.Set; 11 | 12 | /** 13 | * 14 | * */ 15 | public class Solution { 16 | 17 | public static void main(String... args) { 18 | System.out.println(String.join(" ", sort(create()))); 19 | } 20 | 21 | private static Map> create() { 22 | Map> map = new HashMap<>(); 23 | map.put("CSC300", new HashSet<>(Arrays.asList("CSC100", "CSC200"))); 24 | map.put("CSC200", new HashSet<>(Collections.singletonList("CSC100"))); 25 | map.put("CSC100", Collections.emptySet()); 26 | return map; 27 | } 28 | 29 | public static List sort(Map> map) { 30 | Set visited = new HashSet<>(); 31 | List res = new ArrayList<>(); 32 | 33 | for (String courseId : map.keySet()) 34 | dfs(courseId, map, visited, res); 35 | 36 | return res; 37 | } 38 | 39 | private static void dfs(String courseId, Map> map, Set visited, List res) { 40 | if (visited.contains(courseId)) 41 | return; 42 | 43 | visited.add(courseId); 44 | 45 | for (String id : map.getOrDefault(courseId, Collections.emptySet())) 46 | dfs(id, map, visited, res); 47 | 48 | res.add(courseId); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/airbnb/_101/#101.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/airbnb/_101/#101.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/airbnb/_167/#167.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/airbnb/_167/#167.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/airbnb/_167/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.airbnb._167; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * 8 | * */ 9 | public class Solution { 10 | 11 | public static void main(String... args) { 12 | String[] words = { "code", "edoc", "da", "d" }; 13 | List pairs = findAllPalindromePair(words); 14 | pairs.forEach(pair -> System.out.println("(" + pair.i + ", " + pair.j + ')')); 15 | } 16 | 17 | public static List findAllPalindromePair(String[] words) { 18 | List res = new ArrayList<>(); 19 | 20 | for (int i = 0; i < words.length; i++) { 21 | for (int j = i + 1; j < words.length; j++) { 22 | if (isPalindrome(words[i] + words[j])) 23 | res.add(new Pair(i, j)); 24 | if (isPalindrome(words[j] + words[i])) 25 | res.add(new Pair(j, i)); 26 | } 27 | } 28 | 29 | return res; 30 | } 31 | 32 | private static boolean isPalindrome(String word) { 33 | for (int i = 0, j = word.length() - 1; i < j; i++, j--) 34 | if (word.charAt(i) != word.charAt(j)) 35 | return false; 36 | 37 | return true; 38 | } 39 | 40 | public static final class Pair { 41 | private final int i; 42 | private final int j; 43 | 44 | public Pair(int i, int j) { 45 | this.i = i; 46 | this.j = j; 47 | } 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/airbnb/_177/#177.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/airbnb/_177/#177.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/airbnb/hard/_009/#009.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/airbnb/hard/_009/#009.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/airbnb/hard/_009/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.airbnb.hard._009; 2 | 3 | /** 4 | * 5 | * 6 | */ 7 | public class Solution { 8 | 9 | public static void main(String... args) { 10 | System.out.println(largesNonAdjacentNumbersSumb(new int[] { 2, 4, 6, 2, 5 })); // 13 11 | System.out.println(largesNonAdjacentNumbersSumb(new int[] { 5, 1, 1, 5 })); // 10 12 | System.out.println(largesNonAdjacentNumbersSumb(new int[] { 5, 1, -1, -1, 5 })); // 10 13 | } 14 | 15 | public static int largesNonAdjacentNumbersSumb(int[] arr) { 16 | if (arr == null || arr.length == 0) 17 | return 0; 18 | if (arr.length == 1) 19 | return arr[0]; 20 | if (arr.length == 2) 21 | return Math.max(arr[0], arr[1]); 22 | 23 | for (int i = 2; i < arr.length; i++) { 24 | if (i > 2) 25 | arr[i] = Math.max(arr[i - 2], arr[i - 3] + Math.max(0, arr[i])); 26 | else 27 | arr[i] = arr[i - 2] + Math.max(0, arr[i]); 28 | } 29 | 30 | return Math.max(arr[arr.length - 1], arr[arr.length - 2]); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/alibaba/_101/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.alibaba._101; 2 | 3 | /** 4 | * 5 | * 6 | */ 7 | public class Solution { 8 | 9 | public static void main(String... args) { 10 | System.out.println(getMinSum(4)); 11 | System.out.println(getMinSum(50)); 12 | } 13 | 14 | public static Pair getMinSum(int num) { 15 | for (int a = num / 2; a >= 2; a--) 16 | if (isPrime(a) && isPrime(num - a)) 17 | return new Pair(a, num - a); 18 | 19 | return null; 20 | } 21 | 22 | private static boolean isPrime(int num) { 23 | if (num < 2) 24 | return false; 25 | 26 | for (int i = 2, sqrt = (int)Math.sqrt(num); i <= sqrt; i++) 27 | if (num % i == 0) 28 | return false; 29 | 30 | return true; 31 | } 32 | 33 | private static final class Pair { 34 | private final int a; 35 | private final int b; 36 | 37 | private Pair(int a, int b) { 38 | this.a = a; 39 | this.b = b; 40 | } 41 | 42 | @Override 43 | public String toString() { 44 | return "[" + a + ',' + b + ']'; 45 | } 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/_084/#084.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/_084/#084.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/_133/#133.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/_133/#133.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/_137/#137.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/_137/#137.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/_137/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.amazon._137; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | BitArray bitArray = new BitArray(16); 10 | bitArray.set(5, true); 11 | bitArray.set(6, true); 12 | bitArray.set(7, true); 13 | bitArray.set(9, true); 14 | bitArray.set(13, true); 15 | bitArray.set(15, true); 16 | 17 | for (int i = 0; i < 16; i++) 18 | System.out.println(i + ":" + bitArray.get(i)); 19 | } 20 | 21 | public static final class BitArray { 22 | private static final int INDEX_LENGTH = 8; // size of int 23 | 24 | private final int[] arr; 25 | 26 | public BitArray(int size) { 27 | arr = new int[size / INDEX_LENGTH + (size % INDEX_LENGTH != 0 ? 1 : 0)]; 28 | } 29 | 30 | public void set(int i, boolean val) { 31 | if (val) 32 | arr[getArrayIndex(i)] |= getBitMask(i); 33 | else 34 | arr[getArrayIndex(i)] &= ~getBitMask(i); 35 | } 36 | 37 | public boolean get(int i) { 38 | return (arr[getArrayIndex(i)] & getBitMask(i)) != 0; 39 | } 40 | 41 | private static int getArrayIndex(int i) { 42 | return i / INDEX_LENGTH; 43 | } 44 | 45 | private static int getBitMask(int i) { 46 | return 1 << i % INDEX_LENGTH; 47 | } 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/_143/#143.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/_143/#143.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/_143/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.amazon._143; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * 7 | * */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | int[] arr = {9, 12, 3, 5, 14, 10, 10}; 12 | System.out.println(Arrays.toString(arr)); 13 | System.out.println(Arrays.toString(partition(arr, 10))); 14 | } 15 | 16 | public static int[] partition(int[] arr, int x) { 17 | for (int i = 0, lt = 0, gt = arr.length - 1; i < gt; ) { 18 | if (arr[i] < x) 19 | swap(arr, lt++, i++); 20 | else if (arr[i] > x) 21 | swap(arr, gt--, i); 22 | else 23 | i++; 24 | } 25 | 26 | return arr; 27 | } 28 | 29 | private static void swap(int[] arr, int i, int j) { 30 | int tmp = arr[i]; 31 | arr[i] = arr[j]; 32 | arr[j] = tmp; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/_154/#154.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/_154/#154.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/_154/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.amazon._154; 2 | 3 | import java.util.Arrays; 4 | import java.util.PriorityQueue; 5 | import java.util.Queue; 6 | 7 | /** 8 | * 9 | * */ 10 | public class Solution { 11 | 12 | public static void main(String... args) { 13 | int[] arr = { 9, 12, 3, 5, 14, 10, 10 }; 14 | System.out.println(Arrays.toString(arr)); 15 | 16 | Stack stack = new Stack<>(); 17 | 18 | for (int item : arr) 19 | stack.push(item); 20 | 21 | for (int i = 0; i < arr.length; i++) 22 | System.out.println(stack.pop()); 23 | } 24 | 25 | public static final class Stack { 26 | private final Queue> maxHeap = new PriorityQueue<>(); 27 | private int size; 28 | 29 | public void push(T item) { 30 | maxHeap.add(new Node(size++, item)); 31 | } 32 | 33 | public T pop() { 34 | if (size == 0) 35 | throw new RuntimeException(); 36 | 37 | size--; 38 | return maxHeap.remove().item; 39 | } 40 | 41 | public static final class Node implements Comparable> { 42 | private final int pos; 43 | private final T item; 44 | 45 | public Node(int pos, T item) { 46 | this.pos = pos; 47 | this.item = item; 48 | } 49 | 50 | @Override 51 | public int compareTo(Node obj) { 52 | return Integer.compare(obj.pos, pos); 53 | } 54 | } 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/_157/#157.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/_157/#157.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/_157/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.amazon._157; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * 8 | * */ 9 | public class Solution { 10 | 11 | public static void main(String... args) { 12 | System.out.println(isRearrangePalindrome("carrace")); // true 13 | System.out.println(isRearrangePalindrome("daily")); // false 14 | } 15 | 16 | public static boolean isRearrangePalindrome(String str) { 17 | Map map = new HashMap<>(); 18 | 19 | for (int i = 0; i < str.length(); i++) 20 | map.put(str.charAt(i), map.getOrDefault(str.charAt(i), 0) + 1); 21 | 22 | int noPair = 0; 23 | 24 | for (int count : map.values()) 25 | if (count % 2 != 0) 26 | noPair++; 27 | 28 | return noPair <= 1; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/_171/#171.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/_171/#171.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/_184/#184.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/_184/#184.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/_184/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.amazon._184; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | System.out.println(getGreatestCommonDenominator(new int[] { 42, 56, 14 })); // 14 10 | } 11 | 12 | public static int getGreatestCommonDenominator(int[] arr) { 13 | int min = getMin(arr); 14 | 15 | for (int i = 1, j = min / 2; i < j; i++) { 16 | int k = min / i; 17 | 18 | if (isAccepted(k, arr)) 19 | return k; 20 | } 21 | 22 | return Integer.MAX_VALUE; 23 | } 24 | 25 | private static int getMin(int[] arr) { 26 | int min = Integer.MAX_VALUE; 27 | 28 | for (int val : arr) 29 | min = Math.min(min, val); 30 | 31 | return min; 32 | } 33 | 34 | private static boolean isAccepted(int k, int[] arr) { 35 | for (int val : arr) 36 | if (val % k != 0) 37 | return false; 38 | return true; 39 | } 40 | 41 | 42 | } 43 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/_197/#197.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/_197/#197.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/_197/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.amazon._197; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * 7 | * */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | int[] arr = { 1, 2, 3, 4, 5 }; 12 | rotateRight(arr, 2); 13 | System.out.println(Arrays.toString(arr)); // { 4, 5, 1, 2, 3 } 14 | } 15 | 16 | public static void rotateRight(int[] arr, int k) { 17 | k %= arr.length; 18 | 19 | reverse(arr, 0, arr.length - 1); 20 | reverse(arr, 0, k - 1); 21 | reverse(arr, k, arr.length - 1); 22 | } 23 | 24 | private static void reverse(int[] arr, int l, int r) { 25 | while (l < r) 26 | swap(arr, l++, r--); 27 | } 28 | 29 | private static void swap(int[] arr, int i, int j) { 30 | int tmp = arr[i]; 31 | arr[i] = arr[j]; 32 | arr[j] = tmp; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/_237/#237.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/_237/#237.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/easy/_029/#029.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/easy/_029/#029.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/easy/_029/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.amazon.easy._029; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | System.out.println(encode("AAAABBBCCDAA")); // 4A3B2C1D2A 10 | System.out.println(decode("4A3B2C1D2A")); // AAAABBBCCDAA 11 | } 12 | 13 | public static String encode(String str) { 14 | StringBuilder buf = new StringBuilder(str.length()); 15 | char ch = '\0'; 16 | int length = 0; 17 | 18 | 19 | for (int i = 0; i < str.length(); i++) { 20 | if (ch == '\0' || ch == str.charAt(i)) { 21 | ch = str.charAt(i); 22 | length++; 23 | } else { 24 | buf.append(length).append(ch); 25 | ch = str.charAt(i); 26 | length = 1; 27 | } 28 | } 29 | 30 | if (ch != '\0') 31 | buf.append(length).append(ch); 32 | 33 | return buf.toString(); 34 | } 35 | 36 | public static String decode(String str) { 37 | StringBuilder buf = new StringBuilder(); 38 | 39 | for (int i = 0; i < str.length(); ) { 40 | int length = str.charAt(i++) - '0'; 41 | char ch = str.charAt(i++); 42 | 43 | for (int j = 0; j < length; j++) 44 | buf.append(ch); 45 | } 46 | 47 | return buf.toString(); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/easy/_043/#043.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/easy/_043/#043.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/easy/_043/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.amazon.easy._043; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * 7 | * */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | int[] arr = { 4, 3, 5, 9, 9, 9, 4, 5, 1, 9, 2, 7, 1, 1, 4, 1, 2, 2, 1, 9 }; 12 | System.out.println(Arrays.toString(arr)); 13 | 14 | Stack stack = new Stack(arr.length); 15 | 16 | for (int val : arr) { 17 | stack.push(val); 18 | System.out.print(stack.max() + ", "); 19 | } 20 | 21 | System.out.println(); 22 | 23 | for (int i = 0; i < arr.length; i++) { 24 | int max = stack.max(); 25 | int val = stack.pop(); 26 | System.out.print(val + ":" + max + " | "); 27 | } 28 | 29 | System.out.println(); 30 | } 31 | 32 | public static final class Stack { 33 | 34 | private final int[] values; 35 | private final int[] maxValues; 36 | private int top = -1; 37 | 38 | public Stack(int size) { 39 | values = new int[size]; 40 | maxValues = new int[size]; 41 | } 42 | 43 | public void push(int val) { 44 | if (top + 1 == values.length) 45 | throw new StackOverflowError(); 46 | 47 | top++; 48 | values[top] = val; 49 | maxValues[top] = Math.max(top == 0 ? Integer.MIN_VALUE : maxValues[top - 1], values[top]); 50 | } 51 | 52 | public int pop() { 53 | if (top == -1) 54 | throw new RuntimeException(); 55 | return values[top--]; 56 | } 57 | 58 | public int max() { 59 | if (top == -1) 60 | throw new RuntimeException(); 61 | return maxValues[top]; 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/easy/_065/#065.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/easy/_065/#065.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/hard/_012/#012.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/hard/_012/#012.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/hard/_012/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.amazon.hard._012; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * 7 | * */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | System.out.println(countUniqueWays(1, 1, 2)); // 1 12 | System.out.println(countUniqueWays(2, 1, 2)); // 2 13 | System.out.println(countUniqueWays(3, 1, 2)); // 3 14 | System.out.println(countUniqueWays(4, 1, 2)); // 5 15 | 16 | System.out.println(countUniqueWays(4, 1, 3, 5)); // 3 17 | } 18 | 19 | public static int countUniqueWays(int n, int... steps) { 20 | Arrays.sort(steps); 21 | return _countUniqueWays(n, 0, 0, steps); 22 | } 23 | 24 | private static int _countUniqueWays(int n, int cur, int res, int... steps) { 25 | if (cur == n) 26 | res++; 27 | 28 | if (cur < n) { 29 | for (int i = 0; i < steps.length; i++) 30 | res = _countUniqueWays(n, cur + steps[i], res, steps); 31 | } 32 | 33 | return res; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/hard/_013/#013.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/hard/_013/#013.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/hard/_013/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.amazon.hard._013; 2 | 3 | import java.util.HashMap; 4 | import java.util.HashSet; 5 | import java.util.Map; 6 | import java.util.Set; 7 | 8 | /** 9 | * 10 | * */ 11 | public class Solution { 12 | 13 | public static void main(String... args) { 14 | System.out.println(findLongestSubstring("abcba", 2)); // bcb 15 | } 16 | 17 | public static String findLongestSubstring(String str, int k) { 18 | Map> map = new HashMap<>(); 19 | String res = null; 20 | 21 | for (int i = 0, j = 0; j < str.length(); ) { 22 | if (map.containsKey(str.charAt(j))) 23 | map.get(str.charAt(j)).add(j++); 24 | else if (map.size() < k) { 25 | map.put(str.charAt(j), new HashSet<>()); 26 | map.get(str.charAt(j)).add(j++); 27 | } else { 28 | if (res == null || j - i + 1 > res.length()) 29 | res = str.substring(i, j); 30 | 31 | map.get(str.charAt(i)).remove(i); 32 | 33 | if (map.get(str.charAt(i)).isEmpty()) 34 | map.remove(str.charAt(i)); 35 | 36 | i++; 37 | } 38 | } 39 | 40 | return res; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/hard/_046/#046.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/hard/_046/#046.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/medium/_049/#049.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/medium/_049/#049.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/medium/_049/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.amazon.medium._049; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | System.out.println(findMaxSum(34, -50, 42, 14, -5, 86)); // 137 10 | System.out.println(findMaxSum(-5, -1, -8, -9)); // 0 11 | } 12 | 13 | public static int findMaxSum(int... arr) { 14 | int maxSum = 0; 15 | 16 | for (int i = 0, sum = 0; i < arr.length; i++) { 17 | maxSum = Math.max(maxSum, arr[i]); 18 | maxSum = Math.max(maxSum, sum = Math.max(sum + arr[i], 0)); 19 | } 20 | 21 | return maxSum; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/medium/_057/#057.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/medium/_057/#057.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/medium/_057/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.amazon.medium._057; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.List; 6 | 7 | /** 8 | * 9 | * */ 10 | public class Solution { 11 | 12 | public static void main(String... args) { 13 | List res = splitIntoWords("the quick brown fox jumps over the lazy dog", 10); 14 | System.out.println(res != null ? Arrays.toString(res.toArray(new String[0])) : null); 15 | } 16 | 17 | public static List splitIntoWords(String str, int k) { 18 | String[] words = str.split("\\s+"); 19 | List res = new ArrayList<>(words.length); 20 | StringBuilder buf = null; 21 | 22 | for (String word : words) { 23 | if (word.length() > k) 24 | return null; 25 | if (buf == null) 26 | buf = new StringBuilder(word); 27 | else if (buf.length() + word.length() + 1 <= k) 28 | buf.append(' ').append(word); 29 | else { 30 | res.add(buf.toString()); 31 | buf = new StringBuilder(word); 32 | } 33 | } 34 | 35 | if (buf != null) 36 | res.add(buf.toString()); 37 | 38 | return res; 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/medium/_058/#058.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/medium/_058/#058.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/amazon/medium/_058/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.amazon.medium._058; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | System.out.println(findIndexByValue(new int[] { 13, 18, 25, 2, 8, 10 }, 8)); // 4 10 | System.out.println(findIndexByValue(new int[] { 7, 8, 9, 1, 2, 3, 4, 5, 6 }, 8)); // 1 11 | } 12 | 13 | public static Integer findIndexByValue(int[] arr, int val) { 14 | return find(arr, val, 0, arr.length - 1); 15 | } 16 | 17 | private static Integer find(int[] arr, int val, int left, int right) { 18 | if (left == right) 19 | return arr[left] == val ? left : null; 20 | 21 | int mid = (left + right) / 2; 22 | 23 | if (arr[left] <= arr[mid] && arr[left] <= val && val <= arr[mid] 24 | || arr[mid + 1] <= arr[right] && (arr[mid + 1] > val || val > arr[right])) 25 | return find(arr, val, left, mid); 26 | return find(arr, val, mid + 1, right); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/apple/_093/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.apple._093; 2 | 3 | import org.apache.commons.lang3.NotImplementedException; 4 | 5 | /** 6 | * 7 | * */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | Node root = generate(); 12 | } 13 | 14 | public static int getLargestBstSize(Node node) { 15 | throw new NotImplementedException("Yet not implemented"); 16 | } 17 | 18 | private static boolean isBst(Node node) { 19 | if(node == null) 20 | return true; 21 | 22 | boolean bst = true; 23 | 24 | if(node.left != null) { 25 | bst &= isBst(node.left); 26 | } 27 | throw new NotImplementedException("Yet not implemented"); 28 | 29 | } 30 | 31 | private static Node generate() { 32 | return null; 33 | } 34 | 35 | public static final class Node { 36 | private final int val; 37 | private Node left; 38 | private Node right; 39 | private int size = -1; 40 | 41 | public Node(int val) { 42 | this.val = val; 43 | } 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/apple/_135/#135.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/apple/_135/#135.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/apple/_148/#148.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/apple/_148/#148.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/apple/_148/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.apple._148; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * 7 | * */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | int length = 3; 12 | Arrays.stream(grayCode(length)) 13 | .mapToObj(code -> String.format("%" + length + 's', Integer.toBinaryString(code)).replace(' ', '0')) 14 | .forEach(System.out::println); 15 | } 16 | 17 | public static int[] grayCode(int n) { 18 | return gray(0, n, new int[(int)Math.pow(2, n)]); 19 | } 20 | 21 | private static int[] gray(int curLength, int maxLength, int[] codes) { 22 | if (curLength > maxLength) 23 | return codes; 24 | if (curLength == 0) 25 | codes[0] = 0; 26 | else 27 | for (int i = 0, t = 1 << (curLength - 1); i < t; i++) 28 | codes[t + i] = codes[t - i - 1] + (1 << (curLength - 1)); 29 | 30 | return gray(curLength + 1, maxLength, codes); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/apple/_196/#196.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/apple/_196/#196.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/apple/_196/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.apple._196; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * 8 | * */ 9 | public class Solution { 10 | 11 | public static void main(String... args) { 12 | System.out.println(findMostFrequentSubtreeSum(createTree())); // 2 13 | } 14 | 15 | private static Node createTree() { 16 | Node five = new Node(5); 17 | five.left = new Node(2); 18 | five.right = new Node(-5); 19 | return five; 20 | } 21 | 22 | public static int findMostFrequentSubtreeSum(Node root) { 23 | Map sumCount = new HashMap<>(); 24 | getSubtreeSum(root, sumCount); 25 | return findMostFrequentKey(sumCount); 26 | } 27 | 28 | private static int getSubtreeSum(Node node, Map sumCount) { 29 | if (node == null) 30 | return 0; 31 | 32 | int sumLeft = getSubtreeSum(node.left, sumCount); 33 | int sumRight = getSubtreeSum(node.right, sumCount); 34 | int sum = sumLeft + sumRight + node.val; 35 | 36 | sumCount.put(sum, sumCount.getOrDefault(sum, 0) + 1); 37 | 38 | return sum; 39 | } 40 | 41 | private static int findMostFrequentKey(Map sumCount) { 42 | if (sumCount.isEmpty()) 43 | return 0; 44 | 45 | int res = sumCount.keySet().iterator().next(); 46 | 47 | 48 | for (int sum : sumCount.keySet()) 49 | if (sumCount.get(sum) > sumCount.get(res)) 50 | res = sum; 51 | 52 | return res; 53 | } 54 | 55 | public static final class Node { 56 | 57 | private final int val; 58 | private Node left; 59 | private Node right; 60 | 61 | public Node(int val) { 62 | this.val = val; 63 | } 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/apple/_233/#233.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/apple/_233/#233.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/apple/_233/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.apple._233; 2 | 3 | /** 4 | * 5 | * 6 | */ 7 | public class Solution { 8 | 9 | public static void main(String... args) { 10 | System.out.println(fib(0)); // 0 11 | System.out.println(fib(1)); // 1 12 | System.out.println(fib(2)); // 1 13 | System.out.println(fib(3)); // 2 14 | System.out.println(fib(4)); // 3 15 | System.out.println(fib(5)); // 5 16 | System.out.println(fib(6)); // 8 17 | System.out.println(fib(22)); // 17711 18 | } 19 | 20 | public static long fib(int n) { 21 | long x1 = 1; 22 | long x2 = 0; 23 | 24 | for(int i = 0; i < n; i++) { 25 | x2 = x1 + x2; 26 | x1 = x2 - x1; 27 | } 28 | 29 | return x2; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/apple/medium/_010/#010.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/apple/medium/_010/#010.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/apple/medium/_010/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.apple.medium._010; 2 | 3 | import java.util.concurrent.TimeUnit; 4 | 5 | /** 6 | * 7 | * */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | long fromTIme = System.currentTimeMillis(); 12 | new JobScheduler().task(() -> System.out.println(System.currentTimeMillis() - fromTIme), TimeUnit.SECONDS.toMillis(5)); 13 | System.out.println("-- done --"); 14 | } 15 | 16 | public static final class JobScheduler { 17 | 18 | public void task(Runnable func, long delay) { 19 | new Thread(() -> { 20 | try { 21 | Thread.sleep(delay); 22 | func.run(); 23 | } catch(InterruptedException e) { 24 | e.printStackTrace(); 25 | } 26 | }).start(); 27 | } 28 | 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/apple/medium/_053/#053.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/apple/medium/_053/#053.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/apple/medium/_053/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.apple.medium._053; 2 | 3 | import java.util.Deque; 4 | import java.util.LinkedList; 5 | 6 | /** 7 | * 8 | * */ 9 | public class Solution { 10 | 11 | public static void main(String... args) { 12 | Queue queue = new Queue<>(); 13 | 14 | queue.enqueue(1); 15 | queue.enqueue(2); 16 | queue.enqueue(3); 17 | queue.enqueue(4); 18 | System.out.println(queue.dequeue()); // 1 19 | System.out.println(queue.dequeue()); // 2 20 | 21 | queue.enqueue(5); 22 | queue.enqueue(6); 23 | queue.enqueue(7); 24 | queue.enqueue(8); 25 | 26 | System.out.println(queue.dequeue()); // 3 27 | System.out.println(queue.dequeue()); // 4 28 | System.out.println(queue.dequeue()); // 5 29 | System.out.println(queue.dequeue()); // 6 30 | System.out.println(queue.dequeue()); // 7 31 | System.out.println(queue.dequeue()); // 8 32 | } 33 | 34 | public static class Queue { 35 | private final Deque stackEnqueue = new LinkedList<>(); 36 | private final Deque stackDequeue = new LinkedList<>(); 37 | 38 | public void enqueue(V value) { 39 | stackEnqueue.push(value); 40 | } 41 | 42 | public V dequeue() { 43 | if (stackDequeue.isEmpty()) 44 | while (!stackEnqueue.isEmpty()) 45 | stackDequeue.push(stackEnqueue.remove()); 46 | 47 | return stackDequeue.isEmpty() ? null : stackDequeue.remove(); 48 | } 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/bloomberg/_176/#176.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/bloomberg/_176/#176.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/bloomberg/_176/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.bloomberg._176; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String[] args) { 9 | System.out.println(exists("abc", "bcd")); // true 10 | System.out.println(exists("foo", "bar")); // false 11 | } 12 | 13 | public static boolean exists(String one, String two) { 14 | if (one.length() != two.length()) 15 | return false; 16 | 17 | char[] arr = new char[26]; 18 | 19 | for (int i = 0; i < one.length(); i++) { 20 | char chOne = one.charAt(i); 21 | char chTwo = two.charAt(i); 22 | 23 | if (arr[chOne - 'a'] == '\0') 24 | arr[chOne - 'a'] = chTwo; 25 | 26 | if (arr[chOne - 'a'] != chTwo) 27 | return false; 28 | } 29 | 30 | return true; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/bufferbox/_146/#146.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/bufferbox/_146/#146.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/bufferbox/_146/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.bufferbox._146; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | Node root = createTree(); 10 | root = prune(root); 11 | } 12 | 13 | private static Node createTree() { 14 | Node one = new Node(1); 15 | one.left = new Node(0); 16 | one.right = new Node(0); 17 | 18 | Node two = new Node(0); 19 | two.left = one; 20 | two.right = new Node(0); 21 | 22 | Node root = new Node(0); 23 | root.left = new Node(1); 24 | root.right = two; 25 | 26 | return root; 27 | } 28 | 29 | public static Node prune(Node node) { 30 | if (node == null) 31 | return null; 32 | 33 | node.left = prune(node.left); 34 | node.right = prune(node.right); 35 | return node.data == 0 && node.left == null && node.right == null ? null : node; 36 | } 37 | 38 | private static final class Node { 39 | private final int data; 40 | private Node left; 41 | private Node right; 42 | 43 | public Node(int data) { 44 | this.data = data; 45 | } 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/cisco/_109/#109.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/cisco/_109/#109.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/cisco/_109/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.cisco._109; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | System.out.println(Integer.toBinaryString(swapEvenOddBIts(0b10101010))); 10 | System.out.println(Integer.toBinaryString(swapEvenOddBIts(0b11100010))); 11 | } 12 | 13 | private static int swapEvenOddBIts(int val) { 14 | return (val & 0b01010101) << 1 | (val & 0b10101010) >> 1; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/coursera/_098/#098.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/coursera/_098/#098.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/dropbox/_246/#246.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/dropbox/_246/#246.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/dropbox/_246/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.dropbox._246; 2 | 3 | import java.util.Deque; 4 | import java.util.LinkedList; 5 | 6 | /** 7 | * 8 | * */ 9 | public class Solution { 10 | 11 | public static void main(String... args) { 12 | System.out.println(isBalanced("([])[]({})")); // true 13 | System.out.println(isBalanced("([)]")); // false 14 | System.out.println(isBalanced("((()")); // false 15 | } 16 | 17 | public static boolean isBalanced(String str) { 18 | Deque stack = new LinkedList<>(); 19 | 20 | for (int i = 0; i < str.length(); i++) { 21 | char ch = str.charAt(i); 22 | 23 | if (ch == '(' || ch == '[' || ch == '{') 24 | stack.push(ch); 25 | else { 26 | if (stack.isEmpty()) 27 | return false; 28 | 29 | char prv = stack.pop(); 30 | 31 | if (prv == '(' && ch != ')') 32 | return false; 33 | if (prv == '[' && ch != ']') 34 | return false; 35 | if (prv == '{' && ch != '}') 36 | return false; 37 | } 38 | } 39 | 40 | return stack.isEmpty(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/_085/#085.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/_085/#085.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/_085/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.facebook._085; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | System.out.println(ifElse(4, 7, 1)); // 4 10 | System.out.println(ifElse(4, 7, 0)); // 7 11 | } 12 | 13 | public static int ifElse(int x, int y, int b) { 14 | return (x * b) | (y * (1 - b)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/_110/#110.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/_110/#110.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/_110/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.facebook._110; 2 | 3 | import java.util.*; 4 | 5 | /** 6 | * 7 | * */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | getLeavesPath(createTree()).forEach(path -> System.out.println(Arrays.toString(path))); // [[1, 2], [1, 3, 4], [1, 3, 5]] 12 | } 13 | 14 | private static Node createTree() { 15 | Node two = new Node(2); 16 | Node four = new Node(4); 17 | Node five = new Node(5); 18 | 19 | Node three = new Node(3); 20 | three.left = four; 21 | three.right = five; 22 | 23 | Node root = new Node(1); 24 | root.left = two; 25 | root.right = three; 26 | 27 | return root; 28 | } 29 | 30 | public static List getLeavesPath(Node root) { 31 | List res = new LinkedList<>(); 32 | dfs(root, new LinkedList<>(), res); 33 | return res; 34 | } 35 | 36 | private static void dfs(Node node, Deque stack, List res) { 37 | if (node == null) 38 | return; 39 | 40 | stack.push(node); 41 | dfs(node.left, stack, res); 42 | dfs(node.right, stack, res); 43 | 44 | if (node.left == null && node.right == null) { 45 | int[] path = new int[stack.size()]; 46 | int i = path.length; 47 | 48 | for (Node parent : new ArrayList<>(stack)) 49 | path[--i] = parent.data; 50 | 51 | res.add(path); 52 | } 53 | 54 | stack.pop(); 55 | } 56 | 57 | private static final class Node { 58 | private final int data; 59 | private Node left; 60 | private Node right; 61 | 62 | public Node(int data) { 63 | this.data = data; 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/_117/#117.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/_117/#117.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/_126/#126.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/_126/#126.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/_126/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.facebook._126; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * 7 | * */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | int[] arr = { 1, 2, 3, 4, 5, 6 }; 12 | System.out.println(Arrays.toString(arr)); // [1, 2, 3, 4, 5, 6] 13 | rotate(arr, 2); 14 | System.out.println(Arrays.toString(arr)); // [3, 4, 5, 6, 1, 2] 15 | } 16 | 17 | public static void rotate(int[] arr, int k) { 18 | if ((k %= arr.length) != 0) { 19 | k = k < 0 ? arr.length + k : k; 20 | swapSubArr(arr, 0, arr.length); 21 | swapSubArr(arr, 0, arr.length - k); 22 | swapSubArr(arr, arr.length - k, arr.length); 23 | } 24 | } 25 | 26 | private static void swapSubArr(int[] arr, int from, int to) { 27 | for (int i = from, j = to - 1; i < j; i++, j--) 28 | swap(arr, i, j); 29 | } 30 | 31 | private static void swap(int[] arr, int i, int j) { 32 | int tmp = arr[i]; 33 | arr[i] = arr[j]; 34 | arr[j] = tmp; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/_130/#130.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/_130/#130.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/_134/#134.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/_134/#134.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/_134/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.facebook._134; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * 8 | * */ 9 | public class Solution { 10 | 11 | public static void main(String... args) { 12 | } 13 | 14 | public static class SparseArray { 15 | private final Map map = new HashMap<>(); 16 | private int size; 17 | 18 | public void init(int[] arr, int size) { 19 | map.clear(); 20 | this.size = size; 21 | 22 | for (int i = 0; i < arr.length; i++) 23 | set(i, arr[i]); 24 | } 25 | 26 | public void set(int i, int val) { 27 | if (i < 0 || i >= size) 28 | throw new ArrayIndexOutOfBoundsException(); 29 | 30 | if (val != 0) 31 | map.put(i, val); 32 | } 33 | 34 | public int get(int i) { 35 | if (i < 0 || i >= size) 36 | throw new ArrayIndexOutOfBoundsException(); 37 | return map.getOrDefault(i, 0); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/_156/#156.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/_156/#156.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/_156/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.facebook._156; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | System.out.println(findSmallest(13)); // 2 10 | System.out.println(findSmallest(27)); // 3 11 | } 12 | 13 | public static int findSmallest(int n) { 14 | if (n == 0) 15 | return 0; 16 | 17 | int[] cache = new int[n + 1]; 18 | 19 | for (int i = 1; i < cache.length; i++) 20 | cache[i] = Integer.MAX_VALUE; 21 | 22 | for (int i = 1; i < cache.length; i++) { 23 | int j = 1; 24 | 25 | while (j * j <= i) { 26 | cache[i] = Math.min(cache[i], cache[i - j * j] + 1); 27 | j++; 28 | } 29 | } 30 | 31 | return cache[n]; 32 | } 33 | 34 | 35 | } 36 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/_161/#161.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/_161/#161.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/_161/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.facebook._161; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | System.out.println(Long.toBinaryString(reverseBits(0xF0F0F0F0))); // 0x0F0F0F0F 10 | } 11 | 12 | public static int reverseBits(int val) { 13 | for (long i = 1L, j = 1L << 31; i < j; i <<= 1, j >>= 1) { 14 | if ((val & i) != (val & j)) 15 | val = (int)(val & ~i & val & ~j | (~val & i) | (~val & j)); 16 | 17 | System.out.println(Long.toBinaryString(val)); 18 | } 19 | 20 | return val; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/_168/#168.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/_168/#168.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/_168/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.facebook._168; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * 7 | * */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | int[][] matrix = { 12 | { 1, 2, 3 }, 13 | { 4, 5, 6 }, 14 | { 7, 8, 9 } }; 15 | 16 | rotateClockwise(matrix); 17 | 18 | for (int[] row : matrix) 19 | System.out.println(Arrays.toString(row)); // [[7, 4, 1], [8, 5, 2], [9, 6, 3]] 20 | } 21 | 22 | public static void rotateClockwise(int[][] matrix) { 23 | final int width = matrix[0].length; 24 | final int height = matrix.length; 25 | 26 | // flip horizontally 27 | for (int row = 0; row < height; row++) 28 | for (int col1 = 0, col2 = width - 1; col1 < col2; col1++, col2--) 29 | swap(matrix, row, col1, row, col2); 30 | 31 | // flip diagonally 32 | for (int i = 0; i < width - 1; i++) { 33 | int row1 = i; 34 | int row2 = height - 1; 35 | int col1 = 0; 36 | int col2 = width - i - 1; 37 | 38 | while (row1 >= 0) { 39 | swap(matrix, row1--, col1++, row2--, col2++); 40 | } 41 | } 42 | } 43 | 44 | private static void swap(int[][] matrix, int row1, int col1, int row2, int col2) { 45 | int tmp = matrix[row1][col1]; 46 | matrix[row1][col1] = matrix[row2][col2]; 47 | matrix[row2][col2] = tmp; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/_170/#170.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/_170/#170.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/_182/#182.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/_182/#182.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/_190/#190.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/_190/#190.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/_190/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.facebook._190; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | System.out.println(findMaxSubarraySum(new int[] { 8, -1, 3, 4 })); // 15; 10 | System.out.println(findMaxSubarraySum(new int[] { -4, 5, 1, 0 })); // 6; 11 | } 12 | 13 | public static int findMaxSubarraySum(int[] arr) { 14 | int[] sum = new int[arr.length]; 15 | sum[0] = arr[0]; 16 | 17 | for (int i = 1; i < sum.length; i++) 18 | if (arr[i] >= 0) 19 | sum[i] = Math.max(sum[i - 1] + arr[i], arr[i]); 20 | else 21 | sum[i] = arr[i]; 22 | 23 | int max = 0; 24 | int left = sum[0]; 25 | 26 | for (int i = 0; i < sum.length; i++) { 27 | max = Math.max(max, sum[i]); 28 | 29 | if (sum[0] < 0 || i > 0 && sum[i] < sum[i - 1]) 30 | continue; 31 | 32 | left = Math.max(left, max); 33 | } 34 | 35 | return Math.max(max, left + sum[sum.length - 1]); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/_199/#199.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/_199/#199.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/_199/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.facebook._199; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | System.out.println(findBalancedString("(()")); // (()); 10 | System.out.println(findBalancedString("))()(")); // ()(); 11 | } 12 | 13 | public static String findBalancedString(String str) { 14 | StringBuilder buf = new StringBuilder(); 15 | int count = 0; 16 | 17 | for (int i = 0; i < str.length(); i++) { 18 | char ch = str.charAt(i); 19 | 20 | if (ch == '(') { 21 | count++; 22 | buf.append('('); 23 | } else if (ch == ')') { 24 | if (count > 0) { 25 | buf.append(')'); 26 | count--; 27 | } 28 | } 29 | } 30 | 31 | for (int i = 0; i < count; i++) 32 | buf.append(')'); 33 | 34 | return buf.toString(); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/easy/_027/#027.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/easy/_027/#027.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/easy/_027/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.facebook.easy._027; 2 | 3 | import java.util.Deque; 4 | import java.util.LinkedList; 5 | 6 | /** 7 | * 8 | * */ 9 | public class Solution { 10 | 11 | public static void main(String... args) { 12 | System.out.println(isBalanced("([])[]({})")); // true 13 | System.out.println(isBalanced("([)]")); // false 14 | System.out.println(isBalanced("((()")); // false 15 | } 16 | 17 | public static boolean isBalanced(String str) { 18 | Deque stack = new LinkedList<>(); 19 | 20 | for (int i = 0; i < str.length(); i++) { 21 | char ch = str.charAt(i); 22 | 23 | if (ch == '(' || ch == '[' || ch == '{') 24 | stack.push(ch); 25 | else { 26 | if (stack.isEmpty()) 27 | return false; 28 | 29 | char prv = stack.pop(); 30 | 31 | if (prv == '(' && ch != ')') 32 | return false; 33 | if (prv == '[' && ch != ']') 34 | return false; 35 | if (prv == '{' && ch != '}') 36 | return false; 37 | } 38 | } 39 | 40 | return stack.isEmpty(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/easy/_047/#047.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/easy/_047/#047.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/easy/_047/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.facebook.easy._047; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | System.out.println(getMaxProfit(new int[] { 9, 11, 8, 5, 7, 10 })); // 5 10 | } 11 | 12 | public static int getMaxProfit(int[] stockPrices) { 13 | int maxProfit = 0; 14 | int min = stockPrices[0]; 15 | 16 | for (int day = 1; day < stockPrices.length; day++) { 17 | int curMaxProfit = stockPrices[day] - min; 18 | maxProfit = day == 1 ? curMaxProfit : Math.max(maxProfit, curMaxProfit); 19 | min = Math.min(min, stockPrices[day]); 20 | } 21 | 22 | return maxProfit; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/easy/_069/#069.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/easy/_069/#069.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/easy/_069/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.facebook._069; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * 7 | * */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | System.out.println(maxTriplet(new int[] { -10, -10, 5, 2 })); // 500 12 | } 13 | 14 | public static int maxTriplet(int[] arr) { 15 | Arrays.sort(arr); 16 | int res = arr[arr.length - 1]; 17 | return Math.max(res, arr[arr.length - 3] * arr[arr.length - 2] * arr[arr.length - 1]); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/medium/_007/#007.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/medium/_007/#007.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/medium/_007/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.facebook.medium._007; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | System.out.println(count("1")); // 1 10 | System.out.println(count("11")); // 2 11 | System.out.println(count("111")); // 3 12 | System.out.println(count("1111")); // 5 13 | } 14 | 15 | public static int count(String msg) { 16 | if (msg == null || msg.isEmpty()) 17 | return 0; 18 | if (msg.length() == 1) 19 | return 1; 20 | if (msg.length() == 2) 21 | return 2; 22 | return count(msg.substring(1)) + count(msg.substring(2)); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/medium/_015/#015.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/medium/_015/#015.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/medium/_062/#062.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/medium/_062/#062.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/medium/_062/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.facebook._062; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * 7 | * */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | System.out.println(countTotalWays(2, 2)); // 2 12 | System.out.println(countTotalWays(3, 4)); // 10 13 | System.out.println(countTotalWays(4, 4)); // 20 14 | } 15 | 16 | public static int countTotalWays(int N, int M) { 17 | int[] arr = new int[M]; 18 | Arrays.fill(arr, 1); 19 | 20 | for (int i = 1; i < N; i++) 21 | for (int j = 0; j < M; j++) 22 | arr[j] += j == 0 ? 0 : arr[j - 1]; 23 | 24 | return arr[M - 1]; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/medium/_079/#079.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/medium/_079/#079.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/facebook/medium/_079/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.facebook._079; 2 | 3 | import java.util.Deque; 4 | import java.util.LinkedList; 5 | 6 | /** 7 | * 8 | * */ 9 | public class Solution { 10 | 11 | public static void main(String... args) { 12 | System.out.println(check(new int[]{10, 5, 7})); // true 13 | System.out.println(check(new int[]{10, 5, 1})); // false 14 | } 15 | 16 | public static boolean check(int[] arr) { 17 | int replace = 0; 18 | Deque stack = new LinkedList<>(); 19 | 20 | for (int val : arr) { 21 | while (!stack.isEmpty() && val < stack.element()) { 22 | stack.pop(); 23 | replace++; 24 | } 25 | 26 | if (replace > 1) 27 | return false; 28 | 29 | stack.push(val); 30 | } 31 | 32 | return true; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/goldmansachs/_149/#149.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/goldmansachs/_149/#149.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/goldmansachs/_149/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.goldmansachs._149; 2 | 3 | import java.util.Arrays; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | 7 | /** 8 | * 9 | * */ 10 | public class Solution { 11 | 12 | public static void main(String... args) { 13 | System.out.println(sum(1, 3)); // 5 14 | } 15 | 16 | private static final Map SUM = new HashMap<>(); 17 | 18 | static { 19 | int i = 0; 20 | long sum = 0; 21 | 22 | SUM.put(-1, 0L); 23 | 24 | for (int num : Arrays.asList(1, 2, 3, 4, 5, 6)) 25 | SUM.put(i++, sum += num); 26 | } 27 | 28 | public static long sum(int i, int j) { 29 | return SUM.get(j - 1) - SUM.get(i - 1); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_083/#083.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_083/#083.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_083/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.google._083; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | Node root = invert(createTree()); 10 | } 11 | 12 | private static Node createTree() { 13 | Node a = new Node('a'); 14 | Node b = new Node('b'); 15 | Node c = new Node('c'); 16 | Node d = new Node('d'); 17 | Node e = new Node('e'); 18 | Node f = new Node('f'); 19 | 20 | a.left = b; 21 | a.right = c; 22 | 23 | b.left = d; 24 | b.right = e; 25 | 26 | c.left = f; 27 | 28 | return a; 29 | } 30 | 31 | public static Node invert(Node node) { 32 | if (node == null) 33 | return null; 34 | 35 | Node left = node.left; 36 | Node right = node.right; 37 | node.right = invert(left); 38 | node.left = invert(right); 39 | 40 | return node; 41 | } 42 | 43 | private static final class Node { 44 | private final char data; 45 | private Node left; 46 | private Node right; 47 | 48 | public Node(char data) { 49 | this.data = data; 50 | } 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_086/#086.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_086/#086.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_086/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.google._086; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | System.out.println(minParenthesesToRemove("(()())((()))")); // 0 10 | System.out.println(minParenthesesToRemove("()())()")); // 1 11 | System.out.println(minParenthesesToRemove(")(")); // 2 12 | } 13 | 14 | public static int minParenthesesToRemove(String str) { 15 | int count = 0; 16 | int res = 0; 17 | 18 | for (int i = 0; i < str.length(); i++) { 19 | if (str.charAt(i) == '(') 20 | count++; 21 | else if (str.charAt(i) == ')') 22 | count--; 23 | else 24 | continue; 25 | 26 | if (count >= 0) 27 | continue; 28 | 29 | res++; 30 | count = 0; 31 | } 32 | 33 | return res + Math.abs(count); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_100/#100.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_100/#100.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_104/#104.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_104/#104.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_108/#108.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_108/#108.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_108/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.google._108; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | System.out.println(isShifted("abcde", "cdeab")); // true 10 | System.out.println(isShifted("abc", "acb")); // false 11 | } 12 | 13 | public static boolean isShifted(String one, String two) { 14 | if (one.length() != two.length()) 15 | return false; 16 | if (one.equals(two)) 17 | return true; 18 | 19 | for (int i = 0; i <= one.length(); i++) 20 | if ((one = shiftLeft(one)).equals(two)) 21 | return true; 22 | 23 | return false; 24 | } 25 | 26 | private static String shiftLeft(String str) { 27 | return str.substring(1) + str.charAt(0); 28 | } 29 | } 30 | 31 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_111/#111.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_111/#111.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_113/#113.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_113/#113.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_113/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.google._113; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | String str = "hello world here"; 10 | System.out.println(str); 11 | System.out.println(reverseWords(str)); 12 | System.out.println(new String(reverseWords(str.toCharArray()))); 13 | } 14 | 15 | public static String reverseWords(String str) { 16 | if (str == null || str.isEmpty()) 17 | return str; 18 | 19 | String[] words = str.split("\\s+"); 20 | StringBuilder buf = new StringBuilder(); 21 | 22 | for (int i = words.length - 1; i >= 0; i--) { 23 | if (buf.length() > 0) 24 | buf.append(' '); 25 | buf.append(words[i]); 26 | } 27 | 28 | return buf.toString(); 29 | } 30 | 31 | public static char[] reverseWords(char... arr) { 32 | swapArr(arr, 0, arr.length - 1); 33 | 34 | for (int i = 0, j = 0; j < arr.length; j++) { 35 | if (arr[j] == ' ') { 36 | if (arr[i] != ' ') 37 | swapArr(arr, i, j - 1); 38 | 39 | i = j; 40 | } else { 41 | if (arr[i] == ' ') 42 | i = j; 43 | if (j == arr.length - 1 && arr[i] != ' ') 44 | swapArr(arr, i, j); 45 | } 46 | } 47 | 48 | return arr; 49 | } 50 | 51 | private static void swapArr(char[] arr, int i, int j) { 52 | while (i < j) 53 | swap(arr, i++, j--); 54 | } 55 | 56 | private static void swap(char[] arr, int i, int j) { 57 | char tmp = arr[i]; 58 | arr[i] = arr[j]; 59 | arr[j] = tmp; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_115/#115.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_115/#115.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_115/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.google._115; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | Node t = new Node(3); 10 | Node root = createTree(t); 11 | System.out.println(isSubtreeEquals(root, t)); // true 12 | } 13 | 14 | private static Node createTree(Node t) { 15 | Node four = new Node(4); 16 | four.left = new Node(7); 17 | 18 | Node two = new Node(2); 19 | two.right = four; 20 | 21 | t.left = new Node(5); 22 | t.right = new Node(6); 23 | 24 | Node root = new Node(1); 25 | root.left = two; 26 | root.right = t; 27 | 28 | return root; 29 | } 30 | 31 | public static boolean isSubtreeEquals(Node s, Node t) { 32 | String strS = serialize(s); 33 | String strT = serialize(t); 34 | return strS.contains(strT); 35 | } 36 | 37 | private static String serialize(Node root) { 38 | return dfs(root, new StringBuilder()).toString(); 39 | } 40 | 41 | private static StringBuilder dfs(Node node, StringBuilder buf) { 42 | if (node != null) { 43 | buf.append(node.data); 44 | 45 | buf.append('('); 46 | dfs(node.left, buf); 47 | buf.append(')'); 48 | buf.append('('); 49 | dfs(node.right, buf); 50 | buf.append(')'); 51 | } 52 | 53 | return buf; 54 | } 55 | 56 | private static final class Node { 57 | private final int data; 58 | private Node left; 59 | private Node right; 60 | 61 | public Node(int data) { 62 | this.data = data; 63 | } 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_118/#118.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_118/#118.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_118/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.google._118; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * 7 | * */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | System.out.println(Arrays.toString(squareAndSort(-9, -2, 0, 2, 3))); 12 | } 13 | 14 | public static int[] squareAndSort(int... sorted) { 15 | int[] res = new int[sorted.length]; 16 | 17 | for (int i = 0, j = sorted.length - 1, k = res.length - 1; i <= j; k--) { 18 | int absLeft = Math.abs(sorted[i]); 19 | int absRight = Math.abs(sorted[j]); 20 | 21 | if (absLeft >= absRight) { 22 | res[k] = absLeft * absLeft; 23 | i++; 24 | } else { 25 | res[k] = absRight * absRight; 26 | j--; 27 | } 28 | } 29 | 30 | return res; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_119/#119.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_119/#119.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_119/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.google._119; 2 | 3 | import java.util.Arrays; 4 | import java.util.Comparator; 5 | import java.util.List; 6 | import java.util.Set; 7 | import java.util.TreeSet; 8 | import java.util.stream.Collectors; 9 | 10 | /** 11 | * 12 | * */ 13 | public class Solution { 14 | 15 | public static void main(String... args) { 16 | Set nums = getCoversIntervals(Arrays.asList( 17 | new Interval(0, 3), 18 | new Interval(2, 6), 19 | new Interval(3, 4), 20 | new Interval(6, 9))); 21 | System.out.println(nums.stream().map(String::valueOf).collect(Collectors.joining(" "))); // 3 9 22 | } 23 | 24 | public static Set getCoversIntervals(List intervals) { 25 | final Comparator sortByStartAndLengthDesc = (one, two) -> 26 | one.start != two.start ? Integer.compare(one.start, two.start) : Integer.compare(two.start, one.start); 27 | 28 | Set uniqueIntervals = new TreeSet<>(sortByStartAndLengthDesc); 29 | uniqueIntervals.addAll(intervals); 30 | 31 | int end = -1; 32 | Set res = new TreeSet<>(); 33 | 34 | for (Interval interval : intervals) 35 | if (end == -1 || interval.start > end) 36 | res.add(end = interval.end); 37 | 38 | return res; 39 | } 40 | 41 | public static final class Interval { 42 | private final int start; 43 | private final int end; 44 | 45 | public Interval(int start, int end) { 46 | this.start = start; 47 | this.end = end; 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_125/#125.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_125/#125.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_136/#136.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_136/#136.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_136/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.google._136; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | int[][] matrix = { 10 | {1, 1, 0, 0}, 11 | {1, 0, 1, 1}, 12 | {1, 0, 1, 1}, 13 | {0, 1, 0, 0}}; 14 | System.out.println(getLargestAreaSet(matrix)); // 4 15 | } 16 | 17 | public static int getLargestAreaSet(int[][] matrix) { 18 | int maxArea = 0; 19 | int[] cache = new int[matrix[0].length]; 20 | 21 | for (int row = 0; row < matrix.length; row++) { 22 | for (int col = 0; col < matrix[row].length; col++) { 23 | if (matrix[row][col] == 0) 24 | cache[col] = 0; 25 | else 26 | cache[col]++; 27 | } 28 | 29 | maxArea = Math.max(maxArea, inferArea(cache)); 30 | } 31 | 32 | return maxArea; 33 | } 34 | 35 | private static int inferArea(int[] cache) { 36 | int area = 0; 37 | 38 | for (int i = 0; i < cache.length; i++) { 39 | if (cache[i] == 0) 40 | continue; 41 | 42 | int min = cache[i]; 43 | 44 | for (int j = i; j < cache.length && cache[j] != 0; j++) 45 | area = Math.max(area, Math.min(min, cache[j]) * (j - i + 1)); 46 | } 47 | 48 | return area; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_138/#138.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_138/#138.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_138/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.google._138; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * 7 | * */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | System.out.println(getMinCoinsAmount(16)); // 3 12 | } 13 | 14 | private static final int[] DENOMINATION = {1, 5, 10, 25}; 15 | 16 | public static int getMinCoinsAmount(int n) { 17 | int[] cache = new int[n + 1]; 18 | Arrays.fill(cache, Integer.MAX_VALUE); 19 | 20 | for (int d : DENOMINATION) 21 | if (d < cache.length) 22 | cache[d] = 1; 23 | 24 | for (int i = 1; i < cache.length; i++) 25 | for (int d : DENOMINATION) 26 | if (i - d > 0) 27 | cache[i] = Math.min(cache[i], 1 + cache[i - d]); 28 | 29 | return cache[n]; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_139/#139.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_139/#139.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_139/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.google._139; 2 | 3 | import java.util.Arrays; 4 | import java.util.Iterator; 5 | import java.util.List; 6 | 7 | /** 8 | * 9 | * */ 10 | public class Solution { 11 | 12 | public static void main(String... args) { 13 | List words = Arrays.asList("one", "two", "three", "four", "five"); 14 | PeekableInterface it = new PeekableInterface<>(words); 15 | 16 | int i = 0; 17 | 18 | while (it.hasNext()) { 19 | if (i++ % 2 == 0) 20 | System.out.format("next:%s -> peek:%s\n", it.next(), it.hasNext() ? it.peek() : null); 21 | else 22 | System.out.format("peek:%s -> next:%s\n", it.peek(), it.next()); 23 | } 24 | } 25 | 26 | public static final class PeekableInterface implements Iterator { 27 | private final Iterator it; 28 | private E next; 29 | private boolean nextExists; 30 | 31 | public PeekableInterface(Iterable iterable) { 32 | it = iterable.iterator(); 33 | } 34 | 35 | @Override 36 | public boolean hasNext() { 37 | return nextExists || it.hasNext(); 38 | } 39 | 40 | @Override 41 | public E next() { 42 | if (nextExists) { 43 | E e = next; 44 | nextExists = false; 45 | next = null; 46 | return e; 47 | } 48 | 49 | return it.next(); 50 | } 51 | 52 | public E peek() { 53 | if (!nextExists) 54 | next = it.next(); 55 | 56 | nextExists = true; 57 | return next; 58 | } 59 | 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_142/#142.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_142/#142.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_142/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.google._142; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | System.out.println(isBalanced("(()*")); // true 10 | System.out.println(isBalanced("(*)")); // true 11 | System.out.println(isBalanced(")*(")); // false 12 | } 13 | 14 | public static boolean isBalanced(String str) { 15 | int count = 0; 16 | int star = 0; 17 | 18 | for (int i = 0; i < str.length(); i++) { 19 | if (str.charAt(i) == '*') 20 | star++; 21 | else if (str.charAt(i) == '(') 22 | count++; 23 | else if (str.charAt(i) == ')') { 24 | if (--count < 0) { 25 | count++; 26 | 27 | if (--star < 0) 28 | return false; 29 | } 30 | } 31 | } 32 | 33 | return Math.abs(count) <= star; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_144/#144.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_144/#144.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_144/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.google._144; 2 | 3 | import java.util.Collections; 4 | import java.util.HashMap; 5 | import java.util.HashSet; 6 | import java.util.Map; 7 | import java.util.Set; 8 | import java.util.TreeSet; 9 | 10 | /** 11 | * 12 | * 13 | */ 14 | public class Solution { 15 | 16 | public static void main(String... args) { 17 | System.out.println(findNearestLargerNumberIndex(0)); 18 | } 19 | 20 | private static final int[] arr = { 4, 1, 3, 5, 6, 4 }; 21 | private static final Map> mapNumPos = new HashMap<>(); 22 | private static final Map mapNumNextNum = new HashMap<>(); 23 | 24 | static { 25 | Set unique = new TreeSet<>(); 26 | 27 | for (int i = 0; i < arr.length; i++) { 28 | unique.add(arr[i]); 29 | 30 | if (!mapNumPos.containsKey(arr[i])) 31 | mapNumPos.put(arr[i], new HashSet<>()); 32 | 33 | mapNumPos.get(arr[i]).add(i); 34 | } 35 | 36 | Integer prv = null; 37 | 38 | for (Integer num : unique) { 39 | if (prv != null) 40 | mapNumNextNum.put(prv, num); 41 | prv = num; 42 | } 43 | } 44 | 45 | public static int findNearestLargerNumberIndex(int i) { 46 | int num = arr[i]; 47 | Integer nextNum = mapNumNextNum.get(num); 48 | Set nextNumPos = nextNum != null ? mapNumPos.get(nextNum) : Collections.emptySet(); 49 | return nextNumPos.isEmpty() ? -1 : nextNumPos.iterator().next(); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_145/#145.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_145/#145.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_159/#159.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_159/#159.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_159/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.google._159; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | /** 7 | * 8 | * */ 9 | public class Solution { 10 | 11 | public static void main(String... args) { 12 | System.out.println(getFirstRecurringChar("acbbac")); // b 13 | System.out.println(getFirstRecurringChar("abcdef")); // null 14 | } 15 | 16 | public static String getFirstRecurringChar(String str) { 17 | Set letters = new HashSet<>(); 18 | 19 | for (int i = 0; i < str.length(); i++) { 20 | char ch = str.charAt(i); 21 | 22 | if (letters.contains(ch)) 23 | return String.valueOf(ch); 24 | 25 | letters.add(ch); 26 | } 27 | 28 | return null; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_164/#164.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_164/#164.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_164/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.google._164; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | System.out.println(findDuplicate(new int[] { 1, 2, 3, 4, 5, 3 })); // 3 10 | System.out.println(findDuplicate(new int[] { 1, 2, 3, 4, 4, 5 })); // 4 11 | } 12 | 13 | public static int findDuplicate(int[] arr) { 14 | int expected = 0; 15 | int actual = 0; 16 | 17 | for (int i = 0; i < arr.length; i++) { 18 | expected += i; 19 | actual += arr[i]; 20 | } 21 | 22 | return actual - expected; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_165/#165.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_165/#165.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_165/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.google._165; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * 7 | * */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | System.out.println(Arrays.toString(modify(new int[] { 3, 4, 9, 6, 1 }))); // [1, 1, 2, 1, 0] 12 | } 13 | 14 | public static int[] modify(int[] arr) { 15 | int[] res = new int[arr.length]; 16 | 17 | for (int i = arr.length - 1; i >= 0; i--) { 18 | for (int j = i + 1; j < arr.length; j++) { 19 | if (arr[j] > arr[i]) 20 | continue; 21 | 22 | res[i] = arr[j] == arr[i] ? res[j] : res[j] + 1; 23 | break; 24 | } 25 | } 26 | 27 | return res; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_180/#180.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_180/#180.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_180/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.google._180; 2 | 3 | import java.util.ArrayDeque; 4 | import java.util.Arrays; 5 | import java.util.Deque; 6 | import java.util.LinkedList; 7 | import java.util.Queue; 8 | 9 | /** 10 | * 11 | * */ 12 | public class Solution { 13 | 14 | public static void main(String... args) { 15 | System.out.println(Arrays.toString(interleave(new ArrayDeque<>(Arrays.asList(5, 4, 3, 2, 1))).toArray(new Integer[0]))); // [1, 5, 2, 4, 3] 16 | System.out.println(Arrays.toString(interleave(new ArrayDeque<>(Arrays.asList(4, 3, 2, 1))).toArray(new Integer[0]))); // [1, 4, 2, 3] 17 | } 18 | 19 | public static Deque interleave(Deque stack) { 20 | Queue queue = new LinkedList<>(); 21 | 22 | for (int i = stack.size() - 1; i > 1; i--) { 23 | for (int j = 0; j < i; j++) 24 | queue.add(stack.pop()); 25 | 26 | for (int j = 0; j < i; j++) 27 | stack.push(queue.remove()); 28 | } 29 | 30 | return stack; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_181/#181.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_181/#181.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_181/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.google._181; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.Collections; 6 | import java.util.List; 7 | import java.util.Optional; 8 | 9 | /** 10 | * 11 | * */ 12 | public class Solution { 13 | 14 | public static void main(String... args) { 15 | System.out.println(Arrays.toString(split("racecarannakayak").toArray(new String[0]))); // ["racecar", "anna", "kayak"] 16 | System.out.println(Arrays.toString(split("abc").toArray(new String[0]))); // ["a", "b", "c"] 17 | } 18 | 19 | public static List split(String str) { 20 | return Optional.ofNullable(findPalindromes(str, 0, str.length() - 1)).orElse(Collections.emptyList()); 21 | } 22 | 23 | private static List findPalindromes(String str, int left, int right) { 24 | for (int i = right; i >= left; i--) { 25 | if (!isPalindrome(str, left, i)) 26 | continue; 27 | 28 | List tail = i + 1 <= right ? findPalindromes(str, i + 1, right) : Collections.emptyList(); 29 | 30 | if (tail == null) 31 | continue; 32 | 33 | List res = new ArrayList<>(tail.size() + 1); 34 | res.add(str.substring(left, i + 1)); 35 | res.addAll(tail); 36 | 37 | return res; 38 | } 39 | 40 | return null; 41 | } 42 | 43 | private static boolean isPalindrome(String str, int i, int j) { 44 | while (i < j) 45 | if (str.charAt(i++) != str.charAt(j--)) 46 | return false; 47 | 48 | return true; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_189/#189.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_189/#189.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_189/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.google._189; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | /** 7 | * 8 | * */ 9 | public class Solution { 10 | 11 | public static void main(String... args) { 12 | System.out.println(getLongestDistinctLength(new int[] { 5, 1, 3, 5, 2, 3, 4, 1 })); // 5 13 | } 14 | 15 | public static int getLongestDistinctLength(int[] arr) { 16 | int res = 0; 17 | Set unique = new HashSet<>(); 18 | 19 | for (int i = 0, j = 0; j < arr.length; ) { 20 | if (unique.contains(arr[j])) 21 | unique.remove(arr[i++]); 22 | else { 23 | unique.add(arr[j]); 24 | res = Math.max(res, unique.size()); 25 | j++; 26 | } 27 | } 28 | 29 | return res; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_192/#192.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_192/#192.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_192/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.google._192; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | System.out.println(canGetEnd(new int[] { 1, 3, 1, 2, 0, 1 })); // true 10 | System.out.println(canGetEnd(new int[] { 1, 2, 1, 0, 0 })); // false 11 | System.out.println(canGetEnd(new int[] { 2, 2, 0, 0 })); // false 12 | } 13 | 14 | public static boolean canGetEnd(int[] arr) { 15 | boolean[] tmp = new boolean[arr.length]; 16 | 17 | for (int i = arr.length - 1; i >= 0; i--) { 18 | tmp[i] = i + arr[i] >= arr.length; 19 | 20 | for (int j = i + 1; j <= Math.min(i + arr[i], arr.length - 1) && !tmp[i]; j++) 21 | tmp[i] = tmp[j]; 22 | } 23 | 24 | return tmp[0]; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_195/#192.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_195/#192.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_195/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.google._195; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | int[][] matrix = { 10 | { 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 } }; 15 | System.out.println(count(matrix, 1, 1, 3, 3)); 16 | } 17 | 18 | public static int count(int[][] matrix, int i1, int j1, int i2, int j2) { 19 | int res = 0; 20 | int low = matrix[i1][j1]; 21 | int high = matrix[i2][j2]; 22 | 23 | for (int[] row : matrix) { 24 | if (row[0] > high || row[row.length - 1] < low) 25 | res += row.length; 26 | else if (row[0] >= low && row[row.length - 1] <= high) 27 | continue; 28 | 29 | for (int i = 0; i < row.length && row[i] < low; i++) 30 | res++; 31 | for (int i = row.length - 1; i >= 0 && row[i] > high; i--) 32 | res++; 33 | } 34 | 35 | return res; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_198/#198.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_198/#198.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_198/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.google._198; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * 7 | * */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | System.out.println(Arrays.toString(findLargestSubsetWithCondition(new int[] { 3, 5, 10, 20, 21 }))); // { 5, 10, 20 } 12 | System.out.println(Arrays.toString(findLargestSubsetWithCondition(new int[] { 1, 3, 6, 24 }))); // { 1, 3, 6, 24 } 13 | } 14 | 15 | public static int[] findLargestSubsetWithCondition(int[] arr) { 16 | int length = 0; 17 | int offs = 0; 18 | 19 | for (int i = 0, j = 0; j < arr.length; ) { 20 | int min = Math.min(arr[i], arr[j]); 21 | int max = Math.max(arr[i], arr[j]); 22 | 23 | if (max % min == 0) { 24 | if (j - i + 1 > length) { 25 | length = j - i + 1; 26 | offs = i; 27 | } 28 | 29 | j++; 30 | } else 31 | i = j; 32 | } 33 | 34 | int[] res = new int[length]; 35 | 36 | for (int i = 0; i < length; i++) 37 | res[i] = arr[offs + i]; 38 | 39 | return res; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_201/#201.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_201/#201.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_201/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.google._201; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | System.out.println(findMaxWeightPath(new int[][] { { 1 }, { 2, 3 }, { 1, 5, 1 } })); // 9 10 | } 11 | 12 | public static int findMaxWeightPath(int[][] arr) { 13 | int res = 0; 14 | 15 | for (int row = 0; row < arr.length; row++) { 16 | for (int col = 0; col < arr[row].length; col++) { 17 | arr[row][col] += Math.max(left(arr, row, col), right(arr, row, col)); 18 | res = Math.max(res, arr[row][col]); 19 | } 20 | } 21 | 22 | return res; 23 | } 24 | 25 | private static int left(int[][] arr, int row, int col) { 26 | return row == 0 || col == 0 ? 0 : arr[row - 1][col - 1]; 27 | } 28 | 29 | private static int right(int[][] arr, int row, int col) { 30 | return row == 0 || col == arr[row].length - 1 ? 0 : arr[row - 1][col]; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_232/#232.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_232/#232.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/_232/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.google._232; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * 8 | * */ 9 | public class Solution { 10 | 11 | public static void main(String... args) { 12 | PrefixMapSum mapsum = new PrefixMapSum(); 13 | 14 | mapsum.insert("columnar", 3); 15 | System.out.println(mapsum.sum("col")); // 3 16 | 17 | mapsum.insert("column", 2); 18 | System.out.println(mapsum.sum("col")); // 5 19 | } 20 | 21 | private static final class PrefixMapSum { 22 | 23 | private final Map map = new HashMap<>(); 24 | 25 | public void insert(String key, int value) { 26 | map.put(key, value); 27 | } 28 | 29 | public int sum(String prefix) { 30 | int sum = 0; 31 | 32 | for (Map.Entry entry : map.entrySet()) 33 | if (entry.getKey().startsWith(prefix)) 34 | sum += entry.getValue(); 35 | 36 | return sum; 37 | } 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/easy/_001/#001.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/easy/_001/#001.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/easy/_001/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.google.easy._001; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | /** 7 | * 8 | * */ 9 | public class Solution { 10 | 11 | public static void main(String... args) { 12 | int[] arr = { 10, 15, 3, 7 }; 13 | System.out.println(check(arr, 17)); 14 | } 15 | 16 | public static boolean check(int[] arr, int k) { 17 | if (arr == null || arr.length < 2) 18 | return false; 19 | 20 | Set tmp = new HashSet<>(); 21 | 22 | for (int val : arr) { 23 | if (tmp.contains(val)) 24 | return true; 25 | tmp.add(k - val); 26 | } 27 | 28 | return false; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/easy/_008/#008.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/easy/_008/#008.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/easy/_008/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.google.easy._008; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | Node node = createTree(); 10 | System.out.println(countUnivalSubtrees(node)); 11 | } 12 | 13 | private static Node createTree() { 14 | Node a = new Node(1); 15 | a.left = new Node(1); 16 | a.right = new Node(1); 17 | 18 | Node b = new Node(0); 19 | b.left = a; 20 | b.right = new Node(0); 21 | 22 | 23 | Node root = new Node(0); 24 | root.left = new Node(1); 25 | root.right = b; 26 | 27 | return root; 28 | } 29 | 30 | private static class Node { 31 | 32 | private final int val; 33 | private Node left; 34 | private Node right; 35 | 36 | public Node(int val) { 37 | this.val = val; 38 | } 39 | } 40 | 41 | public static int countUnivalSubtrees(Node node) { 42 | if (node == null) 43 | return 0; 44 | 45 | int count = countUnivalSubtrees(node.left); 46 | count += countUnivalSubtrees(node.right); 47 | 48 | return isUnivalTree(node) ? count + 1 : count; 49 | } 50 | 51 | private static boolean isUnivalTree(Node node) { 52 | if (node == null) 53 | return true; 54 | if (node.left != null && node.left.val != node.val) 55 | return false; 56 | if (node.right != null && node.right.val != node.val) 57 | return false; 58 | return isUnivalTree(node.left) && isUnivalTree(node.right); 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/easy/_020/#020.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/easy/_020/#020.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/easy/_020/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.google.easy._020; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | Node node10 = new Node(10, null); 10 | Node node8 = new Node(8, node10); 11 | Node node7 = new Node(7, node8); 12 | Node node3 = new Node(3, node7); 13 | Node node1 = new Node(1, node8); 14 | Node node99 = new Node(99, node1); 15 | 16 | Node res = findIntersectionNode(node3, node99); 17 | System.out.println(res.value); // 8; 18 | } 19 | 20 | public static Node findIntersectionNode(Node one, Node two) { 21 | int lengthOne = getLength(one); 22 | int lengthTwo = getLength(two); 23 | 24 | if (lengthOne > lengthTwo) 25 | one = skip(one, lengthOne - lengthTwo); 26 | else if (lengthOne < lengthTwo) 27 | two = skip(two, lengthTwo - lengthOne); 28 | 29 | while (one != null && one != two) { 30 | one = one.next; 31 | two = two.next; 32 | } 33 | 34 | return one; 35 | } 36 | 37 | private static int getLength(Node node) { 38 | int length = 0; 39 | 40 | while (node != null) { 41 | node = node.next; 42 | length++; 43 | } 44 | 45 | return length; 46 | } 47 | 48 | private static Node skip(Node node, int count) { 49 | while (node != null && count > 0) { 50 | node = node.next; 51 | count--; 52 | } 53 | 54 | return node; 55 | } 56 | 57 | private static final class Node { 58 | private final int value; 59 | private final Node next; 60 | 61 | public Node(int value, Node next) { 62 | this.value = value; 63 | this.next = next; 64 | } 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/easy/_023/#023.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/easy/_023/#023.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/easy/_031/#031.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/easy/_031/#031.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/easy/_031/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.google.easy._031; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | System.out.println(getEditDistance("kitten", "sitting")); 10 | System.out.println(getEditDistance("foo", "bar")); 11 | System.out.println(getEditDistance("money", "rooney")); 12 | } 13 | 14 | public static int getEditDistance(String one, String two) { 15 | int[] arr = new int[26]; 16 | 17 | for (int i = 0; i < one.length(); i++) 18 | arr[one.charAt(i) - 'a']++; 19 | 20 | for (int i = 0; i < two.length(); i++) 21 | arr[two.charAt(i) - 'a']--; 22 | 23 | int add = 0; 24 | int sub = 0; 25 | 26 | for (int delta : arr) { 27 | if (delta > 0) 28 | add += delta; 29 | else if (delta < 0) 30 | sub += Math.abs(delta); 31 | } 32 | 33 | return Math.max(add, sub); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/easy/_037/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.google.easy._037; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.List; 6 | 7 | /** 8 | * 9 | * */ 10 | public class Solution { 11 | 12 | public static void main(String... args) { 13 | getPowerSet(new int[] { 1, 2, 3 }).forEach(arr -> System.out.println(Arrays.toString(arr))); 14 | } 15 | 16 | public static List getPowerSet(int[] arr) { 17 | List res = new ArrayList<>(); 18 | res.add(new int[0]); 19 | 20 | for (int length = 1; length <= arr.length; length++) { 21 | for (int i = 0; i <= arr.length - length; i++) { 22 | int[] tmp = new int[length]; 23 | System.arraycopy(arr, i, tmp, 0, length); 24 | res.add(tmp); 25 | } 26 | } 27 | 28 | return res; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/easy/_073/#073.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/easy/_073/#073.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/easy/_073/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.google._073; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | Node root = createList(1, 2, 3, 4, 5); 10 | print(root); 11 | Node reverseRoot = reverseInPlace(root); 12 | print(reverseRoot); 13 | } 14 | 15 | public static Node reverseInPlace(Node node) { 16 | Node prv = null; 17 | 18 | while (node != null) { 19 | Node next = node.next; 20 | 21 | if (prv == null) { 22 | prv = node; 23 | prv.next = null; 24 | } else { 25 | node.next = prv; 26 | prv = node; 27 | } 28 | 29 | node = next; 30 | } 31 | 32 | return prv; 33 | } 34 | 35 | private static void print(Node node) { 36 | while (node != null) { 37 | System.out.print(node.val + ", "); 38 | node = node.next; 39 | } 40 | 41 | System.out.println(); 42 | } 43 | 44 | private static Node createList(int... vals) { 45 | Node root = null; 46 | Node node = null; 47 | 48 | for (int val : vals) { 49 | Node tmp = new Node(val); 50 | 51 | if (node == null) { 52 | node = tmp; 53 | root = node; 54 | } else { 55 | node.next = tmp; 56 | node = tmp; 57 | } 58 | } 59 | 60 | return root; 61 | } 62 | 63 | private static class Node { 64 | 65 | private final int val; 66 | private Node next; 67 | 68 | public Node(int val) { 69 | this.val = val; 70 | } 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/easy/_080/#080.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/easy/_080/#080.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/easy/_080/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.google._080; 2 | 3 | import java.util.*; 4 | 5 | /** 6 | * 7 | * */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | System.out.println(findDeepestNode(createTree())); // d 12 | } 13 | 14 | private static Node createTree() { 15 | Node d = new Node('d'); 16 | Node b = new Node('b'); 17 | b.left = d; 18 | 19 | Node c = new Node('c'); 20 | Node root = new Node('a'); 21 | root.left = b; 22 | root.right = c; 23 | 24 | return root; 25 | } 26 | 27 | public static char findDeepestNode(Node root) { 28 | if (root == null) 29 | return '\0'; 30 | 31 | Deque> queue = new LinkedList<>(); 32 | queue.add(Collections.singletonList(root)); 33 | List deepestNodes = Collections.emptyList(); 34 | 35 | while (!queue.isEmpty()) { 36 | deepestNodes = queue.poll(); 37 | List nodes = new ArrayList<>(); 38 | 39 | for (Node node : deepestNodes) { 40 | if (node.left != null) 41 | nodes.add(node.left); 42 | if (node.right != null) 43 | nodes.add(node.right); 44 | } 45 | 46 | if (!nodes.isEmpty()) 47 | queue.add(nodes); 48 | } 49 | 50 | return deepestNodes.isEmpty() ? '\0' : deepestNodes.iterator().next().data; 51 | } 52 | 53 | private static final class Node { 54 | private final char data; 55 | private Node left; 56 | private Node right; 57 | 58 | public Node(char data) { 59 | this.data = data; 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/hard/_006/#006.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/hard/_006/#006.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/hard/_017/#017.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/hard/_017/#017.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/hard/_018/#018.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/hard/_018/#018.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/hard/_018/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.google.hard._018; 2 | 3 | import java.util.Arrays; 4 | import java.util.Comparator; 5 | import java.util.PriorityQueue; 6 | import java.util.Queue; 7 | 8 | /** 9 | * 10 | * 11 | * */ 12 | public class Solution { 13 | 14 | public static void main(String... args) { 15 | System.out.println(Arrays.toString(maxSubArr(new int[] { 10, 5, 2, 7, 8, 7 }, 3))); // [10, 7, 8, 8] 16 | } 17 | 18 | public static int[] maxSubArr(int[] arr, int k) { 19 | int[] res = new int[arr.length - k + 1]; 20 | Queue maxHeap = new PriorityQueue<>(Comparator.reverseOrder()); 21 | 22 | for (int i = 0, j = 0, m = 0; j < arr.length; ) { 23 | maxHeap.add(arr[j++]); 24 | 25 | if (maxHeap.size() > k) 26 | maxHeap.remove(arr[i++]); 27 | if (maxHeap.size() == k) 28 | res[m++] = maxHeap.element(); 29 | } 30 | 31 | return res; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/hard/_035/#035.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/hard/_035/#035.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/hard/_035/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.google.hard._035; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * 7 | * */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | char[] arr = { 'G', 'B', 'R', 'R', 'B', 'R', 'G' }; 12 | System.out.println(Arrays.toString(arr)); 13 | sort(arr); 14 | System.out.println(Arrays.toString(arr)); 15 | } 16 | 17 | public static void sort(char... arr) { 18 | int r = 0; 19 | int b = arr.length - 1; 20 | int i = 0; 21 | 22 | while (i <= b) { 23 | if (arr[i] == 'R') 24 | swap(arr, i++, r++); 25 | else if (arr[i] == 'B') 26 | swap(arr, i, b--); 27 | else 28 | i++; 29 | } 30 | } 31 | 32 | private static void swap(char[] arr, int i, int j) { 33 | char ch = arr[i]; 34 | arr[i] = arr[j]; 35 | arr[j] = ch; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/hard/_042/#042.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/hard/_042/#042.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/hard/_042/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.google.hard._042; 2 | 3 | import java.util.Arrays; 4 | import java.util.Deque; 5 | import java.util.LinkedList; 6 | 7 | /** 8 | * 9 | * */ 10 | public class Solution { 11 | 12 | public static void main(String... args) { 13 | System.out.println(Arrays.toString(getSubset(new int[] { 12, 1, 61, 5, 9, 2 }, 24))); 14 | } 15 | 16 | public static int[] getSubset(int[] arr, int k) { 17 | Deque stack = new LinkedList<>(); 18 | find(arr, 0, k, stack); 19 | return stack.stream().mapToInt(Integer::intValue).toArray(); 20 | } 21 | 22 | private static boolean find(int[] arr, int i, int k, Deque stack) { 23 | if (k == 0) 24 | return true; 25 | if (k < 0 || i >= arr.length) 26 | return false; 27 | 28 | for (; i < arr.length; i++) { 29 | stack.push(arr[i]); 30 | 31 | if (find(arr, i + 1, k - arr[i], stack)) 32 | return true; 33 | 34 | stack.pop(); 35 | } 36 | 37 | return false; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/hard/_052/#052.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/hard/_052/#052.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/hard/_052/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.google.hard._052; 2 | 3 | import java.util.LinkedHashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * 8 | * */ 9 | public class Solution { 10 | 11 | public static void main(String... args) { 12 | LRUCache cache = new LRUCache<>(3); 13 | cache.set("one", "one1"); 14 | cache.set("two", "two1"); 15 | cache.set("three", "three1"); 16 | cache.set("four", "four1"); 17 | System.out.println(cache.get("two")); // two1 18 | System.out.println(cache.get("one")); // null 19 | 20 | cache.set("five", "five1"); 21 | System.out.println(cache.get("two")); // two1 22 | System.out.println(cache.get("three")); // null 23 | 24 | cache.set("four", "four2"); 25 | System.out.println(cache.get("four")); // four2 26 | } 27 | 28 | public static final class LRUCache { 29 | private final Map map = new LinkedHashMap<>(); 30 | private final int maxSize; 31 | 32 | public LRUCache(int maxSize) { 33 | this.maxSize = maxSize; 34 | } 35 | 36 | public void set(K key, V value) { 37 | if (map.containsKey(key)) 38 | map.remove(key); 39 | else if (map.size() == maxSize) 40 | map.remove(map.keySet().iterator().next()); 41 | 42 | map.put(key, value); 43 | } 44 | 45 | public V get(K key) { 46 | if (!map.containsKey(key)) 47 | return null; 48 | 49 | V value = map.remove(key); 50 | map.put(key, value); 51 | return value; 52 | } 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/hard/_067/#067.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/hard/_067/#067.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/hard/_067/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.google._067; 2 | 3 | import java.util.LinkedHashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * 8 | * */ 9 | public class Solution { 10 | 11 | public static void main(String... args) { 12 | LRUCache cache = new LRUCache<>(3); 13 | cache.set("one", "one1"); 14 | cache.set("two", "two1"); 15 | cache.set("three", "three1"); 16 | cache.set("four", "four1"); 17 | System.out.println(cache.get("two")); // two1 18 | System.out.println(cache.get("one")); // null 19 | 20 | cache.set("five", "five1"); 21 | System.out.println(cache.get("two")); // two1 22 | System.out.println(cache.get("three")); // null 23 | 24 | cache.set("four", "four2"); 25 | System.out.println(cache.get("four")); // four2 26 | } 27 | 28 | public static final class LRUCache { 29 | private final Map map = new LinkedHashMap<>(); 30 | private final int maxSize; 31 | 32 | public LRUCache(int maxSize) { 33 | this.maxSize = maxSize; 34 | } 35 | 36 | public void set(K key, V value) { 37 | if (map.containsKey(key)) 38 | map.remove(key); 39 | else if (map.size() == maxSize) 40 | map.remove(map.keySet().iterator().next()); 41 | 42 | map.put(key, value); 43 | } 44 | 45 | public V get(K key) { 46 | if (!map.containsKey(key)) 47 | return null; 48 | 49 | V value = map.remove(key); 50 | map.put(key, value); 51 | return value; 52 | } 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/medium/_003/#003.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/medium/_003/#003.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/medium/_014/#014.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/medium/_014/#014.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/medium/_024/#024.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/medium/_024/#024.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/medium/_026/#026.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/medium/_026/#026.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/medium/_078/#078.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/google/medium/_078/#078.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/medium/_078/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.google._078; 2 | 3 | import java.util.Arrays; 4 | import java.util.Iterator; 5 | import java.util.LinkedList; 6 | import java.util.List; 7 | import java.util.stream.Collectors; 8 | 9 | /** 10 | * 11 | * */ 12 | public class Solution { 13 | 14 | public static void main(String... args) { 15 | List> sortedLists = Arrays.asList( 16 | Arrays.asList(0, 1, 4, 5, 6, 8, 8), 17 | Arrays.asList(0, 1, 3, 3, 3, 3, 5, 6, 7), 18 | Arrays.asList(0, 0, 1, 3, 4, 5, 6, 6, 6, 7, 8, 8, 8) 19 | ); 20 | 21 | System.out.println(merge(sortedLists).stream() 22 | .map(val -> Integer.toString(val)) 23 | .collect(Collectors.joining(" "))); 24 | } 25 | 26 | public static List merge(List> sortedLists) { 27 | List res = new LinkedList<>(); 28 | Iterator[] its = sortedLists.stream().map(List::iterator).toArray(Iterator[]::new); 29 | Integer[] values = new Integer[sortedLists.size()]; 30 | 31 | while (true) { 32 | int minIndex = -1; 33 | 34 | for (int i = 0; i < values.length; i++) { 35 | values[i] = values[i] == null && its[i].hasNext() ? its[i].next() : values[i]; 36 | 37 | if (values[i] != null) 38 | minIndex = minIndex == -1 || values[minIndex] != null && values[minIndex] > values[i] ? i : minIndex; 39 | } 40 | 41 | if (minIndex == -1) 42 | break; 43 | 44 | res.add(values[minIndex]); 45 | values[minIndex] = null; 46 | } 47 | 48 | return res; 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/google/medium/_596/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.google.medium._596; 2 | 3 | import com.vaani.dsa.ds.algos.tree.binary.InvertBinaryTree; 4 | import com.vaani.dsa.ds.core.tree.binarytree.simple.BinaryTreeNode; 5 | 6 | /* 7 | Invert a binary tree. 8 | 9 | For example, given the following tree: 10 | 11 | ``` 12 | a 13 | / \ 14 | b c 15 | / \ / 16 | d e f 17 | ``` 18 | 19 | should become: 20 | 21 | ``` 22 | a 23 | / \ 24 | c b 25 | \ / \ 26 | f e d 27 | ``` 28 | 29 | */ 30 | public class Solution { 31 | 32 | public static void main(String[] args) { 33 | InvertBinaryTree invertBinaryTree = new InvertBinaryTree(); 34 | BinaryTreeNode tree = new BinaryTreeNode(1); 35 | invertBinaryTree.invertTreeIterative(tree); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/ibm/_231/#231.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/ibm/_231/#231.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/ibm/_231/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.ibm._231; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | System.out.println(rearrange("aaabbc")); // ababac 10 | System.out.println(rearrange("a")); // a 11 | System.out.println(rearrange("aaab")); // None 12 | System.out.println(rearrange("")); // None 13 | } 14 | 15 | public static String rearrange(String str) { 16 | char[] arr = groupLetters(str); 17 | 18 | if (arr == null) 19 | return "None"; 20 | 21 | for (int i = 1, lst = arr.length - 1; i < arr.length; i++) { 22 | if (arr[i] != arr[i - 1]) 23 | continue; 24 | if (arr[i] == arr[lst]) 25 | return "None"; 26 | arr[i] = arr[lst]; 27 | arr[lst] = arr[i - 1]; 28 | } 29 | 30 | return new String(arr); 31 | } 32 | 33 | private static char[] groupLetters(String str) { 34 | if (str == null || str.isEmpty()) 35 | return null; 36 | if (str.length() == 1) 37 | return str.toCharArray(); 38 | 39 | int[] counts = new int[26]; 40 | str = str.toLowerCase(); 41 | 42 | for (int i = 0; i < str.length(); i++) 43 | counts[str.charAt(i) - 'a']++; 44 | 45 | char[] arr = new char[str.length()]; 46 | 47 | for (int i = 0, j = 0; i < counts.length; i++) 48 | for (int k = 0; k < counts[i]; k++) 49 | arr[j++] = (char)('a' + i); 50 | 51 | return arr; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/janestreet/_116/#116.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/janestreet/_116/#116.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/janestreet/_116/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.janestreet._116; 2 | 3 | import java.util.concurrent.atomic.AtomicInteger; 4 | 5 | /** 6 | * 7 | * */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | Node root = generate(); 12 | System.out.println(root.val()); 13 | System.out.println(root.left().val()); 14 | System.out.println(root.right().val()); 15 | } 16 | 17 | public static Node generate() { 18 | return new Node(); 19 | } 20 | 21 | public static final class Node { 22 | 23 | private static final AtomicInteger COUNT = new AtomicInteger(); 24 | 25 | private final int val = COUNT.incrementAndGet(); 26 | private Node left; 27 | private Node right; 28 | 29 | public int val() { 30 | return val; 31 | } 32 | 33 | public Node left() { 34 | if (left == null) 35 | left = new Node(); 36 | return left; 37 | } 38 | 39 | public Node right() { 40 | if (right == null) 41 | right = new Node(); 42 | return right; 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/janestreet/_163/#163.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/janestreet/_163/#163.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/janestreet/_163/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.janestreet._163; 2 | 3 | import java.util.Deque; 4 | import java.util.LinkedList; 5 | 6 | /** 7 | * 8 | * */ 9 | public class Solution { 10 | 11 | public static void main(String... args) { 12 | System.out.println(evaluateReversePolishNotation(new Object[] { 5, 3, '+' })); // 8 13 | System.out.println(evaluateReversePolishNotation(new Object[] { 15, 7, 1, 1, '+', '-', '/', 3, '*', 2, 1, 1, '+', '+', '-' })); // 5 14 | } 15 | 16 | public static double evaluateReversePolishNotation(Object[] expr) { 17 | Deque stack = new LinkedList<>(); 18 | 19 | 20 | for (Object item : expr) { 21 | if (item instanceof Number) 22 | stack.push(((Number)item).doubleValue()); 23 | else { 24 | double two = stack.pop(); 25 | double one = stack.pop(); 26 | char op = (Character)item; 27 | 28 | if (op == '+') 29 | stack.push(one + two); 30 | else if (op == '-') 31 | stack.push(one - two); 32 | else if (op == '/') 33 | stack.push(one / two); 34 | else if (op == '*') 35 | stack.push(one * two); 36 | } 37 | } 38 | 39 | return stack.pop(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/janestreet/medium/_005/#005.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/janestreet/medium/_005/#005.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/janestreet/medium/_005/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.janestreet.medium._005; 2 | 3 | import java.util.function.Function; 4 | 5 | /** 6 | * 7 | * */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | System.out.println(car(cons(3, 4))); 12 | System.out.println(cdr(cons(3, 4))); 13 | } 14 | 15 | private static Function, Integer> cons(int a, int b) { 16 | return func -> func.apply(new Pair(a, b)); 17 | } 18 | 19 | private static int car(Function, Integer> cons) { 20 | return cons.apply(pair -> pair.a); 21 | } 22 | 23 | private static int cdr(Function, Integer> cons) { 24 | return cons.apply(pair -> pair.b); 25 | } 26 | 27 | private static class Pair { 28 | 29 | int a; 30 | int b; 31 | 32 | public Pair(int a, int b) { 33 | this.a = a; 34 | this.b = b; 35 | } 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/linkedin/_089/#089.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/linkedin/_089/#089.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/linkedin/_123/#123.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/linkedin/_123/#123.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/linkedin/_123/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.linkedin._123; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | System.out.println(isNumber("10")); 10 | System.out.println(isNumber("-10")); 11 | System.out.println(isNumber("10.1")); 12 | System.out.println(isNumber("-10.1")); 13 | System.out.println(isNumber("1e5")); 14 | System.out.println(); 15 | System.out.println(isNumber("a")); 16 | System.out.println(isNumber("x 1")); 17 | System.out.println(isNumber("a-2")); 18 | System.out.println(isNumber("-")); 19 | } 20 | 21 | public static boolean isNumber(String str) { 22 | try { 23 | Double.parseDouble(str); 24 | return true; 25 | } catch(Exception e) { 26 | return false; 27 | } 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/linkedin/_150/#150.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/linkedin/_150/#150.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/linkedin/_150/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.linkedin._150; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.Comparator; 6 | import java.util.List; 7 | import java.util.PriorityQueue; 8 | import java.util.Queue; 9 | import java.util.function.Function; 10 | 11 | /** 12 | * 13 | * */ 14 | public class Solution { 15 | 16 | public static void main(String... args) { 17 | List points = Arrays.asList(new Point(0, 0), new Point(5, 4), new Point(3, 1)); 18 | getClosest(points, new Point(1, 2), 2).forEach(point -> System.out.println(point.x + ":" + point.y)); 19 | } 20 | 21 | public static List getClosest(List points, Point centralPoint, int k) { 22 | final Function getDistance = point -> (point.x - centralPoint.x) * (point.x - centralPoint.x) 23 | + (point.y - centralPoint.y) * (point.y - centralPoint.y); 24 | final Comparator sortByDistanceDesc = (one, two) -> Integer.compare(getDistance.apply(two), getDistance.apply(one)); 25 | 26 | Queue queue = new PriorityQueue<>(sortByDistanceDesc); 27 | 28 | points.forEach(point -> { 29 | if (queue.size() < k) 30 | queue.add(point); 31 | else if (sortByDistanceDesc.compare(queue.element(), point) < 0) { 32 | queue.remove(); 33 | queue.add(point); 34 | } 35 | }); 36 | 37 | return new ArrayList<>(queue); 38 | } 39 | 40 | private static final class Point { 41 | 42 | private final int x; 43 | private final int y; 44 | 45 | public Point(int x, int y) { 46 | this.x = x; 47 | this.y = y; 48 | } 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/lyft/_102/#102.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/lyft/_102/#102.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/lyft/_102/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.lyft._102; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * 7 | * 8 | */ 9 | public class Solution { 10 | 11 | public static void main(String... args) { 12 | System.out.println(Arrays.toString(getSum(new int[]{1, 2, 3, 4, 5}, 9))); 13 | } 14 | 15 | public static int[] getSum(int[] arr, int k) { 16 | int i = 0; 17 | int j = 0; 18 | int sum = 0; 19 | 20 | while (j <= arr.length && sum != k) { 21 | if (sum < k) 22 | sum += arr[j++]; 23 | else 24 | sum -= arr[i++]; 25 | } 26 | 27 | int[] res = new int[j - i]; 28 | System.arraycopy(arr, i, res, 0, res.length); 29 | 30 | return res; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/microsoft/_096/#096.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/microsoft/_096/#096.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/microsoft/_096/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.microsoft._096; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.Collections; 6 | import java.util.List; 7 | 8 | /** 9 | * 10 | * */ 11 | public class Solution { 12 | 13 | public static void main(String... args) { 14 | getAllPermutations(new int[] { 2, 3, 1 }).stream() 15 | .map(Arrays::toString) 16 | .forEach(System.out::println); 17 | } 18 | 19 | public static List getAllPermutations(int[] arr) { 20 | if (arr == null || arr.length == 0) 21 | return Collections.emptyList(); 22 | 23 | arr = Arrays.copyOf(arr, arr.length); 24 | Arrays.sort(arr); 25 | 26 | List res = new ArrayList<>(); 27 | res.add(Arrays.copyOf(arr, arr.length)); 28 | 29 | while (getNextPermutation(arr) != null) 30 | res.add(Arrays.copyOf(arr, arr.length)); 31 | 32 | return res; 33 | } 34 | 35 | private static int[] getNextPermutation(int[] arr) { 36 | for (int i = arr.length - 2; i >= 0; i--) { 37 | if (arr[i] >= arr[i + 1]) 38 | continue; 39 | 40 | int j = arr.length; 41 | 42 | while (arr[--j] < arr[i]) ; 43 | 44 | swap(arr, i, j); 45 | swapTail(arr, i + 1); 46 | return arr; 47 | } 48 | 49 | return null; 50 | } 51 | 52 | private static void swapTail(int[] arr, int i) { 53 | for (int j = arr.length - 1; i < j; i++, j--) 54 | swap(arr, i, j); 55 | } 56 | 57 | private static void swap(int[] arr, int i, int j) { 58 | int tmp = arr[i]; 59 | arr[i] = arr[j]; 60 | arr[j] = tmp; 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/microsoft/_099/#099.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/microsoft/_099/#099.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/microsoft/_099/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.microsoft._099; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | System.out.println(findLongestConsecutiveElementsLength(100, 4, 200, 1, 3, 2)); // 4 10 | } 11 | 12 | public static int findLongestConsecutiveElementsLength(int... arr) { 13 | int min = Integer.MAX_VALUE; 14 | int max = Integer.MIN_VALUE; 15 | 16 | for (int num : arr) { 17 | min = Math.min(min, num); 18 | max = Math.max(max, num); 19 | } 20 | 21 | boolean[] existed = new boolean[max - min + 1]; 22 | 23 | for (int num : arr) 24 | existed[num - min] = true; 25 | 26 | int res = 0; 27 | 28 | for (int i = 0, cur = 0; i < existed.length; i++) { 29 | if (existed[i]) 30 | res = Math.max(res, ++cur); 31 | else 32 | cur = 0; 33 | } 34 | 35 | return res; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/microsoft/_107/#107.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/microsoft/_107/#107.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/microsoft/_107/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.microsoft._107; 2 | 3 | import java.util.Collections; 4 | import java.util.Deque; 5 | import java.util.LinkedList; 6 | import java.util.List; 7 | import java.util.stream.Collectors; 8 | 9 | /** 10 | * 11 | * */ 12 | public class Solution { 13 | 14 | public static void main(String... args) { 15 | System.out.println(bfs(createTree()).stream() 16 | .map(node -> String.valueOf(node.data)) 17 | .collect(Collectors.joining(","))); // 1,2,3,4,5 18 | } 19 | 20 | private static Node createTree() { 21 | Node two = new Node(2); 22 | Node four = new Node(4); 23 | Node five = new Node(5); 24 | 25 | Node three = new Node(3); 26 | three.left = four; 27 | three.right = five; 28 | 29 | Node root = new Node(1); 30 | root.left = two; 31 | root.right = three; 32 | 33 | return root; 34 | } 35 | 36 | public static List bfs(Node root) { 37 | if (root == null) 38 | return Collections.emptyList(); 39 | 40 | List res = new LinkedList<>(); 41 | Deque queue = new LinkedList<>(); 42 | queue.add(root); 43 | 44 | while (!queue.isEmpty()) { 45 | Node node = queue.poll(); 46 | res.add(node); 47 | 48 | if (node.left != null) 49 | queue.add(node.left); 50 | if (node.right != null) 51 | queue.add(node.right); 52 | } 53 | 54 | return res; 55 | } 56 | 57 | private static final class Node { 58 | private final int data; 59 | private Node left; 60 | private Node right; 61 | 62 | public Node(int data) { 63 | this.data = data; 64 | } 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/microsoft/_120/#120.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/microsoft/_120/#120.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/microsoft/_120/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.microsoft._120; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | System.out.println(TwistSingleton.getInstance().id); // 1 10 | System.out.println(TwistSingleton.getInstance().id); // 2 11 | System.out.println(TwistSingleton.getInstance().id); // 1 12 | System.out.println(TwistSingleton.getInstance().id); // 2 13 | } 14 | 15 | public static final class TwistSingleton { 16 | private static final TwistSingleton ODD = new TwistSingleton(1); 17 | private static final TwistSingleton EVEN = new TwistSingleton(2); 18 | private static boolean odd = true; 19 | 20 | public static TwistSingleton getInstance() { 21 | if (odd) { 22 | odd = false; 23 | return ODD; 24 | } 25 | 26 | odd = true; 27 | return EVEN; 28 | } 29 | 30 | private final int id; 31 | 32 | private TwistSingleton(int id) { 33 | this.id = id; 34 | } 35 | 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/microsoft/_127/#127.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/microsoft/_127/#127.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/microsoft/_127/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.microsoft._127; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | System.out.println(convertToNumber(convertToLinkedList(54321))); // 54321 10 | System.out.println(convertToNumber(sum(convertToLinkedList(99), convertToLinkedList(25)))); // 124 11 | } 12 | 13 | public static Node sum(Node one, Node two) { 14 | int sum = convertToNumber(one) + convertToNumber(two); 15 | return convertToLinkedList(sum); 16 | } 17 | 18 | private static int convertToNumber(Node node) { 19 | StringBuilder buf = new StringBuilder(); 20 | 21 | while (node != null) { 22 | buf.append(node.digit); 23 | node = node.next; 24 | } 25 | 26 | return Integer.parseInt(buf.reverse().toString()); 27 | } 28 | 29 | private static Node convertToLinkedList(int num) { 30 | String str = String.valueOf(num); 31 | Node root = null; 32 | Node prv = null; 33 | 34 | for (int i = str.length() - 1; i >= 0; i--) { 35 | Node node = new Node(str.charAt(i)); 36 | 37 | if (prv != null) 38 | prv.next = node; 39 | prv = node; 40 | 41 | root = root == null ? node : root; 42 | } 43 | 44 | return root; 45 | } 46 | 47 | private static final class Node { 48 | private final char digit; 49 | private Node next; 50 | 51 | public Node(char digit) { 52 | assert Character.isDigit(digit); 53 | this.digit = digit; 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/microsoft/_200/#200.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/microsoft/_200/#200.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/microsoft/easy/_050/#050.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/microsoft/easy/_050/#050.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/microsoft/easy/_055/#055.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/microsoft/easy/_055/#055.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/microsoft/easy/_063/#063.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/microsoft/easy/_063/#063.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/microsoft/easy/_063/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.microsoft.easy._063; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | char[][] matrix = { 10 | {'F', 'A', 'C', 'I'}, 11 | {'O', 'B', 'Q', 'P'}, 12 | {'A', 'N', 'O', 'B'}, 13 | {'M', 'A', 'S', 'S'}}; 14 | System.out.println(isContainsWord(matrix, "FOAM")); // true 15 | System.out.println(isContainsWord(matrix, "MASS")); // true 16 | System.out.println(isContainsWord(matrix, "MASSS")); // false 17 | } 18 | 19 | public static boolean isContainsWord(char[][] matrix, String word) { 20 | for (int row = 0; row < matrix.length; row++) 21 | for (int col = 0; col < matrix[row].length; col++) 22 | if (checkRow(matrix, row, col, word) || checkColumn(matrix, row, col, word)) 23 | return true; 24 | return false; 25 | } 26 | 27 | private static boolean checkColumn(char[][] matrix, int row, int col, String word) { 28 | if (col + word.length() > matrix[row].length) 29 | return false; 30 | 31 | for (int i = 0; i < matrix[row].length; i++) 32 | if (matrix[row][col + i] != word.charAt(i)) 33 | return false; 34 | 35 | return true; 36 | } 37 | 38 | private static boolean checkRow(char[][] matrix, int row, int col, String word) { 39 | if (row + word.length() > matrix.length) 40 | return false; 41 | 42 | for (int i = 0; i < matrix.length; i++) 43 | if (matrix[row + i][col] != word.charAt(i)) 44 | return false; 45 | 46 | return true; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/microsoft/hard/_075/#075.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/microsoft/hard/_075/#075.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/microsoft/hard/_075/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.microsoft.hard._075; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | System.out.println(findLongestIncreasingSubsequence(new int[]{0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15})); // 6 10 | } 11 | 12 | public static int findLongestIncreasingSubsequence(int[] arr) { 13 | int[] cache = new int[arr.length]; 14 | int res = 0; 15 | 16 | for (int i = 0, max = 0; i < arr.length; i++, max = 0) { 17 | for (int j = 0; j < i; j++) 18 | if (arr[j] < arr[i]) 19 | max = Math.max(max, cache[j]); 20 | 21 | res = Math.max(res, cache[i] = max + 1); 22 | } 23 | 24 | return res; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/microsoft/medium/_022/#022.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/microsoft/medium/_022/#022.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/microsoft/medium/_022/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.microsoft.medium._022; 2 | 3 | import java.util.*; 4 | import java.util.stream.Collectors; 5 | 6 | /** 7 | * 8 | * */ 9 | public class Solution { 10 | public static void main(String[] args) { 11 | System.out.println(splitWords(words("quick", "brown", "the", "fox"), "thequickbrownfox")); 12 | System.out.println(splitWords(words("bed", "bath", "bedbath", "and", "beyond"), "bedbathandbeyond")); 13 | System.out.println(splitWords(words("the", "theremin"), "theremin")); 14 | System.out.println(splitWords(words("a", "aa", "aaa", "aaaa", "aaaaa"), "aaaaab")); 15 | } 16 | 17 | private static Set words(String... words) { 18 | return Arrays.stream(words).collect(Collectors.toSet()); 19 | } 20 | 21 | public static List splitWords(Set words, String str) { 22 | Deque queue = new LinkedList<>(); 23 | splitWords(queue, words, str, 0); 24 | return queue.isEmpty() ? null : new ArrayList<>(queue); 25 | } 26 | 27 | private static boolean splitWords(Deque queue, Set words, String str, int offs) { 28 | if (offs >= str.length()) 29 | return true; 30 | 31 | for (String word : words) { 32 | if (!str.startsWith(word, offs)) 33 | continue; 34 | 35 | queue.addLast(word); 36 | 37 | if (splitWords(queue, words, str, offs + word.length())) 38 | return true; 39 | 40 | queue.removeLast(); 41 | } 42 | 43 | return false; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/mongodb/_155/#155.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/mongodb/_155/#155.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/mongodb/_155/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.mongodb._155; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * 8 | * */ 9 | public class Solution { 10 | 11 | public static void main(String[] args) { 12 | System.out.println(findMajorityElement(new int[] { 1, 2, 1, 1, 3, 4, 0 })); // 1 13 | } 14 | 15 | public static int findMajorityElement(int[] arr) { 16 | Map map = new HashMap<>(); 17 | int res = -1; 18 | 19 | for (int num : arr) { 20 | int count = map.getOrDefault(num, 0) + 1; 21 | 22 | if (res == -1 || count > map.getOrDefault(res, 0)) 23 | res = num; 24 | 25 | map.put(num, count); 26 | } 27 | 28 | return res; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/others/_129/#129.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/others/_129/#129.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/others/_129/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.others._129; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | public static void main(String[] args) { 8 | System.out.println(floorSqrt(81)); // 9 9 | System.out.println(floorSqrt(9)); // 3 10 | System.out.println(floorSqrt(200)); // 14 11 | } 12 | 13 | public static int floorSqrt(int n) { 14 | if (n == 0 || n == 1) 15 | return n; 16 | 17 | int lo = 0; 18 | int hi = n; 19 | int sqrt = 0; 20 | 21 | while (lo <= hi) { 22 | int mid = (lo + hi) / 2; 23 | 24 | if (mid * mid == n) 25 | return mid; 26 | 27 | if (mid * mid < n) { 28 | lo = mid + 1; 29 | sqrt = mid; 30 | } else 31 | hi = mid - 1; 32 | } 33 | 34 | return sqrt; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/others/_147/#147.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/others/_147/#147.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/others/_147/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.others._147; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * 7 | * */ 8 | public class Solution { 9 | public static void main(String[] args) { 10 | System.out.println(Arrays.toString(sort(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }))); 11 | System.out.println(Arrays.toString(sort(new int[] { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }))); 12 | System.out.println(Arrays.toString(sort(new int[] { 18, 3, 11, 5, 9, 17, 18, 13, 5, 7 }))); 13 | } 14 | 15 | public static int[] sort(int[] arr) { 16 | // bubble sort (but Pancake sorting should be) 17 | for (int left = 0, right = arr.length - 1; left < right; left++, right--) { 18 | for (int i = left; i < right; i++) 19 | if (arr[i] > arr[i + 1]) 20 | reverse(arr, i, i + 1); 21 | 22 | for (int i = right; i > left; i--) 23 | if (arr[i - 1] > arr[i]) 24 | reverse(arr, i - 1, i); 25 | } 26 | 27 | return arr; 28 | } 29 | 30 | private static void reverse(int[] arr, int i, int j) { 31 | while (i < j) { 32 | int tmp = arr[i]; 33 | arr[i] = arr[j]; 34 | arr[j] = tmp; 35 | i++; 36 | j--; 37 | } 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/others/_151/#151.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/others/_151/#151.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/others/_151/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.others._151; 2 | 3 | import java.util.stream.Collectors; 4 | import java.util.stream.IntStream; 5 | 6 | /** 7 | * 8 | * */ 9 | public class Solution { 10 | public static void main(String[] args) { 11 | char[][] image = { 12 | { 'B', 'B', 'W' }, 13 | { 'W', 'W', 'W' }, 14 | { 'W', 'W', 'W' }, 15 | { 'B', 'W', 'B' } }; 16 | print(image); 17 | replaceColor(image, 'G', 2, 2); 18 | print(image); 19 | } 20 | 21 | private static void print(char[][] image) { 22 | for (char[] row : image) 23 | System.out.println(IntStream.range(0, row.length) 24 | .mapToObj(col -> String.valueOf(row[col])) 25 | .collect(Collectors.joining(" "))); 26 | System.out.println(); 27 | } 28 | 29 | public static void replaceColor(char[][] image, char dest, int row, int col) { 30 | if (image[row][col] != dest) 31 | dfs(image, image[row][col], dest, row, col); 32 | } 33 | 34 | private static void dfs(char[][] image, char src, char dest, int row, int col) { 35 | if (row < 0 || row >= image.length) 36 | return; 37 | if (col < 0 || col >= image[row].length) 38 | return; 39 | if (image[row][col] != src) 40 | return; 41 | 42 | image[row][col] = dest; 43 | dfs(image, src, dest, row, col + 1); 44 | dfs(image, src, dest, row, col - 1); 45 | dfs(image, src, dest, row + 1, col); 46 | dfs(image, src, dest, row - 1, col); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/others/_153/#153.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/others/_153/#153.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/others/_153/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.others._153; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String[] args) { 9 | System.out.println(getMinDistance("dog cat hello cat dog dog hello cat world", "hello", "world")); // 1 10 | } 11 | 12 | public static int getMinDistance(String str, String one, String two) { 13 | String[] words = str.split("\\s+"); 14 | 15 | int posOne = -1; 16 | int posTwo = -1; 17 | int res = Integer.MAX_VALUE; 18 | 19 | for (int i = 0; i < words.length; i++) { 20 | if (words[i].equals(one)) 21 | posOne = i; 22 | else if (words[i].equals(two)) 23 | posTwo = i; 24 | else 25 | continue; 26 | 27 | if (posOne != -1 && posTwo != -1) 28 | res = Math.min(res, Math.abs(posTwo - posOne) - 1); 29 | } 30 | 31 | return res == Integer.MAX_VALUE ? -1 : res; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/palantir/_095/#095.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/palantir/_095/#095.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/palantir/_095/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.palantir._095; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * 7 | * */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | System.out.println(Arrays.toString(findNextPermutation(new int[] { 1, 2, 3 }))); // [1, 3, 2] 12 | System.out.println(Arrays.toString(findNextPermutation(new int[] { 1, 3, 2 }))); // [2, 1, 3] 13 | System.out.println(Arrays.toString(findNextPermutation(new int[] { 2, 1, 3 }))); // [2, 3, 1] 14 | System.out.println(Arrays.toString(findNextPermutation(new int[] { 2, 3, 1 }))); // [3, 1, 2] 15 | System.out.println(Arrays.toString(findNextPermutation(new int[] { 3, 1, 2 }))); // [3, 2, 1] 16 | System.out.println(Arrays.toString(findNextPermutation(new int[] { 3, 2, 1 }))); // [1, 2, 3] 17 | } 18 | 19 | public static int[] findNextPermutation(int[] arr) { 20 | if (arr == null) 21 | return null; 22 | if (arr.length <= 1) 23 | return arr; 24 | 25 | for (int i = arr.length - 2; i >= 0; i--) { 26 | if (arr[i] >= arr[i + 1]) 27 | continue; 28 | 29 | int j = arr.length; 30 | 31 | while (arr[--j] < arr[i]) ; 32 | 33 | swap(arr, i, j); 34 | swapTail(arr, i + 1); 35 | 36 | return arr; 37 | } 38 | 39 | swapTail(arr, 0); 40 | 41 | return arr; 42 | } 43 | 44 | private static void swapTail(int[] arr, int i) { 45 | for (int j = arr.length - 1; i < j; i++, j--) 46 | swap(arr, i, j); 47 | } 48 | 49 | private static void swap(int[] arr, int i, int j) { 50 | int tmp = arr[i]; 51 | arr[i] = arr[j]; 52 | arr[j] = tmp; 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/palantir/_202/#202.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/palantir/_202/#202.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/palantir/_202/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.palantir._202; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | System.out.println(isPalindrome(121)); // true 10 | System.out.println(isPalindrome(888)); // true 11 | System.out.println(isPalindrome(678)); // true 12 | } 13 | 14 | public static boolean isPalindrome(int val) { 15 | int width = getWidth(val); 16 | 17 | for (int i = 0, j = width - 1; i < j; i++, j--) 18 | if (getDigit(val, i) != getDigit(val, j)) 19 | return false; 20 | 21 | return true; 22 | } 23 | 24 | private static int getWidth(int val) { 25 | int width = 0; 26 | 27 | while (val != 0) { 28 | width++; 29 | val /= 10; 30 | } 31 | 32 | return width; 33 | } 34 | 35 | private static int getDigit(int val, int i) { 36 | while (i-- > 0) { 37 | val /= 10; 38 | } 39 | 40 | return val % 10; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/palantir/_241/#241.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/palantir/_241/#241.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/palantir/_241/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.palantir._241; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * 7 | * */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | System.out.println(calcIndex(new int[] { 4, 3, 0, 1, 5 })); // 3 12 | } 13 | 14 | public static int calcIndex(int[] papers) { 15 | Arrays.sort(papers); 16 | 17 | for (int i = papers.length - 1; 0 <= i; i--) 18 | if (papers[i] <= papers.length - i) 19 | return papers[i]; 20 | 21 | return 0; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/palantir/easy/_028/#028.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/palantir/easy/_028/#028.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/palantir/easy/_028/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.palantir.easy._028; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.List; 6 | 7 | /** 8 | * 9 | * */ 10 | public class Solution { 11 | 12 | public static void main(String... args) { 13 | justify(Arrays.asList("the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"), 16).forEach(System.out::println); 14 | } 15 | 16 | public static List justify(List words, int k) { 17 | List lines = new ArrayList<>(); 18 | List line = new ArrayList<>(); 19 | int length = 0; 20 | 21 | for (String word : words) { 22 | if (length + line.size() + line.size() > k) { 23 | lines.add(justify(line, length, k)); 24 | line.clear(); 25 | length = 0; 26 | } 27 | 28 | line.add(word); 29 | length += word.length(); 30 | } 31 | 32 | if (!line.isEmpty()) 33 | lines.add(justify(line, length, k)); 34 | 35 | return lines; 36 | } 37 | 38 | private static String justify(List words, int length, int k) { 39 | int[] spaces = new int[words.size() - 1]; 40 | 41 | while (length < k) { 42 | for (int i = 0; i < spaces.length && length < k; i++) { 43 | spaces[i]++; 44 | length++; 45 | } 46 | } 47 | 48 | int i = 0; 49 | StringBuilder buf = new StringBuilder(); 50 | 51 | for (String word : words) { 52 | buf.append(word); 53 | 54 | if (i < spaces.length) { 55 | for (int j = 0; j < spaces[i]; j++) 56 | buf.append(' '); 57 | 58 | i++; 59 | } 60 | } 61 | 62 | return buf.toString(); 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/pinterest/_106/#106.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/pinterest/_106/#106.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/pinterest/_106/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.pinterest._106; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | System.out.println(check1(2, 0, 1, 0)); // true 10 | System.out.println(check1(1, 1, 0, 1)); // false 11 | System.out.println(check1(2, 3, 1, 0, 0)); // true 12 | 13 | System.out.println(check2(2, 0, 1, 0)); // true 14 | System.out.println(check2(1, 1, 0, 1)); // false 15 | System.out.println(check2(2, 3, 1, 0, 0)); // true 16 | } 17 | 18 | public static boolean check1(int... arr) { 19 | boolean[] tmp = new boolean[arr.length]; 20 | tmp[tmp.length - 1] = true; 21 | 22 | for (int i = tmp.length - 2; i >= 0; i--) { 23 | if (arr[i] + i >= arr.length) 24 | tmp[i] = true; 25 | else { 26 | for (int j = i + 1; j < arr[i] + i + 1; j++) { 27 | if (tmp[j]) { 28 | tmp[i] = true; 29 | break; 30 | } 31 | } 32 | } 33 | } 34 | 35 | return tmp[0]; 36 | } 37 | 38 | public static boolean check2(int... arr) { 39 | for (int i = 0, max = 1; i < arr.length - 1; i++) 40 | if ((max = Math.max(max - 1, arr[i])) == 0) 41 | return false; 42 | 43 | return true; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/riotgames/_132/#132.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/riotgames/_132/#132.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/riotgames/_132/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.riotgames._132; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * 8 | * */ 9 | public class Solution { 10 | 11 | public static void main(String... args) { 12 | } 13 | 14 | public static class HitCounter { 15 | private final Map map = new HashMap<>(); 16 | private int total; 17 | 18 | public void record(long timestamp) { 19 | map.put(timestamp, map.getOrDefault(timestamp, 0) + 1); 20 | total++; 21 | } 22 | 23 | public int total() { 24 | return total; 25 | } 26 | 27 | public int range(long lower, long upper) { 28 | int res = 0; 29 | 30 | for (Map.Entry entry : map.entrySet()) 31 | if (lower <= entry.getKey() && entry.getKey() <= upper) 32 | res += entry.getValue(); 33 | 34 | return res; 35 | } 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/slack/_158/#158.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/slack/_158/#158.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/slack/_158/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.slack._158; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String[] args) { 9 | int[][] board = { 10 | { 0, 0, 1 }, 11 | { 0, 0, 1 }, 12 | { 1, 0, 0 } }; 13 | System.out.println(findWaysAmount(board)); // 2 14 | } 15 | 16 | public static int findWaysAmount(int[][] board) { 17 | final int width = board[0].length; 18 | final int height = board.length; 19 | 20 | int[] arr = new int[width]; 21 | arr[0] = 1; 22 | 23 | for (int row = 0; row < height; row++) { 24 | for (int col = 0; col < width; col++) { 25 | if (board[row][col] == 1) 26 | arr[col] = 0; 27 | else if (col > 0) 28 | arr[col] += arr[col - 1]; 29 | } 30 | } 31 | 32 | return arr[width - 1]; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/snapchat/_131/#131.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/snapchat/_131/#131.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/snapchat/easy/_021/#021.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/snapchat/easy/_021/#021.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/snapchat/easy/_021/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.snapchat.easy._021; 2 | 3 | import java.util.Map; 4 | import java.util.TreeMap; 5 | 6 | /** 7 | * 8 | * */ 9 | public class Solution { 10 | 11 | public static void main(String... args) { 12 | System.out.println(getRequiredRoomsAmount(new Interval[]{ 13 | new Interval(30, 75), 14 | new Interval(0, 50), 15 | new Interval(60, 150)})); 16 | } 17 | 18 | private static final class Interval { 19 | private final int start; 20 | private final int end; 21 | 22 | public Interval(int start, int end) { 23 | this.start = start; 24 | this.end = end; 25 | } 26 | } 27 | 28 | public static int getRequiredRoomsAmount(Interval[] intervals) { 29 | Map map = new TreeMap<>(); 30 | 31 | for (Interval interval : intervals) { 32 | map.put(interval.start, map.getOrDefault(interval.start, 0) + 1); 33 | map.put(interval.end, map.getOrDefault(interval.end, 0) - 1); 34 | } 35 | 36 | int res = 0; 37 | int count = 0; 38 | 39 | for (int delta : map.values()) 40 | res = Math.max(res, count += delta); 41 | 42 | return res; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/snapchat/easy/_077/#077.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/snapchat/easy/_077/#077.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/snapchat/easy/_077/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.snapchat.easy._077; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.Map; 6 | import java.util.TreeMap; 7 | 8 | /** 9 | * 10 | * */ 11 | public class Solution { 12 | 13 | public static void main(String... args) { 14 | List res = mergeIntervals(new Interval[]{new Interval(1, 3), new Interval(5, 8), new Interval(4, 10), new Interval(20, 25)}); 15 | res.forEach(interval -> System.out.format("[%d:%d]\n", interval.from, interval.to)); 16 | } 17 | 18 | public static List mergeIntervals(Interval[] intervals) { 19 | Map map = new TreeMap<>(); 20 | 21 | for (Interval interval : intervals) { 22 | map.put(interval.from, map.getOrDefault(interval.from, 0) + 1); 23 | map.put(interval.to, map.getOrDefault(interval.to, 0) - 1); 24 | } 25 | 26 | List res = new ArrayList<>(); 27 | int from = -1; 28 | int count = 0; 29 | 30 | for (Map.Entry entry : map.entrySet()) { 31 | count += entry.getValue(); 32 | 33 | if (entry.getValue() > 0) 34 | from = from == -1 ? entry.getKey() : from; 35 | else if (count == 0) { 36 | res.add(new Interval(from, entry.getKey())); 37 | from = -1; 38 | } 39 | } 40 | 41 | return res; 42 | } 43 | 44 | private static final class Interval { 45 | private final int from; 46 | private final int to; 47 | 48 | public Interval(int from, int to) { 49 | this.from = from; 50 | this.to = to; 51 | } 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/square/_103/#103.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/square/_103/#103.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/square/_103/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.square._103; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | System.out.println(getShortestSubstring("figehaeci", "aei")); 10 | } 11 | 12 | private static String getShortestSubstring(String str, String test) { 13 | String res = null; 14 | 15 | for (int i = 0, j = i; i < str.length() - test.length(); i++, j = i) { 16 | if (test.indexOf(str.charAt(i)) == -1) 17 | continue; 18 | 19 | for (int k = 0, pos; k < test.length(); k++) 20 | if (test.charAt(k) != str.charAt(i) && (pos = str.indexOf(test.charAt(k), i)) != -1) 21 | j = Math.max(j, pos); 22 | 23 | if (res == null || res.length() > j + 1 - i) 24 | res = str.substring(i, j + 1); 25 | } 26 | 27 | return res; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/square/_162/#162.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/square/_162/#162.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/square/_162/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.square._162; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.HashMap; 6 | import java.util.List; 7 | import java.util.Map; 8 | 9 | /** 10 | * 11 | * */ 12 | public class Solution { 13 | 14 | public static void main(String... args) { 15 | List words = Arrays.asList("dog", "cat", "apple", "apricot", "fish"); 16 | List uniquePrefixes = getShortestUniquePrefix(words); 17 | uniquePrefixes.forEach(System.out::println); // d, c, app, apr, f 18 | } 19 | 20 | public static List getShortestUniquePrefix(List words) { 21 | Map prefixesAmount = new HashMap<>(); 22 | 23 | for (String word : words) { 24 | for (int i = 1; i <= word.length(); i++) { 25 | String prefix = word.substring(0, i); 26 | int amount = prefixesAmount.getOrDefault(prefix, 0); 27 | prefixesAmount.put(prefix, amount + 1); 28 | } 29 | } 30 | 31 | List uniquePrefixes = new ArrayList<>(); 32 | 33 | for (String word : words) { 34 | int size = uniquePrefixes.size(); 35 | 36 | for (int i = 1; i <= word.length(); i++) { 37 | String prefix = word.substring(0, i); 38 | 39 | if (prefixesAmount.get(prefix) == 1) { 40 | uniquePrefixes.add(prefix); 41 | break; 42 | } 43 | } 44 | 45 | if (uniquePrefixes.size() == size) 46 | throw new RuntimeException("Cannot get unique prefix for '" + word + '\''); 47 | } 48 | 49 | return uniquePrefixes; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/square/_244/#244.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/square/_244/#244.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/square/_244/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.square._244; 2 | 3 | import java.util.Arrays; 4 | import java.util.HashSet; 5 | import java.util.LinkedList; 6 | import java.util.List; 7 | import java.util.Set; 8 | 9 | /** 10 | * 11 | * 12 | */ 13 | public class Solution { 14 | 15 | public static void main(String... args) { 16 | System.out.println(Arrays.toString(getPrimeNumbersLessThan(100))); 17 | } 18 | 19 | public static int[] getPrimeNumbersLessThan(int n) { 20 | PrimeNumberGenerator primeGenerator = new PrimeNumberGenerator(); 21 | List primes = new LinkedList<>(); 22 | 23 | while (true) { 24 | int prime = primeGenerator.next(); 25 | 26 | if (prime >= n) 27 | break; 28 | 29 | primes.add(prime); 30 | } 31 | 32 | return primes.stream().mapToInt(i -> i).toArray(); 33 | } 34 | 35 | public static final class PrimeNumberGenerator { 36 | 37 | private final Set primes = new HashSet<>(); 38 | private int prime = 1; 39 | 40 | public int next() { 41 | int n = prime; 42 | 43 | do { 44 | n++; 45 | } while (isMarked(n)); 46 | 47 | primes.add(n); 48 | prime = n; 49 | 50 | return prime; 51 | } 52 | 53 | private boolean isMarked(int n) { 54 | for (int prime : primes) 55 | if (n % prime == 0) 56 | return true; 57 | return false; 58 | } 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/stripe/_097/#097.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/stripe/_097/#097.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/stripe/_173/#173.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/stripe/_173/#173.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/stripe/_173/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.stripe._173; 2 | 3 | import java.util.Collections; 4 | import java.util.LinkedHashMap; 5 | import java.util.Map; 6 | 7 | /** 8 | * 9 | * */ 10 | public class Solution { 11 | 12 | public static void main(String... args) { 13 | Map foo = new LinkedHashMap<>(); 14 | foo.put("a", 5); 15 | foo.put("bar", Collections.singletonMap("baz", 8)); 16 | 17 | Map dic = new LinkedHashMap<>(); 18 | dic.put("key", 3); 19 | dic.put("foo", foo); 20 | 21 | Map map = flatten(dic); 22 | map.forEach((key, value) -> System.out.println(key + ": " + value)); 23 | } 24 | 25 | public static Map flatten(Map dic) { 26 | return flatten(null, dic, new LinkedHashMap<>()); 27 | } 28 | 29 | private static Map flatten(String prefix, Map dic, Map map) { 30 | for (Map.Entry entry : dic.entrySet()) { 31 | String key = prefix == null ? entry.getKey() : prefix + '.' + entry.getKey(); 32 | Object value = entry.getValue(); 33 | 34 | if (value instanceof Map) 35 | flatten(key, (Map)value, map); 36 | else 37 | map.put(key, String.valueOf(value)); 38 | } 39 | 40 | return map; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/stripe/_191/#191.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/stripe/_191/#191.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/stripe/hard/_004/#004.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/stripe/hard/_004/#004.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/stripe/hard/_004/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.stripe.hard._004; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | System.out.println(findLowestMissingPositive1(new int[] { 3, 4, -1, 1 })); 10 | System.out.println(findLowestMissingPositive1(new int[] { 1, 2, 0 })); 11 | } 12 | 13 | public static int findLowestMissingPositive1(int[] arr) { 14 | if (arr == null || arr.length < 2) 15 | return -1; 16 | 17 | for (int i = 0; i < arr.length; ) { 18 | if (arr[i] <= 0 || arr[i] > arr.length || arr[i] - 1 == i) 19 | i++; 20 | else 21 | swap(arr, i, arr[i] - 1); 22 | } 23 | 24 | for (int i = 0; i < arr.length; i++) 25 | if (arr[i] - 1 != i) 26 | return i + 1; 27 | 28 | return -1; 29 | } 30 | 31 | private static void swap(int[] arr, int i, int j) { 32 | int tmp = arr[i]; 33 | arr[i] = arr[j]; 34 | arr[j] = tmp; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/twitter/_112/#112.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/twitter/_112/#112.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/twitter/easy/_016/#016.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/twitter/easy/_016/#016.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/twitter/medium/_011/#011.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/twitter/medium/_011/#011.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/uber/_160/#160.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/uber/_160/#160.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/uber/_166/#166.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/uber/_166/#166.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/uber/_166/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.uber._166; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | int[][] data = { 10 | { 1, 2 }, 11 | { 3 }, 12 | {}, 13 | { 4, 5, 6 } }; 14 | 15 | Iterator2D it = new Iterator2D(data); 16 | 17 | for (int i = 0; i < 7; i++) 18 | System.out.println(it.next()); 19 | } 20 | 21 | public static final class Iterator2D { 22 | private final int[][] data; 23 | private int row = 0; 24 | private int col = -1; 25 | private boolean used = true; 26 | 27 | public Iterator2D(int[][] data) { 28 | this.data = data; 29 | } 30 | 31 | public int next() { 32 | if (!findNextElement()) 33 | throw new RuntimeException("No more elements"); 34 | 35 | used = true; 36 | return data[row][col]; 37 | } 38 | 39 | public boolean has_next() { 40 | return findNextElement(); 41 | } 42 | 43 | private boolean findNextElement() { 44 | if (row >= data.length) 45 | return false; 46 | if (!used) 47 | return true; 48 | 49 | used = false; 50 | 51 | while (row < data.length) { 52 | col++; 53 | 54 | if (col < data[row].length) 55 | return true; 56 | 57 | col = -1; 58 | row++; 59 | } 60 | 61 | return false; 62 | } 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/uber/_203/#203.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/uber/_203/#203.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/uber/_203/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.uber._203; 2 | 3 | /** 4 | * 5 | * */ 6 | public class Solution { 7 | 8 | public static void main(String... args) { 9 | System.out.println(findOffs(new int[] { 5, 7, 10, 3, 4 })); // 3 10 | System.out.println(findOffs(new int[] { 4, 5, 6, 7, 1, 2, 3 })); // 4 11 | System.out.println(findOffs(new int[] { 2, 1 })); // 1 12 | } 13 | 14 | public static int findOffs(int[] arr) { 15 | return find(arr, 0, arr.length - 1); 16 | } 17 | 18 | private static int find(int[] arr, int i, int j) { 19 | if (i == j) 20 | return i; 21 | int mid = (i + j) / 2; 22 | return arr[mid] < arr[j] ? find(arr, i, mid) : find(arr, mid + 1, j); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/uber/hard/_002/#002.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/uber/hard/_002/#002.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/uber/hard/_002/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.uber.hard._002; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * 7 | * */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | System.out.println(Arrays.toString(product(new int[] { 1, 2, 3, 4, 5 }))); 12 | System.out.println(Arrays.toString(product(new int[] { 3, 2, 1 }))); 13 | } 14 | 15 | public static int[] product(int[] arr) { 16 | if (arr == null || arr.length < 2) 17 | return null; 18 | 19 | int[] res = new int[arr.length]; 20 | long product = 1; 21 | 22 | for (int val : arr) 23 | product *= val; 24 | 25 | for (int i = 0; i < arr.length; i++) 26 | res[i] = (int)(product / arr[i]); 27 | 28 | return res; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/yelp/_081/#081.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/yelp/_081/#081.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/yelp/_081/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.yelp._081; 2 | 3 | import java.util.*; 4 | 5 | /** 6 | * 7 | * */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | System.out.println(String.join(" ", getWords(23))); 12 | } 13 | 14 | public static Set getWords(int num) { 15 | String digits = String.valueOf(num); 16 | Set words = new TreeSet<>(); 17 | 18 | for (int i = 0; i < digits.length(); i++) { 19 | char digit = digits.charAt(i); 20 | 21 | Set prvWords = words.isEmpty() ? Collections.emptySet() : new HashSet<>(words); 22 | words.clear(); 23 | 24 | for (char letter : getLetters(digit)) { 25 | if (prvWords.isEmpty()) 26 | words.add(String.valueOf(letter)); 27 | else 28 | for (String word : prvWords) 29 | words.add(word + letter); 30 | } 31 | } 32 | 33 | return words; 34 | } 35 | 36 | private static char[] getLetters(char digit) { 37 | return MAP.getOrDefault(digit, EMPTY); 38 | } 39 | 40 | private static final char[] EMPTY = new char[0]; 41 | private static final Map MAP = new HashMap<>(); 42 | 43 | static { 44 | MAP.put('2', new char[] { 'a', 'b', 'c' }); 45 | MAP.put('3', new char[] { 'd', 'e', 'f' }); 46 | MAP.put('4', new char[] { 'g', 'h', 'i' }); 47 | MAP.put('5', new char[] { 'j', 'k', 'l' }); 48 | MAP.put('6', new char[] { 'm', 'n', 'o' }); 49 | MAP.put('7', new char[] { 'p', 'q', 'r', 's' }); 50 | MAP.put('8', new char[] { 't', 'u', 'v' }); 51 | MAP.put('9', new char[] { 'w', 'x', 'y', 'z' }); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/zillow/_122/#122.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kinshuk4/daily-coding-problem-solutions/6defb493f41e152280a789f8bd5f499094bed4ca/java/daily-coding-problem/src/main/java/com/vaani/dcp/zillow/_122/#122.pdf -------------------------------------------------------------------------------- /java/daily-coding-problem/src/main/java/com/vaani/dcp/zillow/_122/Solution.java: -------------------------------------------------------------------------------- 1 | package com.vaani.dcp.zillow._122; 2 | 3 | import java.util.function.BiFunction; 4 | 5 | /** 6 | * 7 | * */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | int[][] matrix = 12 | { 13 | { 0, 3, 1, 1 }, 14 | { 2, 0, 0, 4 }, 15 | { 1, 5, 3, 1 } 16 | }; 17 | System.out.println(maxCoins(matrix)); 18 | } 19 | 20 | public static int maxCoins(int[][] matrix) { 21 | if (matrix == null) 22 | return 0; 23 | 24 | BiFunction get = 25 | (row, col) -> row < 0 || row >= matrix.length || col < 0 || col >= matrix[row].length ? 0 : matrix[row][col]; 26 | int width = matrix[0].length; 27 | 28 | for (int row = 0; row < matrix.length; row++) 29 | for (int col = 0; col < width; col++) 30 | matrix[row][col] += Math.max(get.apply(row - 1, col), get.apply(row, col - 1)); 31 | 32 | return matrix[matrix.length - 1][width - 1]; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /java/daily-coding-problem/target/classes/META-INF/daily-coding-problem.kotlin_module: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /java/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.vaani 8 | daily-coding-problem-parent 9 | 1.0-SNAPSHOT 10 | pom 11 | 12 | 9 13 | 1.9 14 | 1.9 15 | 16 | 17 | 18 | 19 | daily-coding-problem 20 | algorithms-java 21 | 22 | 23 | 24 | --------------------------------------------------------------------------------