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 66cc23a

Browse files
datastructures
1 parent 7e810e5 commit 66cc23a

File tree

3 files changed

+345
-0
lines changed

3 files changed

+345
-0
lines changed
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
package ArraysAndStrings;
2+
3+
import java.util.*;
4+
5+
// Byte-by-byte and cracking the coding interview
6+
public class FiftyQuestions {
7+
8+
/** 1. Find median of two sorted arrays
9+
* arr1 = [1, 3, 5]
10+
* arr2 = [2, 4, 6]
11+
* median(arr1, arr2) = 3.5
12+
* */
13+
static double median(int[] nums1, int[] nums2) {
14+
int totalLength = nums1.length + nums2.length;
15+
int medianIndex;
16+
boolean even = totalLength % 2 == 0;
17+
if (even) {
18+
medianIndex = totalLength / 2;
19+
}
20+
else {
21+
medianIndex = (totalLength - 1) / 2;
22+
}
23+
24+
int i = 0, j = 0;
25+
int[] nums3 = new int[medianIndex + 1];
26+
for (int k = 0; k <= medianIndex; k++) {
27+
if (i >= nums1.length)
28+
nums3[k] = nums2[j++];
29+
else if (j >= nums2.length)
30+
nums3[k] = nums1[i++];
31+
else if (nums1[i] <= nums2[j])
32+
nums3[k] = nums1[i++];
33+
else {
34+
nums3[k] = nums2[j++];
35+
}
36+
}
37+
38+
if (even)
39+
return (nums3[medianIndex] + nums3[medianIndex-1]) / 2.0;
40+
else
41+
return nums3[medianIndex];
42+
}
43+
44+
/** 2. Given a list of items with values and weights, as well as a max weight, find the
45+
maximum value you can generate from items where the sum of the weights is less than
46+
the max.
47+
*/
48+
// Item class
49+
static class Item {
50+
int weight;
51+
int value;
52+
53+
public Item(int weight, int value) {
54+
this.weight = weight;
55+
this.value = value;
56+
}
57+
}
58+
59+
static int knapsack(Item[] items, int W) {
60+
// cache[i][j] = max value for the first i items with a max weight of j
61+
int[][] cache = new int[items.length + 1][W + 1];
62+
for (int i = 1; i <= items.length; i++) {
63+
for (int j = 0; j <= W; j++) {
64+
// If including item[i-1] would exceed max weight j, don't
65+
// include the item. Otherwise take the max value of including
66+
// or excluding the item
67+
if (items[i-1].weight > j) cache[i][j] = cache[i-1][j];
68+
else cache[i][j] = Math.max(cache[i-1][j], cache[i-1][j-items[i-1].weight] + items[i-1].value);
69+
}
70+
}
71+
72+
return cache[items.length][W];
73+
}
74+
75+
/**
76+
* 4. Find Duplicates
77+
*
78+
* Given an array of integers where each value 1 <= x <= len(array), write a
79+
* function that finds all the duplicates in the array.
80+
* */
81+
static int[] findDuplicates(int[] arr) {
82+
Set<Integer> uniqueNums = new HashSet<>();
83+
int[] dups = new int[arr.length];
84+
int j = 0;
85+
for (int num : arr) {
86+
if (!uniqueNums.add(num)) {
87+
dups[j++] = num;
88+
}
89+
}
90+
return dups;
91+
}
92+
93+
static int consecutiveArray(int[] arr) {
94+
Set<Integer> values = new HashSet<>();
95+
for (int i : arr) {
96+
values.add(i);
97+
}
98+
99+
// For each value, check if its the first in a sequence of consecutive
100+
// numbers and iterate through to find the length of the consecutive
101+
// sequence
102+
int maxLength = 0;
103+
for (int i : values) {
104+
// If it is not the leftmost value in the sequence, don't bother
105+
if (values.contains(i - 1))
106+
continue;
107+
int length = 0;
108+
109+
// Iterate through sequence
110+
while (values.contains(i++)) {
111+
length++;
112+
}
113+
maxLength = Math.max(maxLength, length);
114+
}
115+
116+
return maxLength;
117+
}
118+
119+
/**
120+
* ctci 17.6
121+
* write a method to find indices m and n s.t. if you sorted elements m through n,
122+
* the entire array would be sorted. Minimize n-m
123+
* Input : 1,2,4,7,10,7,12,6,7,16,18,19
124+
* Output: 3,9
125+
* */
126+
static void findIndices(int[] arr) { //not correct
127+
int firstIndex = 0;
128+
int lastIndex = 0;
129+
int j = 0;
130+
for (int i = 0; i < arr.length-2; i++) {
131+
if (arr[i] > arr[i+1]) {
132+
firstIndex = i + 1;
133+
break;
134+
}
135+
}
136+
for (int i = arr.length-1; i > 2; i-- ) {
137+
if (arr[i] > arr[i-1]) {
138+
lastIndex = i;
139+
break;
140+
}
141+
}
142+
for (int i = 0; i <= firstIndex; i++) {
143+
if (arr[i] == arr[firstIndex]) {
144+
System.out.println("first index is " + i);
145+
break;
146+
}
147+
}
148+
for (int i = firstIndex; i <= lastIndex; i++) {
149+
if (arr[i] == arr[firstIndex]) {
150+
j = i;
151+
}
152+
}
153+
System.out.println("last index is = " + j );
154+
}
155+
156+
/** ctci 17.7 given 1284 print "One Thousand Two Hundred Eighty Four*/
157+
static void printIntegerInWords(int num) {
158+
159+
}
160+
161+
/** ctci 17.8 contiguous sequence with largest sum*/
162+
static int cSum(int[] arr) {
163+
int maxSum = 0;
164+
int currSum = 0;
165+
for (int j : arr) {
166+
currSum += j;
167+
maxSum = Math.max(maxSum, currSum);
168+
if (currSum < 0) {
169+
currSum = 0;
170+
}
171+
}
172+
return maxSum;
173+
}
174+
175+
static void allPairs(int[] arr, int k) {
176+
// 1,2,2,3 and k = 4 => (1,3) and (2,2)
177+
Map<Integer, Integer> nums = new HashMap<>();
178+
for (int num : arr)
179+
nums.merge(num, 1 , Integer::sum);
180+
for (int num : arr) {
181+
if (num == k - num && nums.get(num) > 1)
182+
System.out.printf("Pair - (%d:%d)%n", num, num);
183+
if (nums.containsKey(k-num))
184+
System.out.printf("Pair - (%d:%d)%n", num, k-num);
185+
186+
}
187+
}
188+
189+
static int search(int[] nums, int target, int left, int right) {
190+
// 50, 60, 70, 80, 0, 10, 20, 30, 40
191+
// 0 1 2 3 4 5 6 7 8
192+
int mid = (left + right) / 2;
193+
if (nums[mid] == target)
194+
return mid;
195+
else if (right < left)
196+
return -1;
197+
198+
if (nums[left] < nums[mid]) { // left of mid is sorted
199+
if (target <= nums[mid] && target >= nums[left]) { // element is in left array
200+
return search(nums, target, left, mid-1);
201+
}
202+
else {
203+
return search(nums, target, mid+1, right);
204+
}
205+
}
206+
else if (nums[mid] < nums[right]) { // right of mid is sorted
207+
if (target >= nums[mid] && target <= nums[right]) { //
208+
return search(nums, target, mid+1, right);
209+
}
210+
else {
211+
return search(nums, target, left, mid-1);
212+
}
213+
}
214+
else if (target <= nums[right]) {
215+
return search(nums, target, mid+1, right);
216+
}
217+
else {
218+
return search(nums, target, left, mid-1);
219+
}
220+
}
221+
222+
public static void main(String[] args) {
223+
int index = search(new int[]{2,2,2,3,4,2}, 3, 0, 5);
224+
System.out.println("index of 3 is " + index);
225+
}
226+
}

‎src/DataStructures/BinaryTree.java‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package DataStructures;
2+
3+
class Node {
4+
int val;
5+
Node left;
6+
Node right;
7+
8+
public Node() {
9+
this.left = null;
10+
this.right = null;
11+
}
12+
public Node(int val) {
13+
this.val = val;
14+
this.left = null;
15+
this.right = null;
16+
}
17+
}
18+
19+
public class BinaryTree {
20+
Node root;
21+
22+
23+
/**
24+
* calculate height of binary tree - dfs
25+
* */
26+
static int heightOfTree() {return 0;}
27+
}

‎src/DataStructures/LinkedList.java‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package DataStructures;
2+
3+
class ListNode {
4+
int val;
5+
ListNode next;
6+
ListNode() {}
7+
ListNode(int val) { this.val = val; }
8+
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
}
10+
11+
public class LinkedList {
12+
// Merge two sorted lists
13+
static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
14+
ListNode l3 = null;
15+
16+
while (l1 != null || l2 != null) {
17+
if (l1 != null) {
18+
if (l2 != null) {
19+
if (l1.next == null) { // reached end of list 1
20+
if (l1.val <= l2.val) {
21+
l3 = appendNode(l3, l1);
22+
l1 = l1.next;
23+
} else {
24+
l3 = appendNode(l3, l2);
25+
l2 = l2.next;
26+
}
27+
} else {
28+
if (l1.val <= l2.val) {
29+
l3 = appendNode(l3, l1);
30+
l1 = l1.next;
31+
}
32+
}
33+
}
34+
else {
35+
l3 = appendNode(l3, l1);
36+
l1 = l1.next;
37+
}
38+
}
39+
if (l2 != null ) {
40+
if (l1 != null) {
41+
if (l2.next == null) { // reached end of list 2
42+
if (l2.val <= l1.val) {
43+
l3 = appendNode(l3, l2);
44+
l2 = l2.next;
45+
}
46+
else {
47+
l3 = appendNode(l3, l1);
48+
l1 = l1.next;
49+
}
50+
}
51+
else {
52+
if (l2.val <= l1.val) {
53+
l3 = appendNode(l3, l2);
54+
l2 = l2.next;
55+
}
56+
}
57+
}
58+
else {
59+
l3 = appendNode(l3, l2);
60+
l2 = l2.next;
61+
}
62+
}
63+
}
64+
return l3;
65+
}
66+
67+
static ListNode appendNode(ListNode head, ListNode nodeToInsert) {
68+
if (head == null) {
69+
head = new ListNode(nodeToInsert.val);
70+
}
71+
else {
72+
ListNode currNode = head;
73+
while (currNode.next != null) {
74+
currNode = currNode.next;
75+
}
76+
currNode.next = new ListNode(nodeToInsert.val);
77+
}
78+
return head;
79+
}
80+
81+
public static void main(String[] args) {
82+
ListNode node1 = new ListNode(1);
83+
node1.next = new ListNode(2);
84+
node1.next.next = new ListNode(4);
85+
86+
ListNode node2 = new ListNode(1);
87+
node2.next = new ListNode(3);
88+
node2.next.next = new ListNode(4);
89+
90+
ListNode mergedList = mergeTwoLists(node1, node2);
91+
}
92+
}

0 commit comments

Comments
(0)

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