blob: fd29cd1f34cf697fa22050f6c4f94dc94ffc1135 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
Task 1: "Words Length
You are given a string $S with 3 or more words.
Write a script to find the length of the string except the first and last words ignoring whitespace.
Example 1:
Input: $S = "The Weekly Challenge"
Output: 6
Example 2:
Input: $S = "The purpose of our lives is to be happy"
Output: 23
ME: WRONG! length(purpose of our lives is to be)==29
My notes: simple, clearly defined: use the power of regexes.
Task 2: "Flip Array
You are given an array @A of positive numbers.
Write a script to flip the sign of some members of the given array so
that the sum of the all members is minimum non-negative.
Given an array of positive elements, you have to flip the sign of some
of its elements such that the resultant sum of the elements of array
should be minimum non-negative(as close to zero as possible). Return
the minimum no. of elements whose sign needs to be flipped such that
the resultant sum is minimum non-negative.
Example 1:
Input: @A = (3, 10, 8)
Output: 1
Explanation:
Flipping the sign of just one element 10 gives the result 1 i.e. (3) +
(-10) + (8) = 1
Example 2:
Input: @A = (12, 2, 10)
Output: 1
Explanation:
Flipping the sign of just one element 12 gives the result 0 i.e. (-12) +
(2) + (10) = 0
My notes: clearly defined, but how to go about this one? several
possible approaches. brute force: each element may either be
negated or not; try all combinations:-) not very elegant, but does
the job..
|