Showing posts with label LeetCode Medium. Show all posts
Showing posts with label LeetCode Medium. Show all posts

Saturday, September 2, 2017

LeetCode - 12. Integer to Roman

Source Link:
https://leetcode.com/problems/integer-to-roman/description/

Question: 

Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.

My Solution:
Note:
1. store all the possible "numbers" and "strings"
int[] nums = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
String[] roman = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
2. Using "StringBuilder" (important)
StringBuilder string_builder = new StringBuilder();
3. for each possible nums (from big one to small one)
for(int i=0; i < nums.length; i++){
while( num >= nums[i]){
num = num - nums[i];
string_builder.append(roman[i]);  
}
4. Using "toString()"
return string_builder.toString(); 

LeetCode - 5. Longest Palindromic Substring

Source Link:
https://leetcode.com/problems/longest-palindromic-substring/description/

Question:
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.

Example:
Input: "babad"
Output: "bab" 
Note: "aba" is also a valid answer.

My Solution: Note:
1. main idea: Expand Around Center
a palindrome can be "expanded" from its center
2. we need to keep "the max length" and "the start point of the max palindrome"
int max_len = 0;
int max_start =0;
3. try to expand the palindrome from the "center"
for(int i=0; i < s.length(); i++){
expand(s, i, i); // case 1: the Palindrome is with "odd" length
expand(s, i, i+1); // case 2: the Palindrome is with "even" length
 
4. the longest palindrome by using str.substring(int start, int end)
longest_palindrome = s.substring(max_start, max_start + max_len); 
5. write a function to expand the palindrome from the "center"
public void expand(String s, int left, int right)
6. while s.charAt(left)==s.charAt(right), then expand by "left--" and "right++"
while(left >=0 && right <= s.length()-1 && s.charAt(left) == s.charAt(right)){
left --;
right ++;
}
7. note: we ill do "left--" and "right++" one more time
the current start and end points are "left+1" and "right-1" (be careful)

int cur_start = left +1;
int cur_end = right - 1;

LeetCode - 505. The Maze II

Source Link:
https://leetcode.com/articles/the-maze-ii/

Question:
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolling up, down, left or right, but it won't stop rolling until hitting a wall. When the ball stops, it could choose the next direction.
Given the ball's start position, the destination and the maze, find the shortest distance for the ball to stop at the destination. The distance is defined by the number of empty spaces traveled by the ball from the start position (excluded) to the destination (included). If the ball cannot stop at the destination, return -1.
The maze is represented by a binary 2D array. 1 means the wall and 0 means the empty space. You may assume that the borders of the maze are all walls. The start and destination coordinates are represented by row and column indexes.

 


My Solution: Note:
1. note: we use int[2] to stand for (x,y), i.e., (int[0], int[1])
int[] start, int[] dest
2.  "new" int[][] to store the distance so far (with the same size as the maze)
int[][] distance = new int[maze.length][maze[0].length];
3. note: need to set the distance as "Integer.MAX_VALUE" (be careful)
for (int[] row: distance)
Arrays.fill(row, Integer.MAX_VALUE);
4. initial setting: the start point
distance[start[0]][start[1]] = 0;
5. note: after while loop, the stop point is "{x - dir[0], y - dir[1]}"
check if the distance is smaller (important)
be careful about the stop point
if (distance[s[0]][s[1]] + count < distance[x - dir[0]][y - dir[1]]) {
// if it is smaller, then update it
distance[x - dir[0]][y - dir[1]] = distance[s[0]][s[1]] + count;
// add the "stop point" into the queue
queue.add(new int[] {x - dir[0], y - dir[1]});
}

Codility - Lesson 16 Greedy algorithms - 2. MaxNonoverlappingSegments

Source Link: https://app.codility.com/programmers/lessons/16-greedy_algorithms/max_nonoverlapping_segments/ Question: Located on a line ...