crazykani
BAN USER- 0of 0 votes
Answers
- crazykani in United StatesJumper Game: A NxN grid which contains either of 0-empty, 1 - player 1, 2 - player 2. Given a position in the grid, find the longest jump path. For jump path, you can jump only diagonally, you can jump on opponent cell and also the landing cell should be empty. No opponent cell can be jumped more than once. Write a function which takes grid and a specific position in the grid, and returns the longest possible number of jumps in the grid. For Example: if grid = { { 0,0,0,0,0,0 }, { 0,1,0,0,0,0 }, { 2,0,2,0,2,0 }, { 0,0,0,0,0,0 }, { 0,0,0,0,0,0 }, { 0,0,0,0,0,0 }, }; Answer should be 2 - (1,1) -> (3,3) -> (1,5)
| Report Duplicate | Flag | PURGE
Algorithm
public class ReplaceString1 {
/**
* Given a string.Replace the words whose lengt>=4 and
* is even,with a space between the two equal halves of the word.
* consider only alphabets for finding the eveness of the word
I/P "A person can't walk in this street"
O/P "A per son ca n't wa lk in th is stre et"
*
*/
public static void main(String[] args) {
String str = "A person can''t walk in this street";
String mStr = replace(str);
System.out.println(mStr);
}
private static String replace(String str) {
StringBuffer sb = new StringBuffer();
String[] tokens = str.split(" ");
for(String s: tokens) System.out.println(s);
for(String s: tokens){
if(len(s)<4) {
sb.append(s);
sb.append(" ");
}
else{
if(len(s)%2==0){
sb.append(s.substring(0,len(s)/2));
sb.append(" ");
sb.append(s.substring(len(s)/2) + " ");
}
else{
sb.append(s + " ");
}
}
}
return sb.toString();
}
public static int len(String s){
int totalLen = s.length();
int count = 0;
for(char c:s.toCharArray()){
if(c== '\'') count++;
}
return totalLen - count;
}
}
import java.util.Random;
public class LookAndSay {
public static void main(String[] args) {
int n;
if(args.length == 1)
n = Integer.parseInt(args[0]);
else
n= (new Random()).nextInt(10) + 1;
looknsay(n);
}
private static void looknsay(int n) {
//System.out.println(n);
String s = "1";
System.out.println(s);
for(int i=1;i<n;i++){
s = genNext(s);
System.out.println(s);
}
System.out.println(s);
}
private static String genNext(String s) {
StringBuffer sb = new StringBuffer();
int count;
for(int i=0;i<s.length();i++){
count = 1;
while( i+1 < s.length() && s.charAt(i) == s.charAt(i+1)){
++i;
++count;
}
sb.append(count);
sb.append(s.charAt(i));
}
return sb.toString();
}
}
public class ValidPalindromeDate {
public static void main(String[] args) {
StringBuffer sb;
int sYear = 1001;
int eYear = 1201;
while(sYear <= eYear){
sb = new StringBuffer(String.valueOf(sYear));
sb.reverse().append(String.valueOf(sYear));
isValid(sb.toString());
sYear ++;
sb = null;
}
}
private static void isValid(String date) {
int[] numDaysinmonth = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int year = Integer.parseInt(date.substring(4));
int month;
int day;
if(Integer.parseInt(date.substring(0))==0)
month = Integer.parseInt(date.substring(1,2));
else
month = Integer.parseInt(date.substring(0,2));
if(Integer.parseInt(date.substring(2))==0)
day = Integer.parseInt(date.substring(3,4));
else
day = Integer.parseInt(date.substring(2,4));
//System.out.println("day:" + day);
//System.out.println("month" + month);
//System.out.println("year" + year);
if(!(year%100 == 0 && year%4 == 0)){//leap year
if(month == 2){//feb
if(month > 0 && month <= 12 && day > 0 && day <= numDaysinmonth[month] + 1)
System.out.println("This date is valid! " + date);
else
System.out.println("Not a valid date " + date);
}
else{//not feb
if(month > 0 && month <= 12 && day > 0 && day <= numDaysinmonth[month])
System.out.println("This date is valid! " + date);
else
System.out.println("Not a valid date " + date);
}
}
else{// not a leap year
if(month > 0 && month <= 12 && day > 0 && day <= numDaysinmonth[month])
System.out.println("This date is valid! "+ date);
else
System.out.println("Not a valid date " + date);
}
}
}
You have to compare maximum of all the difference with the threshold value. I think your code should be changed to as follows:
if(col - 1 >= 0) {
left = image[col - 1];
diff_left = Math.abs(left - value);
}
if(col + 1 < width) {
right =image[col + 1];
diff_right = Math.abs(right - value);
}
if(col + width < n) {
bottom = image[ width + col];
diff_bottom = Math.abs(bottom - value);
}
if(col - width >= 0) {
top = image[col - width];
diff_top = Math.abs(top - value);
}
int temp_diff = Math.max(Math.max(diff_left,diff_right),
Math.max(diff_top,diff_bottom));
if(temp_diff > threshold)
output[col] = value;
else
output[col] = 0;
}
- crazykani November 30, 2014