Skip to content

Commit 928d4d0

Browse files
authored
Added tasks 153-208
1 parent 1eca4f5 commit 928d4d0

File tree

31 files changed

+989
-0
lines changed

31 files changed

+989
-0
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g0101_0200.s0153_find_minimum_in_rotated_sorted_array
2+
3+
// #Medium #Top_100_Liked_Questions #Array #Binary_Search #Algorithm_II_Day_2_Binary_Search
4+
// #Binary_Search_I_Day_12 #Udemy_Binary_Search #Big_O_Time_O(log_N)_Space_O(log_N)
5+
// #2023_11_05_Time_457_ms_(78.79%)_Space_55.5_MB_(6.06%)
6+
7+
object Solution {
8+
def findMin(nums: Array[Int]): Int = {
9+
def findMinUtil(l: Int, r: Int): Int = {
10+
if (l == r) {
11+
nums(l)
12+
} else {
13+
val mid = (l + r) / 2
14+
if (mid == l && nums(mid) < nums(r)) {
15+
nums(l)
16+
} else if (mid - 1 >= 0 && nums(mid - 1) > nums(mid)) {
17+
nums(mid)
18+
} else if (nums(mid) < nums(l)) {
19+
findMinUtil(l, mid - 1)
20+
} else if (nums(mid) > nums(r)) {
21+
findMinUtil(mid + 1, r)
22+
} else {
23+
findMinUtil(l, mid - 1)
24+
}
25+
}
26+
}
27+
28+
val l = 0
29+
val r = nums.length - 1
30+
findMinUtil(l, r)
31+
}
32+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
153\. Find Minimum in Rotated Sorted Array
2+
3+
Medium
4+
5+
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,2,4,5,6,7]` might become:
6+
7+
* `[4,5,6,7,0,1,2]` if it was rotated `4` times.
8+
* `[0,1,2,4,5,6,7]` if it was rotated `7` times.
9+
10+
Notice that **rotating** an array `[a[0], a[1], a[2], ..., a[n-1]]` 1 time results in the array `[a[n-1], a[0], a[1], a[2], ..., a[n-2]]`.
11+
12+
Given the sorted rotated array `nums` of **unique** elements, return _the minimum element of this array_.
13+
14+
You must write an algorithm that runs in `O(log n) time.`
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [3,4,5,1,2]
19+
20+
**Output:** 1
21+
22+
**Explanation:** The original array was [1,2,3,4,5] rotated 3 times.
23+
24+
**Example 2:**
25+
26+
**Input:** nums = [4,5,6,7,0,1,2]
27+
28+
**Output:** 0
29+
30+
**Explanation:** The original array was [0,1,2,4,5,6,7] and it was rotated 4 times.
31+
32+
**Example 3:**
33+
34+
**Input:** nums = [11,13,15,17]
35+
36+
**Output:** 11
37+
38+
**Explanation:** The original array was [11,13,15,17] and it was rotated 4 times.
39+
40+
**Constraints:**
41+
42+
* `n == nums.length`
43+
* `1 <= n <= 5000`
44+
* `-5000 <= nums[i] <= 5000`
45+
* All the integers of `nums` are **unique**.
46+
* `nums` is sorted and rotated between `1` and `n` times.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package g0101_0200.s0155_min_stack
2+
3+
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Stack #Design
4+
// #Data_Structure_II_Day_14_Stack_Queue #Programming_Skills_II_Day_18 #Level_2_Day_16_Design
5+
// #Udemy_Design #Big_O_Time_O(1)_Space_O(N)
6+
// #2023_11_05_Time_566_ms_(90.91%)_Space_60.6_MB_(84.85%)
7+
8+
class MinStack {
9+
private class Node(var min: Int, var data: Int, var previousNode: Node, var nextNode: Node)
10+
11+
private var currentNode: Node = _
12+
13+
def push(`val`: Int): Unit = {
14+
if (currentNode == null) {
15+
currentNode = new Node(`val`, `val`, null, null)
16+
} else {
17+
currentNode.nextNode = new Node(Math.min(currentNode.min, `val`), `val`, currentNode, null)
18+
currentNode = currentNode.nextNode
19+
}
20+
}
21+
22+
def pop(): Unit = {
23+
currentNode = currentNode.previousNode
24+
}
25+
26+
def top(): Int = {
27+
currentNode.data
28+
}
29+
30+
def getMin(): Int = {
31+
currentNode.min
32+
}
33+
}
34+
35+
/*
36+
* Your MinStack object will be instantiated and called as such:
37+
* var obj = new MinStack()
38+
* obj.push(`val`)
39+
* obj.pop()
40+
* var param_3 = obj.top()
41+
* var param_4 = obj.getMin()
42+
*/
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
155\. Min Stack
2+
3+
Easy
4+
5+
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
6+
7+
Implement the `MinStack` class:
8+
9+
* `MinStack()` initializes the stack object.
10+
* `void push(int val)` pushes the element `val` onto the stack.
11+
* `void pop()` removes the element on the top of the stack.
12+
* `int top()` gets the top element of the stack.
13+
* `int getMin()` retrieves the minimum element in the stack.
14+
15+
**Example 1:**
16+
17+
**Input**
18+
19+
["MinStack","push","push","push","getMin","pop","top","getMin"]
20+
[[],[-2],[0],[-3],[],[],[],[]]
21+
22+
**Output:** [null,null,null,null,-3,null,0,-2]
23+
24+
**Explanation:**
25+
26+
MinStack minStack = new MinStack();
27+
minStack.push(-2);
28+
minStack.push(0);
29+
minStack.push(-3);
30+
minStack.getMin(); // return -3
31+
minStack.pop();
32+
minStack.top(); // return 0
33+
minStack.getMin(); // return -2
34+
35+
**Constraints:**
36+
37+
* <code>-2<sup>31</sup> <= val <= 2<sup>31</sup> - 1</code>
38+
* Methods `pop`, `top` and `getMin` operations will always be called on **non-empty** stacks.
39+
* At most <code>3 * 10<sup>4</sup></code> calls will be made to `push`, `pop`, `top`, and `getMin`.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g0101_0200.s0160_intersection_of_two_linked_lists
2+
3+
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Hash_Table #Two_Pointers #Linked_List
4+
// #Data_Structure_II_Day_11_Linked_List #Udemy_Linked_List #Big_O_Time_O(M+N)_Space_O(1)
5+
// #2023_11_05_Time_564_ms_(96.43%)_Space_59.1_MB_(10.71%)
6+
7+
import com_github_leetcode.ListNode
8+
9+
/*
10+
* Definition for singly-linked list.
11+
* class ListNode(var _x: Int = 0) {
12+
* var next: ListNode = null
13+
* var x: Int = _x
14+
* }
15+
*/
16+
object Solution {
17+
def getIntersectionNode(headA: ListNode, headB: ListNode): ListNode = {
18+
var node1 = headA
19+
var node2 = headB
20+
21+
while (node1 != node2) {
22+
node1 = if (node1 == null) headB else node1.next
23+
node2 = if (node2 == null) headA else node2.next
24+
}
25+
26+
node1
27+
}
28+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
160\. Intersection of Two Linked Lists
2+
3+
Easy
4+
5+
Given the heads of two singly linked-lists `headA` and `headB`, return _the node at which the two lists intersect_. If the two linked lists have no intersection at all, return `null`.
6+
7+
For example, the following two linked lists begin to intersect at node `c1`:
8+
9+
![](https://assets.leetcode.com/uploads/2021/03/05/160_statement.png)
10+
11+
The test cases are generated such that there are no cycles anywhere in the entire linked structure.
12+
13+
**Note** that the linked lists must **retain their original structure** after the function returns.
14+
15+
**Custom Judge:**
16+
17+
The inputs to the **judge** are given as follows (your program is **not** given these inputs):
18+
19+
* `intersectVal` - The value of the node where the intersection occurs. This is `0` if there is no intersected node.
20+
* `listA` - The first linked list.
21+
* `listB` - The second linked list.
22+
* `skipA` - The number of nodes to skip ahead in `listA` (starting from the head) to get to the intersected node.
23+
* `skipB` - The number of nodes to skip ahead in `listB` (starting from the head) to get to the intersected node.
24+
25+
The judge will then create the linked structure based on these inputs and pass the two heads, `headA` and `headB` to your program. If you correctly return the intersected node, then your solution will be **accepted**.
26+
27+
**Example 1:**
28+
29+
![](https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png)
30+
31+
**Input:** intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
32+
33+
**Output:** Intersected at '8'
34+
35+
**Explanation:** The intersected node's value is 8 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,6,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.
36+
37+
**Example 2:**
38+
39+
![](https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png)
40+
41+
**Input:** intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
42+
43+
**Output:** Intersected at '2'
44+
45+
**Explanation:** The intersected node's value is 2 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [1,9,1,2,4]. From the head of B, it reads as [3,2,4]. There are 3 nodes before the intersected node in A; There are 1 node before the intersected node in B.
46+
47+
**Example 3:**
48+
49+
![](https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png)
50+
51+
**Input:** intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
52+
53+
**Output:** No intersection
54+
55+
**Explanation:** From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5]. Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values. Explanation: The two lists do not intersect, so return null.
56+
57+
**Constraints:**
58+
59+
* The number of nodes of `listA` is in the `m`.
60+
* The number of nodes of `listB` is in the `n`.
61+
* <code>0 <= m, n <= 3 * 10<sup>4</sup></code>
62+
* <code>1 <= Node.val <= 10<sup>5</sup></code>
63+
* `0 <= skipA <= m`
64+
* `0 <= skipB <= n`
65+
* `intersectVal` is `0` if `listA` and `listB` do not intersect.
66+
* `intersectVal == listA[skipA] == listB[skipB]` if `listA` and `listB` intersect.
67+
68+
**Follow up:** Could you write a solution that runs in `O(n)` time and use only `O(1)` memory?
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package g0101_0200.s0169_majority_element
2+
3+
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table #Sorting #Counting
4+
// #Divide_and_Conquer #Data_Structure_II_Day_1_Array #Udemy_Famous_Algorithm
5+
// #Big_O_Time_O(n)_Space_O(1) #2023_11_05_Time_538_ms_(92.57%)_Space_59.7_MB_(52.70%)
6+
7+
object Solution {
8+
def majorityElement(nums: Array[Int]): Int = {
9+
var count = 1
10+
var majority = nums(0)
11+
12+
// For Potential Majority Element
13+
for (i <- 1 until nums.length) {
14+
if (nums(i) == majority) {
15+
count += 1
16+
} else {
17+
if (count > 1) {
18+
count -= 1
19+
} else {
20+
majority = nums(i)
21+
}
22+
}
23+
}
24+
25+
// For Confirmation
26+
count = 0
27+
for (j <- nums) {
28+
if (j == majority) {
29+
count += 1
30+
}
31+
}
32+
33+
if (count >= (nums.length / 2) + 1) {
34+
majority
35+
} else {
36+
-1
37+
}
38+
}
39+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
169\. Majority Element
2+
3+
Easy
4+
5+
Given an array `nums` of size `n`, return _the majority element_.
6+
7+
The majority element is the element that appears more than `⌊n / 2⌋` times. You may assume that the majority element always exists in the array.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [3,2,3]
12+
13+
**Output:** 3
14+
15+
**Example 2:**
16+
17+
**Input:** nums = [2,2,1,1,1,2,2]
18+
19+
**Output:** 2
20+
21+
**Constraints:**
22+
23+
* `n == nums.length`
24+
* <code>1 <= n <= 5 * 10<sup>4</sup></code>
25+
* <code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code>
26+
27+
**Follow-up:** Could you solve the problem in linear time and in `O(1)` space?
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g0101_0200.s0189_rotate_array
2+
3+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Math #Two_Pointers
4+
// #Algorithm_I_Day_2_Two_Pointers #Udemy_Arrays #Big_O_Time_O(n)_Space_O(1)
5+
// #2023_11_05_Time_600_ms_(96.59%)_Space_73.7_MB_(17.05%)
6+
7+
object Solution {
8+
private def reverse(nums: Array[Int], l: Int, r: Int): Unit = {
9+
var left = l
10+
var right = r
11+
while (left <= right) {
12+
val temp = nums(left)
13+
nums(left) = nums(right)
14+
nums(right) = temp
15+
left += 1
16+
right -= 1
17+
}
18+
}
19+
20+
def rotate(nums: Array[Int], k: Int): Unit = {
21+
val n = nums.length
22+
val t = n - (k % n)
23+
reverse(nums, 0, t - 1)
24+
reverse(nums, t, n - 1)
25+
reverse(nums, 0, n - 1)
26+
}
27+
}

0 commit comments

Comments
 (0)