Skip to content

Commit 6cf3dc8

Browse files
committed
Add hackerrank and interviewbit
1 parent 9bf55b5 commit 6cf3dc8

File tree

8 files changed

+318
-0
lines changed

8 files changed

+318
-0
lines changed

Diff for: .gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33
*.cmx
44
*.o
55
*.cmo
6+
*.iml
7+
.idea
8+
out/

Diff for: README.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ My sport programming adventures.
44
* [codeforces.ru](http://codeforces.ru/profile/kronos)
55
* [topcoder](http://www.topcoder.com/member-profile/kronos_vano/algorithm)
66
* [acm.mipt.ru](http://acm.mipt.ru/judge/users.pl?user=kronos)
7+
* [hackerrank.com](https://www.hackerrank.com/kronos_vano)
8+
* [interviewbit.com](https://www.interviewbit.com/profile/ivan_samsonov)

Diff for: hackerrank.com/src/com/hackerrank/HappyLadyBug.java

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2016 Ivan Samsonov
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included
14+
* in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package com.hackerrank;
25+
26+
import java.io.IOException;
27+
import java.util.Arrays;
28+
29+
/**
30+
* An ${Name} class.
31+
*
32+
* <p>
33+
* Description
34+
* </p>
35+
*
36+
* @author Ivan Samsonov ([email protected])
37+
* @since 1.0.0
38+
*
39+
*/
40+
// https://www.hackerrank.com/challenges/happy-ladybugs
41+
public class HappyLadyBug extends Solution {
42+
43+
public void solve() throws IOException {
44+
int q = in.nextInt();
45+
for (int i = 0; i < q; i++) {
46+
in.nextInt();
47+
48+
String s = in.next();
49+
boolean space = false;
50+
boolean bug = false;
51+
boolean happy = true;
52+
boolean pair = true;
53+
int[] letters = new int[26];
54+
Arrays.fill(letters, 0);
55+
for (int j = 0; j < s.length(); j++) {
56+
char c = s.charAt(j);
57+
if (c == '_') {
58+
space = true;
59+
} else {
60+
bug = true;
61+
letters[(int)c - (int)'A']++;
62+
63+
happy &= ((j > 0) && (s.charAt(j-1) == c)) ||
64+
((j < s.length()-1) && (s.charAt(j+1) == c));
65+
}
66+
}
67+
68+
for (int j = 0; pair && (j < 26); j++) {
69+
pair = letters[j] != 1;
70+
}
71+
72+
if (happy || !bug || (bug && space && pair)) {
73+
System.out.println("YES");
74+
} else {
75+
System.out.println("NO");
76+
}
77+
}
78+
}
79+
}

Diff for: hackerrank.com/src/com/hackerrank/Main.java

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.hackerrank;
2+
3+
import java.io.IOException;
4+
5+
public class Main {
6+
public static void main(String[] args) throws IOException {
7+
// TODO
8+
}
9+
}

Diff for: hackerrank.com/src/com/hackerrank/Solution.java

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2016 Ivan Samsonov
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included
14+
* in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package com.hackerrank;
25+
/**
26+
* A Solution class.
27+
*
28+
* <p>
29+
* Description
30+
* </p>
31+
*
32+
* @author Ivan Samsonov ([email protected])
33+
* @since 1.0.0
34+
*
35+
*/
36+
37+
import java.io.*;
38+
import java.util.*;
39+
40+
public class Solution {
41+
protected Scanner in;
42+
43+
Solution() {
44+
this.in = new Scanner(System.in);
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2016 Ivan Samsonov
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included
14+
* in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package com.interviewbit;
25+
26+
import java.util.Arrays;
27+
import java.util.List;
28+
import java.util.Set;
29+
import java.util.TreeSet;
30+
31+
/**
32+
* An ${Name} class.
33+
*
34+
* <p>
35+
* Description
36+
* </p>
37+
*
38+
* @author Ivan Samsonov ([email protected])
39+
* @since 1.0.0
40+
*
41+
*/
42+
public class FindDuplicateInArray {
43+
public int repeatedNumber(final List<Integer> a) {
44+
int answer = -1;
45+
Set<Integer> set = new TreeSet<>();
46+
int length = a.size();
47+
int square = (int)Math.sqrt(length);
48+
int index = 0;
49+
int size = length / square;
50+
int[] counts = new int[size+1];
51+
int mod = length % size;
52+
Arrays.fill(counts, 0);
53+
for (Integer e: a) {
54+
index = e / square;
55+
if (e % square == 0) {
56+
index--;
57+
}
58+
59+
counts[index]++;
60+
61+
if ((counts[index] > square) || (mod > 0 && index == size && counts[index] >= mod)) {
62+
break;
63+
}
64+
}
65+
66+
for (Integer e : a) {
67+
int q = e / square;
68+
if (e % square == 0) {
69+
q--;
70+
}
71+
if (q == index) {
72+
if (set.contains(e)) {
73+
answer = e;
74+
break;
75+
} else {
76+
set.add(e);
77+
}
78+
}
79+
}
80+
81+
return answer;
82+
}
83+
}

Diff for: interviewbit.com/src/com/interviewbit/Main.java

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.interviewbit;
2+
3+
public class Main {
4+
5+
public static void main(String[] args) {
6+
// TODO
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2016 Ivan Samsonov
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included
14+
* in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package com.interviewbit;
25+
26+
import java.util.ArrayList;
27+
28+
/**
29+
* An ${Name} class.
30+
*
31+
* <p>
32+
* Description
33+
* </p>
34+
*
35+
* @author Ivan Samsonov ([email protected])
36+
* @since 1.0.0
37+
*
38+
*/
39+
public class MaxNonNegativeSubArray {
40+
public ArrayList<Integer> maxset(ArrayList<Integer> a) {
41+
int current_from = -1;
42+
int to = 0;
43+
int from = a.size();
44+
long max_sum = 0;
45+
long current_sum = 0;
46+
ArrayList<Integer> answer = new ArrayList<>();
47+
48+
for (int i = 0; i < a.size(); i++) {
49+
if (a.get(i) < 0) {
50+
if (current_from != -1) {
51+
if (current_sum > max_sum) {
52+
from = current_from;
53+
to = i;
54+
max_sum = current_sum;
55+
} else if (current_sum == max_sum && (to - from < i - current_from)) {
56+
from = current_from;
57+
to = i;
58+
}
59+
60+
current_sum = 0;
61+
current_from = -1;
62+
}
63+
} else {
64+
if (current_from == -1) {
65+
current_from = i;
66+
current_sum = a.get(i);
67+
} else {
68+
current_sum +=a.get(i);
69+
}
70+
}
71+
}
72+
if (current_from != -1) {
73+
if (current_sum > max_sum) {
74+
from = current_from;
75+
to = a.size();
76+
} else if (current_sum == max_sum && (to-from < a.size() - current_from)) {
77+
from = current_from;
78+
to = a.size();
79+
}
80+
}
81+
82+
for (int i = from; i < to; i++) {
83+
answer.add(a.get(i));
84+
}
85+
86+
return answer;
87+
}
88+
}

0 commit comments

Comments
 (0)