Ugly Numbers :
http://www.geeksforgeeks.org/archives/753
http://www.geeksforgeeks.org/archives/753
Apart from coding and design interview questions, this page contains updates on my learnings with Java. It helps me organize my learning. Read about my future self here : https://siliconvalleystories.blogspot.com/
Kadane's algorithm :
Simple idea of the Kadane's algorithm is to look for all positive contiguous segments of the array (max_ending_here is used for this). And keep track of maximum sum contiguous segment among all positive segments (max_so_far is used for this).
int
maxSubArraySum(
int
a[],
int
size)
{
int
max_so_far = 0, max_ending_here = 0;
int
i;
for
(i = 0; i < size; i++)
{
max_ending_here = max_ending_here + a[i];
if
(max_ending_here < 0)
max_ending_here = 0;
if
(max_so_far < max_ending_here)
max_so_far = max_ending_here;
}
return
max_so_far;
}
null
) -- a particular object can be stored in zero or more variables simultaneously. This does not create new objects, see #3.hashCode()
and equals()
sensibly, make good hash keys.
equals()
method, they must have the same hashCode()
value (although the reverse is not generally true).
hashCode()
, when its discriminating ability is entirely subsumed by that of equals()
?
hashCode()
method exists purely for efficiency. The Java platform architects anticipated the importance of hash-based collection classes -- such as Hashtable
, HashMap
, and HashSet
-- in typical Java applications, and comparing against many objects with equals()
can be computationally expensive. Having every Java object support hashCode()
allows for efficient storage and retrieval using hash-based collections.
equals()
.hashCode()
and equals()
methods of keys:equals()
returns true
when you compare them), their hashCode()
method must return the same number. If keys violate this, then keys that are equal might be stored in different buckets, and the hashmap would not be able to find key-value pairs (because it's going to look in the same bucket).equals()
to tell them apart.String s1 = "abcde";
String s2 = new String("abcde");
String s3 = "abcde";
java.lang.String
class. s2 will be a new String object.(s1 == s2) is false
(s1 == s3) is true
(s1.equals(s2)) is true
String myString = new String( "old String" ); String myCache = myString; System.out.println( "equal: " + myString.equals( myCache ) ); System.out.println( "same: " + ( myString == myCache ) ); myString = "not " + myString; System.out.println( "equal: " + myString.equals( myCache ) ); System.out.println( "same: " + ( myString == myCache ) ); |
equal: true same: true equal: false same: false