patterncsharpMinor
Minimum sub-array challenge
Viewed 0 times
arraysubminimumchallenge
Problem
Below is my solution to finding the minimum product sub-interval. It passed 8/12 or so test cases, but the other test cases timed out if they hadn't finished within 2 seconds. When I compile this code as Release, optimized, and to target x64, it takes about 7.6 seconds to finish on large input sizes, such as this testcase. (I'm running this on Windows 8.1 with CPU: AMD FX-8320 3.49 GHz).
How can this code be improved upon so that it runs in \$ O(n \log(n)) \$ time, or what constant-time improvements can be made?
using System;
using System.Collections.Generic;
using System.Linq;
class SubInterval : IComparable {
public int start;
public int end;
public int Length { get { return start == 0 && end == 0? 0 : end - start + 1; } }
public int CompareTo(SubInterval other) {
if (this.Length > other.Length)
return 1;
else if (this.Length lookup = new List() { i };
for (int k = i; k () { k };
}
else if (a[k] == min && k > i) {
lookup.Add(k);
}
}
if (min == 0) {
SubInterval zero = new SubInterval { start = i, end = j };
Console.WriteLine("0 {0} {1}", zero.start, zero.end);
}
else if (min == 1 && lookup.Count > 1) {
SubInterval longest = getLongestSubInterval1s(lookup, a);
Console.WriteLine("1 {0} {1}", longest.start, longest.end);
}
else {
int idxMin = lookup[0];
SubInterval sub = new SubInterval { start = idxMin, end = idxMin };
Console.WriteLine("{2} {0} {1}", sub.start, sub.end, a[idxMin]);
}
}
static SubInterval getLongestSubInterval1s(List samesLookup, int[] a) {
List list = new List();
SubInterval sub;
for (int k = 0, ikj = 0; k ();
}How can this code be improved upon so that it runs in \$ O(n \log(n)) \$ time, or what constant-time improvements can be made?
Solution
This review won't help much with your time limit exceeded problem,
but there are some poor practices in your code that I'd like to point out.
The
Actually, since all the if-else branches return,
you can simplify to:
Method names that start with the word "get" normally return something.
So it would be better to rename it accordingly.
but there are some poor practices in your code that I'd like to point out.
The
else statement is dead code, and the last else if can be changed to else here:if (this.Length > other.Length)
return 1;
else if (this.Length < other.Length)
return -1;
else if (this.Length == other.Length)
return this.start < other.start ? 1 : -1;
else
return 0;Actually, since all the if-else branches return,
you can simplify to:
if (this.Length > other.Length)
return 1;
if (this.Length < other.Length)
return -1;
return this.start < other.start ? 1 : -1;Method names that start with the word "get" normally return something.
getMinSubInterval is void, and it prints stuff.So it would be better to rename it accordingly.
Code Snippets
if (this.Length > other.Length)
return 1;
else if (this.Length < other.Length)
return -1;
else if (this.Length == other.Length)
return this.start < other.start ? 1 : -1;
else
return 0;if (this.Length > other.Length)
return 1;
if (this.Length < other.Length)
return -1;
return this.start < other.start ? 1 : -1;Context
StackExchange Code Review Q#93619, answer score: 6
Revisions (0)
No revisions yet.