Skip to content

Commit 357f009

Browse files
authored
Added tasks 706, 707, 709, 710
1 parent e24331a commit 357f009

File tree

13 files changed

+516
-0
lines changed

13 files changed

+516
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
170170

171171
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
172172
|-|-|-|-|-|-
173+
| 0707 |[Design Linked List](src/main/kotlin/g0701_0800/s0707_design_linked_list/MyLinkedList.kt)| Medium | Design, Linked_List | 243 | 100.00
173174

174175
#### Day 20
175176

@@ -678,6 +679,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
678679
| 0138 |[Copy List with Random Pointer](src.save/main/kotlin/g0101_0200/s0138_copy_list_with_random_pointer/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Linked_List | 274 | 80.58
679680
| 0025 |[Reverse Nodes in k-Group](src.save/main/kotlin/g0001_0100/s0025_reverse_nodes_in_k_group/Solution.kt)| Hard | Top_100_Liked_Questions, Linked_List, Recursion | 194 | 87.72
680681
| 0146 |[LRU Cache](src.save/main/kotlin/g0101_0200/s0146_lru_cache/LRUCache.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Design, Linked_List, Doubly_Linked_List | 1116 | 97.93
682+
| 0707 |[Design Linked List](src/main/kotlin/g0701_0800/s0707_design_linked_list/MyLinkedList.kt)| Medium | Design, Linked_List | 243 | 100.00
681683

682684
#### Udemy Tree Stack Queue
683685

@@ -886,6 +888,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
886888
|-|-|-|-|-|-
887889
| 0075 |[Sort Colors](src.save/main/kotlin/g0001_0100/s0075_sort_colors/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers | 198 | 85.66
888890
| 0056 |[Merge Intervals](src.save/main/kotlin/g0001_0100/s0056_merge_intervals/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting | 323 | 99.68
891+
| 0706 |[Design HashMap](src/main/kotlin/g0701_0800/s0706_design_hashmap/MyHashMap.kt)| Easy | Array, Hash_Table, Design, Linked_List, Hash_Function | 405 | 92.11
889892

890893
#### Day 3 Array
891894

@@ -957,6 +960,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
957960
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
958961
|-|-|-|-|-|-
959962
| 0024 |[Swap Nodes in Pairs](src.save/main/kotlin/g0001_0100/s0024_swap_nodes_in_pairs/Solution.kt)| Medium | Top_100_Liked_Questions, Linked_List, Recursion | 149 | 99.39
963+
| 0707 |[Design Linked List](src/main/kotlin/g0701_0800/s0707_design_linked_list/MyLinkedList.kt)| Medium | Design, Linked_List | 243 | 100.00
960964

961965
#### Day 13 Linked List
962966

@@ -1656,6 +1660,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
16561660

16571661
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
16581662
|-|-|-|-|-|-
1663+
| 0709 |[To Lower Case](src/main/kotlin/g0701_0800/s0709_to_lower_case/Solution.kt)| Easy | String | 142 | 98.68
16591664

16601665
#### Day 10 Linked List and Tree
16611666

@@ -1687,6 +1692,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
16871692
| 0864 |[Shortest Path to Get All Keys](src/main/kotlin/g0801_0900/s0864_shortest_path_to_get_all_keys/Solution.kt)| Hard | Breadth_First_Search, Bit_Manipulation | 176 | 100.00
16881693
| 0763 |[Partition Labels](src/main/kotlin/g0701_0800/s0763_partition_labels/Solution.kt)| Medium | Top_100_Liked_Questions, String, Hash_Table, Greedy, Two_Pointers, Data_Structure_II_Day_7_String | 235 | 84.75
16891694
| 0739 |[Daily Temperatures](src/main/kotlin/g0701_0800/s0739_daily_temperatures/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Stack, Monotonic_Stack, Programming_Skills_II_Day_6 | 936 | 80.54
1695+
| 0710 |[Random Pick with Blacklist](src/main/kotlin/g0701_0800/s0710_random_pick_with_blacklist/Solution.kt)| Hard | Hash_Table, Math, Sorting, Binary_Search, Randomized | 632 | 100.00
1696+
| 0709 |[To Lower Case](src/main/kotlin/g0701_0800/s0709_to_lower_case/Solution.kt)| Easy | String, Programming_Skills_I_Day_9_String | 142 | 98.68
1697+
| 0707 |[Design Linked List](src/main/kotlin/g0701_0800/s0707_design_linked_list/MyLinkedList.kt)| Medium | Design, Linked_List, Data_Structure_II_Day_12_Linked_List, Programming_Skills_II_Day_19, Udemy_Linked_List | 243 | 100.00
1698+
| 0706 |[Design HashMap](src/main/kotlin/g0701_0800/s0706_design_hashmap/MyHashMap.kt)| Easy | Array, Hash_Table, Design, Linked_List, Hash_Function, Data_Structure_II_Day_2_Array | 405 | 92.11
16901699
| 0705 |[Design HashSet](src/main/kotlin/g0701_0800/s0705_design_hashset/MyHashSet.kt)| Easy | Array, Hash_Table, Design, Linked_List, Hash_Function | 385 | 75.61
16911700
| 0704 |[Binary Search](src/main/kotlin/g0701_0800/s0704_binary_search/Solution.kt)| Easy | Array, Binary_Search, Algorithm_I_Day_1_Binary_Search, Binary_Search_I_Day_1, Level_1_Day_7_Binary_Search, Udemy_Binary_Search | 261 | 77.91
16921701
| 0703 |[Kth Largest Element in a Stream](src/main/kotlin/g0701_0800/s0703_kth_largest_element_in_a_stream/KthLargest.kt)| Easy | Tree, Binary_Tree, Design, Heap_Priority_Queue, Binary_Search_Tree, Data_Stream | 286 | 95.45
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package g0701_0800.s0706_design_hashmap
2+
3+
// #Easy #Array #Hash_Table #Design #Linked_List #Hash_Function #Data_Structure_II_Day_2_Array
4+
// #2023_02_24_Time_405_ms_(92.11%)_Space_58.1_MB_(93.42%)
5+
6+
@Suppress("NAME_SHADOWING")
7+
class MyHashMap {
8+
private var arr: Array<MutableList<Entry>?>? = null
9+
10+
init {
11+
arr = arrayOfNulls<MutableList<Entry>?>(1000)
12+
}
13+
14+
fun put(key: Int, value: Int) {
15+
val bucket = key % 1000
16+
if (arr!![bucket] == null) {
17+
val list = ArrayList<Entry>()
18+
val e = Entry()
19+
e.key = key
20+
e.value = value
21+
list.add(e)
22+
arr!![bucket] = list
23+
} else {
24+
val list: MutableList<Entry>? = arr!![bucket]
25+
val e = Entry()
26+
e.key = key
27+
e.value = value
28+
list!!.remove(e)
29+
list.add(e)
30+
}
31+
}
32+
33+
fun get(key: Int): Int {
34+
val bucket = key % 1000
35+
var ans = -1
36+
val list: ArrayList<Entry>? = arr!![bucket] as ArrayList<Entry>?
37+
if (list != null) {
38+
for (e in list) {
39+
if (e.key == key) {
40+
ans = e.value
41+
}
42+
}
43+
}
44+
return ans
45+
}
46+
47+
fun remove(key: Int) {
48+
val bucket = key % 1000
49+
val list: ArrayList<Entry>? = arr!![bucket] as ArrayList<Entry>?
50+
val e = Entry()
51+
e.key = key
52+
list?.remove(e)
53+
}
54+
55+
internal class Entry {
56+
var key = 0
57+
var value = 0
58+
override fun hashCode(): Int {
59+
val prime = 31
60+
var result = 1
61+
result = prime * result + key
62+
return result
63+
}
64+
65+
override fun equals(other: Any?): Boolean {
66+
if (this === other) {
67+
return true
68+
}
69+
if (other == null) {
70+
return false
71+
}
72+
if (javaClass != other.javaClass) {
73+
return false
74+
}
75+
val other = other as Entry
76+
return key == other.key
77+
}
78+
}
79+
}
80+
81+
/*
82+
* Your MyHashMap object will be instantiated and called as such:
83+
* var obj = MyHashMap()
84+
* obj.put(key,value)
85+
* var param_2 = obj.get(key)
86+
* obj.remove(key)
87+
*/
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
706\. Design HashMap
2+
3+
Easy
4+
5+
Design a HashMap without using any built-in hash table libraries.
6+
7+
Implement the `MyHashMap` class:
8+
9+
* `MyHashMap()` initializes the object with an empty map.
10+
* `void put(int key, int value)` inserts a `(key, value)` pair into the HashMap. If the `key` already exists in the map, update the corresponding `value`.
11+
* `int get(int key)` returns the `value` to which the specified `key` is mapped, or `-1` if this map contains no mapping for the `key`.
12+
* `void remove(key)` removes the `key` and its corresponding `value` if the map contains the mapping for the `key`.
13+
14+
**Example 1:**
15+
16+
**Input**
17+
18+
["MyHashMap", "put", "put", "get", "get", "put", "get", "remove", "get"]
19+
20+
[[], [1, 1], [2, 2], [1], [3], [2, 1], [2], [2], [2]]
21+
22+
**Output:** [null, null, null, 1, -1, null, 1, null, -1]
23+
24+
**Explanation:**
25+
26+
MyHashMap myHashMap = new MyHashMap();
27+
myHashMap.put(1, 1); // The map is now [[1,1]]
28+
myHashMap.put(2, 2); // The map is now [[1,1], [2,2]]
29+
myHashMap.get(1); // return 1, The map is now [[1,1], [2,2]]
30+
myHashMap.get(3); // return -1 (i.e., not found), The map is now [[1,1], [2,2]]
31+
myHashMap.put(2, 1); // The map is now [[1,1], [2,1]] (i.e., update the existing value)
32+
myHashMap.get(2); // return 1, The map is now [[1,1], [2,1]]
33+
myHashMap.remove(2); // remove the mapping for 2, The map is now [[1,1]]
34+
myHashMap.get(2); // return -1 (i.e., not found), The map is now [[1,1]]
35+
36+
**Constraints:**
37+
38+
* <code>0 <= key, value <= 10<sup>6</sup></code>
39+
* At most <code>10<sup>4</sup></code> calls will be made to `put`, `get`, and `remove`.
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package g0701_0800.s0707_design_linked_list
2+
3+
// #Medium #Design #Linked_List #Data_Structure_II_Day_12_Linked_List #Programming_Skills_II_Day_19
4+
// #Udemy_Linked_List #2023_02_24_Time_243_ms_(100.00%)_Space_43.8_MB_(46.34%)
5+
6+
class MyLinkedList {
7+
private class Node(var `val`: Int) {
8+
var next: Node? = null
9+
}
10+
11+
private var head: Node?
12+
private var tail: Node? = null
13+
private var size: Int
14+
15+
init {
16+
head = tail
17+
size = 0
18+
}
19+
20+
operator fun get(index: Int): Int {
21+
if (index >= size) {
22+
return -1
23+
}
24+
if (index == 0) {
25+
return head!!.`val`
26+
}
27+
var i = 0
28+
var ptr = head
29+
while (i++ < index - 1) {
30+
ptr = ptr!!.next
31+
}
32+
return ptr!!.next!!.`val`
33+
}
34+
35+
fun addAtHead(`val`: Int) {
36+
val node = Node(`val`)
37+
if (head == null) {
38+
tail = node
39+
head = tail
40+
size++
41+
return
42+
}
43+
node.next = head
44+
head = node
45+
size++
46+
}
47+
48+
fun addAtTail(`val`: Int) {
49+
if (head == null) {
50+
addAtHead(`val`)
51+
return
52+
}
53+
val node = Node(`val`)
54+
tail!!.next = node
55+
tail = node
56+
size++
57+
}
58+
59+
fun addAtIndex(index: Int, `val`: Int) {
60+
if (index > size) {
61+
return
62+
}
63+
if (index == 0) {
64+
addAtHead(`val`)
65+
return
66+
}
67+
if (index == size) {
68+
addAtTail(`val`)
69+
return
70+
}
71+
var i = 0
72+
val node = Node(`val`)
73+
var ptr = head
74+
while (i++ < index - 1) {
75+
ptr = ptr!!.next
76+
}
77+
node.next = ptr!!.next
78+
ptr.next = node
79+
size++
80+
}
81+
82+
fun deleteAtIndex(index: Int) {
83+
if (index >= size) {
84+
return
85+
}
86+
if (index == 0) {
87+
head = head!!.next
88+
size--
89+
return
90+
}
91+
var i = 0
92+
var ptr = head
93+
while (i++ < index - 1) {
94+
ptr = ptr!!.next
95+
}
96+
ptr!!.next = ptr.next!!.next
97+
if (index == size - 1) {
98+
tail = ptr
99+
}
100+
size--
101+
}
102+
}
103+
104+
/*
105+
* Your MyLinkedList object will be instantiated and called as such:
106+
* var obj = MyLinkedList()
107+
* var param_1 = obj.get(index)
108+
* obj.addAtHead(`val`)
109+
* obj.addAtTail(`val`)
110+
* obj.addAtIndex(index,`val`)
111+
* obj.deleteAtIndex(index)
112+
*/
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
707\. Design Linked List
2+
3+
Medium
4+
5+
Design your implementation of the linked list. You can choose to use a singly or doubly linked list.
6+
A node in a singly linked list should have two attributes: `val` and `next`. `val` is the value of the current node, and `next` is a pointer/reference to the next node.
7+
If you want to use the doubly linked list, you will need one more attribute `prev` to indicate the previous node in the linked list. Assume all nodes in the linked list are **0-indexed**.
8+
9+
Implement the `MyLinkedList` class:
10+
11+
* `MyLinkedList()` Initializes the `MyLinkedList` object.
12+
* `int get(int index)` Get the value of the <code>index<sup>th</sup></code> node in the linked list. If the index is invalid, return `-1`.
13+
* `void addAtHead(int val)` Add a node of value `val` before the first element of the linked list. After the insertion, the new node will be the first node of the linked list.
14+
* `void addAtTail(int val)` Append a node of value `val` as the last element of the linked list.
15+
* `void addAtIndex(int index, int val)` Add a node of value `val` before the <code>index<sup>th</sup></code> node in the linked list. If `index` equals the length of the linked list, the node will be appended to the end of the linked list. If `index` is greater than the length, the node **will not be inserted**.
16+
* `void deleteAtIndex(int index)` Delete the <code>index<sup>th</sup></code> node in the linked list, if the index is valid.
17+
18+
**Example 1:**
19+
20+
**Input**
21+
22+
["MyLinkedList", "addAtHead", "addAtTail", "addAtIndex", "get", "deleteAtIndex", "get"]
23+
24+
[[], [1], [3], [1, 2], [1], [1], [1]]
25+
26+
**Output:** [null, null, null, null, 2, null, 3]
27+
28+
**Explanation:**
29+
30+
MyLinkedList myLinkedList = new MyLinkedList();
31+
myLinkedList.addAtHead(1);
32+
myLinkedList.addAtTail(3);
33+
myLinkedList.addAtIndex(1, 2); // linked list becomes 1->2->3
34+
myLinkedList.get(1); // return 2
35+
myLinkedList.deleteAtIndex(1); // now the linked list is 1->3
36+
myLinkedList.get(1); // return 3
37+
38+
**Constraints:**
39+
40+
* `0 <= index, val <= 1000`
41+
* Please do not use the built-in LinkedList library.
42+
* At most `2000` calls will be made to `get`, `addAtHead`, `addAtTail`, `addAtIndex` and `deleteAtIndex`.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package g0701_0800.s0709_to_lower_case
2+
3+
// #Easy #String #Programming_Skills_I_Day_9_String
4+
// #2023_02_24_Time_142_ms_(98.68%)_Space_35.1_MB_(7.89%)
5+
6+
class Solution {
7+
fun toLowerCase(s: String): String {
8+
val c = s.toCharArray()
9+
for (i in s.indices) {
10+
if (c[i] in 'A'..'Z') {
11+
c[i] = (c[i].code - 'A'.code + 'a'.code).toChar()
12+
}
13+
}
14+
return String(c)
15+
}
16+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
709\. To Lower Case
2+
3+
Easy
4+
5+
Given a string `s`, return _the string after replacing every uppercase letter with the same lowercase letter_.
6+
7+
**Example 1:**
8+
9+
**Input:** s = "Hello"
10+
11+
**Output:** "hello"
12+
13+
**Example 2:**
14+
15+
**Input:** s = "here"
16+
17+
**Output:** "here"
18+
19+
**Example 3:**
20+
21+
**Input:** s = "LOVELY"
22+
23+
**Output:** "lovely"
24+
25+
**Constraints:**
26+
27+
* `1 <= s.length <= 100`
28+
* `s` consists of printable ASCII characters.

0 commit comments

Comments
 (0)