om471987
BAN USERPython code
input = [ -1, 1, 3, -2, 2]
n = len(input)
output = [0] * n
negativeIndex = 0
positiveIndex = n - 1
for i, t in enumerate(input):
if input[i] < 0:
output[negativeIndex] = input[i]
negativeIndex += 1
if input[n - 1 - i] > 0:
output[positiveIndex] = input[n - 1 - i]
positiveIndex -= 1
print(output)
in python
input = [ -1, 1, 3, -2, 2]
output = [0] * len(input)
negatives = 0
for t in input:
if t < 0:
negatives += 1
negativeIndex = 0
positiveIndex = negatives
for t in input:
if t < 0:
output[negativeIndex] = t
negativeIndex += 1
else:
output[positiveIndex] = t
positiveIndex += 1
print(output)
Python code
import sys
from math import fabs
input = [2, -1, -2, 1, -4, 2, 8]
n = len(input)
start = 0
maxSum = -sys.maxsize - 1
firstArray = []
secondArray = []
while start < n - 1:
subLength = 1
while (start + subLength) < n:
first = input[start:(start + subLength)]
second = input[(start + subLength):]
subLength += 1
firstSum = 0
for element in first:
firstSum += element
secondSum = 0
for element in second:
secondSum += element
if fabs(firstSum - secondSum) > maxSum:
maxSum = fabs(secondSum - firstSum)
firstArray = first
secondArray = second
start += 1
print(firstArray, secondArray)
Python Version
import sys
input = [[4, 10, 15, 24, 26], [0, 9, 12, 20], [5, 18, 22, 30]]
k = len(input)
counter = [0] * k
def permutations():
minRange = []
while True:
group = []
for i, t in enumerate(counter):
group.append(input[i][t])
group.sort()
if len(minRange) == 0:
minRange = group
elif (group[k - 1] - group[0]) < (minRange[k - 1] - minRange[0]):
minRange = group
i = 0
while i < len(counter):
counter[i] += 1
if counter[i] == len(input[i]):
counter[i] = 0
i += 1
if i == len(counter):
print(minRange)
return
else:
break
permutations()
This is not DP problem. There is no way to split into subproblems and reuse solutions
- om471987 June 12, 2016Here is the greedy solution
{{
public static void PrintSteppingNumbers(int start, int end)
{
for (var input = start; input < end; input++)
{
var isFirst = true;
var notMatch = false;
int previous = 0;
while (input > 0)
{
if (isFirst)
{
isFirst = false;
previous = input % 10;
}
else
{
if (Math.Abs(previous - (input % 10)) != 1)
{
notMatch = true;
break;
}
}
input = input / 10;
}
if (!notMatch)
{
Console.WriteLine(input);
}
}
}
}}