Anil Singh
BAN USER
We can use combination of HashMap and customized Stack.
Stack here should maintained in such a way that we should see LFU object as peek,
and in case memory is full remove object from map same as peek of stack.
I always disagree with solutions coming out of naive iterations !!!
This can be solved by using Binary Search.
Perform a binary search for SUM - arr[i] in array.
Time complexity is O(n) here.
- Anil Singh April 20, 2013How come time coplexuty is O(n) here ?
- Anil Singh April 20, 2013public class ArrayMaxSum {
/**
* @param args
*/
public static void main(String[] args) {
String string = "1,2,-10,3,4,-20,5,6,4,-50,9,0,-1";
String[] numbers = string.split(",");
LinkedList<String> lst = new LinkedList<String>();
LinkedList<String> tmp = new LinkedList<String>();
int max_so_far = 0;
int max_ending_here = 0;
for (int i = 0; i < numbers.length-1; i++)
{
max_ending_here = max_ending_here + Integer.parseInt(numbers[i]);
lst.add(numbers[i]);
if (max_ending_here < 0)
{
max_ending_here = 0;
lst.clear();
}
if (max_ending_here > max_so_far)
{
tmp.clear();
max_so_far = max_ending_here;
tmp.addAll(lst);
}
}
for(String str : tmp)
{
System.out.print(str+", ");
}
System.out.print(" = "+max_so_far);
}
}
After for loop firstKth points to first Kth element in list.
After completion of while loop lastKth will point to Kth from last in list.
Repjimbtam, Backend Developer at ASAPInfosystemsPvtLtd
I was extremely into photography for a number of years.My father was also interested in photography, so I was ...
Repmarkhlee8, Research Scientist at Bank of America
I am a cooking Instructor in New York USA, I Have a degree from the prestigious Culinary Arts Institute of ...
RepTinaaJohn, Cloud Support Associate at AMD
Hi,Everyone.I am an art teacher at Woodson Institute.Foster and encourage critical thinking and analysis about art and ...
Repvickiwgerber, Member Technical Staff at Boomerang Commerce
I am Vicki From San Antonio USA, I am working as an Interior decorator in Golden Joy company. I have ...
Repshanitajjana, +27655765355 SSD MONEY CLEANING CHEMICAL +27655765355 BLACK MONEY IN JOHANNESBURG, OMAN, SAUDI ARABIA, SUDAN, Somalia ,Zimbabwe Botswana at 247quickbookshelp
Hi I am Shanita from San Bernardino USA. I am working as a manager in flore company. I am outgoing ...
Repverarfurman, Problem Setter at Cerberus Capital
I am Vera from Bullard USA, I am working as a Violin repairer in a Handyman company. My interest in ...
Repnancyhfloress, Computer Scientist at AMD
Hey there, I’m Nancy. I’m a small business owner living in Sunrise, FL 33323. I am a fan ...
Repjessicajbunting, Aghori Mahakal Tantrik at ABC TECH SUPPORT
Hi I am Jessica, from Houston USA. I am working as a manager in Incredible Universe company. I enjoy additional ...
Repkellydbrown23, Jr. Software Engineer at Auto NInja
I live in College Park USA with my family, and my current job is clerk in Luria’s company. I ...
Repannaharricks, Computer Scientist at ABC TECH SUPPORT
Hi, I am a Social worker to help people handle everyday life problems. I also assist people who have issues ...
Repstacyrdavid, Associate at Abs india pvt. ltd.
Hi, I am Stacy working as a Cashier in Dollar store the US. I work for Us government to Collect ...
Repashleymbosse, Associate at Accenture
I am an enthusiastic, hard-working and disciplined Catering Assistant with excellent track-record in working in the food industry. I am ...
Repelisahbuff, Apple Phone Number available 24/7 for our Customers at Altera
I have previous work experience in a customer focused environment. I like to Enjoy my Free Time Playing football.I ...
RepHi, I am Anne from Portsmouth, USA. I have been working as a freelance digital illustrator specialized in 3D character ...
RepCarlERhodes, Cloud Support Associate at Baidu
I am an Engineering manager in Mosier USA. I am a freelance events coordinator and a lifetime entrepreneur. I want ...
Repvictorcraigw, Animator at Chicago Mercantile Exchange
Hi, I am Victor working as a Speech Writer in the USA. Spoke at an academic conference about mantra for ...
Open Chat in New Window
Looks like an excellent solution driven by mathematics !!!
- Anil Singh January 16, 2014