Unknown's avatar

329. Longest Increasing Path in a Matrix

329. Longest Increasing Path in a Matrix

 

class Solution {
	public int longestIncreasingPath(int[][] matrix) {
         if (matrix.length == 0) return 0;
		int max = Integer.MIN_VALUE;
		int [][] mem = new int[matrix.length][matrix[0].length];
		for (int i = 0; i < matrix.length; i++)
			Arrays.fill(mem[i], 0);
		for (int i = 0; i < matrix.length; i++) {
			for (int j = 0; j < matrix[i].length; j++) {
				max = Math.max(max,helper(i, j, mem,matrix));
			}
		}

		return max;
	}


	public int helper(int x, int y, int[][] mem, int[][] matrix) {
		if (mem[x][y] != 0) {
			return mem[x][y];
		}
		int[] dx = { 0, 1, -1, 0 };
		int[] dy = { 1, 0, 0, -1 };

		for (int i = 0; i = 0 && newX = 0 && newY  matrix[x][y]) {
				mem[x][y] = Math.max(mem[x][y], helper(newX, newY,mem, matrix));
			}
		}
		return ++mem[x][y];
	}
}
Unknown's avatar

1122. Relative Sort Array

1122. Relative Sort Array

class Solution {
    	public int[] relativeSortArray(int[] arr1, int[] arr2) {
		HashMap h = new HashMap();
		for (int i = 0; i < arr2.length; i++) {
			h.put(i, arr2[i]);
		}
		// queue to keep the new list in ascending order
		PriorityQueue rem = new PriorityQueue();
		int existing = 0;
		for (int i = 0; i < arr1.length; i++) {
			if (null != getKeyFromValue(h, arr1[i])) {
				arr1[i] = getKeyFromValue(h, arr1[i]);
				existing++;
			} else {
				rem.add(arr1[i]);
               arr1[i] = Integer.MAX_VALUE;

			}
		}
		Arrays.sort(arr1);
		for (int i = 0; i < existing; i++) {
			arr1[i] = h.get(arr1[i]);
		}
		for (int i = existing; i < arr1.length; i++) {
			arr1[i] = rem.poll();
		}
		return arr1;
	}

	public static Integer getKeyFromValue(HashMap hm, int value) {
		for (Integer o : hm.keySet()) {
			if (((Integer) hm.get(o)).intValue() == value) {
				return o;
			}
		}
		return null;
	}
}
Unknown's avatar

1185. Day of the Week

1185. Day of the Week

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
class Solution {
	  	public String dayOfTheWeek(int day, int month, int year) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = format.parse(year + "-" + ((month <= 9) ? ("0" + month) : ("" + month)) + "-" + ((day <= 9) ? ("0" + day) : ("" + day)));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int d = date.getDay();
		String dayString = null;
		switch (d) {
		case 1:
			dayString = "Monday";
			break;
		case 2:
			dayString = "Tuesday";
			break;
		case 3:
			dayString = "Wednesday";
			break;
		case 4:
			dayString = "Thursday";
			break;
		case 5:
			dayString = "Friday";
			break;
		case 6:
			dayString = "Saturday";
			break;
		case 0:
			dayString = "Sunday";
			break;
		default:
			dayString = "Invalid day";
			break;
		}

		return dayString;
	}
}
Unknown's avatar

1160. Find Words That Can Be Formed by Characters

1160. Find Words That Can Be Formed by Characters

class Solution {
		public int countCharacters(String[] words, String chars) {
		HashMap freq = new HashMap();
		for (int i = 0; i < chars.length(); i++) {
			freq.put(chars.charAt(i), freq.getOrDefault(chars.charAt(i), 0) + 1);
		}
		int count = 0;
		for (int i = 0; i < words.length; i++) {
			HashMap freq1 = new HashMap(freq);
			boolean charsNotFound = false;
			for (int j = 0; j  0)
					freq1.put(words[i].charAt(j), freq1.get(words[i].charAt(j)) - 1);
				else {
					charsNotFound = true;
					break;
				}
			}
			if (!charsNotFound)
				count += words[i].length();
		}
		return count;
	}
}
Unknown's avatar

28. Implement strStr()

28. Implement strStr()

class Solution {
    public int strStr(String haystack, String needle) {
        if (needle.equals("")) return 0;
        if (haystack.equals("")) return -1;
        for (int i = 0; i < haystack.length();){
            int temp = i;
            int neadleIndex= 0;
            while(i < haystack.length() && 
                  neadleIndex < needle.length() &&
                  haystack.charAt(i) ==needle.charAt(neadleIndex)){
                neadleIndex++;
                i++;
                }
                if(neadleIndex-1 == needle.length() -1) return temp;
            else {
                i = temp+1 ;
            }
            }
        
        return -1;
    }
}
Unknown's avatar

13. Roman to Integer

13. Roman to Integer

class Solution {
    public int romanToInt(String s) {
        int out = 0;
        for (int i = 0; i <s>= 0 &amp;&amp; s.charAt(i-1) == 'I'){
                    out -= 1;
                    out += 4;
                }else out += 5;
            }
             //3
            else if (s.charAt(i) == 'X'){
                if ((i-1) &gt;= 0 &amp;&amp; s.charAt(i-1) == 'I'){
                    out -= 1;
                    out += 9;
                }else out += 10;
            }
             //4
            else if (s.charAt(i) == 'L'){
                if ((i-1) &gt;= 0 &amp;&amp; s.charAt(i-1) == 'X'){
                    out -= 10;
                    out += 40;
                }else out += 50;
            }
             //5
            else if (s.charAt(i) == 'C'){
                if ((i-1) &gt;= 0 &amp;&amp; s.charAt(i-1) == 'X'){
                    out -= 10;
                    out += 90;
                }else out += 100;
            }
             //6
            else if (s.charAt(i) == 'D'){
                if ((i-1) &gt;= 0 &amp;&amp; s.charAt(i-1) == 'C'){
                    out -= 100;
                    out += 400;
                }else out += 500;
            }
             //7
            else if (s.charAt(i) == 'M'){
                if ((i-1) &gt;= 0 &amp;&amp; s.charAt(i-1) == 'C'){
                    out -= 100;
                    out += 900;
                }else out += 1000;
            }
        }
        return out;
    }
}