Skip to content

Commit ec142f0

Browse files
committed
[LC1152][Brute Force][Java]
Just follow what the problem description want us to do.
1 parent 602d05b commit ec142f0

File tree

6 files changed

+150
-1
lines changed

6 files changed

+150
-1
lines changed

docs/source/problems/leetcode.rst

+20-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,26 @@ Let's consider a few examples.
156156
Implementation wise, make sure the direction change is calculated correctly. For example, when the robot change from facing north to facing west. The direction vector changes
157157
from :math:`\langle 0,1 \rangle` to :math:`\langle -1,0 \rangle`. If the direction vector is :math:`\langle vec_x, vec_y \rangle`, then after the direction change, the direction vector
158158
becomes :math:`\langle -vec_y, vec_x \rangle`.
159-
159+
160+
************************************
161+
1152. AnalyzeUserWebsiteVisitPattern
162+
************************************
163+
164+
The brute force solution is sufficient for this problem. A few remarks:
165+
166+
- We can use the following nested loops to generate all possible list of length 3 following the same ordering from a given list::
167+
168+
for (int i = 0; i < list.size(); i++) {
169+
for (int j = i + 1; j < list.size(); j++) {
170+
for (int k = j + 1; k < list.size(); k++) {
171+
172+
In general, to generate all possible length :math:`k`, we need :math:`k` nested loops.
173+
174+
- Be alert the case of "visit the same 3-seq in one user". That is , one user may visit the same pattern multiple times.
175+
176+
- We don't have to sort timestamps and then username and website following the ordering of timestamp at the very beginning.
177+
We can sort websites by timestamp for each individual user, e.g., ``list.sort(Comparator.comparingInt(a -> a.time));``
178+
160179

161180
******************************************************
162181
1481. Least Number of Unique Integers after K Removals

docs/source/topics/dp.rst

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.. _dp.rst:
2+
3+
###################
4+
Dynamic Programming
5+
###################
6+
7+
- :ref:`problems/leetcode:53. Maximum Subarray`

docs/source/topics/java.rst

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ Array
2929
- `extend each list inside a 2D list with one more element using lambda <https://github.com/xxks-kkk/shuati/blob/914af3077ebbbd55fe76fd2cea5aa667ebb54e4a/java/java-leetcode/src/main/java/ThreeSum.java#L50>`__.
3030
An example is: Given ``[[1,2],[3,4]]``, we add ``5`` to each list and obtain ``[[1,2,5],[3,4,5]]``.
3131

32+
- `create List<String> from String[] <https://github.com/xxks-kkk/shuati/blob/74bc965b4f491e6ad81fc3afc3ea3748352b1a56/java/java-leetcode/src/main/java/AnalyzeUserWebsiteVisitPattern.java#L51-L53>`__
33+
3234
========
3335
Hash Map
3436
========

docs/source/topics/two-pointers.rst

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.. _two-pointers.rst:
2+
3+
############
4+
Two Pointers
5+
############
6+
7+
- :ref:`problems/leetcode:15. 3Sum`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import java.util.ArrayList;
2+
import java.util.Collections;
3+
import java.util.Comparator;
4+
import java.util.HashMap;
5+
import java.util.HashSet;
6+
import java.util.List;
7+
import java.util.Map;
8+
import java.util.Objects;
9+
import java.util.Set;
10+
11+
// LC-1152
12+
public class AnalyzeUserWebsiteVisitPattern
13+
{
14+
public List<String> mostVisitedPattern(String[] username, int[] timestamp, String[] website)
15+
{
16+
Map<String, List<Pair>> map = new HashMap<>();
17+
int n = username.length;
18+
// collect the website info for every user, key: username, value: (timestamp, website)
19+
for (int i = 0; i < n; i++) {
20+
map.putIfAbsent(username[i], new ArrayList<>());
21+
map.get(username[i]).add(new Pair(timestamp[i], website[i]));
22+
}
23+
// count map to record every 3 combination occurring time for the different user.
24+
Map<String, Integer> count = new HashMap<>();
25+
String res = "";
26+
for (String key : map.keySet()) {
27+
// this set is to avoid visit the same 3-seq in one user
28+
Set<String> set = new HashSet<>();
29+
List<Pair> list = map.get(key);
30+
// sort by time (this avoids sort timestamp and then sort username, website following timestamp)
31+
list.sort(Comparator.comparingInt(a -> a.time));
32+
// brutal force O(N ^ 3)
33+
for (int i = 0; i < list.size(); i++) {
34+
for (int j = i + 1; j < list.size(); j++) {
35+
for (int k = j + 1; k < list.size(); k++) {
36+
String str = list.get(i).web + " " + list.get(j).web + " " + list.get(k).web;
37+
if (!set.contains(str)) {
38+
count.put(str, count.getOrDefault(str, 0) + 1);
39+
set.add(str);
40+
}
41+
if (res.equals("") ||
42+
count.get(res) < count.get(str) ||
43+
(Objects.equals(count.get(res), count.get(str)) && res.compareTo(str) > 0) /* make sure the right lexi order */) {
44+
res = str;
45+
}
46+
}
47+
}
48+
}
49+
}
50+
// grab the right answer
51+
String[] r = res.split(" ");
52+
List<String> result = new ArrayList<>();
53+
Collections.addAll(result, r);
54+
return result;
55+
}
56+
57+
static class Pair
58+
{
59+
int time;
60+
String web;
61+
62+
public Pair(int time, String web)
63+
{
64+
this.time = time;
65+
this.web = web;
66+
}
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import org.junit.jupiter.api.BeforeAll;
2+
import org.junit.jupiter.api.TestInstance;
3+
import org.junit.jupiter.params.ParameterizedTest;
4+
import org.junit.jupiter.params.provider.MethodSource;
5+
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
11+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
12+
public class TestAnalyzeUserWebsiteVisitPattern
13+
{
14+
private AnalyzeUserWebsiteVisitPattern analyzeUserWebsiteVisitPattern;
15+
16+
@BeforeAll
17+
public void setUp()
18+
{
19+
analyzeUserWebsiteVisitPattern = new AnalyzeUserWebsiteVisitPattern();
20+
}
21+
22+
private static Object[][] testAnalyzeUserWebsiteVisitPatternDataProvider()
23+
{
24+
return new Object[][] {
25+
{new String[] {"joe", "joe", "joe", "james", "james", "james", "james", "mary", "mary", "mary"},
26+
new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
27+
new String[] {"home", "about", "career", "home", "cart", "maps", "home", "home", "about", "career"},
28+
Arrays.asList("home", "about", "career")},
29+
{new String[] {"ua", "ua", "ua", "ub", "ub", "ub"},
30+
new int[] {1, 2, 3, 4, 5, 6},
31+
new String[] {"a", "b", "a", "a", "b", "c"},
32+
Arrays.asList("a", "b", "a")}
33+
};
34+
}
35+
36+
@ParameterizedTest
37+
@MethodSource("testAnalyzeUserWebsiteVisitPatternDataProvider")
38+
public void testLeastNumberOfUniqueIntegersAfterKRemovals(String[] username, int[] timestamp, String[] website, List<String> expectation)
39+
{
40+
List<String> ret = analyzeUserWebsiteVisitPattern.mostVisitedPattern(username, timestamp, website);
41+
assertEquals(expectation.size(), ret.size());
42+
for (int i = 0; i < ret.size(); ++i) {
43+
assertEquals(expectation.get(i), ret.get(i));
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)