I almost got this question:
compare every element to x, and sort them accordingly, similar to quicksort kinda.
but i failed to understand 2 things:
numbers itself DON'T GO ANYWHERE, they just stay in memory, IT IS ALL ABOUT TWISTING THE ARROWS - POINTERS btw nodes!!!!!!
so i failed to implement this idea: unwind/ detach your first node, but save the next element to keep going through the original list!
this is the drawing:
example:
x = 20
value found - 12.
12 < 20 , so detach it from original list and make it point to the new "less than x " list!
public static node partition(int x, node list){
node smaller = null ;
node larger = null ;
while(list != null){
node saved_list_next = list.next ;
if(list.data < x){
list.next = smaller ;
smaller = list ;
}
else{
list.next = larger ;
larger = list ;
}
list = saved_list_next ;
}
if(smaller == null) return larger ;
node head = smaller ;
while(smaller.next!= null) smaller = smaller.next ;
node middle = new node((short) -1) ;
smaller.next = middle ;
smaller.next.next = larger ;
return head ;
}
HERE I SOLVE /PRACTICE PROGRAMMING EXERCISES FROM VARIOUS sources ENJOY!
ctci 1.6 - rotate nxn matrix clockwise
question: for nxn matrix, rotate all cells by 90degrees clockwise.
HOnestly, i did not understand the question itself, so i looked up the solution; run my example and figured out how it works.
as always, the idea is to hack the pattern - how the indexes change - and simply code it appropriately.
i drawn this picture:
public class ctci_1_6{
public static void main(String []a){
int image_matrix[][] = {
{1,1,1,1,1,1},
{2,2,2,2,2,2},
{3,3,3,3,3,3},
{4,4,4,4,4,4},
{5,5,5,5,5,5},
{6,6,6,6,6,6}
} ;
print_matrix(image_matrix,6) ;
rotate(image_matrix,6) ;
System.out.println("\n\n AFTER :") ;
print_matrix(image_matrix,6) ;
}
public static void print_matrix(int matrix[][], int n){
for(int i = 0 ; i < n ; i++){
for(int j = 0 ;j < n ; j++){
System.out.print( matrix[i][j] + "\t") ;
}
System.out.println() ;
}
}
public static void rotate(int matrix[][], int n){
for(int layer = 0 ; layer < n/2 ; layer++){
System.out.println("---------------------------------------------------------------------------------------------") ;
int first = layer ;
int last = n - 1 - layer ;
System.out.println("first: " + first + " , last : " + last) ;
for(int i = first; i < last ; i++){
System.out.println("^^^^^") ;
int offset = i - first ;
System.out.println(" offset: " + offset) ;
int top = matrix[first][i] ;
//left -> top
matrix[first][i] = matrix[last - offset][first] ;
System.out.printf("m[%d][%d] = m[%d][%d]\n", first, i, (last - offset), first) ;
//bottom -> left
matrix[last - offset][first] = matrix[last][last - offset] ;
System.out.printf("m[%d][%d] = m[%d][%d]\n", (last - offset), first, last, (last -offset)) ;
//right->bottom
matrix[last][last - offset] = matrix[i][last] ;
System.out.printf("m[%d][%d] = m[%d][%d]\n", last, (last - offset), i, last) ;
//top ->right
matrix[i][last] = top ;
System.out.printf("m[%d][%d] = m[%d][%d]\n", i,last, first,i) ;
}
System.out.println() ;
}
}
}
HOnestly, i did not understand the question itself, so i looked up the solution; run my example and figured out how it works.
as always, the idea is to hack the pattern - how the indexes change - and simply code it appropriately.
i drawn this picture:
public class ctci_1_6{
public static void main(String []a){
int image_matrix[][] = {
{1,1,1,1,1,1},
{2,2,2,2,2,2},
{3,3,3,3,3,3},
{4,4,4,4,4,4},
{5,5,5,5,5,5},
{6,6,6,6,6,6}
} ;
print_matrix(image_matrix,6) ;
rotate(image_matrix,6) ;
System.out.println("\n\n AFTER :") ;
print_matrix(image_matrix,6) ;
}
public static void print_matrix(int matrix[][], int n){
for(int i = 0 ; i < n ; i++){
for(int j = 0 ;j < n ; j++){
System.out.print( matrix[i][j] + "\t") ;
}
System.out.println() ;
}
}
public static void rotate(int matrix[][], int n){
for(int layer = 0 ; layer < n/2 ; layer++){
System.out.println("---------------------------------------------------------------------------------------------") ;
int first = layer ;
int last = n - 1 - layer ;
System.out.println("first: " + first + " , last : " + last) ;
for(int i = first; i < last ; i++){
System.out.println("^^^^^") ;
int offset = i - first ;
System.out.println(" offset: " + offset) ;
int top = matrix[first][i] ;
//left -> top
matrix[first][i] = matrix[last - offset][first] ;
System.out.printf("m[%d][%d] = m[%d][%d]\n", first, i, (last - offset), first) ;
//bottom -> left
matrix[last - offset][first] = matrix[last][last - offset] ;
System.out.printf("m[%d][%d] = m[%d][%d]\n", (last - offset), first, last, (last -offset)) ;
//right->bottom
matrix[last][last - offset] = matrix[i][last] ;
System.out.printf("m[%d][%d] = m[%d][%d]\n", last, (last - offset), i, last) ;
//top ->right
matrix[i][last] = top ;
System.out.printf("m[%d][%d] = m[%d][%d]\n", i,last, first,i) ;
}
System.out.println() ;
}
}
}
Posted by
erjan
musings about vim
I decided not to learn vim, gvim etc.
The advantages of it are well known, but i would rather become familiar wih Eclipse/visual studio and other standard frameworks.
at this point, i see no strong preference towards vim as i was thinking 2 years ago. After all, most features(autocompletion, syntax highlight) become the standard of the industry, most engineers will demand them in any tool.
So i reviewed my craving to study vim and get good at it,after all i want to build cool things, apps and eclipse/visual studio are pretty good!
Posted by
erjan
cracking the coding interview interview question - 1.5
ok, i m doing some cracking the coding interview questions just to improve a bit my skills. and it s not that bad!
look up my code; it works , but has testing printing.
the question itself : compress a string, example : aaa = a3, ab = a1b1, abc = a1b1c1 etc.
if the compressed string is longer then return original.
import java.util.* ;
public class ctci{
public static void main(String args[]){
String result = compressed("aa") ;
}
public static String compressed(String original_string){
if(original_string.length() <=2 ) return original_string ;
String compressed_string = "" ;
for(int i = 0 ; i < original_string.length() ; i++){
System.out.println("------------------------------------------------------------------------------------------------------------------------------ i : " + i ) ;
char current_char = original_string.charAt(i) ;
System.out.println("current char " + current_char) ;
int streak = 1 ;
while(i < original_string.length() -1 && current_char == original_string.charAt(i+1) ) {
System.out.println("streak len growing: " + streak + " and letter is : " + original_string.charAt(i+1)) ;
i++ ;
streak++ ;
}
//compressed_string += (current_char + "") + streak ;
compressed_string += String.valueOf(current_char) + streak ;
System.out.println( "streak ended, length of the streak " + streak + " ; and the new string is " + compressed_string ) ;
}
if(compressed_string.length() >= original_string.length()){System.out.println("original returned! " + original_string) ; return original_string ;}
else{System.out.println("compressed returned! " + compressed_string) ; return compressed_string ;}
}
}
look up my code; it works , but has testing printing.
the question itself : compress a string, example : aaa = a3, ab = a1b1, abc = a1b1c1 etc.
if the compressed string is longer then return original.
import java.util.* ;
public class ctci{
public static void main(String args[]){
String result = compressed("aa") ;
}
public static String compressed(String original_string){
if(original_string.length() <=2 ) return original_string ;
String compressed_string = "" ;
for(int i = 0 ; i < original_string.length() ; i++){
System.out.println("------------------------------------------------------------------------------------------------------------------------------ i : " + i ) ;
char current_char = original_string.charAt(i) ;
System.out.println("current char " + current_char) ;
int streak = 1 ;
while(i < original_string.length() -1 && current_char == original_string.charAt(i+1) ) {
System.out.println("streak len growing: " + streak + " and letter is : " + original_string.charAt(i+1)) ;
i++ ;
streak++ ;
}
//compressed_string += (current_char + "") + streak ;
compressed_string += String.valueOf(current_char) + streak ;
System.out.println( "streak ended, length of the streak " + streak + " ; and the new string is " + compressed_string ) ;
}
if(compressed_string.length() >= original_string.length()){System.out.println("original returned! " + original_string) ; return original_string ;}
else{System.out.println("compressed returned! " + compressed_string) ; return compressed_string ;}
}
}
Posted by
erjan
Subscribe to:
Comments (Atom)

