Skip to content

Commit cb02115

Browse files
committed
add 817 java,
1 parent ecaace9 commit cb02115

File tree

5 files changed

+231
-11
lines changed

5 files changed

+231
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@
420420
369| [Plus One Linked List](https://leetcode.com/problems/plus-one-linked-list/)| [Python](./leetcode_python/Linked_list/plus-one-linked-list.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/LinkedList/PlusOneLinkedList.java) | _O(n)_ | _O(1)_ | Medium | 🔒, linkedlist, `basic`, google | AGAIN****** (3)
421421
445| [Add Two Numbers II](https://leetcode.com/problems/add-two-numbers-ii/)| [Python](./leetcode_python/Linked_list/add-two-numbers-ii.py) | _O(m + n)_ | _O(m + n)_| Medium |`trick`, linked list, string,`good basic`, `amazon`| AGAIN*** (3)
422422
725 | [Split Linked List in Parts](https://leetcode.com/problems/split-linked-list-in-parts/) | [Python](./leetcode_python/Linked_list/split-linked-list-in-parts.py) | _O(n + k)_ | _O(1)_ | Medium |mod, split linked list, linked list, good trick,`amazon`| AGAIN************ (6) (again)
423-
817 | [Linked List Components](https://leetcode.com/problems/linked-list-components/) | [Python](./leetcode_python/Linked_list/linked-list-components.py) | _O(m + n)_ | _O(m)_ | Medium || OK*
423+
817 | [Linked List Components](https://leetcode.com/problems/linked-list-components/) | [Python](./leetcode_python/Linked_list/linked-list-components.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/LinkedList/LinkedListComponents.java) | _O(m + n)_ | _O(m)_ | Medium |set, array, linkedlist, google| AGAIN** (1)
424424
430 | [Flatten a Multilevel Doubly Linked List](https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/) | [Python](./leetcode_python/Linked_list/flatten_a_multilevel_doubly_linked_list.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/LinkedList/FlattenMultilevelDoublyLinkedList.java) | || Medium |`good trick`,`doubly linked list`,`AGAIN`,`dfs`, `fb`, google| AGAIN********* (4)
425425
707| [Design Linked List](https://leetcode.com/problems/design-linked-list/) | [Python](./leetcode_python/Linked_list/design_linked_list.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/LinkedList/DesignLinkedList.java) | _O(n)_ | _O(h)_ | Medium|linked list basic OP| AGAIN (1)
426426
708| [Insert into a Cyclic Sorted List](https://leetcode.com/problems/insert-into-a-sorted-circular-linked-list) | [Python](./leetcode_python/Linked_list/insert_into_a_cyclic_sorted_sorted_linked_list.py) | _O(n)_ | _O(h)_ | Medium|`AGAIN`,`cyclic linked list`,`good trick`, `google`, `amazon`, `fb` | AGAIN******** (4)

data/progress.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
20241231: 769
1+
20241231: 769,817
22
20241229: 498(again),934
33
20241228: 379,173,855(todo)
44
20241227: 079,212(todo),362,849

data/to_review.txt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
1-
2025-02-24 -> ['769']
1+
2025-02-24 -> ['769,817']
22
2025-02-22 -> ['498(again),934']
33
2025-02-21 -> ['379,173,855(todo)']
44
2025-02-20 -> ['079,212(todo),362,849']
55
2025-02-15 -> ['369,311']
66
2025-02-14 -> ['370']
77
2025-02-13 -> ['815,871,593,1109']
88
2025-02-07 -> ['560,523']
9-
2025-02-03 -> ['769']
9+
2025-02-03 -> ['769,817']
1010
2025-02-01 -> ['498(again),934', '304,853,325']
1111
2025-01-31 -> ['379,173,855(todo)']
1212
2025-01-30 -> ['079,212(todo),362,849']
1313
2025-01-26 -> ['370(todo)']
1414
2025-01-25 -> ['369,311']
1515
2025-01-24 -> ['370', '34,767']
1616
2025-01-23 -> ['815,871,593,1109']
17-
2025-01-21 -> ['769']
17+
2025-01-21 -> ['769,817']
1818
2025-01-20 -> ['722,380']
1919
2025-01-19 -> ['498(again),934', '33,81']
2020
2025-01-18 -> ['379,173,855(todo)']
2121
2025-01-17 -> ['079,212(todo),362,849', '560,523', '253']
2222
2025-01-16 -> ['776,31']
2323
2025-01-15 -> ['004(todo),34(todo),162(todo),275(todo)']
2424
2025-01-14 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
25-
2025-01-13 -> ['769']
25+
2025-01-13 -> ['769,817']
2626
2025-01-12 -> ['369,311']
2727
2025-01-11 -> ['498(again),934', '370', '304,853,325', '394']
2828
2025-01-10 -> ['379,173,855(todo)', '815,871,593,1109', '833,950']
2929
2025-01-09 -> ['079,212(todo),362,849']
30-
2025-01-08 -> ['769']
30+
2025-01-08 -> ['769,817']
3131
2025-01-06 -> ['498(again),934']
32-
2025-01-05 -> ['769', '379,173,855(todo)', '370(todo)']
32+
2025-01-05 -> ['769,817', '379,173,855(todo)', '370(todo)']
3333
2025-01-04 -> ['079,212(todo),362,849', '369,311', '560,523', '53,210,207']
34-
2025-01-03 -> ['769', '498(again),934', '370', '34,767', '444']
35-
2025-01-02 -> ['769', '379,173,855(todo)', '815,871,593,1109', '1188,130,855(again)']
36-
2025-01-01 -> ['769', '498(again),934', '079,212(todo),362,849']
34+
2025-01-03 -> ['769,817', '498(again),934', '370', '34,767', '444']
35+
2025-01-02 -> ['769,817', '379,173,855(todo)', '815,871,593,1109', '1188,130,855(again)']
36+
2025-01-01 -> ['769,817', '498(again),934', '079,212(todo),362,849']
3737
2024-12-31 -> ['498(again),934', '379,173,855(todo)']
3838
2024-12-30 -> ['498(again),934', '379,173,855(todo)', '079,212(todo),362,849', '369,311', '722,380']
3939
2024-12-29 -> ['379,173,855(todo)', '079,212(todo),362,849', '370', '304,853,325', '33,81']
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
package LeetCodeJava.LinkedList;
2+
3+
// https://leetcode.com/problems/linked-list-components/description/
4+
5+
import LeetCodeJava.DataStructure.ListNode;
6+
7+
import java.util.ArrayList;
8+
import java.util.HashSet;
9+
import java.util.List;
10+
import java.util.Set;
11+
12+
/**
13+
* 817. Linked List Components
14+
* Solved
15+
* Medium
16+
* Topics
17+
* Companies
18+
* You are given the head of a linked list containing unique integer values and an integer array nums that is a subset of the linked list values.
19+
*
20+
* Return the number of connected components in nums where two values are connected if they appear consecutively in the linked list.
21+
*
22+
*
23+
*
24+
* Example 1:
25+
*
26+
*
27+
* Input: head = [0,1,2,3], nums = [0,1,3]
28+
* Output: 2
29+
* Explanation: 0 and 1 are connected, so [0, 1] and [3] are the two connected components.
30+
* Example 2:
31+
*
32+
*
33+
* Input: head = [0,1,2,3,4], nums = [0,3,1,4]
34+
* Output: 2
35+
* Explanation: 0 and 1 are connected, 3 and 4 are connected, so [0, 1] and [3, 4] are the two connected components.
36+
*
37+
*
38+
* Constraints:
39+
*
40+
* The number of nodes in the linked list is n.
41+
* 1 <= n <= 104
42+
* 0 <= Node.val < n
43+
* All the values Node.val are unique.
44+
* 1 <= nums.length <= n
45+
* 0 <= nums[i] < n
46+
* All the values of nums are unique.
47+
*
48+
*/
49+
public class LinkedListComponents {
50+
/**
51+
* Definition for singly-linked list.
52+
* public class ListNode {
53+
* int val;
54+
* ListNode next;
55+
* ListNode() {}
56+
* ListNode(int val) { this.val = val; }
57+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
58+
* }
59+
*/
60+
// V0
61+
// TODO : fix
62+
// public int numComponents(ListNode head, int[] nums) {
63+
//
64+
// // edge
65+
// if (head.next == null && nums.length == 1){
66+
// if(head.val == nums[0]){
67+
// return 1;
68+
// }
69+
// return 0;
70+
// }
71+
//
72+
// // ListNode -> list
73+
// List<Integer> head_list = new ArrayList<>();
74+
// List<Integer> nums_list = new ArrayList<>();
75+
//
76+
// for(int i = 0; i < nums.length; i++){
77+
// nums_list.add(nums[i]);
78+
// }
79+
//
80+
// // sort nums_list
81+
// nums_list.sort(Integer::compareTo);
82+
//
83+
// while(head != null){
84+
// head_list.add(head.val);
85+
// head = head.next;
86+
// }
87+
//
88+
// System.out.println(">>> nums_list = " + nums_list);
89+
// System.out.println(">>> head_list = " + head_list);
90+
//
91+
// int cnt = 0;
92+
// boolean prevInNums = false;
93+
// // 2 pointers
94+
// for(int i = 0; i < head_list.size()-1; i++){
95+
// List<Integer> tmp = new ArrayList<>();
96+
// for(int j = i; j < head_list.size(); j++){
97+
// tmp.add(head_list.get(j));
98+
// if(!nums_list.contains(tmp)){
99+
// if (!tmp.isEmpty()){
100+
// cnt += 1;
101+
// }
102+
// break;
103+
// }
104+
// }
105+
// }
106+
//
107+
// if(prevInNums){
108+
// cnt += 1;
109+
// }
110+
//
111+
// return cnt;
112+
// }
113+
114+
// V1
115+
// IDEA: set, linkedlist (gpt)
116+
public int numComponents_1(ListNode head, int[] nums) {
117+
// Convert nums array to a HashSet for O(1) lookups
118+
Set<Integer> numsSet = new HashSet<>();
119+
for (int num : nums) {
120+
numsSet.add(num);
121+
}
122+
123+
int count = 0;
124+
boolean inComponent = false;
125+
126+
// Traverse the linked list
127+
while (head != null) {
128+
if (numsSet.contains(head.val)) {
129+
// Start a new component if not already in one
130+
if (!inComponent) {
131+
count++;
132+
inComponent = true;
133+
}
134+
} else {
135+
// End the current component
136+
inComponent = false;
137+
}
138+
head = head.next;
139+
}
140+
141+
return count;
142+
}
143+
144+
// V2
145+
// https://leetcode.com/problems/linked-list-components/editorial/
146+
// IDEA: GROUPING
147+
public int numComponents_2(ListNode head, int[] G) {
148+
Set<Integer> Gset = new HashSet();
149+
for (int x : G)
150+
Gset.add(x);
151+
152+
ListNode cur = head;
153+
int ans = 0;
154+
155+
while (cur != null) {
156+
if (Gset.contains(cur.val) &&
157+
(cur.next == null || !Gset.contains(cur.next.val)))
158+
ans++;
159+
cur = cur.next;
160+
}
161+
162+
return ans;
163+
}
164+
165+
}

leetcode_java/src/main/java/dev/workspace6.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,4 +1209,59 @@ public int maxChunksToSorted(int[] arr) {
12091209
return res;
12101210
}
12111211

1212+
// LC 817
1213+
// https://leetcode.com/problems/linked-list-components/
1214+
// 4,15 pm - 4.40 pm
1215+
public int numComponents(ListNode head, int[] nums) {
1216+
1217+
// edge
1218+
if (head.next == null && nums.length == 1){
1219+
if(head.val == nums[0]){
1220+
return 1;
1221+
}
1222+
return 0;
1223+
}
1224+
1225+
// ListNode -> list
1226+
List<Integer> head_list = new ArrayList<>();
1227+
List<Integer> nums_list = new ArrayList<>();
1228+
1229+
for(int i = 0; i < nums.length; i++){
1230+
nums_list.add(nums[i]);
1231+
}
1232+
1233+
// sort nums_list
1234+
nums_list.sort(Integer::compareTo);
1235+
1236+
while(head != null){
1237+
head_list.add(head.val);
1238+
head = head.next;
1239+
}
1240+
1241+
System.out.println(">>> nums_list = " + nums_list);
1242+
System.out.println(">>> head_list = " + head_list);
1243+
1244+
int cnt = 0;
1245+
boolean prevInNums = false;
1246+
// 2 pointers
1247+
for(int i = 0; i < head_list.size()-1; i++){
1248+
List<Integer> tmp = new ArrayList<>();
1249+
for(int j = i; j < head_list.size(); j++){
1250+
tmp.add(head_list.get(j));
1251+
if(!nums_list.contains(tmp)){
1252+
if (!tmp.isEmpty()){
1253+
cnt += 1;
1254+
}
1255+
break;
1256+
}
1257+
}
1258+
}
1259+
1260+
if(prevInNums){
1261+
cnt += 1;
1262+
}
1263+
1264+
return cnt;
1265+
}
1266+
12121267
}

0 commit comments

Comments
 (0)