Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 72fd0c0

Browse files
committed
Some coding solution added
1 parent db5e7a2 commit 72fd0c0

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed

‎AlgorithmCode/MaximumSumSubArray.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public static void maximumSumSubArray (int arr[]) {
2020

2121
public static void main(String[] args) {
2222
int arr [] = {5,7,-13,2,8,10};
23+
// Output:
24+
// 20 (2 + 8 + 10)
2325
maximumSumSubArray(arr);
2426
}
2527
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
public class CodingQuestion10_4 {
2+
// We can't use size or length method of an array in the solution method
3+
public static void main(String[] args) {
4+
int arr[] = {1,2,3,4,5,6,7,8,9,13,15,20,31,33,40,50,100};
5+
int target = 13;
6+
7+
int answer = solution(arr, target);
8+
System.out.println(answer);
9+
}
10+
11+
public static int solution(int arr[], int target) {
12+
int left = 0;
13+
int right = Integer.MAX_VALUE;
14+
while (left < right) {
15+
int mid = (int)(Math.ceil((left + (long)right) / 2.0));
16+
if (elementAt(mid, arr) == -1)
17+
right = mid - 1;
18+
else
19+
left = mid;
20+
}
21+
22+
int limitIdx = left;
23+
24+
25+
left = 0;
26+
right = limitIdx;
27+
while (left <= right) {
28+
int mid = (left + right) / 2;
29+
if (arr[mid] > target) {
30+
right = mid - 1;
31+
} else if (arr[mid] < target){
32+
left = mid + 1;
33+
} else
34+
return mid;
35+
}
36+
37+
// Not Found
38+
return -1;
39+
}
40+
41+
public static int elementAt(int i, int arr[]) {
42+
if (i > arr.length - 1)
43+
return -1;
44+
else
45+
return arr[i];
46+
}
47+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import java.util.HashMap;
2+
3+
public class CodingQuestion10_5 {
4+
public static void main(String[] args) {
5+
String strEx[] = {"at", "", "", "", "ball", "", "", "car", "", "", "dad", "", ""};
6+
HashMap<String, Integer> hm = new HashMap<>();
7+
for (int i = 0; i < strEx.length; i++) {
8+
if (!strEx.equals("")) {
9+
hm.put(strEx[i], i);
10+
}
11+
}
12+
String Query = "car";
13+
System.out.println(Query(hm, Query));
14+
}
15+
16+
public static int Query(HashMap hm, String Query) {
17+
return (int)hm.get(Query);
18+
}
19+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import java.util.*;
2+
import java.util.LinkedList;
3+
4+
public class programmers_hash_0 {
5+
public boolean solution(String phone_book[]) {
6+
Trie tree = new Trie(new Trie.TrieNode('@'));
7+
Arrays.sort(phone_book);
8+
for (int i = 0; i < phone_book.length; i++) {
9+
if (!tree.insert(phone_book[i])){
10+
return false;
11+
}
12+
}
13+
return true;
14+
}
15+
16+
public static void main(String[] args) {
17+
programmers_hash_0 sol = new programmers_hash_0();
18+
String arr[] = {"12", "123", "1111"};
19+
System.out.println(sol.solution(arr));
20+
}
21+
22+
static class Trie {
23+
TrieNode root;
24+
25+
public Trie(TrieNode root) {
26+
this.root = root;
27+
}
28+
29+
static class TrieNode {
30+
char a;
31+
TrieNode children[];
32+
boolean isTerminal;
33+
34+
public TrieNode(char a) {
35+
this.a = a;
36+
this.children = new TrieNode[10];
37+
}
38+
}
39+
40+
public boolean insert(String str) {
41+
TrieNode temp = this.root;
42+
for (int i = 0; i < str.length(); i++) {
43+
char character = str.charAt(i);
44+
if (temp.children[character-'0'] != null) {
45+
if (temp.children[character-'0'].isTerminal)
46+
return false;
47+
temp = temp.children[character-'0'];
48+
}
49+
else {
50+
temp.children[character-'0'] = new TrieNode(character);
51+
temp = temp.children[character-'0'];
52+
}
53+
}
54+
temp.isTerminal = true;
55+
return true;
56+
}
57+
}
58+
}
59+
60+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /