Skip to content

Commit 37b21b4

Browse files
committed
418_Sentence_Screen_Fitting
1 parent ae1a296 commit 37b21b4

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,7 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
781781
|57| **[338. Counting Bits](https://tinyurl.com/y5ly38nj)** | [Python](https://tinyurl.com/wu6rdaw/338_Counting_Bits.py), [Swift](https://tinyurl.com/wuja3c4/338_Counting_Bits.swift)| | Medium | --- |
782782
|58| **[1269. Number of Ways to Stay in the Same Place After Some Steps](https://tinyurl.com/y43jo8j3)** | [Python](https://tinyurl.com/wu6rdaw/1269_Number_of_Ways_to_Stay_in_the_Same_Place_After_Some_Steps.py), [Swift](https://tinyurl.com/wuja3c4/1269_Number_of_Ways_to_Stay_in_the_Same_Place_After_Some_Steps.swift)| [Art 1](https://tinyurl.com/yynv9xe2) | Hard | Loved the problem. Very versatile |
783783
|59| **[1277. Count Square Submatrices with All Ones](https://tinyurl.com/y6a82a9r)** | [Python](https://tinyurl.com/wu6rdaw/1277_Count_Square_Submatrices_with_All_Ones.py), [Swift](https://tinyurl.com/wuja3c4/1277_Count_Square_Submatrices_with_All_Ones.swift)| [Must](https://tinyurl.com/y4sa8zgk) | Medium | --- |
784+
|59| [418. Sentence Screen Fitting](https://tinyurl.com/yhtu7lld) | [Swift](https://tinyurl.com/wuja3c4/418_Sentence_Screen_Fitting.swift)| [Art 1](https://tinyurl.com/yh4xt84c) | Medium | --- |
784785

785786

786787
</p>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Naive solution. O(row*col). TLE
2+
class Solution {
3+
func wordsTyping(_ sentence: [String], _ rows: Int, _ cols: Int) -> Int {
4+
var (count, currentWordIndex) = (0, 0)
5+
for _ in 0..<rows {
6+
var col = -1
7+
while col < cols {
8+
col += Array(sentence[currentWordIndex]).count
9+
if col < cols {
10+
col += 1
11+
currentWordIndex += 1
12+
if currentWordIndex >= sentence.count {
13+
currentWordIndex = 0
14+
count += 1
15+
}
16+
}
17+
}
18+
}
19+
return count
20+
}
21+
}
22+
23+
// DP. O(row*sentenceCount)
24+
class Solution {
25+
func wordsTyping(_ sentence: [String], _ rows: Int, _ cols: Int) -> Int {
26+
for word in sentence {
27+
if word.count > cols {
28+
return 0
29+
}
30+
}
31+
32+
var memo = [Int:(Int, Int)]() // CurrentWordIndex: (SentenceCountInCurrentRow, StartingWordIndexOfNextRow)
33+
var (totalSentenceCount, currentRowStartWordIndex) = (0, 0)
34+
for _ in 0..<rows {
35+
if let (currentSentenceCount, nextRowStartWordIndex) = memo[currentRowStartWordIndex] {
36+
totalSentenceCount += currentSentenceCount
37+
currentRowStartWordIndex = nextRowStartWordIndex
38+
} else {
39+
var (currentCol, currentRowSentenceCount, currentIndex) = (0, 0, currentRowStartWordIndex)
40+
while currentCol < cols {
41+
let wordLen = Array(sentence[currentIndex]).count
42+
if currentCol + wordLen <= cols {
43+
currentCol += (wordLen + 1)
44+
currentIndex = (currentIndex + 1) % sentence.count
45+
if currentIndex == 0 {
46+
currentRowSentenceCount += 1
47+
}
48+
} else {
49+
break
50+
}
51+
}
52+
totalSentenceCount += currentRowSentenceCount
53+
memo[currentRowStartWordIndex] = (currentRowSentenceCount, currentIndex)
54+
currentRowStartWordIndex = currentIndex
55+
}
56+
}
57+
58+
return totalSentenceCount
59+
}
60+
}

0 commit comments

Comments
 (0)