├── Day01_SortThePeople.java ├── Day02_SortArrayByIncreasingFrequency.java ├── Day03_SortTheJumbledNumbers.java ├── Day04_SortAnArray.java ├── Day05_FindTheCitySmallestNoOFNeibours.java ├── Day06_MinimumCostToConvertString.java ├── Day07_ReverseString.java ├── Day08_FindTheDifference.java ├── Day09_SingleNumber.java ├── Day10_SoreOfAString.java ├── Day11_SmallestEvenMultiple ├── Day12_SumMultiples.java ├── Day13_PowerOfTwo.java ├── Day14_RotateString.java ├── Day15_PlusOne.java ├── Day16_ShuffleString.java ├── Day17_ClearDigits.java ├── Day18_LemonadeChange.java ├── Day19_Convert1DArrayInto2DArray.java ├── Day20_FindTheStudentThatWillReplaceTheChalk.java ├── Day21_SumOfDigitsOfStringAfterCovert.java ├── Day22_NumberOf1Bits.java ├── Day23_Pow(x,n).java ├── Day24_ReverseInteger.java ├── Day25_ReverseBits.java ├── Day26_AddDigits.java ├── Day27_MissingNumber.java ├── Day28_ThirdMaximumNumber.java ├── Day29_PowerOfFour.java ├── Day30_HappyNumber.java └── README.md /Day01_SortThePeople.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String[] sortPeople(String[] names, int[] heights) { 3 | HashMap map = new HashMap(); 4 | for(int i = 0;i=0;i--){ 11 | res[j] = map.get(heights[i]); 12 | j++; 13 | } 14 | return res; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Day02_SortArrayByIncreasingFrequency.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] frequencySort(int[] nums) { 3 | Map f =new HashMap<>(); 4 | Integer [] newNums = new Integer[nums.length]; 5 | 6 | for(int i=0; i{ 12 | if (f.get(n1) != f.get(n2)){ 13 | return f.get(n1)-f.get(n2); 14 | } 15 | else{ 16 | return n2-n1; 17 | } 18 | }); 19 | for (int i = 0; i < nums.length; i++) { 20 | nums[i] = newNums[i]; 21 | } 22 | 23 | return nums; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Day03_SortTheJumbledNumbers.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] sortJumbled(int[] mapping, int[] nums) { 3 | int pairs[] = new int[nums.length]; 4 | HashMap map = new HashMap<>(); 5 | for(int i=0;i0){ 14 | int digit=n%10; 15 | n=n/10; 16 | mapped_n+=base*mapping[digit]; 17 | base*=10; 18 | } 19 | map.put(i,mapped_n); 20 | } 21 | Map nmap = sortByValue(map); 22 | for(int i=0;i List[int]: 3 | return sorted(nums) 4 | 5 | -------------------------------------------------------------------------------- /Day05_FindTheCitySmallestNoOFNeibours.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findTheCity(int n, int[][] edges, int distanceThreshold) { 3 | int[][] distance = new int[n][n]; 4 | for (int i = 0; i < n; i++) { 5 | Arrays.fill(distance[i], Integer.MAX_VALUE); 6 | distance[i][i] = 0; // distance to itself is 0 7 | } 8 | 9 | // The distance between nodes which are connected temporary distance between them 10 | for (int[] edge : edges) { 11 | int node1 = edge[0], node2 = edge[1], dist = edge[2]; 12 | distance[node1][node2] = dist; 13 | distance[node2][node1] = dist; 14 | } 15 | 16 | for (int midle = 0; midle < n; midle++) { 17 | for (int source = 0; source < n; source++) { 18 | for (int destination = 0; destination < n; destination++) { 19 | if (distance[source][midle] < Integer.MAX_VALUE && distance[midle][destination] < Integer.MAX_VALUE) { 20 | distance[source][destination] = Math.min( 21 | distance[source][destination], distance[source][midle] + distance[midle][destination] 22 | ); // the minimum distance is either current value or new value with path that goes through midle 23 | } 24 | } 25 | } 26 | } 27 | 28 | int minimum_number = n; 29 | int res = -1; 30 | 31 | for (int source = 0; source < n; source++) { 32 | int source_count = 0; 33 | for (int destination = 0; destination < n; destination++) { 34 | if (distance[source][destination] <= distanceThreshold) { 35 | source_count++; 36 | } 37 | } 38 | 39 | if (source_count <= minimum_number) { // as in dijkstra when number equal we choose greater node 40 | minimum_number = source_count; 41 | res = source; 42 | } 43 | } 44 | 45 | return res; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Day06_MinimumCostToConvertString.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public long minimumCost(String source, String target, char[] original, char[] changed, int[] cost) { 3 | int[][] dis = new int[26][26]; 4 | for (int i = 0; i < 26; i++) { 5 | Arrays.fill(dis[i], Integer.MAX_VALUE); 6 | dis[i][i] = 0; 7 | } 8 | for (int i = 0; i < cost.length; i++) { 9 | dis[original[i] - 'a'][changed[i] - 'a'] = Math.min(dis[original[i] - 'a'][changed[i] - 'a'], cost[i]); 10 | } 11 | for (int k = 0; k < 26; k++) { 12 | for (int i = 0; i < 26; i++) 13 | if (dis[i][k] < Integer.MAX_VALUE) { 14 | for (int j = 0; j < 26; j++) { 15 | if (dis[k][j] < Integer.MAX_VALUE) { 16 | dis[i][j] = Math.min(dis[i][j], dis[i][k] + dis[k][j]); 17 | } 18 | } 19 | } 20 | } 21 | long ans = 0L; 22 | for (int i = 0; i < source.length(); i++) { 23 | int c1 = source.charAt(i) - 'a'; 24 | int c2 = target.charAt(i) - 'a'; 25 | if (dis[c1][c2] == Integer.MAX_VALUE) { 26 | return -1L; 27 | } else { 28 | ans += (long)dis[c1][c2]; 29 | } 30 | } 31 | return ans; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Day07_ReverseString.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void reverseString(char[] s) { 3 | int l=s.length-1,i; 4 | for(i=0;i=0 && m==10){ 7 | if(n==0){ 8 | digits = new int[digits.length+1]; 9 | digits[n]=1; 10 | return(digits); 11 | } 12 | else{ 13 | digits[n-1]+=1; 14 | } 15 | digits[n]=0; 16 | n--; 17 | m=digits[n]; 18 | } 19 | return(digits); 20 | 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Day16_ShuffleString.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String restoreString(String s, int[] indices) { 3 | StringBuffer stri = new StringBuffer(s); 4 | for(int i=0;i='0'&& s.charAt(i)<='9'){ 6 | sb.deleteCharAt(sb.length()-1); 7 | 8 | } 9 | else{ 10 | sb.append(s.charAt(i)); 11 | } 12 | } 13 | return sb.toString(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Day18_LemonadeChange.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean lemonadeChange(int[] bills) { 3 | int five=0; 4 | int ten =0; 5 | for(int i=0;i0) ten-=10; 12 | else five-=10; 13 | five-=5; 14 | } 15 | else{ 16 | ten+=10; 17 | five-=5; 18 | } 19 | if(five<0 || ten<0) return(false); 20 | } 21 | return(five>=0 && ten>=0); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Day19_Convert1DArrayInto2DArray.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[][] construct2DArray(int[] original, int m, int n) { 3 | int a[][] = new int[m][n]; 4 | int k=0; 5 | if(m*n!=original.length) return new int[0][0]; 6 | 7 | for(int i=0;i 0) { 10 | int temp = 0; 11 | for (char x : number.toString().toCharArray()) { 12 | temp += x - '0'; 13 | } 14 | number = new StringBuilder(String.valueOf(temp)); 15 | k--; 16 | } 17 | return Integer.parseInt(number.toString()); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Day22_NumberOf1Bits.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int hammingWeight(int n) { 3 | int a=0; 4 | while(n>0) 5 | { 6 | if(n%2==1) a+=1; 7 | n/=2; 8 | } 9 | return a; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Day23_Pow(x,n).java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public double myPow(double x, int n) { 3 | 4 | double ans = 1.0; 5 | long nTemp = n; 6 | 7 | if(n < 0) 8 | nTemp = nTemp * -1; 9 | 10 | while(nTemp > 0){ 11 | if(nTemp % 2 == 0){ 12 | x *= x; 13 | nTemp /=2; 14 | } 15 | else{ 16 | ans *= x; 17 | nTemp--; 18 | } 19 | } 20 | return n < 0 ? 1 / ans : ans; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Day24_ReverseInteger.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int reverse(int x) { 3 | long sum=0; 4 | while(x!=0) 5 | { 6 | sum= sum*10 + (x%10); 7 | x=x/10; 8 | } 9 | if(sum > Integer.MAX_VALUE || sum>= 1; 12 | } 13 | return result; 14 | 15 | } 16 | } 17 | 18 | // time 19 | public class Solution { 20 | // you need treat n as an unsigned value 21 | public int reverseBits(int n) { 22 | return Integer.reverse(n); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Day26_AddDigits.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int addDigits(int num) { 3 | 4 | while(num/10 != 0) 5 | { 6 | int sum=0; 7 | while(num!=0) 8 | { 9 | sum+=num%10; 10 | num/=10; 11 | } 12 | num=sum; 13 | } 14 | return num; 15 | 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Day27_MissingNumber.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int missingNumber(int[] nums) { 3 | int i=0,j; 4 | int sum=0; 5 | for(j=0;jmax1){ 9 | max3 = max2; 10 | max2 = max1; 11 | max1 = num; 12 | }else if(max1>num && num>max2){ 13 | max3 = max2; 14 | max2 = num; 15 | }else if(max2>num && num>max3){ 16 | max3=num; 17 | } 18 | } 19 | return max3 != Long.MIN_VALUE ? (int) max3 : (int) max1; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Day29_PowerOfFour.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isPowerOfFour(int n) { 3 | if(n<=0) return false; 4 | while(n>1) 5 | { 6 | if(n%4!=0) return false; 7 | n=n/4; 8 | } 9 | return true; 10 | 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Day30_HappyNumber.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isHappy(int n) { 3 | int sum = 0; 4 | if (n == 1) { 5 | return true; 6 | } else if (n==4) { 7 | return false; 8 | } else { 9 | while (n > 0) { 10 | sum = sum + (int) Math.pow(n % 10, 2); 11 | n = n / 10; 12 | } 13 | return isHappy(sum); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LeetCode 30-Day Challenge Solutions 🚀 2 | 3 | ## 📌 Repository Overview 4 | This repository is dedicated to solving **LeetCode Problem 30** as part of a **30-day coding challenge**. The goal of this challenge is to enhance problem-solving skills by consistently working through algorithmic problems over an extended period. Each day presents a new challenge that is carefully analyzed and solved with an optimized approach. 5 | 6 | ## 📂 Contents 7 | - **Problem Statement:** A brief description of the problem. 8 | - **Approach & Solution:** Step-by-step explanation of the implemented solution. 9 | - **Code Implementations:** Solutions in various programming languages (if applicable). 10 | - **Time & Space Complexity Analysis:** Optimization details. 11 | 12 | ## 🚀 Technologies Used 13 | - **Programming Language:** Java 14 | - **Problems:** Algorithms & Data Structures 15 | 16 | ## 🔥 How to Use 17 | 18 | ### 1️⃣ Clone the repository 19 | ```sh 20 | git clone https://github.com/sriram2195/leetcode-java.git 21 | ``` 22 | ### 2️⃣ Navigate to the project directory 23 | ```sh 24 | cd leetcode-java 25 | ``` 26 | ### 3️⃣ Run the solution file 27 | ```sh 28 | python Day1_SortThePeople.java # Example for Python 29 | ``` 30 | 31 | ## 📞 Contact 32 | For any queries or discussions, feel free to **open an issue** or **reach out**. 33 | 34 | --- 35 | Happy Coding! 🚀 36 | --------------------------------------------------------------------------------