Skip to content

Commit 1460518

Browse files
add 3 random questions
1 parent 8ffe904 commit 1460518

File tree

3 files changed

+187
-50
lines changed

3 files changed

+187
-50
lines changed

build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ sourceCompatibility = 1.8
1515
targetCompatibility = 1.8
1616

1717
repositories {
18-
maven { url "http://repo.maven.apache.org/maven2" }
18+
mavenCentral()
19+
maven { url "http://repo.maven.apache.org/maven2" }
1920
}
2021

2122
dependencies {
23+
compile 'com.google.code.gson:gson:2.8.0'
2224
testCompile group: 'junit', name: 'junit', version:'4.12'
2325
}
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.google.gson.JsonArray;
4+
import com.google.gson.JsonElement;
5+
import com.google.gson.JsonObject;
6+
import com.google.gson.JsonParser;
7+
8+
import java.io.BufferedReader;
9+
import java.io.InputStreamReader;
10+
import java.net.HttpURLConnection;
11+
import java.net.URL;
12+
import java.util.ArrayList;
13+
import java.util.Collections;
14+
import java.util.List;
15+
import java.util.Map;
16+
import java.util.TreeMap;
17+
18+
public class _99999RandomQuestions {
19+
20+
public static void main(String... args) {
21+
int[] nums = new int[]{1, 2, 3, 4, 5, -1, -3, -6, 3, 2, -4};
22+
// int[] nums = new int[]{-1, -2, 1,2,3};
23+
// int[] nums = new int[]{-1, -2, 1,2,3,-1, -2};
24+
// List<int[]> result = subarraySum_v2(nums);
25+
26+
System.out.println(rollingString("abc", new String[]{"0 0 L", "2 2 L", "0 2 R"}));
27+
28+
GetMovies getMovies = new GetMovies();
29+
System.out.println(getMovies.getMovieTitles("spiderman"));
30+
31+
System.out.println(counting("00110"));
32+
33+
}
34+
35+
static String rollingString(String s, String[] operations) {
36+
char[] chars = s.toCharArray();
37+
for (String operation : operations) {
38+
String[] ops = operation.split(" ");
39+
for (int i = Integer.parseInt(ops[0]); i <= Integer.parseInt(ops[1]); i++) {
40+
if ("L".equals(ops[2])) {
41+
if (chars[i] == 'a') {
42+
chars[i] = 'z';
43+
} else {
44+
chars[i] -= 1;
45+
}
46+
} else if ("R".equals(ops[2])) {
47+
if (chars[i] == 'z') {
48+
chars[i] = 'a';
49+
} else {
50+
chars[i] += 1;
51+
}
52+
}
53+
}
54+
}
55+
return new String(chars);
56+
}
57+
58+
public static class GetMovies {
59+
static String[] getMovieTitles(String substr) {
60+
final String url = "https://jsonmock.hackerrank.com/api/movies/search/?Title=";
61+
List<String> movies = new ArrayList<>();
62+
try {
63+
String response = getResponse(url + substr);
64+
JsonParser parser = new JsonParser();
65+
JsonElement rootNode = parser.parse(response);
66+
67+
JsonObject details = rootNode.getAsJsonObject();
68+
69+
JsonElement totalMovies = details.get("total");
70+
System.out.println(totalMovies.toString());
71+
72+
JsonElement totalPages = details.get("total_pages");
73+
System.out.println(totalPages.toString());
74+
75+
int currentPage = 0;
76+
while (currentPage++ < Integer.parseInt(totalPages.toString())) {
77+
nextPage(movies, currentPage, substr);
78+
}
79+
Collections.sort(movies);
80+
} catch (Exception e) {
81+
e.printStackTrace();
82+
}
83+
String[] result = new String[movies.size()];
84+
return movies.toArray(result);
85+
}
86+
87+
static void nextPage(List<String> movies, int currentPage, String substr) throws Exception {
88+
final String url = "https://jsonmock.hackerrank.com/api/movies/search/?Title=";
89+
String response = getResponse(url + substr + "&page=" + currentPage);
90+
JsonParser parser = new JsonParser();
91+
JsonElement rootNode = parser.parse(response);
92+
93+
JsonObject details = rootNode.getAsJsonObject();
94+
JsonElement data = details.get("data");
95+
JsonArray jsonArray = data.getAsJsonArray();
96+
for (JsonElement each : jsonArray) {
97+
JsonObject titleObject = each.getAsJsonObject();
98+
String title = titleObject.get("Title").getAsString();
99+
movies.add(title);
100+
}
101+
}
102+
103+
static String getResponse(String urlToRead) throws Exception {
104+
StringBuilder result = new StringBuilder();
105+
URL url = new URL(urlToRead);
106+
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
107+
conn.setRequestMethod("GET");
108+
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
109+
String line;
110+
while ((line = rd.readLine()) != null) {
111+
result.append(line);
112+
}
113+
rd.close();
114+
return result.toString();
115+
}
116+
}
117+
118+
/**Problem: count binary substrings:
119+
* The 0's and 1's are grouped consecutively and their numbers are equal
120+
* e.g.
121+
* 00110 => 3 because there are 3 substrings that have equal number of consecutive 1's and 0's: 0011, 01, 10
122+
* 10101 => 4, there are 4 substrings: 10, 01, 10, 01*/
123+
static int counting(String s) {
124+
int n = s.length();
125+
/**a[i][0] denotes from most left up to i (inclusive), how many consecutive 0's
126+
* a[i][1] denotes from most left up to i (inclusive), how many consecutive 1's*/
127+
int[][] a = new int[n][2];
128+
/**a[i][0] denotes from i (inclusive) to the most right, how many consecutive 0's
129+
* b[i][0] denotes from i (inclusive) to the most right, how many consecutive 1's*/
130+
int[][] b = new int[n][2];
131+
for (int i = 0; i < n; i++) {
132+
if (s.charAt(i) == '0') {
133+
a[i][0] = 1 + (i - 1 >= 0 ? a[i - 1][0] : 0);
134+
} else {
135+
a[i][1] = 1 + (i - 1 >= 0 ? a[i - 1][1] : 0);
136+
}
137+
}
138+
for (int i = n - 1; i >= 0; i--) {
139+
if (s.charAt(i) == '0') {
140+
b[i][0] = 1 + (i + 1 < n ? b[i + 1][0] : 0);
141+
} else {
142+
b[i][1] = 1 + (i + 1 < n ? b[i + 1][1] : 0);
143+
}
144+
145+
}
146+
long ans = 0;
147+
for (int i = 0; i + 1 < n; i++) {
148+
ans += Math.min(a[i][0], b[i + 1][1]);
149+
ans += Math.min(a[i][1], b[i + 1][0]);
150+
}
151+
return (int) ans;
152+
}
153+
154+
public static class SubArraySum {
155+
/**
156+
* Given an array, return the start/end indices of the contiguous subarray that have the largest sum.
157+
* Note:
158+
* 1. There could be multiple subarrays, return all of the indices.
159+
*/
160+
public static List<int[]> subarraySum(int[] nums) {
161+
int[] preSums = new int[nums.length + 1];
162+
for (int i = 1; i <= nums.length; i++) {
163+
preSums[i] = preSums[i - 1] + nums[i - 1];
164+
}
165+
TreeMap<Integer, List<int[]>> preSum = new TreeMap(Collections.reverseOrder());
166+
for (int i = 1; i <= nums.length; i++) {
167+
for (int j = 0; j < i - 1; j++) {
168+
int sum = preSums[i] - preSums[j];
169+
if (!preSum.containsKey(sum)) {
170+
List<int[]> value = new ArrayList<>();
171+
value.add(new int[]{j, i - 1});
172+
preSum.put(sum, value);
173+
} else {
174+
List<int[]> value = preSum.get(sum);
175+
value.add(new int[]{j, i - 1});
176+
preSum.put(sum, value);
177+
}
178+
}
179+
}
180+
Map.Entry<Integer, List<int[]>> firstEntry = preSum.firstEntry();
181+
return firstEntry.getValue();
182+
}
183+
}
184+
}

src/main/java/com/fishercoder/solutions/_999MSOnlineAssessment.java

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy